1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:37:34 +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

@ -647,7 +647,7 @@ Vector<PatternPartition> partition_number_pattern(VM& vm, NumberFormat& number_f
else if ((part.starts_with("unitIdentifier:"sv)) && (number_format.style() == NumberFormat::Style::Unit)) {
// Note: Our implementation combines "unitPrefix" and "unitSuffix" into one field, "unitIdentifier".
auto identifier_index = part.substring_view("unitIdentifier:"sv.length()).to_uint();
auto identifier_index = part.substring_view("unitIdentifier:"sv.length()).to_number<unsigned>();
VERIFY(identifier_index.has_value());
// i. Let unit be numberFormat.[[Unit]].
@ -862,7 +862,7 @@ Vector<PatternPartition> partition_notation_sub_pattern(NumberFormat& number_for
else if (part.starts_with("compactIdentifier:"sv)) {
// Note: Our implementation combines "compactSymbol" and "compactName" into one field, "compactIdentifier".
auto identifier_index = part.substring_view("compactIdentifier:"sv.length()).to_uint();
auto identifier_index = part.substring_view("compactIdentifier:"sv.length()).to_number<unsigned>();
VERIFY(identifier_index.has_value());
// 1. Let compactSymbol be an ILD string representing exponent in short form, which may depend on x in languages having different plural forms. The implementation must be able to provide this string, or else the pattern would not have a "{compactSymbol}" placeholder.
@ -1034,7 +1034,7 @@ static RawPrecisionResult to_raw_precision_function(MathematicalValue const& num
result.number = result.number.divided_by(10);
if (mode == PreferredResult::GreaterThanNumber && digit.to_uint().value() != 0)
if (mode == PreferredResult::GreaterThanNumber && digit.to_number<unsigned>().value() != 0)
result.number = result.number.plus(1);
}
@ -1183,7 +1183,7 @@ static RawFixedResult to_raw_fixed_function(MathematicalValue const& number, int
result.number = result.number.multiplied_by(10);
if (mode == PreferredResult::GreaterThanNumber && digit.to_uint().value() != 0)
if (mode == PreferredResult::GreaterThanNumber && digit.to_number<unsigned>().value() != 0)
result.number = result.number.plus(1);
}

View file

@ -23,7 +23,7 @@ PluralRules::PluralRules(Object& prototype)
::Locale::PluralOperands get_operands(StringView string)
{
// 1.Let n be ! ToNumber(s).
auto number = string.to_double(AK::TrimWhitespace::Yes).release_value();
auto number = string.to_number<double>(AK::TrimWhitespace::Yes).release_value();
// 2. Assert: n is finite.
VERIFY(isfinite(number));
@ -57,7 +57,7 @@ PluralRules::PluralRules(Object& prototype)
return static_cast<u64>(fabs(value));
},
[](StringView value) {
auto value_as_int = value.template to_int<i64>().value();
auto value_as_int = value.template to_number<i64>().value();
return static_cast<u64>(value_as_int);
});
@ -65,7 +65,7 @@ PluralRules::PluralRules(Object& prototype)
auto fraction_digit_count = fraction_slice.length();
// 8. Let f be ! ToNumber(fracSlice).
auto fraction = fraction_slice.is_empty() ? 0u : fraction_slice.template to_uint<u64>().value();
auto fraction = fraction_slice.is_empty() ? 0u : fraction_slice.template to_number<u64>().value();
// 9. Let significantFracSlice be the value of fracSlice stripped of trailing "0".
auto significant_fraction_slice = fraction_slice.trim("0"sv, TrimMode::Right);
@ -74,7 +74,7 @@ PluralRules::PluralRules(Object& prototype)
auto significant_fraction_digit_count = significant_fraction_slice.length();
// 11. Let significantFrac be ! ToNumber(significantFracSlice).
auto significant_fraction = significant_fraction_slice.is_empty() ? 0u : significant_fraction_slice.template to_uint<u64>().value();
auto significant_fraction = significant_fraction_slice.is_empty() ? 0u : significant_fraction_slice.template to_number<u64>().value();
// 12. Return a new Record { [[Number]]: abs(n), [[IntegerDigits]]: i, [[FractionDigits]]: f, [[NumberOfFractionDigits]]: fracDigitCount, [[FractionDigitsWithoutTrailing]]: significantFrac, [[NumberOfFractionDigitsWithoutTrailing]]: significantFracDigitCount }.
return ::Locale::PluralOperands {