mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-20 07:09:15 +01:00
When parsing a descriptor, it is useful to be able to check whether a
string begins with a substring without consuming that substring as
another function such as Func() will be used later which requires that
substring to be present at the beginning.
Specifically, for MuSig2, this modified Const will be used to determine
whether a an expression begins with "musig(" before a subsequent
Func("musig", ...) is used.
53 lines
1.4 KiB
C++
53 lines
1.4 KiB
C++
// Copyright (c) 2018-present The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <script/parsing.h>
|
|
|
|
#include <span.h>
|
|
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <string>
|
|
|
|
namespace script {
|
|
|
|
bool Const(const std::string& str, std::span<const char>& sp, bool skip)
|
|
{
|
|
if ((size_t)sp.size() >= str.size() && std::equal(str.begin(), str.end(), sp.begin())) {
|
|
if (skip) sp = sp.subspan(str.size());
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool Func(const std::string& str, std::span<const char>& sp)
|
|
{
|
|
if ((size_t)sp.size() >= str.size() + 2 && sp[str.size()] == '(' && sp[sp.size() - 1] == ')' && std::equal(str.begin(), str.end(), sp.begin())) {
|
|
sp = sp.subspan(str.size() + 1, sp.size() - str.size() - 2);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::span<const char> Expr(std::span<const char>& sp)
|
|
{
|
|
int level = 0;
|
|
auto it = sp.begin();
|
|
while (it != sp.end()) {
|
|
if (*it == '(' || *it == '{') {
|
|
++level;
|
|
} else if (level && (*it == ')' || *it == '}')) {
|
|
--level;
|
|
} else if (level == 0 && (*it == ')' || *it == '}' || *it == ',')) {
|
|
break;
|
|
}
|
|
++it;
|
|
}
|
|
std::span<const char> ret = sp.first(it - sp.begin());
|
|
sp = sp.subspan(it - sp.begin());
|
|
return ret;
|
|
}
|
|
|
|
} // namespace script
|