blob: 3a919ea9f5d9bd77d7b91d9dc6becb8e988a63b6 [file] [log] [blame]
Fredrik Roubert0596fae2017-04-18 21:34:02 +02001// © 2016 and later: Unicode, Inc. and others.
Fredrik Roubert64339d32016-10-21 19:43:16 +02002// License & terms of use: http://www.unicode.org/copyright.html
Claire Ho85bf2e22009-11-24 14:23:02 -08003/*
4*******************************************************************************
Fredrik Roubert8de051c2016-03-10 13:13:27 +01005* Copyright (C) 2007-2016, International Business Machines Corporation and
Claire Ho85bf2e22009-11-24 14:23:02 -08006* others. All Rights Reserved.
7*******************************************************************************
8*
9* File PLURRULE_IMPL.H
10*
11*******************************************************************************
12*/
13
14
Markus Scherer0b3ec052015-12-14 21:57:41 +000015#ifndef PLURRULE_IMPL
16#define PLURRULE_IMPL
Claire Ho85bf2e22009-11-24 14:23:02 -080017
clairehob26ce3a2012-01-10 17:54:41 -080018// Internal definitions for the PluralRules implementation.
19
Markus Scherer0b3ec052015-12-14 21:57:41 +000020#include "unicode/utypes.h"
21
Claire Ho85bf2e22009-11-24 14:23:02 -080022#if !UCONFIG_NO_FORMATTING
23
24#include "unicode/format.h"
25#include "unicode/locid.h"
26#include "unicode/parseerr.h"
Fredrik Roubert0596fae2017-04-18 21:34:02 +020027#include "unicode/strenum.h"
ccornelius59d709d2014-02-20 10:29:46 -080028#include "unicode/ures.h"
Claire Ho85bf2e22009-11-24 14:23:02 -080029#include "uvector.h"
30#include "hash.h"
Fredrik Roubertffdc27e2017-11-27 19:42:42 +010031#include "uassert.h"
Claire Ho85bf2e22009-11-24 14:23:02 -080032
ccornelius59d709d2014-02-20 10:29:46 -080033class PluralRulesTest;
34
Claire Ho85bf2e22009-11-24 14:23:02 -080035U_NAMESPACE_BEGIN
36
ccornelius59d709d2014-02-20 10:29:46 -080037class AndConstraint;
38class RuleChain;
Fredrik Roubertc14898b2015-09-28 19:31:03 +020039class DigitInterval;
Fredrik Roubert8de051c2016-03-10 13:13:27 +010040class PluralRules;
Fredrik Roubertc14898b2015-09-28 19:31:03 +020041class VisibleDigits;
Claire Ho85bf2e22009-11-24 14:23:02 -080042
Fredrik Roubert57b72de2018-06-21 14:29:02 +020043namespace pluralimpl {
ccornelius59d709d2014-02-20 10:29:46 -080044
Fredrik Roubert57b72de2018-06-21 14:29:02 +020045// TODO: Remove this and replace with u"" literals. Was for EBCDIC compatibility.
46
47static const UChar DOT = ((UChar) 0x002E);
48static const UChar SINGLE_QUOTE = ((UChar) 0x0027);
49static const UChar SLASH = ((UChar) 0x002F);
50static const UChar BACKSLASH = ((UChar) 0x005C);
51static const UChar SPACE = ((UChar) 0x0020);
52static const UChar EXCLAMATION = ((UChar) 0x0021);
53static const UChar QUOTATION_MARK = ((UChar) 0x0022);
54static const UChar NUMBER_SIGN = ((UChar) 0x0023);
55static const UChar PERCENT_SIGN = ((UChar) 0x0025);
56static const UChar ASTERISK = ((UChar) 0x002A);
57static const UChar COMMA = ((UChar) 0x002C);
58static const UChar HYPHEN = ((UChar) 0x002D);
59static const UChar U_ZERO = ((UChar) 0x0030);
60static const UChar U_ONE = ((UChar) 0x0031);
61static const UChar U_TWO = ((UChar) 0x0032);
62static const UChar U_THREE = ((UChar) 0x0033);
63static const UChar U_FOUR = ((UChar) 0x0034);
64static const UChar U_FIVE = ((UChar) 0x0035);
65static const UChar U_SIX = ((UChar) 0x0036);
66static const UChar U_SEVEN = ((UChar) 0x0037);
67static const UChar U_EIGHT = ((UChar) 0x0038);
68static const UChar U_NINE = ((UChar) 0x0039);
69static const UChar COLON = ((UChar) 0x003A);
70static const UChar SEMI_COLON = ((UChar) 0x003B);
71static const UChar EQUALS = ((UChar) 0x003D);
72static const UChar AT = ((UChar) 0x0040);
73static const UChar CAP_A = ((UChar) 0x0041);
74static const UChar CAP_B = ((UChar) 0x0042);
75static const UChar CAP_R = ((UChar) 0x0052);
76static const UChar CAP_Z = ((UChar) 0x005A);
77static const UChar LOWLINE = ((UChar) 0x005F);
78static const UChar LEFTBRACE = ((UChar) 0x007B);
79static const UChar RIGHTBRACE = ((UChar) 0x007D);
80static const UChar TILDE = ((UChar) 0x007E);
81static const UChar ELLIPSIS = ((UChar) 0x2026);
82
83static const UChar LOW_A = ((UChar) 0x0061);
84static const UChar LOW_B = ((UChar) 0x0062);
85static const UChar LOW_C = ((UChar) 0x0063);
86static const UChar LOW_D = ((UChar) 0x0064);
87static const UChar LOW_E = ((UChar) 0x0065);
88static const UChar LOW_F = ((UChar) 0x0066);
89static const UChar LOW_G = ((UChar) 0x0067);
90static const UChar LOW_H = ((UChar) 0x0068);
91static const UChar LOW_I = ((UChar) 0x0069);
92static const UChar LOW_J = ((UChar) 0x006a);
93static const UChar LOW_K = ((UChar) 0x006B);
94static const UChar LOW_L = ((UChar) 0x006C);
95static const UChar LOW_M = ((UChar) 0x006D);
96static const UChar LOW_N = ((UChar) 0x006E);
97static const UChar LOW_O = ((UChar) 0x006F);
98static const UChar LOW_P = ((UChar) 0x0070);
99static const UChar LOW_Q = ((UChar) 0x0071);
100static const UChar LOW_R = ((UChar) 0x0072);
101static const UChar LOW_S = ((UChar) 0x0073);
102static const UChar LOW_T = ((UChar) 0x0074);
103static const UChar LOW_U = ((UChar) 0x0075);
104static const UChar LOW_V = ((UChar) 0x0076);
105static const UChar LOW_W = ((UChar) 0x0077);
106static const UChar LOW_Y = ((UChar) 0x0079);
107static const UChar LOW_Z = ((UChar) 0x007A);
108
109}
Claire Ho85bf2e22009-11-24 14:23:02 -0800110
111
ccornelius59d709d2014-02-20 10:29:46 -0800112static const int32_t PLURAL_RANGE_HIGH = 0x7fffffff;
Claire Ho85bf2e22009-11-24 14:23:02 -0800113
ccornelius59d709d2014-02-20 10:29:46 -0800114enum tokenType {
Claire Ho85bf2e22009-11-24 14:23:02 -0800115 none,
Claire Ho85bf2e22009-11-24 14:23:02 -0800116 tNumber,
117 tComma,
118 tSemiColon,
119 tSpace,
120 tColon,
ccornelius59d709d2014-02-20 10:29:46 -0800121 tAt, // '@'
Claire Ho85bf2e22009-11-24 14:23:02 -0800122 tDot,
ccornelius59d709d2014-02-20 10:29:46 -0800123 tDot2,
124 tEllipsis,
Claire Ho85bf2e22009-11-24 14:23:02 -0800125 tKeyword,
Claire Ho85bf2e22009-11-24 14:23:02 -0800126 tAnd,
127 tOr,
ccornelius59d709d2014-02-20 10:29:46 -0800128 tMod, // 'mod' or '%'
129 tNot, // 'not' only.
130 tIn, // 'in' only.
131 tEqual, // '=' only.
132 tNotEqual, // '!='
133 tTilde,
Claire Ho85bf2e22009-11-24 14:23:02 -0800134 tWithin,
Claire Ho85bf2e22009-11-24 14:23:02 -0800135 tIs,
ccornelius59d709d2014-02-20 10:29:46 -0800136 tVariableN,
137 tVariableI,
138 tVariableF,
139 tVariableV,
140 tVariableT,
141 tDecimal,
142 tInteger,
143 tEOF
144};
Claire Ho85bf2e22009-11-24 14:23:02 -0800145
ccornelius59d709d2014-02-20 10:29:46 -0800146
147class PluralRuleParser: public UMemory {
Claire Ho85bf2e22009-11-24 14:23:02 -0800148public:
ccornelius59d709d2014-02-20 10:29:46 -0800149 PluralRuleParser();
150 virtual ~PluralRuleParser();
151
152 void parse(const UnicodeString &rules, PluralRules *dest, UErrorCode &status);
153 void getNextToken(UErrorCode &status);
154 void checkSyntax(UErrorCode &status);
155 static int32_t getNumberValue(const UnicodeString &token);
156
Claire Ho85bf2e22009-11-24 14:23:02 -0800157private:
ccornelius59d709d2014-02-20 10:29:46 -0800158 static tokenType getKeyType(const UnicodeString& token, tokenType type);
159 static tokenType charType(UChar ch);
160 static UBool isValidKeyword(const UnicodeString& token);
161
162 const UnicodeString *ruleSrc; // The rules string.
163 int32_t ruleIndex; // String index in the input rules, the current parse position.
164 UnicodeString token; // Token most recently scanned.
165 tokenType type;
166 tokenType prevType;
167
168 // The items currently being parsed & built.
169 // Note: currentChain may not be the last RuleChain in the
170 // list because the "other" chain is forced to the end.
171 AndConstraint *curAndConstraint;
172 RuleChain *currentChain;
173
174 int32_t rangeLowIdx; // Indices in the UVector of ranges of the
175 int32_t rangeHiIdx; // low and hi values currently being parsed.
176
177 enum EParseState {
178 kKeyword,
179 kExpr,
180 kValue,
181 kRangeList,
182 kSamples
183 };
ccornelius59d709d2014-02-20 10:29:46 -0800184};
185
Fredrik Roubertffdc27e2017-11-27 19:42:42 +0100186enum PluralOperand {
187 /**
188 * The double value of the entire number.
189 */
190 PLURAL_OPERAND_N,
191
192 /**
193 * The integer value, with the fraction digits truncated off.
194 */
195 PLURAL_OPERAND_I,
196
197 /**
198 * All visible fraction digits as an integer, including trailing zeros.
199 */
200 PLURAL_OPERAND_F,
201
202 /**
203 * Visible fraction digits as an integer, not including trailing zeros.
204 */
205 PLURAL_OPERAND_T,
206
207 /**
208 * Number of visible fraction digits.
209 */
210 PLURAL_OPERAND_V,
211
212 /**
213 * Number of visible fraction digits, not including trailing zeros.
214 */
215 PLURAL_OPERAND_W,
216
217 /**
218 * THIS OPERAND IS DEPRECATED AND HAS BEEN REMOVED FROM THE SPEC.
219 *
220 * <p>Returns the integer value, but will fail if the number has fraction digits.
221 * That is, using "j" instead of "i" is like implicitly adding "v is 0".
222 *
223 * <p>For example, "j is 3" is equivalent to "i is 3 and v is 0": it matches
224 * "3" but not "3.1" or "3.0".
225 */
226 PLURAL_OPERAND_J
227};
228
229/**
230 * Converts from the tokenType enum to PluralOperand. Asserts that the given
231 * tokenType can be mapped to a PluralOperand.
232 */
233PluralOperand tokenTypeToPluralOperand(tokenType tt);
234
235/**
236 * An interface to FixedDecimal, allowing for other implementations.
237 * @internal
238 */
239class U_I18N_API IFixedDecimal {
240 public:
241 virtual ~IFixedDecimal();
242
243 /**
244 * Returns the value corresponding to the specified operand (n, i, f, t, v, or w).
245 * If the operand is 'n', returns a double; otherwise, returns an integer.
246 */
247 virtual double getPluralOperand(PluralOperand operand) const = 0;
248
249 virtual bool isNaN() const = 0;
250
251 virtual bool isInfinite() const = 0;
Fredrik Roubert57b72de2018-06-21 14:29:02 +0200252
253 /** Whether the number has no nonzero fraction digits. */
254 virtual bool hasIntegerValue() const = 0;
Fredrik Roubertffdc27e2017-11-27 19:42:42 +0100255};
256
ccornelius59d709d2014-02-20 10:29:46 -0800257/**
258 * class FixedDecimal serves to communicate the properties
259 * of a formatted number from a decimal formatter to PluralRules::select()
260 *
261 * see DecimalFormat::getFixedDecimal()
262 * @internal
263 */
Fredrik Roubertffdc27e2017-11-27 19:42:42 +0100264class U_I18N_API FixedDecimal: public IFixedDecimal, public UObject {
ccornelius59d709d2014-02-20 10:29:46 -0800265 public:
266 /**
267 * @param n the number, e.g. 12.345
268 * @param v The number of visible fraction digits, e.g. 3
269 * @param f The fraction digits, e.g. 345
270 */
271 FixedDecimal(double n, int32_t v, int64_t f);
272 FixedDecimal(double n, int32_t);
273 explicit FixedDecimal(double n);
274 FixedDecimal();
Fredrik Roubertffdc27e2017-11-27 19:42:42 +0100275 ~FixedDecimal() U_OVERRIDE;
ccornelius59d709d2014-02-20 10:29:46 -0800276 FixedDecimal(const UnicodeString &s, UErrorCode &ec);
277 FixedDecimal(const FixedDecimal &other);
278
Fredrik Roubertffdc27e2017-11-27 19:42:42 +0100279 double getPluralOperand(PluralOperand operand) const U_OVERRIDE;
280 bool isNaN() const U_OVERRIDE;
281 bool isInfinite() const U_OVERRIDE;
Fredrik Roubert57b72de2018-06-21 14:29:02 +0200282 bool hasIntegerValue() const U_OVERRIDE;
Fredrik Roubertffdc27e2017-11-27 19:42:42 +0100283
284 bool isNanOrInfinity() const; // used in decimfmtimpl.cpp
285
ccornelius59d709d2014-02-20 10:29:46 -0800286 int32_t getVisibleFractionDigitCount() const;
287
288 void init(double n, int32_t v, int64_t f);
289 void init(double n);
290 UBool quickInit(double n); // Try a fast-path only initialization,
291 // return TRUE if successful.
292 void adjustForMinFractionDigits(int32_t min);
293 static int64_t getFractionalDigits(double n, int32_t v);
294 static int32_t decimals(double n);
295
296 double source;
297 int32_t visibleDecimalDigitCount;
298 int64_t decimalDigits;
299 int64_t decimalDigitsWithoutTrailingZeros;
300 int64_t intValue;
Fredrik Roubert57b72de2018-06-21 14:29:02 +0200301 UBool _hasIntegerValue;
ccornelius59d709d2014-02-20 10:29:46 -0800302 UBool isNegative;
Fredrik Roubertffdc27e2017-11-27 19:42:42 +0100303 UBool _isNaN;
304 UBool _isInfinite;
Claire Ho85bf2e22009-11-24 14:23:02 -0800305};
306
307class AndConstraint : public UMemory {
308public:
309 typedef enum RuleOp {
310 NONE,
311 MOD
312 } RuleOp;
Victor Chang4578a1c2018-10-22 04:26:58 +0100313 RuleOp op = AndConstraint::NONE;
314 int32_t opNum = -1; // for mod expressions, the right operand of the mod.
315 int32_t value = -1; // valid for 'is' rules only.
316 UVector32 *rangeList = nullptr; // for 'in', 'within' rules. Null otherwise.
317 UBool negated = FALSE; // TRUE for negated rules.
318 UBool integerOnly = FALSE; // TRUE for 'within' rules.
319 tokenType digitsType = none; // n | i | v | f constraint.
320 AndConstraint *next = nullptr;
321 // Internal error status, used for errors that occur during the copy constructor.
322 UErrorCode fInternalStatus = U_ZERO_ERROR;
clairehob26ce3a2012-01-10 17:54:41 -0800323
Victor Chang4578a1c2018-10-22 04:26:58 +0100324 AndConstraint() = default;
Claire Ho85bf2e22009-11-24 14:23:02 -0800325 AndConstraint(const AndConstraint& other);
326 virtual ~AndConstraint();
Victor Chang4578a1c2018-10-22 04:26:58 +0100327 AndConstraint* add(UErrorCode& status);
ccornelius59d709d2014-02-20 10:29:46 -0800328 // UBool isFulfilled(double number);
Fredrik Roubertffdc27e2017-11-27 19:42:42 +0100329 UBool isFulfilled(const IFixedDecimal &number);
Claire Ho85bf2e22009-11-24 14:23:02 -0800330};
331
332class OrConstraint : public UMemory {
333public:
Victor Chang4578a1c2018-10-22 04:26:58 +0100334 AndConstraint *childNode = nullptr;
335 OrConstraint *next = nullptr;
336 // Internal error status, used for errors that occur during the copy constructor.
337 UErrorCode fInternalStatus = U_ZERO_ERROR;
clairehob26ce3a2012-01-10 17:54:41 -0800338
Victor Chang4578a1c2018-10-22 04:26:58 +0100339 OrConstraint() = default;
Claire Ho85bf2e22009-11-24 14:23:02 -0800340 OrConstraint(const OrConstraint& other);
341 virtual ~OrConstraint();
Victor Chang4578a1c2018-10-22 04:26:58 +0100342 AndConstraint* add(UErrorCode& status);
ccornelius59d709d2014-02-20 10:29:46 -0800343 // UBool isFulfilled(double number);
Fredrik Roubertffdc27e2017-11-27 19:42:42 +0100344 UBool isFulfilled(const IFixedDecimal &number);
Claire Ho85bf2e22009-11-24 14:23:02 -0800345};
346
347class RuleChain : public UMemory {
348public:
ccornelius59d709d2014-02-20 10:29:46 -0800349 UnicodeString fKeyword;
Victor Chang4578a1c2018-10-22 04:26:58 +0100350 RuleChain *fNext = nullptr;
351 OrConstraint *ruleHeader = nullptr;
ccornelius59d709d2014-02-20 10:29:46 -0800352 UnicodeString fDecimalSamples; // Samples strings from rule source
353 UnicodeString fIntegerSamples; // without @decimal or @integer, otherwise unprocessed.
Victor Chang4578a1c2018-10-22 04:26:58 +0100354 UBool fDecimalSamplesUnbounded = FALSE;
355 UBool fIntegerSamplesUnbounded = FALSE;
356 // Internal error status, used for errors that occur during the copy constructor.
357 UErrorCode fInternalStatus = U_ZERO_ERROR;
ccornelius59d709d2014-02-20 10:29:46 -0800358
Victor Chang4578a1c2018-10-22 04:26:58 +0100359 RuleChain() = default;
Claire Ho85bf2e22009-11-24 14:23:02 -0800360 RuleChain(const RuleChain& other);
Claire Ho85bf2e22009-11-24 14:23:02 -0800361 virtual ~RuleChain();
ccornelius59d709d2014-02-20 10:29:46 -0800362
Fredrik Roubertffdc27e2017-11-27 19:42:42 +0100363 UnicodeString select(const IFixedDecimal &number) const;
ccornelius59d709d2014-02-20 10:29:46 -0800364 void dumpRules(UnicodeString& result);
365 UErrorCode getKeywords(int32_t maxArraySize, UnicodeString *keywords, int32_t& arraySize) const;
366 UBool isKeyword(const UnicodeString& keyword) const;
Claire Ho85bf2e22009-11-24 14:23:02 -0800367};
368
369class PluralKeywordEnumeration : public StringEnumeration {
370public:
371 PluralKeywordEnumeration(RuleChain *header, UErrorCode& status);
372 virtual ~PluralKeywordEnumeration();
373 static UClassID U_EXPORT2 getStaticClassID(void);
374 virtual UClassID getDynamicClassID(void) const;
375 virtual const UnicodeString* snext(UErrorCode& status);
376 virtual void reset(UErrorCode& status);
377 virtual int32_t count(UErrorCode& status) const;
378private:
ccornelius59d709d2014-02-20 10:29:46 -0800379 int32_t pos;
380 UVector fKeywordNames;
381};
382
383
384class U_I18N_API PluralAvailableLocalesEnumeration: public StringEnumeration {
385 public:
386 PluralAvailableLocalesEnumeration(UErrorCode &status);
387 virtual ~PluralAvailableLocalesEnumeration();
388 virtual const char* next(int32_t *resultLength, UErrorCode& status);
389 virtual void reset(UErrorCode& status);
390 virtual int32_t count(UErrorCode& status) const;
391 private:
392 UErrorCode fOpenStatus;
Victor Chang4578a1c2018-10-22 04:26:58 +0100393 UResourceBundle *fLocales = nullptr;
394 UResourceBundle *fRes = nullptr;
Claire Ho85bf2e22009-11-24 14:23:02 -0800395};
396
397U_NAMESPACE_END
398
399#endif /* #if !UCONFIG_NO_FORMATTING */
400
401#endif // _PLURRULE_IMPL
402//eof