]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/plurrule.h
ICU-400.37.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / plurrule.h
CommitLineData
46f4442e
A
1/*
2*******************************************************************************
3* Copyright (C) 2008, 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
16#ifndef PLURRULE
17#define PLURRULE
18
19#include "unicode/utypes.h"
20
21/**
22 * \file
23 * \brief C++ API: PluralRules object
24 */
25
26#if !UCONFIG_NO_FORMATTING
27
28#include "unicode/format.h"
29
30U_NAMESPACE_BEGIN
31
32class Hashtable;
33class RuleChain;
34class RuleParser;
35
36/**
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.
43 *
44 * Examples:<pre>
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.
52 * </p><pre>
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...
58 * </p><pre>
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...
66 * </p>
67 * <p>
68 * Syntax:<pre>
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)?
79 * value = digit+
80 * digit = 0|1|2|3|4|5|6|7|8|9
81 * range = value'..'value
82 * </pre></p>
83 * <p>
84 * The difference between 'in' and 'within' is that 'in' only includes
85 * integers in the specified range, while 'within' includes all values.</p>
86 * <p>
87 * Keywords
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.
92 * </p>
93 *
94 * Examples:<pre>
95 * UnicodeString keyword = pl->select(number);
96 * if (keyword== UnicodeString("one") {
97 * ...
98 * }
99 * else if ( ... )
100 * </pre>
101 */
102class U_I18N_API PluralRules : public UObject {
103public:
104
105 /**
106 * Constructor.
107 * @param status Output param set to success/failure code on exit, which
108 * must not indicate a failure before the function call.
109 *
110 * @draft ICU 4.0
111 */
112 PluralRules(UErrorCode& status);
113
114 /**
115 * Copy constructor.
116 * @draft ICU 4.0
117 */
118 PluralRules(const PluralRules& other);
119
120 /**
121 * Destructor.
122 * @draft ICU 4.0
123 */
124 virtual ~PluralRules();
125
126 /**
127 * Clone
128 * @draft ICU 4.0
129 */
130 PluralRules* clone() const;
131
132 /**
133 * Assignment operator.
134 * @draft ICU 4.0
135 */
136 PluralRules& operator=(const PluralRules&);
137
138 /**
139 * Creates a PluralRules from a description if it is parsable, otherwise
140 * returns null.
141 *
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.
146 * @draft ICU 4.0
147 */
148 static PluralRules* U_EXPORT2 createRules(const UnicodeString& description,
149 UErrorCode& status);
150
151 /**
152 * The default rules that accept any number.
153 *
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.
157 * @draft ICU 4.0
158 */
159 static PluralRules* U_EXPORT2 createDefaultRules(UErrorCode& status);
160
161 /**
162 * Provides access to the predefined <code>PluralRules</code> for a given
163 * locale.
164 *
165 * @param locale The locale for which a <code>PluralRules</code> object is
166 * returned.
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.
174 * @draft ICU 4.0
175 */
176 static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UErrorCode& status);
177
178 /**
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.
182 *
183 * @param number The number for which the rule has to be determined.
184 * @return The keyword of the selected rule.
185 * @draft ICU 4.0
186 */
187 UnicodeString select(int32_t number) const;
188
189 /**
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.
193 *
194 * @param number The number for which the rule has to be determined.
195 * @return The keyword of the selected rule.
196 * @draft ICU 4.0
197 */
198 UnicodeString select(double number) const;
199
200 /**
201 * Returns a list of all rule keywords used in this <code>PluralRules</code>
202 * object. The rule 'other' is always present by default.
203 *
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.
208 * @draft ICU 4.0
209 */
210 StringEnumeration* getKeywords(UErrorCode& status) const;
211
212 /**
213 * Returns TRUE if the given keyword is defined in this
214 * <code>PluralRules</code> object.
215 *
216 * @param keyword the input keyword.
217 * @return TRUE if the input keyword is defined.
218 * Otherwise, return FALSE.
219 * @draft ICU 4.0
220 */
221 UBool isKeyword(const UnicodeString& keyword) const;
222
223
224 /**
225 * Returns keyword for default plural form.
226 *
227 * @return keyword for default plural form.
228 * @internal 4.0
229 * @draft ICU 4.0
230 */
231 UnicodeString getKeywordOther() const;
232
233 /**
234 * Compares the equality of two PluralRules objects.
235 *
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.
239 * @draft ICU 4.0
240 */
241 virtual UBool operator==(const PluralRules& other) const;
242
243 /**
244 * Compares the inequality of two PluralRules objects.
245 *
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.
249 * @draft ICU 4.0
250 */
251 UBool operator!=(const PluralRules& other) const {return !operator==(other);}
252
253
254 /**
255 * ICU "poor man's RTTI", returns a UClassID for this class.
256 *
257 * @draft ICU 4.0
258 *
259 */
260 static UClassID U_EXPORT2 getStaticClassID(void);
261
262 /**
263 * ICU "poor man's RTTI", returns a UClassID for the actual class.
264 *
265 * @draft ICU 4.0
266 */
267 virtual UClassID getDynamicClassID() const;
268
269
270private:
271 Hashtable *fLocaleStringsHash;
272 UnicodeString mLocaleName;
273 RuleChain *mRules;
274 RuleParser *mParser;
275
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);
283
284};
285
286U_NAMESPACE_END
287
288#endif /* #if !UCONFIG_NO_FORMATTING */
289
290#endif // _PLURRULE
291//eof