1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html |
3 | /* |
4 | ******************************************************************************* |
5 | * Copyright (C) 2008-2015, International Business Machines Corporation and |
6 | * others. All Rights Reserved. |
7 | ******************************************************************************* |
8 | * |
9 | * |
10 | * File PLURRULE.H |
11 | * |
12 | * Modification History:* |
13 | * Date Name Description |
14 | * |
15 | ******************************************************************************** |
16 | */ |
17 | |
18 | #ifndef PLURRULE |
19 | #define PLURRULE |
20 | |
21 | #include "unicode/utypes.h" |
22 | |
23 | #if U_SHOW_CPLUSPLUS_API |
24 | |
25 | /** |
26 | * \file |
27 | * \brief C++ API: PluralRules object |
28 | */ |
29 | |
30 | #if !UCONFIG_NO_FORMATTING |
31 | |
32 | #include "unicode/format.h" |
33 | #include "unicode/upluralrules.h" |
34 | #ifndef U_HIDE_INTERNAL_API |
35 | #include "unicode/numfmt.h" |
36 | #endif /* U_HIDE_INTERNAL_API */ |
37 | |
38 | /** |
39 | * Value returned by PluralRules::getUniqueKeywordValue() when there is no |
40 | * unique value to return. |
41 | * @stable ICU 4.8 |
42 | */ |
43 | #define UPLRULES_NO_UNIQUE_VALUE ((double)-0.00123456777) |
44 | |
45 | U_NAMESPACE_BEGIN |
46 | |
47 | class Hashtable; |
48 | class IFixedDecimal; |
49 | class RuleChain; |
50 | class PluralRuleParser; |
51 | class PluralKeywordEnumeration; |
52 | class AndConstraint; |
53 | class SharedPluralRules; |
54 | |
55 | namespace number { |
56 | class FormattedNumber; |
57 | } |
58 | |
59 | /** |
60 | * Defines rules for mapping non-negative numeric values onto a small set of |
61 | * keywords. Rules are constructed from a text description, consisting |
62 | * of a series of keywords and conditions. The {@link #select} method |
63 | * examines each condition in order and returns the keyword for the |
64 | * first condition that matches the number. If none match, |
65 | * default rule(other) is returned. |
66 | * |
67 | * For more information, details, and tips for writing rules, see the |
68 | * LDML spec, C.11 Language Plural Rules: |
69 | * http://www.unicode.org/draft/reports/tr35/tr35.html#Language_Plural_Rules |
70 | * |
71 | * Examples:<pre> |
72 | * "one: n is 1; few: n in 2..4"</pre> |
73 | * This defines two rules, for 'one' and 'few'. The condition for |
74 | * 'one' is "n is 1" which means that the number must be equal to |
75 | * 1 for this condition to pass. The condition for 'few' is |
76 | * "n in 2..4" which means that the number must be between 2 and |
77 | * 4 inclusive for this condition to pass. All other numbers |
78 | * are assigned the keyword "other" by the default rule. |
79 | * </p><pre> |
80 | * "zero: n is 0; one: n is 1; zero: n mod 100 in 1..19"</pre> |
81 | * This illustrates that the same keyword can be defined multiple times. |
82 | * Each rule is examined in order, and the first keyword whose condition |
83 | * passes is the one returned. Also notes that a modulus is applied |
84 | * to n in the last rule. Thus its condition holds for 119, 219, 319... |
85 | * </p><pre> |
86 | * "one: n is 1; few: n mod 10 in 2..4 and n mod 100 not in 12..14"</pre> |
87 | * This illustrates conjunction and negation. The condition for 'few' |
88 | * has two parts, both of which must be met: "n mod 10 in 2..4" and |
89 | * "n mod 100 not in 12..14". The first part applies a modulus to n |
90 | * before the test as in the previous example. The second part applies |
91 | * a different modulus and also uses negation, thus it matches all |
92 | * numbers _not_ in 12, 13, 14, 112, 113, 114, 212, 213, 214... |
93 | * </p> |
94 | * <p> |
95 | * Syntax:<pre> |
96 | * \code |
97 | * rules = rule (';' rule)* |
98 | * rule = keyword ':' condition |
99 | * keyword = <identifier> |
100 | * condition = and_condition ('or' and_condition)* |
101 | * and_condition = relation ('and' relation)* |
102 | * relation = is_relation | in_relation | within_relation | 'n' <EOL> |
103 | * is_relation = expr 'is' ('not')? value |
104 | * in_relation = expr ('not')? 'in' range_list |
105 | * within_relation = expr ('not')? 'within' range |
106 | * expr = ('n' | 'i' | 'f' | 'v' | 'j') ('mod' value)? |
107 | * range_list = (range | value) (',' range_list)* |
108 | * value = digit+ ('.' digit+)? |
109 | * digit = 0|1|2|3|4|5|6|7|8|9 |
110 | * range = value'..'value |
111 | * \endcode |
112 | * </pre></p> |
113 | * <p> |
114 | * <p> |
115 | * The i, f, and v values are defined as follows: |
116 | * </p> |
117 | * <ul> |
118 | * <li>i to be the integer digits.</li> |
119 | * <li>f to be the visible fractional digits, as an integer.</li> |
120 | * <li>v to be the number of visible fraction digits.</li> |
121 | * <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> |
122 | * </ul> |
123 | * <p> |
124 | * Examples are in the following table: |
125 | * </p> |
126 | * <table border='1' style="border-collapse:collapse"> |
127 | * <tr> |
128 | * <th>n</th> |
129 | * <th>i</th> |
130 | * <th>f</th> |
131 | * <th>v</th> |
132 | * </tr> |
133 | * <tr> |
134 | * <td>1.0</td> |
135 | * <td>1</td> |
136 | * <td align="right">0</td> |
137 | * <td>1</td> |
138 | * </tr> |
139 | * <tr> |
140 | * <td>1.00</td> |
141 | * <td>1</td> |
142 | * <td align="right">0</td> |
143 | * <td>2</td> |
144 | * </tr> |
145 | * <tr> |
146 | * <td>1.3</td> |
147 | * <td>1</td> |
148 | * <td align="right">3</td> |
149 | * <td>1</td> |
150 | * </tr> |
151 | * <tr> |
152 | * <td>1.03</td> |
153 | * <td>1</td> |
154 | * <td align="right">3</td> |
155 | * <td>2</td> |
156 | * </tr> |
157 | * <tr> |
158 | * <td>1.23</td> |
159 | * <td>1</td> |
160 | * <td align="right">23</td> |
161 | * <td>2</td> |
162 | * </tr> |
163 | * </table> |
164 | * <p> |
165 | * The difference between 'in' and 'within' is that 'in' only includes integers in the specified range, while 'within' |
166 | * includes all values. Using 'within' with a range_list consisting entirely of values is the same as using 'in' (it's |
167 | * not an error). |
168 | * </p> |
169 | |
170 | * An "identifier" is a sequence of characters that do not have the |
171 | * Unicode Pattern_Syntax or Pattern_White_Space properties. |
172 | * <p> |
173 | * The difference between 'in' and 'within' is that 'in' only includes |
174 | * integers in the specified range, while 'within' includes all values. |
175 | * Using 'within' with a range_list consisting entirely of values is the |
176 | * same as using 'in' (it's not an error). |
177 | *</p> |
178 | * <p> |
179 | * Keywords |
180 | * could be defined by users or from ICU locale data. There are 6 |
181 | * predefined values in ICU - 'zero', 'one', 'two', 'few', 'many' and |
182 | * 'other'. Callers need to check the value of keyword returned by |
183 | * {@link #select} method. |
184 | * </p> |
185 | * |
186 | * Examples:<pre> |
187 | * UnicodeString keyword = pl->select(number); |
188 | * if (keyword== UnicodeString("one") { |
189 | * ... |
190 | * } |
191 | * else if ( ... ) |
192 | * </pre> |
193 | * <strong>Note:</strong><br> |
194 | * <p> |
195 | * ICU defines plural rules for many locales based on CLDR <i>Language Plural Rules</i>. |
196 | * For these predefined rules, see CLDR page at |
197 | * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html |
198 | * </p> |
199 | */ |
200 | class U_I18N_API PluralRules : public UObject { |
201 | public: |
202 | |
203 | /** |
204 | * Constructor. |
205 | * @param status Output param set to success/failure code on exit, which |
206 | * must not indicate a failure before the function call. |
207 | * |
208 | * @stable ICU 4.0 |
209 | */ |
210 | PluralRules(UErrorCode& status); |
211 | |
212 | /** |
213 | * Copy constructor. |
214 | * @stable ICU 4.0 |
215 | */ |
216 | PluralRules(const PluralRules& other); |
217 | |
218 | /** |
219 | * Destructor. |
220 | * @stable ICU 4.0 |
221 | */ |
222 | virtual ~PluralRules(); |
223 | |
224 | /** |
225 | * Clone |
226 | * @stable ICU 4.0 |
227 | */ |
228 | PluralRules* clone() const; |
229 | |
230 | /** |
231 | * Assignment operator. |
232 | * @stable ICU 4.0 |
233 | */ |
234 | PluralRules& operator=(const PluralRules&); |
235 | |
236 | /** |
237 | * Creates a PluralRules from a description if it is parsable, otherwise |
238 | * returns NULL. |
239 | * |
240 | * @param description rule description |
241 | * @param status Output param set to success/failure code on exit, which |
242 | * must not indicate a failure before the function call. |
243 | * @return new PluralRules pointer. NULL if there is an error. |
244 | * @stable ICU 4.0 |
245 | */ |
246 | static PluralRules* U_EXPORT2 createRules(const UnicodeString& description, |
247 | UErrorCode& status); |
248 | |
249 | /** |
250 | * The default rules that accept any number. |
251 | * |
252 | * @param status Output param set to success/failure code on exit, which |
253 | * must not indicate a failure before the function call. |
254 | * @return new PluralRules pointer. NULL if there is an error. |
255 | * @stable ICU 4.0 |
256 | */ |
257 | static PluralRules* U_EXPORT2 createDefaultRules(UErrorCode& status); |
258 | |
259 | /** |
260 | * Provides access to the predefined cardinal-number <code>PluralRules</code> for a given |
261 | * locale. |
262 | * Same as forLocale(locale, UPLURAL_TYPE_CARDINAL, status). |
263 | * |
264 | * @param locale The locale for which a <code>PluralRules</code> object is |
265 | * returned. |
266 | * @param status Output param set to success/failure code on exit, which |
267 | * must not indicate a failure before the function call. |
268 | * @return The predefined <code>PluralRules</code> object pointer for |
269 | * this locale. If there's no predefined rules for this locale, |
270 | * the rules for the closest parent in the locale hierarchy |
271 | * that has one will be returned. The final fallback always |
272 | * returns the default 'other' rules. |
273 | * @stable ICU 4.0 |
274 | */ |
275 | static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UErrorCode& status); |
276 | |
277 | /** |
278 | * Provides access to the predefined <code>PluralRules</code> for a given |
279 | * locale and the plural type. |
280 | * |
281 | * @param locale The locale for which a <code>PluralRules</code> object is |
282 | * returned. |
283 | * @param type The plural type (e.g., cardinal or ordinal). |
284 | * @param status Output param set to success/failure code on exit, which |
285 | * must not indicate a failure before the function call. |
286 | * @return The predefined <code>PluralRules</code> object pointer for |
287 | * this locale. If there's no predefined rules for this locale, |
288 | * the rules for the closest parent in the locale hierarchy |
289 | * that has one will be returned. The final fallback always |
290 | * returns the default 'other' rules. |
291 | * @stable ICU 50 |
292 | */ |
293 | static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UPluralType type, UErrorCode& status); |
294 | |
295 | #ifndef U_HIDE_INTERNAL_API |
296 | /** |
297 | * Return a StringEnumeration over the locales for which there is plurals data. |
298 | * @return a StringEnumeration over the locales available. |
299 | * @internal |
300 | */ |
301 | static StringEnumeration* U_EXPORT2 getAvailableLocales(UErrorCode &status); |
302 | |
303 | /** |
304 | * Returns whether or not there are overrides. |
305 | * @param locale the locale to check. |
306 | * @return |
307 | * @internal |
308 | */ |
309 | static UBool hasOverride(const Locale &locale); |
310 | |
311 | /** |
312 | * For ICU use only. |
313 | * creates a SharedPluralRules object |
314 | * @internal |
315 | */ |
316 | static PluralRules* U_EXPORT2 internalForLocale(const Locale& locale, UPluralType type, UErrorCode& status); |
317 | |
318 | /** |
319 | * For ICU use only. |
320 | * Returns handle to the shared, cached PluralRules instance. |
321 | * Caller must call removeRef() on returned value once it is done with |
322 | * the shared instance. |
323 | * @internal |
324 | */ |
325 | static const SharedPluralRules* U_EXPORT2 createSharedInstance( |
326 | const Locale& locale, UPluralType type, UErrorCode& status); |
327 | |
328 | |
329 | #endif /* U_HIDE_INTERNAL_API */ |
330 | |
331 | /** |
332 | * Given an integer, returns the keyword of the first rule |
333 | * that applies to the number. This function can be used with |
334 | * isKeyword* functions to determine the keyword for default plural rules. |
335 | * |
336 | * @param number The number for which the rule has to be determined. |
337 | * @return The keyword of the selected rule. |
338 | * @stable ICU 4.0 |
339 | */ |
340 | UnicodeString select(int32_t number) const; |
341 | |
342 | /** |
343 | * Given a floating-point number, returns the keyword of the first rule |
344 | * that applies to the number. This function can be used with |
345 | * isKeyword* functions to determine the keyword for default plural rules. |
346 | * |
347 | * @param number The number for which the rule has to be determined. |
348 | * @return The keyword of the selected rule. |
349 | * @stable ICU 4.0 |
350 | */ |
351 | UnicodeString select(double number) const; |
352 | |
353 | /** |
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. |
357 | * |
358 | * A FormattedNumber allows you to specify an exponent or trailing zeros, |
359 | * which can affect the plural category. To get a FormattedNumber, see |
360 | * NumberFormatter. |
361 | * |
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. |
366 | * @stable ICU 64 |
367 | */ |
368 | UnicodeString select(const number::FormattedNumber& number, UErrorCode& status) const; |
369 | |
370 | #ifndef U_HIDE_INTERNAL_API |
371 | /** |
372 | * @internal |
373 | */ |
374 | UnicodeString select(const IFixedDecimal &number) const; |
375 | #endif /* U_HIDE_INTERNAL_API */ |
376 | |
377 | /** |
378 | * Returns a list of all rule keywords used in this <code>PluralRules</code> |
379 | * object. The rule 'other' is always present by default. |
380 | * |
381 | * @param status Output param set to success/failure code on exit, which |
382 | * must not indicate a failure before the function call. |
383 | * @return StringEnumeration with the keywords. |
384 | * The caller must delete the object. |
385 | * @stable ICU 4.0 |
386 | */ |
387 | StringEnumeration* getKeywords(UErrorCode& status) const; |
388 | |
389 | #ifndef U_HIDE_DEPRECATED_API |
390 | /** |
391 | * Deprecated Function, does not return useful results. |
392 | * |
393 | * Originally intended to return a unique value for this keyword if it exists, |
394 | * else the constant UPLRULES_NO_UNIQUE_VALUE. |
395 | * |
396 | * @param keyword The keyword. |
397 | * @return Stub deprecated function returns UPLRULES_NO_UNIQUE_VALUE always. |
398 | * @deprecated ICU 55 |
399 | */ |
400 | double getUniqueKeywordValue(const UnicodeString& keyword); |
401 | |
402 | /** |
403 | * Deprecated Function, does not produce useful results. |
404 | * |
405 | * Originally intended to return all the values for which select() would return the keyword. |
406 | * If the keyword is unknown, returns no values, but this is not an error. If |
407 | * the number of values is unlimited, returns no values and -1 as the |
408 | * count. |
409 | * |
410 | * The number of returned values is typically small. |
411 | * |
412 | * @param keyword The keyword. |
413 | * @param dest Array into which to put the returned values. May |
414 | * be NULL if destCapacity is 0. |
415 | * @param destCapacity The capacity of the array, must be at least 0. |
416 | * @param status The error code. Deprecated function, always sets U_UNSUPPORTED_ERROR. |
417 | * @return The count of values available, or -1. This count |
418 | * can be larger than destCapacity, but no more than |
419 | * destCapacity values will be written. |
420 | * @deprecated ICU 55 |
421 | */ |
422 | int32_t getAllKeywordValues(const UnicodeString &keyword, |
423 | double *dest, int32_t destCapacity, |
424 | UErrorCode& status); |
425 | #endif /* U_HIDE_DEPRECATED_API */ |
426 | |
427 | /** |
428 | * Returns sample values for which select() would return the keyword. If |
429 | * the keyword is unknown, returns no values, but this is not an error. |
430 | * |
431 | * The number of returned values is typically small. |
432 | * |
433 | * @param keyword The keyword. |
434 | * @param dest Array into which to put the returned values. May |
435 | * be NULL if destCapacity is 0. |
436 | * @param destCapacity The capacity of the array, must be at least 0. |
437 | * @param status The error code. |
438 | * @return The count of values written. |
439 | * If more than destCapacity samples are available, then |
440 | * only destCapacity are written, and destCapacity is returned as the count, |
441 | * rather than setting a U_BUFFER_OVERFLOW_ERROR. |
442 | * (The actual number of keyword values could be unlimited.) |
443 | * @stable ICU 4.8 |
444 | */ |
445 | int32_t getSamples(const UnicodeString &keyword, |
446 | double *dest, int32_t destCapacity, |
447 | UErrorCode& status); |
448 | |
449 | /** |
450 | * Returns TRUE if the given keyword is defined in this |
451 | * <code>PluralRules</code> object. |
452 | * |
453 | * @param keyword the input keyword. |
454 | * @return TRUE if the input keyword is defined. |
455 | * Otherwise, return FALSE. |
456 | * @stable ICU 4.0 |
457 | */ |
458 | UBool isKeyword(const UnicodeString& keyword) const; |
459 | |
460 | |
461 | /** |
462 | * Returns keyword for default plural form. |
463 | * |
464 | * @return keyword for default plural form. |
465 | * @stable ICU 4.0 |
466 | */ |
467 | UnicodeString getKeywordOther() const; |
468 | |
469 | #ifndef U_HIDE_INTERNAL_API |
470 | /** |
471 | * |
472 | * @internal |
473 | */ |
474 | UnicodeString getRules() const; |
475 | #endif /* U_HIDE_INTERNAL_API */ |
476 | |
477 | /** |
478 | * Compares the equality of two PluralRules objects. |
479 | * |
480 | * @param other The other PluralRules object to be compared with. |
481 | * @return True if the given PluralRules is the same as this |
482 | * PluralRules; false otherwise. |
483 | * @stable ICU 4.0 |
484 | */ |
485 | virtual UBool operator==(const PluralRules& other) const; |
486 | |
487 | /** |
488 | * Compares the inequality of two PluralRules objects. |
489 | * |
490 | * @param other The PluralRules object to be compared with. |
491 | * @return True if the given PluralRules is not the same as this |
492 | * PluralRules; false otherwise. |
493 | * @stable ICU 4.0 |
494 | */ |
495 | UBool operator!=(const PluralRules& other) const {return !operator==(other);} |
496 | |
497 | |
498 | /** |
499 | * ICU "poor man's RTTI", returns a UClassID for this class. |
500 | * |
501 | * @stable ICU 4.0 |
502 | * |
503 | */ |
504 | static UClassID U_EXPORT2 getStaticClassID(void); |
505 | |
506 | /** |
507 | * ICU "poor man's RTTI", returns a UClassID for the actual class. |
508 | * |
509 | * @stable ICU 4.0 |
510 | */ |
511 | virtual UClassID getDynamicClassID() const; |
512 | |
513 | |
514 | private: |
515 | RuleChain *mRules; |
516 | |
517 | PluralRules(); // default constructor not implemented |
518 | void parseDescription(const UnicodeString& ruleData, UErrorCode &status); |
519 | int32_t getNumberValue(const UnicodeString& token) const; |
520 | UnicodeString getRuleFromResource(const Locale& locale, UPluralType type, UErrorCode& status); |
521 | RuleChain *rulesForKeyword(const UnicodeString &keyword) const; |
522 | |
523 | /** |
524 | * An internal status variable used to indicate that the object is in an 'invalid' state. |
525 | * Used by copy constructor, the assignment operator and the clone method. |
526 | */ |
527 | UErrorCode mInternalStatus; |
528 | |
529 | friend class PluralRuleParser; |
530 | }; |
531 | |
532 | U_NAMESPACE_END |
533 | |
534 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
535 | |
536 | #endif /* U_SHOW_CPLUSPLUS_API */ |
537 | |
538 | #endif // _PLURRULE |
539 | //eof |
540 | |