2 *******************************************************************************
3 * Copyright (C) 2007-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
9 *******************************************************************************
16 // Internal definitions for the PluralRules implementation.
18 #include "unicode/utypes.h"
20 #if !UCONFIG_NO_FORMATTING
22 #include "unicode/format.h"
23 #include "unicode/locid.h"
24 #include "unicode/parseerr.h"
25 #include "unicode/ures.h"
29 class PluralRulesTest
;
39 static const UChar DOT
= ((UChar
)0x002E);
40 static const UChar SINGLE_QUOTE
= ((UChar
)0x0027);
41 static const UChar SLASH
= ((UChar
)0x002F);
42 static const UChar BACKSLASH
= ((UChar
)0x005C);
43 static const UChar SPACE
= ((UChar
)0x0020);
44 static const UChar EXCLAMATION
= ((UChar
)0x0021);
45 static const UChar QUOTATION_MARK
= ((UChar
)0x0022);
46 static const UChar NUMBER_SIGN
= ((UChar
)0x0023);
47 static const UChar PERCENT_SIGN
= ((UChar
)0x0025);
48 static const UChar ASTERISK
= ((UChar
)0x002A);
49 static const UChar COMMA
= ((UChar
)0x002C);
50 static const UChar HYPHEN
= ((UChar
)0x002D);
51 static const UChar U_ZERO
= ((UChar
)0x0030);
52 static const UChar U_ONE
= ((UChar
)0x0031);
53 static const UChar U_TWO
= ((UChar
)0x0032);
54 static const UChar U_THREE
= ((UChar
)0x0033);
55 static const UChar U_FOUR
= ((UChar
)0x0034);
56 static const UChar U_FIVE
= ((UChar
)0x0035);
57 static const UChar U_SIX
= ((UChar
)0x0036);
58 static const UChar U_SEVEN
= ((UChar
)0x0037);
59 static const UChar U_EIGHT
= ((UChar
)0x0038);
60 static const UChar U_NINE
= ((UChar
)0x0039);
61 static const UChar COLON
= ((UChar
)0x003A);
62 static const UChar SEMI_COLON
= ((UChar
)0x003B);
63 static const UChar EQUALS
= ((UChar
)0x003D);
64 static const UChar AT
= ((UChar
)0x0040);
65 static const UChar CAP_A
= ((UChar
)0x0041);
66 static const UChar CAP_B
= ((UChar
)0x0042);
67 static const UChar CAP_R
= ((UChar
)0x0052);
68 static const UChar CAP_Z
= ((UChar
)0x005A);
69 static const UChar LOWLINE
= ((UChar
)0x005F);
70 static const UChar LEFTBRACE
= ((UChar
)0x007B);
71 static const UChar RIGHTBRACE
= ((UChar
)0x007D);
72 static const UChar TILDE
= ((UChar
)0x007E);
73 static const UChar ELLIPSIS
= ((UChar
)0x2026);
75 static const UChar LOW_A
= ((UChar
)0x0061);
76 static const UChar LOW_B
= ((UChar
)0x0062);
77 static const UChar LOW_C
= ((UChar
)0x0063);
78 static const UChar LOW_D
= ((UChar
)0x0064);
79 static const UChar LOW_E
= ((UChar
)0x0065);
80 static const UChar LOW_F
= ((UChar
)0x0066);
81 static const UChar LOW_G
= ((UChar
)0x0067);
82 static const UChar LOW_H
= ((UChar
)0x0068);
83 static const UChar LOW_I
= ((UChar
)0x0069);
84 static const UChar LOW_J
= ((UChar
)0x006a);
85 static const UChar LOW_K
= ((UChar
)0x006B);
86 static const UChar LOW_L
= ((UChar
)0x006C);
87 static const UChar LOW_M
= ((UChar
)0x006D);
88 static const UChar LOW_N
= ((UChar
)0x006E);
89 static const UChar LOW_O
= ((UChar
)0x006F);
90 static const UChar LOW_P
= ((UChar
)0x0070);
91 static const UChar LOW_Q
= ((UChar
)0x0071);
92 static const UChar LOW_R
= ((UChar
)0x0072);
93 static const UChar LOW_S
= ((UChar
)0x0073);
94 static const UChar LOW_T
= ((UChar
)0x0074);
95 static const UChar LOW_U
= ((UChar
)0x0075);
96 static const UChar LOW_V
= ((UChar
)0x0076);
97 static const UChar LOW_W
= ((UChar
)0x0077);
98 static const UChar LOW_Y
= ((UChar
)0x0079);
99 static const UChar LOW_Z
= ((UChar
)0x007A);
102 static const int32_t PLURAL_RANGE_HIGH
= 0x7fffffff;
118 tMod
, // 'mod' or '%'
137 class PluralRuleParser
: public UMemory
{
140 virtual ~PluralRuleParser();
142 void parse(const UnicodeString
&rules
, PluralRules
*dest
, UErrorCode
&status
);
143 void getNextToken(UErrorCode
&status
);
144 void checkSyntax(UErrorCode
&status
);
145 static int32_t getNumberValue(const UnicodeString
&token
);
148 static tokenType
getKeyType(const UnicodeString
& token
, tokenType type
);
149 static tokenType
charType(UChar ch
);
150 static UBool
isValidKeyword(const UnicodeString
& token
);
152 const UnicodeString
*ruleSrc
; // The rules string.
153 int32_t ruleIndex
; // String index in the input rules, the current parse position.
154 UnicodeString token
; // Token most recently scanned.
158 // The items currently being parsed & built.
159 // Note: currentChain may not be the last RuleChain in the
160 // list because the "other" chain is forced to the end.
161 AndConstraint
*curAndConstraint
;
162 RuleChain
*currentChain
;
164 int32_t rangeLowIdx
; // Indices in the UVector of ranges of the
165 int32_t rangeHiIdx
; // low and hi values currently being parsed.
178 * class FixedDecimal serves to communicate the properties
179 * of a formatted number from a decimal formatter to PluralRules::select()
181 * see DecimalFormat::getFixedDecimal()
184 class U_I18N_API FixedDecimal
: public UMemory
{
187 * @param n the number, e.g. 12.345
188 * @param v The number of visible fraction digits, e.g. 3
189 * @param f The fraction digits, e.g. 345
191 FixedDecimal(double n
, int32_t v
, int64_t f
);
192 FixedDecimal(double n
, int32_t);
193 explicit FixedDecimal(double n
);
194 explicit FixedDecimal(const VisibleDigits
&n
);
196 FixedDecimal(const UnicodeString
&s
, UErrorCode
&ec
);
197 FixedDecimal(const FixedDecimal
&other
);
199 double get(tokenType operand
) const;
200 int32_t getVisibleFractionDigitCount() const;
202 void init(double n
, int32_t v
, int64_t f
);
204 UBool
quickInit(double n
); // Try a fast-path only initialization,
205 // return TRUE if successful.
206 void adjustForMinFractionDigits(int32_t min
);
207 static int64_t getFractionalDigits(double n
, int32_t v
);
208 static int32_t decimals(double n
);
211 int32_t visibleDecimalDigitCount
;
212 int64_t decimalDigits
;
213 int64_t decimalDigitsWithoutTrailingZeros
;
215 UBool hasIntegerValue
;
217 UBool isNanOrInfinity
;
220 class AndConstraint
: public UMemory
{
222 typedef enum RuleOp
{
227 int32_t opNum
; // for mod expressions, the right operand of the mod.
228 int32_t value
; // valid for 'is' rules only.
229 UVector32
*rangeList
; // for 'in', 'within' rules. Null otherwise.
230 UBool negated
; // TRUE for negated rules.
231 UBool integerOnly
; // TRUE for 'within' rules.
232 tokenType digitsType
; // n | i | v | f constraint.
236 AndConstraint(const AndConstraint
& other
);
237 virtual ~AndConstraint();
238 AndConstraint
* add();
239 // UBool isFulfilled(double number);
240 UBool
isFulfilled(const FixedDecimal
&number
);
243 class OrConstraint
: public UMemory
{
245 AndConstraint
*childNode
;
249 OrConstraint(const OrConstraint
& other
);
250 virtual ~OrConstraint();
251 AndConstraint
* add();
252 // UBool isFulfilled(double number);
253 UBool
isFulfilled(const FixedDecimal
&number
);
256 class RuleChain
: public UMemory
{
258 UnicodeString fKeyword
;
260 OrConstraint
*ruleHeader
;
261 UnicodeString fDecimalSamples
; // Samples strings from rule source
262 UnicodeString fIntegerSamples
; // without @decimal or @integer, otherwise unprocessed.
263 UBool fDecimalSamplesUnbounded
;
264 UBool fIntegerSamplesUnbounded
;
268 RuleChain(const RuleChain
& other
);
269 virtual ~RuleChain();
271 UnicodeString
select(const FixedDecimal
&number
) const;
272 void dumpRules(UnicodeString
& result
);
273 UErrorCode
getKeywords(int32_t maxArraySize
, UnicodeString
*keywords
, int32_t& arraySize
) const;
274 UBool
isKeyword(const UnicodeString
& keyword
) const;
277 class PluralKeywordEnumeration
: public StringEnumeration
{
279 PluralKeywordEnumeration(RuleChain
*header
, UErrorCode
& status
);
280 virtual ~PluralKeywordEnumeration();
281 static UClassID U_EXPORT2
getStaticClassID(void);
282 virtual UClassID
getDynamicClassID(void) const;
283 virtual const UnicodeString
* snext(UErrorCode
& status
);
284 virtual void reset(UErrorCode
& status
);
285 virtual int32_t count(UErrorCode
& status
) const;
288 UVector fKeywordNames
;
292 class U_I18N_API PluralAvailableLocalesEnumeration
: public StringEnumeration
{
294 PluralAvailableLocalesEnumeration(UErrorCode
&status
);
295 virtual ~PluralAvailableLocalesEnumeration();
296 virtual const char* next(int32_t *resultLength
, UErrorCode
& status
);
297 virtual void reset(UErrorCode
& status
);
298 virtual int32_t count(UErrorCode
& status
) const;
300 UErrorCode fOpenStatus
;
301 UResourceBundle
*fLocales
;
302 UResourceBundle
*fRes
;
307 #endif /* #if !UCONFIG_NO_FORMATTING */
309 #endif // _PLURRULE_IMPL