Branch data Line data Source code
# 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 : : #include <util/spanparsing.h> # 6 : : # 7 : : #include <span.h> # 8 : : # 9 : : #include <string> # 10 : : #include <vector> # 11 : : # 12 : : namespace spanparsing { # 13 : : # 14 : : bool Const(const std::string& str, Span<const char>& sp) # 15 : 1373 : { # 16 [ + + ][ + + ]: 1373 : if ((size_t)sp.size() >= str.size() && std::equal(str.begin(), str.end(), sp.begin())) { # 17 : 1371 : sp = sp.subspan(str.size()); # 18 : 1371 : return true; # 19 : 1371 : } # 20 : 2 : return false; # 21 : 2 : } # 22 : : # 23 : : bool Func(const std::string& str, Span<const char>& sp) # 24 : 24255 : { # 25 [ + + ][ + + ]: 24255 : if ((size_t)sp.size() >= str.size() + 2 && sp[str.size()] == '(' && sp[sp.size() - 1] == ')' && std::equal(str.begin(), str.end(), sp.begin())) { # [ + - ][ + + ] # 26 : 3814 : sp = sp.subspan(str.size() + 1, sp.size() - str.size() - 2); # 27 : 3814 : return true; # 28 : 3814 : } # 29 : 20441 : return false; # 30 : 20441 : } # 31 : : # 32 : : Span<const char> Expr(Span<const char>& sp) # 33 : 5621 : { # 34 : 5621 : int level = 0; # 35 : 5621 : auto it = sp.begin(); # 36 [ + + ]: 720762 : while (it != sp.end()) { # 37 [ + + ]: 716536 : if (*it == '(') { # 38 : 5142 : ++level; # 39 [ + + ][ + + ]: 711394 : } else if (level && *it == ')') { # 40 : 5142 : --level; # 41 [ + + ][ + + ]: 706252 : } else if (level == 0 && (*it == ')' || *it == ',')) { # [ + + ] # 42 : 1395 : break; # 43 : 1395 : } # 44 : 715141 : ++it; # 45 : 715141 : } # 46 : 5621 : Span<const char> ret = sp.first(it - sp.begin()); # 47 : 5621 : sp = sp.subspan(it - sp.begin()); # 48 : 5621 : return ret; # 49 : 5621 : } # 50 : : # 51 : : std::vector<Span<const char>> Split(const Span<const char>& sp, char sep) # 52 : 10890 : { # 53 : 10890 : std::vector<Span<const char>> ret; # 54 : 10890 : auto it = sp.begin(); # 55 : 10890 : auto start = it; # 56 [ + + ]: 1177714 : while (it != sp.end()) { # 57 [ + + ]: 1166824 : if (*it == sep) { # 58 : 12283 : ret.emplace_back(start, it); # 59 : 12283 : start = it + 1; # 60 : 12283 : } # 61 : 1166824 : ++it; # 62 : 1166824 : } # 63 : 10890 : ret.emplace_back(start, it); # 64 : 10890 : return ret; # 65 : 10890 : } # 66 : : # 67 : : } // namespace spanparsing