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