blob: 7b30f69b0773d62b261835a9d523a4f3aca91583 [file] [log] [blame]
*   "one: n is 1; few: n in 2..4"
* This defines two rules, for 'one' and 'few'. The condition for * 'one' is "n is 1" which means that the number must be equal to * 1 for this condition to pass. The condition for 'few' is * "n in 2..4" which means that the number must be between 2 and * 4 inclusive for this condition to pass. All other numbers * are assigned the keyword "other" by the default rule. *

*    "zero: n is 0; one: n is 1; zero: n mod 100 in 1..19"
* This illustrates that the same keyword can be defined multiple times. * Each rule is examined in order, and the first keyword whose condition * passes is the one returned. Also notes that a modulus is applied * to n in the last rule. Thus its condition holds for 119, 219, 319... *

*    "one: n is 1; few: n mod 10 in 2..4 and n mod 100 not in 12..14"
* This illustrates conjunction and negation. The condition for 'few' * has two parts, both of which must be met: "n mod 10 in 2..4" and * "n mod 100 not in 12..14". The first part applies a modulus to n * before the test as in the previous example. The second part applies * a different modulus and also uses negation, thus it matches all * numbers _not_ in 12, 13, 14, 112, 113, 114, 212, 213, 214... *

*

* Syntax:

* \code
* rules         = rule (';' rule)*
* rule          = keyword ':' condition
* keyword       =
* condition     = and_condition ('or' and_condition)*
* and_condition = relation ('and' relation)*
* relation      = is_relation | in_relation | within_relation | 'n'
* is_relation   = expr 'is' ('not')? value
* in_relation   = expr ('not')? 'in' range_list
* within_relation = expr ('not')? 'within' range
* expr          = ('n' | 'i' | 'f' | 'v' | 'j') ('mod' value)?
* range_list    = (range | value) (',' range_list)*
* value         = digit+  ('.' digit+)?
* digit         = 0|1|2|3|4|5|6|7|8|9
* range         = value'..'value
* \endcode
*

*

*

* The i, f, and v values are defined as follows: *

*
*
• i to be the integer digits.
• *
• f to be the visible fractional digits, as an integer.
• *
• v to be the number of visible fraction digits.
• *
• j is defined to only match integers. That is j is 3 fails if v != 0 (eg for 3.1 or 3.0).
• *
*

* Examples are in the following table: *

* *
nifv
1.0101
1.00102
1.3131
1.03132
1.231232
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* The difference between 'in' and 'within' is that 'in' only includes integers in the specified range, while 'within' * includes all values. Using 'within' with a range_list consisting entirely of values is the same as using 'in' (it's * not an error). *

* An "identifier" is a sequence of characters that do not have the * Unicode Pattern_Syntax or Pattern_White_Space properties. *

* The difference between 'in' and 'within' is that 'in' only includes * integers in the specified range, while 'within' includes all values. * Using 'within' with a range_list consisting entirely of values is the * same as using 'in' (it's not an error). *

*

* Keywords * could be defined by users or from ICU locale data. There are 6 * predefined values in ICU - 'zero', 'one', 'two', 'few', 'many' and * 'other'. Callers need to check the value of keyword returned by * {@link #select} method. *

* * Examples:
* UnicodeString keyword = pl->select(number);
* if (keyword== UnicodeString("one") {
*     ...
* }
* else if ( ... )
*
* Note:
*

* ICU defines plural rules for many locales based on CLDR Language Plural Rules. * For these predefined rules, see CLDR page at * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html *

*/ class U_I18N_API PluralRules : public UObject { public: /** * Constructor. * @param status Output param set to success/failure code on exit, which * must not indicate a failure before the function call. * * @stable ICU 4.0 */ PluralRules(UErrorCode& status); /** * Copy constructor. * @stable ICU 4.0 */ PluralRules(const PluralRules& other); /** * Destructor. * @stable ICU 4.0 */ virtual ~PluralRules(); /** * Clone * @stable ICU 4.0 */ PluralRules* clone() const; /** * Assignment operator. * @stable ICU 4.0 */ PluralRules& operator=(const PluralRules&); /** * Creates a PluralRules from a description if it is parsable, otherwise * returns NULL. * * @param description rule description * @param status Output param set to success/failure code on exit, which * must not indicate a failure before the function call. * @return new PluralRules pointer. NULL if there is an error. * @stable ICU 4.0 */ static PluralRules* U_EXPORT2 createRules(const UnicodeString& description, UErrorCode& status); /** * The default rules that accept any number. * * @param status Output param set to success/failure code on exit, which * must not indicate a failure before the function call. * @return new PluralRules pointer. NULL if there is an error. * @stable ICU 4.0 */ static PluralRules* U_EXPORT2 createDefaultRules(UErrorCode& status); /** * Provides access to the predefined cardinal-number PluralRules for a given * locale. * Same as forLocale(locale, UPLURAL_TYPE_CARDINAL, status). * * @param locale The locale for which a PluralRules object is * returned. * @param status Output param set to success/failure code on exit, which * must not indicate a failure before the function call. * @return The predefined PluralRules object pointer for * this locale. If there's no predefined rules for this locale, * the rules for the closest parent in the locale hierarchy * that has one will be returned. The final fallback always * returns the default 'other' rules. * @stable ICU 4.0 */ static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UErrorCode& status); /** * Provides access to the predefined PluralRules for a given * locale and the plural type. * * @param locale The locale for which a PluralRules object is * returned. * @param type The plural type (e.g., cardinal or ordinal). * @param status Output param set to success/failure code on exit, which * must not indicate a failure before the function call. * @return The predefined PluralRules object pointer for * this locale. If there's no predefined rules for this locale, * the rules for the closest parent in the locale hierarchy * that has one will be returned. The final fallback always * returns the default 'other' rules. * @stable ICU 50 */ static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UPluralType type, UErrorCode& status); #ifndef U_HIDE_INTERNAL_API /** * Return a StringEnumeration over the locales for which there is plurals data. * @return a StringEnumeration over the locales available. * @internal */ static StringEnumeration* U_EXPORT2 getAvailableLocales(UErrorCode &status); /** * Returns whether or not there are overrides. * @param locale the locale to check. * @return * @internal */ static UBool hasOverride(const Locale &locale); /** * For ICU use only. * creates a SharedPluralRules object * @internal */ static PluralRules* U_EXPORT2 internalForLocale(const Locale& locale, UPluralType type, UErrorCode& status); /** * For ICU use only. * Returns handle to the shared, cached PluralRules instance. * Caller must call removeRef() on returned value once it is done with * the shared instance. * @internal */ static const SharedPluralRules* U_EXPORT2 createSharedInstance( const Locale& locale, UPluralType type, UErrorCode& status); #endif /* U_HIDE_INTERNAL_API */ /** * Given an integer, returns the keyword of the first rule * that applies to the number. This function can be used with * isKeyword* functions to determine the keyword for default plural rules. * * @param number The number for which the rule has to be determined. * @return The keyword of the selected rule. * @stable ICU 4.0 */ UnicodeString select(int32_t number) const; /** * Given a floating-point number, returns the keyword of the first rule * that applies to the number. This function can be used with * isKeyword* functions to determine the keyword for default plural rules. * * @param number The number for which the rule has to be determined. * @return The keyword of the selected rule. * @stable ICU 4.0 */ UnicodeString select(double number) const; /** * Given a formatted number, returns the keyword of the first rule * that applies to the number. This function can be used with * isKeyword* functions to determine the keyword for default plural rules. * * A FormattedNumber allows you to specify an exponent or trailing zeros, * which can affect the plural category. To get a FormattedNumber, see * NumberFormatter. * * @param number The number for which the rule has to be determined. * @param status Set if an error occurs while selecting plural keyword. * This could happen if the FormattedNumber is invalid. * @return The keyword of the selected rule. * @stable ICU 64 */ UnicodeString select(const number::FormattedNumber& number, UErrorCode& status) const; #ifndef U_HIDE_DRAFT_API /** * Given a formatted number range, returns the overall plural form of the * range. For example, "3-5" returns "other" in English. * * To get a FormattedNumberRange, see NumberRangeFormatter. * * This method only works if PluralRules was created with a locale. If it was created * from PluralRules::createRules(), this method sets status code U_UNSUPPORTED_ERROR. * * @param range The number range onto which the rules will be applied. * @param status Set if an error occurs while selecting plural keyword. * This could happen if the FormattedNumberRange is invalid, * or if plural ranges data is unavailable. * @return The keyword of the selected rule. * @draft ICU 68 */ UnicodeString select(const number::FormattedNumberRange& range, UErrorCode& status) const; #endif // U_HIDE_DRAFT_API #ifndef U_HIDE_INTERNAL_API /** * @internal */ UnicodeString select(const IFixedDecimal &number) const; /** * @internal */ UnicodeString select(const number::impl::UFormattedNumberRangeData* urange, UErrorCode& status) const; #endif /* U_HIDE_INTERNAL_API */ /** * Returns a list of all rule keywords used in this PluralRules * object. The rule 'other' is always present by default. * * @param status Output param set to success/failure code on exit, which * must not indicate a failure before the function call. * @return StringEnumeration with the keywords. * The caller must delete the object. * @stable ICU 4.0 */ StringEnumeration* getKeywords(UErrorCode& status) const; #ifndef U_HIDE_DEPRECATED_API /** * Deprecated Function, does not return useful results. * * Originally intended to return a unique value for this keyword if it exists, * else the constant UPLRULES_NO_UNIQUE_VALUE. * * @param keyword The keyword. * @return Stub deprecated function returns UPLRULES_NO_UNIQUE_VALUE always. * @deprecated ICU 55 */ double getUniqueKeywordValue(const UnicodeString& keyword); /** * Deprecated Function, does not produce useful results. * * Originally intended to return all the values for which select() would return the keyword. * If the keyword is unknown, returns no values, but this is not an error. If * the number of values is unlimited, returns no values and -1 as the * count. * * The number of returned values is typically small. * * @param keyword The keyword. * @param dest Array into which to put the returned values. May * be NULL if destCapacity is 0. * @param destCapacity The capacity of the array, must be at least 0. * @param status The error code. Deprecated function, always sets U_UNSUPPORTED_ERROR. * @return The count of values available, or -1. This count * can be larger than destCapacity, but no more than * destCapacity values will be written. * @deprecated ICU 55 */ int32_t getAllKeywordValues(const UnicodeString &keyword, double *dest, int32_t destCapacity, UErrorCode& status); #endif /* U_HIDE_DEPRECATED_API */ /** * Returns sample values for which select() would return the keyword. If * the keyword is unknown, returns no values, but this is not an error. * * The number of returned values is typically small. * * @param keyword The keyword. * @param dest Array into which to put the returned values. May * be NULL if destCapacity is 0. * @param destCapacity The capacity of the array, must be at least 0. * @param status The error code. * @return The count of values written. * If more than destCapacity samples are available, then * only destCapacity are written, and destCapacity is returned as the count, * rather than setting a U_BUFFER_OVERFLOW_ERROR. * (The actual number of keyword values could be unlimited.) * @stable ICU 4.8 */ int32_t getSamples(const UnicodeString &keyword, double *dest, int32_t destCapacity, UErrorCode& status); #ifndef U_HIDE_INTERNAL_API /** * Internal-only function that returns FixedDecimals instead of doubles. * * Returns sample values for which select() would return the keyword. If * the keyword is unknown, returns no values, but this is not an error. * * The number of returned values is typically small. * * @param keyword The keyword. * @param dest Array into which to put the returned values. May * be NULL if destCapacity is 0. * @param destCapacity The capacity of the array, must be at least 0. * @param status The error code. * @return The count of values written. * If more than destCapacity samples are available, then * only destCapacity are written, and destCapacity is returned as the count, * rather than setting a U_BUFFER_OVERFLOW_ERROR. * (The actual number of keyword values could be unlimited.) * @internal */ int32_t getSamples(const UnicodeString &keyword, FixedDecimal *dest, int32_t destCapacity, UErrorCode& status); #endif /* U_HIDE_INTERNAL_API */ /** * Returns true if the given keyword is defined in this * PluralRules object. * * @param keyword the input keyword. * @return true if the input keyword is defined. * Otherwise, return false. * @stable ICU 4.0 */ UBool isKeyword(const UnicodeString& keyword) const; /** * Returns keyword for default plural form. * * @return keyword for default plural form. * @stable ICU 4.0 */ UnicodeString getKeywordOther() const; #ifndef U_HIDE_INTERNAL_API /** * * @internal */ UnicodeString getRules() const; #endif /* U_HIDE_INTERNAL_API */ /** * Compares the equality of two PluralRules objects. * * @param other The other PluralRules object to be compared with. * @return True if the given PluralRules is the same as this * PluralRules; false otherwise. * @stable ICU 4.0 */ virtual UBool operator==(const PluralRules& other) const; /** * Compares the inequality of two PluralRules objects. * * @param other The PluralRules object to be compared with. * @return True if the given PluralRules is not the same as this * PluralRules; false otherwise. * @stable ICU 4.0 */ UBool operator!=(const PluralRules& other) const {return !operator==(other);} /** * ICU "poor man's RTTI", returns a UClassID for this class. * * @stable ICU 4.0 * */ static UClassID U_EXPORT2 getStaticClassID(void); /** * ICU "poor man's RTTI", returns a UClassID for the actual class. * * @stable ICU 4.0 */ virtual UClassID getDynamicClassID() const; private: RuleChain *mRules; StandardPluralRanges *mStandardPluralRanges; PluralRules(); // default constructor not implemented void parseDescription(const UnicodeString& ruleData, UErrorCode &status); int32_t getNumberValue(const UnicodeString& token) const; UnicodeString getRuleFromResource(const Locale& locale, UPluralType type, UErrorCode& status); RuleChain *rulesForKeyword(const UnicodeString &keyword) const; PluralRules *clone(UErrorCode& status) const; /** * An internal status variable used to indicate that the object is in an 'invalid' state. * Used by copy constructor, the assignment operator and the clone method. */ UErrorCode mInternalStatus; friend class PluralRuleParser; }; U_NAMESPACE_END #endif /* #if !UCONFIG_NO_FORMATTING */ #endif /* U_SHOW_CPLUSPLUS_API */ #endif // _PLURRULE //eof