@@ -1793,8 +1793,9 @@ inline NodeRef<Key> Parse(Span<const char> in, const Ctx& ctx)
1793
1793
// Get threshold
1794
1794
int next_comma = FindNextChar (in, ' ,' );
1795
1795
if (next_comma < 1 ) return false ;
1796
- int64_t k;
1797
- if (!ParseInt64 (std::string (in.begin (), in.begin () + next_comma), &k)) return false ;
1796
+ const auto k_to_integral{ToIntegral<int64_t >(std::string_view (in.begin (), next_comma))};
1797
+ if (!k_to_integral.has_value ()) return false ;
1798
+ const int64_t k{k_to_integral.value ()};
1798
1799
in = in.subspan (next_comma + 1 );
1799
1800
// Get keys. It is compatible for both compressed and x-only keys.
1800
1801
std::vector<Key> keys;
@@ -1948,35 +1949,33 @@ inline NodeRef<Key> Parse(Span<const char> in, const Ctx& ctx)
1948
1949
} else if (Const (" after(" , in)) {
1949
1950
int arg_size = FindNextChar (in, ' )' );
1950
1951
if (arg_size < 1 ) return {};
1951
- int64_t num;
1952
- if (!ParseInt64 (std::string (in.begin (), in.begin () + arg_size), &num)) return {};
1953
- if (num < 1 || num >= 0x80000000L ) return {};
1954
- constructed.push_back (MakeNodeRef<Key>(internal::NoDupCheck{}, ctx.MsContext (), Fragment::AFTER, num));
1952
+ const auto num{ToIntegral<int64_t >(std::string_view (in.begin (), arg_size))};
1953
+ if (!num.has_value () || *num < 1 || *num >= 0x80000000L ) return {};
1954
+ constructed.push_back (MakeNodeRef<Key>(internal::NoDupCheck{}, ctx.MsContext (), Fragment::AFTER, *num));
1955
1955
in = in.subspan (arg_size + 1 );
1956
- script_size += 1 + (num > 16 ) + (num > 0x7f ) + (num > 0x7fff ) + (num > 0x7fffff );
1956
+ script_size += 1 + (* num > 16 ) + (* num > 0x7f ) + (* num > 0x7fff ) + (* num > 0x7fffff );
1957
1957
} else if (Const (" older(" , in)) {
1958
1958
int arg_size = FindNextChar (in, ' )' );
1959
1959
if (arg_size < 1 ) return {};
1960
- int64_t num;
1961
- if (!ParseInt64 (std::string (in.begin (), in.begin () + arg_size), &num)) return {};
1962
- if (num < 1 || num >= 0x80000000L ) return {};
1963
- constructed.push_back (MakeNodeRef<Key>(internal::NoDupCheck{}, ctx.MsContext (), Fragment::OLDER, num));
1960
+ const auto num{ToIntegral<int64_t >(std::string_view (in.begin (), arg_size))};
1961
+ if (!num.has_value () || *num < 1 || *num >= 0x80000000L ) return {};
1962
+ constructed.push_back (MakeNodeRef<Key>(internal::NoDupCheck{}, ctx.MsContext (), Fragment::OLDER, *num));
1964
1963
in = in.subspan (arg_size + 1 );
1965
- script_size += 1 + (num > 16 ) + (num > 0x7f ) + (num > 0x7fff ) + (num > 0x7fffff );
1964
+ script_size += 1 + (* num > 16 ) + (* num > 0x7f ) + (* num > 0x7fff ) + (* num > 0x7fffff );
1966
1965
} else if (Const (" multi(" , in)) {
1967
1966
if (!parse_multi_exp (in, /* is_multi_a = */ false )) return {};
1968
1967
} else if (Const (" multi_a(" , in)) {
1969
1968
if (!parse_multi_exp (in, /* is_multi_a = */ true )) return {};
1970
1969
} else if (Const (" thresh(" , in)) {
1971
1970
int next_comma = FindNextChar (in, ' ,' );
1972
1971
if (next_comma < 1 ) return {};
1973
- if (! ParseInt64 ( std::string (in.begin (), in. begin () + next_comma), &k)) return { };
1974
- if (k < 1 ) return {};
1972
+ const auto k{ToIntegral< int64_t >( std::string_view (in.begin (), next_comma)) };
1973
+ if (!k. has_value () || * k < 1 ) return {};
1975
1974
in = in.subspan (next_comma + 1 );
1976
1975
// n = 1 here because we read the first WRAPPED_EXPR before reaching THRESH
1977
- to_parse.emplace_back (ParseContext::THRESH, 1 , k);
1976
+ to_parse.emplace_back (ParseContext::THRESH, 1 , * k);
1978
1977
to_parse.emplace_back (ParseContext::WRAPPED_EXPR, -1 , -1 );
1979
- script_size += 2 + (k > 16 ) + (k > 0x7f ) + (k > 0x7fff ) + (k > 0x7fffff );
1978
+ script_size += 2 + (* k > 16 ) + (* k > 0x7f ) + (* k > 0x7fff ) + (* k > 0x7fffff );
1980
1979
} else if (Const (" andor(" , in)) {
1981
1980
to_parse.emplace_back (ParseContext::ANDOR, -1 , -1 );
1982
1981
to_parse.emplace_back (ParseContext::CLOSE_BRACKET, -1 , -1 );
0 commit comments