diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-01-27 12:40:31 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-27 20:30:52 +0000 |
commit | 2d2f713426cee6da2fe6ffe3fbc94abc313c7d4c (patch) | |
tree | 560a4df9640cc91d30a3b4458ae35c0cfe5bb04c /Meta | |
parent | 236025df193a4841ea0984eaaca8b88b1ea1bb55 (diff) | |
download | serenity-2d2f713426cee6da2fe6ffe3fbc94abc313c7d4c.zip |
LibUnicode: Generate per-locale minimum grouping digit values
Previously, we were breaking up digits into groups without regard for
the locale's minimumGroupingDigits value in the CLDR. This value is 1 in
most locales, but is 2 in locales such as pl-PL. What this means is that
in those locales, the group separator should only be inserted if the
thousands group has at least 2 digits. So 1000 is formatted as "1,000"
in en-US, but "1000" in pl-PL. And 10000 is "10,000" in en-US and
"10 000" in pl-PL.
Diffstat (limited to 'Meta')
-rw-r--r-- | Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp index 715a3e3dc9..167a9e1e46 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp @@ -255,6 +255,7 @@ struct AK::Traits<Unit> : public GenericTraits<Unit> { struct Locale { Vector<NumberSystemIndexType> number_systems; HashMap<String, UnitIndexType> units {}; + u8 minimum_grouping_digits { 0 }; }; struct UnicodeLocaleData { @@ -455,6 +456,7 @@ static ErrorOr<void> parse_number_systems(String locale_numbers_path, UnicodeLoc auto const& main_object = numbers.as_object().get("main"sv); auto const& locale_object = main_object.as_object().get(numbers_path.parent().basename()); auto const& locale_numbers_object = locale_object.as_object().get("numbers"sv); + auto const& minimum_grouping_digits = locale_numbers_object.as_object().get("minimumGroupingDigits"sv); Vector<Optional<NumberSystem>> number_systems; number_systems.resize(locale_data.number_systems.size()); @@ -605,6 +607,7 @@ static ErrorOr<void> parse_number_systems(String locale_numbers_path, UnicodeLoc locale.number_systems.append(system_index); } + locale.minimum_grouping_digits = minimum_grouping_digits.as_string().template to_uint<u8>().value(); return {}; } @@ -872,6 +875,21 @@ struct Unit { locale_data.unique_systems.generate(generator, "NumberSystemData"sv, "s_number_systems"sv, 10); locale_data.unique_units.generate(generator, "Unit"sv, "s_units"sv, 10); + auto locales = locale_data.locales.keys(); + quick_sort(locales); + + generator.set("size", String::number(locales.size())); + generator.append(R"~~~( +static constexpr Array<u8, @size@> s_minimum_grouping_digits { { )~~~"); + + bool first = true; + for (auto const& locale : locales) { + generator.append(first ? " " : ", "); + generator.append(String::number(locale_data.locales.find(locale)->value.minimum_grouping_digits)); + first = false; + } + generator.append(" } };\n"); + auto append_map = [&](String name, auto type, auto const& map) { generator.set("name", name); generator.set("type", type); @@ -959,8 +977,14 @@ Optional<StringView> get_number_system_symbol(StringView locale, StringView syst Optional<NumberGroupings> get_number_system_groupings(StringView locale, StringView system) { + auto locale_value = locale_from_string(locale); + if (!locale_value.has_value()) + return {}; + + u8 minimum_grouping_digits = s_minimum_grouping_digits[to_underlying(*locale_value) - 1]; + if (auto const* number_system = find_number_system(locale, system); number_system != nullptr) - return NumberGroupings { number_system->primary_grouping_size, number_system->secondary_grouping_size }; + return NumberGroupings { minimum_grouping_digits, number_system->primary_grouping_size, number_system->secondary_grouping_size }; return {}; } |