1 // Copyright (c) 2018-2019 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 <vector>
12 
13 namespace spanparsing {
14 
15 /** Parse a constant.
16  *
17  * If sp's initial part matches str, sp is updated to skip that part, and true is returned.
18  * Otherwise sp is unmodified and false is returned.
19  */
20 bool Const(const std::string& str, Span<const char>& sp);
21 
22 /** Parse a function call.
23  *
24  * If sp's initial part matches str + "(", and sp ends with ")", sp is updated to be the
25  * section between the braces, and true is returned. Otherwise sp is unmodified and false
26  * is returned.
27  */
28 bool Func(const std::string& str, Span<const char>& sp);
29 
30 /** Extract the expression that sp begins with.
31  *
32  * This function will return the initial part of sp, up to (but not including) the first
33  * comma or closing brace, skipping ones that are surrounded by braces. So for example,
34  * for "foo(bar(1),2),3" the initial part "foo(bar(1),2)" will be returned. sp will be
35  * updated to skip the initial part that is returned.
36  */
37 Span<const char> Expr(Span<const char>& sp);
38 
39 /** Split a string on every instance of sep, returning a vector.
40  *
41  * If sep does not occur in sp, a singleton with the entirety of sp is returned.
42  *
43  * Note that this function does not care about braces, so splitting
44  * "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}.
45  */
46 std::vector<Span<const char>> Split(const Span<const char>& sp, char sep);
47 
48 } // namespace spanparsing
49 
50 #endif // BITCOIN_UTIL_SPANPARSING_H
51