diff options
author | Federico Igne <undyamon@disroot.org> | 2023-12-15 18:21:49 +0100 |
---|---|---|
committer | Federico Igne <undyamon@disroot.org> | 2023-12-15 18:21:49 +0100 |
commit | c23733d2882f8d0b47e3685b2ccdf899397511d8 (patch) | |
tree | 3194fb1c567a5149d9b4db195e4ea39c982397e7 | |
parent | eafa46252f2d41a86bf218b8af0460eeb5818eb9 (diff) | |
download | aoc-c23733d2882f8d0b47e3685b2ccdf899397511d8.tar.gz aoc-c23733d2882f8d0b47e3685b2ccdf899397511d8.zip |
feat(cpp): add 'accumulate' and 'trim' util functions
-rw-r--r-- | 2023/include/util.h | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/2023/include/util.h b/2023/include/util.h index 1b5f1c5..e396e3c 100644 --- a/2023/include/util.h +++ b/2023/include/util.h | |||
@@ -5,35 +5,67 @@ | |||
5 | #include <cstring> | 5 | #include <cstring> |
6 | #include <stdexcept> | 6 | #include <stdexcept> |
7 | #include <string> | 7 | #include <string> |
8 | #include <string_view> | ||
8 | 9 | ||
9 | namespace util | 10 | namespace util |
10 | { | 11 | { |
11 | 12 | ||
12 | /* TODO */ | 13 | namespace separators |
14 | { | ||
15 | |||
13 | constexpr char SPACE[] = " "; | 16 | constexpr char SPACE[] = " "; |
14 | constexpr char COLON[] = ":"; | 17 | constexpr char COLON[] = ":"; |
15 | constexpr char SEMICOLON[] = ";"; | 18 | constexpr char SEMICOLON[] = ";"; |
19 | constexpr char COMMA[] = ","; | ||
16 | 20 | ||
17 | inline void trim(std::string& str) | 21 | } |
22 | |||
23 | /** @brief Trims a string view. | ||
24 | * | ||
25 | * @param the string view. | ||
26 | */ | ||
27 | inline void trim(std::string_view& view) | ||
18 | { | 28 | { |
19 | str.erase(str.begin(), std::find_if_not(str.cbegin(), str.cend(), ::isspace)); | 29 | std::string_view spaces{ " \f\r\n\t\v" }; |
20 | str.erase(std::find_if_not(str.crbegin(), str.crend(), ::isspace).base(), str.end()); | 30 | if (auto n = view.find_first_not_of(spaces); n != view.npos) |
31 | { | ||
32 | view.remove_prefix(n); | ||
33 | } | ||
34 | if (auto n = view.find_last_not_of(spaces); n != view.npos) | ||
35 | { | ||
36 | view.remove_suffix(view.size() - n - 1); | ||
37 | } | ||
21 | } | 38 | } |
22 | 39 | ||
40 | /** @brief Split a string view according to a delimiter. | ||
41 | * | ||
42 | * This function applies a custom callable to all substring of a string | ||
43 | * view generated by splitting it according to a delimiter. | ||
44 | * | ||
45 | * @tparam delim the delimiter to split the string view. | ||
46 | * @tparam Accumulate a callable to call on each substring. | ||
47 | * @param view the string view. | ||
48 | * @param the callable object. | ||
49 | */ | ||
23 | template<const char* delim, typename Accumulate> | 50 | template<const char* delim, typename Accumulate> |
24 | void accumulate(const std::string& str, Accumulate acc) | 51 | void accumulate(std::string_view& view, Accumulate acc) |
25 | { | 52 | { |
26 | size_t pos{}; | 53 | size_t from{}, to; |
27 | std::string elem; | 54 | std::string elem; |
28 | 55 | ||
29 | while ((pos = str.find(delim)) != std::string::npos) { | 56 | while ((to = view.find(delim, from)) != std::string_view::npos) { |
30 | auto sub = str.substr(0, pos); | 57 | auto sub = view.substr(from, to - from); |
58 | trim(sub); | ||
59 | acc(sub); | ||
60 | from = to + std::strlen(delim); | ||
61 | } | ||
62 | auto sub = view.substr(from); | ||
63 | if (not sub.empty()) | ||
64 | { | ||
31 | trim(sub); | 65 | trim(sub); |
32 | acc(sub); | 66 | acc(sub); |
33 | pos += std::strlen(delim); | ||
34 | } | 67 | } |
35 | } | 68 | } |
36 | /* ---- */ | ||
37 | 69 | ||
38 | /** @brief Discard element from stream by type. | 70 | /** @brief Discard element from stream by type. |
39 | * | 71 | * |