@@ -18,39 +18,31 @@ std::string base_blob<BITS>::GetHex() const
18
18
}
19
19
20
20
template <unsigned int BITS>
21
- void base_blob<BITS>::SetHex(const char * psz )
21
+ void base_blob<BITS>::SetHex(const std::string_view str )
22
22
{
23
23
std::fill (m_data.begin (), m_data.end (), 0 );
24
24
25
- // skip leading spaces
26
- while (IsSpace (*psz))
27
- psz++;
25
+ const auto trimmed = util::RemovePrefixView (util::TrimStringView (str), " 0x" );
28
26
29
- // skip 0x
30
- if (psz[0 ] == ' 0' && ToLower (psz[1 ]) == ' x' )
31
- psz += 2 ;
32
-
33
- // hex string to uint
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".
34
30
size_t digits = 0 ;
35
- while (::HexDigit (psz[digits]) != -1 )
36
- digits++;
31
+ for (const char c : trimmed) {
32
+ if (::HexDigit (c) == -1 ) break ;
33
+ ++digits;
34
+ }
37
35
unsigned char * p1 = m_data.data ();
38
36
unsigned char * pend = p1 + WIDTH;
39
37
while (digits > 0 && p1 < pend) {
40
- *p1 = ::HexDigit (psz [--digits]);
38
+ *p1 = ::HexDigit (trimmed [--digits]);
41
39
if (digits > 0 ) {
42
- *p1 |= ((unsigned char )::HexDigit (psz [--digits]) << 4 );
40
+ *p1 |= ((unsigned char )::HexDigit (trimmed [--digits]) << 4 );
43
41
p1++;
44
42
}
45
43
}
46
44
}
47
45
48
- template <unsigned int BITS>
49
- void base_blob<BITS>::SetHex(const std::string& str)
50
- {
51
- SetHex (str.c_str ());
52
- }
53
-
54
46
template <unsigned int BITS>
55
47
std::string base_blob<BITS>::ToString() const
56
48
{
@@ -60,14 +52,12 @@ std::string base_blob<BITS>::ToString() const
60
52
// Explicit instantiations for base_blob<160>
61
53
template std::string base_blob<160 >::GetHex() const ;
62
54
template std::string base_blob<160 >::ToString() const ;
63
- template void base_blob<160 >::SetHex(const char *);
64
- template void base_blob<160 >::SetHex(const std::string&);
55
+ template void base_blob<160 >::SetHex(std::string_view);
65
56
66
57
// Explicit instantiations for base_blob<256>
67
58
template std::string base_blob<256 >::GetHex() const ;
68
59
template std::string base_blob<256 >::ToString() const ;
69
- template void base_blob<256 >::SetHex(const char *);
70
- template void base_blob<256 >::SetHex(const std::string&);
60
+ template void base_blob<256 >::SetHex(std::string_view);
71
61
72
62
const uint256 uint256::ZERO (0 );
73
63
const uint256 uint256::ONE (1 );
0 commit comments