Skip to content

Commit 963bc9b

Browse files
sipaMacroFake
authored andcommitted
Make IsHexNumber use string_view
1 parent 4006299 commit 963bc9b

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

src/util/strencodings.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,14 @@ bool IsHex(std::string_view str)
6666
return (str.size() > 0) && (str.size()%2 == 0);
6767
}
6868

69-
bool IsHexNumber(const std::string& str)
69+
bool IsHexNumber(std::string_view str)
7070
{
71-
size_t starting_location = 0;
72-
if (str.size() > 2 && *str.begin() == '0' && *(str.begin()+1) == 'x') {
73-
starting_location = 2;
74-
}
75-
for (const char c : str.substr(starting_location)) {
71+
if (str.substr(0, 2) == "0x") str.remove_prefix(2);
72+
for (char c : str) {
7673
if (HexDigit(c) < 0) return false;
7774
}
7875
// Return false for empty string or "0x".
79-
return (str.size() > starting_location);
76+
return str.size() > 0;
8077
}
8178

8279
std::vector<unsigned char> ParseHex(std::string_view str)

src/util/strencodings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ bool IsHex(std::string_view str);
6363
/**
6464
* Return true if the string is a hex number, optionally prefixed with "0x"
6565
*/
66-
bool IsHexNumber(const std::string& str);
66+
bool IsHexNumber(std::string_view str);
6767
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
6868
std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
6969
std::string EncodeBase64(Span<const unsigned char> input);

0 commit comments

Comments
 (0)