diff options
author | Linus Groh <mail@linusgroh.de> | 2023-03-01 14:48:12 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-03-01 17:55:42 +0000 |
commit | e77503e49bec6917ab5104b0353f39fcaa48de41 (patch) | |
tree | 7110d7b05d27dbd39d64d3426f7530b937ed175a /Userland/Libraries | |
parent | 9fb7f7fcebed4cea92c7826f1244a81e5828851a (diff) | |
download | serenity-e77503e49bec6917ab5104b0353f39fcaa48de41.zip |
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 :^)
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Date.cpp | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Value.cpp | 16 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Value.h | 2 |
3 files changed, 14 insertions, 14 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Date.cpp b/Userland/Libraries/LibJS/Runtime/Date.cpp index 2c42a60fa4..cb0251ed98 100644 --- a/Userland/Libraries/LibJS/Runtime/Date.cpp +++ b/Userland/Libraries/LibJS/Runtime/Date.cpp @@ -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). diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 0dd7bbc2d6..660fe78bb2 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -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 diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 2f3cf2d11c..81ac4c57ec 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -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); } |