1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2008-2015, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
12 * Modification History:*
13 * Date Name Description
15 ********************************************************************************
21 #include "unicode/utypes.h"
25 * \brief C++ API: PluralRules object
28 #if !UCONFIG_NO_FORMATTING
30 #include "unicode/format.h"
31 #include "unicode/upluralrules.h"
32 #ifndef U_HIDE_INTERNAL_API
33 #include "unicode/numfmt.h"
34 #endif /* U_HIDE_INTERNAL_API */
37 * Value returned by PluralRules::getUniqueKeywordValue() when there is no
38 * unique value to return.
41 #define UPLRULES_NO_UNIQUE_VALUE ((double)-0.00123456777)
43 #if U_SHOW_CPLUSPLUS_API
49 class PluralRuleParser
;
50 class PluralKeywordEnumeration
;
52 class SharedPluralRules
;
55 class FormattedNumber
;
59 * Defines rules for mapping non-negative numeric values onto a small set of
60 * keywords. Rules are constructed from a text description, consisting
61 * of a series of keywords and conditions. The {@link #select} method
62 * examines each condition in order and returns the keyword for the
63 * first condition that matches the number. If none match,
64 * default rule(other) is returned.
66 * For more information, details, and tips for writing rules, see the
67 * LDML spec, C.11 Language Plural Rules:
68 * http://www.unicode.org/draft/reports/tr35/tr35.html#Language_Plural_Rules
71 * "one: n is 1; few: n in 2..4"</pre>
72 * This defines two rules, for 'one' and 'few'. The condition for
73 * 'one' is "n is 1" which means that the number must be equal to
74 * 1 for this condition to pass. The condition for 'few' is
75 * "n in 2..4" which means that the number must be between 2 and
76 * 4 inclusive for this condition to pass. All other numbers
77 * are assigned the keyword "other" by the default rule.
79 * "zero: n is 0; one: n is 1; zero: n mod 100 in 1..19"</pre>
80 * This illustrates that the same keyword can be defined multiple times.
81 * Each rule is examined in order, and the first keyword whose condition
82 * passes is the one returned. Also notes that a modulus is applied
83 * to n in the last rule. Thus its condition holds for 119, 219, 319...
85 * "one: n is 1; few: n mod 10 in 2..4 and n mod 100 not in 12..14"</pre>
86 * This illustrates conjunction and negation. The condition for 'few'
87 * has two parts, both of which must be met: "n mod 10 in 2..4" and
88 * "n mod 100 not in 12..14". The first part applies a modulus to n
89 * before the test as in the previous example. The second part applies
90 * a different modulus and also uses negation, thus it matches all
91 * numbers _not_ in 12, 13, 14, 112, 113, 114, 212, 213, 214...
96 * rules = rule (';' rule)*
97 * rule = keyword ':' condition
98 * keyword = <identifier>
99 * condition = and_condition ('or' and_condition)*
100 * and_condition = relation ('and' relation)*
101 * relation = is_relation | in_relation | within_relation | 'n' <EOL>
102 * is_relation = expr 'is' ('not')? value
103 * in_relation = expr ('not')? 'in' range_list
104 * within_relation = expr ('not')? 'within' range
105 * expr = ('n' | 'i' | 'f' | 'v' | 'j') ('mod' value)?
106 * range_list = (range | value) (',' range_list)*
107 * value = digit+ ('.' digit+)?
108 * digit = 0|1|2|3|4|5|6|7|8|9
109 * range = value'..'value
114 * The i, f, and v values are defined as follows:
117 * <li>i to be the integer digits.</li>
118 * <li>f to be the visible fractional digits, as an integer.</li>
119 * <li>v to be the number of visible fraction digits.</li>
120 * <li>j is defined to only match integers. That is j is 3 fails if v != 0 (eg for 3.1 or 3.0).</li>
123 * Examples are in the following table:
125 * <table border='1' style="border-collapse:collapse">
135 * <td align="right">0</td>
141 * <td align="right">0</td>
147 * <td align="right">3</td>
153 * <td align="right">3</td>
159 * <td align="right">23</td>
164 * The difference between 'in' and 'within' is that 'in' only includes integers in the specified range, while 'within'
165 * includes all values. Using 'within' with a range_list consisting entirely of values is the same as using 'in' (it's
169 * An "identifier" is a sequence of characters that do not have the
170 * Unicode Pattern_Syntax or Pattern_White_Space properties.
172 * The difference between 'in' and 'within' is that 'in' only includes
173 * integers in the specified range, while 'within' includes all values.
174 * Using 'within' with a range_list consisting entirely of values is the
175 * same as using 'in' (it's not an error).
179 * could be defined by users or from ICU locale data. There are 6
180 * predefined values in ICU - 'zero', 'one', 'two', 'few', 'many' and
181 * 'other'. Callers need to check the value of keyword returned by
182 * {@link #select} method.
186 * UnicodeString keyword = pl->select(number);
187 * if (keyword== UnicodeString("one") {
192 * <strong>Note:</strong><br>
194 * ICU defines plural rules for many locales based on CLDR <i>Language Plural Rules</i>.
195 * For these predefined rules, see CLDR page at
196 * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
199 class U_I18N_API PluralRules
: public UObject
{
204 * @param status Output param set to success/failure code on exit, which
205 * must not indicate a failure before the function call.
209 PluralRules(UErrorCode
& status
);
215 PluralRules(const PluralRules
& other
);
221 virtual ~PluralRules();
227 PluralRules
* clone() const;
230 * Assignment operator.
233 PluralRules
& operator=(const PluralRules
&);
236 * Creates a PluralRules from a description if it is parsable, otherwise
239 * @param description rule description
240 * @param status Output param set to success/failure code on exit, which
241 * must not indicate a failure before the function call.
242 * @return new PluralRules pointer. NULL if there is an error.
245 static PluralRules
* U_EXPORT2
createRules(const UnicodeString
& description
,
249 * The default rules that accept any number.
251 * @param status Output param set to success/failure code on exit, which
252 * must not indicate a failure before the function call.
253 * @return new PluralRules pointer. NULL if there is an error.
256 static PluralRules
* U_EXPORT2
createDefaultRules(UErrorCode
& status
);
259 * Provides access to the predefined cardinal-number <code>PluralRules</code> for a given
261 * Same as forLocale(locale, UPLURAL_TYPE_CARDINAL, status).
263 * @param locale The locale for which a <code>PluralRules</code> object is
265 * @param status Output param set to success/failure code on exit, which
266 * must not indicate a failure before the function call.
267 * @return The predefined <code>PluralRules</code> object pointer for
268 * this locale. If there's no predefined rules for this locale,
269 * the rules for the closest parent in the locale hierarchy
270 * that has one will be returned. The final fallback always
271 * returns the default 'other' rules.
274 static PluralRules
* U_EXPORT2
forLocale(const Locale
& locale
, UErrorCode
& status
);
277 * Provides access to the predefined <code>PluralRules</code> for a given
278 * locale and the plural type.
280 * @param locale The locale for which a <code>PluralRules</code> object is
282 * @param type The plural type (e.g., cardinal or ordinal).
283 * @param status Output param set to success/failure code on exit, which
284 * must not indicate a failure before the function call.
285 * @return The predefined <code>PluralRules</code> object pointer for
286 * this locale. If there's no predefined rules for this locale,
287 * the rules for the closest parent in the locale hierarchy
288 * that has one will be returned. The final fallback always
289 * returns the default 'other' rules.
292 static PluralRules
* U_EXPORT2
forLocale(const Locale
& locale
, UPluralType type
, UErrorCode
& status
);
294 #ifndef U_HIDE_INTERNAL_API
296 * Return a StringEnumeration over the locales for which there is plurals data.
297 * @return a StringEnumeration over the locales available.
300 static StringEnumeration
* U_EXPORT2
getAvailableLocales(UErrorCode
&status
);
303 * Returns whether or not there are overrides.
304 * @param locale the locale to check.
308 static UBool
hasOverride(const Locale
&locale
);
312 * creates a SharedPluralRules object
315 static PluralRules
* U_EXPORT2
internalForLocale(const Locale
& locale
, UPluralType type
, UErrorCode
& status
);
319 * Returns handle to the shared, cached PluralRules instance.
320 * Caller must call removeRef() on returned value once it is done with
321 * the shared instance.
324 static const SharedPluralRules
* U_EXPORT2
createSharedInstance(
325 const Locale
& locale
, UPluralType type
, UErrorCode
& status
);
328 #endif /* U_HIDE_INTERNAL_API */
331 * Given an integer, returns the keyword of the first rule
332 * that applies to the number. This function can be used with
333 * isKeyword* functions to determine the keyword for default plural rules.
335 * @param number The number for which the rule has to be determined.
336 * @return The keyword of the selected rule.
339 UnicodeString
select(int32_t number
) const;
342 * Given a floating-point number, returns the keyword of the first rule
343 * that applies to the number. This function can be used with
344 * isKeyword* functions to determine the keyword for default plural rules.
346 * @param number The number for which the rule has to be determined.
347 * @return The keyword of the selected rule.
350 UnicodeString
select(double number
) const;
352 #ifndef U_HIDE_DRAFT_API
354 * Given a formatted number, returns the keyword of the first rule
355 * that applies to the number. This function can be used with
356 * isKeyword* functions to determine the keyword for default plural rules.
358 * A FormattedNumber allows you to specify an exponent or trailing zeros,
359 * which can affect the plural category. To get a FormattedNumber, see
362 * @param number The number for which the rule has to be determined.
363 * @param status Set if an error occurs while selecting plural keyword.
364 * This could happen if the FormattedNumber is invalid.
365 * @return The keyword of the selected rule.
368 UnicodeString
select(const number::FormattedNumber
& number
, UErrorCode
& status
) const;
369 #endif /* U_HIDE_DRAFT_API */
371 #ifndef U_HIDE_INTERNAL_API
375 UnicodeString
select(const IFixedDecimal
&number
) const;
376 #endif /* U_HIDE_INTERNAL_API */
379 * Returns a list of all rule keywords used in this <code>PluralRules</code>
380 * object. The rule 'other' is always present by default.
382 * @param status Output param set to success/failure code on exit, which
383 * must not indicate a failure before the function call.
384 * @return StringEnumeration with the keywords.
385 * The caller must delete the object.
388 StringEnumeration
* getKeywords(UErrorCode
& status
) const;
390 #ifndef U_HIDE_DEPRECATED_API
392 * Deprecated Function, does not return useful results.
394 * Originally intended to return a unique value for this keyword if it exists,
395 * else the constant UPLRULES_NO_UNIQUE_VALUE.
397 * @param keyword The keyword.
398 * @return Stub deprecated function returns UPLRULES_NO_UNIQUE_VALUE always.
401 double getUniqueKeywordValue(const UnicodeString
& keyword
);
404 * Deprecated Function, does not produce useful results.
406 * Originally intended to return all the values for which select() would return the keyword.
407 * If the keyword is unknown, returns no values, but this is not an error. If
408 * the number of values is unlimited, returns no values and -1 as the
411 * The number of returned values is typically small.
413 * @param keyword The keyword.
414 * @param dest Array into which to put the returned values. May
415 * be NULL if destCapacity is 0.
416 * @param destCapacity The capacity of the array, must be at least 0.
417 * @param status The error code. Deprecated function, always sets U_UNSUPPORTED_ERROR.
418 * @return The count of values available, or -1. This count
419 * can be larger than destCapacity, but no more than
420 * destCapacity values will be written.
423 int32_t getAllKeywordValues(const UnicodeString
&keyword
,
424 double *dest
, int32_t destCapacity
,
426 #endif /* U_HIDE_DEPRECATED_API */
429 * Returns sample values for which select() would return the keyword. If
430 * the keyword is unknown, returns no values, but this is not an error.
432 * The number of returned values is typically small.
434 * @param keyword The keyword.
435 * @param dest Array into which to put the returned values. May
436 * be NULL if destCapacity is 0.
437 * @param destCapacity The capacity of the array, must be at least 0.
438 * @param status The error code.
439 * @return The count of values written.
440 * If more than destCapacity samples are available, then
441 * only destCapacity are written, and destCapacity is returned as the count,
442 * rather than setting a U_BUFFER_OVERFLOW_ERROR.
443 * (The actual number of keyword values could be unlimited.)
446 int32_t getSamples(const UnicodeString
&keyword
,
447 double *dest
, int32_t destCapacity
,
451 * Returns TRUE if the given keyword is defined in this
452 * <code>PluralRules</code> object.
454 * @param keyword the input keyword.
455 * @return TRUE if the input keyword is defined.
456 * Otherwise, return FALSE.
459 UBool
isKeyword(const UnicodeString
& keyword
) const;
463 * Returns keyword for default plural form.
465 * @return keyword for default plural form.
468 UnicodeString
getKeywordOther() const;
470 #ifndef U_HIDE_INTERNAL_API
475 UnicodeString
getRules() const;
476 #endif /* U_HIDE_INTERNAL_API */
479 * Compares the equality of two PluralRules objects.
481 * @param other The other PluralRules object to be compared with.
482 * @return True if the given PluralRules is the same as this
483 * PluralRules; false otherwise.
486 virtual UBool
operator==(const PluralRules
& other
) const;
489 * Compares the inequality of two PluralRules objects.
491 * @param other The PluralRules object to be compared with.
492 * @return True if the given PluralRules is not the same as this
493 * PluralRules; false otherwise.
496 UBool
operator!=(const PluralRules
& other
) const {return !operator==(other
);}
500 * ICU "poor man's RTTI", returns a UClassID for this class.
505 static UClassID U_EXPORT2
getStaticClassID(void);
508 * ICU "poor man's RTTI", returns a UClassID for the actual class.
512 virtual UClassID
getDynamicClassID() const;
518 PluralRules(); // default constructor not implemented
519 void parseDescription(const UnicodeString
& ruleData
, UErrorCode
&status
);
520 int32_t getNumberValue(const UnicodeString
& token
) const;
521 UnicodeString
getRuleFromResource(const Locale
& locale
, UPluralType type
, UErrorCode
& status
);
522 RuleChain
*rulesForKeyword(const UnicodeString
&keyword
) const;
525 * An internal status variable used to indicate that the object is in an 'invalid' state.
526 * Used by copy constructor, the assignment operator and the clone method.
528 UErrorCode mInternalStatus
;
530 friend class PluralRuleParser
;
534 #endif // U_SHOW_CPLUSPLUS_API
536 #endif /* #if !UCONFIG_NO_FORMATTING */