plurrule.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. #ifndef PLURRULE
  18. #define PLURRULE
  19. #include "unicode/utypes.h"
  20. #if U_SHOW_CPLUSPLUS_API
  21. /**
  22. * \file
  23. * \brief C++ API: PluralRules object
  24. */
  25. #if !UCONFIG_NO_FORMATTING
  26. #include "unicode/format.h"
  27. #include "unicode/upluralrules.h"
  28. #ifndef U_HIDE_INTERNAL_API
  29. #include "unicode/numfmt.h"
  30. #endif /* U_HIDE_INTERNAL_API */
  31. /**
  32. * Value returned by PluralRules::getUniqueKeywordValue() when there is no
  33. * unique value to return.
  34. * @stable ICU 4.8
  35. */
  36. #define UPLRULES_NO_UNIQUE_VALUE ((double)-0.00123456777)
  37. U_NAMESPACE_BEGIN
  38. class Hashtable;
  39. class IFixedDecimal;
  40. class FixedDecimal;
  41. class RuleChain;
  42. class PluralRuleParser;
  43. class PluralKeywordEnumeration;
  44. class AndConstraint;
  45. class SharedPluralRules;
  46. class StandardPluralRanges;
  47. namespace number {
  48. class FormattedNumber;
  49. class FormattedNumberRange;
  50. namespace impl {
  51. class UFormattedNumberRangeData;
  52. class DecimalQuantity;
  53. class DecNum;
  54. }
  55. }
  56. #ifndef U_HIDE_INTERNAL_API
  57. using icu::number::impl::DecimalQuantity;
  58. #endif /* U_HIDE_INTERNAL_API */
  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, Part 3.5 Language Plural Rules:
  69. * https://www.unicode.org/reports/tr35/tr35-numbers.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. * An "identifier" is a sequence of characters that do not have the
  170. * Unicode Pattern_Syntax or Pattern_White_Space properties.
  171. * <p>
  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).
  176. *</p>
  177. * <p>
  178. * Keywords
  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.
  183. * </p>
  184. *
  185. * Examples:<pre>
  186. * UnicodeString keyword = pl->select(number);
  187. * if (keyword== UnicodeString("one") {
  188. * ...
  189. * }
  190. * else if ( ... )
  191. * </pre>
  192. * <strong>Note:</strong><br>
  193. * <p>
  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. * https://unicode-org.github.io/cldr-staging/charts/latest/supplemental/language_plural_rules.html
  197. * </p>
  198. */
  199. class U_I18N_API PluralRules : public UObject {
  200. public:
  201. /**
  202. * Constructor.
  203. * @param status Output param set to success/failure code on exit, which
  204. * must not indicate a failure before the function call.
  205. *
  206. * @stable ICU 4.0
  207. */
  208. PluralRules(UErrorCode& status);
  209. /**
  210. * Copy constructor.
  211. * @stable ICU 4.0
  212. */
  213. PluralRules(const PluralRules& other);
  214. /**
  215. * Destructor.
  216. * @stable ICU 4.0
  217. */
  218. virtual ~PluralRules();
  219. /**
  220. * Clone
  221. * @stable ICU 4.0
  222. */
  223. PluralRules* clone() const;
  224. /**
  225. * Assignment operator.
  226. * @stable ICU 4.0
  227. */
  228. PluralRules& operator=(const PluralRules&);
  229. /**
  230. * Creates a PluralRules from a description if it is parsable, otherwise
  231. * returns nullptr.
  232. *
  233. * @param description rule description
  234. * @param status Output param set to success/failure code on exit, which
  235. * must not indicate a failure before the function call.
  236. * @return new PluralRules pointer. nullptr if there is an error.
  237. * @stable ICU 4.0
  238. */
  239. static PluralRules* U_EXPORT2 createRules(const UnicodeString& description,
  240. UErrorCode& status);
  241. /**
  242. * The default rules that accept any number.
  243. *
  244. * @param status Output param set to success/failure code on exit, which
  245. * must not indicate a failure before the function call.
  246. * @return new PluralRules pointer. nullptr if there is an error.
  247. * @stable ICU 4.0
  248. */
  249. static PluralRules* U_EXPORT2 createDefaultRules(UErrorCode& status);
  250. /**
  251. * Provides access to the predefined cardinal-number <code>PluralRules</code> for a given
  252. * locale.
  253. * Same as forLocale(locale, UPLURAL_TYPE_CARDINAL, status).
  254. *
  255. * @param locale The locale for which a <code>PluralRules</code> object is
  256. * returned.
  257. * @param status Output param set to success/failure code on exit, which
  258. * must not indicate a failure before the function call.
  259. * @return The predefined <code>PluralRules</code> object pointer for
  260. * this locale. If there's no predefined rules for this locale,
  261. * the rules for the closest parent in the locale hierarchy
  262. * that has one will be returned. The final fallback always
  263. * returns the default 'other' rules.
  264. * @stable ICU 4.0
  265. */
  266. static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UErrorCode& status);
  267. /**
  268. * Provides access to the predefined <code>PluralRules</code> for a given
  269. * locale and the plural type.
  270. *
  271. * @param locale The locale for which a <code>PluralRules</code> object is
  272. * returned.
  273. * @param type The plural type (e.g., cardinal or ordinal).
  274. * @param status Output param set to success/failure code on exit, which
  275. * must not indicate a failure before the function call.
  276. * @return The predefined <code>PluralRules</code> object pointer for
  277. * this locale. If there's no predefined rules for this locale,
  278. * the rules for the closest parent in the locale hierarchy
  279. * that has one will be returned. The final fallback always
  280. * returns the default 'other' rules.
  281. * @stable ICU 50
  282. */
  283. static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UPluralType type, UErrorCode& status);
  284. #ifndef U_HIDE_INTERNAL_API
  285. /**
  286. * Return a StringEnumeration over the locales for which there is plurals data.
  287. * @return a StringEnumeration over the locales available.
  288. * @internal
  289. */
  290. static StringEnumeration* U_EXPORT2 getAvailableLocales(UErrorCode &status);
  291. /**
  292. * For ICU use only.
  293. * creates a SharedPluralRules object
  294. * @internal
  295. */
  296. static PluralRules* U_EXPORT2 internalForLocale(const Locale& locale, UPluralType type, UErrorCode& status);
  297. /**
  298. * For ICU use only.
  299. * Returns handle to the shared, cached PluralRules instance.
  300. * Caller must call removeRef() on returned value once it is done with
  301. * the shared instance.
  302. * @internal
  303. */
  304. static const SharedPluralRules* U_EXPORT2 createSharedInstance(
  305. const Locale& locale, UPluralType type, UErrorCode& status);
  306. #endif /* U_HIDE_INTERNAL_API */
  307. /**
  308. * Given an integer, returns the keyword of the first rule
  309. * that applies to the number. This function can be used with
  310. * isKeyword* functions to determine the keyword for default plural rules.
  311. *
  312. * @param number The number for which the rule has to be determined.
  313. * @return The keyword of the selected rule.
  314. * @stable ICU 4.0
  315. */
  316. UnicodeString select(int32_t number) const;
  317. /**
  318. * Given a floating-point number, returns the keyword of the first rule
  319. * that applies to the number. This function can be used with
  320. * isKeyword* functions to determine the keyword for default plural rules.
  321. *
  322. * @param number The number for which the rule has to be determined.
  323. * @return The keyword of the selected rule.
  324. * @stable ICU 4.0
  325. */
  326. UnicodeString select(double number) const;
  327. /**
  328. * Given a formatted number, returns the keyword of the first rule
  329. * that applies to the number. This function can be used with
  330. * isKeyword* functions to determine the keyword for default plural rules.
  331. *
  332. * A FormattedNumber allows you to specify an exponent or trailing zeros,
  333. * which can affect the plural category. To get a FormattedNumber, see
  334. * NumberFormatter.
  335. *
  336. * @param number The number for which the rule has to be determined.
  337. * @param status Set if an error occurs while selecting plural keyword.
  338. * This could happen if the FormattedNumber is invalid.
  339. * @return The keyword of the selected rule.
  340. * @stable ICU 64
  341. */
  342. UnicodeString select(const number::FormattedNumber& number, UErrorCode& status) const;
  343. /**
  344. * Given a formatted number range, returns the overall plural form of the
  345. * range. For example, "3-5" returns "other" in English.
  346. *
  347. * To get a FormattedNumberRange, see NumberRangeFormatter.
  348. *
  349. * This method only works if PluralRules was created with a locale. If it was created
  350. * from PluralRules::createRules(), this method sets status code U_UNSUPPORTED_ERROR.
  351. *
  352. * @param range The number range onto which the rules will be applied.
  353. * @param status Set if an error occurs while selecting plural keyword.
  354. * This could happen if the FormattedNumberRange is invalid,
  355. * or if plural ranges data is unavailable.
  356. * @return The keyword of the selected rule.
  357. * @stable ICU 68
  358. */
  359. UnicodeString select(const number::FormattedNumberRange& range, UErrorCode& status) const;
  360. #ifndef U_HIDE_INTERNAL_API
  361. /**
  362. * @internal
  363. */
  364. UnicodeString select(const IFixedDecimal &number) const;
  365. /**
  366. * @internal
  367. */
  368. UnicodeString select(const number::impl::UFormattedNumberRangeData* urange, UErrorCode& status) const;
  369. #endif /* U_HIDE_INTERNAL_API */
  370. /**
  371. * Returns a list of all rule keywords used in this <code>PluralRules</code>
  372. * object. The rule 'other' is always present by default.
  373. *
  374. * @param status Output param set to success/failure code on exit, which
  375. * must not indicate a failure before the function call.
  376. * @return StringEnumeration with the keywords.
  377. * The caller must delete the object.
  378. * @stable ICU 4.0
  379. */
  380. StringEnumeration* getKeywords(UErrorCode& status) const;
  381. #ifndef U_HIDE_DEPRECATED_API
  382. /**
  383. * Deprecated Function, does not return useful results.
  384. *
  385. * Originally intended to return a unique value for this keyword if it exists,
  386. * else the constant UPLRULES_NO_UNIQUE_VALUE.
  387. *
  388. * @param keyword The keyword.
  389. * @return Stub deprecated function returns UPLRULES_NO_UNIQUE_VALUE always.
  390. * @deprecated ICU 55
  391. */
  392. double getUniqueKeywordValue(const UnicodeString& keyword);
  393. /**
  394. * Deprecated Function, does not produce useful results.
  395. *
  396. * Originally intended to return all the values for which select() would return the keyword.
  397. * If the keyword is unknown, returns no values, but this is not an error. If
  398. * the number of values is unlimited, returns no values and -1 as the
  399. * count.
  400. *
  401. * The number of returned values is typically small.
  402. *
  403. * @param keyword The keyword.
  404. * @param dest Array into which to put the returned values. May
  405. * be nullptr if destCapacity is 0.
  406. * @param destCapacity The capacity of the array, must be at least 0.
  407. * @param status The error code. Deprecated function, always sets U_UNSUPPORTED_ERROR.
  408. * @return The count of values available, or -1. This count
  409. * can be larger than destCapacity, but no more than
  410. * destCapacity values will be written.
  411. * @deprecated ICU 55
  412. */
  413. int32_t getAllKeywordValues(const UnicodeString &keyword,
  414. double *dest, int32_t destCapacity,
  415. UErrorCode& status);
  416. #endif /* U_HIDE_DEPRECATED_API */
  417. /**
  418. * Returns sample values for which select() would return the keyword. If
  419. * the keyword is unknown, returns no values, but this is not an error.
  420. *
  421. * The number of returned values is typically small.
  422. *
  423. * @param keyword The keyword.
  424. * @param dest Array into which to put the returned values. May
  425. * be nullptr if destCapacity is 0.
  426. * @param destCapacity The capacity of the array, must be at least 0.
  427. * @param status The error code.
  428. * @return The count of values written.
  429. * If more than destCapacity samples are available, then
  430. * only destCapacity are written, and destCapacity is returned as the count,
  431. * rather than setting a U_BUFFER_OVERFLOW_ERROR.
  432. * (The actual number of keyword values could be unlimited.)
  433. * @stable ICU 4.8
  434. */
  435. int32_t getSamples(const UnicodeString &keyword,
  436. double *dest, int32_t destCapacity,
  437. UErrorCode& status);
  438. #ifndef U_HIDE_INTERNAL_API
  439. /**
  440. * Internal-only function that returns DecimalQuantitys instead of doubles.
  441. *
  442. * Returns sample values for which select() would return the keyword. If
  443. * the keyword is unknown, returns no values, but this is not an error.
  444. *
  445. * The number of returned values is typically small.
  446. *
  447. * @param keyword The keyword.
  448. * @param dest Array into which to put the returned values. May
  449. * be nullptr if destCapacity is 0.
  450. * @param destCapacity The capacity of the array, must be at least 0.
  451. * @param status The error code.
  452. * @return The count of values written.
  453. * If more than destCapacity samples are available, then
  454. * only destCapacity are written, and destCapacity is returned as the count,
  455. * rather than setting a U_BUFFER_OVERFLOW_ERROR.
  456. * (The actual number of keyword values could be unlimited.)
  457. * @internal
  458. */
  459. int32_t getSamples(const UnicodeString &keyword,
  460. DecimalQuantity *dest, int32_t destCapacity,
  461. UErrorCode& status);
  462. #endif /* U_HIDE_INTERNAL_API */
  463. /**
  464. * Returns true if the given keyword is defined in this
  465. * <code>PluralRules</code> object.
  466. *
  467. * @param keyword the input keyword.
  468. * @return true if the input keyword is defined.
  469. * Otherwise, return false.
  470. * @stable ICU 4.0
  471. */
  472. UBool isKeyword(const UnicodeString& keyword) const;
  473. /**
  474. * Returns keyword for default plural form.
  475. *
  476. * @return keyword for default plural form.
  477. * @stable ICU 4.0
  478. */
  479. UnicodeString getKeywordOther() const;
  480. #ifndef U_HIDE_INTERNAL_API
  481. /**
  482. *
  483. * @internal
  484. */
  485. UnicodeString getRules() const;
  486. #endif /* U_HIDE_INTERNAL_API */
  487. /**
  488. * Compares the equality of two PluralRules objects.
  489. *
  490. * @param other The other PluralRules object to be compared with.
  491. * @return true if the given PluralRules is the same as this
  492. * PluralRules; false otherwise.
  493. * @stable ICU 4.0
  494. */
  495. virtual bool operator==(const PluralRules& other) const;
  496. /**
  497. * Compares the inequality of two PluralRules objects.
  498. *
  499. * @param other The PluralRules object to be compared with.
  500. * @return true if the given PluralRules is not the same as this
  501. * PluralRules; false otherwise.
  502. * @stable ICU 4.0
  503. */
  504. bool operator!=(const PluralRules& other) const {return !operator==(other);}
  505. /**
  506. * ICU "poor man's RTTI", returns a UClassID for this class.
  507. *
  508. * @stable ICU 4.0
  509. *
  510. */
  511. static UClassID U_EXPORT2 getStaticClassID();
  512. /**
  513. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  514. *
  515. * @stable ICU 4.0
  516. */
  517. virtual UClassID getDynamicClassID() const override;
  518. private:
  519. RuleChain *mRules;
  520. StandardPluralRanges *mStandardPluralRanges;
  521. PluralRules() = delete; // default constructor not implemented
  522. UnicodeString getRuleFromResource(const Locale& locale, UPluralType type, UErrorCode& status);
  523. RuleChain *rulesForKeyword(const UnicodeString &keyword) const;
  524. PluralRules *clone(UErrorCode& status) const;
  525. /**
  526. * An internal status variable used to indicate that the object is in an 'invalid' state.
  527. * Used by copy constructor, the assignment operator and the clone method.
  528. */
  529. UErrorCode mInternalStatus;
  530. friend class PluralRuleParser;
  531. };
  532. U_NAMESPACE_END
  533. #endif /* #if !UCONFIG_NO_FORMATTING */
  534. #endif /* U_SHOW_CPLUSPLUS_API */
  535. #endif // _PLURRULE
  536. //eof