mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-02-01 03:01:05 +00:00
string: add base argument for ToIntegral to operate on hexadecimal
This commit is contained in:
parent
0b0d9125c1
commit
4e300df712
@ -835,6 +835,39 @@ BOOST_AUTO_TEST_CASE(test_LocaleIndependentAtoi)
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("256"), 255U);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_ToIntegralHex)
|
||||
{
|
||||
std::optional<uint64_t> n;
|
||||
// Valid values
|
||||
n = ToIntegral<uint64_t>("1234", 16);
|
||||
BOOST_CHECK_EQUAL(*n, 0x1234);
|
||||
n = ToIntegral<uint64_t>("a", 16);
|
||||
BOOST_CHECK_EQUAL(*n, 0xA);
|
||||
n = ToIntegral<uint64_t>("0000000a", 16);
|
||||
BOOST_CHECK_EQUAL(*n, 0xA);
|
||||
n = ToIntegral<uint64_t>("100", 16);
|
||||
BOOST_CHECK_EQUAL(*n, 0x100);
|
||||
n = ToIntegral<uint64_t>("DEADbeef", 16);
|
||||
BOOST_CHECK_EQUAL(*n, 0xDEADbeef);
|
||||
n = ToIntegral<uint64_t>("FfFfFfFf", 16);
|
||||
BOOST_CHECK_EQUAL(*n, 0xFfFfFfFf);
|
||||
n = ToIntegral<uint64_t>("123456789", 16);
|
||||
BOOST_CHECK_EQUAL(*n, 0x123456789ULL);
|
||||
n = ToIntegral<uint64_t>("0", 16);
|
||||
BOOST_CHECK_EQUAL(*n, 0);
|
||||
n = ToIntegral<uint64_t>("FfFfFfFfFfFfFfFf", 16);
|
||||
BOOST_CHECK_EQUAL(*n, 0xFfFfFfFfFfFfFfFfULL);
|
||||
n = ToIntegral<int64_t>("-1", 16);
|
||||
BOOST_CHECK_EQUAL(*n, -1);
|
||||
// Invalid values
|
||||
BOOST_CHECK(!ToIntegral<uint64_t>("", 16));
|
||||
BOOST_CHECK(!ToIntegral<uint64_t>("-1", 16));
|
||||
BOOST_CHECK(!ToIntegral<uint64_t>("10 00", 16));
|
||||
BOOST_CHECK(!ToIntegral<uint64_t>("1 ", 16));
|
||||
BOOST_CHECK(!ToIntegral<uint64_t>("0xAB", 16));
|
||||
BOOST_CHECK(!ToIntegral<uint64_t>("FfFfFfFfFfFfFfFf0", 16));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_FormatParagraph)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");
|
||||
|
||||
@ -169,17 +169,18 @@ constexpr inline bool IsSpace(char c) noexcept {
|
||||
/**
|
||||
* Convert string to integral type T. Leading whitespace, a leading +, or any
|
||||
* trailing character fail the parsing. The required format expressed as regex
|
||||
* is `-?[0-9]+`. The minus sign is only permitted for signed integer types.
|
||||
* is `-?[0-9]+` by default (or `-?[0-9a-fA-F]+` if base = 16).
|
||||
* The minus sign is only permitted for signed integer types.
|
||||
*
|
||||
* @returns std::nullopt if the entire string could not be parsed, or if the
|
||||
* parsed value is not in the range representable by the type T.
|
||||
*/
|
||||
template <typename T>
|
||||
std::optional<T> ToIntegral(std::string_view str)
|
||||
std::optional<T> ToIntegral(std::string_view str, size_t base = 10)
|
||||
{
|
||||
static_assert(std::is_integral_v<T>);
|
||||
T result;
|
||||
const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result);
|
||||
const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result, base);
|
||||
if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user