From c65dea64bd9adaef6955981c5f633cca485218aa Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 13 Nov 2021 10:15:33 -0500 Subject: 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. --- .../LibUnicode/GenerateUnicodeNumberFormat.cpp | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'Meta/Lagom') diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp index fb19c3ae95..0c828ba5bc 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp @@ -79,24 +79,23 @@ struct UnicodeLocaleData { Vector numeric_symbols; }; -static void parse_number_pattern(String pattern, UnicodeLocaleData& locale_data, NumberFormat& format, bool is_unit_pattern = false) +static void parse_number_pattern(String pattern, UnicodeLocaleData& locale_data, NumberFormat& format) { // https://unicode.org/reports/tr35/tr35-numbers.html#Number_Format_Patterns // https://cldr.unicode.org/translation/number-currency-formats/number-and-currency-patterns auto replace_patterns = [&](String pattern) { - if (is_unit_pattern) { - // Skip parsing unit patterns, which are of the form "{0} {1}", and do not contain any - // of the replacement patterns below. - return pattern; - } - static HashMap replacements = { + { "{0}"sv, "{number}"sv }, + { "{1}"sv, "{currency}"sv }, { "%"sv, "{percentSign}"sv }, { "+"sv, "{plusSign}"sv }, { "-"sv, "{minusSign}"sv }, { "ยค"sv, "{currency}"sv }, // U+00A4 Currency Sign }; + for (auto const& replacement : replacements) + pattern = pattern.replace(replacement.key, replacement.value, true); + if (auto start_number_index = pattern.find_any_of("#0"sv, String::SearchDirection::Forward); start_number_index.has_value()) { auto end_number_index = *start_number_index + 1; @@ -111,9 +110,6 @@ static void parse_number_pattern(String pattern, UnicodeLocaleData& locale_data, pattern.substring_view(end_number_index)); } - for (auto const& replacement : replacements) - pattern = pattern.replace(replacement.key, replacement.value, true); - return pattern; }; @@ -173,18 +169,16 @@ static void parse_number_systems(String locale_numbers_path, UnicodeLocaleData& return; NumberFormat format {}; - bool is_unit_pattern = false; if (auto type = split_key[0].template to_uint(); type.has_value()) { VERIFY(*type % 10 == 0); format.magnitude = static_cast(log10(*type)); } else { VERIFY(split_key[0] == "unitPattern"sv); - is_unit_pattern = true; } format.plurality = NumberFormat::plurality_from_string(split_key[2]); - parse_number_pattern(value.as_string(), locale_data, format, is_unit_pattern); + parse_number_pattern(value.as_string(), locale_data, format); result.append(move(format)); }); -- cgit v1.2.3