From 7d8dfe2fafc703709891c37c7504f63e4e5dbcd0 Mon Sep 17 00:00:00 2001 From: Hodlinator <172445034+hodlinator@users.noreply.github.com> Date: Thu, 11 Jul 2024 22:38:43 +0200 Subject: [PATCH] refactor: Add base_blob::SetHex(std::string_view) Also consolidates other SetHex() overloads to call it. --- src/uint256.cpp | 34 ++++++++++++---------------------- src/uint256.h | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/uint256.cpp b/src/uint256.cpp index 7f81c3c448a9b9..1efcff51e7956a 100644 --- a/src/uint256.cpp +++ b/src/uint256.cpp @@ -18,39 +18,31 @@ std::string base_blob::GetHex() const } template -void base_blob::SetHex(const char* psz) +void base_blob::SetHex(std::string_view str) { std::fill(m_data.begin(), m_data.end(), 0); - // skip leading spaces - while (IsSpace(*psz)) - psz++; - - // skip 0x - if (psz[0] == '0' && ToLower(psz[1]) == 'x') - psz += 2; + str = util::LeftTrimStringView(str); + str = util::RemovePrefixView(str, "0x"); // hex string to uint size_t digits = 0; - while (::HexDigit(psz[digits]) != -1) - digits++; + for (const char c : str) { + if (::HexDigit(c) == -1) + break; + ++digits; + } unsigned char* p1 = m_data.data(); unsigned char* pend = p1 + WIDTH; while (digits > 0 && p1 < pend) { - *p1 = ::HexDigit(psz[--digits]); + *p1 = ::HexDigit(str[--digits]); if (digits > 0) { - *p1 |= ((unsigned char)::HexDigit(psz[--digits]) << 4); + *p1 |= ((unsigned char)::HexDigit(str[--digits]) << 4); p1++; } } } -template -void base_blob::SetHex(const std::string& str) -{ - SetHex(str.c_str()); -} - template std::string base_blob::ToString() const { @@ -60,14 +52,12 @@ std::string base_blob::ToString() const // Explicit instantiations for base_blob<160> template std::string base_blob<160>::GetHex() const; template std::string base_blob<160>::ToString() const; -template void base_blob<160>::SetHex(const char*); -template void base_blob<160>::SetHex(const std::string&); +template void base_blob<160>::SetHex(std::string_view); // Explicit instantiations for base_blob<256> template std::string base_blob<256>::GetHex() const; template std::string base_blob<256>::ToString() const; -template void base_blob<256>::SetHex(const char*); -template void base_blob<256>::SetHex(const std::string&); +template void base_blob<256>::SetHex(std::string_view); const uint256 uint256::ZERO(0); const uint256 uint256::ONE(1); diff --git a/src/uint256.h b/src/uint256.h index d35b3a66fa59f0..6e6b387e8b5018 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -58,8 +58,9 @@ class base_blob friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; } std::string GetHex() const; - void SetHex(const char* psz); - void SetHex(const std::string& str); + void SetHex(std::string_view str); + inline void SetHex(const char* psz) { SetHex(std::string_view{psz}); } + inline void SetHex(const std::string& str) { SetHex(std::string_view{str}); } std::string ToString() const; constexpr const unsigned char* data() const { return m_data.data(); } @@ -132,5 +133,15 @@ inline uint256 uint256S(const std::string& str) rv.SetHex(str); return rv; } +/* uint256 from std::string_view. + * This is a separate function because the constructor uint256(std::string_view str) can result + * in dangerously catching uint256(0) via std::string_view(const char*). + */ +inline uint256 uint256S(std::string_view str) +{ + uint256 rv; + rv.SetHex(str); + return rv; +} #endif // BITCOIN_UINT256_H