From 453d3063f5b484d46dd6e704b296bb6191ad8f13 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 26 Jan 2023 15:54:09 +0000 Subject: LibJS: Port pad_iso_year() to String --- Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp | 8 ++++---- Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h | 4 ++-- Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTime.cpp | 2 +- Userland/Libraries/LibJS/Runtime/Temporal/PlainMonthDay.cpp | 2 +- Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonth.cpp | 2 +- 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 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 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 - * Copyright (c) 2021-2022, Linus Groh + * Copyright (c) 2021-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -52,7 +52,7 @@ DateDurationRecord difference_iso_date(VM&, i32 year1, u8 month1, u8 day1, i32 y ThrowCompletionOr 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 pad_iso_year(VM&, i32 y); ThrowCompletionOr temporal_date_to_string(VM&, PlainDate&, StringView show_calendar); ThrowCompletionOr 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 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 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 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)); -- cgit v1.2.3