diff options
author | Federico Igne <undyamon@disroot.org> | 2023-12-20 00:54:49 +0100 |
---|---|---|
committer | Federico Igne <undyamon@disroot.org> | 2023-12-20 00:54:49 +0100 |
commit | 95afbe5174be402b681cd98ea83cc063cd23f5c0 (patch) | |
tree | fe7790f346e99e4e661723b568ff2be3ba6f8891 /2023/include | |
parent | e3c9f0c73077de51167491ca1ba4d5ccba005435 (diff) | |
download | aoc-95afbe5174be402b681cd98ea83cc063cd23f5c0.tar.gz aoc-95afbe5174be402b681cd98ea83cc063cd23f5c0.zip |
Diffstat (limited to '2023/include')
-rw-r--r-- | 2023/include/util.h | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/2023/include/util.h b/2023/include/util.h index 3152719..100c9ed 100644 --- a/2023/include/util.h +++ b/2023/include/util.h | |||
@@ -7,6 +7,7 @@ | |||
7 | #include <string> | 7 | #include <string> |
8 | #include <string_view> | 8 | #include <string_view> |
9 | #include <type_traits> | 9 | #include <type_traits> |
10 | #include <vector> | ||
10 | 11 | ||
11 | namespace util | 12 | namespace util |
12 | { | 13 | { |
@@ -49,7 +50,7 @@ inline void trim(std::string_view& view) | |||
49 | * @param the callable object. | 50 | * @param the callable object. |
50 | */ | 51 | */ |
51 | template<const char* delim, typename Accumulate> | 52 | template<const char* delim, typename Accumulate> |
52 | void accumulate(std::string_view& view, Accumulate acc) | 53 | void accumulate(std::string_view view, Accumulate acc) |
53 | { | 54 | { |
54 | size_t from{}, to; | 55 | size_t from{}, to; |
55 | std::string elem; | 56 | std::string elem; |
@@ -68,6 +69,25 @@ void accumulate(std::string_view& view, Accumulate acc) | |||
68 | } | 69 | } |
69 | } | 70 | } |
70 | 71 | ||
72 | /** @brief Split a string view according to a delimiter. | ||
73 | * | ||
74 | * Specialized version of `util::accumulate` collecting the split | ||
75 | * `std::string_view` in a `std::vector`. | ||
76 | * | ||
77 | * @tparam delim the delimiter to split the string view. | ||
78 | * @param view the string view. | ||
79 | * | ||
80 | * @see util::accumulate; | ||
81 | */ | ||
82 | template<const char* delim> | ||
83 | std::vector<std::string_view> split(std::string_view view) | ||
84 | { | ||
85 | std::vector<std::string_view> v; | ||
86 | auto callback = [&v](const std::string_view& s) { v.push_back(s); }; | ||
87 | accumulate<delim>(view, callback); | ||
88 | return v; | ||
89 | } | ||
90 | |||
71 | /** @brief Discard element from stream by type. | 91 | /** @brief Discard element from stream by type. |
72 | * | 92 | * |
73 | * @tparam T the type of the element to discard. | 93 | * @tparam T the type of the element to discard. |