]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/plurrule_impl.h
ICU-62107.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / plurrule_impl.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2007-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 *
9 * File PLURRULE_IMPL.H
10 *
11 *******************************************************************************
12 */
13
14
15 #ifndef PLURRULE_IMPL
16 #define PLURRULE_IMPL
17
18 // Internal definitions for the PluralRules implementation.
19
20 #include "unicode/utypes.h"
21
22 #if !UCONFIG_NO_FORMATTING
23
24 #include "unicode/format.h"
25 #include "unicode/locid.h"
26 #include "unicode/parseerr.h"
27 #include "unicode/strenum.h"
28 #include "unicode/ures.h"
29 #include "uvector.h"
30 #include "hash.h"
31 #include "uassert.h"
32
33 class PluralRulesTest;
34
35 U_NAMESPACE_BEGIN
36
37 class AndConstraint;
38 class RuleChain;
39 class DigitInterval;
40 class PluralRules;
41 class VisibleDigits;
42
43 static const UChar DOT = ((UChar) 0x002E);
44 static const UChar SINGLE_QUOTE = ((UChar) 0x0027);
45 static const UChar SLASH = ((UChar) 0x002F);
46 static const UChar BACKSLASH = ((UChar) 0x005C);
47 static const UChar SPACE = ((UChar) 0x0020);
48 static const UChar EXCLAMATION = ((UChar) 0x0021);
49 static const UChar QUOTATION_MARK = ((UChar) 0x0022);
50 static const UChar NUMBER_SIGN = ((UChar) 0x0023);
51 static const UChar PERCENT_SIGN = ((UChar) 0x0025);
52 static const UChar ASTERISK = ((UChar) 0x002A);
53 static const UChar COMMA = ((UChar) 0x002C);
54 static const UChar HYPHEN = ((UChar) 0x002D);
55 static const UChar U_ZERO = ((UChar) 0x0030);
56 static const UChar U_ONE = ((UChar) 0x0031);
57 static const UChar U_TWO = ((UChar) 0x0032);
58 static const UChar U_THREE = ((UChar) 0x0033);
59 static const UChar U_FOUR = ((UChar) 0x0034);
60 static const UChar U_FIVE = ((UChar) 0x0035);
61 static const UChar U_SIX = ((UChar) 0x0036);
62 static const UChar U_SEVEN = ((UChar) 0x0037);
63 static const UChar U_EIGHT = ((UChar) 0x0038);
64 static const UChar U_NINE = ((UChar) 0x0039);
65 static const UChar COLON = ((UChar) 0x003A);
66 static const UChar SEMI_COLON = ((UChar) 0x003B);
67 static const UChar EQUALS = ((UChar) 0x003D);
68 static const UChar AT = ((UChar) 0x0040);
69 static const UChar CAP_A = ((UChar) 0x0041);
70 static const UChar CAP_B = ((UChar) 0x0042);
71 static const UChar CAP_R = ((UChar) 0x0052);
72 static const UChar CAP_Z = ((UChar) 0x005A);
73 static const UChar LOWLINE = ((UChar) 0x005F);
74 static const UChar LEFTBRACE = ((UChar) 0x007B);
75 static const UChar RIGHTBRACE = ((UChar) 0x007D);
76 static const UChar TILDE = ((UChar) 0x007E);
77 static const UChar ELLIPSIS = ((UChar) 0x2026);
78
79 static const UChar LOW_A = ((UChar) 0x0061);
80 static const UChar LOW_B = ((UChar) 0x0062);
81 static const UChar LOW_C = ((UChar) 0x0063);
82 static const UChar LOW_D = ((UChar) 0x0064);
83 static const UChar LOW_E = ((UChar) 0x0065);
84 static const UChar LOW_F = ((UChar) 0x0066);
85 static const UChar LOW_G = ((UChar) 0x0067);
86 static const UChar LOW_H = ((UChar) 0x0068);
87 static const UChar LOW_I = ((UChar) 0x0069);
88 static const UChar LOW_J = ((UChar) 0x006a);
89 static const UChar LOW_K = ((UChar) 0x006B);
90 static const UChar LOW_L = ((UChar) 0x006C);
91 static const UChar LOW_M = ((UChar) 0x006D);
92 static const UChar LOW_N = ((UChar) 0x006E);
93 static const UChar LOW_O = ((UChar) 0x006F);
94 static const UChar LOW_P = ((UChar) 0x0070);
95 static const UChar LOW_Q = ((UChar) 0x0071);
96 static const UChar LOW_R = ((UChar) 0x0072);
97 static const UChar LOW_S = ((UChar) 0x0073);
98 static const UChar LOW_T = ((UChar) 0x0074);
99 static const UChar LOW_U = ((UChar) 0x0075);
100 static const UChar LOW_V = ((UChar) 0x0076);
101 static const UChar LOW_W = ((UChar) 0x0077);
102 static const UChar LOW_Y = ((UChar) 0x0079);
103 static const UChar LOW_Z = ((UChar) 0x007A);
104
105
106
107 static const int32_t PLURAL_RANGE_HIGH = 0x7fffffff;
108
109 enum tokenType {
110 none,
111 tNumber,
112 tComma,
113 tSemiColon,
114 tSpace,
115 tColon,
116 tAt, // '@'
117 tDot,
118 tDot2,
119 tEllipsis,
120 tKeyword,
121 tAnd,
122 tOr,
123 tMod, // 'mod' or '%'
124 tNot, // 'not' only.
125 tIn, // 'in' only.
126 tEqual, // '=' only.
127 tNotEqual, // '!='
128 tTilde,
129 tWithin,
130 tIs,
131 tVariableN,
132 tVariableI,
133 tVariableF,
134 tVariableV,
135 tVariableT,
136 tDecimal,
137 tInteger,
138 tEOF
139 };
140
141
142 class PluralRuleParser: public UMemory {
143 public:
144 PluralRuleParser();
145 virtual ~PluralRuleParser();
146
147 void parse(const UnicodeString &rules, PluralRules *dest, UErrorCode &status);
148 void getNextToken(UErrorCode &status);
149 void checkSyntax(UErrorCode &status);
150 static int32_t getNumberValue(const UnicodeString &token);
151
152 private:
153 static tokenType getKeyType(const UnicodeString& token, tokenType type);
154 static tokenType charType(UChar ch);
155 static UBool isValidKeyword(const UnicodeString& token);
156
157 const UnicodeString *ruleSrc; // The rules string.
158 int32_t ruleIndex; // String index in the input rules, the current parse position.
159 UnicodeString token; // Token most recently scanned.
160 tokenType type;
161 tokenType prevType;
162
163 // The items currently being parsed & built.
164 // Note: currentChain may not be the last RuleChain in the
165 // list because the "other" chain is forced to the end.
166 AndConstraint *curAndConstraint;
167 RuleChain *currentChain;
168
169 int32_t rangeLowIdx; // Indices in the UVector of ranges of the
170 int32_t rangeHiIdx; // low and hi values currently being parsed.
171
172 enum EParseState {
173 kKeyword,
174 kExpr,
175 kValue,
176 kRangeList,
177 kSamples
178 };
179
180 };
181
182 enum PluralOperand {
183 /**
184 * The double value of the entire number.
185 */
186 PLURAL_OPERAND_N,
187
188 /**
189 * The integer value, with the fraction digits truncated off.
190 */
191 PLURAL_OPERAND_I,
192
193 /**
194 * All visible fraction digits as an integer, including trailing zeros.
195 */
196 PLURAL_OPERAND_F,
197
198 /**
199 * Visible fraction digits as an integer, not including trailing zeros.
200 */
201 PLURAL_OPERAND_T,
202
203 /**
204 * Number of visible fraction digits.
205 */
206 PLURAL_OPERAND_V,
207
208 /**
209 * Number of visible fraction digits, not including trailing zeros.
210 */
211 PLURAL_OPERAND_W,
212
213 /**
214 * THIS OPERAND IS DEPRECATED AND HAS BEEN REMOVED FROM THE SPEC.
215 *
216 * <p>Returns the integer value, but will fail if the number has fraction digits.
217 * That is, using "j" instead of "i" is like implicitly adding "v is 0".
218 *
219 * <p>For example, "j is 3" is equivalent to "i is 3 and v is 0": it matches
220 * "3" but not "3.1" or "3.0".
221 */
222 PLURAL_OPERAND_J
223 };
224
225 /**
226 * Converts from the tokenType enum to PluralOperand. Asserts that the given
227 * tokenType can be mapped to a PluralOperand.
228 */
229 PluralOperand tokenTypeToPluralOperand(tokenType tt);
230
231 /**
232 * An interface to FixedDecimal, allowing for other implementations.
233 * @internal
234 */
235 class U_I18N_API IFixedDecimal {
236 public:
237 virtual ~IFixedDecimal();
238
239 /**
240 * Returns the value corresponding to the specified operand (n, i, f, t, v, or w).
241 * If the operand is 'n', returns a double; otherwise, returns an integer.
242 */
243 virtual double getPluralOperand(PluralOperand operand) const = 0;
244
245 virtual bool isNaN() const = 0;
246
247 virtual bool isInfinite() const = 0;
248
249 /** Whether the number has no nonzero fraction digits. */
250 virtual bool hasIntegerValue() const = 0;
251 };
252
253 /**
254 * class FixedDecimal serves to communicate the properties
255 * of a formatted number from a decimal formatter to PluralRules::select()
256 *
257 * see DecimalFormat::getFixedDecimal()
258 * @internal
259 */
260 class U_I18N_API FixedDecimal: public IFixedDecimal, public UObject {
261 public:
262 /**
263 * @param n the number, e.g. 12.345
264 * @param v The number of visible fraction digits, e.g. 3
265 * @param f The fraction digits, e.g. 345
266 */
267 FixedDecimal(double n, int32_t v, int64_t f);
268 FixedDecimal(double n, int32_t);
269 explicit FixedDecimal(double n);
270 explicit FixedDecimal(const VisibleDigits &n);
271 FixedDecimal();
272 ~FixedDecimal() U_OVERRIDE;
273 FixedDecimal(const UnicodeString &s, UErrorCode &ec);
274 FixedDecimal(const FixedDecimal &other);
275
276 double getPluralOperand(PluralOperand operand) const U_OVERRIDE;
277 bool isNaN() const U_OVERRIDE;
278 bool isInfinite() const U_OVERRIDE;
279 bool hasIntegerValue() const U_OVERRIDE;
280
281 bool isNanOrInfinity() const; // used in decimfmtimpl.cpp
282
283 int32_t getVisibleFractionDigitCount() const;
284
285 void init(double n, int32_t v, int64_t f);
286 void init(double n);
287 UBool quickInit(double n); // Try a fast-path only initialization,
288 // return TRUE if successful.
289 void adjustForMinFractionDigits(int32_t min);
290 static int64_t getFractionalDigits(double n, int32_t v);
291 static int32_t decimals(double n);
292
293 double source;
294 int32_t visibleDecimalDigitCount;
295 int64_t decimalDigits;
296 int64_t decimalDigitsWithoutTrailingZeros;
297 int64_t intValue;
298 UBool _hasIntegerValue;
299 UBool isNegative;
300 UBool _isNaN;
301 UBool _isInfinite;
302 };
303
304 class AndConstraint : public UMemory {
305 public:
306 typedef enum RuleOp {
307 NONE,
308 MOD
309 } RuleOp;
310 RuleOp op;
311 int32_t opNum; // for mod expressions, the right operand of the mod.
312 int32_t value; // valid for 'is' rules only.
313 UVector32 *rangeList; // for 'in', 'within' rules. Null otherwise.
314 UBool negated; // TRUE for negated rules.
315 UBool integerOnly; // TRUE for 'within' rules.
316 tokenType digitsType; // n | i | v | f constraint.
317 AndConstraint *next;
318
319 AndConstraint();
320 AndConstraint(const AndConstraint& other);
321 virtual ~AndConstraint();
322 AndConstraint* add();
323 // UBool isFulfilled(double number);
324 UBool isFulfilled(const IFixedDecimal &number);
325 };
326
327 class OrConstraint : public UMemory {
328 public:
329 AndConstraint *childNode;
330 OrConstraint *next;
331 OrConstraint();
332
333 OrConstraint(const OrConstraint& other);
334 virtual ~OrConstraint();
335 AndConstraint* add();
336 // UBool isFulfilled(double number);
337 UBool isFulfilled(const IFixedDecimal &number);
338 };
339
340 class RuleChain : public UMemory {
341 public:
342 UnicodeString fKeyword;
343 RuleChain *fNext;
344 OrConstraint *ruleHeader;
345 UnicodeString fDecimalSamples; // Samples strings from rule source
346 UnicodeString fIntegerSamples; // without @decimal or @integer, otherwise unprocessed.
347 UBool fDecimalSamplesUnbounded;
348 UBool fIntegerSamplesUnbounded;
349
350
351 RuleChain();
352 RuleChain(const RuleChain& other);
353 virtual ~RuleChain();
354
355 UnicodeString select(const IFixedDecimal &number) const;
356 void dumpRules(UnicodeString& result);
357 UErrorCode getKeywords(int32_t maxArraySize, UnicodeString *keywords, int32_t& arraySize) const;
358 UBool isKeyword(const UnicodeString& keyword) const;
359 };
360
361 class PluralKeywordEnumeration : public StringEnumeration {
362 public:
363 PluralKeywordEnumeration(RuleChain *header, UErrorCode& status);
364 virtual ~PluralKeywordEnumeration();
365 static UClassID U_EXPORT2 getStaticClassID(void);
366 virtual UClassID getDynamicClassID(void) const;
367 virtual const UnicodeString* snext(UErrorCode& status);
368 virtual void reset(UErrorCode& status);
369 virtual int32_t count(UErrorCode& status) const;
370 private:
371 int32_t pos;
372 UVector fKeywordNames;
373 };
374
375
376 class U_I18N_API PluralAvailableLocalesEnumeration: public StringEnumeration {
377 public:
378 PluralAvailableLocalesEnumeration(UErrorCode &status);
379 virtual ~PluralAvailableLocalesEnumeration();
380 virtual const char* next(int32_t *resultLength, UErrorCode& status);
381 virtual void reset(UErrorCode& status);
382 virtual int32_t count(UErrorCode& status) const;
383 private:
384 UErrorCode fOpenStatus;
385 UResourceBundle *fLocales;
386 UResourceBundle *fRes;
387 };
388
389 U_NAMESPACE_END
390
391 #endif /* #if !UCONFIG_NO_FORMATTING */
392
393 #endif // _PLURRULE_IMPL
394 //eof