Skip to content

Commit

Permalink
refactor: Add base_blob::SetHex(std::string_view)
Browse files Browse the repository at this point in the history
Also consolidates other SetHex() overloads to call it.
  • Loading branch information
hodlinator committed Jul 12, 2024
1 parent 8f4293d commit 7d8dfe2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
34 changes: 12 additions & 22 deletions src/uint256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,31 @@ std::string base_blob<BITS>::GetHex() const
}

template <unsigned int BITS>
void base_blob<BITS>::SetHex(const char* psz)
void base_blob<BITS>::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 <unsigned int BITS>
void base_blob<BITS>::SetHex(const std::string& str)
{
SetHex(str.c_str());
}

template <unsigned int BITS>
std::string base_blob<BITS>::ToString() const
{
Expand All @@ -60,14 +52,12 @@ std::string base_blob<BITS>::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);
15 changes: 13 additions & 2 deletions src/uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down Expand Up @@ -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

0 comments on commit 7d8dfe2

Please sign in to comment.