refactor: Remove SetHexDeprecated

After replacing all instances of `SetHexDeprecated` in the GUI,
remove it entirely and reimplement the behavior in `FromHex`.
This commit is contained in:
marcofleon 2025-04-09 15:27:33 +01:00
parent 6b63218ec2
commit 868816d962
3 changed files with 16 additions and 52 deletions

View file

@ -147,7 +147,7 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
uint256{"0000000000000000000000000000000000000000000000000000000000000001"}); uint256{"0000000000000000000000000000000000000000000000000000000000000001"});
} }
BOOST_AUTO_TEST_CASE(methods) // GetHex SetHexDeprecated FromHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize BOOST_AUTO_TEST_CASE(methods) // GetHex FromHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
{ {
BOOST_CHECK_EQUAL(R1L.GetHex(), R1L.ToString()); BOOST_CHECK_EQUAL(R1L.GetHex(), R1L.ToString());
BOOST_CHECK_EQUAL(R2L.GetHex(), R2L.ToString()); BOOST_CHECK_EQUAL(R2L.GetHex(), R2L.ToString());
@ -155,9 +155,6 @@ BOOST_AUTO_TEST_CASE(methods) // GetHex SetHexDeprecated FromHex begin() end() s
BOOST_CHECK_EQUAL(MaxL.GetHex(), MaxL.ToString()); BOOST_CHECK_EQUAL(MaxL.GetHex(), MaxL.ToString());
uint256 TmpL(R1L); uint256 TmpL(R1L);
BOOST_CHECK_EQUAL(TmpL, R1L); BOOST_CHECK_EQUAL(TmpL, R1L);
// Verify previous values don't persist when setting to truncated string.
TmpL.SetHexDeprecated("21");
BOOST_CHECK_EQUAL(TmpL.ToString(), "0000000000000000000000000000000000000000000000000000000000000021");
BOOST_CHECK_EQUAL(uint256::FromHex(R2L.ToString()).value(), R2L); BOOST_CHECK_EQUAL(uint256::FromHex(R2L.ToString()).value(), R2L);
BOOST_CHECK_EQUAL(uint256::FromHex(ZeroL.ToString()).value(), uint256()); BOOST_CHECK_EQUAL(uint256::FromHex(ZeroL.ToString()).value(), uint256());

View file

@ -17,32 +17,6 @@ std::string base_blob<BITS>::GetHex() const
return HexStr(m_data_rev); return HexStr(m_data_rev);
} }
template <unsigned int BITS>
void base_blob<BITS>::SetHexDeprecated(const std::string_view str)
{
std::fill(m_data.begin(), m_data.end(), 0);
const auto trimmed = util::RemovePrefixView(util::TrimStringView(str), "0x");
// Note: if we are passed a greater number of digits than would fit as bytes
// in m_data, we will be discarding the leftmost ones.
// str="12bc" in a WIDTH=1 m_data => m_data[] == "\0xbc", not "0x12".
size_t digits = 0;
for (const char c : trimmed) {
if (::HexDigit(c) == -1) break;
++digits;
}
unsigned char* p1 = m_data.data();
unsigned char* pend = p1 + WIDTH;
while (digits > 0 && p1 < pend) {
*p1 = ::HexDigit(trimmed[--digits]);
if (digits > 0) {
*p1 |= ((unsigned char)::HexDigit(trimmed[--digits]) << 4);
p1++;
}
}
}
template <unsigned int BITS> template <unsigned int BITS>
std::string base_blob<BITS>::ToString() const std::string base_blob<BITS>::ToString() const
{ {
@ -52,12 +26,10 @@ 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>::SetHexDeprecated(std::string_view);
// 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>::SetHexDeprecated(std::string_view);
const uint256 uint256::ZERO(0); const uint256 uint256::ZERO(0);
const uint256 uint256::ONE(1); const uint256 uint256::ONE(1);

View file

@ -69,11 +69,11 @@ public:
/** @name Hex representation /** @name Hex representation
* *
* The hex representation used by GetHex(), ToString(), FromHex() and * The hex representation used by GetHex(), ToString(), and FromHex()
* SetHexDeprecated() is unusual, since it shows bytes of the base_blob in * is unusual, since it shows bytes of the base_blob in reverse order.
* reverse order. For example, a 4-byte blob {0x12, 0x34, 0x56, 0x78} is * For example, a 4-byte blob {0x12, 0x34, 0x56, 0x78} is represented
* represented as "78563412" instead of the more typical "12345678" * as "78563412" instead of the more typical "12345678" representation
* representation that would be shown in a hex editor or used by typical * that would be shown in a hex editor or used by typical
* byte-array / hex conversion functions like python's bytes.hex() and * byte-array / hex conversion functions like python's bytes.hex() and
* bytes.fromhex(). * bytes.fromhex().
* *
@ -92,20 +92,6 @@ public:
* *
* @{*/ * @{*/
std::string GetHex() const; std::string GetHex() const;
/** Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
*
* - Hex numbers that don't specify enough bytes to fill the internal array
* will be treated as setting the beginning of it, which corresponds to
* the least significant bytes when converted to base_uint.
*
* - Hex numbers specifying too many bytes will have the numerically most
* significant bytes (the beginning of the string) narrowed away.
*
* - An odd count of hex digits will result in the high bits of the leftmost
* byte being zero.
* "0x123" => {0x23, 0x1, 0x0, ..., 0x0}
*/
void SetHexDeprecated(std::string_view str);
std::string ToString() const; std::string ToString() const;
/**@}*/ /**@}*/
@ -158,7 +144,16 @@ std::optional<uintN_t> FromHex(std::string_view str)
{ {
if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt; if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt;
uintN_t rv; uintN_t rv;
rv.SetHexDeprecated(str); unsigned char* p1 = rv.begin();
unsigned char* pend = rv.end();
size_t digits = str.size();
while (digits > 0 && p1 < pend) {
*p1 = ::HexDigit(str[--digits]);
if (digits > 0) {
*p1 |= ((unsigned char)::HexDigit(str[--digits]) << 4);
p1++;
}
}
return rv; return rv;
} }
/** /**