Bitcoin ABC 0.30.5
P2P Digital Currency
spanparsing.cpp
Go to the documentation of this file.
1// Copyright (c) 2018 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
12namespace spanparsing {
13
14bool Const(const std::string &str, Span<const char> &sp) {
15 if (size_t(sp.size()) >= str.size() &&
16 std::equal(str.begin(), str.end(), sp.begin())) {
17 sp = sp.subspan(str.size());
18 return true;
19 }
20 return false;
21}
22
23bool Func(const std::string &str, Span<const char> &sp) {
24 if (size_t(sp.size()) >= str.size() + 2 && sp[str.size()] == '(' &&
25 sp[sp.size() - 1] == ')' &&
26 std::equal(str.begin(), str.end(), sp.begin())) {
27 sp = sp.subspan(str.size() + 1, sp.size() - str.size() - 2);
28 return true;
29 }
30 return false;
31}
32
34 int level = 0;
35 auto it = sp.begin();
36 while (it != sp.end()) {
37 if (*it == '(') {
38 ++level;
39 } else if (level && *it == ')') {
40 --level;
41 } else if (level == 0 && (*it == ')' || *it == ',')) {
42 break;
43 }
44 ++it;
45 }
46 Span<const char> ret = sp.first(it - sp.begin());
47 sp = sp.subspan(it - sp.begin());
48 return ret;
49}
50
51} // namespace spanparsing
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
constexpr std::size_t size() const noexcept
Definition: span.h:209
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
Definition: span.h:218
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:227
constexpr C * begin() const noexcept
Definition: span.h:199
constexpr C * end() const noexcept
Definition: span.h:200
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:14
bool Func(const std::string &str, Span< const char > &sp)
Parse a function call.
Definition: spanparsing.cpp:23