11
11
12
12
#include < span.h>
13
13
#include < util/bip32.h>
14
+ #include < util/spanparsing.h>
14
15
#include < util/system.h>
15
16
#include < util/strencodings.h>
16
17
@@ -632,63 +633,6 @@ enum class ParseScriptContext {
632
633
P2WSH,
633
634
};
634
635
635
- /* * Parse a constant. If successful, sp is updated to skip the constant and return true. */
636
- bool Const (const std::string& str, Span<const char >& sp)
637
- {
638
- if ((size_t )sp.size () >= str.size () && std::equal (str.begin (), str.end (), sp.begin ())) {
639
- sp = sp.subspan (str.size ());
640
- return true ;
641
- }
642
- return false ;
643
- }
644
-
645
- /* * Parse a function call. If successful, sp is updated to be the function's argument(s). */
646
- bool Func (const std::string& str, Span<const char >& sp)
647
- {
648
- if ((size_t )sp.size () >= str.size () + 2 && sp[str.size ()] == ' (' && sp[sp.size () - 1 ] == ' )' && std::equal (str.begin (), str.end (), sp.begin ())) {
649
- sp = sp.subspan (str.size () + 1 , sp.size () - str.size () - 2 );
650
- return true ;
651
- }
652
- return false ;
653
- }
654
-
655
- /* * Return the expression that sp begins with, and update sp to skip it. */
656
- Span<const char > Expr (Span<const char >& sp)
657
- {
658
- int level = 0 ;
659
- auto it = sp.begin ();
660
- while (it != sp.end ()) {
661
- if (*it == ' (' ) {
662
- ++level;
663
- } else if (level && *it == ' )' ) {
664
- --level;
665
- } else if (level == 0 && (*it == ' )' || *it == ' ,' )) {
666
- break ;
667
- }
668
- ++it;
669
- }
670
- Span<const char > ret = sp.first (it - sp.begin ());
671
- sp = sp.subspan (it - sp.begin ());
672
- return ret;
673
- }
674
-
675
- /* * Split a string on every instance of sep, returning a vector. */
676
- std::vector<Span<const char >> Split (const Span<const char >& sp, char sep)
677
- {
678
- std::vector<Span<const char >> ret;
679
- auto it = sp.begin ();
680
- auto start = it;
681
- while (it != sp.end ()) {
682
- if (*it == sep) {
683
- ret.emplace_back (start, it);
684
- start = it + 1 ;
685
- }
686
- ++it;
687
- }
688
- ret.emplace_back (start, it);
689
- return ret;
690
- }
691
-
692
636
/* * Parse a key path, being passed a split list of elements (the first element is ignored). */
693
637
NODISCARD bool ParseKeyPath (const std::vector<Span<const char >>& split, KeyPath& out, std::string& error)
694
638
{
@@ -715,6 +659,8 @@ NODISCARD bool ParseKeyPath(const std::vector<Span<const char>>& split, KeyPath&
715
659
/* * Parse a public key that excludes origin information. */
716
660
std::unique_ptr<PubkeyProvider> ParsePubkeyInner (const Span<const char >& sp, bool permit_uncompressed, FlatSigningProvider& out, std::string& error)
717
661
{
662
+ using namespace spanparsing ;
663
+
718
664
auto split = Split (sp, ' /' );
719
665
std::string str (split[0 ].begin (), split[0 ].end ());
720
666
if (str.size () == 0 ) {
@@ -774,6 +720,8 @@ std::unique_ptr<PubkeyProvider> ParsePubkeyInner(const Span<const char>& sp, boo
774
720
/* * Parse a public key including origin information (if enabled). */
775
721
std::unique_ptr<PubkeyProvider> ParsePubkey (const Span<const char >& sp, bool permit_uncompressed, FlatSigningProvider& out, std::string& error)
776
722
{
723
+ using namespace spanparsing ;
724
+
777
725
auto origin_split = Split (sp, ' ]' );
778
726
if (origin_split.size () > 2 ) {
779
727
error = " Multiple ']' characters found for a single pubkey" ;
@@ -808,6 +756,8 @@ std::unique_ptr<PubkeyProvider> ParsePubkey(const Span<const char>& sp, bool per
808
756
/* * Parse a script in a particular context. */
809
757
std::unique_ptr<DescriptorImpl> ParseScript (Span<const char >& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error)
810
758
{
759
+ using namespace spanparsing ;
760
+
811
761
auto expr = Expr (sp);
812
762
if (Func (" pk" , expr)) {
813
763
auto pubkey = ParsePubkey (expr, ctx != ParseScriptContext::P2WSH, out, error);
@@ -1003,6 +953,8 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
1003
953
/* * Check a descriptor checksum, and update desc to be the checksum-less part. */
1004
954
bool CheckChecksum (Span<const char >& sp, bool require_checksum, std::string& error, std::string* out_checksum = nullptr )
1005
955
{
956
+ using namespace spanparsing ;
957
+
1006
958
auto check_split = Split (sp, ' #' );
1007
959
if (check_split.size () > 2 ) {
1008
960
error = " Multiple '#' symbols" ;
0 commit comments