diff options
-rw-r--r-- | src/util/strencodings.cpp | 30 | ||||
-rw-r--r-- | src/util/strencodings.h | 3 |
2 files changed, 12 insertions, 21 deletions
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index 940fa90da2..f5288eca17 100644 --- a/src/util/strencodings.cpp +++ b/src/util/strencodings.cpp @@ -81,32 +81,24 @@ bool IsHexNumber(const std::string& str) return (str.size() > starting_location); } -std::vector<unsigned char> ParseHex(const char* psz) +std::vector<unsigned char> ParseHex(std::string_view str) { // convert hex dump to vector std::vector<unsigned char> vch; - while (true) - { - while (IsSpace(*psz)) - psz++; - signed char c = HexDigit(*psz++); - if (c == (signed char)-1) - break; - auto n{uint8_t(c << 4)}; - c = HexDigit(*psz++); - if (c == (signed char)-1) - break; - n |= c; - vch.push_back(n); + auto it = str.begin(); + while (it != str.end() && it + 1 != str.end()) { + if (IsSpace(*it)) { + ++it; + continue; + } + auto c1 = HexDigit(*(it++)); + auto c2 = HexDigit(*(it++)); + if (c1 < 0 || c2 < 0) break; + vch.push_back(uint8_t(c1 << 4) | c2); } return vch; } -std::vector<unsigned char> ParseHex(const std::string& str) -{ - return ParseHex(str.c_str()); -} - void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut) { size_t colon = in.find_last_of(':'); diff --git a/src/util/strencodings.h b/src/util/strencodings.h index 1f83fa3ffa..0e72466fc3 100644 --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -55,8 +55,7 @@ enum class ByteUnit : uint64_t { * @return A new string without unsafe chars */ std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT); -std::vector<unsigned char> ParseHex(const char* psz); -std::vector<unsigned char> ParseHex(const std::string& str); +std::vector<unsigned char> 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.*/ |