diff --git a/src/script/parsing.cpp b/src/script/parsing.cpp index 254ced6f0b0..dd79f67e58e 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 8986aa57d07..462a42e7b5e 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 05d2c914c6a..8016c2d8c56 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;