plurrule.h 17 KB

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