aboutsummaryrefslogtreecommitdiff
path: root/src/script/parsing.cpp
diff options
context:
space:
mode:
authorRyan Ofsky <ryan@ofsky.org>2023-12-06 15:58:47 -0500
committerRyan Ofsky <ryan@ofsky.org>2024-05-16 10:16:08 -0500
commit9bcce2608dd2515dc35a0f0866abc9d43903c795 (patch)
tree50931479f943d1c70bb472b815697f0f44dc8ad7 /src/script/parsing.cpp
parent6dd2ad47922694d2ab84bad4dac9dd442c5df617 (diff)
util: move spanparsing.h to script/parsing.h
Move miniscript / descriptor script parsing functions out of util library so they are not a dependency of the kernel. There are no changes to code or behavior.
Diffstat (limited to 'src/script/parsing.cpp')
-rw-r--r--src/script/parsing.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/script/parsing.cpp b/src/script/parsing.cpp
new file mode 100644
index 0000000000..3528ac9bfa
--- /dev/null
+++ b/src/script/parsing.cpp
@@ -0,0 +1,52 @@
+// Copyright (c) 2018-2022 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, Span<const char>& sp)
+{
+ if ((size_t)sp.size() >= str.size() && std::equal(str.begin(), str.end(), sp.begin())) {
+ sp = sp.subspan(str.size());
+ return true;
+ }
+ return false;
+}
+
+bool Func(const std::string& str, 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;
+}
+
+Span<const char> Expr(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;
+ }
+ Span<const char> ret = sp.first(it - sp.begin());
+ sp = sp.subspan(it - sp.begin());
+ return ret;
+}
+
+} // namespace script