mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 20:03:34 -03:00
util: Add ParseHex<std::byte>() helper
This commit is contained in:
parent
fabdf81983
commit
fae1006019
3 changed files with 11 additions and 4 deletions
|
@ -25,6 +25,8 @@ FUZZ_TARGET_INIT(hex, initialize_hex)
|
|||
{
|
||||
const std::string random_hex_string(buffer.begin(), buffer.end());
|
||||
const std::vector<unsigned char> data = ParseHex(random_hex_string);
|
||||
const std::vector<std::byte> bytes{ParseHex<std::byte>(random_hex_string)};
|
||||
assert(AsBytes(Span{data}) == Span{bytes});
|
||||
const std::string hex_data = HexStr(data);
|
||||
if (IsHex(random_hex_string)) {
|
||||
assert(ToLower(random_hex_string) == hex_data);
|
||||
|
|
|
@ -76,9 +76,10 @@ bool IsHexNumber(std::string_view str)
|
|||
return str.size() > 0;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> ParseHex(std::string_view str)
|
||||
template <typename Byte>
|
||||
std::vector<Byte> ParseHex(std::string_view str)
|
||||
{
|
||||
std::vector<unsigned char> vch;
|
||||
std::vector<Byte> vch;
|
||||
auto it = str.begin();
|
||||
while (it != str.end() && it + 1 != str.end()) {
|
||||
if (IsSpace(*it)) {
|
||||
|
@ -88,10 +89,12 @@ std::vector<unsigned char> ParseHex(std::string_view str)
|
|||
auto c1 = HexDigit(*(it++));
|
||||
auto c2 = HexDigit(*(it++));
|
||||
if (c1 < 0 || c2 < 0) break;
|
||||
vch.push_back(uint8_t(c1 << 4) | c2);
|
||||
vch.push_back(Byte(c1 << 4) | Byte(c2));
|
||||
}
|
||||
return vch;
|
||||
}
|
||||
template std::vector<std::byte> ParseHex(std::string_view);
|
||||
template std::vector<uint8_t> ParseHex(std::string_view);
|
||||
|
||||
void SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
|
||||
{
|
||||
|
|
|
@ -55,7 +55,9 @@ enum class ByteUnit : uint64_t {
|
|||
* @return A new string without unsafe chars
|
||||
*/
|
||||
std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT);
|
||||
std::vector<unsigned char> ParseHex(std::string_view str);
|
||||
/** Parse the hex string into bytes (uint8_t or std::byte). Ignores whitespace. */
|
||||
template <typename Byte = uint8_t>
|
||||
std::vector<Byte> ParseHex(std::string_view str);
|
||||
signed char HexDigit(char c);
|
||||
/* Returns true if each character in str is a hex character, and has an even
|
||||
* number of hex digits.*/
|
||||
|
|
Loading…
Reference in a new issue