mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-01 20:10:28 +00:00
LibJS: Make string_to_number() return double instead of Optional<Value>
This would never return an empty optional or non-numeric value, and in fact every caller as_double()'d the value right away. Let's make the type match reality instead :^)
This commit is contained in:
parent
9fb7f7fceb
commit
e77503e49b
Notes:
sideshowbarker
2024-07-17 04:09:56 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/e77503e49b Pull-request: https://github.com/SerenityOS/serenity/pull/17682
3 changed files with 14 additions and 14 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
|
@ -570,7 +570,7 @@ double parse_time_zone_offset_string(StringView offset_string)
|
|||
auto parsed_hours = *parse_result->time_zone_utc_offset_hour;
|
||||
|
||||
// 10. Let hours be ℝ(StringToNumber(CodePointsToString(parsedHours))).
|
||||
auto hours = string_to_number(parsed_hours)->as_double();
|
||||
auto hours = string_to_number(parsed_hours);
|
||||
|
||||
double minutes { 0 };
|
||||
double seconds { 0 };
|
||||
|
@ -587,7 +587,7 @@ double parse_time_zone_offset_string(StringView offset_string)
|
|||
auto parsed_minutes = *parse_result->time_zone_utc_offset_minute;
|
||||
|
||||
// b. Let minutes be ℝ(StringToNumber(CodePointsToString(parsedMinutes))).
|
||||
minutes = string_to_number(parsed_minutes)->as_double();
|
||||
minutes = string_to_number(parsed_minutes);
|
||||
}
|
||||
|
||||
// 13. If parseResult does not contain two MinuteSecond Parse Nodes, then
|
||||
|
@ -601,7 +601,7 @@ double parse_time_zone_offset_string(StringView offset_string)
|
|||
auto parsed_seconds = *parse_result->time_zone_utc_offset_second;
|
||||
|
||||
// b. Let seconds be ℝ(StringToNumber(CodePointsToString(parsedSeconds))).
|
||||
seconds = string_to_number(parsed_seconds)->as_double();
|
||||
seconds = string_to_number(parsed_seconds);
|
||||
}
|
||||
|
||||
// 15. If parseResult does not contain a TemporalDecimalFraction Parse Node, then
|
||||
|
@ -621,7 +621,7 @@ double parse_time_zone_offset_string(StringView offset_string)
|
|||
auto nanoseconds_string = fraction.substring_view(1, 9);
|
||||
|
||||
// d. Let nanoseconds be ℝ(StringToNumber(nanosecondsString)).
|
||||
nanoseconds = string_to_number(nanoseconds_string)->as_double();
|
||||
nanoseconds = string_to_number(nanoseconds_string);
|
||||
}
|
||||
|
||||
// 17. Return sign × (((hours × 60 + minutes) × 60 + seconds) × 10^9 + nanoseconds).
|
||||
|
|
|
@ -664,36 +664,36 @@ static Optional<NumberParseResult> parse_number_text(StringView text)
|
|||
}
|
||||
|
||||
// 7.1.4.1.1 StringToNumber ( str ), https://tc39.es/ecma262/#sec-stringtonumber
|
||||
Optional<Value> string_to_number(StringView string)
|
||||
double string_to_number(StringView string)
|
||||
{
|
||||
// 1. Let text be StringToCodePoints(str).
|
||||
DeprecatedString text = Utf8View(string).trim(whitespace_characters, AK::TrimMode::Both).as_string();
|
||||
|
||||
// 2. Let literal be ParseText(text, StringNumericLiteral).
|
||||
if (text.is_empty())
|
||||
return Value(0);
|
||||
return 0;
|
||||
if (text == "Infinity" || text == "+Infinity")
|
||||
return js_infinity();
|
||||
return INFINITY;
|
||||
if (text == "-Infinity")
|
||||
return js_negative_infinity();
|
||||
return -INFINITY;
|
||||
|
||||
auto result = parse_number_text(text);
|
||||
|
||||
// 3. If literal is a List of errors, return NaN.
|
||||
if (!result.has_value())
|
||||
return js_nan();
|
||||
return NAN;
|
||||
|
||||
// 4. Return StringNumericValue of literal.
|
||||
if (result->base != 10) {
|
||||
auto bigint = Crypto::UnsignedBigInteger::from_base(result->base, result->literal);
|
||||
return Value(bigint.to_double());
|
||||
return bigint.to_double();
|
||||
}
|
||||
|
||||
auto maybe_double = text.to_double(AK::TrimWhitespace::No);
|
||||
if (!maybe_double.has_value())
|
||||
return js_nan();
|
||||
return NAN;
|
||||
|
||||
return Value(*maybe_double);
|
||||
return *maybe_double;
|
||||
}
|
||||
|
||||
// 7.1.4 ToNumber ( argument ), https://tc39.es/ecma262/#sec-tonumber
|
||||
|
|
|
@ -563,7 +563,7 @@ enum class NumberToStringMode {
|
|||
};
|
||||
ErrorOr<String> number_to_string(double, NumberToStringMode = NumberToStringMode::WithExponent);
|
||||
DeprecatedString number_to_deprecated_string(double, NumberToStringMode = NumberToStringMode::WithExponent);
|
||||
Optional<Value> string_to_number(StringView);
|
||||
double string_to_number(StringView);
|
||||
|
||||
inline bool Value::operator==(Value const& value) const { return same_value(*this, value); }
|
||||
|
||||
|
|
Loading…
Reference in a new issue