Changeset View
Changeset View
Standalone View
Standalone View
src/util/strencodings.cpp
Show First 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | bool IsHex(const std::string &str) { | ||||
for (std::string::const_iterator it(str.begin()); it != str.end(); ++it) { | for (std::string::const_iterator it(str.begin()); it != str.end(); ++it) { | ||||
if (HexDigit(*it) < 0) { | if (HexDigit(*it) < 0) { | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
return (str.size() > 0) && (str.size() % 2 == 0); | return (str.size() > 0) && (str.size() % 2 == 0); | ||||
} | } | ||||
bool IsHexNumber(const std::string &str) { | bool IsHexNumber(const std::string &string) noexcept { | ||||
size_t starting_location = 0; | size_t starting_location = 0; | ||||
if (str.size() > 2 && *str.begin() == '0' && *(str.begin() + 1) == 'x') { | try { | ||||
if (string.substr(0, 2) == "0x") { | |||||
starting_location = 2; | starting_location = 2; | ||||
} | } | ||||
for (auto c : str.substr(starting_location)) { | } catch (const std::out_of_range &oor) { | ||||
if (HexDigit(c) < 0) { | } // substr(0,x) should never throw out_of_range, but hey, compilers | ||||
// if string is just "" or "0x", return false | |||||
if (string.size() == starting_location) { | |||||
return false; | return false; | ||||
} | } | ||||
} | |||||
// Return false for empty string or "0x". | return string.find_first_not_of("0123456789abcdefABCDEF", | ||||
return (str.size() > starting_location); | starting_location) == std::string::npos; | ||||
} | } | ||||
std::vector<uint8_t> ParseHex(const char *psz) { | std::vector<uint8_t> ParseHex(const char *psz) { | ||||
// convert hex dump to vector | // convert hex dump to vector | ||||
std::vector<uint8_t> vch; | std::vector<uint8_t> vch; | ||||
while (true) { | while (true) { | ||||
while (IsSpace(*psz)) { | while (IsSpace(*psz)) { | ||||
psz++; | psz++; | ||||
▲ Show 20 Lines • Show All 766 Lines • Show Last 20 Lines |