mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 17:44:56 +09:00
AK: StringUtils, add "convert_to_uint_from_hex" method
New method to convert hex string unsigned integer.
This commit is contained in:
parent
9300a8cfe5
commit
738235574f
Notes:
sideshowbarker
2024-07-19 06:18:32 +09:00
Author: https://github.com/asliturk
Commit: 738235574f
Pull-request: https://github.com/SerenityOS/serenity/pull/2310
Reviewed-by: https://github.com/linusg
2 changed files with 33 additions and 1 deletions
|
@ -143,6 +143,38 @@ unsigned convert_to_uint(const StringView& str, bool& ok)
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned convert_to_uint_from_hex(const StringView& str, bool& ok)
|
||||||
|
{
|
||||||
|
if (str.is_empty()) {
|
||||||
|
ok = false;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned value = 0;
|
||||||
|
const auto count = str.length();
|
||||||
|
|
||||||
|
for (size_t i = 0; i < count; i++) {
|
||||||
|
char digit = str[i];
|
||||||
|
u8 digit_val;
|
||||||
|
|
||||||
|
if (digit >= '0' && digit <= '9') {
|
||||||
|
digit_val = digit - '0';
|
||||||
|
} else if (digit >= 'a' && digit <= 'f') {
|
||||||
|
digit_val = 10 + (digit - 'a');
|
||||||
|
} else if (digit >= 'A' && digit <= 'F') {
|
||||||
|
digit_val = 10 + (digit - 'A');
|
||||||
|
} else {
|
||||||
|
ok = false;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
value = (value << 4) + digit_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
ok = true;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
static inline char to_lowercase(char c)
|
static inline char to_lowercase(char c)
|
||||||
{
|
{
|
||||||
if (c >= 'A' && c <= 'Z')
|
if (c >= 'A' && c <= 'Z')
|
||||||
|
|
|
@ -41,8 +41,8 @@ namespace StringUtils {
|
||||||
bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive);
|
bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive);
|
||||||
int convert_to_int(const StringView&, bool& ok);
|
int convert_to_int(const StringView&, bool& ok);
|
||||||
unsigned convert_to_uint(const StringView&, bool& ok);
|
unsigned convert_to_uint(const StringView&, bool& ok);
|
||||||
|
unsigned convert_to_uint_from_hex(const StringView&, bool& ok);
|
||||||
bool equals_ignoring_case(const StringView&, const StringView&);
|
bool equals_ignoring_case(const StringView&, const StringView&);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue