]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/plurrule.cpp
ICU-57166.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / plurrule.cpp
1 /*
2 *******************************************************************************
3 * Copyright (C) 2007-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
6 *
7 * File plurrule.cpp
8 */
9
10 #include <math.h>
11 #include <stdio.h>
12
13 #include "unicode/utypes.h"
14 #include "unicode/localpointer.h"
15 #include "unicode/plurrule.h"
16 #include "unicode/upluralrules.h"
17 #include "unicode/ures.h"
18 #include "charstr.h"
19 #include "cmemory.h"
20 #include "cstring.h"
21 #include "digitlst.h"
22 #include "hash.h"
23 #include "locutil.h"
24 #include "mutex.h"
25 #include "patternprops.h"
26 #include "plurrule_impl.h"
27 #include "putilimp.h"
28 #include "ucln_in.h"
29 #include "ustrfmt.h"
30 #include "uassert.h"
31 #include "uvectr32.h"
32 #include "sharedpluralrules.h"
33 #include "unifiedcache.h"
34 #include "digitinterval.h"
35 #include "visibledigits.h"
36
37
38 #if !UCONFIG_NO_FORMATTING
39
40 U_NAMESPACE_BEGIN
41
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};
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};
55 static const UChar PK_WITHIN[]={LOW_W,LOW_I,LOW_T,LOW_H,LOW_I,LOW_N,0};
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};
58
59 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(PluralRules)
60 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(PluralKeywordEnumeration)
61
62 PluralRules::PluralRules(UErrorCode& /*status*/)
63 : UObject(),
64 mRules(NULL)
65 {
66 }
67
68 PluralRules::PluralRules(const PluralRules& other)
69 : UObject(other),
70 mRules(NULL)
71 {
72 *this=other;
73 }
74
75 PluralRules::~PluralRules() {
76 delete mRules;
77 }
78
79 SharedPluralRules::~SharedPluralRules() {
80 delete ptr;
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 }
98 }
99
100 return *this;
101 }
102
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
116 PluralRules* U_EXPORT2
117 PluralRules::createRules(const UnicodeString& description, UErrorCode& status) {
118 if (U_FAILURE(status)) {
119 return NULL;
120 }
121
122 PluralRuleParser parser;
123 PluralRules *newRules = new PluralRules(status);
124 if (U_SUCCESS(status) && newRules == NULL) {
125 status = U_MEMORY_ALLOCATION_ERROR;
126 }
127 parser.parse(description, newRules, status);
128 if (U_FAILURE(status)) {
129 delete newRules;
130 newRules = NULL;
131 }
132 return newRules;
133 }
134
135
136 PluralRules* U_EXPORT2
137 PluralRules::createDefaultRules(UErrorCode& status) {
138 return createRules(UnicodeString(TRUE, PLURAL_DEFAULT_RULE, -1), status);
139 }
140
141 /******************************************************************************/
142 /* Create PluralRules cache */
143
144 template<> U_I18N_API
145 const SharedPluralRules *LocaleCacheKey<SharedPluralRules>::createObject(
146 const void * /*unused*/, UErrorCode &status) const {
147 const char *localeId = fLoc.getName();
148 PluralRules *pr = PluralRules::internalForLocale(
149 localeId, UPLURAL_TYPE_CARDINAL, status);
150 if (U_FAILURE(status)) {
151 return NULL;
152 }
153 SharedPluralRules *result = new SharedPluralRules(pr);
154 if (result == NULL) {
155 status = U_MEMORY_ALLOCATION_ERROR;
156 delete pr;
157 return NULL;
158 }
159 result->addRef();
160 return result;
161 }
162
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;
177 UnifiedCache::getByLocale(locale, result, status);
178 return result;
179 }
180
181 PluralRules* U_EXPORT2
182 PluralRules::forLocale(const Locale& locale, UErrorCode& status) {
183 return forLocale(locale, UPLURAL_TYPE_CARDINAL, status);
184 }
185
186 PluralRules* U_EXPORT2
187 PluralRules::forLocale(const Locale& locale, UPluralType type, UErrorCode& status) {
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) {
206 if (U_FAILURE(status)) {
207 return NULL;
208 }
209 if (type >= UPLURAL_TYPE_COUNT) {
210 status = U_ILLEGAL_ARGUMENT_ERROR;
211 return NULL;
212 }
213 PluralRules *newObj = new PluralRules(status);
214 if (newObj==NULL || U_FAILURE(status)) {
215 delete newObj;
216 return NULL;
217 }
218 UnicodeString locRule = newObj->getRuleFromResource(locale, type, status);
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);
225 status = U_ZERO_ERROR;
226 }
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.
233
234 return newObj;
235 }
236
237 UnicodeString
238 PluralRules::select(int32_t number) const {
239 return select(FixedDecimal(number));
240 }
241
242 UnicodeString
243 PluralRules::select(double number) const {
244 return select(FixedDecimal(number));
245 }
246
247 UnicodeString
248 PluralRules::select(const FixedDecimal &number) const {
249 if (mRules == NULL) {
250 return UnicodeString(TRUE, PLURAL_DEFAULT_RULE, -1);
251 }
252 else {
253 return mRules->select(number);
254 }
255 }
256
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
267 StringEnumeration*
268 PluralRules::getKeywords(UErrorCode& status) const {
269 if (U_FAILURE(status)) return NULL;
270 StringEnumeration* nameEnumerator = new PluralKeywordEnumeration(mRules, status);
271 if (U_FAILURE(status)) {
272 delete nameEnumerator;
273 return NULL;
274 }
275
276 return nameEnumerator;
277 }
278
279 double
280 PluralRules::getUniqueKeywordValue(const UnicodeString& /* keyword */) {
281 // Not Implemented.
282 return UPLRULES_NO_UNIQUE_VALUE;
283 }
284
285 int32_t
286 PluralRules::getAllKeywordValues(const UnicodeString & /* keyword */, double * /* dest */,
287 int32_t /* destCapacity */, UErrorCode& error) {
288 error = U_UNSUPPORTED_ERROR;
289 return 0;
290 }
291
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;
298 }
299 return scale;
300 }
301
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 }
339
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.
342
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 }
364 }
365 sampleStartIdx = sampleEndIdx + 1;
366 }
367 return sampleCount;
368 }
369
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;
377 }
378 int32_t numSamples = getSamplesFromString(rc->fIntegerSamples, dest, destCapacity, status);
379 if (numSamples == 0) {
380 numSamples = getSamplesFromString(rc->fDecimalSamples, dest, destCapacity, status);
381 }
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;
394 }
395
396
397 UBool
398 PluralRules::isKeyword(const UnicodeString& keyword) const {
399 if (0 == keyword.compare(PLURAL_KEYWORD_OTHER, 5)) {
400 return true;
401 }
402 return rulesForKeyword(keyword) != NULL;
403 }
404
405 UnicodeString
406 PluralRules::getKeywordOther() const {
407 return UnicodeString(TRUE, PLURAL_KEYWORD_OTHER, 5);
408 }
409
410 UBool
411 PluralRules::operator==(const PluralRules& other) const {
412 const UnicodeString *ptrKeyword;
413 UErrorCode status= U_ZERO_ERROR;
414
415 if ( this == &other ) {
416 return TRUE;
417 }
418 LocalPointer<StringEnumeration> myKeywordList(getKeywords(status));
419 LocalPointer<StringEnumeration> otherKeywordList(other.getKeywords(status));
420 if (U_FAILURE(status)) {
421 return FALSE;
422 }
423
424 if (myKeywordList->count(status)!=otherKeywordList->count(status)) {
425 return FALSE;
426 }
427 myKeywordList->reset(status);
428 while ((ptrKeyword=myKeywordList->snext(status))!=NULL) {
429 if (!other.isKeyword(*ptrKeyword)) {
430 return FALSE;
431 }
432 }
433 otherKeywordList->reset(status);
434 while ((ptrKeyword=otherKeywordList->snext(status))!=NULL) {
435 if (!this->isKeyword(*ptrKeyword)) {
436 return FALSE;
437 }
438 }
439 if (U_FAILURE(status)) {
440 return FALSE;
441 }
442
443 return TRUE;
444 }
445
446
447 void
448 PluralRuleParser::parse(const UnicodeString& ruleData, PluralRules *prules, UErrorCode &status)
449 {
450 if (U_FAILURE(status)) {
451 return;
452 }
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);
458 if (U_FAILURE(status)) {
459 return;
460 }
461 checkSyntax(status);
462 if (U_FAILURE(status)) {
463 return;
464 }
465 switch (type) {
466 case tAnd:
467 U_ASSERT(curAndConstraint != NULL);
468 curAndConstraint = curAndConstraint->add();
469 break;
470 case tOr:
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();
481 }
482 break;
483 case tIs:
484 U_ASSERT(curAndConstraint != NULL);
485 U_ASSERT(curAndConstraint->value == -1);
486 U_ASSERT(curAndConstraint->rangeList == NULL);
487 break;
488 case tNot:
489 U_ASSERT(curAndConstraint != NULL);
490 curAndConstraint->negated=TRUE;
491 break;
492
493 case tNotEqual:
494 curAndConstraint->negated=TRUE;
495 U_FALLTHROUGH;
496 case tIn:
497 case tWithin:
498 case tEqual:
499 U_ASSERT(curAndConstraint != NULL);
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);
507 break;
508 case tNumber:
509 U_ASSERT(curAndConstraint != NULL);
510 if ( (curAndConstraint->op==AndConstraint::MOD)&&
511 (curAndConstraint->opNum == -1 ) ) {
512 curAndConstraint->opNum=getNumberValue(token);
513 }
514 else {
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 }
535 }
536 }
537 break;
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;
551 case tMod:
552 U_ASSERT(curAndConstraint != NULL);
553 curAndConstraint->op=AndConstraint::MOD;
554 break;
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;
563 case tKeyword:
564 {
565 RuleChain *newChain = new RuleChain;
566 if (newChain == NULL) {
567 status = U_MEMORY_ALLOCATION_ERROR;
568 break;
569 }
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;
580 }
581 newChain->fNext = insertAfter->fNext;
582 insertAfter->fNext = newChain;
583 }
584 OrConstraint *orNode = new OrConstraint();
585 newChain->ruleHeader = orNode;
586 curAndConstraint = orNode->add();
587 currentChain = newChain;
588 }
589 break;
590
591 case tInteger:
592 for (;;) {
593 getNextToken(status);
594 if (U_FAILURE(status) || type == tSemiColon || type == tEOF || type == tAt) {
595 break;
596 }
597 if (type == tEllipsis) {
598 currentChain->fIntegerSamplesUnbounded = TRUE;
599 continue;
600 }
601 currentChain->fIntegerSamples.append(token);
602 }
603 break;
604
605 case tDecimal:
606 for (;;) {
607 getNextToken(status);
608 if (U_FAILURE(status) || type == tSemiColon || type == tEOF || type == tAt) {
609 break;
610 }
611 if (type == tEllipsis) {
612 currentChain->fDecimalSamplesUnbounded = TRUE;
613 continue;
614 }
615 currentChain->fDecimalSamples.append(token);
616 }
617 break;
618
619 default:
620 break;
621 }
622 prevType=type;
623 if (U_FAILURE(status)) {
624 break;
625 }
626 }
627 }
628
629 UnicodeString
630 PluralRules::getRuleFromResource(const Locale& locale, UPluralType type, UErrorCode& errCode) {
631 UnicodeString emptyStr;
632
633 if (U_FAILURE(errCode)) {
634 return emptyStr;
635 }
636 LocalUResourceBundlePointer rb(ures_openDirect(NULL, "plurals", &errCode));
637 if(U_FAILURE(errCode)) {
638 return emptyStr;
639 }
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));
654 if(U_FAILURE(errCode)) {
655 return emptyStr;
656 }
657 int32_t resLen=0;
658 const char *curLocaleName=locale.getName();
659 const UChar* s = ures_getStringByKey(locRes.getAlias(), curLocaleName, &resLen, &errCode);
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();
666 uprv_strcpy(parentLocaleName, curLocaleName);
667
668 while (uloc_getParent(parentLocaleName, parentLocaleName,
669 ULOC_FULLNAME_CAPACITY, &status) > 0) {
670 resLen=0;
671 s = ures_getStringByKey(locRes.getAlias(), parentLocaleName, &resLen, &status);
672 if (s != NULL) {
673 errCode = U_ZERO_ERROR;
674 break;
675 }
676 status = U_ZERO_ERROR;
677 }
678 }
679 if (s==NULL) {
680 return emptyStr;
681 }
682
683 char setKey[256];
684 u_UCharsToChars(s, setKey, resLen + 1);
685 // printf("\n PluralRule: %s\n", setKey);
686
687 LocalUResourceBundlePointer ruleRes(ures_getByKey(rb.getAlias(), "rules", NULL, &errCode));
688 if(U_FAILURE(errCode)) {
689 return emptyStr;
690 }
691 LocalUResourceBundlePointer setRes(ures_getByKey(ruleRes.getAlias(), setKey, NULL, &errCode));
692 if (U_FAILURE(errCode)) {
693 return emptyStr;
694 }
695
696 int32_t numberKeys = ures_getSize(setRes.getAlias());
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;
718 }
719
720
721 AndConstraint::AndConstraint() {
722 op = AndConstraint::NONE;
723 opNum=-1;
724 value = -1;
725 rangeList = NULL;
726 negated = FALSE;
727 integerOnly = FALSE;
728 digitsType = none;
729 next=NULL;
730 }
731
732
733 AndConstraint::AndConstraint(const AndConstraint& other) {
734 this->op = other.op;
735 this->opNum=other.opNum;
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 }
743 this->integerOnly=other.integerOnly;
744 this->negated=other.negated;
745 this->digitsType = other.digitsType;
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() {
755 delete rangeList;
756 if (next!=NULL) {
757 delete next;
758 }
759 }
760
761
762 UBool
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;
768 }
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;
785 }
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 }
792 }
793 } while (FALSE);
794
795 if (negated) {
796 result = !result;
797 }
798 return result;
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 }
846 U_ASSERT(curOrConstraint->childNode == NULL);
847 curOrConstraint->childNode = new AndConstraint();
848 }
849 return curOrConstraint->childNode;
850 }
851
852 UBool
853 OrConstraint::isFulfilled(const FixedDecimal &number) {
854 OrConstraint* orRule=this;
855 UBool result=FALSE;
856
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 }
866
867 return result;
868 }
869
870
871 RuleChain::RuleChain(): fKeyword(), fNext(NULL), ruleHeader(NULL), fDecimalSamples(), fIntegerSamples(),
872 fDecimalSamplesUnbounded(FALSE), fIntegerSamplesUnbounded(FALSE) {
873 }
874
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) {
879 if (other.ruleHeader != NULL) {
880 this->ruleHeader = new OrConstraint(*(other.ruleHeader));
881 }
882 if (other.fNext != NULL ) {
883 this->fNext = new RuleChain(*other.fNext);
884 }
885 }
886
887 RuleChain::~RuleChain() {
888 delete fNext;
889 delete ruleHeader;
890 }
891
892
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 }
904
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;
922 }
923
924 void
925 RuleChain::dumpRules(UnicodeString& result) {
926 UChar digitString[16];
927
928 if ( ruleHeader != NULL ) {
929 result += fKeyword;
930 result += COLON;
931 result += SPACE;
932 OrConstraint* orRule=ruleHeader;
933 while ( orRule != NULL ) {
934 AndConstraint* andRule=orRule->childNode;
935 while ( andRule != NULL ) {
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) {
942 result += UNICODE_STRING_SIMPLE("not ");
943 }
944 uprv_itou(digitString,16, andRule->value,10,0);
945 result += UnicodeString(digitString);
946 }
947 else {
948 result += tokenString(andRule->digitsType);
949 result += SPACE;
950 if (andRule->op==AndConstraint::MOD) {
951 result += UNICODE_STRING_SIMPLE("mod ");
952 uprv_itou(digitString,16, andRule->opNum,10,0);
953 result += UnicodeString(digitString);
954 }
955 if (andRule->rangeList==NULL) {
956 if (andRule->negated) {
957 result += UNICODE_STRING_SIMPLE(" is not ");
958 uprv_itou(digitString,16, andRule->value,10,0);
959 result += UnicodeString(digitString);
960 }
961 else {
962 result += UNICODE_STRING_SIMPLE(" is ");
963 uprv_itou(digitString,16, andRule->value,10,0);
964 result += UnicodeString(digitString);
965 }
966 }
967 else {
968 if (andRule->negated) {
969 if ( andRule->integerOnly ) {
970 result += UNICODE_STRING_SIMPLE(" not in ");
971 }
972 else {
973 result += UNICODE_STRING_SIMPLE(" not within ");
974 }
975 }
976 else {
977 if ( andRule->integerOnly ) {
978 result += UNICODE_STRING_SIMPLE(" in ");
979 }
980 else {
981 result += UNICODE_STRING_SIMPLE(" within ");
982 }
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);
988 result += UnicodeString(digitString);
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 }
995 }
996 }
997 }
998 if ( (andRule=andRule->next) != NULL) {
999 result += UNICODE_STRING_SIMPLE(" and ");
1000 }
1001 }
1002 if ( (orRule = orRule->next) != NULL ) {
1003 result += UNICODE_STRING_SIMPLE(" or ");
1004 }
1005 }
1006 }
1007 if ( fNext != NULL ) {
1008 result += UNICODE_STRING_SIMPLE("; ");
1009 fNext->dumpRules(result);
1010 }
1011 }
1012
1013
1014 UErrorCode
1015 RuleChain::getKeywords(int32_t capacityOfKeywords, UnicodeString* keywords, int32_t& arraySize) const {
1016 if ( arraySize < capacityOfKeywords-1 ) {
1017 keywords[arraySize++]=fKeyword;
1018 }
1019 else {
1020 return U_BUFFER_OVERFLOW_ERROR;
1021 }
1022
1023 if ( fNext != NULL ) {
1024 return fNext->getKeywords(capacityOfKeywords, keywords, arraySize);
1025 }
1026 else {
1027 return U_ZERO_ERROR;
1028 }
1029 }
1030
1031 UBool
1032 RuleChain::isKeyword(const UnicodeString& keywordParam) const {
1033 if ( fKeyword == keywordParam ) {
1034 return TRUE;
1035 }
1036
1037 if ( fNext != NULL ) {
1038 return fNext->isKeyword(keywordParam);
1039 }
1040 else {
1041 return FALSE;
1042 }
1043 }
1044
1045
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() {
1053 }
1054
1055
1056 int32_t
1057 PluralRuleParser::getNumberValue(const UnicodeString& token) {
1058 int32_t i;
1059 char digits[128];
1060
1061 i = token.extract(0, token.length(), digits, UPRV_LENGTHOF(digits), US_INV);
1062 digits[i]='\0';
1063
1064 return((int32_t)atoi(digits));
1065 }
1066
1067
1068 void
1069 PluralRuleParser::checkSyntax(UErrorCode &status)
1070 {
1071 if (U_FAILURE(status)) {
1072 return;
1073 }
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
1080 switch(prevType) {
1081 case none:
1082 case tSemiColon:
1083 if (type!=tKeyword && type != tEOF) {
1084 status = U_UNEXPECTED_TOKEN;
1085 }
1086 break;
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) {
1094 status = U_UNEXPECTED_TOKEN;
1095 }
1096 break;
1097 case tKeyword:
1098 if (type != tColon) {
1099 status = U_UNEXPECTED_TOKEN;
1100 }
1101 break;
1102 case tColon:
1103 if (!(type == tVariableN ||
1104 type == tVariableI ||
1105 type == tVariableF ||
1106 type == tVariableT ||
1107 type == tVariableV ||
1108 type == tAt)) {
1109 status = U_UNEXPECTED_TOKEN;
1110 }
1111 break;
1112 case tIs:
1113 if ( type != tNumber && type != tNot) {
1114 status = U_UNEXPECTED_TOKEN;
1115 }
1116 break;
1117 case tNot:
1118 if (type != tNumber && type != tIn && type != tWithin) {
1119 status = U_UNEXPECTED_TOKEN;
1120 }
1121 break;
1122 case tMod:
1123 case tDot2:
1124 case tIn:
1125 case tWithin:
1126 case tEqual:
1127 case tNotEqual:
1128 if (type != tNumber) {
1129 status = U_UNEXPECTED_TOKEN;
1130 }
1131 break;
1132 case tAnd:
1133 case tOr:
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) {
1144 status = U_UNEXPECTED_TOKEN;
1145 }
1146 break;
1147 case tNumber:
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)
1152 {
1153 status = U_UNEXPECTED_TOKEN;
1154 }
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 }
1162 break;
1163 default:
1164 status = U_UNEXPECTED_TOKEN;
1165 break;
1166 }
1167 }
1168
1169
1170 /*
1171 * Scan the next token from the input rules.
1172 * rules and returned token type are in the parser state variables.
1173 */
1174 void
1175 PluralRuleParser::getNextToken(UErrorCode &status)
1176 {
1177 if (U_FAILURE(status)) {
1178 return;
1179 }
1180
1181 UChar ch;
1182 while (ruleIndex < ruleSrc->length()) {
1183 ch = ruleSrc->charAt(ruleIndex);
1184 type = charType(ch);
1185 if (type != tSpace) {
1186 break;
1187 }
1188 ++(ruleIndex);
1189 }
1190 if (ruleIndex >= ruleSrc->length()) {
1191 type = tEOF;
1192 return;
1193 }
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;
1259 }
1260
1261 tokenType
1262 PluralRuleParser::charType(UChar ch) {
1263 if ((ch>=U_ZERO) && (ch<=U_NINE)) {
1264 return tNumber;
1265 }
1266 if (ch>=LOW_A && ch<=LOW_Z) {
1267 return tKeyword;
1268 }
1269 switch (ch) {
1270 case COLON:
1271 return tColon;
1272 case SPACE:
1273 return tSpace;
1274 case SEMI_COLON:
1275 return tSemiColon;
1276 case DOT:
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;
1292 default :
1293 return none;
1294 }
1295 }
1296
1297
1298 // Set token type for reserved words in the Plural Rule syntax.
1299
1300 tokenType
1301 PluralRuleParser::getKeyType(const UnicodeString &token, tokenType keyType)
1302 {
1303 if (keyType != tKeyword) {
1304 return keyType;
1305 }
1306
1307 if (0 == token.compare(PK_VAR_N, 1)) {
1308 keyType = tVariableN;
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)) {
1318 keyType = tIs;
1319 } else if (0 == token.compare(PK_AND, 3)) {
1320 keyType = tAnd;
1321 } else if (0 == token.compare(PK_IN, 2)) {
1322 keyType = tIn;
1323 } else if (0 == token.compare(PK_WITHIN, 6)) {
1324 keyType = tWithin;
1325 } else if (0 == token.compare(PK_NOT, 3)) {
1326 keyType = tNot;
1327 } else if (0 == token.compare(PK_MOD, 3)) {
1328 keyType = tMod;
1329 } else if (0 == token.compare(PK_OR, 2)) {
1330 keyType = tOr;
1331 } else if (0 == token.compare(PK_DECIMAL, 7)) {
1332 keyType = tDecimal;
1333 } else if (0 == token.compare(PK_INTEGER, 7)) {
1334 keyType = tInteger;
1335 }
1336 return keyType;
1337 }
1338
1339
1340 PluralKeywordEnumeration::PluralKeywordEnumeration(RuleChain *header, UErrorCode& status)
1341 : pos(0), fKeywordNames(status) {
1342 if (U_FAILURE(status)) {
1343 return;
1344 }
1345 fKeywordNames.setDeleter(uprv_deleteUObject);
1346 UBool addKeywordOther=TRUE;
1347 RuleChain *node=header;
1348 while(node!=NULL) {
1349 fKeywordNames.addElement(new UnicodeString(node->fKeyword), status);
1350 if (U_FAILURE(status)) {
1351 return;
1352 }
1353 if (0 == node->fKeyword.compare(PLURAL_KEYWORD_OTHER, 5)) {
1354 addKeywordOther= FALSE;
1355 }
1356 node=node->fNext;
1357 }
1358
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() {
1383 }
1384
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 }
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
1673 U_NAMESPACE_END
1674
1675
1676 #endif /* #if !UCONFIG_NO_FORMATTING */
1677
1678 //eof