1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 02:07:35 +00:00

LibJS: Update spec comments to use ToZeroPaddedDecimalString AO

This is an editorial change in the ECMA-262 and Temporal specs.

See:
- 843d8b8
- 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:
Linus Groh 2022-04-12 22:20:33 +01:00
parent 431a9938a8
commit 5397278bfc
10 changed files with 41 additions and 45 deletions

View file

@ -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.

View file

@ -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);
}

View file

@ -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);

View file

@ -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]]).

View file

@ -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);

View file

@ -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());

View file

@ -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);

View file

@ -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());
}

View file

@ -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);
}