mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 14:38:29 +01:00
Make ParseHex use string_view
This commit is contained in:
@@ -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(':');
|
||||
|
||||
@@ -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.*/
|
||||
|
||||
Reference in New Issue
Block a user