2 *******************************************************************************
3 * Copyright (C) 2008, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
10 * Modification History:*
11 * Date Name Description
13 ********************************************************************************
19 #include "unicode/utypes.h"
23 * \brief C++ API: PluralRules object
26 #if !UCONFIG_NO_FORMATTING
28 #include "unicode/format.h"
37 * Defines rules for mapping positive long values onto a small set of
38 * keywords. Rules are constructed from a text description, consisting
39 * of a series of keywords and conditions. The {@link #select} method
40 * examines each condition in order and returns the keyword for the
41 * first condition that matches the number. If none match,
42 * default rule(other) is returned.
45 * "one: n is 1; few: n in 2..4"</pre>
46 * This defines two rules, for 'one' and 'few'. The condition for
47 * 'one' is "n is 1" which means that the number must be equal to
48 * 1 for this condition to pass. The condition for 'few' is
49 * "n in 2..4" which means that the number must be between 2 and
50 * 4 inclusive for this condition to pass. All other numbers
51 * are assigned the keyword "other" by the default rule.
53 * "zero: n is 0; one: n is 1; zero: n mod 100 in 1..19"</pre>
54 * This illustrates that the same keyword can be defined multiple times.
55 * Each rule is examined in order, and the first keyword whose condition
56 * passes is the one returned. Also notes that a modulus is applied
57 * to n in the last rule. Thus its condition holds for 119, 219, 319...
59 * "one: n is 1; few: n mod 10 in 2..4 and n mod 100 not in 12..14"</pre>
60 * This illustrates conjunction and negation. The condition for 'few'
61 * has two parts, both of which must be met: "n mod 10 in 2..4" and
62 * "n mod 100 not in 12..14". The first part applies a modulus to n
63 * before the test as in the previous example. The second part applies
64 * a different modulus and also uses negation, thus it matches all
65 * numbers _not_ in 12, 13, 14, 112, 113, 114, 212, 213, 214...
69 * rules = rule (';' rule)*
70 * rule = keyword ':' condition
71 * keyword = <identifier>
72 * condition = and_condition ('or' and_condition)*
73 * and_condition = relation ('and' relation)*
74 * relation = is_relation | in_relation | within_relation | 'n' <EOL>
75 * is_relation = expr 'is' ('not')? value
76 * in_relation = expr ('not')? 'in' range
77 * within_relation = expr ('not')? 'within' range
78 * expr = 'n' ('mod' value)?
80 * digit = 0|1|2|3|4|5|6|7|8|9
81 * range = value'..'value
84 * The difference between 'in' and 'within' is that 'in' only includes
85 * integers in the specified range, while 'within' includes all values.</p>
88 * could be defined by users or from ICU locale data. There are 6
89 * predefined values in ICU - 'zero', 'one', 'two', 'few', 'many' and
90 * 'other'. Callers need to check the value of keyword returned by
91 * {@link #select} method.
95 * UnicodeString keyword = pl->select(number);
96 * if (keyword== UnicodeString("one") {
102 class U_I18N_API PluralRules
: public UObject
{
107 * @param status Output param set to success/failure code on exit, which
108 * must not indicate a failure before the function call.
112 PluralRules(UErrorCode
& status
);
118 PluralRules(const PluralRules
& other
);
124 virtual ~PluralRules();
130 PluralRules
* clone() const;
133 * Assignment operator.
136 PluralRules
& operator=(const PluralRules
&);
139 * Creates a PluralRules from a description if it is parsable, otherwise
142 * @param description rule description
143 * @param status Output param set to success/failure code on exit, which
144 * must not indicate a failure before the function call.
145 * @return new PluralRules pointer. NULL if there is an error.
148 static PluralRules
* U_EXPORT2
createRules(const UnicodeString
& description
,
152 * The default rules that accept any number.
154 * @param status Output param set to success/failure code on exit, which
155 * must not indicate a failure before the function call.
156 * @return new PluralRules pointer. NULL if there is an error.
159 static PluralRules
* U_EXPORT2
createDefaultRules(UErrorCode
& status
);
162 * Provides access to the predefined <code>PluralRules</code> for a given
165 * @param locale The locale for which a <code>PluralRules</code> object is
167 * @param status Output param set to success/failure code on exit, which
168 * must not indicate a failure before the function call.
169 * @return The predefined <code>PluralRules</code> object pointer for
170 * this locale. If there's no predefined rules for this locale,
171 * the rules for the closest parent in the locale hierarchy
172 * that has one will be returned. The final fallback always
173 * returns the default 'other' rules.
176 static PluralRules
* U_EXPORT2
forLocale(const Locale
& locale
, UErrorCode
& status
);
179 * Given a number, returns the keyword of the first rule that applies to
180 * the number. This function can be used with isKeyword* functions to
181 * determine the keyword for default plural rules.
183 * @param number The number for which the rule has to be determined.
184 * @return The keyword of the selected rule.
187 UnicodeString
select(int32_t number
) const;
190 * Given a number, returns the keyword of the first rule that applies to
191 * the number. This function can be used with isKeyword* functions to
192 * determine the keyword for default plural rules.
194 * @param number The number for which the rule has to be determined.
195 * @return The keyword of the selected rule.
198 UnicodeString
select(double number
) const;
201 * Returns a list of all rule keywords used in this <code>PluralRules</code>
202 * object. The rule 'other' is always present by default.
204 * @param status Output param set to success/failure code on exit, which
205 * must not indicate a failure before the function call.
206 * @return StringEnumeration with the keywords.
207 * The caller must delete the object.
210 StringEnumeration
* getKeywords(UErrorCode
& status
) const;
213 * Returns TRUE if the given keyword is defined in this
214 * <code>PluralRules</code> object.
216 * @param keyword the input keyword.
217 * @return TRUE if the input keyword is defined.
218 * Otherwise, return FALSE.
221 UBool
isKeyword(const UnicodeString
& keyword
) const;
225 * Returns keyword for default plural form.
227 * @return keyword for default plural form.
231 UnicodeString
getKeywordOther() const;
234 * Compares the equality of two PluralRules objects.
236 * @param other The other PluralRules object to be compared with.
237 * @return True if the given PluralRules is the same as this
238 * PluralRules; false otherwise.
241 virtual UBool
operator==(const PluralRules
& other
) const;
244 * Compares the inequality of two PluralRules objects.
246 * @param other The PluralRules object to be compared with.
247 * @return True if the given PluralRules is not the same as this
248 * PluralRules; false otherwise.
251 UBool
operator!=(const PluralRules
& other
) const {return !operator==(other
);}
255 * ICU "poor man's RTTI", returns a UClassID for this class.
260 static UClassID U_EXPORT2
getStaticClassID(void);
263 * ICU "poor man's RTTI", returns a UClassID for the actual class.
267 virtual UClassID
getDynamicClassID() const;
271 Hashtable
*fLocaleStringsHash
;
272 UnicodeString mLocaleName
;
276 PluralRules(); // default constructor not implemented
277 int32_t getRepeatLimit() const;
278 void parseDescription(UnicodeString
& ruleData
, RuleChain
& rules
, UErrorCode
&status
);
279 void getNextLocale(const UnicodeString
& localeData
, int32_t* curIndex
, UnicodeString
& localeName
);
280 void addRules(RuleChain
& rules
);
281 int32_t getNumberValue(const UnicodeString
& token
) const;
282 UnicodeString
getRuleFromResource(const Locale
& locale
, UErrorCode
& status
);
288 #endif /* #if !UCONFIG_NO_FORMATTING */