diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-12-11 00:37:34 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-12-11 14:17:47 +0000 |
commit | 2a7f36b392479dbb41bb3fac6512c9b176c175ec (patch) | |
tree | 913af8f7fafd1fbaa9948af8e925432772fa59c6 /Userland/Libraries | |
parent | 9cc323b0b0434e5e480b97fb53dff82639b37570 (diff) | |
download | serenity-2a7f36b392479dbb41bb3fac6512c9b176c175ec.zip |
LibJS+LibUnicode: Generate unique numeric symbol lists
There are 443 number system objects generated, each of which held an
array of number system symbols. Of those 443 arrays, only 39 are unique.
To uniquely store these, this change moves the generated NumericSymbol
enumeration to the public LibUnicode/NumberFormat.h header with a pre-
defined set of symbols that we need. This is to ensure the generated,
unique arrays are created in a known order with known symbols. While it
is unfortunate to no longer discover these symbols at generation time,
it does allow us to ignore unwanted symbols and perform less string-to-
enumeration conversions at lookup time.
Diffstat (limited to 'Userland/Libraries')
4 files changed, 23 insertions, 12 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp index 705388477b..b870464eba 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.cpp @@ -1021,7 +1021,7 @@ ThrowCompletionOr<Vector<PatternPartition>> format_date_time_pattern(GlobalObjec // Non-standard, TR-35 requires the decimal separator before injected {fractionalSecondDigits} partitions // to adhere to the selected locale. This depends on other generated data, so it is deferred to here. else if (part == "decimal"sv) { - auto decimal_symbol = Unicode::get_number_system_symbol(data_locale, date_time_format.numbering_system(), "decimal"sv).value_or("."sv); + auto decimal_symbol = Unicode::get_number_system_symbol(data_locale, date_time_format.numbering_system(), Unicode::NumericSymbol::Decimal).value_or("."sv); result.append({ "literal"sv, decimal_symbol }); } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp index 2de65ec445..84ba644235 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp @@ -605,12 +605,12 @@ Vector<PatternPartition> partition_number_pattern(NumberFormat& number_format, d // 2. If x is NaN, then if (Value(number).is_nan()) { // a. Let n be an implementation- and locale-dependent (ILD) String value indicating the NaN value. - formatted_string = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), "nan"sv).value_or("NaN"sv); + formatted_string = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), Unicode::NumericSymbol::NaN).value_or("NaN"sv); } // 3. Else if x is a non-finite Number, then else if (!Value(number).is_finite_number()) { // a. Let n be an ILD String value indicating infinity. - formatted_string = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), "infinity"sv).value_or("infinity"sv); + formatted_string = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), Unicode::NumericSymbol::Infinity).value_or("infinity"sv); } // 4. Else, else { @@ -669,7 +669,7 @@ Vector<PatternPartition> partition_number_pattern(NumberFormat& number_format, d // d. Else if p is equal to "plusSign", then else if (part == "plusSign"sv) { // i. Let plusSignSymbol be the ILND String representing the plus sign. - auto plus_sign_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), "plusSign"sv).value_or("+"sv); + auto plus_sign_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), Unicode::NumericSymbol::PlusSign).value_or("+"sv); // ii. Append a new Record { [[Type]]: "plusSign", [[Value]]: plusSignSymbol } as the last element of result. result.append({ "plusSign"sv, plus_sign_symbol }); } @@ -677,7 +677,7 @@ Vector<PatternPartition> partition_number_pattern(NumberFormat& number_format, d // e. Else if p is equal to "minusSign", then else if (part == "minusSign"sv) { // i. Let minusSignSymbol be the ILND String representing the minus sign. - auto minus_sign_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), "minusSign"sv).value_or("-"sv); + auto minus_sign_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), Unicode::NumericSymbol::MinusSign).value_or("-"sv); // ii. Append a new Record { [[Type]]: "minusSign", [[Value]]: minusSignSymbol } as the last element of result. result.append({ "minusSign"sv, minus_sign_symbol }); } @@ -685,7 +685,7 @@ Vector<PatternPartition> partition_number_pattern(NumberFormat& number_format, d // f. Else if p is equal to "percentSign" and numberFormat.[[Style]] is "percent", then else if ((part == "percentSign"sv) && (number_format.style() == NumberFormat::Style::Percent)) { // i. Let percentSignSymbol be the ILND String representing the percent sign. - auto percent_sign_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), "percentSign"sv).value_or("%"sv); + auto percent_sign_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), Unicode::NumericSymbol::PercentSign).value_or("%"sv); // ii. Append a new Record { [[Type]]: "percentSign", [[Value]]: percentSignSymbol } as the last element of result. result.append({ "percentSign"sv, percent_sign_symbol }); } @@ -937,7 +937,7 @@ Vector<PatternPartition> partition_notation_sub_pattern(NumberFormat& number_for // 6. If the numberFormat.[[UseGrouping]] is true, then if (use_grouping) { // a. Let groupSepSymbol be the implementation-, locale-, and numbering system-dependent (ILND) String representing the grouping separator. - auto group_sep_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), "group"sv).value_or(","sv); + auto group_sep_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), Unicode::NumericSymbol::Group).value_or(","sv); // b. Let groups be a List whose elements are, in left to right order, the substrings defined by ILND set of locations within the integer. auto groups = separate_integer_into_groups(*grouping_sizes, integer); @@ -969,7 +969,7 @@ Vector<PatternPartition> partition_notation_sub_pattern(NumberFormat& number_for // 8. If fraction is not undefined, then if (fraction.has_value()) { // a. Let decimalSepSymbol be the ILND String representing the decimal separator. - auto decimal_sep_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), "decimal"sv).value_or("."sv); + auto decimal_sep_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), Unicode::NumericSymbol::Decimal).value_or("."sv); // b. Append a new Record { [[Type]]: "decimal", [[Value]]: decimalSepSymbol } as the last element of result. result.append({ "decimal"sv, decimal_sep_symbol }); // c. Append a new Record { [[Type]]: "fraction", [[Value]]: fraction } as the last element of result. @@ -993,7 +993,7 @@ Vector<PatternPartition> partition_notation_sub_pattern(NumberFormat& number_for // vi. Else if p is equal to "scientificSeparator", then else if (part == "scientificSeparator"sv) { // 1. Let scientificSeparator be the ILND String representing the exponent separator. - auto scientific_separator = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), "exponential"sv).value_or("E"sv); + auto scientific_separator = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), Unicode::NumericSymbol::Exponential).value_or("E"sv); // 2. Append a new Record { [[Type]]: "exponentSeparator", [[Value]]: scientificSeparator } as the last element of result. result.append({ "exponentSeparator"sv, scientific_separator }); } @@ -1002,7 +1002,7 @@ Vector<PatternPartition> partition_notation_sub_pattern(NumberFormat& number_for // 1. If exponent < 0, then if (exponent < 0) { // a. Let minusSignSymbol be the ILND String representing the minus sign. - auto minus_sign_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), "minusSign"sv).value_or("-"sv); + auto minus_sign_symbol = Unicode::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), Unicode::NumericSymbol::MinusSign).value_or("-"sv); // b. Append a new Record { [[Type]]: "exponentMinusSign", [[Value]]: minusSignSymbol } as the last element of result. result.append({ "exponentMinusSign"sv, minus_sign_symbol }); diff --git a/Userland/Libraries/LibUnicode/NumberFormat.cpp b/Userland/Libraries/LibUnicode/NumberFormat.cpp index cffd0d4c04..7933dd005c 100644 --- a/Userland/Libraries/LibUnicode/NumberFormat.cpp +++ b/Userland/Libraries/LibUnicode/NumberFormat.cpp @@ -16,7 +16,7 @@ namespace Unicode { -Optional<StringView> get_number_system_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView system, [[maybe_unused]] StringView symbol) +Optional<StringView> get_number_system_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView system, [[maybe_unused]] NumericSymbol symbol) { #if ENABLE_UNICODE_DATA return Detail::get_number_system_symbol(locale, system, symbol); diff --git a/Userland/Libraries/LibUnicode/NumberFormat.h b/Userland/Libraries/LibUnicode/NumberFormat.h index b56e870ad7..c08d61097d 100644 --- a/Userland/Libraries/LibUnicode/NumberFormat.h +++ b/Userland/Libraries/LibUnicode/NumberFormat.h @@ -54,7 +54,18 @@ struct NumberFormat { Vector<StringView> identifiers {}; }; -Optional<StringView> get_number_system_symbol(StringView locale, StringView system, StringView symbol); +enum class NumericSymbol : u8 { + Decimal, + Exponential, + Group, + Infinity, + MinusSign, + NaN, + PercentSign, + PlusSign, +}; + +Optional<StringView> get_number_system_symbol(StringView locale, StringView system, NumericSymbol symbol); Optional<NumberGroupings> get_number_system_groupings(StringView locale, StringView system); Optional<NumberFormat> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type); Vector<NumberFormat> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type); |