1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
5 * Copyright (C) 1997-2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
8 * file name: nfsubs.cpp
10 * tab size: 8 (not used)
13 * Modification history
15 * 10/11/2001 Doug Ported from ICU4J
19 #include "utypeinfo.h" // for 'typeid' to work
26 static const UChar gLessThan = 0x003c;
27 static const UChar gEquals = 0x003d;
28 static const UChar gGreaterThan = 0x003e;
29 static const UChar gPercent = 0x0025;
30 static const UChar gPound = 0x0023;
31 static const UChar gZero = 0x0030;
32 static const UChar gSpace = 0x0020;
34 static const UChar gEqualsEquals[] =
38 static const UChar gGreaterGreaterGreaterThan[] =
42 static const UChar gGreaterGreaterThan[] =
49 class SameValueSubstitution : public NFSubstitution {
51 SameValueSubstitution(int32_t pos,
52 const NFRuleSet* ruleset,
53 const UnicodeString& description,
55 virtual ~SameValueSubstitution();
57 virtual int64_t transformNumber(int64_t number) const { return number; }
58 virtual double transformNumber(double number) const { return number; }
59 virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const { return newRuleValue; }
60 virtual double calcUpperBound(double oldUpperBound) const { return oldUpperBound; }
61 virtual UChar tokenChar() const { return (UChar)0x003d; } // '='
64 static UClassID getStaticClassID(void);
65 virtual UClassID getDynamicClassID(void) const;
68 SameValueSubstitution::~SameValueSubstitution() {}
70 class MultiplierSubstitution : public NFSubstitution {
75 MultiplierSubstitution(int32_t _pos,
77 const NFRuleSet* _ruleSet,
78 const UnicodeString& description,
80 : NFSubstitution(_pos, _ruleSet, description, status), divisor(_divisor)
82 ldivisor = util64_fromDouble(divisor);
84 status = U_PARSE_ERROR;
87 virtual ~MultiplierSubstitution();
89 virtual void setDivisor(int32_t radix, int32_t exponent, UErrorCode& status) {
90 divisor = uprv_pow(radix, exponent);
91 ldivisor = util64_fromDouble(divisor);
94 status = U_PARSE_ERROR;
98 virtual UBool operator==(const NFSubstitution& rhs) const;
100 virtual int64_t transformNumber(int64_t number) const {
101 return number / ldivisor;
104 virtual double transformNumber(double number) const {
106 return uprv_floor(number / divisor);
108 return number/divisor;
112 virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const {
113 return newRuleValue * divisor;
116 virtual double calcUpperBound(double /*oldUpperBound*/) const { return divisor; }
118 virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
121 static UClassID getStaticClassID(void);
122 virtual UClassID getDynamicClassID(void) const;
125 MultiplierSubstitution::~MultiplierSubstitution() {}
127 class ModulusSubstitution : public NFSubstitution {
130 const NFRule* ruleToUse;
132 ModulusSubstitution(int32_t pos,
134 const NFRule* rulePredecessor,
135 const NFRuleSet* ruleSet,
136 const UnicodeString& description,
138 virtual ~ModulusSubstitution();
140 virtual void setDivisor(int32_t radix, int32_t exponent, UErrorCode& status) {
141 divisor = uprv_pow(radix, exponent);
142 ldivisor = util64_fromDouble(divisor);
145 status = U_PARSE_ERROR;
149 virtual UBool operator==(const NFSubstitution& rhs) const;
151 virtual void doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
152 virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
154 virtual int64_t transformNumber(int64_t number) const { return number % ldivisor; }
155 virtual double transformNumber(double number) const { return uprv_fmod(number, divisor); }
157 virtual UBool doParse(const UnicodeString& text,
158 ParsePosition& parsePosition,
162 Formattable& result) const;
164 virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const {
165 return oldRuleValue - uprv_fmod(oldRuleValue, divisor) + newRuleValue;
168 virtual double calcUpperBound(double /*oldUpperBound*/) const { return divisor; }
170 virtual UBool isModulusSubstitution() const { return TRUE; }
172 virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
174 virtual void toString(UnicodeString& result) const;
177 static UClassID getStaticClassID(void);
178 virtual UClassID getDynamicClassID(void) const;
181 ModulusSubstitution::~ModulusSubstitution() {}
183 class IntegralPartSubstitution : public NFSubstitution {
185 IntegralPartSubstitution(int32_t _pos,
186 const NFRuleSet* _ruleSet,
187 const UnicodeString& description,
189 : NFSubstitution(_pos, _ruleSet, description, status) {}
190 virtual ~IntegralPartSubstitution();
192 virtual int64_t transformNumber(int64_t number) const { return number; }
193 virtual double transformNumber(double number) const { return uprv_floor(number); }
194 virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue + oldRuleValue; }
195 virtual double calcUpperBound(double /*oldUpperBound*/) const { return DBL_MAX; }
196 virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
199 static UClassID getStaticClassID(void);
200 virtual UClassID getDynamicClassID(void) const;
203 IntegralPartSubstitution::~IntegralPartSubstitution() {}
205 class FractionalPartSubstitution : public NFSubstitution {
208 enum { kMaxDecimalDigits = 8 };
210 FractionalPartSubstitution(int32_t pos,
211 const NFRuleSet* ruleSet,
212 const UnicodeString& description,
214 virtual ~FractionalPartSubstitution();
216 virtual UBool operator==(const NFSubstitution& rhs) const;
218 virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
219 virtual void doSubstitution(int64_t /*number*/, UnicodeString& /*toInsertInto*/, int32_t /*_pos*/, int32_t /*recursionCount*/, UErrorCode& /*status*/) const {}
220 virtual int64_t transformNumber(int64_t /*number*/) const { return 0; }
221 virtual double transformNumber(double number) const { return number - uprv_floor(number); }
223 virtual UBool doParse(const UnicodeString& text,
224 ParsePosition& parsePosition,
228 Formattable& result) const;
230 virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue + oldRuleValue; }
231 virtual double calcUpperBound(double /*oldUpperBound*/) const { return 0.0; }
232 virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
235 static UClassID getStaticClassID(void);
236 virtual UClassID getDynamicClassID(void) const;
239 FractionalPartSubstitution::~FractionalPartSubstitution() {}
241 class AbsoluteValueSubstitution : public NFSubstitution {
243 AbsoluteValueSubstitution(int32_t _pos,
244 const NFRuleSet* _ruleSet,
245 const UnicodeString& description,
247 : NFSubstitution(_pos, _ruleSet, description, status) {}
248 virtual ~AbsoluteValueSubstitution();
250 virtual int64_t transformNumber(int64_t number) const { return number >= 0 ? number : -number; }
251 virtual double transformNumber(double number) const { return uprv_fabs(number); }
252 virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const { return -newRuleValue; }
253 virtual double calcUpperBound(double /*oldUpperBound*/) const { return DBL_MAX; }
254 virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
257 static UClassID getStaticClassID(void);
258 virtual UClassID getDynamicClassID(void) const;
261 AbsoluteValueSubstitution::~AbsoluteValueSubstitution() {}
263 class NumeratorSubstitution : public NFSubstitution {
265 int64_t ldenominator;
268 static inline UnicodeString fixdesc(const UnicodeString& desc) {
269 if (desc.endsWith(LTLT, 2)) {
270 UnicodeString result(desc, 0, desc.length()-1);
275 NumeratorSubstitution(int32_t _pos,
278 const UnicodeString& description,
280 : NFSubstitution(_pos, _ruleSet, fixdesc(description), status), denominator(_denominator)
282 ldenominator = util64_fromDouble(denominator);
283 withZeros = description.endsWith(LTLT, 2);
285 virtual ~NumeratorSubstitution();
287 virtual UBool operator==(const NFSubstitution& rhs) const;
289 virtual int64_t transformNumber(int64_t number) const { return number * ldenominator; }
290 virtual double transformNumber(double number) const { return uprv_round(number * denominator); }
292 virtual void doSubstitution(int64_t /*number*/, UnicodeString& /*toInsertInto*/, int32_t /*_pos*/, int32_t /*recursionCount*/, UErrorCode& /*status*/) const {}
293 virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
294 virtual UBool doParse(const UnicodeString& text,
295 ParsePosition& parsePosition,
298 UBool /*lenientParse*/,
299 Formattable& result) const;
301 virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue / oldRuleValue; }
302 virtual double calcUpperBound(double /*oldUpperBound*/) const { return denominator; }
303 virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
305 static const UChar LTLT[2];
308 static UClassID getStaticClassID(void);
309 virtual UClassID getDynamicClassID(void) const;
312 NumeratorSubstitution::~NumeratorSubstitution() {}
315 NFSubstitution::makeSubstitution(int32_t pos,
317 const NFRule* predecessor,
318 const NFRuleSet* ruleSet,
319 const RuleBasedNumberFormat* formatter,
320 const UnicodeString& description,
323 // if the description is empty, return a NullSubstitution
324 if (description.length() == 0) {
328 switch (description.charAt(0)) {
329 // if the description begins with '<'...
331 // throw an exception if the rule is a negative number
333 if (rule->getBaseValue() == NFRule::kNegativeNumberRule) {
334 // throw new IllegalArgumentException("<< not allowed in negative-number rule");
335 status = U_PARSE_ERROR;
339 // if the rule is a fraction rule, return an
340 // IntegralPartSubstitution
341 else if (rule->getBaseValue() == NFRule::kImproperFractionRule
342 || rule->getBaseValue() == NFRule::kProperFractionRule
343 || rule->getBaseValue() == NFRule::kMasterRule) {
344 return new IntegralPartSubstitution(pos, ruleSet, description, status);
347 // if the rule set containing the rule is a fraction
348 // rule set, return a NumeratorSubstitution
349 else if (ruleSet->isFractionRuleSet()) {
350 return new NumeratorSubstitution(pos, (double)rule->getBaseValue(),
351 formatter->getDefaultRuleSet(), description, status);
354 // otherwise, return a MultiplierSubstitution
356 return new MultiplierSubstitution(pos, rule->getDivisor(), ruleSet,
357 description, status);
360 // if the description begins with '>'...
362 // if the rule is a negative-number rule, return
363 // an AbsoluteValueSubstitution
364 if (rule->getBaseValue() == NFRule::kNegativeNumberRule) {
365 return new AbsoluteValueSubstitution(pos, ruleSet, description, status);
368 // if the rule is a fraction rule, return a
369 // FractionalPartSubstitution
370 else if (rule->getBaseValue() == NFRule::kImproperFractionRule
371 || rule->getBaseValue() == NFRule::kProperFractionRule
372 || rule->getBaseValue() == NFRule::kMasterRule) {
373 return new FractionalPartSubstitution(pos, ruleSet, description, status);
376 // if the rule set owning the rule is a fraction rule set,
377 // throw an exception
378 else if (ruleSet->isFractionRuleSet()) {
379 // throw new IllegalArgumentException(">> not allowed in fraction rule set");
380 status = U_PARSE_ERROR;
384 // otherwise, return a ModulusSubstitution
386 return new ModulusSubstitution(pos, rule->getDivisor(), predecessor,
387 ruleSet, description, status);
390 // if the description begins with '=', always return a
391 // SameValueSubstitution
393 return new SameValueSubstitution(pos, ruleSet, description, status);
395 // and if it's anything else, throw an exception
397 // throw new IllegalArgumentException("Illegal substitution character");
398 status = U_PARSE_ERROR;
403 NFSubstitution::NFSubstitution(int32_t _pos,
404 const NFRuleSet* _ruleSet,
405 const UnicodeString& description,
407 : pos(_pos), ruleSet(NULL), numberFormat(NULL)
409 // the description should begin and end with the same character.
410 // If it doesn't that's a syntax error. Otherwise,
411 // makeSubstitution() was the only thing that needed to know
412 // about these characters, so strip them off
413 UnicodeString workingDescription(description);
414 if (description.length() >= 2
415 && description.charAt(0) == description.charAt(description.length() - 1))
417 workingDescription.remove(description.length() - 1, 1);
418 workingDescription.remove(0, 1);
420 else if (description.length() != 0) {
421 // throw new IllegalArgumentException("Illegal substitution syntax");
422 status = U_PARSE_ERROR;
426 if (workingDescription.length() == 0) {
427 // if the description was just two paired token characters
428 // (i.e., "<<" or ">>"), it uses the rule set it belongs to to
430 this->ruleSet = _ruleSet;
432 else if (workingDescription.charAt(0) == gPercent) {
433 // if the description contains a rule set name, that's the rule
434 // set we use to format the result: get a reference to the
436 this->ruleSet = _ruleSet->getOwner()->findRuleSet(workingDescription, status);
438 else if (workingDescription.charAt(0) == gPound || workingDescription.charAt(0) ==gZero) {
439 // if the description begins with 0 or #, treat it as a
440 // DecimalFormat pattern, and initialize a DecimalFormat with
441 // that pattern (then set it to use the DecimalFormatSymbols
442 // belonging to our formatter)
443 const DecimalFormatSymbols* sym = _ruleSet->getOwner()->getDecimalFormatSymbols();
445 status = U_MISSING_RESOURCE_ERROR;
448 DecimalFormat *tempNumberFormat = new DecimalFormat(workingDescription, *sym, status);
450 if (!tempNumberFormat) {
451 status = U_MEMORY_ALLOCATION_ERROR;
454 if (U_FAILURE(status)) {
455 delete tempNumberFormat;
458 this->numberFormat = tempNumberFormat;
460 else if (workingDescription.charAt(0) == gGreaterThan) {
461 // if the description is ">>>", this substitution bypasses the
462 // usual rule-search process and always uses the rule that precedes
463 // it in its own rule set's rule list (this is used for place-value
464 // notations: formats where you want to see a particular part of
465 // a number even when it's 0)
467 // this causes problems when >>> is used in a frationalPartSubstitution
468 // this->ruleSet = NULL;
469 this->ruleSet = _ruleSet;
470 this->numberFormat = NULL;
473 // and of the description is none of these things, it's a syntax error
475 // throw new IllegalArgumentException("Illegal substitution syntax");
476 status = U_PARSE_ERROR;
480 NFSubstitution::~NFSubstitution()
487 * Set's the substitution's divisor. Used by NFRule.setBaseValue().
488 * A no-op for all substitutions except multiplier and modulus
490 * @param radix The radix of the divisor
491 * @param exponent The exponent of the divisor
494 NFSubstitution::setDivisor(int32_t /*radix*/, int32_t /*exponent*/, UErrorCode& /*status*/) {
495 // a no-op for all substitutions except multiplier and modulus substitutions
499 NFSubstitution::setDecimalFormatSymbols(const DecimalFormatSymbols &newSymbols, UErrorCode& /*status*/) {
500 if (numberFormat != NULL) {
501 numberFormat->setDecimalFormatSymbols(newSymbols);
505 //-----------------------------------------------------------------------
507 //-----------------------------------------------------------------------
509 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NFSubstitution)
512 * Compares two substitutions for equality
513 * @param The substitution to compare this one to
514 * @return true if the two substitutions are functionally equivalent
517 NFSubstitution::operator==(const NFSubstitution& rhs) const
519 // compare class and all of the fields all substitutions have
521 // this should be called by subclasses before their own equality tests
522 return typeid(*this) == typeid(rhs)
524 && (ruleSet == NULL) == (rhs.ruleSet == NULL)
525 // && ruleSet == rhs.ruleSet causes circularity, other checks to make instead?
526 && (numberFormat == NULL
527 ? (rhs.numberFormat == NULL)
528 : (*numberFormat == *rhs.numberFormat));
532 * Returns a textual description of the substitution
533 * @return A textual description of the substitution. This might
534 * not be identical to the description it was created from, but
535 * it'll produce the same result.
538 NFSubstitution::toString(UnicodeString& text) const
540 // use tokenChar() to get the character at the beginning and
541 // end of the substitutin token. In between them will go
542 // either the name of the rule set it uses, or the pattern of
543 // the DecimalFormat it uses
545 text.append(tokenChar());
548 if (ruleSet != NULL) {
549 ruleSet->getName(temp);
550 } else if (numberFormat != NULL) {
551 numberFormat->toPattern(temp);
554 text.append(tokenChar());
557 //-----------------------------------------------------------------------
559 //-----------------------------------------------------------------------
562 * Performs a mathematical operation on the number, formats it using
563 * either ruleSet or decimalFormat, and inserts the result into
565 * @param number The number being formatted.
566 * @param toInsertInto The string we insert the result into
567 * @param pos The position in toInsertInto where the owning rule's
568 * rule text begins (this value is added to this substitution's
569 * position to determine exactly where to insert the new text)
572 NFSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
574 if (ruleSet != NULL) {
575 // perform a transformation on the number that is dependent
576 // on the type of substitution this is, then just call its
577 // rule set's format() method to format the result
578 ruleSet->format(transformNumber(number), toInsertInto, _pos + this->pos, recursionCount, status);
579 } else if (numberFormat != NULL) {
580 // or perform the transformation on the number (preserving
581 // the result's fractional part if the formatter it set
582 // to show it), then use that formatter's format() method
583 // to format the result
584 double numberToFormat = transformNumber((double)number);
585 if (numberFormat->getMaximumFractionDigits() == 0) {
586 numberToFormat = uprv_floor(numberToFormat);
590 numberFormat->format(numberToFormat, temp, status);
591 toInsertInto.insert(_pos + this->pos, temp);
596 * Performs a mathematical operation on the number, formats it using
597 * either ruleSet or decimalFormat, and inserts the result into
599 * @param number The number being formatted.
600 * @param toInsertInto The string we insert the result into
601 * @param pos The position in toInsertInto where the owning rule's
602 * rule text begins (this value is added to this substitution's
603 * position to determine exactly where to insert the new text)
606 NFSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const {
607 // perform a transformation on the number being formatted that
608 // is dependent on the type of substitution this is
609 double numberToFormat = transformNumber(number);
611 if (uprv_isInfinite(numberToFormat)) {
612 // This is probably a minus rule. Combine it with an infinite rule.
613 const NFRule *infiniteRule = ruleSet->findDoubleRule(uprv_getInfinity());
614 infiniteRule->doFormat(numberToFormat, toInsertInto, _pos + this->pos, recursionCount, status);
618 // if the result is an integer, from here on out we work in integer
619 // space (saving time and memory and preserving accuracy)
620 if (numberToFormat == uprv_floor(numberToFormat) && ruleSet != NULL) {
621 ruleSet->format(util64_fromDouble(numberToFormat), toInsertInto, _pos + this->pos, recursionCount, status);
623 // if the result isn't an integer, then call either our rule set's
624 // format() method or our DecimalFormat's format() method to
627 if (ruleSet != NULL) {
628 ruleSet->format(numberToFormat, toInsertInto, _pos + this->pos, recursionCount, status);
629 } else if (numberFormat != NULL) {
631 numberFormat->format(numberToFormat, temp);
632 toInsertInto.insert(_pos + this->pos, temp);
638 //-----------------------------------------------------------------------
640 //-----------------------------------------------------------------------
647 * Parses a string using the rule set or DecimalFormat belonging
648 * to this substitution. If there's a match, a mathematical
649 * operation (the inverse of the one used in formatting) is
650 * performed on the result of the parse and the value passed in
651 * and returned as the result. The parse position is updated to
652 * point to the first unmatched character in the string.
653 * @param text The string to parse
654 * @param parsePosition On entry, ignored, but assumed to be 0.
655 * On exit, this is updated to point to the first unmatched
656 * character (or 0 if the substitution didn't match)
657 * @param baseValue A partial parse result that should be
658 * combined with the result of this parse
659 * @param upperBound When searching the rule set for a rule
660 * matching the string passed in, only rules with base values
661 * lower than this are considered
662 * @param lenientParse If true and matching against rules fails,
663 * the substitution will also try matching the text against
664 * numerals using a default-costructed NumberFormat. If false,
665 * no extra work is done. (This value is false whenever the
666 * formatter isn't in lenient-parse mode, but is also false
667 * under some conditions even when the formatter _is_ in
668 * lenient-parse mode.)
669 * @return If there's a match, this is the result of composing
670 * baseValue with whatever was returned from matching the
671 * characters. This will be either a Long or a Double. If there's
672 * no match this is new Long(0) (not null), and parsePosition
676 NFSubstitution::doParse(const UnicodeString& text,
677 ParsePosition& parsePosition,
681 Formattable& result) const
684 fprintf(stderr, "<nfsubs> %x bv: %g ub: %g\n", this, baseValue, upperBound);
686 // figure out the highest base value a rule can have and match
687 // the text being parsed (this varies according to the type of
688 // substitutions: multiplier, modulus, and numerator substitutions
689 // restrict the search to rules with base values lower than their
690 // own; same-value substitutions leave the upper bound wherever
691 // it was, and the others allow any rule to match
692 upperBound = calcUpperBound(upperBound);
694 // use our rule set to parse the text. If that fails and
695 // lenient parsing is enabled (this is always false if the
696 // formatter's lenient-parsing mode is off, but it may also
697 // be false even when the formatter's lenient-parse mode is
698 // on), then also try parsing the text using a default-
699 // constructed NumberFormat
700 if (ruleSet != NULL) {
701 ruleSet->parse(text, parsePosition, upperBound, result);
702 if (lenientParse && !ruleSet->isFractionRuleSet() && parsePosition.getIndex() == 0) {
703 UErrorCode status = U_ZERO_ERROR;
704 NumberFormat* fmt = NumberFormat::createInstance(status);
705 if (U_SUCCESS(status)) {
706 fmt->parse(text, result, parsePosition);
711 // ...or use our DecimalFormat to parse the text
712 } else if (numberFormat != NULL) {
713 numberFormat->parse(text, result, parsePosition);
716 // if the parse was successful, we've already advanced the caller's
717 // parse position (this is the one function that doesn't have one
718 // of its own). Derive a parse result and return it as a Long,
719 // if possible, or a Double
720 if (parsePosition.getIndex() != 0) {
721 UErrorCode status = U_ZERO_ERROR;
722 double tempResult = result.getDouble(status);
724 // composeRuleValue() produces a full parse result from
725 // the partial parse result passed to this function from
726 // the caller (this is either the owning rule's base value
727 // or the partial result obtained from composing the
728 // owning rule's base value with its other substitution's
729 // parse result) and the partial parse result obtained by
730 // matching the substitution (which will be the same value
731 // the caller would get by parsing just this part of the
732 // text with RuleBasedNumberFormat.parse() ). How the two
733 // values are used to derive the full parse result depends
734 // on the types of substitutions: For a regular rule, the
735 // ultimate result is its multiplier substitution's result
736 // times the rule's divisor (or the rule's base value) plus
737 // the modulus substitution's result (which will actually
738 // supersede part of the rule's base value). For a negative-
739 // number rule, the result is the negative of its substitution's
740 // result. For a fraction rule, it's the sum of its two
741 // substitution results. For a rule in a fraction rule set,
742 // it's the numerator substitution's result divided by
743 // the rule's base value. Results from same-value substitutions
744 // propagate back upard, and null substitutions don't affect
746 tempResult = composeRuleValue(tempResult, baseValue);
747 result.setDouble(tempResult);
749 // if the parse was UNsuccessful, return 0
757 * Returns true if this is a modulus substitution. (We didn't do this
758 * with instanceof partially because it causes source files to
759 * proliferate and partially because we have to port this to C++.)
760 * @return true if this object is an instance of ModulusSubstitution
763 NFSubstitution::isModulusSubstitution() const {
767 //===================================================================
768 // SameValueSubstitution
769 //===================================================================
772 * A substitution that passes the value passed to it through unchanged.
773 * Represented by == in rule descriptions.
775 SameValueSubstitution::SameValueSubstitution(int32_t _pos,
776 const NFRuleSet* _ruleSet,
777 const UnicodeString& description,
779 : NFSubstitution(_pos, _ruleSet, description, status)
781 if (0 == description.compare(gEqualsEquals, 2)) {
782 // throw new IllegalArgumentException("== is not a legal token");
783 status = U_PARSE_ERROR;
787 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SameValueSubstitution)
789 //===================================================================
790 // MultiplierSubstitution
791 //===================================================================
793 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MultiplierSubstitution)
795 UBool MultiplierSubstitution::operator==(const NFSubstitution& rhs) const
797 return NFSubstitution::operator==(rhs) &&
798 divisor == ((const MultiplierSubstitution*)&rhs)->divisor;
802 //===================================================================
803 // ModulusSubstitution
804 //===================================================================
807 * A substitution that divides the number being formatted by the its rule's
808 * divisor and formats the remainder. Represented by ">>" in a
811 ModulusSubstitution::ModulusSubstitution(int32_t _pos,
813 const NFRule* predecessor,
814 const NFRuleSet* _ruleSet,
815 const UnicodeString& description,
817 : NFSubstitution(_pos, _ruleSet, description, status)
821 ldivisor = util64_fromDouble(_divisor);
823 // the owning rule's divisor controls the behavior of this
824 // substitution: rather than keeping a backpointer to the rule,
825 // we keep a copy of the divisor
828 status = U_PARSE_ERROR;
831 if (0 == description.compare(gGreaterGreaterGreaterThan, 3)) {
832 // the >>> token doesn't alter how this substituion calculates the
833 // values it uses for formatting and parsing, but it changes
834 // what's done with that value after it's obtained: >>> short-
835 // circuits the rule-search process and goes straight to the
836 // specified rule to format the substitution value
837 ruleToUse = predecessor;
841 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ModulusSubstitution)
843 UBool ModulusSubstitution::operator==(const NFSubstitution& rhs) const
845 return NFSubstitution::operator==(rhs) &&
846 divisor == ((const ModulusSubstitution*)&rhs)->divisor &&
847 ruleToUse == ((const ModulusSubstitution*)&rhs)->ruleToUse;
850 //-----------------------------------------------------------------------
852 //-----------------------------------------------------------------------
856 * If this is a >>> substitution, use ruleToUse to fill in
857 * the substitution. Otherwise, just use the superclass function.
858 * @param number The number being formatted
859 * @toInsertInto The string to insert the result of this substitution
861 * @param pos The position of the rule text in toInsertInto
864 ModulusSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
866 // if this isn't a >>> substitution, just use the inherited version
867 // of this function (which uses either a rule set or a DecimalFormat
868 // to format its substitution value)
869 if (ruleToUse == NULL) {
870 NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
872 // a >>> substitution goes straight to a particular rule to
873 // format the substitution value
875 int64_t numberToFormat = transformNumber(number);
876 ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos(), recursionCount, status);
881 * If this is a >>> substitution, use ruleToUse to fill in
882 * the substitution. Otherwise, just use the superclass function.
883 * @param number The number being formatted
884 * @toInsertInto The string to insert the result of this substitution
886 * @param pos The position of the rule text in toInsertInto
889 ModulusSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
891 // if this isn't a >>> substitution, just use the inherited version
892 // of this function (which uses either a rule set or a DecimalFormat
893 // to format its substitution value)
894 if (ruleToUse == NULL) {
895 NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
897 // a >>> substitution goes straight to a particular rule to
898 // format the substitution value
900 double numberToFormat = transformNumber(number);
902 ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos(), recursionCount, status);
906 //-----------------------------------------------------------------------
908 //-----------------------------------------------------------------------
911 * If this is a >>> substitution, match only against ruleToUse.
912 * Otherwise, use the superclass function.
913 * @param text The string to parse
914 * @param parsePosition Ignored on entry, updated on exit to point to
915 * the first unmatched character.
916 * @param baseValue The partial parse result prior to calling this
920 ModulusSubstitution::doParse(const UnicodeString& text,
921 ParsePosition& parsePosition,
925 Formattable& result) const
927 // if this isn't a >>> substitution, we can just use the
928 // inherited parse() routine to do the parsing
929 if (ruleToUse == NULL) {
930 return NFSubstitution::doParse(text, parsePosition, baseValue, upperBound, lenientParse, result);
932 // but if it IS a >>> substitution, we have to do it here: we
933 // use the specific rule's doParse() method, and then we have to
934 // do some of the other work of NFRuleSet.parse()
936 ruleToUse->doParse(text, parsePosition, FALSE, upperBound, result);
938 if (parsePosition.getIndex() != 0) {
939 UErrorCode status = U_ZERO_ERROR;
940 double tempResult = result.getDouble(status);
941 tempResult = composeRuleValue(tempResult, baseValue);
942 result.setDouble(tempResult);
949 * Returns a textual description of the substitution
950 * @return A textual description of the substitution. This might
951 * not be identical to the description it was created from, but
952 * it'll produce the same result.
955 ModulusSubstitution::toString(UnicodeString& text) const
957 // use tokenChar() to get the character at the beginning and
958 // end of the substitutin token. In between them will go
959 // either the name of the rule set it uses, or the pattern of
960 // the DecimalFormat it uses
962 if ( ruleToUse != NULL ) { // Must have been a >>> substitution.
964 text.append(tokenChar());
965 text.append(tokenChar());
966 text.append(tokenChar());
967 } else { // Otherwise just use the super-class function.
968 NFSubstitution::toString(text);
971 //===================================================================
972 // IntegralPartSubstitution
973 //===================================================================
975 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IntegralPartSubstitution)
978 //===================================================================
979 // FractionalPartSubstitution
980 //===================================================================
984 * Constructs a FractionalPartSubstitution. This object keeps a flag
985 * telling whether it should format by digits or not. In addition,
986 * it marks the rule set it calls (if any) as a fraction rule set.
988 FractionalPartSubstitution::FractionalPartSubstitution(int32_t _pos,
989 const NFRuleSet* _ruleSet,
990 const UnicodeString& description,
992 : NFSubstitution(_pos, _ruleSet, description, status)
997 // akk, ruleSet can change in superclass constructor
998 if (0 == description.compare(gGreaterGreaterThan, 2) ||
999 0 == description.compare(gGreaterGreaterGreaterThan, 3) ||
1000 _ruleSet == getRuleSet()) {
1002 if (0 == description.compare(gGreaterGreaterGreaterThan, 3)) {
1007 ((NFRuleSet*)getRuleSet())->makeIntoFractionRuleSet();
1011 //-----------------------------------------------------------------------
1013 //-----------------------------------------------------------------------
1016 * If in "by digits" mode, fills in the substitution one decimal digit
1017 * at a time using the rule set containing this substitution.
1018 * Otherwise, uses the superclass function.
1019 * @param number The number being formatted
1020 * @param toInsertInto The string to insert the result of formatting
1021 * the substitution into
1022 * @param pos The position of the owning rule's rule text in
1026 FractionalPartSubstitution::doSubstitution(double number, UnicodeString& toInsertInto,
1027 int32_t _pos, int32_t recursionCount, UErrorCode& status) const
1029 // if we're not in "byDigits" mode, just use the inherited
1030 // doSubstitution() routine
1032 NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
1034 // if we're in "byDigits" mode, transform the value into an integer
1035 // by moving the decimal point eight places to the right and
1036 // pulling digits off the right one at a time, formatting each digit
1037 // as an integer using this substitution's owning rule set
1038 // (this is slower, but more accurate, than doing it from the
1041 // int32_t numberToFormat = (int32_t)uprv_round(transformNumber(number) * uprv_pow(10, kMaxDecimalDigits));
1042 // // this flag keeps us from formatting trailing zeros. It starts
1043 // // out false because we're pulling from the right, and switches
1044 // // to true the first time we encounter a non-zero digit
1045 // UBool doZeros = FALSE;
1046 // for (int32_t i = 0; i < kMaxDecimalDigits; i++) {
1047 // int64_t digit = numberToFormat % 10;
1048 // if (digit != 0 || doZeros) {
1049 // if (doZeros && useSpaces) {
1050 // toInsertInto.insert(_pos + getPos(), gSpace);
1053 // getRuleSet()->format(digit, toInsertInto, _pos + getPos());
1055 // numberToFormat /= 10;
1060 dl.roundFixedPoint(20); // round to 20 fraction digits.
1061 dl.reduce(); // Removes any trailing zeros.
1064 for (int32_t didx = dl.getCount()-1; didx>=dl.getDecimalAt(); didx--) {
1065 // Loop iterates over fraction digits, starting with the LSD.
1066 // include both real digits from the number, and zeros
1067 // to the left of the MSD but to the right of the decimal point.
1068 if (pad && useSpaces) {
1069 toInsertInto.insert(_pos + getPos(), gSpace);
1073 int64_t digit = didx>=0 ? dl.getDigit(didx) - '0' : 0;
1074 getRuleSet()->format(digit, toInsertInto, _pos + getPos(), recursionCount, status);
1078 // hack around lack of precision in digitlist. if we would end up with
1079 // "foo point" make sure we add a " zero" to the end.
1080 getRuleSet()->format((int64_t)0, toInsertInto, _pos + getPos(), recursionCount, status);
1085 //-----------------------------------------------------------------------
1087 //-----------------------------------------------------------------------
1090 * If in "by digits" mode, parses the string as if it were a string
1091 * of individual digits; otherwise, uses the superclass function.
1092 * @param text The string to parse
1093 * @param parsePosition Ignored on entry, but updated on exit to point
1094 * to the first unmatched character
1095 * @param baseValue The partial parse result prior to entering this
1097 * @param upperBound Only consider rules with base values lower than
1098 * this when filling in the substitution
1099 * @param lenientParse If true, try matching the text as numerals if
1100 * matching as words doesn't work
1101 * @return If the match was successful, the current partial parse
1102 * result; otherwise new Long(0). The result is either a Long or
1107 FractionalPartSubstitution::doParse(const UnicodeString& text,
1108 ParsePosition& parsePosition,
1110 double /*upperBound*/,
1112 Formattable& resVal) const
1114 // if we're not in byDigits mode, we can just use the inherited
1117 return NFSubstitution::doParse(text, parsePosition, baseValue, 0, lenientParse, resVal);
1119 // if we ARE in byDigits mode, parse the text one digit at a time
1120 // using this substitution's owning rule set (we do this by setting
1121 // upperBound to 10 when calling doParse() ) until we reach
1124 UnicodeString workText(text);
1125 ParsePosition workPos(1);
1128 // double p10 = 0.1;
1131 NumberFormat* fmt = NULL;
1132 while (workText.length() > 0 && workPos.getIndex() != 0) {
1133 workPos.setIndex(0);
1135 getRuleSet()->parse(workText, workPos, 10, temp);
1136 UErrorCode status = U_ZERO_ERROR;
1137 digit = temp.getLong(status);
1138 // digit = temp.getType() == Formattable::kLong ?
1140 // (int32_t)temp.getDouble();
1142 if (lenientParse && workPos.getIndex() == 0) {
1144 status = U_ZERO_ERROR;
1145 fmt = NumberFormat::createInstance(status);
1146 if (U_FAILURE(status)) {
1152 fmt->parse(workText, temp, workPos);
1153 digit = temp.getLong(status);
1157 if (workPos.getIndex() != 0) {
1158 dl.append((char)('0' + digit));
1159 // result += digit * p10;
1161 parsePosition.setIndex(parsePosition.getIndex() + workPos.getIndex());
1162 workText.removeBetween(0, workPos.getIndex());
1163 while (workText.length() > 0 && workText.charAt(0) == gSpace) {
1164 workText.removeBetween(0, 1);
1165 parsePosition.setIndex(parsePosition.getIndex() + 1);
1171 result = dl.getCount() == 0 ? 0 : dl.getDouble();
1172 result = composeRuleValue(result, baseValue);
1173 resVal.setDouble(result);
1179 FractionalPartSubstitution::operator==(const NFSubstitution& rhs) const
1181 return NFSubstitution::operator==(rhs) &&
1182 ((const FractionalPartSubstitution*)&rhs)->byDigits == byDigits;
1185 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FractionalPartSubstitution)
1188 //===================================================================
1189 // AbsoluteValueSubstitution
1190 //===================================================================
1192 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(AbsoluteValueSubstitution)
1194 //===================================================================
1195 // NumeratorSubstitution
1196 //===================================================================
1199 NumeratorSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t apos, int32_t recursionCount, UErrorCode& status) const {
1200 // perform a transformation on the number being formatted that
1201 // is dependent on the type of substitution this is
1203 double numberToFormat = transformNumber(number);
1204 int64_t longNF = util64_fromDouble(numberToFormat);
1206 const NFRuleSet* aruleSet = getRuleSet();
1207 if (withZeros && aruleSet != NULL) {
1208 // if there are leading zeros in the decimal expansion then emit them
1210 int32_t len = toInsertInto.length();
1211 while ((nf *= 10) < denominator) {
1212 toInsertInto.insert(apos + getPos(), gSpace);
1213 aruleSet->format((int64_t)0, toInsertInto, apos + getPos(), recursionCount, status);
1215 apos += toInsertInto.length() - len;
1218 // if the result is an integer, from here on out we work in integer
1219 // space (saving time and memory and preserving accuracy)
1220 if (numberToFormat == longNF && aruleSet != NULL) {
1221 aruleSet->format(longNF, toInsertInto, apos + getPos(), recursionCount, status);
1223 // if the result isn't an integer, then call either our rule set's
1224 // format() method or our DecimalFormat's format() method to
1225 // format the result
1227 if (aruleSet != NULL) {
1228 aruleSet->format(numberToFormat, toInsertInto, apos + getPos(), recursionCount, status);
1231 getNumberFormat()->format(numberToFormat, temp, status);
1232 toInsertInto.insert(apos + getPos(), temp);
1238 NumeratorSubstitution::doParse(const UnicodeString& text,
1239 ParsePosition& parsePosition,
1242 UBool /*lenientParse*/,
1243 Formattable& result) const
1245 // we don't have to do anything special to do the parsing here,
1246 // but we have to turn lenient parsing off-- if we leave it on,
1247 // it SERIOUSLY messes up the algorithm
1249 // if withZeros is true, we need to count the zeros
1250 // and use that to adjust the parse result
1251 UErrorCode status = U_ZERO_ERROR;
1252 int32_t zeroCount = 0;
1253 UnicodeString workText(text);
1256 ParsePosition workPos(1);
1259 while (workText.length() > 0 && workPos.getIndex() != 0) {
1260 workPos.setIndex(0);
1261 getRuleSet()->parse(workText, workPos, 1, temp); // parse zero or nothing at all
1262 if (workPos.getIndex() == 0) {
1263 // we failed, either there were no more zeros, or the number was formatted with digits
1264 // either way, we're done
1269 parsePosition.setIndex(parsePosition.getIndex() + workPos.getIndex());
1270 workText.remove(0, workPos.getIndex());
1271 while (workText.length() > 0 && workText.charAt(0) == gSpace) {
1272 workText.remove(0, 1);
1273 parsePosition.setIndex(parsePosition.getIndex() + 1);
1278 workText.remove(0, (int32_t)parsePosition.getIndex());
1279 parsePosition.setIndex(0);
1282 // we've parsed off the zeros, now let's parse the rest from our current position
1283 NFSubstitution::doParse(workText, parsePosition, withZeros ? 1 : baseValue, upperBound, FALSE, result);
1286 // any base value will do in this case. is there a way to
1287 // force this to not bother trying all the base values?
1289 // compute the 'effective' base and prescale the value down
1290 int64_t n = result.getLong(status); // force conversion!
1297 // now add the zeros
1298 while (zeroCount > 0) {
1302 // d is now our true denominator
1303 result.setDouble((double)n/(double)d);
1310 NumeratorSubstitution::operator==(const NFSubstitution& rhs) const
1312 return NFSubstitution::operator==(rhs) &&
1313 denominator == ((const NumeratorSubstitution*)&rhs)->denominator;
1316 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NumeratorSubstitution)
1318 const UChar NumeratorSubstitution::LTLT[] = { 0x003c, 0x003c };