@@ -140,42 +140,52 @@ BOOST_AUTO_TEST_CASE(parse_hex)
140
140
// Basic test vector
141
141
result = ParseHex (" 04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" );
142
142
BOOST_CHECK_EQUAL_COLLECTIONS (result.begin (), result.end (), expected.begin (), expected.end ());
143
+ result = TryParseHex<uint8_t >(" 04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" ).value ();
144
+ BOOST_CHECK_EQUAL_COLLECTIONS (result.begin (), result.end (), expected.begin (), expected.end ());
143
145
144
146
// Spaces between bytes must be supported
145
147
result = ParseHex (" 12 34 56 78" );
146
148
BOOST_CHECK (result.size () == 4 && result[0 ] == 0x12 && result[1 ] == 0x34 && result[2 ] == 0x56 && result[3 ] == 0x78 );
149
+ result = TryParseHex<uint8_t >(" 12 34 56 78" ).value ();
150
+ BOOST_CHECK (result.size () == 4 && result[0 ] == 0x12 && result[1 ] == 0x34 && result[2 ] == 0x56 && result[3 ] == 0x78 );
147
151
148
152
// Leading space must be supported (used in BerkeleyEnvironment::Salvage)
149
153
result = ParseHex (" 89 34 56 78" );
150
154
BOOST_CHECK (result.size () == 4 && result[0 ] == 0x89 && result[1 ] == 0x34 && result[2 ] == 0x56 && result[3 ] == 0x78 );
155
+ result = TryParseHex<uint8_t >(" 89 34 56 78" ).value ();
156
+ BOOST_CHECK (result.size () == 4 && result[0 ] == 0x89 && result[1 ] == 0x34 && result[2 ] == 0x56 && result[3 ] == 0x78 );
151
157
152
158
// Mixed case and spaces are supported
153
159
result = ParseHex (" Ff aA " );
154
160
BOOST_CHECK (result.size () == 2 && result[0 ] == 0xff && result[1 ] == 0xaa );
161
+ result = TryParseHex<uint8_t >(" Ff aA " ).value ();
162
+ BOOST_CHECK (result.size () == 2 && result[0 ] == 0xff && result[1 ] == 0xaa );
155
163
156
164
// Empty string is supported
157
165
result = ParseHex (" " );
158
166
BOOST_CHECK (result.size () == 0 );
167
+ result = TryParseHex<uint8_t >(" " ).value ();
168
+ BOOST_CHECK (result.size () == 0 );
159
169
160
- // Spaces between nibbles is treated as end
161
- result = ParseHex (" AAF F" );
162
- BOOST_CHECK (result. size () == 1 && result[ 0 ] == 0xaa );
170
+ // Spaces between nibbles is treated as invalid
171
+ BOOST_CHECK_EQUAL ( ParseHex (" AAF F" ). size (), 0 );
172
+ BOOST_CHECK (! TryParseHex ( " AAF F " ). has_value () );
163
173
164
- // Embedded null is treated as end
174
+ // Embedded null is treated as invalid
165
175
const std::string with_embedded_null{" 11 " s
166
176
" \0 "
167
177
" 22 " s};
168
178
BOOST_CHECK_EQUAL (with_embedded_null.size (), 11 );
169
- result = ParseHex (with_embedded_null);
170
- BOOST_CHECK (result. size () == 1 && result[ 0 ] == 0x11 );
179
+ BOOST_CHECK_EQUAL ( ParseHex (with_embedded_null). size (), 0 );
180
+ BOOST_CHECK (! TryParseHex (with_embedded_null). has_value () );
171
181
172
- // Stop parsing at invalid value
173
- result = ParseHex (" 1234 invalid 1234" );
174
- BOOST_CHECK (result. size () == 2 && result[ 0 ] == 0x12 && result[ 1 ] == 0x34 );
182
+ // Non-hex is treated as invalid
183
+ BOOST_CHECK_EQUAL ( ParseHex (" 1234 invalid 1234" ). size (), 0 );
184
+ BOOST_CHECK (! TryParseHex ( " 1234 invalid 1234 " ). has_value () );
175
185
176
- // Truncated input is treated as end
177
- result = ParseHex (" 12 3" );
178
- BOOST_CHECK (result. size () == 1 && result[ 0 ] == 0x12 );
186
+ // Truncated input is treated as invalid
187
+ BOOST_CHECK_EQUAL ( ParseHex (" 12 3" ). size (), 0 );
188
+ BOOST_CHECK (! TryParseHex ( " 12 3 " ). has_value () );
179
189
}
180
190
181
191
BOOST_AUTO_TEST_CASE (util_HexStr)
0 commit comments