From 88113125716c50ce4deb864041840d53a567554c Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Mon, 15 Jan 2024 17:08:47 -0500 Subject: [PATCH] script/parsing: Allow Const to not skip the found constant 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. --- src/script/parsing.cpp | 4 ++-- src/script/parsing.h | 4 ++-- src/test/util_tests.cpp | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/script/parsing.cpp b/src/script/parsing.cpp index 254ced6f0b..dd79f67e58 100644 --- a/src/script/parsing.cpp +++ b/src/script/parsing.cpp @@ -12,10 +12,10 @@ namespace script { -bool Const(const std::string& str, std::span& sp) +bool Const(const std::string& str, std::span& sp, bool skip) { if ((size_t)sp.size() >= str.size() && std::equal(str.begin(), str.end(), sp.begin())) { - sp = sp.subspan(str.size()); + if (skip) sp = sp.subspan(str.size()); return true; } return false; diff --git a/src/script/parsing.h b/src/script/parsing.h index 8986aa57d0..462a42e7b5 100644 --- a/src/script/parsing.h +++ b/src/script/parsing.h @@ -13,10 +13,10 @@ namespace script { /** Parse a constant. * - * If sp's initial part matches str, sp is updated to skip that part, and true is returned. + * If sp's initial part matches str, sp is optionally updated to skip that part, and true is returned. * Otherwise sp is unmodified and false is returned. */ -bool Const(const std::string& str, std::span& sp); +bool Const(const std::string& str, std::span& sp, bool skip = true); /** Parse a function call. * diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 05d2c914c6..8016c2d8c5 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -1137,13 +1137,24 @@ BOOST_AUTO_TEST_CASE(test_script_parsing) BOOST_CHECK(success); BOOST_CHECK_EQUAL(SpanToStr(sp), "MilkToastHoney"); + success = Const("Milk", sp, /*skip=*/false); + BOOST_CHECK(success); + BOOST_CHECK_EQUAL(SpanToStr(sp), "MilkToastHoney"); + success = Const("Milk", sp); BOOST_CHECK(success); BOOST_CHECK_EQUAL(SpanToStr(sp), "ToastHoney"); + success = Const("Bread", sp, /*skip=*/false); + BOOST_CHECK(!success); + success = Const("Bread", sp); BOOST_CHECK(!success); + success = Const("Toast", sp, /*skip=*/false); + BOOST_CHECK(success); + BOOST_CHECK_EQUAL(SpanToStr(sp), "ToastHoney"); + success = Const("Toast", sp); BOOST_CHECK(success); BOOST_CHECK_EQUAL(SpanToStr(sp), "Honey"); @@ -1151,10 +1162,13 @@ BOOST_AUTO_TEST_CASE(test_script_parsing) success = Const("Honeybadger", sp); BOOST_CHECK(!success); + success = Const("Honey", sp, /*skip=*/false); + BOOST_CHECK(success); + BOOST_CHECK_EQUAL(SpanToStr(sp), "Honey"); + success = Const("Honey", sp); BOOST_CHECK(success); BOOST_CHECK_EQUAL(SpanToStr(sp), ""); - // Func(...): parse a function call, update span to argument if successful input = "Foo(Bar(xy,z()))"; sp = input;