plurrule.h 17 KB

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