1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:18:12 +00:00

LibJS+LibUnicode: Don't remove {currency} keys in GetNumberFormatPattern

In order to implement Intl.NumberFormat.prototype.formatToParts, do not
replace {currency} keys in the format pattern before ECMA-402 tells us
to. Otherwise, the array return by formatToParts will not contain the
expected currency key.

Early replacement was done to avoid resolving the currency display more
than once, as it involves a couple of round trips to search through
LibUnicode data. So this adds a non-standard method to NumberFormat to
do this resolution and cache the result.

Another side effect of this change is that LibUnicode must replace unit
format patterns of the form "{0} {1}" during code generation. These were
previously skipped during code generation because LibJS would just
replace the keys with the currency display at runtime. But now that the
currency display injection is delayed, any {0} or {1} keys in the format
pattern will cause PartitionNumberPattern to abort.
This commit is contained in:
Timothy Flynn 2021-11-13 10:15:33 -05:00 committed by Linus Groh
parent d872d030f1
commit c65dea64bd
5 changed files with 58 additions and 50 deletions

View file

@ -997,7 +997,7 @@ Optional<NumberFormat> select_currency_unit_pattern(StringView locale, StringVie
}
// https://www.unicode.org/reports/tr35/tr35-numbers.html#Currencies
String create_currency_format_pattern(StringView currency_display, StringView base_pattern)
Optional<String> augment_currency_format_pattern([[maybe_unused]] StringView currency_display, [[maybe_unused]] StringView base_pattern)
{
#if ENABLE_UNICODE_DATA
constexpr auto number_key = "{number}"sv;
@ -1011,7 +1011,7 @@ String create_currency_format_pattern(StringView currency_display, StringView ba
VERIFY(currency_index.has_value());
Utf8View utf8_currency_display { currency_display };
Optional<String> currency_display_with_spacing;
Optional<String> currency_key_with_spacing;
auto last_code_point = [](StringView string) {
Utf8View utf8_string { string };
@ -1030,7 +1030,7 @@ String create_currency_format_pattern(StringView currency_display, StringView ba
u32 first_currency_code_point = *utf8_currency_display.begin();
if (!code_point_has_general_category(first_currency_code_point, GeneralCategory::Symbol))
currency_display_with_spacing = String::formatted("{}{}", spacing, currency_display);
currency_key_with_spacing = String::formatted("{}{}", spacing, currency_key);
}
} else {
u32 last_pattern_code_point = last_code_point(base_pattern.substring_view(0, *number_index));
@ -1039,15 +1039,15 @@ String create_currency_format_pattern(StringView currency_display, StringView ba
u32 last_currency_code_point = last_code_point(currency_display);
if (!code_point_has_general_category(last_currency_code_point, GeneralCategory::Symbol))
currency_display_with_spacing = String::formatted("{}{}", currency_display, spacing);
currency_key_with_spacing = String::formatted("{}{}", currency_key, spacing);
}
}
if (currency_display_with_spacing.has_value())
return base_pattern.replace(currency_key, *currency_display_with_spacing);
if (currency_key_with_spacing.has_value())
return base_pattern.replace(currency_key, *currency_key_with_spacing);
#endif
return base_pattern.replace(currency_key, currency_display);
return {};
}
String LanguageID::to_string() const