]>
Commit | Line | Data |
---|---|---|
46f4442e A |
1 | /* |
2 | ******************************************************************************* | |
2ca993e8 | 3 | * Copyright (C) 2007-2016, International Business Machines Corporation and |
46f4442e A |
4 | * others. All Rights Reserved. |
5 | ******************************************************************************* | |
6 | * | |
51004dcb | 7 | * File plurrule.cpp |
46f4442e A |
8 | */ |
9 | ||
57a6839d A |
10 | #include <math.h> |
11 | #include <stdio.h> | |
12 | ||
46f4442e | 13 | #include "unicode/utypes.h" |
4388f060 | 14 | #include "unicode/localpointer.h" |
46f4442e | 15 | #include "unicode/plurrule.h" |
51004dcb | 16 | #include "unicode/upluralrules.h" |
4388f060 | 17 | #include "unicode/ures.h" |
57a6839d | 18 | #include "charstr.h" |
46f4442e A |
19 | #include "cmemory.h" |
20 | #include "cstring.h" | |
57a6839d | 21 | #include "digitlst.h" |
46f4442e | 22 | #include "hash.h" |
57a6839d | 23 | #include "locutil.h" |
46f4442e | 24 | #include "mutex.h" |
4388f060 | 25 | #include "patternprops.h" |
46f4442e A |
26 | #include "plurrule_impl.h" |
27 | #include "putilimp.h" | |
28 | #include "ucln_in.h" | |
29 | #include "ustrfmt.h" | |
4388f060 | 30 | #include "uassert.h" |
57a6839d A |
31 | #include "uvectr32.h" |
32 | #include "sharedpluralrules.h" | |
b331163b | 33 | #include "unifiedcache.h" |
2ca993e8 A |
34 | #include "digitinterval.h" |
35 | #include "visibledigits.h" | |
36 | ||
46f4442e A |
37 | |
38 | #if !UCONFIG_NO_FORMATTING | |
39 | ||
57a6839d | 40 | U_NAMESPACE_BEGIN |
46f4442e | 41 | |
46f4442e A |
42 | static const UChar PLURAL_KEYWORD_OTHER[]={LOW_O,LOW_T,LOW_H,LOW_E,LOW_R,0}; |
43 | static const UChar PLURAL_DEFAULT_RULE[]={LOW_O,LOW_T,LOW_H,LOW_E,LOW_R,COLON,SPACE,LOW_N,0}; | |
44 | static const UChar PK_IN[]={LOW_I,LOW_N,0}; | |
45 | static const UChar PK_NOT[]={LOW_N,LOW_O,LOW_T,0}; | |
46 | static const UChar PK_IS[]={LOW_I,LOW_S,0}; | |
47 | static const UChar PK_MOD[]={LOW_M,LOW_O,LOW_D,0}; | |
48 | static const UChar PK_AND[]={LOW_A,LOW_N,LOW_D,0}; | |
49 | static const UChar PK_OR[]={LOW_O,LOW_R,0}; | |
50 | static const UChar PK_VAR_N[]={LOW_N,0}; | |
57a6839d A |
51 | static const UChar PK_VAR_I[]={LOW_I,0}; |
52 | static const UChar PK_VAR_F[]={LOW_F,0}; | |
53 | static const UChar PK_VAR_T[]={LOW_T,0}; | |
54 | static const UChar PK_VAR_V[]={LOW_V,0}; | |
46f4442e | 55 | static const UChar PK_WITHIN[]={LOW_W,LOW_I,LOW_T,LOW_H,LOW_I,LOW_N,0}; |
57a6839d A |
56 | static const UChar PK_DECIMAL[]={LOW_D,LOW_E,LOW_C,LOW_I,LOW_M,LOW_A,LOW_L,0}; |
57 | static const UChar PK_INTEGER[]={LOW_I,LOW_N,LOW_T,LOW_E,LOW_G,LOW_E,LOW_R,0}; | |
46f4442e A |
58 | |
59 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(PluralRules) | |
60 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(PluralKeywordEnumeration) | |
61 | ||
57a6839d | 62 | PluralRules::PluralRules(UErrorCode& /*status*/) |
46f4442e | 63 | : UObject(), |
57a6839d | 64 | mRules(NULL) |
46f4442e | 65 | { |
46f4442e A |
66 | } |
67 | ||
68 | PluralRules::PluralRules(const PluralRules& other) | |
69 | : UObject(other), | |
57a6839d | 70 | mRules(NULL) |
46f4442e A |
71 | { |
72 | *this=other; | |
73 | } | |
74 | ||
75 | PluralRules::~PluralRules() { | |
76 | delete mRules; | |
57a6839d A |
77 | } |
78 | ||
79 | SharedPluralRules::~SharedPluralRules() { | |
80 | delete ptr; | |
46f4442e A |
81 | } |
82 | ||
83 | PluralRules* | |
84 | PluralRules::clone() const { | |
85 | return new PluralRules(*this); | |
86 | } | |
87 | ||
88 | PluralRules& | |
89 | PluralRules::operator=(const PluralRules& other) { | |
90 | if (this != &other) { | |
91 | delete mRules; | |
92 | if (other.mRules==NULL) { | |
93 | mRules = NULL; | |
94 | } | |
95 | else { | |
96 | mRules = new RuleChain(*other.mRules); | |
97 | } | |
46f4442e A |
98 | } |
99 | ||
100 | return *this; | |
101 | } | |
102 | ||
57a6839d A |
103 | StringEnumeration* PluralRules::getAvailableLocales(UErrorCode &status) { |
104 | StringEnumeration *result = new PluralAvailableLocalesEnumeration(status); | |
105 | if (result == NULL && U_SUCCESS(status)) { | |
106 | status = U_MEMORY_ALLOCATION_ERROR; | |
107 | } | |
108 | if (U_FAILURE(status)) { | |
109 | delete result; | |
110 | result = NULL; | |
111 | } | |
112 | return result; | |
113 | } | |
114 | ||
115 | ||
46f4442e A |
116 | PluralRules* U_EXPORT2 |
117 | PluralRules::createRules(const UnicodeString& description, UErrorCode& status) { | |
729e4ab9 A |
118 | if (U_FAILURE(status)) { |
119 | return NULL; | |
120 | } | |
57a6839d A |
121 | |
122 | PluralRuleParser parser; | |
46f4442e | 123 | PluralRules *newRules = new PluralRules(status); |
57a6839d A |
124 | if (U_SUCCESS(status) && newRules == NULL) { |
125 | status = U_MEMORY_ALLOCATION_ERROR; | |
46f4442e | 126 | } |
57a6839d | 127 | parser.parse(description, newRules, status); |
46f4442e A |
128 | if (U_FAILURE(status)) { |
129 | delete newRules; | |
57a6839d | 130 | newRules = NULL; |
46f4442e | 131 | } |
57a6839d | 132 | return newRules; |
46f4442e A |
133 | } |
134 | ||
57a6839d | 135 | |
46f4442e A |
136 | PluralRules* U_EXPORT2 |
137 | PluralRules::createDefaultRules(UErrorCode& status) { | |
4388f060 | 138 | return createRules(UnicodeString(TRUE, PLURAL_DEFAULT_RULE, -1), status); |
46f4442e A |
139 | } |
140 | ||
57a6839d A |
141 | /******************************************************************************/ |
142 | /* Create PluralRules cache */ | |
143 | ||
b331163b A |
144 | template<> U_I18N_API |
145 | const SharedPluralRules *LocaleCacheKey<SharedPluralRules>::createObject( | |
146 | const void * /*unused*/, UErrorCode &status) const { | |
147 | const char *localeId = fLoc.getName(); | |
57a6839d A |
148 | PluralRules *pr = PluralRules::internalForLocale( |
149 | localeId, UPLURAL_TYPE_CARDINAL, status); | |
150 | if (U_FAILURE(status)) { | |
151 | return NULL; | |
152 | } | |
b331163b | 153 | SharedPluralRules *result = new SharedPluralRules(pr); |
57a6839d A |
154 | if (result == NULL) { |
155 | status = U_MEMORY_ALLOCATION_ERROR; | |
156 | delete pr; | |
157 | return NULL; | |
158 | } | |
b331163b | 159 | result->addRef(); |
57a6839d A |
160 | return result; |
161 | } | |
162 | ||
57a6839d A |
163 | /* end plural rules cache */ |
164 | /******************************************************************************/ | |
165 | ||
166 | const SharedPluralRules* U_EXPORT2 | |
167 | PluralRules::createSharedInstance( | |
168 | const Locale& locale, UPluralType type, UErrorCode& status) { | |
169 | if (U_FAILURE(status)) { | |
170 | return NULL; | |
171 | } | |
172 | if (type != UPLURAL_TYPE_CARDINAL) { | |
173 | status = U_UNSUPPORTED_ERROR; | |
174 | return NULL; | |
175 | } | |
176 | const SharedPluralRules *result = NULL; | |
b331163b | 177 | UnifiedCache::getByLocale(locale, result, status); |
57a6839d A |
178 | return result; |
179 | } | |
180 | ||
46f4442e A |
181 | PluralRules* U_EXPORT2 |
182 | PluralRules::forLocale(const Locale& locale, UErrorCode& status) { | |
51004dcb A |
183 | return forLocale(locale, UPLURAL_TYPE_CARDINAL, status); |
184 | } | |
185 | ||
186 | PluralRules* U_EXPORT2 | |
187 | PluralRules::forLocale(const Locale& locale, UPluralType type, UErrorCode& status) { | |
57a6839d A |
188 | if (type != UPLURAL_TYPE_CARDINAL) { |
189 | return internalForLocale(locale, type, status); | |
190 | } | |
191 | const SharedPluralRules *shared = createSharedInstance( | |
192 | locale, type, status); | |
193 | if (U_FAILURE(status)) { | |
194 | return NULL; | |
195 | } | |
196 | PluralRules *result = (*shared)->clone(); | |
197 | shared->removeRef(); | |
198 | if (result == NULL) { | |
199 | status = U_MEMORY_ALLOCATION_ERROR; | |
200 | } | |
201 | return result; | |
202 | } | |
203 | ||
204 | PluralRules* U_EXPORT2 | |
205 | PluralRules::internalForLocale(const Locale& locale, UPluralType type, UErrorCode& status) { | |
729e4ab9 A |
206 | if (U_FAILURE(status)) { |
207 | return NULL; | |
208 | } | |
51004dcb A |
209 | if (type >= UPLURAL_TYPE_COUNT) { |
210 | status = U_ILLEGAL_ARGUMENT_ERROR; | |
211 | return NULL; | |
212 | } | |
46f4442e | 213 | PluralRules *newObj = new PluralRules(status); |
729e4ab9 | 214 | if (newObj==NULL || U_FAILURE(status)) { |
4388f060 | 215 | delete newObj; |
46f4442e A |
216 | return NULL; |
217 | } | |
51004dcb | 218 | UnicodeString locRule = newObj->getRuleFromResource(locale, type, status); |
57a6839d A |
219 | // TODO: which errors, if any, should be returned? |
220 | if (locRule.length() == 0) { | |
221 | // Locales with no specific rules (all numbers have the "other" category | |
222 | // will return a U_MISSING_RESOURCE_ERROR at this point. This is not | |
223 | // an error. | |
224 | locRule = UnicodeString(PLURAL_DEFAULT_RULE); | |
46f4442e | 225 | status = U_ZERO_ERROR; |
46f4442e | 226 | } |
57a6839d A |
227 | PluralRuleParser parser; |
228 | parser.parse(locRule, newObj, status); | |
229 | // TODO: should rule parse errors be returned, or | |
230 | // should we silently use default rules? | |
231 | // Original impl used default rules. | |
232 | // Ask the question to ICU Core. | |
4388f060 | 233 | |
46f4442e A |
234 | return newObj; |
235 | } | |
236 | ||
237 | UnicodeString | |
238 | PluralRules::select(int32_t number) const { | |
57a6839d | 239 | return select(FixedDecimal(number)); |
46f4442e A |
240 | } |
241 | ||
242 | UnicodeString | |
243 | PluralRules::select(double number) const { | |
57a6839d A |
244 | return select(FixedDecimal(number)); |
245 | } | |
246 | ||
247 | UnicodeString | |
248 | PluralRules::select(const FixedDecimal &number) const { | |
46f4442e | 249 | if (mRules == NULL) { |
4388f060 | 250 | return UnicodeString(TRUE, PLURAL_DEFAULT_RULE, -1); |
46f4442e A |
251 | } |
252 | else { | |
253 | return mRules->select(number); | |
254 | } | |
255 | } | |
256 | ||
2ca993e8 A |
257 | UnicodeString |
258 | PluralRules::select(const VisibleDigitsWithExponent &number) const { | |
259 | if (number.getExponent() != NULL) { | |
260 | return UnicodeString(TRUE, PLURAL_DEFAULT_RULE, -1); | |
261 | } | |
262 | return select(FixedDecimal(number.getMantissa())); | |
263 | } | |
264 | ||
265 | ||
266 | ||
46f4442e A |
267 | StringEnumeration* |
268 | PluralRules::getKeywords(UErrorCode& status) const { | |
269 | if (U_FAILURE(status)) return NULL; | |
270 | StringEnumeration* nameEnumerator = new PluralKeywordEnumeration(mRules, status); | |
4388f060 A |
271 | if (U_FAILURE(status)) { |
272 | delete nameEnumerator; | |
273 | return NULL; | |
274 | } | |
729e4ab9 | 275 | |
46f4442e A |
276 | return nameEnumerator; |
277 | } | |
278 | ||
4388f060 | 279 | double |
57a6839d A |
280 | PluralRules::getUniqueKeywordValue(const UnicodeString& /* keyword */) { |
281 | // Not Implemented. | |
282 | return UPLRULES_NO_UNIQUE_VALUE; | |
4388f060 A |
283 | } |
284 | ||
285 | int32_t | |
57a6839d A |
286 | PluralRules::getAllKeywordValues(const UnicodeString & /* keyword */, double * /* dest */, |
287 | int32_t /* destCapacity */, UErrorCode& error) { | |
288 | error = U_UNSUPPORTED_ERROR; | |
289 | return 0; | |
4388f060 A |
290 | } |
291 | ||
57a6839d A |
292 | |
293 | static double scaleForInt(double d) { | |
294 | double scale = 1.0; | |
295 | while (d != floor(d)) { | |
296 | d = d * 10.0; | |
297 | scale = scale * 10.0; | |
4388f060 | 298 | } |
57a6839d A |
299 | return scale; |
300 | } | |
4388f060 | 301 | |
57a6839d A |
302 | static int32_t |
303 | getSamplesFromString(const UnicodeString &samples, double *dest, | |
304 | int32_t destCapacity, UErrorCode& status) { | |
305 | int32_t sampleCount = 0; | |
306 | int32_t sampleStartIdx = 0; | |
307 | int32_t sampleEndIdx = 0; | |
308 | ||
309 | //std::string ss; // TODO: debugging. | |
310 | // std::cout << "PluralRules::getSamples(), samples = \"" << samples.toUTF8String(ss) << "\"\n"; | |
311 | for (sampleCount = 0; sampleCount < destCapacity && sampleStartIdx < samples.length(); ) { | |
312 | sampleEndIdx = samples.indexOf(COMMA, sampleStartIdx); | |
313 | if (sampleEndIdx == -1) { | |
314 | sampleEndIdx = samples.length(); | |
315 | } | |
316 | const UnicodeString &sampleRange = samples.tempSubStringBetween(sampleStartIdx, sampleEndIdx); | |
317 | // ss.erase(); | |
318 | // std::cout << "PluralRules::getSamples(), samplesRange = \"" << sampleRange.toUTF8String(ss) << "\"\n"; | |
319 | int32_t tildeIndex = sampleRange.indexOf(TILDE); | |
320 | if (tildeIndex < 0) { | |
321 | FixedDecimal fixed(sampleRange, status); | |
322 | double sampleValue = fixed.source; | |
323 | if (fixed.visibleDecimalDigitCount == 0 || sampleValue != floor(sampleValue)) { | |
324 | dest[sampleCount++] = sampleValue; | |
325 | } | |
326 | } else { | |
327 | ||
328 | FixedDecimal fixedLo(sampleRange.tempSubStringBetween(0, tildeIndex), status); | |
329 | FixedDecimal fixedHi(sampleRange.tempSubStringBetween(tildeIndex+1), status); | |
330 | double rangeLo = fixedLo.source; | |
331 | double rangeHi = fixedHi.source; | |
332 | if (U_FAILURE(status)) { | |
333 | break; | |
334 | } | |
335 | if (rangeHi < rangeLo) { | |
336 | status = U_INVALID_FORMAT_ERROR; | |
337 | break; | |
338 | } | |
4388f060 | 339 | |
57a6839d A |
340 | // For ranges of samples with fraction decimal digits, scale the number up so that we |
341 | // are adding one in the units place. Avoids roundoffs from repetitive adds of tenths. | |
4388f060 | 342 | |
57a6839d A |
343 | double scale = scaleForInt(rangeLo); |
344 | double t = scaleForInt(rangeHi); | |
345 | if (t > scale) { | |
346 | scale = t; | |
347 | } | |
348 | rangeLo *= scale; | |
349 | rangeHi *= scale; | |
350 | for (double n=rangeLo; n<=rangeHi; n+=1) { | |
351 | // Hack Alert: don't return any decimal samples with integer values that | |
352 | // originated from a format with trailing decimals. | |
353 | // This API is returning doubles, which can't distinguish having displayed | |
354 | // zeros to the right of the decimal. | |
355 | // This results in test failures with values mapping back to a different keyword. | |
356 | double sampleValue = n/scale; | |
357 | if (!(sampleValue == floor(sampleValue) && fixedLo.visibleDecimalDigitCount > 0)) { | |
358 | dest[sampleCount++] = sampleValue; | |
359 | } | |
360 | if (sampleCount >= destCapacity) { | |
361 | break; | |
362 | } | |
363 | } | |
4388f060 | 364 | } |
57a6839d | 365 | sampleStartIdx = sampleEndIdx + 1; |
4388f060 | 366 | } |
57a6839d A |
367 | return sampleCount; |
368 | } | |
4388f060 | 369 | |
57a6839d A |
370 | |
371 | int32_t | |
372 | PluralRules::getSamples(const UnicodeString &keyword, double *dest, | |
373 | int32_t destCapacity, UErrorCode& status) { | |
374 | RuleChain *rc = rulesForKeyword(keyword); | |
375 | if (rc == NULL || destCapacity == 0 || U_FAILURE(status)) { | |
376 | return 0; | |
4388f060 | 377 | } |
57a6839d A |
378 | int32_t numSamples = getSamplesFromString(rc->fIntegerSamples, dest, destCapacity, status); |
379 | if (numSamples == 0) { | |
380 | numSamples = getSamplesFromString(rc->fDecimalSamples, dest, destCapacity, status); | |
4388f060 | 381 | } |
57a6839d A |
382 | return numSamples; |
383 | } | |
384 | ||
385 | ||
386 | RuleChain *PluralRules::rulesForKeyword(const UnicodeString &keyword) const { | |
387 | RuleChain *rc; | |
388 | for (rc = mRules; rc != NULL; rc = rc->fNext) { | |
389 | if (rc->fKeyword == keyword) { | |
390 | break; | |
391 | } | |
392 | } | |
393 | return rc; | |
4388f060 A |
394 | } |
395 | ||
46f4442e A |
396 | |
397 | UBool | |
398 | PluralRules::isKeyword(const UnicodeString& keyword) const { | |
4388f060 | 399 | if (0 == keyword.compare(PLURAL_KEYWORD_OTHER, 5)) { |
46f4442e A |
400 | return true; |
401 | } | |
57a6839d | 402 | return rulesForKeyword(keyword) != NULL; |
46f4442e A |
403 | } |
404 | ||
405 | UnicodeString | |
406 | PluralRules::getKeywordOther() const { | |
4388f060 | 407 | return UnicodeString(TRUE, PLURAL_KEYWORD_OTHER, 5); |
46f4442e A |
408 | } |
409 | ||
410 | UBool | |
411 | PluralRules::operator==(const PluralRules& other) const { | |
46f4442e A |
412 | const UnicodeString *ptrKeyword; |
413 | UErrorCode status= U_ZERO_ERROR; | |
414 | ||
415 | if ( this == &other ) { | |
416 | return TRUE; | |
417 | } | |
4388f060 A |
418 | LocalPointer<StringEnumeration> myKeywordList(getKeywords(status)); |
419 | LocalPointer<StringEnumeration> otherKeywordList(other.getKeywords(status)); | |
729e4ab9 A |
420 | if (U_FAILURE(status)) { |
421 | return FALSE; | |
422 | } | |
46f4442e | 423 | |
4388f060 A |
424 | if (myKeywordList->count(status)!=otherKeywordList->count(status)) { |
425 | return FALSE; | |
46f4442e | 426 | } |
4388f060 A |
427 | myKeywordList->reset(status); |
428 | while ((ptrKeyword=myKeywordList->snext(status))!=NULL) { | |
429 | if (!other.isKeyword(*ptrKeyword)) { | |
729e4ab9 A |
430 | return FALSE; |
431 | } | |
4388f060 A |
432 | } |
433 | otherKeywordList->reset(status); | |
434 | while ((ptrKeyword=otherKeywordList->snext(status))!=NULL) { | |
435 | if (!this->isKeyword(*ptrKeyword)) { | |
46f4442e A |
436 | return FALSE; |
437 | } | |
438 | } | |
4388f060 A |
439 | if (U_FAILURE(status)) { |
440 | return FALSE; | |
441 | } | |
46f4442e | 442 | |
46f4442e A |
443 | return TRUE; |
444 | } | |
445 | ||
57a6839d | 446 | |
46f4442e | 447 | void |
57a6839d | 448 | PluralRuleParser::parse(const UnicodeString& ruleData, PluralRules *prules, UErrorCode &status) |
46f4442e | 449 | { |
729e4ab9 A |
450 | if (U_FAILURE(status)) { |
451 | return; | |
452 | } | |
57a6839d A |
453 | U_ASSERT(ruleIndex == 0); // Parsers are good for a single use only! |
454 | ruleSrc = &ruleData; | |
455 | ||
456 | while (ruleIndex< ruleSrc->length()) { | |
457 | getNextToken(status); | |
46f4442e A |
458 | if (U_FAILURE(status)) { |
459 | return; | |
460 | } | |
57a6839d | 461 | checkSyntax(status); |
46f4442e A |
462 | if (U_FAILURE(status)) { |
463 | return; | |
464 | } | |
465 | switch (type) { | |
466 | case tAnd: | |
4388f060 | 467 | U_ASSERT(curAndConstraint != NULL); |
46f4442e A |
468 | curAndConstraint = curAndConstraint->add(); |
469 | break; | |
470 | case tOr: | |
57a6839d A |
471 | { |
472 | U_ASSERT(currentChain != NULL); | |
473 | OrConstraint *orNode=currentChain->ruleHeader; | |
474 | while (orNode->next != NULL) { | |
475 | orNode = orNode->next; | |
476 | } | |
477 | orNode->next= new OrConstraint(); | |
478 | orNode=orNode->next; | |
479 | orNode->next=NULL; | |
480 | curAndConstraint = orNode->add(); | |
46f4442e | 481 | } |
46f4442e A |
482 | break; |
483 | case tIs: | |
4388f060 | 484 | U_ASSERT(curAndConstraint != NULL); |
57a6839d A |
485 | U_ASSERT(curAndConstraint->value == -1); |
486 | U_ASSERT(curAndConstraint->rangeList == NULL); | |
46f4442e A |
487 | break; |
488 | case tNot: | |
4388f060 | 489 | U_ASSERT(curAndConstraint != NULL); |
57a6839d | 490 | curAndConstraint->negated=TRUE; |
46f4442e | 491 | break; |
57a6839d A |
492 | |
493 | case tNotEqual: | |
494 | curAndConstraint->negated=TRUE; | |
2ca993e8 | 495 | U_FALLTHROUGH; |
46f4442e | 496 | case tIn: |
46f4442e | 497 | case tWithin: |
57a6839d | 498 | case tEqual: |
4388f060 | 499 | U_ASSERT(curAndConstraint != NULL); |
57a6839d A |
500 | curAndConstraint->rangeList = new UVector32(status); |
501 | curAndConstraint->rangeList->addElement(-1, status); // range Low | |
502 | curAndConstraint->rangeList->addElement(-1, status); // range Hi | |
503 | rangeLowIdx = 0; | |
504 | rangeHiIdx = 1; | |
505 | curAndConstraint->value=PLURAL_RANGE_HIGH; | |
506 | curAndConstraint->integerOnly = (type != tWithin); | |
46f4442e A |
507 | break; |
508 | case tNumber: | |
4388f060 | 509 | U_ASSERT(curAndConstraint != NULL); |
46f4442e A |
510 | if ( (curAndConstraint->op==AndConstraint::MOD)&& |
511 | (curAndConstraint->opNum == -1 ) ) { | |
512 | curAndConstraint->opNum=getNumberValue(token); | |
513 | } | |
514 | else { | |
57a6839d A |
515 | if (curAndConstraint->rangeList == NULL) { |
516 | // this is for an 'is' rule | |
517 | curAndConstraint->value = getNumberValue(token); | |
518 | } else { | |
519 | // this is for an 'in' or 'within' rule | |
520 | if (curAndConstraint->rangeList->elementAti(rangeLowIdx) == -1) { | |
521 | curAndConstraint->rangeList->setElementAt(getNumberValue(token), rangeLowIdx); | |
522 | curAndConstraint->rangeList->setElementAt(getNumberValue(token), rangeHiIdx); | |
523 | } | |
524 | else { | |
525 | curAndConstraint->rangeList->setElementAt(getNumberValue(token), rangeHiIdx); | |
526 | if (curAndConstraint->rangeList->elementAti(rangeLowIdx) > | |
527 | curAndConstraint->rangeList->elementAti(rangeHiIdx)) { | |
528 | // Range Lower bound > Range Upper bound. | |
529 | // U_UNEXPECTED_TOKEN seems a little funny, but it is consistently | |
530 | // used for all plural rule parse errors. | |
531 | status = U_UNEXPECTED_TOKEN; | |
532 | break; | |
533 | } | |
534 | } | |
46f4442e A |
535 | } |
536 | } | |
537 | break; | |
57a6839d A |
538 | case tComma: |
539 | // TODO: rule syntax checking is inadequate, can happen with badly formed rules. | |
540 | // Catch cases like "n mod 10, is 1" here instead. | |
541 | if (curAndConstraint == NULL || curAndConstraint->rangeList == NULL) { | |
542 | status = U_UNEXPECTED_TOKEN; | |
543 | break; | |
544 | } | |
545 | U_ASSERT(curAndConstraint->rangeList->size() >= 2); | |
546 | rangeLowIdx = curAndConstraint->rangeList->size(); | |
547 | curAndConstraint->rangeList->addElement(-1, status); // range Low | |
548 | rangeHiIdx = curAndConstraint->rangeList->size(); | |
549 | curAndConstraint->rangeList->addElement(-1, status); // range Hi | |
550 | break; | |
46f4442e | 551 | case tMod: |
4388f060 | 552 | U_ASSERT(curAndConstraint != NULL); |
46f4442e A |
553 | curAndConstraint->op=AndConstraint::MOD; |
554 | break; | |
57a6839d A |
555 | case tVariableN: |
556 | case tVariableI: | |
557 | case tVariableF: | |
558 | case tVariableT: | |
559 | case tVariableV: | |
560 | U_ASSERT(curAndConstraint != NULL); | |
561 | curAndConstraint->digitsType = type; | |
562 | break; | |
46f4442e | 563 | case tKeyword: |
57a6839d A |
564 | { |
565 | RuleChain *newChain = new RuleChain; | |
566 | if (newChain == NULL) { | |
567 | status = U_MEMORY_ALLOCATION_ERROR; | |
568 | break; | |
46f4442e | 569 | } |
57a6839d A |
570 | newChain->fKeyword = token; |
571 | if (prules->mRules == NULL) { | |
572 | prules->mRules = newChain; | |
573 | } else { | |
574 | // The new rule chain goes at the end of the linked list of rule chains, | |
575 | // unless there is an "other" keyword & chain. "other" must remain last. | |
576 | RuleChain *insertAfter = prules->mRules; | |
577 | while (insertAfter->fNext!=NULL && | |
578 | insertAfter->fNext->fKeyword.compare(PLURAL_KEYWORD_OTHER, 5) != 0 ){ | |
579 | insertAfter=insertAfter->fNext; | |
46f4442e | 580 | } |
57a6839d A |
581 | newChain->fNext = insertAfter->fNext; |
582 | insertAfter->fNext = newChain; | |
4388f060 | 583 | } |
57a6839d A |
584 | OrConstraint *orNode = new OrConstraint(); |
585 | newChain->ruleHeader = orNode; | |
46f4442e | 586 | curAndConstraint = orNode->add(); |
57a6839d A |
587 | currentChain = newChain; |
588 | } | |
46f4442e | 589 | break; |
46f4442e | 590 | |
57a6839d A |
591 | case tInteger: |
592 | for (;;) { | |
593 | getNextToken(status); | |
594 | if (U_FAILURE(status) || type == tSemiColon || type == tEOF || type == tAt) { | |
595 | break; | |
4388f060 | 596 | } |
57a6839d A |
597 | if (type == tEllipsis) { |
598 | currentChain->fIntegerSamplesUnbounded = TRUE; | |
599 | continue; | |
600 | } | |
601 | currentChain->fIntegerSamples.append(token); | |
4388f060 | 602 | } |
57a6839d | 603 | break; |
4388f060 | 604 | |
57a6839d A |
605 | case tDecimal: |
606 | for (;;) { | |
607 | getNextToken(status); | |
608 | if (U_FAILURE(status) || type == tSemiColon || type == tEOF || type == tAt) { | |
4388f060 A |
609 | break; |
610 | } | |
57a6839d A |
611 | if (type == tEllipsis) { |
612 | currentChain->fDecimalSamplesUnbounded = TRUE; | |
613 | continue; | |
614 | } | |
615 | currentChain->fDecimalSamples.append(token); | |
4388f060 | 616 | } |
57a6839d A |
617 | break; |
618 | ||
619 | default: | |
620 | break; | |
4388f060 | 621 | } |
57a6839d A |
622 | prevType=type; |
623 | if (U_FAILURE(status)) { | |
624 | break; | |
4388f060 A |
625 | } |
626 | } | |
46f4442e A |
627 | } |
628 | ||
629 | UnicodeString | |
51004dcb | 630 | PluralRules::getRuleFromResource(const Locale& locale, UPluralType type, UErrorCode& errCode) { |
46f4442e | 631 | UnicodeString emptyStr; |
4388f060 | 632 | |
729e4ab9 A |
633 | if (U_FAILURE(errCode)) { |
634 | return emptyStr; | |
635 | } | |
51004dcb | 636 | LocalUResourceBundlePointer rb(ures_openDirect(NULL, "plurals", &errCode)); |
46f4442e | 637 | if(U_FAILURE(errCode)) { |
46f4442e A |
638 | return emptyStr; |
639 | } | |
51004dcb A |
640 | const char *typeKey; |
641 | switch (type) { | |
642 | case UPLURAL_TYPE_CARDINAL: | |
643 | typeKey = "locales"; | |
644 | break; | |
645 | case UPLURAL_TYPE_ORDINAL: | |
646 | typeKey = "locales_ordinals"; | |
647 | break; | |
648 | default: | |
649 | // Must not occur: The caller should have checked for valid types. | |
650 | errCode = U_ILLEGAL_ARGUMENT_ERROR; | |
651 | return emptyStr; | |
652 | } | |
653 | LocalUResourceBundlePointer locRes(ures_getByKey(rb.getAlias(), typeKey, NULL, &errCode)); | |
46f4442e | 654 | if(U_FAILURE(errCode)) { |
46f4442e | 655 | return emptyStr; |
4388f060 | 656 | } |
46f4442e A |
657 | int32_t resLen=0; |
658 | const char *curLocaleName=locale.getName(); | |
51004dcb | 659 | const UChar* s = ures_getStringByKey(locRes.getAlias(), curLocaleName, &resLen, &errCode); |
46f4442e A |
660 | |
661 | if (s == NULL) { | |
662 | // Check parent locales. | |
663 | UErrorCode status = U_ZERO_ERROR; | |
664 | char parentLocaleName[ULOC_FULLNAME_CAPACITY]; | |
665 | const char *curLocaleName=locale.getName(); | |
46f4442e | 666 | uprv_strcpy(parentLocaleName, curLocaleName); |
4388f060 | 667 | |
51004dcb A |
668 | while (uloc_getParent(parentLocaleName, parentLocaleName, |
669 | ULOC_FULLNAME_CAPACITY, &status) > 0) { | |
46f4442e | 670 | resLen=0; |
51004dcb | 671 | s = ures_getStringByKey(locRes.getAlias(), parentLocaleName, &resLen, &status); |
46f4442e A |
672 | if (s != NULL) { |
673 | errCode = U_ZERO_ERROR; | |
674 | break; | |
675 | } | |
676 | status = U_ZERO_ERROR; | |
677 | } | |
678 | } | |
679 | if (s==NULL) { | |
46f4442e A |
680 | return emptyStr; |
681 | } | |
4388f060 | 682 | |
46f4442e | 683 | char setKey[256]; |
46f4442e A |
684 | u_UCharsToChars(s, setKey, resLen + 1); |
685 | // printf("\n PluralRule: %s\n", setKey); | |
4388f060 | 686 | |
51004dcb | 687 | LocalUResourceBundlePointer ruleRes(ures_getByKey(rb.getAlias(), "rules", NULL, &errCode)); |
46f4442e | 688 | if(U_FAILURE(errCode)) { |
46f4442e A |
689 | return emptyStr; |
690 | } | |
51004dcb | 691 | LocalUResourceBundlePointer setRes(ures_getByKey(ruleRes.getAlias(), setKey, NULL, &errCode)); |
46f4442e | 692 | if (U_FAILURE(errCode)) { |
46f4442e A |
693 | return emptyStr; |
694 | } | |
695 | ||
51004dcb | 696 | int32_t numberKeys = ures_getSize(setRes.getAlias()); |
57a6839d A |
697 | UnicodeString result; |
698 | const char *key=NULL; | |
699 | for(int32_t i=0; i<numberKeys; ++i) { // Keys are zero, one, few, ... | |
700 | UnicodeString rules = ures_getNextUnicodeString(setRes.getAlias(), &key, &errCode); | |
701 | UnicodeString uKey(key, -1, US_INV); | |
702 | result.append(uKey); | |
703 | result.append(COLON); | |
704 | result.append(rules); | |
705 | result.append(SEMI_COLON); | |
706 | } | |
707 | return result; | |
708 | } | |
709 | ||
710 | ||
711 | UnicodeString | |
712 | PluralRules::getRules() const { | |
713 | UnicodeString rules; | |
714 | if (mRules != NULL) { | |
715 | mRules->dumpRules(rules); | |
716 | } | |
717 | return rules; | |
46f4442e A |
718 | } |
719 | ||
57a6839d | 720 | |
46f4442e A |
721 | AndConstraint::AndConstraint() { |
722 | op = AndConstraint::NONE; | |
723 | opNum=-1; | |
57a6839d A |
724 | value = -1; |
725 | rangeList = NULL; | |
726 | negated = FALSE; | |
727 | integerOnly = FALSE; | |
728 | digitsType = none; | |
46f4442e A |
729 | next=NULL; |
730 | } | |
731 | ||
732 | ||
733 | AndConstraint::AndConstraint(const AndConstraint& other) { | |
734 | this->op = other.op; | |
735 | this->opNum=other.opNum; | |
57a6839d A |
736 | this->value=other.value; |
737 | this->rangeList=NULL; | |
738 | if (other.rangeList != NULL) { | |
739 | UErrorCode status = U_ZERO_ERROR; | |
740 | this->rangeList = new UVector32(status); | |
741 | this->rangeList->assign(*other.rangeList, status); | |
742 | } | |
46f4442e | 743 | this->integerOnly=other.integerOnly; |
57a6839d A |
744 | this->negated=other.negated; |
745 | this->digitsType = other.digitsType; | |
46f4442e A |
746 | if (other.next==NULL) { |
747 | this->next=NULL; | |
748 | } | |
749 | else { | |
750 | this->next = new AndConstraint(*other.next); | |
751 | } | |
752 | } | |
753 | ||
754 | AndConstraint::~AndConstraint() { | |
57a6839d | 755 | delete rangeList; |
46f4442e A |
756 | if (next!=NULL) { |
757 | delete next; | |
758 | } | |
759 | } | |
760 | ||
761 | ||
762 | UBool | |
57a6839d A |
763 | AndConstraint::isFulfilled(const FixedDecimal &number) { |
764 | UBool result = TRUE; | |
765 | if (digitsType == none) { | |
766 | // An empty AndConstraint, created by a rule with a keyword but no following expression. | |
767 | return TRUE; | |
46f4442e | 768 | } |
57a6839d A |
769 | double n = number.get(digitsType); // pulls n | i | v | f value for the number. |
770 | // Will always be positive. | |
771 | // May be non-integer (n option only) | |
772 | do { | |
773 | if (integerOnly && n != uprv_floor(n)) { | |
774 | result = FALSE; | |
775 | break; | |
776 | } | |
777 | ||
778 | if (op == MOD) { | |
779 | n = fmod(n, opNum); | |
780 | } | |
781 | if (rangeList == NULL) { | |
782 | result = value == -1 || // empty rule | |
783 | n == value; // 'is' rule | |
784 | break; | |
46f4442e | 785 | } |
57a6839d A |
786 | result = FALSE; // 'in' or 'within' rule |
787 | for (int32_t r=0; r<rangeList->size(); r+=2) { | |
788 | if (rangeList->elementAti(r) <= n && n <= rangeList->elementAti(r+1)) { | |
789 | result = TRUE; | |
790 | break; | |
791 | } | |
46f4442e | 792 | } |
57a6839d A |
793 | } while (FALSE); |
794 | ||
795 | if (negated) { | |
796 | result = !result; | |
46f4442e | 797 | } |
57a6839d | 798 | return result; |
46f4442e A |
799 | } |
800 | ||
801 | ||
802 | AndConstraint* | |
803 | AndConstraint::add() | |
804 | { | |
805 | this->next = new AndConstraint(); | |
806 | return this->next; | |
807 | } | |
808 | ||
809 | OrConstraint::OrConstraint() { | |
810 | childNode=NULL; | |
811 | next=NULL; | |
812 | } | |
813 | ||
814 | OrConstraint::OrConstraint(const OrConstraint& other) { | |
815 | if ( other.childNode == NULL ) { | |
816 | this->childNode = NULL; | |
817 | } | |
818 | else { | |
819 | this->childNode = new AndConstraint(*(other.childNode)); | |
820 | } | |
821 | if (other.next == NULL ) { | |
822 | this->next = NULL; | |
823 | } | |
824 | else { | |
825 | this->next = new OrConstraint(*(other.next)); | |
826 | } | |
827 | } | |
828 | ||
829 | OrConstraint::~OrConstraint() { | |
830 | if (childNode!=NULL) { | |
831 | delete childNode; | |
832 | } | |
833 | if (next!=NULL) { | |
834 | delete next; | |
835 | } | |
836 | } | |
837 | ||
838 | AndConstraint* | |
839 | OrConstraint::add() | |
840 | { | |
841 | OrConstraint *curOrConstraint=this; | |
842 | { | |
843 | while (curOrConstraint->next!=NULL) { | |
844 | curOrConstraint = curOrConstraint->next; | |
845 | } | |
57a6839d | 846 | U_ASSERT(curOrConstraint->childNode == NULL); |
46f4442e A |
847 | curOrConstraint->childNode = new AndConstraint(); |
848 | } | |
849 | return curOrConstraint->childNode; | |
850 | } | |
851 | ||
852 | UBool | |
57a6839d | 853 | OrConstraint::isFulfilled(const FixedDecimal &number) { |
46f4442e A |
854 | OrConstraint* orRule=this; |
855 | UBool result=FALSE; | |
4388f060 | 856 | |
46f4442e A |
857 | while (orRule!=NULL && !result) { |
858 | result=TRUE; | |
859 | AndConstraint* andRule = orRule->childNode; | |
860 | while (andRule!=NULL && result) { | |
861 | result = andRule->isFulfilled(number); | |
862 | andRule=andRule->next; | |
863 | } | |
864 | orRule = orRule->next; | |
865 | } | |
4388f060 | 866 | |
46f4442e A |
867 | return result; |
868 | } | |
869 | ||
870 | ||
57a6839d A |
871 | RuleChain::RuleChain(): fKeyword(), fNext(NULL), ruleHeader(NULL), fDecimalSamples(), fIntegerSamples(), |
872 | fDecimalSamplesUnbounded(FALSE), fIntegerSamplesUnbounded(FALSE) { | |
46f4442e A |
873 | } |
874 | ||
57a6839d A |
875 | RuleChain::RuleChain(const RuleChain& other) : |
876 | fKeyword(other.fKeyword), fNext(NULL), ruleHeader(NULL), fDecimalSamples(other.fDecimalSamples), | |
877 | fIntegerSamples(other.fIntegerSamples), fDecimalSamplesUnbounded(other.fDecimalSamplesUnbounded), | |
878 | fIntegerSamplesUnbounded(other.fIntegerSamplesUnbounded) { | |
46f4442e A |
879 | if (other.ruleHeader != NULL) { |
880 | this->ruleHeader = new OrConstraint(*(other.ruleHeader)); | |
881 | } | |
57a6839d A |
882 | if (other.fNext != NULL ) { |
883 | this->fNext = new RuleChain(*other.fNext); | |
46f4442e A |
884 | } |
885 | } | |
886 | ||
887 | RuleChain::~RuleChain() { | |
57a6839d A |
888 | delete fNext; |
889 | delete ruleHeader; | |
46f4442e A |
890 | } |
891 | ||
4388f060 | 892 | |
57a6839d A |
893 | UnicodeString |
894 | RuleChain::select(const FixedDecimal &number) const { | |
895 | if (!number.isNanOrInfinity) { | |
896 | for (const RuleChain *rules = this; rules != NULL; rules = rules->fNext) { | |
897 | if (rules->ruleHeader->isFulfilled(number)) { | |
898 | return rules->fKeyword; | |
899 | } | |
900 | } | |
901 | } | |
902 | return UnicodeString(TRUE, PLURAL_KEYWORD_OTHER, 5); | |
903 | } | |
46f4442e | 904 | |
57a6839d A |
905 | static UnicodeString tokenString(tokenType tok) { |
906 | UnicodeString s; | |
907 | switch (tok) { | |
908 | case tVariableN: | |
909 | s.append(LOW_N); break; | |
910 | case tVariableI: | |
911 | s.append(LOW_I); break; | |
912 | case tVariableF: | |
913 | s.append(LOW_F); break; | |
914 | case tVariableV: | |
915 | s.append(LOW_V); break; | |
916 | case tVariableT: | |
917 | s.append(LOW_T); break; | |
918 | default: | |
919 | s.append(TILDE); | |
920 | } | |
921 | return s; | |
46f4442e A |
922 | } |
923 | ||
924 | void | |
925 | RuleChain::dumpRules(UnicodeString& result) { | |
926 | UChar digitString[16]; | |
4388f060 | 927 | |
46f4442e | 928 | if ( ruleHeader != NULL ) { |
57a6839d A |
929 | result += fKeyword; |
930 | result += COLON; | |
931 | result += SPACE; | |
46f4442e A |
932 | OrConstraint* orRule=ruleHeader; |
933 | while ( orRule != NULL ) { | |
934 | AndConstraint* andRule=orRule->childNode; | |
935 | while ( andRule != NULL ) { | |
57a6839d A |
936 | if ((andRule->op==AndConstraint::NONE) && (andRule->rangeList==NULL) && (andRule->value == -1)) { |
937 | // Empty Rules. | |
938 | } else if ( (andRule->op==AndConstraint::NONE) && (andRule->rangeList==NULL) ) { | |
939 | result += tokenString(andRule->digitsType); | |
940 | result += UNICODE_STRING_SIMPLE(" is "); | |
941 | if (andRule->negated) { | |
46f4442e A |
942 | result += UNICODE_STRING_SIMPLE("not "); |
943 | } | |
57a6839d | 944 | uprv_itou(digitString,16, andRule->value,10,0); |
46f4442e A |
945 | result += UnicodeString(digitString); |
946 | } | |
947 | else { | |
57a6839d A |
948 | result += tokenString(andRule->digitsType); |
949 | result += SPACE; | |
46f4442e | 950 | if (andRule->op==AndConstraint::MOD) { |
57a6839d | 951 | result += UNICODE_STRING_SIMPLE("mod "); |
46f4442e A |
952 | uprv_itou(digitString,16, andRule->opNum,10,0); |
953 | result += UnicodeString(digitString); | |
954 | } | |
57a6839d A |
955 | if (andRule->rangeList==NULL) { |
956 | if (andRule->negated) { | |
46f4442e | 957 | result += UNICODE_STRING_SIMPLE(" is not "); |
57a6839d | 958 | uprv_itou(digitString,16, andRule->value,10,0); |
46f4442e A |
959 | result += UnicodeString(digitString); |
960 | } | |
961 | else { | |
962 | result += UNICODE_STRING_SIMPLE(" is "); | |
57a6839d | 963 | uprv_itou(digitString,16, andRule->value,10,0); |
46f4442e A |
964 | result += UnicodeString(digitString); |
965 | } | |
966 | } | |
967 | else { | |
57a6839d | 968 | if (andRule->negated) { |
46f4442e | 969 | if ( andRule->integerOnly ) { |
57a6839d | 970 | result += UNICODE_STRING_SIMPLE(" not in "); |
46f4442e A |
971 | } |
972 | else { | |
57a6839d | 973 | result += UNICODE_STRING_SIMPLE(" not within "); |
46f4442e | 974 | } |
46f4442e A |
975 | } |
976 | else { | |
977 | if ( andRule->integerOnly ) { | |
978 | result += UNICODE_STRING_SIMPLE(" in "); | |
979 | } | |
980 | else { | |
981 | result += UNICODE_STRING_SIMPLE(" within "); | |
982 | } | |
57a6839d A |
983 | } |
984 | for (int32_t r=0; r<andRule->rangeList->size(); r+=2) { | |
985 | int32_t rangeLo = andRule->rangeList->elementAti(r); | |
986 | int32_t rangeHi = andRule->rangeList->elementAti(r+1); | |
987 | uprv_itou(digitString,16, rangeLo, 10, 0); | |
46f4442e | 988 | result += UnicodeString(digitString); |
57a6839d A |
989 | result += UNICODE_STRING_SIMPLE(".."); |
990 | uprv_itou(digitString,16, rangeHi, 10,0); | |
991 | result += UnicodeString(digitString); | |
992 | if (r+2 < andRule->rangeList->size()) { | |
993 | result += UNICODE_STRING_SIMPLE(", "); | |
994 | } | |
46f4442e A |
995 | } |
996 | } | |
997 | } | |
998 | if ( (andRule=andRule->next) != NULL) { | |
57a6839d | 999 | result += UNICODE_STRING_SIMPLE(" and "); |
46f4442e A |
1000 | } |
1001 | } | |
1002 | if ( (orRule = orRule->next) != NULL ) { | |
57a6839d | 1003 | result += UNICODE_STRING_SIMPLE(" or "); |
46f4442e A |
1004 | } |
1005 | } | |
1006 | } | |
57a6839d A |
1007 | if ( fNext != NULL ) { |
1008 | result += UNICODE_STRING_SIMPLE("; "); | |
1009 | fNext->dumpRules(result); | |
46f4442e A |
1010 | } |
1011 | } | |
1012 | ||
46f4442e A |
1013 | |
1014 | UErrorCode | |
1015 | RuleChain::getKeywords(int32_t capacityOfKeywords, UnicodeString* keywords, int32_t& arraySize) const { | |
1016 | if ( arraySize < capacityOfKeywords-1 ) { | |
57a6839d | 1017 | keywords[arraySize++]=fKeyword; |
46f4442e A |
1018 | } |
1019 | else { | |
1020 | return U_BUFFER_OVERFLOW_ERROR; | |
1021 | } | |
1022 | ||
57a6839d A |
1023 | if ( fNext != NULL ) { |
1024 | return fNext->getKeywords(capacityOfKeywords, keywords, arraySize); | |
46f4442e A |
1025 | } |
1026 | else { | |
1027 | return U_ZERO_ERROR; | |
1028 | } | |
1029 | } | |
1030 | ||
1031 | UBool | |
1032 | RuleChain::isKeyword(const UnicodeString& keywordParam) const { | |
57a6839d | 1033 | if ( fKeyword == keywordParam ) { |
46f4442e A |
1034 | return TRUE; |
1035 | } | |
1036 | ||
57a6839d A |
1037 | if ( fNext != NULL ) { |
1038 | return fNext->isKeyword(keywordParam); | |
46f4442e A |
1039 | } |
1040 | else { | |
1041 | return FALSE; | |
1042 | } | |
1043 | } | |
1044 | ||
1045 | ||
57a6839d A |
1046 | PluralRuleParser::PluralRuleParser() : |
1047 | ruleIndex(0), token(), type(none), prevType(none), | |
1048 | curAndConstraint(NULL), currentChain(NULL), rangeLowIdx(-1), rangeHiIdx(-1) | |
1049 | { | |
1050 | } | |
1051 | ||
1052 | PluralRuleParser::~PluralRuleParser() { | |
46f4442e A |
1053 | } |
1054 | ||
57a6839d A |
1055 | |
1056 | int32_t | |
1057 | PluralRuleParser::getNumberValue(const UnicodeString& token) { | |
1058 | int32_t i; | |
1059 | char digits[128]; | |
1060 | ||
2ca993e8 | 1061 | i = token.extract(0, token.length(), digits, UPRV_LENGTHOF(digits), US_INV); |
57a6839d A |
1062 | digits[i]='\0'; |
1063 | ||
1064 | return((int32_t)atoi(digits)); | |
46f4442e A |
1065 | } |
1066 | ||
57a6839d | 1067 | |
46f4442e | 1068 | void |
57a6839d | 1069 | PluralRuleParser::checkSyntax(UErrorCode &status) |
46f4442e A |
1070 | { |
1071 | if (U_FAILURE(status)) { | |
1072 | return; | |
1073 | } | |
57a6839d A |
1074 | if (!(prevType==none || prevType==tSemiColon)) { |
1075 | type = getKeyType(token, type); // Switch token type from tKeyword if we scanned a reserved word, | |
1076 | // and we are not at the start of a rule, where a | |
1077 | // keyword is expected. | |
1078 | } | |
1079 | ||
46f4442e A |
1080 | switch(prevType) { |
1081 | case none: | |
1082 | case tSemiColon: | |
57a6839d | 1083 | if (type!=tKeyword && type != tEOF) { |
46f4442e A |
1084 | status = U_UNEXPECTED_TOKEN; |
1085 | } | |
1086 | break; | |
57a6839d A |
1087 | case tVariableN: |
1088 | case tVariableI: | |
1089 | case tVariableF: | |
1090 | case tVariableT: | |
1091 | case tVariableV: | |
1092 | if (type != tIs && type != tMod && type != tIn && | |
1093 | type != tNot && type != tWithin && type != tEqual && type != tNotEqual) { | |
46f4442e A |
1094 | status = U_UNEXPECTED_TOKEN; |
1095 | } | |
1096 | break; | |
46f4442e | 1097 | case tKeyword: |
57a6839d | 1098 | if (type != tColon) { |
46f4442e A |
1099 | status = U_UNEXPECTED_TOKEN; |
1100 | } | |
1101 | break; | |
57a6839d A |
1102 | case tColon: |
1103 | if (!(type == tVariableN || | |
1104 | type == tVariableI || | |
1105 | type == tVariableF || | |
1106 | type == tVariableT || | |
1107 | type == tVariableV || | |
1108 | type == tAt)) { | |
46f4442e A |
1109 | status = U_UNEXPECTED_TOKEN; |
1110 | } | |
1111 | break; | |
1112 | case tIs: | |
57a6839d | 1113 | if ( type != tNumber && type != tNot) { |
46f4442e A |
1114 | status = U_UNEXPECTED_TOKEN; |
1115 | } | |
1116 | break; | |
1117 | case tNot: | |
57a6839d | 1118 | if (type != tNumber && type != tIn && type != tWithin) { |
46f4442e A |
1119 | status = U_UNEXPECTED_TOKEN; |
1120 | } | |
1121 | break; | |
1122 | case tMod: | |
57a6839d | 1123 | case tDot2: |
46f4442e A |
1124 | case tIn: |
1125 | case tWithin: | |
57a6839d A |
1126 | case tEqual: |
1127 | case tNotEqual: | |
1128 | if (type != tNumber) { | |
1129 | status = U_UNEXPECTED_TOKEN; | |
1130 | } | |
1131 | break; | |
46f4442e A |
1132 | case tAnd: |
1133 | case tOr: | |
57a6839d A |
1134 | if ( type != tVariableN && |
1135 | type != tVariableI && | |
1136 | type != tVariableF && | |
1137 | type != tVariableT && | |
1138 | type != tVariableV) { | |
1139 | status = U_UNEXPECTED_TOKEN; | |
1140 | } | |
1141 | break; | |
1142 | case tComma: | |
1143 | if (type != tNumber) { | |
46f4442e A |
1144 | status = U_UNEXPECTED_TOKEN; |
1145 | } | |
1146 | break; | |
1147 | case tNumber: | |
57a6839d A |
1148 | if (type != tDot2 && type != tSemiColon && type != tIs && type != tNot && |
1149 | type != tIn && type != tEqual && type != tNotEqual && type != tWithin && | |
1150 | type != tAnd && type != tOr && type != tComma && type != tAt && | |
1151 | type != tEOF) | |
46f4442e A |
1152 | { |
1153 | status = U_UNEXPECTED_TOKEN; | |
1154 | } | |
57a6839d A |
1155 | // TODO: a comma following a number that is not part of a range will be allowed. |
1156 | // It's not the only case of this sort of thing. Parser needs a re-write. | |
1157 | break; | |
1158 | case tAt: | |
1159 | if (type != tDecimal && type != tInteger) { | |
1160 | status = U_UNEXPECTED_TOKEN; | |
1161 | } | |
46f4442e A |
1162 | break; |
1163 | default: | |
1164 | status = U_UNEXPECTED_TOKEN; | |
1165 | break; | |
1166 | } | |
1167 | } | |
1168 | ||
57a6839d A |
1169 | |
1170 | /* | |
1171 | * Scan the next token from the input rules. | |
1172 | * rules and returned token type are in the parser state variables. | |
1173 | */ | |
46f4442e | 1174 | void |
57a6839d | 1175 | PluralRuleParser::getNextToken(UErrorCode &status) |
46f4442e | 1176 | { |
729e4ab9 A |
1177 | if (U_FAILURE(status)) { |
1178 | return; | |
1179 | } | |
57a6839d A |
1180 | |
1181 | UChar ch; | |
1182 | while (ruleIndex < ruleSrc->length()) { | |
1183 | ch = ruleSrc->charAt(ruleIndex); | |
1184 | type = charType(ch); | |
1185 | if (type != tSpace) { | |
1186 | break; | |
46f4442e | 1187 | } |
57a6839d | 1188 | ++(ruleIndex); |
46f4442e | 1189 | } |
57a6839d A |
1190 | if (ruleIndex >= ruleSrc->length()) { |
1191 | type = tEOF; | |
1192 | return; | |
46f4442e | 1193 | } |
57a6839d A |
1194 | int32_t curIndex= ruleIndex; |
1195 | ||
1196 | switch (type) { | |
1197 | case tColon: | |
1198 | case tSemiColon: | |
1199 | case tComma: | |
1200 | case tEllipsis: | |
1201 | case tTilde: // scanned '~' | |
1202 | case tAt: // scanned '@' | |
1203 | case tEqual: // scanned '=' | |
1204 | case tMod: // scanned '%' | |
1205 | // Single character tokens. | |
1206 | ++curIndex; | |
1207 | break; | |
1208 | ||
1209 | case tNotEqual: // scanned '!' | |
1210 | if (ruleSrc->charAt(curIndex+1) == EQUALS) { | |
1211 | curIndex += 2; | |
1212 | } else { | |
1213 | type = none; | |
1214 | curIndex += 1; | |
1215 | } | |
1216 | break; | |
1217 | ||
1218 | case tKeyword: | |
1219 | while (type == tKeyword && ++curIndex < ruleSrc->length()) { | |
1220 | ch = ruleSrc->charAt(curIndex); | |
1221 | type = charType(ch); | |
1222 | } | |
1223 | type = tKeyword; | |
1224 | break; | |
1225 | ||
1226 | case tNumber: | |
1227 | while (type == tNumber && ++curIndex < ruleSrc->length()) { | |
1228 | ch = ruleSrc->charAt(curIndex); | |
1229 | type = charType(ch); | |
1230 | } | |
1231 | type = tNumber; | |
1232 | break; | |
1233 | ||
1234 | case tDot: | |
1235 | // We could be looking at either ".." in a range, or "..." at the end of a sample. | |
1236 | if (curIndex+1 >= ruleSrc->length() || ruleSrc->charAt(curIndex+1) != DOT) { | |
1237 | ++curIndex; | |
1238 | break; // Single dot | |
1239 | } | |
1240 | if (curIndex+2 >= ruleSrc->length() || ruleSrc->charAt(curIndex+2) != DOT) { | |
1241 | curIndex += 2; | |
1242 | type = tDot2; | |
1243 | break; // double dot | |
1244 | } | |
1245 | type = tEllipsis; | |
1246 | curIndex += 3; | |
1247 | break; // triple dot | |
1248 | ||
1249 | default: | |
1250 | status = U_UNEXPECTED_TOKEN; | |
1251 | ++curIndex; | |
1252 | break; | |
1253 | } | |
1254 | ||
1255 | U_ASSERT(ruleIndex <= ruleSrc->length()); | |
1256 | U_ASSERT(curIndex <= ruleSrc->length()); | |
1257 | token=UnicodeString(*ruleSrc, ruleIndex, curIndex-ruleIndex); | |
1258 | ruleIndex = curIndex; | |
46f4442e A |
1259 | } |
1260 | ||
57a6839d A |
1261 | tokenType |
1262 | PluralRuleParser::charType(UChar ch) { | |
46f4442e | 1263 | if ((ch>=U_ZERO) && (ch<=U_NINE)) { |
57a6839d A |
1264 | return tNumber; |
1265 | } | |
1266 | if (ch>=LOW_A && ch<=LOW_Z) { | |
1267 | return tKeyword; | |
46f4442e A |
1268 | } |
1269 | switch (ch) { | |
1270 | case COLON: | |
57a6839d | 1271 | return tColon; |
46f4442e | 1272 | case SPACE: |
57a6839d | 1273 | return tSpace; |
46f4442e | 1274 | case SEMI_COLON: |
57a6839d | 1275 | return tSemiColon; |
46f4442e | 1276 | case DOT: |
57a6839d A |
1277 | return tDot; |
1278 | case COMMA: | |
1279 | return tComma; | |
1280 | case EXCLAMATION: | |
1281 | return tNotEqual; | |
1282 | case EQUALS: | |
1283 | return tEqual; | |
1284 | case PERCENT_SIGN: | |
1285 | return tMod; | |
1286 | case AT: | |
1287 | return tAt; | |
1288 | case ELLIPSIS: | |
1289 | return tEllipsis; | |
1290 | case TILDE: | |
1291 | return tTilde; | |
46f4442e | 1292 | default : |
57a6839d | 1293 | return none; |
46f4442e A |
1294 | } |
1295 | } | |
1296 | ||
1297 | ||
57a6839d A |
1298 | // Set token type for reserved words in the Plural Rule syntax. |
1299 | ||
1300 | tokenType | |
1301 | PluralRuleParser::getKeyType(const UnicodeString &token, tokenType keyType) | |
46f4442e | 1302 | { |
57a6839d A |
1303 | if (keyType != tKeyword) { |
1304 | return keyType; | |
729e4ab9 | 1305 | } |
57a6839d A |
1306 | |
1307 | if (0 == token.compare(PK_VAR_N, 1)) { | |
46f4442e | 1308 | keyType = tVariableN; |
57a6839d A |
1309 | } else if (0 == token.compare(PK_VAR_I, 1)) { |
1310 | keyType = tVariableI; | |
1311 | } else if (0 == token.compare(PK_VAR_F, 1)) { | |
1312 | keyType = tVariableF; | |
1313 | } else if (0 == token.compare(PK_VAR_T, 1)) { | |
1314 | keyType = tVariableT; | |
1315 | } else if (0 == token.compare(PK_VAR_V, 1)) { | |
1316 | keyType = tVariableV; | |
1317 | } else if (0 == token.compare(PK_IS, 2)) { | |
46f4442e | 1318 | keyType = tIs; |
57a6839d | 1319 | } else if (0 == token.compare(PK_AND, 3)) { |
46f4442e | 1320 | keyType = tAnd; |
57a6839d | 1321 | } else if (0 == token.compare(PK_IN, 2)) { |
46f4442e | 1322 | keyType = tIn; |
57a6839d | 1323 | } else if (0 == token.compare(PK_WITHIN, 6)) { |
46f4442e | 1324 | keyType = tWithin; |
57a6839d | 1325 | } else if (0 == token.compare(PK_NOT, 3)) { |
46f4442e | 1326 | keyType = tNot; |
57a6839d | 1327 | } else if (0 == token.compare(PK_MOD, 3)) { |
46f4442e | 1328 | keyType = tMod; |
57a6839d | 1329 | } else if (0 == token.compare(PK_OR, 2)) { |
46f4442e | 1330 | keyType = tOr; |
57a6839d A |
1331 | } else if (0 == token.compare(PK_DECIMAL, 7)) { |
1332 | keyType = tDecimal; | |
1333 | } else if (0 == token.compare(PK_INTEGER, 7)) { | |
1334 | keyType = tInteger; | |
46f4442e | 1335 | } |
57a6839d | 1336 | return keyType; |
46f4442e A |
1337 | } |
1338 | ||
46f4442e | 1339 | |
4388f060 A |
1340 | PluralKeywordEnumeration::PluralKeywordEnumeration(RuleChain *header, UErrorCode& status) |
1341 | : pos(0), fKeywordNames(status) { | |
729e4ab9 A |
1342 | if (U_FAILURE(status)) { |
1343 | return; | |
1344 | } | |
4388f060 A |
1345 | fKeywordNames.setDeleter(uprv_deleteUObject); |
1346 | UBool addKeywordOther=TRUE; | |
1347 | RuleChain *node=header; | |
46f4442e | 1348 | while(node!=NULL) { |
57a6839d | 1349 | fKeywordNames.addElement(new UnicodeString(node->fKeyword), status); |
729e4ab9 A |
1350 | if (U_FAILURE(status)) { |
1351 | return; | |
1352 | } | |
57a6839d | 1353 | if (0 == node->fKeyword.compare(PLURAL_KEYWORD_OTHER, 5)) { |
4388f060 | 1354 | addKeywordOther= FALSE; |
46f4442e | 1355 | } |
57a6839d | 1356 | node=node->fNext; |
46f4442e | 1357 | } |
4388f060 | 1358 | |
46f4442e A |
1359 | if (addKeywordOther) { |
1360 | fKeywordNames.addElement(new UnicodeString(PLURAL_KEYWORD_OTHER), status); | |
1361 | } | |
1362 | } | |
1363 | ||
1364 | const UnicodeString* | |
1365 | PluralKeywordEnumeration::snext(UErrorCode& status) { | |
1366 | if (U_SUCCESS(status) && pos < fKeywordNames.size()) { | |
1367 | return (const UnicodeString*)fKeywordNames.elementAt(pos++); | |
1368 | } | |
1369 | return NULL; | |
1370 | } | |
1371 | ||
1372 | void | |
1373 | PluralKeywordEnumeration::reset(UErrorCode& /*status*/) { | |
1374 | pos=0; | |
1375 | } | |
1376 | ||
1377 | int32_t | |
1378 | PluralKeywordEnumeration::count(UErrorCode& /*status*/) const { | |
1379 | return fKeywordNames.size(); | |
1380 | } | |
1381 | ||
1382 | PluralKeywordEnumeration::~PluralKeywordEnumeration() { | |
46f4442e A |
1383 | } |
1384 | ||
2ca993e8 A |
1385 | FixedDecimal::FixedDecimal(const VisibleDigits &digits) { |
1386 | digits.getFixedDecimal( | |
1387 | source, intValue, decimalDigits, | |
1388 | decimalDigitsWithoutTrailingZeros, | |
1389 | visibleDecimalDigitCount, hasIntegerValue); | |
1390 | isNegative = digits.isNegative(); | |
1391 | isNanOrInfinity = digits.isNaNOrInfinity(); | |
1392 | } | |
57a6839d A |
1393 | |
1394 | FixedDecimal::FixedDecimal(double n, int32_t v, int64_t f) { | |
1395 | init(n, v, f); | |
1396 | // check values. TODO make into unit test. | |
1397 | // | |
1398 | // long visiblePower = (int) Math.pow(10, v); | |
1399 | // if (decimalDigits > visiblePower) { | |
1400 | // throw new IllegalArgumentException(); | |
1401 | // } | |
1402 | // double fraction = intValue + (decimalDigits / (double) visiblePower); | |
1403 | // if (fraction != source) { | |
1404 | // double diff = Math.abs(fraction - source)/(Math.abs(fraction) + Math.abs(source)); | |
1405 | // if (diff > 0.00000001d) { | |
1406 | // throw new IllegalArgumentException(); | |
1407 | // } | |
1408 | // } | |
1409 | } | |
1410 | ||
1411 | FixedDecimal::FixedDecimal(double n, int32_t v) { | |
1412 | // Ugly, but for samples we don't care. | |
1413 | init(n, v, getFractionalDigits(n, v)); | |
1414 | } | |
1415 | ||
1416 | FixedDecimal::FixedDecimal(double n) { | |
1417 | init(n); | |
1418 | } | |
1419 | ||
1420 | FixedDecimal::FixedDecimal() { | |
1421 | init(0, 0, 0); | |
1422 | } | |
1423 | ||
1424 | ||
1425 | // Create a FixedDecimal from a UnicodeString containing a number. | |
1426 | // Inefficient, but only used for samples, so simplicity trumps efficiency. | |
1427 | ||
1428 | FixedDecimal::FixedDecimal(const UnicodeString &num, UErrorCode &status) { | |
1429 | CharString cs; | |
1430 | cs.appendInvariantChars(num, status); | |
1431 | DigitList dl; | |
1432 | dl.set(cs.toStringPiece(), status); | |
1433 | if (U_FAILURE(status)) { | |
1434 | init(0, 0, 0); | |
1435 | return; | |
1436 | } | |
1437 | int32_t decimalPoint = num.indexOf(DOT); | |
1438 | double n = dl.getDouble(); | |
1439 | if (decimalPoint == -1) { | |
1440 | init(n, 0, 0); | |
1441 | } else { | |
1442 | int32_t v = num.length() - decimalPoint - 1; | |
1443 | init(n, v, getFractionalDigits(n, v)); | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | ||
1448 | FixedDecimal::FixedDecimal(const FixedDecimal &other) { | |
1449 | source = other.source; | |
1450 | visibleDecimalDigitCount = other.visibleDecimalDigitCount; | |
1451 | decimalDigits = other.decimalDigits; | |
1452 | decimalDigitsWithoutTrailingZeros = other.decimalDigitsWithoutTrailingZeros; | |
1453 | intValue = other.intValue; | |
1454 | hasIntegerValue = other.hasIntegerValue; | |
1455 | isNegative = other.isNegative; | |
1456 | isNanOrInfinity = other.isNanOrInfinity; | |
1457 | } | |
1458 | ||
1459 | ||
1460 | void FixedDecimal::init(double n) { | |
1461 | int32_t numFractionDigits = decimals(n); | |
1462 | init(n, numFractionDigits, getFractionalDigits(n, numFractionDigits)); | |
1463 | } | |
1464 | ||
1465 | ||
1466 | void FixedDecimal::init(double n, int32_t v, int64_t f) { | |
1467 | isNegative = n < 0.0; | |
1468 | source = fabs(n); | |
1469 | isNanOrInfinity = uprv_isNaN(source) || uprv_isPositiveInfinity(source); | |
1470 | if (isNanOrInfinity) { | |
1471 | v = 0; | |
1472 | f = 0; | |
1473 | intValue = 0; | |
1474 | hasIntegerValue = FALSE; | |
1475 | } else { | |
1476 | intValue = (int64_t)source; | |
1477 | hasIntegerValue = (source == intValue); | |
1478 | } | |
1479 | ||
1480 | visibleDecimalDigitCount = v; | |
1481 | decimalDigits = f; | |
1482 | if (f == 0) { | |
1483 | decimalDigitsWithoutTrailingZeros = 0; | |
1484 | } else { | |
1485 | int64_t fdwtz = f; | |
1486 | while ((fdwtz%10) == 0) { | |
1487 | fdwtz /= 10; | |
1488 | } | |
1489 | decimalDigitsWithoutTrailingZeros = fdwtz; | |
1490 | } | |
1491 | } | |
1492 | ||
1493 | ||
1494 | // Fast path only exact initialization. Return true if successful. | |
1495 | // Note: Do not multiply by 10 each time through loop, rounding cruft can build | |
1496 | // up that makes the check for an integer result fail. | |
1497 | // A single multiply of the original number works more reliably. | |
1498 | static int32_t p10[] = {1, 10, 100, 1000, 10000}; | |
1499 | UBool FixedDecimal::quickInit(double n) { | |
1500 | UBool success = FALSE; | |
1501 | n = fabs(n); | |
1502 | int32_t numFractionDigits; | |
1503 | for (numFractionDigits = 0; numFractionDigits <= 3; numFractionDigits++) { | |
1504 | double scaledN = n * p10[numFractionDigits]; | |
1505 | if (scaledN == floor(scaledN)) { | |
1506 | success = TRUE; | |
1507 | break; | |
1508 | } | |
1509 | } | |
1510 | if (success) { | |
1511 | init(n, numFractionDigits, getFractionalDigits(n, numFractionDigits)); | |
1512 | } | |
1513 | return success; | |
1514 | } | |
1515 | ||
1516 | ||
1517 | ||
1518 | int32_t FixedDecimal::decimals(double n) { | |
1519 | // Count the number of decimal digits in the fraction part of the number, excluding trailing zeros. | |
1520 | // fastpath the common cases, integers or fractions with 3 or fewer digits | |
1521 | n = fabs(n); | |
1522 | for (int ndigits=0; ndigits<=3; ndigits++) { | |
1523 | double scaledN = n * p10[ndigits]; | |
1524 | if (scaledN == floor(scaledN)) { | |
1525 | return ndigits; | |
1526 | } | |
1527 | } | |
1528 | ||
1529 | // Slow path, convert with sprintf, parse converted output. | |
1530 | char buf[30] = {0}; | |
1531 | sprintf(buf, "%1.15e", n); | |
1532 | // formatted number looks like this: 1.234567890123457e-01 | |
1533 | int exponent = atoi(buf+18); | |
1534 | int numFractionDigits = 15; | |
1535 | for (int i=16; ; --i) { | |
1536 | if (buf[i] != '0') { | |
1537 | break; | |
1538 | } | |
1539 | --numFractionDigits; | |
1540 | } | |
1541 | numFractionDigits -= exponent; // Fraction part of fixed point representation. | |
1542 | return numFractionDigits; | |
1543 | } | |
1544 | ||
1545 | ||
1546 | // Get the fraction digits of a double, represented as an integer. | |
1547 | // v is the number of visible fraction digits in the displayed form of the number. | |
1548 | // Example: n = 1001.234, v = 6, result = 234000 | |
1549 | // TODO: need to think through how this is used in the plural rule context. | |
1550 | // This function can easily encounter integer overflow, | |
1551 | // and can easily return noise digits when the precision of a double is exceeded. | |
1552 | ||
1553 | int64_t FixedDecimal::getFractionalDigits(double n, int32_t v) { | |
1554 | if (v == 0 || n == floor(n) || uprv_isNaN(n) || uprv_isPositiveInfinity(n)) { | |
1555 | return 0; | |
1556 | } | |
1557 | n = fabs(n); | |
1558 | double fract = n - floor(n); | |
1559 | switch (v) { | |
1560 | case 1: return (int64_t)(fract*10.0 + 0.5); | |
1561 | case 2: return (int64_t)(fract*100.0 + 0.5); | |
1562 | case 3: return (int64_t)(fract*1000.0 + 0.5); | |
1563 | default: | |
1564 | double scaled = floor(fract * pow(10.0, (double)v) + 0.5); | |
1565 | if (scaled > U_INT64_MAX) { | |
1566 | return U_INT64_MAX; | |
1567 | } else { | |
1568 | return (int64_t)scaled; | |
1569 | } | |
1570 | } | |
1571 | } | |
1572 | ||
1573 | ||
1574 | void FixedDecimal::adjustForMinFractionDigits(int32_t minFractionDigits) { | |
1575 | int32_t numTrailingFractionZeros = minFractionDigits - visibleDecimalDigitCount; | |
1576 | if (numTrailingFractionZeros > 0) { | |
1577 | for (int32_t i=0; i<numTrailingFractionZeros; i++) { | |
1578 | // Do not let the decimalDigits value overflow if there are many trailing zeros. | |
1579 | // Limit the value to 18 digits, the most that a 64 bit int can fully represent. | |
1580 | if (decimalDigits >= 100000000000000000LL) { | |
1581 | break; | |
1582 | } | |
1583 | decimalDigits *= 10; | |
1584 | } | |
1585 | visibleDecimalDigitCount += numTrailingFractionZeros; | |
1586 | } | |
1587 | } | |
1588 | ||
1589 | ||
1590 | double FixedDecimal::get(tokenType operand) const { | |
1591 | switch(operand) { | |
1592 | case tVariableN: return source; | |
1593 | case tVariableI: return (double)intValue; | |
1594 | case tVariableF: return (double)decimalDigits; | |
1595 | case tVariableT: return (double)decimalDigitsWithoutTrailingZeros; | |
1596 | case tVariableV: return visibleDecimalDigitCount; | |
1597 | default: | |
1598 | U_ASSERT(FALSE); // unexpected. | |
1599 | return source; | |
1600 | } | |
1601 | } | |
1602 | ||
1603 | int32_t FixedDecimal::getVisibleFractionDigitCount() const { | |
1604 | return visibleDecimalDigitCount; | |
1605 | } | |
1606 | ||
1607 | ||
1608 | ||
1609 | PluralAvailableLocalesEnumeration::PluralAvailableLocalesEnumeration(UErrorCode &status) { | |
1610 | fLocales = NULL; | |
1611 | fRes = NULL; | |
1612 | fOpenStatus = status; | |
1613 | if (U_FAILURE(status)) { | |
1614 | return; | |
1615 | } | |
1616 | fOpenStatus = U_ZERO_ERROR; | |
1617 | LocalUResourceBundlePointer rb(ures_openDirect(NULL, "plurals", &fOpenStatus)); | |
1618 | fLocales = ures_getByKey(rb.getAlias(), "locales", NULL, &fOpenStatus); | |
1619 | } | |
1620 | ||
1621 | PluralAvailableLocalesEnumeration::~PluralAvailableLocalesEnumeration() { | |
1622 | ures_close(fLocales); | |
1623 | ures_close(fRes); | |
1624 | fLocales = NULL; | |
1625 | fRes = NULL; | |
1626 | } | |
1627 | ||
1628 | const char *PluralAvailableLocalesEnumeration::next(int32_t *resultLength, UErrorCode &status) { | |
1629 | if (U_FAILURE(status)) { | |
1630 | return NULL; | |
1631 | } | |
1632 | if (U_FAILURE(fOpenStatus)) { | |
1633 | status = fOpenStatus; | |
1634 | return NULL; | |
1635 | } | |
1636 | fRes = ures_getNextResource(fLocales, fRes, &status); | |
1637 | if (fRes == NULL || U_FAILURE(status)) { | |
1638 | if (status == U_INDEX_OUTOFBOUNDS_ERROR) { | |
1639 | status = U_ZERO_ERROR; | |
1640 | } | |
1641 | return NULL; | |
1642 | } | |
1643 | const char *result = ures_getKey(fRes); | |
1644 | if (resultLength != NULL) { | |
1645 | *resultLength = uprv_strlen(result); | |
1646 | } | |
1647 | return result; | |
1648 | } | |
1649 | ||
1650 | ||
1651 | void PluralAvailableLocalesEnumeration::reset(UErrorCode &status) { | |
1652 | if (U_FAILURE(status)) { | |
1653 | return; | |
1654 | } | |
1655 | if (U_FAILURE(fOpenStatus)) { | |
1656 | status = fOpenStatus; | |
1657 | return; | |
1658 | } | |
1659 | ures_resetIterator(fLocales); | |
1660 | } | |
1661 | ||
1662 | int32_t PluralAvailableLocalesEnumeration::count(UErrorCode &status) const { | |
1663 | if (U_FAILURE(status)) { | |
1664 | return 0; | |
1665 | } | |
1666 | if (U_FAILURE(fOpenStatus)) { | |
1667 | status = fOpenStatus; | |
1668 | return 0; | |
1669 | } | |
1670 | return ures_getSize(fLocales); | |
1671 | } | |
1672 | ||
46f4442e A |
1673 | U_NAMESPACE_END |
1674 | ||
1675 | ||
1676 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
1677 | ||
1678 | //eof |