Skip to content

Commit

Permalink
Make ParseHex use string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa authored and MacroFake committed Apr 27, 2022
1 parent f58c1f1 commit c1d165a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
30 changes: 11 additions & 19 deletions src/util/strencodings.cpp
Expand Up @@ -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(':');
Expand Down
3 changes: 1 addition & 2 deletions src/util/strencodings.h
Expand Up @@ -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.*/
Expand Down

0 comments on commit c1d165a

Please sign in to comment.