diff options
author | Martin Leitner-Ankerl <martin.ankerl@gmail.com> | 2022-05-02 21:44:09 +0200 |
---|---|---|
committer | Martin Leitner-Ankerl <martin.ankerl@gmail.com> | 2022-05-04 07:34:47 +0200 |
commit | b7ab9db545492927b774912e53aeb834a590621f (patch) | |
tree | 94b912439bd1e205a15e1a39438ee8861e3e057d /src/util/spanparsing.h | |
parent | 12455acca2c3adf5c88ae9c1a02a7c192fe0f53b (diff) | |
download | bitcoin-b7ab9db545492927b774912e53aeb834a590621f.tar.xz |
Extend Split to work with multiple separators
Diffstat (limited to 'src/util/spanparsing.h')
-rw-r--r-- | src/util/spanparsing.h | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/src/util/spanparsing.h b/src/util/spanparsing.h index ebec8714a7..51795271de 100644 --- a/src/util/spanparsing.h +++ b/src/util/spanparsing.h @@ -8,6 +8,7 @@ #include <span.h> #include <string> +#include <string_view> #include <vector> namespace spanparsing { @@ -36,7 +37,7 @@ bool Func(const std::string& str, Span<const char>& sp); */ Span<const char> Expr(Span<const char>& sp); -/** Split a string on every instance of sep, returning a vector. +/** Split a string on any char found in separators, returning a vector. * * If sep does not occur in sp, a singleton with the entirety of sp is returned. * @@ -44,13 +45,13 @@ Span<const char> Expr(Span<const char>& sp); * "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}. */ template <typename T = Span<const char>> -std::vector<T> Split(const Span<const char>& sp, char sep) +std::vector<T> Split(const Span<const char>& sp, std::string_view separators) { std::vector<T> ret; auto it = sp.begin(); auto start = it; while (it != sp.end()) { - if (*it == sep) { + if (separators.find(*it) != std::string::npos) { ret.emplace_back(start, it); start = it + 1; } @@ -60,6 +61,19 @@ std::vector<T> Split(const Span<const char>& sp, char sep) return ret; } +/** Split a string on every instance of sep, returning a vector. + * + * If sep does not occur in sp, a singleton with the entirety of sp is returned. + * + * Note that this function does not care about braces, so splitting + * "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}. + */ +template <typename T = Span<const char>> +std::vector<T> Split(const Span<const char>& sp, char sep) +{ + return Split<T>(sp, std::string_view{&sep, 1}); +} + } // namespace spanparsing #endif // BITCOIN_UTIL_SPANPARSING_H |