mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibJS: Update spec comments to use ToZeroPaddedDecimalString AO
This is an editorial change in the ECMA-262 and Temporal specs. See: - https://github.com/tc39/ecma262/commit/843d8b8 - https://github.com/tc39/proposal-temporal/commit/f9211d9 Note that we don't actually need to implement the AO as we already have String::formatted() for this, and use unified format strings instead of zero-padding in individual steps in many cases anyway.
This commit is contained in:
parent
431a9938a8
commit
5397278bfc
Notes:
sideshowbarker
2024-07-17 20:58:35 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/5397278bfc Pull-request: https://github.com/SerenityOS/serenity/pull/13645 Reviewed-by: https://github.com/trflynn89 ✅
10 changed files with 41 additions and 45 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2021, Petróczi Zoltán <petroczizoltan@tutanota.com>
|
||||
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
||||
|
@ -1084,13 +1084,13 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_string)
|
|||
// 21.4.4.41.1 TimeString ( tv ), https://tc39.es/ecma262/#sec-timestring
|
||||
String time_string(double time)
|
||||
{
|
||||
// 1. Let hour be the String representation of HourFromTime(tv), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary.
|
||||
// 1. Let hour be ToZeroPaddedDecimalString(ℝ(HourFromTime(tv)), 2).
|
||||
auto hour = hour_from_time(time);
|
||||
|
||||
// 2. Let minute be the String representation of MinFromTime(tv), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary.
|
||||
// 2. Let minute be ToZeroPaddedDecimalString(ℝ(MinFromTime(tv)), 2).
|
||||
auto minute = min_from_time(time);
|
||||
|
||||
// 3. Let second be the String representation of SecFromTime(tv), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary.
|
||||
// 3. Let second be ToZeroPaddedDecimalString(ℝ(SecFromTime(tv)), 2).
|
||||
auto second = sec_from_time(time);
|
||||
|
||||
// 4. Return the string-concatenation of hour, ":", minute, ":", second, the code unit 0x0020 (SPACE), and "GMT".
|
||||
|
@ -1106,7 +1106,7 @@ String date_string(double time)
|
|||
// 2. Let month be the Name of the entry in Table 63 with the Number MonthFromTime(tv).
|
||||
auto month = short_month_names[month_from_time(time)];
|
||||
|
||||
// 3. Let day be the String representation of DateFromTime(tv), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary.
|
||||
// 3. Let day be ToZeroPaddedDecimalString(ℝ(DateFromTime(tv)), 2).
|
||||
auto day = date_from_time(time);
|
||||
|
||||
// 4. Let yv be YearFromTime(tv).
|
||||
|
@ -1115,12 +1115,9 @@ String date_string(double time)
|
|||
// 5. If yv ≥ +0𝔽, let yearSign be the empty String; otherwise, let yearSign be "-".
|
||||
auto year_sign = year >= 0 ? ""sv : "-"sv;
|
||||
|
||||
// 6. Let year be the String representation of abs(ℝ(yv)), formatted as a decimal number.
|
||||
year = abs(year);
|
||||
|
||||
// 7. Let paddedYear be ! StringPad(year, 4𝔽, "0", start).
|
||||
// 8. Return the string-concatenation of weekday, the code unit 0x0020 (SPACE), month, the code unit 0x0020 (SPACE), day, the code unit 0x0020 (SPACE), yearSign, and paddedYear.
|
||||
return String::formatted("{} {} {:02} {}{:04}", weekday, month, day, year_sign, year);
|
||||
// 6. Let paddedYear be ToZeroPaddedDecimalString(abs(ℝ(yv)), 4).
|
||||
// 7. Return the string-concatenation of weekday, the code unit 0x0020 (SPACE), month, the code unit 0x0020 (SPACE), day, the code unit 0x0020 (SPACE), yearSign, and paddedYear.
|
||||
return String::formatted("{} {} {:02} {}{:04}", weekday, month, day, year_sign, abs(year));
|
||||
}
|
||||
|
||||
// 21.4.4.41.3 TimeZoneString ( tv ), https://tc39.es/ecma262/#sec-timezoneestring
|
||||
|
@ -1145,10 +1142,10 @@ String time_zone_string(double time)
|
|||
offset *= -1;
|
||||
}
|
||||
|
||||
// 4. Let offsetMin be the String representation of MinFromTime(absOffset), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary.
|
||||
// 4. Let offsetMin be ToZeroPaddedDecimalString(ℝ(MinFromTime(absOffset)), 2).
|
||||
auto offset_min = min_from_time(offset);
|
||||
|
||||
// 5. Let offsetHour be the String representation of HourFromTime(absOffset), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary.
|
||||
// 5. Let offsetHour be ToZeroPaddedDecimalString(ℝ(HourFromTime(absOffset)), 2).
|
||||
auto offset_hour = hour_from_time(offset);
|
||||
|
||||
// 6. Let tzName be an implementation-defined string that is either the empty String or the string-concatenation of the code unit 0x0020 (SPACE), the code unit 0x0028 (LEFT PARENTHESIS), an implementation-defined timezone name, and the code unit 0x0029 (RIGHT PARENTHESIS).
|
||||
|
@ -1226,7 +1223,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_utc_string)
|
|||
// 5. Let month be the Name of the entry in Table 63 with the Number MonthFromTime(tv).
|
||||
auto month = short_month_names[month_from_time(time.as_double())];
|
||||
|
||||
// 6. Let day be the String representation of DateFromTime(tv), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary.
|
||||
// 6. Let day be ToZeroPaddedDecimalString(ℝ(DateFromTime(tv)), 2).
|
||||
auto day = date_from_time(time.as_double());
|
||||
|
||||
// 7. Let yv be YearFromTime(tv).
|
||||
|
@ -1235,12 +1232,9 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_utc_string)
|
|||
// 8. If yv ≥ +0𝔽, let yearSign be the empty String; otherwise, let yearSign be "-".
|
||||
auto year_sign = year >= 0 ? ""sv : "-"sv;
|
||||
|
||||
// 9. Let year be the String representation of abs(ℝ(yv)), formatted as a decimal number.
|
||||
year = abs(year);
|
||||
|
||||
// 10. Let paddedYear be ! StringPad(year, 4𝔽, "0", start).
|
||||
// 11. Return the string-concatenation of weekday, ",", the code unit 0x0020 (SPACE), day, the code unit 0x0020 (SPACE), month, the code unit 0x0020 (SPACE), yearSign, paddedYear, the code unit 0x0020 (SPACE), and TimeString(tv).
|
||||
auto string = String::formatted("{}, {:02} {} {}{:04} {}", weekday, day, month, year_sign, year, time_string(time.as_double()));
|
||||
// 9. Let paddedYear be ToZeroPaddedDecimalString(abs(ℝ(yv)), 4).
|
||||
// 10. Return the string-concatenation of weekday, ",", the code unit 0x0020 (SPACE), day, the code unit 0x0020 (SPACE), month, the code unit 0x0020 (SPACE), yearSign, paddedYear, the code unit 0x0020 (SPACE), and TimeString(tv).
|
||||
auto string = String::formatted("{}, {:02} {} {}{:04} {}", weekday, day, month, year_sign, abs(year), time_string(time.as_double()));
|
||||
return js_string(vm, move(string));
|
||||
}
|
||||
|
||||
|
|
|
@ -941,7 +941,7 @@ String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u
|
|||
if (precision.has<StringView>() && precision.get<StringView>() == "minute"sv)
|
||||
return String::empty();
|
||||
|
||||
// 3. Let secondsString be the string-concatenation of the code unit 0x003A (COLON) and second formatted as a two-digit decimal number, padded to the left with zeroes if necessary.
|
||||
// 3. Let secondsString be the string-concatenation of the code unit 0x003A (COLON) and ToZeroPaddedDecimalString(second, 2).
|
||||
auto seconds_string = String::formatted(":{:02}", second);
|
||||
|
||||
// 4. Let fraction be millisecond × 10^6 + microsecond × 10^3 + nanosecond.
|
||||
|
@ -955,7 +955,7 @@ String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u
|
|||
if (fraction == 0)
|
||||
return seconds_string;
|
||||
|
||||
// b. Set fraction to fraction formatted as a nine-digit decimal number, padded to the left with zeroes if necessary.
|
||||
// b. Set fraction to ToZeroPaddedDecimalString(fraction, 9).
|
||||
fraction_string = String::formatted("{:09}", fraction);
|
||||
|
||||
// c. Set fraction to the longest possible substring of fraction starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO).
|
||||
|
@ -967,7 +967,7 @@ String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u
|
|||
if (precision.get<u8>() == 0)
|
||||
return seconds_string;
|
||||
|
||||
// b. Set fraction to fraction formatted as a nine-digit decimal number, padded to the left with zeroes if necessary.
|
||||
// b. Set fraction to ToZeroPaddedDecimalString(fraction, 9)
|
||||
fraction_string = String::formatted("{:09}", fraction);
|
||||
|
||||
// c. Set fraction to the substring of fraction from 0 to precision.
|
||||
|
|
|
@ -723,6 +723,8 @@ u8 to_iso_week_of_year(i32 year, u8 month, u8 day)
|
|||
// 12.1.36 BuildISOMonthCode ( month ), https://tc39.es/proposal-temporal/#sec-buildisomonthcode
|
||||
String build_iso_month_code(u8 month)
|
||||
{
|
||||
// 1. Let numberPart be ToZeroPaddedDecimalString(month, 2).
|
||||
// 2. Return the string-concatenation of "M" and numberPart.
|
||||
return String::formatted("M{:02}", month);
|
||||
}
|
||||
|
||||
|
|
|
@ -1661,7 +1661,7 @@ String temporal_duration_to_string(double years, double months, double weeks, do
|
|||
// a. Let fraction be abs(milliseconds) × 10^6 + abs(microseconds) × 10^3 + abs(nanoseconds).
|
||||
auto fraction = fabs(milliseconds) * 1'000'000 + fabs(microseconds) * 1'000 + fabs(nanoseconds);
|
||||
|
||||
// b. Let decimalPart be fraction formatted as a nine-digit decimal number, padded to the left with zeroes if necessary.
|
||||
// b. Let decimalPart be ToZeroPaddedDecimalString(fraction, 9).
|
||||
// NOTE: padding with zeros leads to weird results when applied to a double. Not sure if that's a bug in AK/Format.h or if I'm doing this wrong.
|
||||
auto decimal_part = String::formatted("{:09}", (u64)fraction);
|
||||
|
||||
|
|
|
@ -498,14 +498,14 @@ String pad_iso_year(i32 y)
|
|||
|
||||
// 2. If y ≥ 0 and y ≤ 9999, then
|
||||
if (y >= 0 && y <= 9999) {
|
||||
// a. Return y formatted as a four-digit decimal number, padded to the left with zeroes as necessary.
|
||||
// a. Return ToZeroPaddedDecimalString(y, 4).
|
||||
return String::formatted("{:04}", y);
|
||||
}
|
||||
|
||||
// 3. If y > 0, let yearSign be "+"; otherwise, let yearSign be "-".
|
||||
auto year_sign = y > 0 ? '+' : '-';
|
||||
|
||||
// 4. Let year be abs(y), formatted as a six-digit decimal number, padded to the left with zeroes as necessary.
|
||||
// 4. Let year be ToZeroPaddedDecimalString(abs(y), 6).
|
||||
// 5. Return the string-concatenation of yearSign and year.
|
||||
return String::formatted("{}{:06}", year_sign, abs(y));
|
||||
}
|
||||
|
@ -519,10 +519,10 @@ ThrowCompletionOr<String> temporal_date_to_string(GlobalObject& global_object, P
|
|||
// 3. Let year be ! PadISOYear(temporalDate.[[ISOYear]]).
|
||||
auto year = pad_iso_year(temporal_date.iso_year());
|
||||
|
||||
// 4. Let month be temporalDate.[[ISOMonth]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 4. Let month be ToZeroPaddedDecimalString(monthDay.[[ISOMonth]], 2).
|
||||
auto month = String::formatted("{:02}", temporal_date.iso_month());
|
||||
|
||||
// 5. Let day be temporalDate.[[ISODay]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 5. Let day be ToZeroPaddedDecimalString(monthDay.[[ISODay]], 2).
|
||||
auto day = String::formatted("{:02}", temporal_date.iso_day());
|
||||
|
||||
// 6. Let calendarID be ? ToString(temporalDate.[[Calendar]]).
|
||||
|
|
|
@ -275,10 +275,10 @@ ThrowCompletionOr<String> temporal_date_time_to_string(GlobalObject& global_obje
|
|||
// 1. Assert: isoYear, isoMonth, isoDay, hour, minute, second, millisecond, microsecond, and nanosecond are integers.
|
||||
|
||||
// 2. Let year be ! PadISOYear(isoYear).
|
||||
// 3. Let month be isoMonth formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 4. Let day be isoDay formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 5. Let hour be hour formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 6. Let minute be minute formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 3. Let month be ToZeroPaddedDecimalString(isoMonth, 2).
|
||||
// 4. Let day be ToZeroPaddedDecimalString(isoDay, 2).
|
||||
// 5. Let hour be ToZeroPaddedDecimalString(hour, 2).
|
||||
// 6. Let minute be ToZeroPaddedDecimalString(minute, 2).
|
||||
|
||||
// 7. Let seconds be ! FormatSecondsStringPart(second, millisecond, microsecond, nanosecond, precision).
|
||||
auto seconds = format_seconds_string_part(second, millisecond, microsecond, nanosecond, precision);
|
||||
|
|
|
@ -176,8 +176,8 @@ ThrowCompletionOr<String> temporal_month_day_to_string(GlobalObject& global_obje
|
|||
// 1. Assert: Type(monthDay) is Object.
|
||||
// 2. Assert: monthDay has an [[InitializedTemporalMonthDay]] internal slot.
|
||||
|
||||
// 3. Let month be monthDay.[[ISOMonth]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 4. Let day be monthDay.[[ISODay]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 3. Let month be ToZeroPaddedDecimalString(temporalDate.[[ISOMonth]], 2).
|
||||
// 4. Let day be ToZeroPaddedDecimalString(temporalDate.[[ISODay]], 2).
|
||||
// 5. Let result be the string-concatenation of month, the code unit 0x002D (HYPHEN-MINUS), and day.
|
||||
auto result = String::formatted("{:02}-{:02}", month_day.iso_month(), month_day.iso_day());
|
||||
|
||||
|
|
|
@ -427,8 +427,8 @@ String temporal_time_to_string(u8 hour, u8 minute, u8 second, u16 millisecond, u
|
|||
{
|
||||
// 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
|
||||
|
||||
// 2. Let hour be hour formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 3. Let minute be minute formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 2. Let hour be ToZeroPaddedDecimalString(hour, 2).
|
||||
// 3. Let minute be ToZeroPaddedDecimalString(minute, 2).
|
||||
|
||||
// 4. Let seconds be ! FormatSecondsStringPart(second, millisecond, microsecond, nanosecond, precision).
|
||||
auto seconds = format_seconds_string_part(second, millisecond, microsecond, nanosecond, precision);
|
||||
|
|
|
@ -233,7 +233,7 @@ ThrowCompletionOr<String> temporal_year_month_to_string(GlobalObject& global_obj
|
|||
// 2. Assert: yearMonth has an [[InitializedTemporalYearMonth]] internal slot.
|
||||
|
||||
// 3. Let year be ! PadISOYear(yearMonth.[[ISOYear]]).
|
||||
// 4. Let month be yearMonth.[[ISOMonth]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 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 = String::formatted("{}-{:02}", pad_iso_year(year_month.iso_year()), year_month.iso_month());
|
||||
|
||||
|
@ -242,7 +242,7 @@ ThrowCompletionOr<String> temporal_year_month_to_string(GlobalObject& global_obj
|
|||
|
||||
// 7. If showCalendar is "always" or if calendarID is not "iso8601", then
|
||||
if (show_calendar == "always"sv || calendar_id != "iso8601") {
|
||||
// a. Let day be yearMonth.[[ISODay]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// a. Let day be ToZeroPaddedDecimalString(yearMonth.[[ISODay]], 2).
|
||||
// b. Set result to the string-concatenation of result, the code unit 0x002D (HYPHEN-MINUS), and day.
|
||||
result = String::formatted("{}-{:02}", result, year_month.iso_day());
|
||||
}
|
||||
|
|
|
@ -344,16 +344,16 @@ String format_time_zone_offset_string(double offset_nanoseconds)
|
|||
// 7. Let hours be floor(offsetNanoseconds / (3.6 × 10^12)).
|
||||
auto hours = offset / 3600000000000;
|
||||
|
||||
// 8. Let h be hours, formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 8. Let h be ToZeroPaddedDecimalString(hours, 2).
|
||||
builder.appendff("{:02}", hours);
|
||||
// 9. Let m be minutes, formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 9. Let m be ToZeroPaddedDecimalString(minutes, 2).
|
||||
builder.appendff(":{:02}", minutes);
|
||||
// 10. Let s be seconds, formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// Handled by steps 10 & 11
|
||||
// 10. Let s be ToZeroPaddedDecimalString(seconds, 2).
|
||||
// NOTE: Handled by steps 11 & 12
|
||||
|
||||
// 11. If nanoseconds ≠ 0, then
|
||||
if (nanoseconds != 0) {
|
||||
// a. Let fraction be nanoseconds, formatted as a nine-digit decimal number, padded to the left with zeroes if necessary.
|
||||
// a. Let fraction be ToZeroPaddedDecimalString(nanoseconds, 9).
|
||||
// b. Set fraction to the longest possible substring of fraction starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO).
|
||||
// c. Let post be the string-concatenation of the code unit 0x003A (COLON), s, the code unit 0x002E (FULL STOP), and fraction.
|
||||
builder.appendff(":{:02}.{}", seconds, String::formatted("{:09}", nanoseconds).trim("0"sv, TrimMode::Right));
|
||||
|
@ -391,8 +391,8 @@ String format_iso_time_zone_offset_string(double offset_nanoseconds)
|
|||
// 6. Let hours be floor(offsetNanoseconds / (3600 × 10^9)).
|
||||
auto hours = floor(offset_nanoseconds / 3600000000000);
|
||||
|
||||
// 7. Let h be hours, formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 8. Let m be minutes, formatted as a two-digit decimal number, padded to the left with a zero if necessary.
|
||||
// 7. Let h be ToZeroPaddedDecimalString(hours, 2).
|
||||
// 8. Let m be ToZeroPaddedDecimalString(minutes, 2).
|
||||
// 9. Return the string-concatenation of sign, h, the code unit 0x003A (COLON), and m.
|
||||
return String::formatted("{}{:02}:{:02}", sign, (u32)hours, (u32)minutes);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue