Bitcoin Core  27.99.0
P2P Digital Currency
spanparsing.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_UTIL_SPANPARSING_H
6 #define BITCOIN_UTIL_SPANPARSING_H
7 
8 #include <span.h>
9 
10 #include <string>
11 #include <string_view>
12 #include <vector>
13 
14 namespace spanparsing {
15 
21 bool Const(const std::string& str, Span<const char>& sp);
22 
29 bool Func(const std::string& str, Span<const char>& sp);
30 
39 
47 template <typename T = Span<const char>>
48 std::vector<T> Split(const Span<const char>& sp, std::string_view separators)
49 {
50  std::vector<T> ret;
51  auto it = sp.begin();
52  auto start = it;
53  while (it != sp.end()) {
54  if (separators.find(*it) != std::string::npos) {
55  ret.emplace_back(start, it);
56  start = it + 1;
57  }
58  ++it;
59  }
60  ret.emplace_back(start, it);
61  return ret;
62 }
63 
71 template <typename T = Span<const char>>
72 std::vector<T> Split(const Span<const char>& sp, char sep)
73 {
74  return Split<T>(sp, std::string_view{&sep, 1});
75 }
76 
77 } // namespace spanparsing
78 
79 #endif // BITCOIN_UTIL_SPANPARSING_H
int ret
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:98
constexpr C * end() const noexcept
Definition: span.h:176
constexpr C * begin() const noexcept
Definition: span.h:175
Span< const char > Expr(Span< const char > &sp)
Extract the expression that sp begins with.
Definition: spanparsing.cpp:33
bool Const(const std::string &str, Span< const char > &sp)
Parse a constant.
Definition: spanparsing.cpp:15
bool Func(const std::string &str, Span< const char > &sp)
Parse a function call.
Definition: spanparsing.cpp:24
std::vector< T > Split(const Span< const char > &sp, std::string_view separators)
Split a string on any char found in separators, returning a vector.
Definition: spanparsing.h:48