plurrule.h 17 KB

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