Make IsHexNumber use string_view

This commit is contained in:
Pieter Wuille 2022-04-04 11:20:10 -04:00 committed by MacroFake
parent 40062997f2
commit 963bc9b576
2 changed files with 5 additions and 8 deletions

View file

@ -66,17 +66,14 @@ bool IsHex(std::string_view str)
return (str.size() > 0) && (str.size()%2 == 0);
}
bool IsHexNumber(const std::string& str)
bool IsHexNumber(std::string_view str)
{
size_t starting_location = 0;
if (str.size() > 2 && *str.begin() == '0' && *(str.begin()+1) == 'x') {
starting_location = 2;
}
for (const char c : str.substr(starting_location)) {
if (str.substr(0, 2) == "0x") str.remove_prefix(2);
for (char c : str) {
if (HexDigit(c) < 0) return false;
}
// Return false for empty string or "0x".
return (str.size() > starting_location);
return str.size() > 0;
}
std::vector<unsigned char> ParseHex(std::string_view str)

View file

@ -63,7 +63,7 @@ bool IsHex(std::string_view str);
/**
* Return true if the string is a hex number, optionally prefixed with "0x"
*/
bool IsHexNumber(const std::string& str);
bool IsHexNumber(std::string_view str);
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
std::string EncodeBase64(Span<const unsigned char> input);