Skip to content

Commit 868816d

Browse files
committed
refactor: Remove SetHexDeprecated
After replacing all instances of `SetHexDeprecated` in the GUI, remove it entirely and reimplement the behavior in `FromHex`.
1 parent 6b63218 commit 868816d

File tree

3 files changed

+16
-52
lines changed

3 files changed

+16
-52
lines changed

src/test/uint256_tests.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,14 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
147147
uint256{"0000000000000000000000000000000000000000000000000000000000000001"});
148148
}
149149

150-
BOOST_AUTO_TEST_CASE(methods) // GetHex SetHexDeprecated FromHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
150+
BOOST_AUTO_TEST_CASE(methods) // GetHex FromHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
151151
{
152152
BOOST_CHECK_EQUAL(R1L.GetHex(), R1L.ToString());
153153
BOOST_CHECK_EQUAL(R2L.GetHex(), R2L.ToString());
154154
BOOST_CHECK_EQUAL(OneL.GetHex(), OneL.ToString());
155155
BOOST_CHECK_EQUAL(MaxL.GetHex(), MaxL.ToString());
156156
uint256 TmpL(R1L);
157157
BOOST_CHECK_EQUAL(TmpL, R1L);
158-
// Verify previous values don't persist when setting to truncated string.
159-
TmpL.SetHexDeprecated("21");
160-
BOOST_CHECK_EQUAL(TmpL.ToString(), "0000000000000000000000000000000000000000000000000000000000000021");
161158
BOOST_CHECK_EQUAL(uint256::FromHex(R2L.ToString()).value(), R2L);
162159
BOOST_CHECK_EQUAL(uint256::FromHex(ZeroL.ToString()).value(), uint256());
163160

src/uint256.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,6 @@ std::string base_blob<BITS>::GetHex() const
1717
return HexStr(m_data_rev);
1818
}
1919

20-
template <unsigned int BITS>
21-
void base_blob<BITS>::SetHexDeprecated(const std::string_view str)
22-
{
23-
std::fill(m_data.begin(), m_data.end(), 0);
24-
25-
const auto trimmed = util::RemovePrefixView(util::TrimStringView(str), "0x");
26-
27-
// Note: if we are passed a greater number of digits than would fit as bytes
28-
// in m_data, we will be discarding the leftmost ones.
29-
// str="12bc" in a WIDTH=1 m_data => m_data[] == "\0xbc", not "0x12".
30-
size_t digits = 0;
31-
for (const char c : trimmed) {
32-
if (::HexDigit(c) == -1) break;
33-
++digits;
34-
}
35-
unsigned char* p1 = m_data.data();
36-
unsigned char* pend = p1 + WIDTH;
37-
while (digits > 0 && p1 < pend) {
38-
*p1 = ::HexDigit(trimmed[--digits]);
39-
if (digits > 0) {
40-
*p1 |= ((unsigned char)::HexDigit(trimmed[--digits]) << 4);
41-
p1++;
42-
}
43-
}
44-
}
45-
4620
template <unsigned int BITS>
4721
std::string base_blob<BITS>::ToString() const
4822
{
@@ -52,12 +26,10 @@ std::string base_blob<BITS>::ToString() const
5226
// Explicit instantiations for base_blob<160>
5327
template std::string base_blob<160>::GetHex() const;
5428
template std::string base_blob<160>::ToString() const;
55-
template void base_blob<160>::SetHexDeprecated(std::string_view);
5629

5730
// Explicit instantiations for base_blob<256>
5831
template std::string base_blob<256>::GetHex() const;
5932
template std::string base_blob<256>::ToString() const;
60-
template void base_blob<256>::SetHexDeprecated(std::string_view);
6133

6234
const uint256 uint256::ZERO(0);
6335
const uint256 uint256::ONE(1);

src/uint256.h

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ class base_blob
6969

7070
/** @name Hex representation
7171
*
72-
* The hex representation used by GetHex(), ToString(), FromHex() and
73-
* SetHexDeprecated() is unusual, since it shows bytes of the base_blob in
74-
* reverse order. For example, a 4-byte blob {0x12, 0x34, 0x56, 0x78} is
75-
* represented as "78563412" instead of the more typical "12345678"
76-
* representation that would be shown in a hex editor or used by typical
72+
* The hex representation used by GetHex(), ToString(), and FromHex()
73+
* is unusual, since it shows bytes of the base_blob in reverse order.
74+
* For example, a 4-byte blob {0x12, 0x34, 0x56, 0x78} is represented
75+
* as "78563412" instead of the more typical "12345678" representation
76+
* that would be shown in a hex editor or used by typical
7777
* byte-array / hex conversion functions like python's bytes.hex() and
7878
* bytes.fromhex().
7979
*
@@ -92,20 +92,6 @@ class base_blob
9292
*
9393
* @{*/
9494
std::string GetHex() const;
95-
/** Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
96-
*
97-
* - Hex numbers that don't specify enough bytes to fill the internal array
98-
* will be treated as setting the beginning of it, which corresponds to
99-
* the least significant bytes when converted to base_uint.
100-
*
101-
* - Hex numbers specifying too many bytes will have the numerically most
102-
* significant bytes (the beginning of the string) narrowed away.
103-
*
104-
* - An odd count of hex digits will result in the high bits of the leftmost
105-
* byte being zero.
106-
* "0x123" => {0x23, 0x1, 0x0, ..., 0x0}
107-
*/
108-
void SetHexDeprecated(std::string_view str);
10995
std::string ToString() const;
11096
/**@}*/
11197

@@ -158,7 +144,16 @@ std::optional<uintN_t> FromHex(std::string_view str)
158144
{
159145
if (uintN_t::size() * 2 != str.size() || !IsHex(str)) return std::nullopt;
160146
uintN_t rv;
161-
rv.SetHexDeprecated(str);
147+
unsigned char* p1 = rv.begin();
148+
unsigned char* pend = rv.end();
149+
size_t digits = str.size();
150+
while (digits > 0 && p1 < pend) {
151+
*p1 = ::HexDigit(str[--digits]);
152+
if (digits > 0) {
153+
*p1 |= ((unsigned char)::HexDigit(str[--digits]) << 4);
154+
p1++;
155+
}
156+
}
162157
return rv;
163158
}
164159
/**

0 commit comments

Comments
 (0)