diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-07-07 12:05:05 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-07-08 11:51:54 +0200 |
commit | 8aeacccd82208ef3f74c41739978251b12a00ac8 (patch) | |
tree | 2b6dcb5a9bf1f1a91b130e11b62aa65916a53c4a /Meta/Lagom | |
parent | ea78bac36db527abe0e80dce17dc03372e4f7065 (diff) | |
download | serenity-8aeacccd82208ef3f74c41739978251b12a00ac8.zip |
LibUnicode: Generate a list of available plural categories per locale
Separate lists are generated for cardinal and ordinal form.
Diffstat (limited to 'Meta/Lagom')
-rw-r--r-- | Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodePluralRules.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodePluralRules.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodePluralRules.cpp index 3b8fdc818d..8582b786f3 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodePluralRules.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodePluralRules.cpp @@ -503,6 +503,21 @@ static constexpr Array<PluralCategoryFunction, @size@> s_@form@_functions { {)~~ )~~~"); }; + auto append_categories = [&](auto const& name, auto const& rules) { + generator.set("name", name); + generator.set("size", String::number(rules.size() + 1)); + + generator.append(R"~~~( +static constexpr Array<PluralCategory, @size@> @name@ { { PluralCategory::Other)~~~"); + + for (auto [category, condition] : rules) { + generator.set("category"sv, format_identifier({}, category)); + generator.append(", PluralCategory::@category@"sv); + } + + generator.append("} };"); + }; + append_string_conversions("PluralCategory"sv, "plural_category"sv, locale_data.categories); for (auto [locale, rules] : locale_data.locales) { @@ -513,6 +528,18 @@ static constexpr Array<PluralCategoryFunction, @size@> s_@form@_functions { {)~~ append_lookup_table("cardinal"sv); append_lookup_table("ordinal"sv); + generate_mapping(generator, locales, "PluralCategory"sv, "s_cardinal_categories"sv, "s_cardinal_categories_{}", format_identifier, + [&](auto const& name, auto const& locale) { + auto& rules = locale_data.locales.find(locale)->value; + append_categories(name, rules.rules_for_form("cardinal"sv)); + }); + + generate_mapping(generator, locales, "PluralCategory"sv, "s_ordinal_categories"sv, "s_ordinal_categories_{}", format_identifier, + [&](auto const& name, auto const& locale) { + auto& rules = locale_data.locales.find(locale)->value; + append_categories(name, rules.rules_for_form("ordinal"sv)); + }); + generator.append(R"~~~( PluralCategory determine_plural_category(StringView locale, PluralForm form, PluralOperands operands) { @@ -535,6 +562,24 @@ PluralCategory determine_plural_category(StringView locale, PluralForm form, Plu return decider(move(operands)); } +Span<PluralCategory const> available_plural_categories(StringView locale, PluralForm form) +{ + auto locale_value = locale_from_string(locale); + if (!locale_value.has_value()) + return {}; + + auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None. + + switch (form) { + case PluralForm::Cardinal: + return s_cardinal_categories[locale_index]; + case PluralForm::Ordinal: + return s_ordinal_categories[locale_index]; + } + + VERIFY_NOT_REACHED(); +} + } )~~~"); |