plurrule.h 18 KB

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