1 // © 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
22 #include "fmtableimp.h"
24 #include "number_decimalquantity.h"
28 static const UChar gLessThan
= 0x003c;
29 static const UChar gEquals
= 0x003d;
30 static const UChar gGreaterThan
= 0x003e;
31 static const UChar gPercent
= 0x0025;
32 static const UChar gPound
= 0x0023;
33 static const UChar gZero
= 0x0030;
34 static const UChar gSpace
= 0x0020;
36 static const UChar gEqualsEquals
[] =
40 static const UChar gGreaterGreaterGreaterThan
[] =
44 static const UChar gGreaterGreaterThan
[] =
51 using number::impl::DecimalQuantity
;
53 class SameValueSubstitution
: public NFSubstitution
{
55 SameValueSubstitution(int32_t pos
,
56 const NFRuleSet
* ruleset
,
57 const UnicodeString
& description
,
59 virtual ~SameValueSubstitution();
61 virtual int64_t transformNumber(int64_t number
) const { return number
; }
62 virtual double transformNumber(double number
) const { return number
; }
63 virtual double composeRuleValue(double newRuleValue
, double /*oldRuleValue*/) const { return newRuleValue
; }
64 virtual double calcUpperBound(double oldUpperBound
) const { return oldUpperBound
; }
65 virtual UChar
tokenChar() const { return (UChar
)0x003d; } // '='
68 static UClassID
getStaticClassID(void);
69 virtual UClassID
getDynamicClassID(void) const;
72 SameValueSubstitution::~SameValueSubstitution() {}
74 class MultiplierSubstitution
: public NFSubstitution
{
78 MultiplierSubstitution(int32_t _pos
,
80 const NFRuleSet
* _ruleSet
,
81 const UnicodeString
& description
,
83 : NFSubstitution(_pos
, _ruleSet
, description
, status
), divisor(rule
->getDivisor())
86 status
= U_PARSE_ERROR
;
89 virtual ~MultiplierSubstitution();
91 virtual void setDivisor(int32_t radix
, int16_t exponent
, UErrorCode
& status
) {
92 divisor
= util64_pow(radix
, exponent
);
95 status
= U_PARSE_ERROR
;
99 virtual UBool
operator==(const NFSubstitution
& rhs
) const;
101 virtual int64_t transformNumber(int64_t number
) const {
102 return number
/ divisor
;
105 virtual double transformNumber(double number
) const {
107 return uprv_floor(number
/ divisor
);
109 return number
/ divisor
;
113 virtual double composeRuleValue(double newRuleValue
, double /*oldRuleValue*/) const {
114 return newRuleValue
* divisor
;
117 virtual double calcUpperBound(double /*oldUpperBound*/) const { return static_cast<double>(divisor
); }
119 virtual UChar
tokenChar() const { return (UChar
)0x003c; } // '<'
122 static UClassID
getStaticClassID(void);
123 virtual UClassID
getDynamicClassID(void) const;
126 MultiplierSubstitution::~MultiplierSubstitution() {}
128 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
, int16_t exponent
, UErrorCode
& status
) {
141 divisor
= util64_pow(radix
, exponent
);
144 status
= U_PARSE_ERROR
;
148 virtual UBool
operator==(const NFSubstitution
& rhs
) const;
150 virtual void doSubstitution(int64_t number
, UnicodeString
& toInsertInto
, int32_t pos
, int32_t recursionCount
, UErrorCode
& status
) const;
151 virtual void doSubstitution(double number
, UnicodeString
& toInsertInto
, int32_t pos
, int32_t recursionCount
, UErrorCode
& status
) const;
153 virtual int64_t transformNumber(int64_t number
) const { return number
% divisor
; }
154 virtual double transformNumber(double number
) const { return uprv_fmod(number
, static_cast<double>(divisor
)); }
156 virtual UBool
doParse(const UnicodeString
& text
,
157 ParsePosition
& parsePosition
,
161 uint32_t nonNumericalExecutedRuleMask
,
162 Formattable
& result
) const;
164 virtual double composeRuleValue(double newRuleValue
, double oldRuleValue
) const {
165 return oldRuleValue
- uprv_fmod(oldRuleValue
, static_cast<double>(divisor
)) + newRuleValue
;
168 virtual double calcUpperBound(double /*oldUpperBound*/) const { return static_cast<double>(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 uint32_t nonNumericalExecutedRuleMask
,
229 Formattable
& result
) const;
231 virtual double composeRuleValue(double newRuleValue
, double oldRuleValue
) const { return newRuleValue
+ oldRuleValue
; }
232 virtual double calcUpperBound(double /*oldUpperBound*/) const { return 0.0; }
233 virtual UChar
tokenChar() const { return (UChar
)0x003e; } // '>'
236 static UClassID
getStaticClassID(void);
237 virtual UClassID
getDynamicClassID(void) const;
240 FractionalPartSubstitution::~FractionalPartSubstitution() {}
242 class AbsoluteValueSubstitution
: public NFSubstitution
{
244 AbsoluteValueSubstitution(int32_t _pos
,
245 const NFRuleSet
* _ruleSet
,
246 const UnicodeString
& description
,
248 : NFSubstitution(_pos
, _ruleSet
, description
, status
) {}
249 virtual ~AbsoluteValueSubstitution();
251 virtual int64_t transformNumber(int64_t number
) const { return number
>= 0 ? number
: -number
; }
252 virtual double transformNumber(double number
) const { return uprv_fabs(number
); }
253 virtual double composeRuleValue(double newRuleValue
, double /*oldRuleValue*/) const { return -newRuleValue
; }
254 virtual double calcUpperBound(double /*oldUpperBound*/) const { return DBL_MAX
; }
255 virtual UChar
tokenChar() const { return (UChar
)0x003e; } // '>'
258 static UClassID
getStaticClassID(void);
259 virtual UClassID
getDynamicClassID(void) const;
262 AbsoluteValueSubstitution::~AbsoluteValueSubstitution() {}
264 class NumeratorSubstitution
: public NFSubstitution
{
266 int64_t ldenominator
;
269 static inline UnicodeString
fixdesc(const UnicodeString
& desc
) {
270 if (desc
.endsWith(LTLT
, 2)) {
271 UnicodeString
result(desc
, 0, desc
.length()-1);
276 NumeratorSubstitution(int32_t _pos
,
279 const UnicodeString
& description
,
281 : NFSubstitution(_pos
, _ruleSet
, fixdesc(description
), status
), denominator(_denominator
)
283 ldenominator
= util64_fromDouble(denominator
);
284 withZeros
= description
.endsWith(LTLT
, 2);
286 virtual ~NumeratorSubstitution();
288 virtual UBool
operator==(const NFSubstitution
& rhs
) const;
290 virtual int64_t transformNumber(int64_t number
) const { return number
* ldenominator
; }
291 virtual double transformNumber(double number
) const { return uprv_round(number
* denominator
); }
293 virtual void doSubstitution(int64_t /*number*/, UnicodeString
& /*toInsertInto*/, int32_t /*_pos*/, int32_t /*recursionCount*/, UErrorCode
& /*status*/) const {}
294 virtual void doSubstitution(double number
, UnicodeString
& toInsertInto
, int32_t pos
, int32_t recursionCount
, UErrorCode
& status
) const;
295 virtual UBool
doParse(const UnicodeString
& text
,
296 ParsePosition
& parsePosition
,
299 UBool
/*lenientParse*/,
300 uint32_t nonNumericalExecutedRuleMask
,
301 Formattable
& result
) const;
303 virtual double composeRuleValue(double newRuleValue
, double oldRuleValue
) const { return newRuleValue
/ oldRuleValue
; }
304 virtual double calcUpperBound(double /*oldUpperBound*/) const { return denominator
; }
305 virtual UChar
tokenChar() const { return (UChar
)0x003c; } // '<'
307 static const UChar LTLT
[2];
310 static UClassID
getStaticClassID(void);
311 virtual UClassID
getDynamicClassID(void) const;
314 NumeratorSubstitution::~NumeratorSubstitution() {}
317 NFSubstitution::makeSubstitution(int32_t pos
,
319 const NFRule
* predecessor
,
320 const NFRuleSet
* ruleSet
,
321 const RuleBasedNumberFormat
* formatter
,
322 const UnicodeString
& description
,
325 // if the description is empty, return a NullSubstitution
326 if (description
.length() == 0) {
330 switch (description
.charAt(0)) {
331 // if the description begins with '<'...
333 // throw an exception if the rule is a negative number
335 if (rule
->getBaseValue() == NFRule::kNegativeNumberRule
) {
336 // throw new IllegalArgumentException("<< not allowed in negative-number rule");
337 status
= U_PARSE_ERROR
;
341 // if the rule is a fraction rule, return an
342 // IntegralPartSubstitution
343 else if (rule
->getBaseValue() == NFRule::kImproperFractionRule
344 || rule
->getBaseValue() == NFRule::kProperFractionRule
345 || rule
->getBaseValue() == NFRule::kMasterRule
) {
346 return new IntegralPartSubstitution(pos
, ruleSet
, description
, status
);
349 // if the rule set containing the rule is a fraction
350 // rule set, return a NumeratorSubstitution
351 else if (ruleSet
->isFractionRuleSet()) {
352 return new NumeratorSubstitution(pos
, (double)rule
->getBaseValue(),
353 formatter
->getDefaultRuleSet(), description
, status
);
356 // otherwise, return a MultiplierSubstitution
358 return new MultiplierSubstitution(pos
, rule
, ruleSet
,
359 description
, status
);
362 // if the description begins with '>'...
364 // if the rule is a negative-number rule, return
365 // an AbsoluteValueSubstitution
366 if (rule
->getBaseValue() == NFRule::kNegativeNumberRule
) {
367 return new AbsoluteValueSubstitution(pos
, ruleSet
, description
, status
);
370 // if the rule is a fraction rule, return a
371 // FractionalPartSubstitution
372 else if (rule
->getBaseValue() == NFRule::kImproperFractionRule
373 || rule
->getBaseValue() == NFRule::kProperFractionRule
374 || rule
->getBaseValue() == NFRule::kMasterRule
) {
375 return new FractionalPartSubstitution(pos
, ruleSet
, description
, status
);
378 // if the rule set owning the rule is a fraction rule set,
379 // throw an exception
380 else if (ruleSet
->isFractionRuleSet()) {
381 // throw new IllegalArgumentException(">> not allowed in fraction rule set");
382 status
= U_PARSE_ERROR
;
386 // otherwise, return a ModulusSubstitution
388 return new ModulusSubstitution(pos
, rule
, predecessor
,
389 ruleSet
, description
, status
);
392 // if the description begins with '=', always return a
393 // SameValueSubstitution
395 return new SameValueSubstitution(pos
, ruleSet
, description
, status
);
397 // and if it's anything else, throw an exception
399 // throw new IllegalArgumentException("Illegal substitution character");
400 status
= U_PARSE_ERROR
;
405 NFSubstitution::NFSubstitution(int32_t _pos
,
406 const NFRuleSet
* _ruleSet
,
407 const UnicodeString
& description
,
409 : pos(_pos
), ruleSet(NULL
), numberFormat(NULL
)
411 // the description should begin and end with the same character.
412 // If it doesn't that's a syntax error. Otherwise,
413 // makeSubstitution() was the only thing that needed to know
414 // about these characters, so strip them off
415 UnicodeString
workingDescription(description
);
416 if (description
.length() >= 2
417 && description
.charAt(0) == description
.charAt(description
.length() - 1))
419 workingDescription
.remove(description
.length() - 1, 1);
420 workingDescription
.remove(0, 1);
422 else if (description
.length() != 0) {
423 // throw new IllegalArgumentException("Illegal substitution syntax");
424 status
= U_PARSE_ERROR
;
428 if (workingDescription
.length() == 0) {
429 // if the description was just two paired token characters
430 // (i.e., "<<" or ">>"), it uses the rule set it belongs to to
432 this->ruleSet
= _ruleSet
;
434 else if (workingDescription
.charAt(0) == gPercent
) {
435 // if the description contains a rule set name, that's the rule
436 // set we use to format the result: get a reference to the
438 this->ruleSet
= _ruleSet
->getOwner()->findRuleSet(workingDescription
, status
);
440 else if (workingDescription
.charAt(0) == gPound
|| workingDescription
.charAt(0) ==gZero
) {
441 // if the description begins with 0 or #, treat it as a
442 // DecimalFormat pattern, and initialize a DecimalFormat with
443 // that pattern (then set it to use the DecimalFormatSymbols
444 // belonging to our formatter)
445 const DecimalFormatSymbols
* sym
= _ruleSet
->getOwner()->getDecimalFormatSymbols();
447 status
= U_MISSING_RESOURCE_ERROR
;
450 DecimalFormat
*tempNumberFormat
= new DecimalFormat(workingDescription
, *sym
, status
);
452 if (!tempNumberFormat
) {
453 status
= U_MEMORY_ALLOCATION_ERROR
;
456 if (U_FAILURE(status
)) {
457 delete tempNumberFormat
;
460 this->numberFormat
= tempNumberFormat
;
462 else if (workingDescription
.charAt(0) == gGreaterThan
) {
463 // if the description is ">>>", this substitution bypasses the
464 // usual rule-search process and always uses the rule that precedes
465 // it in its own rule set's rule list (this is used for place-value
466 // notations: formats where you want to see a particular part of
467 // a number even when it's 0)
469 // this causes problems when >>> is used in a frationalPartSubstitution
470 // this->ruleSet = NULL;
471 this->ruleSet
= _ruleSet
;
472 this->numberFormat
= NULL
;
475 // and of the description is none of these things, it's a syntax error
477 // throw new IllegalArgumentException("Illegal substitution syntax");
478 status
= U_PARSE_ERROR
;
482 NFSubstitution::~NFSubstitution()
489 * Set's the substitution's divisor. Used by NFRule.setBaseValue().
490 * A no-op for all substitutions except multiplier and modulus
492 * @param radix The radix of the divisor
493 * @param exponent The exponent of the divisor
496 NFSubstitution::setDivisor(int32_t /*radix*/, int16_t /*exponent*/, UErrorCode
& /*status*/) {
497 // a no-op for all substitutions except multiplier and modulus substitutions
501 NFSubstitution::setDecimalFormatSymbols(const DecimalFormatSymbols
&newSymbols
, UErrorCode
& /*status*/) {
502 if (numberFormat
!= NULL
) {
503 numberFormat
->setDecimalFormatSymbols(newSymbols
);
507 //-----------------------------------------------------------------------
509 //-----------------------------------------------------------------------
511 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NFSubstitution
)
514 * Compares two substitutions for equality
515 * @param The substitution to compare this one to
516 * @return true if the two substitutions are functionally equivalent
519 NFSubstitution::operator==(const NFSubstitution
& rhs
) const
521 // compare class and all of the fields all substitutions have
523 // this should be called by subclasses before their own equality tests
524 return typeid(*this) == typeid(rhs
)
526 && (ruleSet
== NULL
) == (rhs
.ruleSet
== NULL
)
527 // && ruleSet == rhs.ruleSet causes circularity, other checks to make instead?
528 && (numberFormat
== NULL
529 ? (rhs
.numberFormat
== NULL
)
530 : (*numberFormat
== *rhs
.numberFormat
));
534 * Returns a textual description of the substitution
535 * @return A textual description of the substitution. This might
536 * not be identical to the description it was created from, but
537 * it'll produce the same result.
540 NFSubstitution::toString(UnicodeString
& text
) const
542 // use tokenChar() to get the character at the beginning and
543 // end of the substitutin token. In between them will go
544 // either the name of the rule set it uses, or the pattern of
545 // the DecimalFormat it uses
547 text
.append(tokenChar());
550 if (ruleSet
!= NULL
) {
551 ruleSet
->getName(temp
);
552 } else if (numberFormat
!= NULL
) {
553 numberFormat
->toPattern(temp
);
556 text
.append(tokenChar());
559 //-----------------------------------------------------------------------
561 //-----------------------------------------------------------------------
564 * Performs a mathematical operation on the number, formats it using
565 * either ruleSet or decimalFormat, and inserts the result into
567 * @param number The number being formatted.
568 * @param toInsertInto The string we insert the result into
569 * @param pos The position in toInsertInto where the owning rule's
570 * rule text begins (this value is added to this substitution's
571 * position to determine exactly where to insert the new text)
574 NFSubstitution::doSubstitution(int64_t number
, UnicodeString
& toInsertInto
, int32_t _pos
, int32_t recursionCount
, UErrorCode
& status
) const
576 if (ruleSet
!= NULL
) {
577 // Perform a transformation on the number that is dependent
578 // on the type of substitution this is, then just call its
579 // rule set's format() method to format the result
580 ruleSet
->format(transformNumber(number
), toInsertInto
, _pos
+ this->pos
, recursionCount
, status
);
581 } else if (numberFormat
!= NULL
) {
582 if (number
<= MAX_INT64_IN_DOUBLE
) {
583 // or perform the transformation on the number (preserving
584 // the result's fractional part if the formatter it set
585 // to show it), then use that formatter's format() method
586 // to format the result
587 double numberToFormat
= transformNumber((double)number
);
588 if (numberFormat
->getMaximumFractionDigits() == 0) {
589 numberToFormat
= uprv_floor(numberToFormat
);
593 numberFormat
->format(numberToFormat
, temp
, status
);
594 toInsertInto
.insert(_pos
+ this->pos
, temp
);
597 // We have gone beyond double precision. Something has to give.
598 // We're favoring accuracy of the large number over potential rules
599 // that round like a CompactDecimalFormat, which is not a common use case.
601 // Perform a transformation on the number that is dependent
602 // on the type of substitution this is, then just call its
603 // rule set's format() method to format the result
604 int64_t numberToFormat
= transformNumber(number
);
606 numberFormat
->format(numberToFormat
, temp
, status
);
607 toInsertInto
.insert(_pos
+ this->pos
, temp
);
613 * Performs a mathematical operation on the number, formats it using
614 * either ruleSet or decimalFormat, and inserts the result into
616 * @param number The number being formatted.
617 * @param toInsertInto The string we insert the result into
618 * @param pos The position in toInsertInto where the owning rule's
619 * rule text begins (this value is added to this substitution's
620 * position to determine exactly where to insert the new text)
623 NFSubstitution::doSubstitution(double number
, UnicodeString
& toInsertInto
, int32_t _pos
, int32_t recursionCount
, UErrorCode
& status
) const {
624 // perform a transformation on the number being formatted that
625 // is dependent on the type of substitution this is
626 double numberToFormat
= transformNumber(number
);
628 if (uprv_isInfinite(numberToFormat
)) {
629 // This is probably a minus rule. Combine it with an infinite rule.
630 const NFRule
*infiniteRule
= ruleSet
->findDoubleRule(uprv_getInfinity());
631 infiniteRule
->doFormat(numberToFormat
, toInsertInto
, _pos
+ this->pos
, recursionCount
, status
);
635 // if the result is an integer, from here on out we work in integer
636 // space (saving time and memory and preserving accuracy)
637 if (numberToFormat
== uprv_floor(numberToFormat
) && ruleSet
!= NULL
) {
638 ruleSet
->format(util64_fromDouble(numberToFormat
), toInsertInto
, _pos
+ this->pos
, recursionCount
, status
);
640 // if the result isn't an integer, then call either our rule set's
641 // format() method or our DecimalFormat's format() method to
644 if (ruleSet
!= NULL
) {
645 ruleSet
->format(numberToFormat
, toInsertInto
, _pos
+ this->pos
, recursionCount
, status
);
646 } else if (numberFormat
!= NULL
) {
648 numberFormat
->format(numberToFormat
, temp
);
649 toInsertInto
.insert(_pos
+ this->pos
, temp
);
655 //-----------------------------------------------------------------------
657 //-----------------------------------------------------------------------
664 * Parses a string using the rule set or DecimalFormat belonging
665 * to this substitution. If there's a match, a mathematical
666 * operation (the inverse of the one used in formatting) is
667 * performed on the result of the parse and the value passed in
668 * and returned as the result. The parse position is updated to
669 * point to the first unmatched character in the string.
670 * @param text The string to parse
671 * @param parsePosition On entry, ignored, but assumed to be 0.
672 * On exit, this is updated to point to the first unmatched
673 * character (or 0 if the substitution didn't match)
674 * @param baseValue A partial parse result that should be
675 * combined with the result of this parse
676 * @param upperBound When searching the rule set for a rule
677 * matching the string passed in, only rules with base values
678 * lower than this are considered
679 * @param lenientParse If true and matching against rules fails,
680 * the substitution will also try matching the text against
681 * numerals using a default-costructed NumberFormat. If false,
682 * no extra work is done. (This value is false whenever the
683 * formatter isn't in lenient-parse mode, but is also false
684 * under some conditions even when the formatter _is_ in
685 * lenient-parse mode.)
686 * @return If there's a match, this is the result of composing
687 * baseValue with whatever was returned from matching the
688 * characters. This will be either a Long or a Double. If there's
689 * no match this is new Long(0) (not null), and parsePosition
693 NFSubstitution::doParse(const UnicodeString
& text
,
694 ParsePosition
& parsePosition
,
698 uint32_t nonNumericalExecutedRuleMask
,
699 Formattable
& result
) const
702 fprintf(stderr
, "<nfsubs> %x bv: %g ub: %g\n", this, baseValue
, upperBound
);
704 // figure out the highest base value a rule can have and match
705 // the text being parsed (this varies according to the type of
706 // substitutions: multiplier, modulus, and numerator substitutions
707 // restrict the search to rules with base values lower than their
708 // own; same-value substitutions leave the upper bound wherever
709 // it was, and the others allow any rule to match
710 upperBound
= calcUpperBound(upperBound
);
712 // use our rule set to parse the text. If that fails and
713 // lenient parsing is enabled (this is always false if the
714 // formatter's lenient-parsing mode is off, but it may also
715 // be false even when the formatter's lenient-parse mode is
716 // on), then also try parsing the text using a default-
717 // constructed NumberFormat
718 if (ruleSet
!= NULL
) {
719 ruleSet
->parse(text
, parsePosition
, upperBound
, nonNumericalExecutedRuleMask
, result
);
720 if (lenientParse
&& !ruleSet
->isFractionRuleSet() && parsePosition
.getIndex() == 0) {
721 UErrorCode status
= U_ZERO_ERROR
;
722 NumberFormat
* fmt
= NumberFormat::createInstance(status
);
723 if (U_SUCCESS(status
)) {
724 fmt
->parse(text
, result
, parsePosition
);
729 // ...or use our DecimalFormat to parse the text
730 } else if (numberFormat
!= NULL
) {
731 numberFormat
->parse(text
, result
, parsePosition
);
734 // if the parse was successful, we've already advanced the caller's
735 // parse position (this is the one function that doesn't have one
736 // of its own). Derive a parse result and return it as a Long,
737 // if possible, or a Double
738 if (parsePosition
.getIndex() != 0) {
739 UErrorCode status
= U_ZERO_ERROR
;
740 double tempResult
= result
.getDouble(status
);
742 // composeRuleValue() produces a full parse result from
743 // the partial parse result passed to this function from
744 // the caller (this is either the owning rule's base value
745 // or the partial result obtained from composing the
746 // owning rule's base value with its other substitution's
747 // parse result) and the partial parse result obtained by
748 // matching the substitution (which will be the same value
749 // the caller would get by parsing just this part of the
750 // text with RuleBasedNumberFormat.parse() ). How the two
751 // values are used to derive the full parse result depends
752 // on the types of substitutions: For a regular rule, the
753 // ultimate result is its multiplier substitution's result
754 // times the rule's divisor (or the rule's base value) plus
755 // the modulus substitution's result (which will actually
756 // supersede part of the rule's base value). For a negative-
757 // number rule, the result is the negative of its substitution's
758 // result. For a fraction rule, it's the sum of its two
759 // substitution results. For a rule in a fraction rule set,
760 // it's the numerator substitution's result divided by
761 // the rule's base value. Results from same-value substitutions
762 // propagate back upard, and null substitutions don't affect
764 tempResult
= composeRuleValue(tempResult
, baseValue
);
765 result
.setDouble(tempResult
);
767 // if the parse was UNsuccessful, return 0
775 * Returns true if this is a modulus substitution. (We didn't do this
776 * with instanceof partially because it causes source files to
777 * proliferate and partially because we have to port this to C++.)
778 * @return true if this object is an instance of ModulusSubstitution
781 NFSubstitution::isModulusSubstitution() const {
787 * @return true if this is a decimal format-only substitution
790 NFSubstitution::isDecimalFormatSubstitutionOnly() const {
791 return (ruleSet
== NULL
&& getNumberFormat() != NULL
);
795 * Apple addition, not currently used
796 * @return true if this substitution uses another ruleSet
799 //NFSubstitution::isRuleSetSubstitutionOnly() const {
800 // return (getNumberFormat() == NULL && ruleSet != NULL);
803 //===================================================================
804 // SameValueSubstitution
805 //===================================================================
808 * A substitution that passes the value passed to it through unchanged.
809 * Represented by == in rule descriptions.
811 SameValueSubstitution::SameValueSubstitution(int32_t _pos
,
812 const NFRuleSet
* _ruleSet
,
813 const UnicodeString
& description
,
815 : NFSubstitution(_pos
, _ruleSet
, description
, status
)
817 if (0 == description
.compare(gEqualsEquals
, 2)) {
818 // throw new IllegalArgumentException("== is not a legal token");
819 status
= U_PARSE_ERROR
;
823 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SameValueSubstitution
)
825 //===================================================================
826 // MultiplierSubstitution
827 //===================================================================
829 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MultiplierSubstitution
)
831 UBool
MultiplierSubstitution::operator==(const NFSubstitution
& rhs
) const
833 return NFSubstitution::operator==(rhs
) &&
834 divisor
== ((const MultiplierSubstitution
*)&rhs
)->divisor
;
838 //===================================================================
839 // ModulusSubstitution
840 //===================================================================
843 * A substitution that divides the number being formatted by the its rule's
844 * divisor and formats the remainder. Represented by ">>" in a
847 ModulusSubstitution::ModulusSubstitution(int32_t _pos
,
849 const NFRule
* predecessor
,
850 const NFRuleSet
* _ruleSet
,
851 const UnicodeString
& description
,
853 : NFSubstitution(_pos
, _ruleSet
, description
, status
)
854 , divisor(rule
->getDivisor())
857 // the owning rule's divisor controls the behavior of this
858 // substitution: rather than keeping a backpointer to the rule,
859 // we keep a copy of the divisor
862 status
= U_PARSE_ERROR
;
865 if (0 == description
.compare(gGreaterGreaterGreaterThan
, 3)) {
866 // the >>> token doesn't alter how this substituion calculates the
867 // values it uses for formatting and parsing, but it changes
868 // what's done with that value after it's obtained: >>> short-
869 // circuits the rule-search process and goes straight to the
870 // specified rule to format the substitution value
871 ruleToUse
= predecessor
;
875 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ModulusSubstitution
)
877 UBool
ModulusSubstitution::operator==(const NFSubstitution
& rhs
) const
879 return NFSubstitution::operator==(rhs
) &&
880 divisor
== ((const ModulusSubstitution
*)&rhs
)->divisor
&&
881 ruleToUse
== ((const ModulusSubstitution
*)&rhs
)->ruleToUse
;
884 //-----------------------------------------------------------------------
886 //-----------------------------------------------------------------------
890 * If this is a >>> substitution, use ruleToUse to fill in
891 * the substitution. Otherwise, just use the superclass function.
892 * @param number The number being formatted
893 * @toInsertInto The string to insert the result of this substitution
895 * @param pos The position of the rule text in toInsertInto
898 ModulusSubstitution::doSubstitution(int64_t number
, UnicodeString
& toInsertInto
, int32_t _pos
, int32_t recursionCount
, UErrorCode
& status
) const
900 // if this isn't a >>> substitution, just use the inherited version
901 // of this function (which uses either a rule set or a DecimalFormat
902 // to format its substitution value)
903 if (ruleToUse
== NULL
) {
904 NFSubstitution::doSubstitution(number
, toInsertInto
, _pos
, recursionCount
, status
);
906 // a >>> substitution goes straight to a particular rule to
907 // format the substitution value
909 int64_t numberToFormat
= transformNumber(number
);
910 ruleToUse
->doFormat(numberToFormat
, toInsertInto
, _pos
+ getPos(), recursionCount
, status
);
915 * If this is a >>> substitution, use ruleToUse to fill in
916 * the substitution. Otherwise, just use the superclass function.
917 * @param number The number being formatted
918 * @toInsertInto The string to insert the result of this substitution
920 * @param pos The position of the rule text in toInsertInto
923 ModulusSubstitution::doSubstitution(double number
, UnicodeString
& toInsertInto
, int32_t _pos
, int32_t recursionCount
, UErrorCode
& status
) const
925 // if this isn't a >>> substitution, just use the inherited version
926 // of this function (which uses either a rule set or a DecimalFormat
927 // to format its substitution value)
928 if (ruleToUse
== NULL
) {
929 NFSubstitution::doSubstitution(number
, toInsertInto
, _pos
, recursionCount
, status
);
931 // a >>> substitution goes straight to a particular rule to
932 // format the substitution value
934 double numberToFormat
= transformNumber(number
);
936 ruleToUse
->doFormat(numberToFormat
, toInsertInto
, _pos
+ getPos(), recursionCount
, status
);
940 //-----------------------------------------------------------------------
942 //-----------------------------------------------------------------------
945 * If this is a >>> substitution, match only against ruleToUse.
946 * Otherwise, use the superclass function.
947 * @param text The string to parse
948 * @param parsePosition Ignored on entry, updated on exit to point to
949 * the first unmatched character.
950 * @param baseValue The partial parse result prior to calling this
954 ModulusSubstitution::doParse(const UnicodeString
& text
,
955 ParsePosition
& parsePosition
,
959 uint32_t nonNumericalExecutedRuleMask
,
960 Formattable
& result
) const
962 // if this isn't a >>> substitution, we can just use the
963 // inherited parse() routine to do the parsing
964 if (ruleToUse
== NULL
) {
965 return NFSubstitution::doParse(text
, parsePosition
, baseValue
, upperBound
, lenientParse
, nonNumericalExecutedRuleMask
, result
);
967 // but if it IS a >>> substitution, we have to do it here: we
968 // use the specific rule's doParse() method, and then we have to
969 // do some of the other work of NFRuleSet.parse()
971 ruleToUse
->doParse(text
, parsePosition
, FALSE
, upperBound
, nonNumericalExecutedRuleMask
, result
);
973 if (parsePosition
.getIndex() != 0) {
974 UErrorCode status
= U_ZERO_ERROR
;
975 double tempResult
= result
.getDouble(status
);
976 tempResult
= composeRuleValue(tempResult
, baseValue
);
977 result
.setDouble(tempResult
);
984 * Returns a textual description of the substitution
985 * @return A textual description of the substitution. This might
986 * not be identical to the description it was created from, but
987 * it'll produce the same result.
990 ModulusSubstitution::toString(UnicodeString
& text
) const
992 // use tokenChar() to get the character at the beginning and
993 // end of the substitutin token. In between them will go
994 // either the name of the rule set it uses, or the pattern of
995 // the DecimalFormat it uses
997 if ( ruleToUse
!= NULL
) { // Must have been a >>> substitution.
999 text
.append(tokenChar());
1000 text
.append(tokenChar());
1001 text
.append(tokenChar());
1002 } else { // Otherwise just use the super-class function.
1003 NFSubstitution::toString(text
);
1006 //===================================================================
1007 // IntegralPartSubstitution
1008 //===================================================================
1010 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IntegralPartSubstitution
)
1013 //===================================================================
1014 // FractionalPartSubstitution
1015 //===================================================================
1019 * Constructs a FractionalPartSubstitution. This object keeps a flag
1020 * telling whether it should format by digits or not. In addition,
1021 * it marks the rule set it calls (if any) as a fraction rule set.
1023 FractionalPartSubstitution::FractionalPartSubstitution(int32_t _pos
,
1024 const NFRuleSet
* _ruleSet
,
1025 const UnicodeString
& description
,
1027 : NFSubstitution(_pos
, _ruleSet
, description
, status
)
1032 // akk, ruleSet can change in superclass constructor
1033 if (0 == description
.compare(gGreaterGreaterThan
, 2) ||
1034 0 == description
.compare(gGreaterGreaterGreaterThan
, 3) ||
1035 _ruleSet
== getRuleSet()) {
1037 if (0 == description
.compare(gGreaterGreaterGreaterThan
, 3)) {
1042 ((NFRuleSet
*)getRuleSet())->makeIntoFractionRuleSet();
1046 //-----------------------------------------------------------------------
1048 //-----------------------------------------------------------------------
1051 * If in "by digits" mode, fills in the substitution one decimal digit
1052 * at a time using the rule set containing this substitution.
1053 * Otherwise, uses the superclass function.
1054 * @param number The number being formatted
1055 * @param toInsertInto The string to insert the result of formatting
1056 * the substitution into
1057 * @param pos The position of the owning rule's rule text in
1061 FractionalPartSubstitution::doSubstitution(double number
, UnicodeString
& toInsertInto
,
1062 int32_t _pos
, int32_t recursionCount
, UErrorCode
& status
) const
1064 // if we're not in "byDigits" mode, just use the inherited
1065 // doSubstitution() routine
1067 NFSubstitution::doSubstitution(number
, toInsertInto
, _pos
, recursionCount
, status
);
1069 // if we're in "byDigits" mode, transform the value into an integer
1070 // by moving the decimal point eight places to the right and
1071 // pulling digits off the right one at a time, formatting each digit
1072 // as an integer using this substitution's owning rule set
1073 // (this is slower, but more accurate, than doing it from the
1076 // int32_t numberToFormat = (int32_t)uprv_round(transformNumber(number) * uprv_pow(10, kMaxDecimalDigits));
1077 // // this flag keeps us from formatting trailing zeros. It starts
1078 // // out false because we're pulling from the right, and switches
1079 // // to true the first time we encounter a non-zero digit
1080 // UBool doZeros = FALSE;
1081 // for (int32_t i = 0; i < kMaxDecimalDigits; i++) {
1082 // int64_t digit = numberToFormat % 10;
1083 // if (digit != 0 || doZeros) {
1084 // if (doZeros && useSpaces) {
1085 // toInsertInto.insert(_pos + getPos(), gSpace);
1088 // getRuleSet()->format(digit, toInsertInto, _pos + getPos());
1090 // numberToFormat /= 10;
1094 dl
.setToDouble(number
);
1095 dl
.roundToMagnitude(-20, UNUM_ROUND_HALFEVEN
, status
); // round to 20 fraction digits.
1098 for (int32_t didx
= dl
.getLowerDisplayMagnitude(); didx
<0; didx
++) {
1099 // Loop iterates over fraction digits, starting with the LSD.
1100 // include both real digits from the number, and zeros
1101 // to the left of the MSD but to the right of the decimal point.
1102 if (pad
&& useSpaces
) {
1103 toInsertInto
.insert(_pos
+ getPos(), gSpace
);
1107 int64_t digit
= dl
.getDigit(didx
);
1108 getRuleSet()->format(digit
, toInsertInto
, _pos
+ getPos(), recursionCount
, status
);
1112 // hack around lack of precision in digitlist. if we would end up with
1113 // "foo point" make sure we add a " zero" to the end.
1114 getRuleSet()->format((int64_t)0, toInsertInto
, _pos
+ getPos(), recursionCount
, status
);
1119 //-----------------------------------------------------------------------
1121 //-----------------------------------------------------------------------
1124 * If in "by digits" mode, parses the string as if it were a string
1125 * of individual digits; otherwise, uses the superclass function.
1126 * @param text The string to parse
1127 * @param parsePosition Ignored on entry, but updated on exit to point
1128 * to the first unmatched character
1129 * @param baseValue The partial parse result prior to entering this
1131 * @param upperBound Only consider rules with base values lower than
1132 * this when filling in the substitution
1133 * @param lenientParse If true, try matching the text as numerals if
1134 * matching as words doesn't work
1135 * @return If the match was successful, the current partial parse
1136 * result; otherwise new Long(0). The result is either a Long or
1141 FractionalPartSubstitution::doParse(const UnicodeString
& text
,
1142 ParsePosition
& parsePosition
,
1144 double /*upperBound*/,
1146 uint32_t nonNumericalExecutedRuleMask
,
1147 Formattable
& resVal
) const
1149 // if we're not in byDigits mode, we can just use the inherited
1152 return NFSubstitution::doParse(text
, parsePosition
, baseValue
, 0, lenientParse
, nonNumericalExecutedRuleMask
, resVal
);
1154 // if we ARE in byDigits mode, parse the text one digit at a time
1155 // using this substitution's owning rule set (we do this by setting
1156 // upperBound to 10 when calling doParse() ) until we reach
1159 UnicodeString
workText(text
);
1160 ParsePosition
workPos(1);
1163 // double p10 = 0.1;
1166 int32_t totalDigits
= 0;
1167 NumberFormat
* fmt
= NULL
;
1168 while (workText
.length() > 0 && workPos
.getIndex() != 0) {
1169 workPos
.setIndex(0);
1171 getRuleSet()->parse(workText
, workPos
, 10, nonNumericalExecutedRuleMask
, temp
);
1172 UErrorCode status
= U_ZERO_ERROR
;
1173 digit
= temp
.getLong(status
);
1174 // digit = temp.getType() == Formattable::kLong ?
1176 // (int32_t)temp.getDouble();
1178 if (lenientParse
&& workPos
.getIndex() == 0) {
1180 status
= U_ZERO_ERROR
;
1181 fmt
= NumberFormat::createInstance(status
);
1182 if (U_FAILURE(status
)) {
1188 fmt
->parse(workText
, temp
, workPos
);
1189 digit
= temp
.getLong(status
);
1193 if (workPos
.getIndex() != 0) {
1194 dl
.appendDigit(static_cast<int8_t>(digit
), 0, true);
1196 // result += digit * p10;
1198 parsePosition
.setIndex(parsePosition
.getIndex() + workPos
.getIndex());
1199 workText
.removeBetween(0, workPos
.getIndex());
1200 while (workText
.length() > 0 && workText
.charAt(0) == gSpace
) {
1201 workText
.removeBetween(0, 1);
1202 parsePosition
.setIndex(parsePosition
.getIndex() + 1);
1208 dl
.adjustMagnitude(-totalDigits
);
1209 result
= dl
.toDouble();
1210 result
= composeRuleValue(result
, baseValue
);
1211 resVal
.setDouble(result
);
1217 FractionalPartSubstitution::operator==(const NFSubstitution
& rhs
) const
1219 return NFSubstitution::operator==(rhs
) &&
1220 ((const FractionalPartSubstitution
*)&rhs
)->byDigits
== byDigits
;
1223 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FractionalPartSubstitution
)
1226 //===================================================================
1227 // AbsoluteValueSubstitution
1228 //===================================================================
1230 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(AbsoluteValueSubstitution
)
1232 //===================================================================
1233 // NumeratorSubstitution
1234 //===================================================================
1237 NumeratorSubstitution::doSubstitution(double number
, UnicodeString
& toInsertInto
, int32_t apos
, int32_t recursionCount
, UErrorCode
& status
) const {
1238 // perform a transformation on the number being formatted that
1239 // is dependent on the type of substitution this is
1241 double numberToFormat
= transformNumber(number
);
1242 int64_t longNF
= util64_fromDouble(numberToFormat
);
1244 const NFRuleSet
* aruleSet
= getRuleSet();
1245 if (withZeros
&& aruleSet
!= NULL
) {
1246 // if there are leading zeros in the decimal expansion then emit them
1248 int32_t len
= toInsertInto
.length();
1249 while ((nf
*= 10) < denominator
) {
1250 toInsertInto
.insert(apos
+ getPos(), gSpace
);
1251 aruleSet
->format((int64_t)0, toInsertInto
, apos
+ getPos(), recursionCount
, status
);
1253 apos
+= toInsertInto
.length() - len
;
1256 // if the result is an integer, from here on out we work in integer
1257 // space (saving time and memory and preserving accuracy)
1258 if (numberToFormat
== longNF
&& aruleSet
!= NULL
) {
1259 aruleSet
->format(longNF
, toInsertInto
, apos
+ getPos(), recursionCount
, status
);
1261 // if the result isn't an integer, then call either our rule set's
1262 // format() method or our DecimalFormat's format() method to
1263 // format the result
1265 if (aruleSet
!= NULL
) {
1266 aruleSet
->format(numberToFormat
, toInsertInto
, apos
+ getPos(), recursionCount
, status
);
1269 getNumberFormat()->format(numberToFormat
, temp
, status
);
1270 toInsertInto
.insert(apos
+ getPos(), temp
);
1276 NumeratorSubstitution::doParse(const UnicodeString
& text
,
1277 ParsePosition
& parsePosition
,
1280 UBool
/*lenientParse*/,
1281 uint32_t nonNumericalExecutedRuleMask
,
1282 Formattable
& result
) const
1284 // we don't have to do anything special to do the parsing here,
1285 // but we have to turn lenient parsing off-- if we leave it on,
1286 // it SERIOUSLY messes up the algorithm
1288 // if withZeros is true, we need to count the zeros
1289 // and use that to adjust the parse result
1290 UErrorCode status
= U_ZERO_ERROR
;
1291 int32_t zeroCount
= 0;
1292 UnicodeString
workText(text
);
1295 ParsePosition
workPos(1);
1298 while (workText
.length() > 0 && workPos
.getIndex() != 0) {
1299 workPos
.setIndex(0);
1300 getRuleSet()->parse(workText
, workPos
, 1, nonNumericalExecutedRuleMask
, temp
); // parse zero or nothing at all
1301 if (workPos
.getIndex() == 0) {
1302 // we failed, either there were no more zeros, or the number was formatted with digits
1303 // either way, we're done
1308 parsePosition
.setIndex(parsePosition
.getIndex() + workPos
.getIndex());
1309 workText
.remove(0, workPos
.getIndex());
1310 while (workText
.length() > 0 && workText
.charAt(0) == gSpace
) {
1311 workText
.remove(0, 1);
1312 parsePosition
.setIndex(parsePosition
.getIndex() + 1);
1317 workText
.remove(0, (int32_t)parsePosition
.getIndex());
1318 parsePosition
.setIndex(0);
1321 // we've parsed off the zeros, now let's parse the rest from our current position
1322 NFSubstitution::doParse(workText
, parsePosition
, withZeros
? 1 : baseValue
, upperBound
, FALSE
, nonNumericalExecutedRuleMask
, result
);
1325 // any base value will do in this case. is there a way to
1326 // force this to not bother trying all the base values?
1328 // compute the 'effective' base and prescale the value down
1329 int64_t n
= result
.getLong(status
); // force conversion!
1336 // now add the zeros
1337 while (zeroCount
> 0) {
1341 // d is now our true denominator
1342 result
.setDouble((double)n
/(double)d
);
1349 NumeratorSubstitution::operator==(const NFSubstitution
& rhs
) const
1351 return NFSubstitution::operator==(rhs
) &&
1352 denominator
== ((const NumeratorSubstitution
*)&rhs
)->denominator
;
1355 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NumeratorSubstitution
)
1357 const UChar
NumeratorSubstitution::LTLT
[] = { 0x003c, 0x003c };