mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-06 14:19:59 -05:00
refactor: Change base_blob::SetHex() to take std::string_view
Clarify that hex strings are parsed as little-endian.
This commit is contained in:
parent
2f5577dc2e
commit
01e314ce0a
3 changed files with 25 additions and 46 deletions
|
@ -58,13 +58,8 @@ static std::string ArrayToString(const unsigned char A[], unsigned int width)
|
||||||
return Stream.str();
|
return Stream.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline uint160 uint160S(const char *str)
|
// Input is treated as little-endian.
|
||||||
{
|
inline uint160 uint160S(std::string_view str)
|
||||||
uint160 rv;
|
|
||||||
rv.SetHex(str);
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
inline uint160 uint160S(const std::string& str)
|
|
||||||
{
|
{
|
||||||
uint160 rv;
|
uint160 rv;
|
||||||
rv.SetHex(str);
|
rv.SetHex(str);
|
||||||
|
@ -156,6 +151,10 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
|
||||||
BOOST_CHECK_LT(OneS, MaxS);
|
BOOST_CHECK_LT(OneS, MaxS);
|
||||||
BOOST_CHECK_LT(R1S, MaxS);
|
BOOST_CHECK_LT(R1S, MaxS);
|
||||||
BOOST_CHECK_LT(R2S, MaxS);
|
BOOST_CHECK_LT(R2S, MaxS);
|
||||||
|
|
||||||
|
// Verify hex strings are little-endian
|
||||||
|
BOOST_CHECK_LT(uint256S("2000000000000000000000000000000000000000000000000000000000000001"),
|
||||||
|
uint256S("1000000000000000000000000000000000000000000000000000000000000002"));
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
|
BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
|
||||||
|
|
|
@ -18,39 +18,31 @@ std::string base_blob<BITS>::GetHex() const
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int BITS>
|
template <unsigned int BITS>
|
||||||
void base_blob<BITS>::SetHex(const char* psz)
|
void base_blob<BITS>::SetHex(const std::string_view str)
|
||||||
{
|
{
|
||||||
std::fill(m_data.begin(), m_data.end(), 0);
|
std::fill(m_data.begin(), m_data.end(), 0);
|
||||||
|
|
||||||
// skip leading spaces
|
const auto trimmed = util::RemovePrefixView(util::TrimStringView(str), "0x");
|
||||||
while (IsSpace(*psz))
|
|
||||||
psz++;
|
|
||||||
|
|
||||||
// skip 0x
|
// Note: if we are passed a greater number of digits than would fit as bytes
|
||||||
if (psz[0] == '0' && ToLower(psz[1]) == 'x')
|
// in m_data, we will be discarding the leftmost ones.
|
||||||
psz += 2;
|
// str="12bc" in a WIDTH=1 m_data => m_data[] == "\0xbc", not "0x12".
|
||||||
|
|
||||||
// hex string to uint
|
|
||||||
size_t digits = 0;
|
size_t digits = 0;
|
||||||
while (::HexDigit(psz[digits]) != -1)
|
for (const char c : trimmed) {
|
||||||
digits++;
|
if (::HexDigit(c) == -1) break;
|
||||||
|
++digits;
|
||||||
|
}
|
||||||
unsigned char* p1 = m_data.data();
|
unsigned char* p1 = m_data.data();
|
||||||
unsigned char* pend = p1 + WIDTH;
|
unsigned char* pend = p1 + WIDTH;
|
||||||
while (digits > 0 && p1 < pend) {
|
while (digits > 0 && p1 < pend) {
|
||||||
*p1 = ::HexDigit(psz[--digits]);
|
*p1 = ::HexDigit(trimmed[--digits]);
|
||||||
if (digits > 0) {
|
if (digits > 0) {
|
||||||
*p1 |= ((unsigned char)::HexDigit(psz[--digits]) << 4);
|
*p1 |= ((unsigned char)::HexDigit(trimmed[--digits]) << 4);
|
||||||
p1++;
|
p1++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int BITS>
|
|
||||||
void base_blob<BITS>::SetHex(const std::string& str)
|
|
||||||
{
|
|
||||||
SetHex(str.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
template <unsigned int BITS>
|
template <unsigned int BITS>
|
||||||
std::string base_blob<BITS>::ToString() const
|
std::string base_blob<BITS>::ToString() const
|
||||||
{
|
{
|
||||||
|
@ -60,14 +52,12 @@ std::string base_blob<BITS>::ToString() const
|
||||||
// Explicit instantiations for base_blob<160>
|
// Explicit instantiations for base_blob<160>
|
||||||
template std::string base_blob<160>::GetHex() const;
|
template std::string base_blob<160>::GetHex() const;
|
||||||
template std::string base_blob<160>::ToString() const;
|
template std::string base_blob<160>::ToString() const;
|
||||||
template void base_blob<160>::SetHex(const char*);
|
template void base_blob<160>::SetHex(std::string_view);
|
||||||
template void base_blob<160>::SetHex(const std::string&);
|
|
||||||
|
|
||||||
// Explicit instantiations for base_blob<256>
|
// Explicit instantiations for base_blob<256>
|
||||||
template std::string base_blob<256>::GetHex() const;
|
template std::string base_blob<256>::GetHex() const;
|
||||||
template std::string base_blob<256>::ToString() const;
|
template std::string base_blob<256>::ToString() const;
|
||||||
template void base_blob<256>::SetHex(const char*);
|
template void base_blob<256>::SetHex(std::string_view);
|
||||||
template void base_blob<256>::SetHex(const std::string&);
|
|
||||||
|
|
||||||
const uint256 uint256::ZERO(0);
|
const uint256 uint256::ZERO(0);
|
||||||
const uint256 uint256::ONE(1);
|
const uint256 uint256::ONE(1);
|
||||||
|
|
|
@ -57,9 +57,9 @@ public:
|
||||||
friend constexpr bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
|
friend constexpr bool operator!=(const base_blob& a, const base_blob& b) { return a.Compare(b) != 0; }
|
||||||
friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
|
friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
|
||||||
|
|
||||||
|
// Hex string representations are little-endian.
|
||||||
std::string GetHex() const;
|
std::string GetHex() const;
|
||||||
void SetHex(const char* psz);
|
void SetHex(std::string_view str);
|
||||||
void SetHex(const std::string& str);
|
|
||||||
std::string ToString() const;
|
std::string ToString() const;
|
||||||
|
|
||||||
constexpr const unsigned char* data() const { return m_data.data(); }
|
constexpr const unsigned char* data() const { return m_data.data(); }
|
||||||
|
@ -112,21 +112,11 @@ public:
|
||||||
static const uint256 ONE;
|
static const uint256 ONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* uint256 from const char *.
|
/* uint256 from std::string_view, treated as little-endian.
|
||||||
* This is a separate function because the constructor uint256(const char*) can result
|
* This is not a uint256 constructor because of historical fears of uint256(0)
|
||||||
* in dangerously catching uint256(0).
|
* resolving to a NULL string and crashing.
|
||||||
*/
|
*/
|
||||||
inline uint256 uint256S(const char *str)
|
inline uint256 uint256S(std::string_view str)
|
||||||
{
|
|
||||||
uint256 rv;
|
|
||||||
rv.SetHex(str);
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
/* uint256 from std::string.
|
|
||||||
* This is a separate function because the constructor uint256(const std::string &str) can result
|
|
||||||
* in dangerously catching uint256(0) via std::string(const char*).
|
|
||||||
*/
|
|
||||||
inline uint256 uint256S(const std::string& str)
|
|
||||||
{
|
{
|
||||||
uint256 rv;
|
uint256 rv;
|
||||||
rv.SetHex(str);
|
rv.SetHex(str);
|
||||||
|
|
Loading…
Add table
Reference in a new issue