jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ******************************************************************************* jpayne@69: * Copyright (C) 2008-2015, International Business Machines Corporation and jpayne@69: * others. All Rights Reserved. jpayne@69: ******************************************************************************* jpayne@69: * jpayne@69: * jpayne@69: * File PLURRULE.H jpayne@69: * jpayne@69: * Modification History:* jpayne@69: * Date Name Description jpayne@69: * jpayne@69: ******************************************************************************** jpayne@69: */ jpayne@69: jpayne@69: #ifndef PLURRULE jpayne@69: #define PLURRULE jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C++ API: PluralRules object jpayne@69: */ jpayne@69: jpayne@69: #if !UCONFIG_NO_FORMATTING jpayne@69: jpayne@69: #include "unicode/format.h" jpayne@69: #include "unicode/upluralrules.h" jpayne@69: #ifndef U_HIDE_INTERNAL_API jpayne@69: #include "unicode/numfmt.h" jpayne@69: #endif /* U_HIDE_INTERNAL_API */ jpayne@69: jpayne@69: /** jpayne@69: * Value returned by PluralRules::getUniqueKeywordValue() when there is no jpayne@69: * unique value to return. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: #define UPLRULES_NO_UNIQUE_VALUE ((double)-0.00123456777) jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: class Hashtable; jpayne@69: class IFixedDecimal; jpayne@69: class RuleChain; jpayne@69: class PluralRuleParser; jpayne@69: class PluralKeywordEnumeration; jpayne@69: class AndConstraint; jpayne@69: class SharedPluralRules; jpayne@69: jpayne@69: namespace number { jpayne@69: class FormattedNumber; jpayne@69: } jpayne@69: jpayne@69: /** jpayne@69: * Defines rules for mapping non-negative numeric values onto a small set of jpayne@69: * keywords. Rules are constructed from a text description, consisting jpayne@69: * of a series of keywords and conditions. The {@link #select} method jpayne@69: * examines each condition in order and returns the keyword for the jpayne@69: * first condition that matches the number. If none match, jpayne@69: * default rule(other) is returned. jpayne@69: * jpayne@69: * For more information, details, and tips for writing rules, see the jpayne@69: * LDML spec, C.11 Language Plural Rules: jpayne@69: * http://www.unicode.org/draft/reports/tr35/tr35.html#Language_Plural_Rules jpayne@69: * jpayne@69: * Examples:
jpayne@69: * "one: n is 1; few: n in 2..4"jpayne@69: * This defines two rules, for 'one' and 'few'. The condition for jpayne@69: * 'one' is "n is 1" which means that the number must be equal to jpayne@69: * 1 for this condition to pass. The condition for 'few' is jpayne@69: * "n in 2..4" which means that the number must be between 2 and jpayne@69: * 4 inclusive for this condition to pass. All other numbers jpayne@69: * are assigned the keyword "other" by the default rule. jpayne@69: *
jpayne@69: * "zero: n is 0; one: n is 1; zero: n mod 100 in 1..19"jpayne@69: * This illustrates that the same keyword can be defined multiple times. jpayne@69: * Each rule is examined in order, and the first keyword whose condition jpayne@69: * passes is the one returned. Also notes that a modulus is applied jpayne@69: * to n in the last rule. Thus its condition holds for 119, 219, 319... jpayne@69: *
jpayne@69: * "one: n is 1; few: n mod 10 in 2..4 and n mod 100 not in 12..14"jpayne@69: * This illustrates conjunction and negation. The condition for 'few' jpayne@69: * has two parts, both of which must be met: "n mod 10 in 2..4" and jpayne@69: * "n mod 100 not in 12..14". The first part applies a modulus to n jpayne@69: * before the test as in the previous example. The second part applies jpayne@69: * a different modulus and also uses negation, thus it matches all jpayne@69: * numbers _not_ in 12, 13, 14, 112, 113, 114, 212, 213, 214... jpayne@69: * jpayne@69: *
jpayne@69: * Syntax:
jpayne@69: * \code jpayne@69: * rules = rule (';' rule)* jpayne@69: * rule = keyword ':' condition jpayne@69: * keyword =jpayne@69: *jpayne@69: * condition = and_condition ('or' and_condition)* jpayne@69: * and_condition = relation ('and' relation)* jpayne@69: * relation = is_relation | in_relation | within_relation | 'n' jpayne@69: * is_relation = expr 'is' ('not')? value jpayne@69: * in_relation = expr ('not')? 'in' range_list jpayne@69: * within_relation = expr ('not')? 'within' range jpayne@69: * expr = ('n' | 'i' | 'f' | 'v' | 'j') ('mod' value)? jpayne@69: * range_list = (range | value) (',' range_list)* jpayne@69: * value = digit+ ('.' digit+)? jpayne@69: * digit = 0|1|2|3|4|5|6|7|8|9 jpayne@69: * range = value'..'value jpayne@69: * \endcode jpayne@69: *
jpayne@69: *
jpayne@69: * The i, f, and v values are defined as follows: jpayne@69: *
jpayne@69: *jpayne@69: * Examples are in the following table: jpayne@69: *
jpayne@69: *n | jpayne@69: *i | jpayne@69: *f | jpayne@69: *v | jpayne@69: *
---|---|---|---|
1.0 | jpayne@69: *1 | jpayne@69: *0 | jpayne@69: *1 | jpayne@69: *
1.00 | jpayne@69: *1 | jpayne@69: *0 | jpayne@69: *2 | jpayne@69: *
1.3 | jpayne@69: *1 | jpayne@69: *3 | jpayne@69: *1 | jpayne@69: *
1.03 | jpayne@69: *1 | jpayne@69: *3 | jpayne@69: *2 | jpayne@69: *
1.23 | jpayne@69: *1 | jpayne@69: *23 | jpayne@69: *2 | jpayne@69: *
jpayne@69: * The difference between 'in' and 'within' is that 'in' only includes integers in the specified range, while 'within' jpayne@69: * includes all values. Using 'within' with a range_list consisting entirely of values is the same as using 'in' (it's jpayne@69: * not an error). jpayne@69: *
jpayne@69: jpayne@69: * An "identifier" is a sequence of characters that do not have the jpayne@69: * Unicode Pattern_Syntax or Pattern_White_Space properties. jpayne@69: *jpayne@69: * The difference between 'in' and 'within' is that 'in' only includes jpayne@69: * integers in the specified range, while 'within' includes all values. jpayne@69: * Using 'within' with a range_list consisting entirely of values is the jpayne@69: * same as using 'in' (it's not an error). jpayne@69: *
jpayne@69: *jpayne@69: * Keywords jpayne@69: * could be defined by users or from ICU locale data. There are 6 jpayne@69: * predefined values in ICU - 'zero', 'one', 'two', 'few', 'many' and jpayne@69: * 'other'. Callers need to check the value of keyword returned by jpayne@69: * {@link #select} method. jpayne@69: *
jpayne@69: * jpayne@69: * Examples:jpayne@69: * UnicodeString keyword = pl->select(number); jpayne@69: * if (keyword== UnicodeString("one") { jpayne@69: * ... jpayne@69: * } jpayne@69: * else if ( ... ) jpayne@69: *jpayne@69: * Note:
jpayne@69: * ICU defines plural rules for many locales based on CLDR Language Plural Rules. jpayne@69: * For these predefined rules, see CLDR page at jpayne@69: * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html jpayne@69: *
jpayne@69: */ jpayne@69: class U_I18N_API PluralRules : public UObject { jpayne@69: public: jpayne@69: jpayne@69: /** jpayne@69: * Constructor. jpayne@69: * @param status Output param set to success/failure code on exit, which jpayne@69: * must not indicate a failure before the function call. jpayne@69: * jpayne@69: * @stable ICU 4.0 jpayne@69: */ jpayne@69: PluralRules(UErrorCode& status); jpayne@69: jpayne@69: /** jpayne@69: * Copy constructor. jpayne@69: * @stable ICU 4.0 jpayne@69: */ jpayne@69: PluralRules(const PluralRules& other); jpayne@69: jpayne@69: /** jpayne@69: * Destructor. jpayne@69: * @stable ICU 4.0 jpayne@69: */ jpayne@69: virtual ~PluralRules(); jpayne@69: jpayne@69: /** jpayne@69: * Clone jpayne@69: * @stable ICU 4.0 jpayne@69: */ jpayne@69: PluralRules* clone() const; jpayne@69: jpayne@69: /** jpayne@69: * Assignment operator. jpayne@69: * @stable ICU 4.0 jpayne@69: */ jpayne@69: PluralRules& operator=(const PluralRules&); jpayne@69: jpayne@69: /** jpayne@69: * Creates a PluralRules from a description if it is parsable, otherwise jpayne@69: * returns NULL. jpayne@69: * jpayne@69: * @param description rule description jpayne@69: * @param status Output param set to success/failure code on exit, which jpayne@69: * must not indicate a failure before the function call. jpayne@69: * @return new PluralRules pointer. NULL if there is an error. jpayne@69: * @stable ICU 4.0 jpayne@69: */ jpayne@69: static PluralRules* U_EXPORT2 createRules(const UnicodeString& description, jpayne@69: UErrorCode& status); jpayne@69: jpayne@69: /** jpayne@69: * The default rules that accept any number. jpayne@69: * jpayne@69: * @param status Output param set to success/failure code on exit, which jpayne@69: * must not indicate a failure before the function call. jpayne@69: * @return new PluralRules pointer. NULL if there is an error. jpayne@69: * @stable ICU 4.0 jpayne@69: */ jpayne@69: static PluralRules* U_EXPORT2 createDefaultRules(UErrorCode& status); jpayne@69: jpayne@69: /** jpayne@69: * Provides access to the predefined cardinal-numberPluralRules
for a given
jpayne@69: * locale.
jpayne@69: * Same as forLocale(locale, UPLURAL_TYPE_CARDINAL, status).
jpayne@69: *
jpayne@69: * @param locale The locale for which a PluralRules
object is
jpayne@69: * returned.
jpayne@69: * @param status Output param set to success/failure code on exit, which
jpayne@69: * must not indicate a failure before the function call.
jpayne@69: * @return The predefined PluralRules
object pointer for
jpayne@69: * this locale. If there's no predefined rules for this locale,
jpayne@69: * the rules for the closest parent in the locale hierarchy
jpayne@69: * that has one will be returned. The final fallback always
jpayne@69: * returns the default 'other' rules.
jpayne@69: * @stable ICU 4.0
jpayne@69: */
jpayne@69: static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UErrorCode& status);
jpayne@69:
jpayne@69: /**
jpayne@69: * Provides access to the predefined PluralRules
for a given
jpayne@69: * locale and the plural type.
jpayne@69: *
jpayne@69: * @param locale The locale for which a PluralRules
object is
jpayne@69: * returned.
jpayne@69: * @param type The plural type (e.g., cardinal or ordinal).
jpayne@69: * @param status Output param set to success/failure code on exit, which
jpayne@69: * must not indicate a failure before the function call.
jpayne@69: * @return The predefined PluralRules
object pointer for
jpayne@69: * this locale. If there's no predefined rules for this locale,
jpayne@69: * the rules for the closest parent in the locale hierarchy
jpayne@69: * that has one will be returned. The final fallback always
jpayne@69: * returns the default 'other' rules.
jpayne@69: * @stable ICU 50
jpayne@69: */
jpayne@69: static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UPluralType type, UErrorCode& status);
jpayne@69:
jpayne@69: #ifndef U_HIDE_INTERNAL_API
jpayne@69: /**
jpayne@69: * Return a StringEnumeration over the locales for which there is plurals data.
jpayne@69: * @return a StringEnumeration over the locales available.
jpayne@69: * @internal
jpayne@69: */
jpayne@69: static StringEnumeration* U_EXPORT2 getAvailableLocales(UErrorCode &status);
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns whether or not there are overrides.
jpayne@69: * @param locale the locale to check.
jpayne@69: * @return
jpayne@69: * @internal
jpayne@69: */
jpayne@69: static UBool hasOverride(const Locale &locale);
jpayne@69:
jpayne@69: /**
jpayne@69: * For ICU use only.
jpayne@69: * creates a SharedPluralRules object
jpayne@69: * @internal
jpayne@69: */
jpayne@69: static PluralRules* U_EXPORT2 internalForLocale(const Locale& locale, UPluralType type, UErrorCode& status);
jpayne@69:
jpayne@69: /**
jpayne@69: * For ICU use only.
jpayne@69: * Returns handle to the shared, cached PluralRules instance.
jpayne@69: * Caller must call removeRef() on returned value once it is done with
jpayne@69: * the shared instance.
jpayne@69: * @internal
jpayne@69: */
jpayne@69: static const SharedPluralRules* U_EXPORT2 createSharedInstance(
jpayne@69: const Locale& locale, UPluralType type, UErrorCode& status);
jpayne@69:
jpayne@69:
jpayne@69: #endif /* U_HIDE_INTERNAL_API */
jpayne@69:
jpayne@69: /**
jpayne@69: * Given an integer, returns the keyword of the first rule
jpayne@69: * that applies to the number. This function can be used with
jpayne@69: * isKeyword* functions to determine the keyword for default plural rules.
jpayne@69: *
jpayne@69: * @param number The number for which the rule has to be determined.
jpayne@69: * @return The keyword of the selected rule.
jpayne@69: * @stable ICU 4.0
jpayne@69: */
jpayne@69: UnicodeString select(int32_t number) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Given a floating-point number, returns the keyword of the first rule
jpayne@69: * that applies to the number. This function can be used with
jpayne@69: * isKeyword* functions to determine the keyword for default plural rules.
jpayne@69: *
jpayne@69: * @param number The number for which the rule has to be determined.
jpayne@69: * @return The keyword of the selected rule.
jpayne@69: * @stable ICU 4.0
jpayne@69: */
jpayne@69: UnicodeString select(double number) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Given a formatted number, returns the keyword of the first rule
jpayne@69: * that applies to the number. This function can be used with
jpayne@69: * isKeyword* functions to determine the keyword for default plural rules.
jpayne@69: *
jpayne@69: * A FormattedNumber allows you to specify an exponent or trailing zeros,
jpayne@69: * which can affect the plural category. To get a FormattedNumber, see
jpayne@69: * NumberFormatter.
jpayne@69: *
jpayne@69: * @param number The number for which the rule has to be determined.
jpayne@69: * @param status Set if an error occurs while selecting plural keyword.
jpayne@69: * This could happen if the FormattedNumber is invalid.
jpayne@69: * @return The keyword of the selected rule.
jpayne@69: * @stable ICU 64
jpayne@69: */
jpayne@69: UnicodeString select(const number::FormattedNumber& number, UErrorCode& status) const;
jpayne@69:
jpayne@69: #ifndef U_HIDE_INTERNAL_API
jpayne@69: /**
jpayne@69: * @internal
jpayne@69: */
jpayne@69: UnicodeString select(const IFixedDecimal &number) const;
jpayne@69: #endif /* U_HIDE_INTERNAL_API */
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns a list of all rule keywords used in this PluralRules
jpayne@69: * object. The rule 'other' is always present by default.
jpayne@69: *
jpayne@69: * @param status Output param set to success/failure code on exit, which
jpayne@69: * must not indicate a failure before the function call.
jpayne@69: * @return StringEnumeration with the keywords.
jpayne@69: * The caller must delete the object.
jpayne@69: * @stable ICU 4.0
jpayne@69: */
jpayne@69: StringEnumeration* getKeywords(UErrorCode& status) const;
jpayne@69:
jpayne@69: #ifndef U_HIDE_DEPRECATED_API
jpayne@69: /**
jpayne@69: * Deprecated Function, does not return useful results.
jpayne@69: *
jpayne@69: * Originally intended to return a unique value for this keyword if it exists,
jpayne@69: * else the constant UPLRULES_NO_UNIQUE_VALUE.
jpayne@69: *
jpayne@69: * @param keyword The keyword.
jpayne@69: * @return Stub deprecated function returns UPLRULES_NO_UNIQUE_VALUE always.
jpayne@69: * @deprecated ICU 55
jpayne@69: */
jpayne@69: double getUniqueKeywordValue(const UnicodeString& keyword);
jpayne@69:
jpayne@69: /**
jpayne@69: * Deprecated Function, does not produce useful results.
jpayne@69: *
jpayne@69: * Originally intended to return all the values for which select() would return the keyword.
jpayne@69: * If the keyword is unknown, returns no values, but this is not an error. If
jpayne@69: * the number of values is unlimited, returns no values and -1 as the
jpayne@69: * count.
jpayne@69: *
jpayne@69: * The number of returned values is typically small.
jpayne@69: *
jpayne@69: * @param keyword The keyword.
jpayne@69: * @param dest Array into which to put the returned values. May
jpayne@69: * be NULL if destCapacity is 0.
jpayne@69: * @param destCapacity The capacity of the array, must be at least 0.
jpayne@69: * @param status The error code. Deprecated function, always sets U_UNSUPPORTED_ERROR.
jpayne@69: * @return The count of values available, or -1. This count
jpayne@69: * can be larger than destCapacity, but no more than
jpayne@69: * destCapacity values will be written.
jpayne@69: * @deprecated ICU 55
jpayne@69: */
jpayne@69: int32_t getAllKeywordValues(const UnicodeString &keyword,
jpayne@69: double *dest, int32_t destCapacity,
jpayne@69: UErrorCode& status);
jpayne@69: #endif /* U_HIDE_DEPRECATED_API */
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns sample values for which select() would return the keyword. If
jpayne@69: * the keyword is unknown, returns no values, but this is not an error.
jpayne@69: *
jpayne@69: * The number of returned values is typically small.
jpayne@69: *
jpayne@69: * @param keyword The keyword.
jpayne@69: * @param dest Array into which to put the returned values. May
jpayne@69: * be NULL if destCapacity is 0.
jpayne@69: * @param destCapacity The capacity of the array, must be at least 0.
jpayne@69: * @param status The error code.
jpayne@69: * @return The count of values written.
jpayne@69: * If more than destCapacity samples are available, then
jpayne@69: * only destCapacity are written, and destCapacity is returned as the count,
jpayne@69: * rather than setting a U_BUFFER_OVERFLOW_ERROR.
jpayne@69: * (The actual number of keyword values could be unlimited.)
jpayne@69: * @stable ICU 4.8
jpayne@69: */
jpayne@69: int32_t getSamples(const UnicodeString &keyword,
jpayne@69: double *dest, int32_t destCapacity,
jpayne@69: UErrorCode& status);
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns TRUE if the given keyword is defined in this
jpayne@69: * PluralRules
object.
jpayne@69: *
jpayne@69: * @param keyword the input keyword.
jpayne@69: * @return TRUE if the input keyword is defined.
jpayne@69: * Otherwise, return FALSE.
jpayne@69: * @stable ICU 4.0
jpayne@69: */
jpayne@69: UBool isKeyword(const UnicodeString& keyword) const;
jpayne@69:
jpayne@69:
jpayne@69: /**
jpayne@69: * Returns keyword for default plural form.
jpayne@69: *
jpayne@69: * @return keyword for default plural form.
jpayne@69: * @stable ICU 4.0
jpayne@69: */
jpayne@69: UnicodeString getKeywordOther() const;
jpayne@69:
jpayne@69: #ifndef U_HIDE_INTERNAL_API
jpayne@69: /**
jpayne@69: *
jpayne@69: * @internal
jpayne@69: */
jpayne@69: UnicodeString getRules() const;
jpayne@69: #endif /* U_HIDE_INTERNAL_API */
jpayne@69:
jpayne@69: /**
jpayne@69: * Compares the equality of two PluralRules objects.
jpayne@69: *
jpayne@69: * @param other The other PluralRules object to be compared with.
jpayne@69: * @return True if the given PluralRules is the same as this
jpayne@69: * PluralRules; false otherwise.
jpayne@69: * @stable ICU 4.0
jpayne@69: */
jpayne@69: virtual UBool operator==(const PluralRules& other) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * Compares the inequality of two PluralRules objects.
jpayne@69: *
jpayne@69: * @param other The PluralRules object to be compared with.
jpayne@69: * @return True if the given PluralRules is not the same as this
jpayne@69: * PluralRules; false otherwise.
jpayne@69: * @stable ICU 4.0
jpayne@69: */
jpayne@69: UBool operator!=(const PluralRules& other) const {return !operator==(other);}
jpayne@69:
jpayne@69:
jpayne@69: /**
jpayne@69: * ICU "poor man's RTTI", returns a UClassID for this class.
jpayne@69: *
jpayne@69: * @stable ICU 4.0
jpayne@69: *
jpayne@69: */
jpayne@69: static UClassID U_EXPORT2 getStaticClassID(void);
jpayne@69:
jpayne@69: /**
jpayne@69: * ICU "poor man's RTTI", returns a UClassID for the actual class.
jpayne@69: *
jpayne@69: * @stable ICU 4.0
jpayne@69: */
jpayne@69: virtual UClassID getDynamicClassID() const;
jpayne@69:
jpayne@69:
jpayne@69: private:
jpayne@69: RuleChain *mRules;
jpayne@69:
jpayne@69: PluralRules(); // default constructor not implemented
jpayne@69: void parseDescription(const UnicodeString& ruleData, UErrorCode &status);
jpayne@69: int32_t getNumberValue(const UnicodeString& token) const;
jpayne@69: UnicodeString getRuleFromResource(const Locale& locale, UPluralType type, UErrorCode& status);
jpayne@69: RuleChain *rulesForKeyword(const UnicodeString &keyword) const;
jpayne@69:
jpayne@69: /**
jpayne@69: * An internal status variable used to indicate that the object is in an 'invalid' state.
jpayne@69: * Used by copy constructor, the assignment operator and the clone method.
jpayne@69: */
jpayne@69: UErrorCode mInternalStatus;
jpayne@69:
jpayne@69: friend class PluralRuleParser;
jpayne@69: };
jpayne@69:
jpayne@69: U_NAMESPACE_END
jpayne@69:
jpayne@69: #endif /* #if !UCONFIG_NO_FORMATTING */
jpayne@69:
jpayne@69: #endif /* U_SHOW_CPLUSPLUS_API */
jpayne@69:
jpayne@69: #endif // _PLURRULE
jpayne@69: //eof