Coverage Report

Created: 2025-10-29 16:48

/root/bitcoin/src/script/parsing.cpp
Line
Count
Source
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 <span.h>
8
9
#include <algorithm>
10
#include <cstddef>
11
#include <string>
12
13
namespace script {
14
15
bool Const(const std::string& str, std::span<const char>& sp, bool skip)
16
244k
{
17
244k
    if ((size_t)sp.size() >= str.size() && std::equal(str.begin(), str.end(), sp.begin())) {
18
118k
        if (skip) sp = sp.subspan(str.size());
19
118k
        return true;
20
118k
    }
21
126k
    return false;
22
244k
}
23
24
bool Func(const std::string& str, std::span<const char>& sp)
25
341k
{
26
341k
    if ((size_t)sp.size() >= str.size() + 2 && sp[str.size()] == '(' && sp[sp.size() - 1] == ')' && std::equal(str.begin(), str.end(), sp.begin())) {
27
45.4k
        sp = sp.subspan(str.size() + 1, sp.size() - str.size() - 2);
28
45.4k
        return true;
29
45.4k
    }
30
296k
    return false;
31
341k
}
32
33
std::span<const char> Expr(std::span<const char>& sp)
34
180k
{
35
180k
    int level = 0;
36
180k
    auto it = sp.begin();
37
25.9M
    while (it != sp.end()) {
38
25.8M
        if (*it == '(' || *it == '{') {
39
63.4k
            ++level;
40
25.8M
        } else if (level && (*it == ')' || *it == '}')) {
41
63.4k
            --level;
42
25.7M
        } else if (level == 0 && (*it == ')' || *it == '}' || *it == ',')) {
43
118k
            break;
44
118k
        }
45
25.7M
        ++it;
46
25.7M
    }
47
180k
    std::span<const char> ret = sp.first(it - sp.begin());
48
180k
    sp = sp.subspan(it - sp.begin());
49
180k
    return ret;
50
180k
}
51
52
} // namespace script