Bitcoin Core 31.99.0
P2P Digital Currency
parsing.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-present 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 <script/parsing.h>
6
7#include <algorithm>
8#include <cstddef>
9#include <string>
10
11namespace script {
12
13bool Const(const std::string& str, std::span<const char>& sp, bool skip)
14{
15 if ((size_t)sp.size() >= str.size() && std::equal(str.begin(), str.end(), sp.begin())) {
16 if (skip) sp = sp.subspan(str.size());
17 return true;
18 }
19 return false;
20}
21
22bool Func(const std::string& str, std::span<const char>& sp)
23{
24 if ((size_t)sp.size() >= str.size() + 2 && sp[str.size()] == '(' && sp[sp.size() - 1] == ')' && std::equal(str.begin(), str.end(), sp.begin())) {
25 sp = sp.subspan(str.size() + 1, sp.size() - str.size() - 2);
26 return true;
27 }
28 return false;
29}
30
31std::span<const char> Expr(std::span<const char>& sp)
32{
33 int level = 0;
34 auto it = sp.begin();
35 while (it != sp.end()) {
36 if (*it == '(' || *it == '{') {
37 ++level;
38 } else if (level && (*it == ')' || *it == '}')) {
39 --level;
40 } else if (level == 0 && (*it == ')' || *it == '}' || *it == ',')) {
41 break;
42 }
43 ++it;
44 }
45 std::span<const char> ret = sp.first(it - sp.begin());
46 sp = sp.subspan(it - sp.begin());
47 return ret;
48}
49
50} // namespace script
int ret
std::span< const char > Expr(std::span< const char > &sp)
Extract the expression that sp begins with.
Definition: parsing.cpp:31
bool Func(const std::string &str, std::span< const char > &sp)
Parse a function call.
Definition: parsing.cpp:22
bool Const(const std::string &str, std::span< const char > &sp, bool skip)
Parse a constant.
Definition: parsing.cpp:13