1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-29 12:17:36 +00:00

Everywhere: Use to_number<T> instead of to_{int,uint,float,double}

In a bunch of cases, this actually ends up simplifying the code as
to_number will handle something such as:

```
Optional<I> opt;
if constexpr (IsSigned<I>)
    opt = view.to_int<I>();
else
    opt = view.to_uint<I>();
```

For us.

The main goal here however is to have a single generic number conversion
API between all of the String classes.
This commit is contained in:
Shannon Booth 2023-12-23 15:59:14 +13:00 committed by Andreas Kling
parent a4ecc65398
commit e2e7c4d574
155 changed files with 397 additions and 412 deletions

View file

@ -108,19 +108,19 @@ public:
u32 d {};
if (parts.size() == 1) {
d = parts[0].to_uint().value_or(256);
d = parts[0].to_number<u32>().value_or(256);
} else if (parts.size() == 2) {
a = parts[0].to_uint().value_or(256);
d = parts[1].to_uint().value_or(256);
a = parts[0].to_number<u32>().value_or(256);
d = parts[1].to_number<u32>().value_or(256);
} else if (parts.size() == 3) {
a = parts[0].to_uint().value_or(256);
b = parts[1].to_uint().value_or(256);
d = parts[2].to_uint().value_or(256);
a = parts[0].to_number<u32>().value_or(256);
b = parts[1].to_number<u32>().value_or(256);
d = parts[2].to_number<u32>().value_or(256);
} else if (parts.size() == 4) {
a = parts[0].to_uint().value_or(256);
b = parts[1].to_uint().value_or(256);
c = parts[2].to_uint().value_or(256);
d = parts[3].to_uint().value_or(256);
a = parts[0].to_number<u32>().value_or(256);
b = parts[1].to_number<u32>().value_or(256);
c = parts[2].to_number<u32>().value_or(256);
d = parts[3].to_number<u32>().value_or(256);
} else {
return {};
}

View file

@ -278,13 +278,13 @@ ErrorOr<JsonValue> JsonParser::parse_number()
StringView number_string(number_buffer.data(), number_buffer.size());
auto to_unsigned_result = number_string.to_uint<u64>();
auto to_unsigned_result = number_string.to_number<u64>();
if (to_unsigned_result.has_value()) {
if (*to_unsigned_result <= NumericLimits<u32>::max())
return JsonValue((u32)*to_unsigned_result);
return JsonValue(*to_unsigned_result);
} else if (auto signed_number = number_string.to_int<i64>(); signed_number.has_value()) {
} else if (auto signed_number = number_string.to_number<i64>(); signed_number.has_value()) {
if (*signed_number <= NumericLimits<i32>::max())
return JsonValue((i32)*signed_number);

View file

@ -124,7 +124,7 @@ static Optional<ParsedIPv4Number> parse_ipv4_number(StringView input)
if (radix == 8)
maybe_output = StringUtils::convert_to_uint_from_octal(input);
else if (radix == 10)
maybe_output = input.to_uint();
maybe_output = input.to_number<u32>();
else if (radix == 16)
maybe_output = StringUtils::convert_to_uint_from_hex(input);
else
@ -1292,10 +1292,11 @@ URL URLParser::basic_parse(StringView raw_input, Optional<URL> const& base_url,
// 1. If buffer is not the empty string, then:
if (!buffer.is_empty()) {
// 1. Let port be the mathematical integer value that is represented by buffer in radix-10 using ASCII digits for digits with values 0 through 9.
auto port = buffer.string_view().to_uint();
auto port = buffer.string_view().to_number<u16>();
// 2. If port is greater than 2^16 1, port-out-of-range validation error, return failure.
if (!port.has_value() || port.value() > 65535) {
// NOTE: This is done by to_number.
if (!port.has_value()) {
report_validation_error();
return {};
}