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

@ -1199,7 +1199,7 @@ CanonicalIndex canonical_numeric_index_string(PropertyKey const& property_key, C
return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);
// 2. Let n be ! ToNumber(argument).
auto maybe_double = argument.to_double(AK::TrimWhitespace::No);
auto maybe_double = argument.to_number<double>(AK::TrimWhitespace::No);
if (!maybe_double.has_value())
return CanonicalIndex(CanonicalIndex::Type::Undefined, 0);

View file

@ -377,7 +377,7 @@ static i64 clip_bigint_to_sane_time(Crypto::SignedBigInteger const& value)
return NumericLimits<i64>::max();
// FIXME: Can we do this without string conversion?
return value.to_base_deprecated(10).to_int<i64>().value();
return value.to_base_deprecated(10).to_number<i64>().value();
}
// 21.4.1.20 GetNamedTimeZoneEpochNanoseconds ( timeZoneIdentifier, year, month, day, hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/ecma262/#sec-getnamedtimezoneepochnanoseconds

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 {

View file

@ -136,7 +136,7 @@ public:
return false;
}
auto property_index = m_string.to_uint(TrimWhitespace::No);
auto property_index = m_string.to_number<unsigned>(TrimWhitespace::No);
if (!property_index.has_value() || property_index.value() == NumericLimits<u32>::max()) {
m_string_may_be_number = false;
return false;

View file

@ -1276,22 +1276,22 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM& vm, ParseResult const& pa
// a. Let monthMV be 1.
// 9. Else,
// a. Let monthMV be ! ToIntegerOrInfinity(CodePointsToString(month)).
auto month_mv = *month.value_or("1"sv).to_uint<u8>();
auto month_mv = *month.value_or("1"sv).to_number<u8>();
// 10. If day is empty, then
// a. Let dayMV be 1.
// 11. Else,
// a. Let dayMV be ! ToIntegerOrInfinity(CodePointsToString(day)).
auto day_mv = *day.value_or("1"sv).to_uint<u8>();
auto day_mv = *day.value_or("1"sv).to_number<u8>();
// 12. Let hourMV be ! ToIntegerOrInfinity(CodePointsToString(hour)).
auto hour_mv = *hour.value_or("0"sv).to_uint<u8>();
auto hour_mv = *hour.value_or("0"sv).to_number<u8>();
// 13. Let minuteMV be ! ToIntegerOrInfinity(CodePointsToString(minute)).
auto minute_mv = *minute.value_or("0"sv).to_uint<u8>();
auto minute_mv = *minute.value_or("0"sv).to_number<u8>();
// 14. Let secondMV be ! ToIntegerOrInfinity(CodePointsToString(second)).
auto second_mv = *second.value_or("0"sv).to_uint<u8>();
auto second_mv = *second.value_or("0"sv).to_number<u8>();
// 15. If secondMV is 60, then
if (second_mv == 60) {
@ -1532,19 +1532,19 @@ ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(VM& vm, StringV
auto f_seconds_part = parse_result->duration_seconds_fraction;
// 4. Let yearsMV be ! ToIntegerOrInfinity(CodePointsToString(years)).
auto years = years_part.value_or("0"sv).to_double().release_value();
auto years = years_part.value_or("0"sv).to_number<double>().release_value();
// 5. Let monthsMV be ! ToIntegerOrInfinity(CodePointsToString(months)).
auto months = months_part.value_or("0"sv).to_double().release_value();
auto months = months_part.value_or("0"sv).to_number<double>().release_value();
// 6. Let weeksMV be ! ToIntegerOrInfinity(CodePointsToString(weeks)).
auto weeks = weeks_part.value_or("0"sv).to_double().release_value();
auto weeks = weeks_part.value_or("0"sv).to_number<double>().release_value();
// 7. Let daysMV be ! ToIntegerOrInfinity(CodePointsToString(days)).
auto days = days_part.value_or("0"sv).to_double().release_value();
auto days = days_part.value_or("0"sv).to_number<double>().release_value();
// 8. Let hoursMV be ! ToIntegerOrInfinity(CodePointsToString(hours)).
auto hours = hours_part.value_or("0"sv).to_double().release_value();
auto hours = hours_part.value_or("0"sv).to_number<double>().release_value();
double minutes;
@ -1561,12 +1561,12 @@ ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(VM& vm, StringV
auto f_hours_scale = (double)f_hours_digits.length();
// d. Let minutesMV be ! ToIntegerOrInfinity(fHoursDigits) / 10^fHoursScale × 60.
minutes = f_hours_digits.to_double().release_value() / pow(10., f_hours_scale) * 60;
minutes = f_hours_digits.to_number<double>().release_value() / pow(10., f_hours_scale) * 60;
}
// 10. Else,
else {
// a. Let minutesMV be ! ToIntegerOrInfinity(CodePointsToString(minutes)).
minutes = minutes_part.value_or("0"sv).to_double().release_value();
minutes = minutes_part.value_or("0"sv).to_number<double>().release_value();
}
double seconds;
@ -1584,12 +1584,12 @@ ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(VM& vm, StringV
auto f_minutes_scale = (double)f_minutes_digits.length();
// d. Let secondsMV be ! ToIntegerOrInfinity(fMinutesDigits) / 10^fMinutesScale × 60.
seconds = f_minutes_digits.to_double().release_value() / pow(10, f_minutes_scale) * 60;
seconds = f_minutes_digits.to_number<double>().release_value() / pow(10, f_minutes_scale) * 60;
}
// 12. Else if seconds is not empty, then
else if (seconds_part.has_value()) {
// a. Let secondsMV be ! ToIntegerOrInfinity(CodePointsToString(seconds)).
seconds = seconds_part.value_or("0"sv).to_double().release_value();
seconds = seconds_part.value_or("0"sv).to_number<double>().release_value();
}
// 13. Else,
else {
@ -1608,7 +1608,7 @@ ThrowCompletionOr<DurationRecord> parse_temporal_duration_string(VM& vm, StringV
auto f_seconds_scale = (double)f_seconds_digits.length();
// c. Let millisecondsMV be ! ToIntegerOrInfinity(fSecondsDigits) / 10^fSecondsScale × 1000.
milliseconds = f_seconds_digits.to_double().release_value() / pow(10, f_seconds_scale) * 1000;
milliseconds = f_seconds_digits.to_number<double>().release_value() / pow(10, f_seconds_scale) * 1000;
}
// 15. Else,
else {

View file

@ -69,7 +69,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_seconds_getter)
auto [s, _] = ns.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000'000'000 });
// 5. Return 𝔽(s).
return Value((double)s.to_base_deprecated(10).to_int<i64>().value());
return Value((double)s.to_base_deprecated(10).to_number<i64>().value());
}
// 8.3.4 get Temporal.Instant.prototype.epochMilliseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.instant.prototype.epochmilliseconds
@ -86,7 +86,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::epoch_milliseconds_getter)
auto [ms, _] = ns.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000'000 });
// 5. Return 𝔽(ms).
return Value((double)ms.to_base_deprecated(10).to_int<i64>().value());
return Value((double)ms.to_base_deprecated(10).to_number<i64>().value());
}
// 8.3.5 get Temporal.Instant.prototype.epochMicroseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.instant.prototype.epochmicroseconds

View file

@ -360,7 +360,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::epoch_seconds_getter)
auto s = ns.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000'000'000 }).quotient;
// 5. Return 𝔽(s).
return Value((double)s.to_base_deprecated(10).to_int<i64>().value());
return Value((double)s.to_base_deprecated(10).to_number<i64>().value());
}
// 6.3.16 get Temporal.ZonedDateTime.prototype.epochMilliseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.epochmilliseconds
@ -377,7 +377,7 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::epoch_milliseconds_getter)
auto ms = ns.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000'000 }).quotient;
// 5. Return 𝔽(ms).
return Value((double)ms.to_base_deprecated(10).to_int<i64>().value());
return Value((double)ms.to_base_deprecated(10).to_number<i64>().value());
}
// 6.3.17 get Temporal.ZonedDateTime.prototype.epochMicroseconds, https://tc39.es/proposal-temporal/#sec-get-temporal.zoneddatetime.prototype.epochmicroseconds

View file

@ -677,7 +677,7 @@ double string_to_number(StringView string)
return bigint.to_double();
}
auto maybe_double = text.to_double(AK::TrimWhitespace::No);
auto maybe_double = text.to_number<double>(AK::TrimWhitespace::No);
if (!maybe_double.has_value())
return NAN;

View file

@ -82,7 +82,7 @@ double Token::double_value() const
}
}
// This should always be a valid double
return value_string.to_double().release_value();
return value_string.to_number<double>().release_value();
}
static u32 hex2int(char x)