
Plural rules in the CLDR are of the form: "cs": { "pluralRule-count-one": "i = 1 and v = 0 @integer 1", "pluralRule-count-few": "i = 2..4 and v = 0 @integer 2~4", "pluralRule-count-many": "v != 0 @decimal 0.0~1.5, 10.0, 100.0 ...", "pluralRule-count-other": "@integer 0, 5~19, 100, 1000, 10000 ..." } The syntax is described here: https://unicode.org/reports/tr35/tr35-numbers.html#Plural_rules_syntax There are up to 2 sets of rules for each locale, a cardinal set and an ordinal set. The approach here is to generate a C++ function for each set of rules. Each condition in the rules (e.g. "i = 1 and v = 0") is transpiled to a C++ if-statement within its function. Then lookup tables are generated to match locales to their generated functions. NOTE: -Wno-parentheses-equality is added to the LibUnicodeData compile flags because the generated plural rules have lots of extra parentheses (because e.g. we need to selectively negate and combine rules). The code to generate only exactly the right number of parentheses is quite hairy, so this just tells the compiler to ignore the extras.
59 lines
1.3 KiB
C++
59 lines
1.3 KiB
C++
/*
|
|
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibUnicode/PluralRules.h>
|
|
|
|
#if ENABLE_UNICODE_DATA
|
|
# include <LibUnicode/UnicodePluralRules.h>
|
|
#endif
|
|
|
|
namespace Unicode {
|
|
|
|
#if !ENABLE_UNICODE_DATA
|
|
enum class PluralCategory : u8 {
|
|
Other,
|
|
};
|
|
#endif
|
|
|
|
PluralForm plural_form_from_string(StringView plural_form)
|
|
{
|
|
if (plural_form == "cardinal"sv)
|
|
return PluralForm::Cardinal;
|
|
if (plural_form == "ordinal"sv)
|
|
return PluralForm::Ordinal;
|
|
VERIFY_NOT_REACHED();
|
|
}
|
|
|
|
StringView plural_form_to_string(PluralForm plural_form)
|
|
{
|
|
switch (plural_form) {
|
|
case PluralForm::Cardinal:
|
|
return "cardinal"sv;
|
|
case PluralForm::Ordinal:
|
|
return "ordinal"sv;
|
|
default:
|
|
VERIFY_NOT_REACHED();
|
|
}
|
|
}
|
|
|
|
Optional<PluralCategory> __attribute__((weak)) plural_category_from_string(StringView category)
|
|
{
|
|
VERIFY(category == "other"sv);
|
|
return PluralCategory::Other;
|
|
}
|
|
|
|
StringView __attribute__((weak)) plural_category_to_string(PluralCategory category)
|
|
{
|
|
VERIFY(category == PluralCategory::Other);
|
|
return "other"sv;
|
|
}
|
|
|
|
PluralCategory __attribute__((weak)) determine_plural_category(StringView, PluralForm, PluralOperands)
|
|
{
|
|
return PluralCategory::Other;
|
|
}
|
|
|
|
}
|