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