@@ -18,39 +18,31 @@ std::string base_blob<BITS>::GetHex() const
1818}
1919
2020template <unsigned int BITS>
21- void base_blob<BITS>::SetHex(const char * psz )
21+ void base_blob<BITS>::SetHex(const std::string_view str )
2222{
2323 std::fill (m_data.begin (), m_data.end (), 0 );
2424
25- // skip leading spaces
26- while (IsSpace (*psz))
27- psz++;
25+ const auto trimmed = util::RemovePrefixView (util::TrimStringView (str), " 0x" );
2826
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".
3430 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+ }
3735 unsigned char * p1 = m_data.data ();
3836 unsigned char * pend = p1 + WIDTH;
3937 while (digits > 0 && p1 < pend) {
40- *p1 = ::HexDigit (psz [--digits]);
38+ *p1 = ::HexDigit (trimmed [--digits]);
4139 if (digits > 0 ) {
42- *p1 |= ((unsigned char )::HexDigit (psz [--digits]) << 4 );
40+ *p1 |= ((unsigned char )::HexDigit (trimmed [--digits]) << 4 );
4341 p1++;
4442 }
4543 }
4644}
4745
48- template <unsigned int BITS>
49- void base_blob<BITS>::SetHex(const std::string& str)
50- {
51- SetHex (str.c_str ());
52- }
53-
5446template <unsigned int BITS>
5547std::string base_blob<BITS>::ToString() const
5648{
@@ -60,14 +52,12 @@ std::string base_blob<BITS>::ToString() const
6052// Explicit instantiations for base_blob<160>
6153template std::string base_blob<160 >::GetHex() const ;
6254template 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);
6556
6657// Explicit instantiations for base_blob<256>
6758template std::string base_blob<256 >::GetHex() const ;
6859template 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);
7161
7262const uint256 uint256::ZERO (0 );
7363const uint256 uint256::ONE (1 );
0 commit comments