diff options
author | Linus Groh <mail@linusgroh.de> | 2023-01-26 15:54:09 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-01-26 20:20:54 +0000 |
commit | 453d3063f5b484d46dd6e704b296bb6191ad8f13 (patch) | |
tree | 4cd5f839ee5bf4944916b1f6bc2c7b21b7921595 | |
parent | 2aa4fcc318d34265522a32661377575700fc64d9 (diff) | |
download | serenity-453d3063f5b484d46dd6e704b296bb6191ad8f13.zip |
LibJS: Port pad_iso_year() to String
5 files changed, 9 insertions, 9 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp index b2af52e179..8d3aec199b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp @@ -395,14 +395,14 @@ ISODateRecord balance_iso_date(double year, double month, double day) } // 3.5.7 PadISOYear ( y ), https://tc39.es/proposal-temporal/#sec-temporal-padisoyear -DeprecatedString pad_iso_year(i32 y) +ThrowCompletionOr<String> pad_iso_year(VM& vm, i32 y) { // 1. Assert: y is an integer. // 2. If y ≥ 0 and y ≤ 9999, then if (y >= 0 && y <= 9999) { // a. Return ToZeroPaddedDecimalString(y, 4). - return DeprecatedString::formatted("{:04}", y); + return TRY_OR_THROW_OOM(vm, String::formatted("{:04}", y)); } // 3. If y > 0, let yearSign be "+"; otherwise, let yearSign be "-". @@ -410,7 +410,7 @@ DeprecatedString pad_iso_year(i32 y) // 4. Let year be ToZeroPaddedDecimalString(abs(y), 6). // 5. Return the string-concatenation of yearSign and year. - return DeprecatedString::formatted("{}{:06}", year_sign, abs(y)); + return TRY_OR_THROW_OOM(vm, String::formatted("{}{:06}", year_sign, abs(y))); } // 3.5.8 TemporalDateToString ( temporalDate, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporaldatetostring @@ -420,7 +420,7 @@ ThrowCompletionOr<DeprecatedString> temporal_date_to_string(VM& vm, PlainDate& t // 2. Assert: temporalDate has an [[InitializedTemporalDate]] internal slot. // 3. Let year be ! PadISOYear(temporalDate.[[ISOYear]]). - auto year = pad_iso_year(temporal_date.iso_year()); + auto year = MUST_OR_THROW_OOM(pad_iso_year(vm, temporal_date.iso_year())); // 4. Let month be ToZeroPaddedDecimalString(monthDay.[[ISOMonth]], 2). auto month = DeprecatedString::formatted("{:02}", temporal_date.iso_month()); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h index a3ad5773bd..1a30e6d48e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h @@ -1,6 +1,6 @@ /* * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> - * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org> + * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -52,7 +52,7 @@ DateDurationRecord difference_iso_date(VM&, i32 year1, u8 month1, u8 day1, i32 y ThrowCompletionOr<ISODateRecord> regulate_iso_date(VM&, double year, double month, double day, StringView overflow); bool is_valid_iso_date(i32 year, u8 month, u8 day); ISODateRecord balance_iso_date(double year, double month, double day); -DeprecatedString pad_iso_year(i32 y); +ThrowCompletionOr<String> pad_iso_year(VM&, i32 y); ThrowCompletionOr<DeprecatedString> temporal_date_to_string(VM&, PlainDate&, StringView show_calendar); ThrowCompletionOr<ISODateRecord> add_iso_date(VM&, i32 year, u8 month, u8 day, double years, double months, double weeks, double days, StringView overflow); i8 compare_iso_date(i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp index c6d26368a4..bf713800ed 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp @@ -264,7 +264,7 @@ ThrowCompletionOr<DeprecatedString> temporal_date_time_to_string(VM& vm, i32 iso auto calendar_string = TRY(maybe_format_calendar_annotation(vm, calendar, show_calendar)); // 9. Return the string-concatenation of year, the code unit 0x002D (HYPHEN-MINUS), month, the code unit 0x002D (HYPHEN-MINUS), day, 0x0054 (LATIN CAPITAL LETTER T), hour, the code unit 0x003A (COLON), minute, seconds, and calendarString. - return DeprecatedString::formatted("{}-{:02}-{:02}T{:02}:{:02}{}{}", pad_iso_year(iso_year), iso_month, iso_day, hour, minute, seconds, calendar_string); + return DeprecatedString::formatted("{}-{:02}-{:02}T{:02}:{:02}{}{}", MUST_OR_THROW_OOM(pad_iso_year(vm, iso_year)), iso_month, iso_day, hour, minute, seconds, calendar_string); } // 5.5.7 CompareISODateTime ( y1, mon1, d1, h1, min1, s1, ms1, mus1, ns1, y2, mon2, d2, h2, min2, s2, ms2, mus2, ns2 ), https://tc39.es/proposal-temporal/#sec-temporal-compareisodatetime diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp index b88012f5e1..ef56ff155a 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp @@ -191,7 +191,7 @@ ThrowCompletionOr<String> temporal_month_day_to_string(VM& vm, PlainMonthDay& mo if (show_calendar.is_one_of("always"sv, "critical"sv) || calendar_id != "iso8601"sv) { // a. Let year be ! PadISOYear(monthDay.[[ISOYear]]). // b. Set result to the string-concatenation of year, the code unit 0x002D (HYPHEN-MINUS), and result. - result = TRY_OR_THROW_OOM(vm, String::formatted("{}-{}", pad_iso_year(month_day.iso_year()), result)); + result = TRY_OR_THROW_OOM(vm, String::formatted("{}-{}", MUST_OR_THROW_OOM(pad_iso_year(vm, month_day.iso_year())), result)); } // 8. Let calendarString be ! FormatCalendarAnnotation(calendarID, showCalendar). diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp index ac3799ac2b..342467aba9 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp @@ -209,7 +209,7 @@ ThrowCompletionOr<DeprecatedString> temporal_year_month_to_string(VM& vm, PlainY // 3. Let year be ! PadISOYear(yearMonth.[[ISOYear]]). // 4. Let month be ToZeroPaddedDecimalString(yearMonth.[[ISOMonth]], 2). // 5. Let result be the string-concatenation of year, the code unit 0x002D (HYPHEN-MINUS), and month. - auto result = DeprecatedString::formatted("{}-{:02}", pad_iso_year(year_month.iso_year()), year_month.iso_month()); + auto result = DeprecatedString::formatted("{}-{:02}", MUST_OR_THROW_OOM(pad_iso_year(vm, year_month.iso_year())), year_month.iso_month()); // 6. Let calendarID be ? ToString(yearMonth.[[Calendar]]). auto calendar_id = TRY(Value(&year_month.calendar()).to_deprecated_string(vm)); |