mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-01 21:59:07 +00:00
AK: Add convert_to_uint_from_octal
This commit is contained in:
parent
6ca34f5647
commit
9e97823ff8
Notes:
sideshowbarker
2024-07-17 22:26:51 +09:00
Author: https://github.com/xavier
Commit: 9e97823ff8
Pull-request: https://github.com/SerenityOS/serenity/pull/11319
Reviewed-by: https://github.com/bgianfo
3 changed files with 91 additions and 0 deletions
|
@ -191,6 +191,41 @@ template Optional<u16> convert_to_uint_from_hex(StringView str, TrimWhitespace);
|
|||
template Optional<u32> convert_to_uint_from_hex(StringView str, TrimWhitespace);
|
||||
template Optional<u64> convert_to_uint_from_hex(StringView str, TrimWhitespace);
|
||||
|
||||
template<typename T>
|
||||
Optional<T> convert_to_uint_from_octal(StringView str, TrimWhitespace trim_whitespace)
|
||||
{
|
||||
auto string = trim_whitespace == TrimWhitespace::Yes
|
||||
? str.trim_whitespace()
|
||||
: str;
|
||||
if (string.is_empty())
|
||||
return {};
|
||||
|
||||
T value = 0;
|
||||
const auto count = string.length();
|
||||
const T upper_bound = NumericLimits<T>::max();
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
char digit = string[i];
|
||||
u8 digit_val;
|
||||
if (value > (upper_bound >> 3))
|
||||
return {};
|
||||
|
||||
if (digit >= '0' && digit <= '7') {
|
||||
digit_val = digit - '0';
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
|
||||
value = (value << 3) + digit_val;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
template Optional<u8> convert_to_uint_from_octal(StringView str, TrimWhitespace);
|
||||
template Optional<u16> convert_to_uint_from_octal(StringView str, TrimWhitespace);
|
||||
template Optional<u32> convert_to_uint_from_octal(StringView str, TrimWhitespace);
|
||||
template Optional<u64> convert_to_uint_from_octal(StringView str, TrimWhitespace);
|
||||
|
||||
bool equals_ignoring_case(StringView a, StringView b)
|
||||
{
|
||||
if (a.length() != b.length())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue