• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ******************************************************************************
3 *   Copyright (C) 1997-2015, International Business Machines
4 *   Corporation and others.  All Rights Reserved.
5 ******************************************************************************
6 *   file name:  nfsubs.cpp
7 *   encoding:   US-ASCII
8 *   tab size:   8 (not used)
9 *   indentation:4
10 *
11 * Modification history
12 * Date        Name      Comments
13 * 10/11/2001  Doug      Ported from ICU4J
14 */
15 
16 #include <stdio.h>
17 #include "utypeinfo.h"  // for 'typeid' to work
18 
19 #include "nfsubs.h"
20 #include "digitlst.h"
21 
22 #if U_HAVE_RBNF
23 
24 static const UChar gLessThan = 0x003c;
25 static const UChar gEquals = 0x003d;
26 static const UChar gGreaterThan = 0x003e;
27 static const UChar gPercent = 0x0025;
28 static const UChar gPound = 0x0023;
29 static const UChar gZero = 0x0030;
30 static const UChar gSpace = 0x0020;
31 
32 static const UChar gEqualsEquals[] =
33 {
34     0x3D, 0x3D, 0
35 }; /* "==" */
36 static const UChar gGreaterGreaterGreaterThan[] =
37 {
38     0x3E, 0x3E, 0x3E, 0
39 }; /* ">>>" */
40 static const UChar gGreaterGreaterThan[] =
41 {
42     0x3E, 0x3E, 0
43 }; /* ">>" */
44 
45 U_NAMESPACE_BEGIN
46 
47 class SameValueSubstitution : public NFSubstitution {
48 public:
49     SameValueSubstitution(int32_t pos,
50         const NFRuleSet* ruleset,
51         const UnicodeString& description,
52         UErrorCode& status);
53     virtual ~SameValueSubstitution();
54 
transformNumber(int64_t number) const55     virtual int64_t transformNumber(int64_t number) const { return number; }
transformNumber(double number) const56     virtual double transformNumber(double number) const { return number; }
composeRuleValue(double newRuleValue,double) const57     virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const { return newRuleValue; }
calcUpperBound(double oldUpperBound) const58     virtual double calcUpperBound(double oldUpperBound) const { return oldUpperBound; }
tokenChar() const59     virtual UChar tokenChar() const { return (UChar)0x003d; } // '='
60 
61 public:
62     static UClassID getStaticClassID(void);
63     virtual UClassID getDynamicClassID(void) const;
64 };
65 
~SameValueSubstitution()66 SameValueSubstitution::~SameValueSubstitution() {}
67 
68 class MultiplierSubstitution : public NFSubstitution {
69     double divisor;
70     int64_t ldivisor;
71 
72 public:
MultiplierSubstitution(int32_t _pos,double _divisor,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)73     MultiplierSubstitution(int32_t _pos,
74         double _divisor,
75         const NFRuleSet* _ruleSet,
76         const UnicodeString& description,
77         UErrorCode& status)
78         : NFSubstitution(_pos, _ruleSet, description, status), divisor(_divisor)
79     {
80         ldivisor = util64_fromDouble(divisor);
81         if (divisor == 0) {
82             status = U_PARSE_ERROR;
83         }
84     }
85     virtual ~MultiplierSubstitution();
86 
setDivisor(int32_t radix,int32_t exponent,UErrorCode & status)87     virtual void setDivisor(int32_t radix, int32_t exponent, UErrorCode& status) {
88         divisor = uprv_pow(radix, exponent);
89         ldivisor = util64_fromDouble(divisor);
90 
91         if(divisor == 0) {
92             status = U_PARSE_ERROR;
93         }
94     }
95 
96     virtual UBool operator==(const NFSubstitution& rhs) const;
97 
transformNumber(int64_t number) const98     virtual int64_t transformNumber(int64_t number) const {
99         return number / ldivisor;
100     }
101 
transformNumber(double number) const102     virtual double transformNumber(double number) const {
103         if (getRuleSet()) {
104             return uprv_floor(number / divisor);
105         } else {
106             return number/divisor;
107         }
108     }
109 
composeRuleValue(double newRuleValue,double) const110     virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const {
111         return newRuleValue * divisor;
112     }
113 
calcUpperBound(double) const114     virtual double calcUpperBound(double /*oldUpperBound*/) const { return divisor; }
115 
tokenChar() const116     virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
117 
118 public:
119     static UClassID getStaticClassID(void);
120     virtual UClassID getDynamicClassID(void) const;
121 };
122 
~MultiplierSubstitution()123 MultiplierSubstitution::~MultiplierSubstitution() {}
124 
125 class ModulusSubstitution : public NFSubstitution {
126     double divisor;
127     int64_t  ldivisor;
128     const NFRule* ruleToUse;
129 public:
130     ModulusSubstitution(int32_t pos,
131         double _divisor,
132         const NFRule* rulePredecessor,
133         const NFRuleSet* ruleSet,
134         const UnicodeString& description,
135         UErrorCode& status);
136     virtual ~ModulusSubstitution();
137 
setDivisor(int32_t radix,int32_t exponent,UErrorCode & status)138     virtual void setDivisor(int32_t radix, int32_t exponent, UErrorCode& status) {
139         divisor = uprv_pow(radix, exponent);
140         ldivisor = util64_fromDouble(divisor);
141 
142         if (divisor == 0) {
143             status = U_PARSE_ERROR;
144         }
145     }
146 
147     virtual UBool operator==(const NFSubstitution& rhs) const;
148 
149     virtual void doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
150     virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
151 
transformNumber(int64_t number) const152     virtual int64_t transformNumber(int64_t number) const { return number % ldivisor; }
transformNumber(double number) const153     virtual double transformNumber(double number) const { return uprv_fmod(number, divisor); }
154 
155     virtual UBool doParse(const UnicodeString& text,
156         ParsePosition& parsePosition,
157         double baseValue,
158         double upperBound,
159         UBool lenientParse,
160         Formattable& result) const;
161 
composeRuleValue(double newRuleValue,double oldRuleValue) const162     virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const {
163         return oldRuleValue - uprv_fmod(oldRuleValue, divisor) + newRuleValue;
164     }
165 
calcUpperBound(double) const166     virtual double calcUpperBound(double /*oldUpperBound*/) const { return divisor; }
167 
isModulusSubstitution() const168     virtual UBool isModulusSubstitution() const { return TRUE; }
169 
tokenChar() const170     virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
171 
172 	virtual void toString(UnicodeString& result) const;
173 
174 public:
175     static UClassID getStaticClassID(void);
176     virtual UClassID getDynamicClassID(void) const;
177 };
178 
~ModulusSubstitution()179 ModulusSubstitution::~ModulusSubstitution() {}
180 
181 class IntegralPartSubstitution : public NFSubstitution {
182 public:
IntegralPartSubstitution(int32_t _pos,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)183     IntegralPartSubstitution(int32_t _pos,
184         const NFRuleSet* _ruleSet,
185         const UnicodeString& description,
186         UErrorCode& status)
187         : NFSubstitution(_pos, _ruleSet, description, status) {}
188     virtual ~IntegralPartSubstitution();
189 
transformNumber(int64_t number) const190     virtual int64_t transformNumber(int64_t number) const { return number; }
transformNumber(double number) const191     virtual double transformNumber(double number) const { return uprv_floor(number); }
composeRuleValue(double newRuleValue,double oldRuleValue) const192     virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue + oldRuleValue; }
calcUpperBound(double) const193     virtual double calcUpperBound(double /*oldUpperBound*/) const { return DBL_MAX; }
tokenChar() const194     virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
195 
196 public:
197     static UClassID getStaticClassID(void);
198     virtual UClassID getDynamicClassID(void) const;
199 };
200 
~IntegralPartSubstitution()201 IntegralPartSubstitution::~IntegralPartSubstitution() {}
202 
203 class FractionalPartSubstitution : public NFSubstitution {
204     UBool byDigits;
205     UBool useSpaces;
206     enum { kMaxDecimalDigits = 8 };
207 public:
208     FractionalPartSubstitution(int32_t pos,
209         const NFRuleSet* ruleSet,
210         const UnicodeString& description,
211         UErrorCode& status);
212     virtual ~FractionalPartSubstitution();
213 
214     virtual UBool operator==(const NFSubstitution& rhs) const;
215 
216     virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
doSubstitution(int64_t,UnicodeString &,int32_t,int32_t,UErrorCode &) const217     virtual void doSubstitution(int64_t /*number*/, UnicodeString& /*toInsertInto*/, int32_t /*_pos*/, int32_t /*recursionCount*/, UErrorCode& /*status*/) const {}
transformNumber(int64_t) const218     virtual int64_t transformNumber(int64_t /*number*/) const { return 0; }
transformNumber(double number) const219     virtual double transformNumber(double number) const { return number - uprv_floor(number); }
220 
221     virtual UBool doParse(const UnicodeString& text,
222         ParsePosition& parsePosition,
223         double baseValue,
224         double upperBound,
225         UBool lenientParse,
226         Formattable& result) const;
227 
composeRuleValue(double newRuleValue,double oldRuleValue) const228     virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue + oldRuleValue; }
calcUpperBound(double) const229     virtual double calcUpperBound(double /*oldUpperBound*/) const { return 0.0; }
tokenChar() const230     virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
231 
232 public:
233     static UClassID getStaticClassID(void);
234     virtual UClassID getDynamicClassID(void) const;
235 };
236 
~FractionalPartSubstitution()237 FractionalPartSubstitution::~FractionalPartSubstitution() {}
238 
239 class AbsoluteValueSubstitution : public NFSubstitution {
240 public:
AbsoluteValueSubstitution(int32_t _pos,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)241     AbsoluteValueSubstitution(int32_t _pos,
242         const NFRuleSet* _ruleSet,
243         const UnicodeString& description,
244         UErrorCode& status)
245         : NFSubstitution(_pos, _ruleSet, description, status) {}
246     virtual ~AbsoluteValueSubstitution();
247 
transformNumber(int64_t number) const248     virtual int64_t transformNumber(int64_t number) const { return number >= 0 ? number : -number; }
transformNumber(double number) const249     virtual double transformNumber(double number) const { return uprv_fabs(number); }
composeRuleValue(double newRuleValue,double) const250     virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const { return -newRuleValue; }
calcUpperBound(double) const251     virtual double calcUpperBound(double /*oldUpperBound*/) const { return DBL_MAX; }
tokenChar() const252     virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
253 
254 public:
255     static UClassID getStaticClassID(void);
256     virtual UClassID getDynamicClassID(void) const;
257 };
258 
~AbsoluteValueSubstitution()259 AbsoluteValueSubstitution::~AbsoluteValueSubstitution() {}
260 
261 class NumeratorSubstitution : public NFSubstitution {
262     double denominator;
263     int64_t ldenominator;
264     UBool withZeros;
265 public:
fixdesc(const UnicodeString & desc)266     static inline UnicodeString fixdesc(const UnicodeString& desc) {
267         if (desc.endsWith(LTLT, 2)) {
268             UnicodeString result(desc, 0, desc.length()-1);
269             return result;
270         }
271         return desc;
272     }
NumeratorSubstitution(int32_t _pos,double _denominator,NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)273     NumeratorSubstitution(int32_t _pos,
274         double _denominator,
275         NFRuleSet* _ruleSet,
276         const UnicodeString& description,
277         UErrorCode& status)
278         : NFSubstitution(_pos, _ruleSet, fixdesc(description), status), denominator(_denominator)
279     {
280         ldenominator = util64_fromDouble(denominator);
281         withZeros = description.endsWith(LTLT, 2);
282     }
283     virtual ~NumeratorSubstitution();
284 
285     virtual UBool operator==(const NFSubstitution& rhs) const;
286 
transformNumber(int64_t number) const287     virtual int64_t transformNumber(int64_t number) const { return number * ldenominator; }
transformNumber(double number) const288     virtual double transformNumber(double number) const { return uprv_round(number * denominator); }
289 
doSubstitution(int64_t,UnicodeString &,int32_t,int32_t,UErrorCode &) const290     virtual void doSubstitution(int64_t /*number*/, UnicodeString& /*toInsertInto*/, int32_t /*_pos*/, int32_t /*recursionCount*/, UErrorCode& /*status*/) const {}
291     virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
292     virtual UBool doParse(const UnicodeString& text,
293         ParsePosition& parsePosition,
294         double baseValue,
295         double upperBound,
296         UBool /*lenientParse*/,
297         Formattable& result) const;
298 
composeRuleValue(double newRuleValue,double oldRuleValue) const299     virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue / oldRuleValue; }
calcUpperBound(double) const300     virtual double calcUpperBound(double /*oldUpperBound*/) const { return denominator; }
tokenChar() const301     virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
302 private:
303     static const UChar LTLT[2];
304 
305 public:
306     static UClassID getStaticClassID(void);
307     virtual UClassID getDynamicClassID(void) const;
308 };
309 
~NumeratorSubstitution()310 NumeratorSubstitution::~NumeratorSubstitution() {}
311 
312 NFSubstitution*
makeSubstitution(int32_t pos,const NFRule * rule,const NFRule * predecessor,const NFRuleSet * ruleSet,const RuleBasedNumberFormat * formatter,const UnicodeString & description,UErrorCode & status)313 NFSubstitution::makeSubstitution(int32_t pos,
314                                  const NFRule* rule,
315                                  const NFRule* predecessor,
316                                  const NFRuleSet* ruleSet,
317                                  const RuleBasedNumberFormat* formatter,
318                                  const UnicodeString& description,
319                                  UErrorCode& status)
320 {
321     // if the description is empty, return a NullSubstitution
322     if (description.length() == 0) {
323         return NULL;
324     }
325 
326     switch (description.charAt(0)) {
327         // if the description begins with '<'...
328     case gLessThan:
329         // throw an exception if the rule is a negative number
330         // rule
331         if (rule->getBaseValue() == NFRule::kNegativeNumberRule) {
332             // throw new IllegalArgumentException("<< not allowed in negative-number rule");
333             status = U_PARSE_ERROR;
334             return NULL;
335         }
336 
337         // if the rule is a fraction rule, return an
338         // IntegralPartSubstitution
339         else if (rule->getBaseValue() == NFRule::kImproperFractionRule
340             || rule->getBaseValue() == NFRule::kProperFractionRule
341             || rule->getBaseValue() == NFRule::kMasterRule) {
342             return new IntegralPartSubstitution(pos, ruleSet, description, status);
343         }
344 
345         // if the rule set containing the rule is a fraction
346         // rule set, return a NumeratorSubstitution
347         else if (ruleSet->isFractionRuleSet()) {
348             return new NumeratorSubstitution(pos, (double)rule->getBaseValue(),
349                 formatter->getDefaultRuleSet(), description, status);
350         }
351 
352         // otherwise, return a MultiplierSubstitution
353         else {
354             return new MultiplierSubstitution(pos, rule->getDivisor(), ruleSet,
355                 description, status);
356         }
357 
358         // if the description begins with '>'...
359     case gGreaterThan:
360         // if the rule is a negative-number rule, return
361         // an AbsoluteValueSubstitution
362         if (rule->getBaseValue() == NFRule::kNegativeNumberRule) {
363             return new AbsoluteValueSubstitution(pos, ruleSet, description, status);
364         }
365 
366         // if the rule is a fraction rule, return a
367         // FractionalPartSubstitution
368         else if (rule->getBaseValue() == NFRule::kImproperFractionRule
369             || rule->getBaseValue() == NFRule::kProperFractionRule
370             || rule->getBaseValue() == NFRule::kMasterRule) {
371             return new FractionalPartSubstitution(pos, ruleSet, description, status);
372         }
373 
374         // if the rule set owning the rule is a fraction rule set,
375         // throw an exception
376         else if (ruleSet->isFractionRuleSet()) {
377             // throw new IllegalArgumentException(">> not allowed in fraction rule set");
378             status = U_PARSE_ERROR;
379             return NULL;
380         }
381 
382         // otherwise, return a ModulusSubstitution
383         else {
384             return new ModulusSubstitution(pos, rule->getDivisor(), predecessor,
385                 ruleSet, description, status);
386         }
387 
388         // if the description begins with '=', always return a
389         // SameValueSubstitution
390     case gEquals:
391         return new SameValueSubstitution(pos, ruleSet, description, status);
392 
393         // and if it's anything else, throw an exception
394     default:
395         // throw new IllegalArgumentException("Illegal substitution character");
396         status = U_PARSE_ERROR;
397     }
398     return NULL;
399 }
400 
NFSubstitution(int32_t _pos,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)401 NFSubstitution::NFSubstitution(int32_t _pos,
402                                const NFRuleSet* _ruleSet,
403                                const UnicodeString& description,
404                                UErrorCode& status)
405                                : pos(_pos), ruleSet(NULL), numberFormat(NULL)
406 {
407     // the description should begin and end with the same character.
408     // If it doesn't that's a syntax error.  Otherwise,
409     // makeSubstitution() was the only thing that needed to know
410     // about these characters, so strip them off
411     UnicodeString workingDescription(description);
412     if (description.length() >= 2
413         && description.charAt(0) == description.charAt(description.length() - 1))
414     {
415         workingDescription.remove(description.length() - 1, 1);
416         workingDescription.remove(0, 1);
417     }
418     else if (description.length() != 0) {
419         // throw new IllegalArgumentException("Illegal substitution syntax");
420         status = U_PARSE_ERROR;
421         return;
422     }
423 
424     if (workingDescription.length() == 0) {
425         // if the description was just two paired token characters
426         // (i.e., "<<" or ">>"), it uses the rule set it belongs to to
427         // format its result
428         this->ruleSet = _ruleSet;
429     }
430     else if (workingDescription.charAt(0) == gPercent) {
431         // if the description contains a rule set name, that's the rule
432         // set we use to format the result: get a reference to the
433         // names rule set
434         this->ruleSet = _ruleSet->getOwner()->findRuleSet(workingDescription, status);
435     }
436     else if (workingDescription.charAt(0) == gPound || workingDescription.charAt(0) ==gZero) {
437         // if the description begins with 0 or #, treat it as a
438         // DecimalFormat pattern, and initialize a DecimalFormat with
439         // that pattern (then set it to use the DecimalFormatSymbols
440         // belonging to our formatter)
441         const DecimalFormatSymbols* sym = _ruleSet->getOwner()->getDecimalFormatSymbols();
442         if (!sym) {
443             status = U_MISSING_RESOURCE_ERROR;
444             return;
445         }
446         DecimalFormat *tempNumberFormat = new DecimalFormat(workingDescription, *sym, status);
447         /* test for NULL */
448         if (!tempNumberFormat) {
449             status = U_MEMORY_ALLOCATION_ERROR;
450             return;
451         }
452         if (U_FAILURE(status)) {
453             delete tempNumberFormat;
454             return;
455         }
456         this->numberFormat = tempNumberFormat;
457     }
458     else if (workingDescription.charAt(0) == gGreaterThan) {
459         // if the description is ">>>", this substitution bypasses the
460         // usual rule-search process and always uses the rule that precedes
461         // it in its own rule set's rule list (this is used for place-value
462         // notations: formats where you want to see a particular part of
463         // a number even when it's 0)
464 
465         // this causes problems when >>> is used in a frationalPartSubstitution
466         // this->ruleSet = NULL;
467         this->ruleSet = _ruleSet;
468         this->numberFormat = NULL;
469     }
470     else {
471         // and of the description is none of these things, it's a syntax error
472 
473         // throw new IllegalArgumentException("Illegal substitution syntax");
474         status = U_PARSE_ERROR;
475     }
476 }
477 
~NFSubstitution()478 NFSubstitution::~NFSubstitution()
479 {
480     delete numberFormat;
481     numberFormat = NULL;
482 }
483 
484 /**
485  * Set's the substitution's divisor.  Used by NFRule.setBaseValue().
486  * A no-op for all substitutions except multiplier and modulus
487  * substitutions.
488  * @param radix The radix of the divisor
489  * @param exponent The exponent of the divisor
490  */
491 void
setDivisor(int32_t,int32_t,UErrorCode &)492 NFSubstitution::setDivisor(int32_t /*radix*/, int32_t /*exponent*/, UErrorCode& /*status*/) {
493   // a no-op for all substitutions except multiplier and modulus substitutions
494 }
495 
496 void
setDecimalFormatSymbols(const DecimalFormatSymbols & newSymbols,UErrorCode &)497 NFSubstitution::setDecimalFormatSymbols(const DecimalFormatSymbols &newSymbols, UErrorCode& /*status*/) {
498     if (numberFormat != NULL) {
499         numberFormat->setDecimalFormatSymbols(newSymbols);
500     }
501 }
502 
503 //-----------------------------------------------------------------------
504 // boilerplate
505 //-----------------------------------------------------------------------
506 
507 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NFSubstitution)
508 
509 /**
510  * Compares two substitutions for equality
511  * @param The substitution to compare this one to
512  * @return true if the two substitutions are functionally equivalent
513  */
514 UBool
515 NFSubstitution::operator==(const NFSubstitution& rhs) const
516 {
517   // compare class and all of the fields all substitutions have
518   // in common
519   // this should be called by subclasses before their own equality tests
520   return typeid(*this) == typeid(rhs)
521   && pos == rhs.pos
522   && (ruleSet == NULL) == (rhs.ruleSet == NULL)
523   // && ruleSet == rhs.ruleSet causes circularity, other checks to make instead?
524   && (numberFormat == NULL
525       ? (rhs.numberFormat == NULL)
526       : (*numberFormat == *rhs.numberFormat));
527 }
528 
529 /**
530  * Returns a textual description of the substitution
531  * @return A textual description of the substitution.  This might
532  * not be identical to the description it was created from, but
533  * it'll produce the same result.
534  */
535 void
toString(UnicodeString & text) const536 NFSubstitution::toString(UnicodeString& text) const
537 {
538   // use tokenChar() to get the character at the beginning and
539   // end of the substitutin token.  In between them will go
540   // either the name of the rule set it uses, or the pattern of
541   // the DecimalFormat it uses
542   text.remove();
543   text.append(tokenChar());
544 
545   UnicodeString temp;
546   if (ruleSet != NULL) {
547     ruleSet->getName(temp);
548   } else if (numberFormat != NULL) {
549     numberFormat->toPattern(temp);
550   }
551   text.append(temp);
552   text.append(tokenChar());
553 }
554 
555 //-----------------------------------------------------------------------
556 // formatting
557 //-----------------------------------------------------------------------
558 
559 /**
560  * Performs a mathematical operation on the number, formats it using
561  * either ruleSet or decimalFormat, and inserts the result into
562  * toInsertInto.
563  * @param number The number being formatted.
564  * @param toInsertInto The string we insert the result into
565  * @param pos The position in toInsertInto where the owning rule's
566  * rule text begins (this value is added to this substitution's
567  * position to determine exactly where to insert the new text)
568  */
569 void
doSubstitution(int64_t number,UnicodeString & toInsertInto,int32_t _pos,int32_t recursionCount,UErrorCode & status) const570 NFSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
571 {
572     if (ruleSet != NULL) {
573         // perform a transformation on the number that is dependent
574         // on the type of substitution this is, then just call its
575         // rule set's format() method to format the result
576         ruleSet->format(transformNumber(number), toInsertInto, _pos + this->pos, recursionCount, status);
577     } else if (numberFormat != NULL) {
578         // or perform the transformation on the number (preserving
579         // the result's fractional part if the formatter it set
580         // to show it), then use that formatter's format() method
581         // to format the result
582         double numberToFormat = transformNumber((double)number);
583         if (numberFormat->getMaximumFractionDigits() == 0) {
584             numberToFormat = uprv_floor(numberToFormat);
585         }
586 
587         UnicodeString temp;
588         numberFormat->format(numberToFormat, temp, status);
589         toInsertInto.insert(_pos + this->pos, temp);
590     }
591 }
592 
593 /**
594  * Performs a mathematical operation on the number, formats it using
595  * either ruleSet or decimalFormat, and inserts the result into
596  * toInsertInto.
597  * @param number The number being formatted.
598  * @param toInsertInto The string we insert the result into
599  * @param pos The position in toInsertInto where the owning rule's
600  * rule text begins (this value is added to this substitution's
601  * position to determine exactly where to insert the new text)
602  */
603 void
doSubstitution(double number,UnicodeString & toInsertInto,int32_t _pos,int32_t recursionCount,UErrorCode & status) const604 NFSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const {
605     // perform a transformation on the number being formatted that
606     // is dependent on the type of substitution this is
607     double numberToFormat = transformNumber(number);
608 
609     if (uprv_isInfinite(numberToFormat)) {
610         // This is probably a minus rule. Combine it with an infinite rule.
611         const NFRule *infiniteRule = ruleSet->findDoubleRule(uprv_getInfinity());
612         infiniteRule->doFormat(numberToFormat, toInsertInto, _pos + this->pos, recursionCount, status);
613         return;
614     }
615 
616     // if the result is an integer, from here on out we work in integer
617     // space (saving time and memory and preserving accuracy)
618     if (numberToFormat == uprv_floor(numberToFormat) && ruleSet != NULL) {
619         ruleSet->format(util64_fromDouble(numberToFormat), toInsertInto, _pos + this->pos, recursionCount, status);
620 
621         // if the result isn't an integer, then call either our rule set's
622         // format() method or our DecimalFormat's format() method to
623         // format the result
624     } else {
625         if (ruleSet != NULL) {
626             ruleSet->format(numberToFormat, toInsertInto, _pos + this->pos, recursionCount, status);
627         } else if (numberFormat != NULL) {
628             UnicodeString temp;
629             numberFormat->format(numberToFormat, temp);
630             toInsertInto.insert(_pos + this->pos, temp);
631         }
632     }
633 }
634 
635 
636     //-----------------------------------------------------------------------
637     // parsing
638     //-----------------------------------------------------------------------
639 
640 #ifdef RBNF_DEBUG
641 #include <stdio.h>
642 #endif
643 
644 /**
645  * Parses a string using the rule set or DecimalFormat belonging
646  * to this substitution.  If there's a match, a mathematical
647  * operation (the inverse of the one used in formatting) is
648  * performed on the result of the parse and the value passed in
649  * and returned as the result.  The parse position is updated to
650  * point to the first unmatched character in the string.
651  * @param text The string to parse
652  * @param parsePosition On entry, ignored, but assumed to be 0.
653  * On exit, this is updated to point to the first unmatched
654  * character (or 0 if the substitution didn't match)
655  * @param baseValue A partial parse result that should be
656  * combined with the result of this parse
657  * @param upperBound When searching the rule set for a rule
658  * matching the string passed in, only rules with base values
659  * lower than this are considered
660  * @param lenientParse If true and matching against rules fails,
661  * the substitution will also try matching the text against
662  * numerals using a default-costructed NumberFormat.  If false,
663  * no extra work is done.  (This value is false whenever the
664  * formatter isn't in lenient-parse mode, but is also false
665  * under some conditions even when the formatter _is_ in
666  * lenient-parse mode.)
667  * @return If there's a match, this is the result of composing
668  * baseValue with whatever was returned from matching the
669  * characters.  This will be either a Long or a Double.  If there's
670  * no match this is new Long(0) (not null), and parsePosition
671  * is left unchanged.
672  */
673 UBool
doParse(const UnicodeString & text,ParsePosition & parsePosition,double baseValue,double upperBound,UBool lenientParse,Formattable & result) const674 NFSubstitution::doParse(const UnicodeString& text,
675                         ParsePosition& parsePosition,
676                         double baseValue,
677                         double upperBound,
678                         UBool lenientParse,
679                         Formattable& result) const
680 {
681 #ifdef RBNF_DEBUG
682     fprintf(stderr, "<nfsubs> %x bv: %g ub: %g\n", this, baseValue, upperBound);
683 #endif
684     // figure out the highest base value a rule can have and match
685     // the text being parsed (this varies according to the type of
686     // substitutions: multiplier, modulus, and numerator substitutions
687     // restrict the search to rules with base values lower than their
688     // own; same-value substitutions leave the upper bound wherever
689     // it was, and the others allow any rule to match
690     upperBound = calcUpperBound(upperBound);
691 
692     // use our rule set to parse the text.  If that fails and
693     // lenient parsing is enabled (this is always false if the
694     // formatter's lenient-parsing mode is off, but it may also
695     // be false even when the formatter's lenient-parse mode is
696     // on), then also try parsing the text using a default-
697     // constructed NumberFormat
698     if (ruleSet != NULL) {
699         ruleSet->parse(text, parsePosition, upperBound, result);
700         if (lenientParse && !ruleSet->isFractionRuleSet() && parsePosition.getIndex() == 0) {
701             UErrorCode status = U_ZERO_ERROR;
702             NumberFormat* fmt = NumberFormat::createInstance(status);
703             if (U_SUCCESS(status)) {
704                 fmt->parse(text, result, parsePosition);
705             }
706             delete fmt;
707         }
708 
709         // ...or use our DecimalFormat to parse the text
710     } else if (numberFormat != NULL) {
711         numberFormat->parse(text, result, parsePosition);
712     }
713 
714     // if the parse was successful, we've already advanced the caller's
715     // parse position (this is the one function that doesn't have one
716     // of its own).  Derive a parse result and return it as a Long,
717     // if possible, or a Double
718     if (parsePosition.getIndex() != 0) {
719         UErrorCode status = U_ZERO_ERROR;
720         double tempResult = result.getDouble(status);
721 
722         // composeRuleValue() produces a full parse result from
723         // the partial parse result passed to this function from
724         // the caller (this is either the owning rule's base value
725         // or the partial result obtained from composing the
726         // owning rule's base value with its other substitution's
727         // parse result) and the partial parse result obtained by
728         // matching the substitution (which will be the same value
729         // the caller would get by parsing just this part of the
730         // text with RuleBasedNumberFormat.parse() ).  How the two
731         // values are used to derive the full parse result depends
732         // on the types of substitutions: For a regular rule, the
733         // ultimate result is its multiplier substitution's result
734         // times the rule's divisor (or the rule's base value) plus
735         // the modulus substitution's result (which will actually
736         // supersede part of the rule's base value).  For a negative-
737         // number rule, the result is the negative of its substitution's
738         // result.  For a fraction rule, it's the sum of its two
739         // substitution results.  For a rule in a fraction rule set,
740         // it's the numerator substitution's result divided by
741         // the rule's base value.  Results from same-value substitutions
742         // propagate back upard, and null substitutions don't affect
743         // the result.
744         tempResult = composeRuleValue(tempResult, baseValue);
745         result.setDouble(tempResult);
746         return TRUE;
747         // if the parse was UNsuccessful, return 0
748     } else {
749         result.setLong(0);
750         return FALSE;
751     }
752 }
753 
754     /**
755      * Returns true if this is a modulus substitution.  (We didn't do this
756      * with instanceof partially because it causes source files to
757      * proliferate and partially because we have to port this to C++.)
758      * @return true if this object is an instance of ModulusSubstitution
759      */
760 UBool
isModulusSubstitution() const761 NFSubstitution::isModulusSubstitution() const {
762     return FALSE;
763 }
764 
765 //===================================================================
766 // SameValueSubstitution
767 //===================================================================
768 
769 /**
770  * A substitution that passes the value passed to it through unchanged.
771  * Represented by == in rule descriptions.
772  */
SameValueSubstitution(int32_t _pos,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)773 SameValueSubstitution::SameValueSubstitution(int32_t _pos,
774                         const NFRuleSet* _ruleSet,
775                         const UnicodeString& description,
776                         UErrorCode& status)
777 : NFSubstitution(_pos, _ruleSet, description, status)
778 {
779     if (0 == description.compare(gEqualsEquals, 2)) {
780         // throw new IllegalArgumentException("== is not a legal token");
781         status = U_PARSE_ERROR;
782     }
783 }
784 
785 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SameValueSubstitution)
786 
787 //===================================================================
788 // MultiplierSubstitution
789 //===================================================================
790 
791 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MultiplierSubstitution)
792 
793 UBool MultiplierSubstitution::operator==(const NFSubstitution& rhs) const
794 {
795     return NFSubstitution::operator==(rhs) &&
796         divisor == ((const MultiplierSubstitution*)&rhs)->divisor;
797 }
798 
799 
800 //===================================================================
801 // ModulusSubstitution
802 //===================================================================
803 
804 /**
805  * A substitution that divides the number being formatted by the its rule's
806  * divisor and formats the remainder.  Represented by "&gt;&gt;" in a
807  * regular rule.
808  */
ModulusSubstitution(int32_t _pos,double _divisor,const NFRule * predecessor,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)809 ModulusSubstitution::ModulusSubstitution(int32_t _pos,
810                                          double _divisor,
811                                          const NFRule* predecessor,
812                                          const NFRuleSet* _ruleSet,
813                                          const UnicodeString& description,
814                                          UErrorCode& status)
815  : NFSubstitution(_pos, _ruleSet, description, status)
816  , divisor(_divisor)
817  , ruleToUse(NULL)
818 {
819   ldivisor = util64_fromDouble(_divisor);
820 
821   // the owning rule's divisor controls the behavior of this
822   // substitution: rather than keeping a backpointer to the rule,
823   // we keep a copy of the divisor
824 
825   if (ldivisor == 0) {
826       status = U_PARSE_ERROR;
827   }
828 
829   if (0 == description.compare(gGreaterGreaterGreaterThan, 3)) {
830     // the >>> token doesn't alter how this substituion calculates the
831     // values it uses for formatting and parsing, but it changes
832     // what's done with that value after it's obtained: >>> short-
833     // circuits the rule-search process and goes straight to the
834     // specified rule to format the substitution value
835     ruleToUse = predecessor;
836   }
837 }
838 
839 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ModulusSubstitution)
840 
841 UBool ModulusSubstitution::operator==(const NFSubstitution& rhs) const
842 {
843   return NFSubstitution::operator==(rhs) &&
844   divisor == ((const ModulusSubstitution*)&rhs)->divisor &&
845   ruleToUse == ((const ModulusSubstitution*)&rhs)->ruleToUse;
846 }
847 
848 //-----------------------------------------------------------------------
849 // formatting
850 //-----------------------------------------------------------------------
851 
852 
853 /**
854  * If this is a &gt;&gt;&gt; substitution, use ruleToUse to fill in
855  * the substitution.  Otherwise, just use the superclass function.
856  * @param number The number being formatted
857  * @toInsertInto The string to insert the result of this substitution
858  * into
859  * @param pos The position of the rule text in toInsertInto
860  */
861 void
doSubstitution(int64_t number,UnicodeString & toInsertInto,int32_t _pos,int32_t recursionCount,UErrorCode & status) const862 ModulusSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
863 {
864     // if this isn't a >>> substitution, just use the inherited version
865     // of this function (which uses either a rule set or a DecimalFormat
866     // to format its substitution value)
867     if (ruleToUse == NULL) {
868         NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
869 
870         // a >>> substitution goes straight to a particular rule to
871         // format the substitution value
872     } else {
873         int64_t numberToFormat = transformNumber(number);
874         ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos(), recursionCount, status);
875     }
876 }
877 
878 /**
879 * If this is a &gt;&gt;&gt; substitution, use ruleToUse to fill in
880 * the substitution.  Otherwise, just use the superclass function.
881 * @param number The number being formatted
882 * @toInsertInto The string to insert the result of this substitution
883 * into
884 * @param pos The position of the rule text in toInsertInto
885 */
886 void
doSubstitution(double number,UnicodeString & toInsertInto,int32_t _pos,int32_t recursionCount,UErrorCode & status) const887 ModulusSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
888 {
889     // if this isn't a >>> substitution, just use the inherited version
890     // of this function (which uses either a rule set or a DecimalFormat
891     // to format its substitution value)
892     if (ruleToUse == NULL) {
893         NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
894 
895         // a >>> substitution goes straight to a particular rule to
896         // format the substitution value
897     } else {
898         double numberToFormat = transformNumber(number);
899 
900         ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos(), recursionCount, status);
901     }
902 }
903 
904 //-----------------------------------------------------------------------
905 // parsing
906 //-----------------------------------------------------------------------
907 
908 /**
909  * If this is a &gt;&gt;&gt; substitution, match only against ruleToUse.
910  * Otherwise, use the superclass function.
911  * @param text The string to parse
912  * @param parsePosition Ignored on entry, updated on exit to point to
913  * the first unmatched character.
914  * @param baseValue The partial parse result prior to calling this
915  * routine.
916  */
917 UBool
doParse(const UnicodeString & text,ParsePosition & parsePosition,double baseValue,double upperBound,UBool lenientParse,Formattable & result) const918 ModulusSubstitution::doParse(const UnicodeString& text,
919                              ParsePosition& parsePosition,
920                              double baseValue,
921                              double upperBound,
922                              UBool lenientParse,
923                              Formattable& result) const
924 {
925     // if this isn't a >>> substitution, we can just use the
926     // inherited parse() routine to do the parsing
927     if (ruleToUse == NULL) {
928         return NFSubstitution::doParse(text, parsePosition, baseValue, upperBound, lenientParse, result);
929 
930         // but if it IS a >>> substitution, we have to do it here: we
931         // use the specific rule's doParse() method, and then we have to
932         // do some of the other work of NFRuleSet.parse()
933     } else {
934         ruleToUse->doParse(text, parsePosition, FALSE, upperBound, result);
935 
936         if (parsePosition.getIndex() != 0) {
937             UErrorCode status = U_ZERO_ERROR;
938             double tempResult = result.getDouble(status);
939             tempResult = composeRuleValue(tempResult, baseValue);
940             result.setDouble(tempResult);
941         }
942 
943         return TRUE;
944     }
945 }
946 /**
947  * Returns a textual description of the substitution
948  * @return A textual description of the substitution.  This might
949  * not be identical to the description it was created from, but
950  * it'll produce the same result.
951  */
952 void
toString(UnicodeString & text) const953 ModulusSubstitution::toString(UnicodeString& text) const
954 {
955   // use tokenChar() to get the character at the beginning and
956   // end of the substitutin token.  In between them will go
957   // either the name of the rule set it uses, or the pattern of
958   // the DecimalFormat it uses
959 
960   if ( ruleToUse != NULL ) { // Must have been a >>> substitution.
961       text.remove();
962       text.append(tokenChar());
963       text.append(tokenChar());
964       text.append(tokenChar());
965   } else { // Otherwise just use the super-class function.
966 	  NFSubstitution::toString(text);
967   }
968 }
969 //===================================================================
970 // IntegralPartSubstitution
971 //===================================================================
972 
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IntegralPartSubstitution)973 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IntegralPartSubstitution)
974 
975 
976 //===================================================================
977 // FractionalPartSubstitution
978 //===================================================================
979 
980 
981     /**
982      * Constructs a FractionalPartSubstitution.  This object keeps a flag
983      * telling whether it should format by digits or not.  In addition,
984      * it marks the rule set it calls (if any) as a fraction rule set.
985      */
986 FractionalPartSubstitution::FractionalPartSubstitution(int32_t _pos,
987                              const NFRuleSet* _ruleSet,
988                              const UnicodeString& description,
989                              UErrorCode& status)
990  : NFSubstitution(_pos, _ruleSet, description, status)
991  , byDigits(FALSE)
992  , useSpaces(TRUE)
993 
994 {
995     // akk, ruleSet can change in superclass constructor
996     if (0 == description.compare(gGreaterGreaterThan, 2) ||
997         0 == description.compare(gGreaterGreaterGreaterThan, 3) ||
998         _ruleSet == getRuleSet()) {
999         byDigits = TRUE;
1000         if (0 == description.compare(gGreaterGreaterGreaterThan, 3)) {
1001             useSpaces = FALSE;
1002         }
1003     } else {
1004         // cast away const
1005         ((NFRuleSet*)getRuleSet())->makeIntoFractionRuleSet();
1006     }
1007 }
1008 
1009 //-----------------------------------------------------------------------
1010 // formatting
1011 //-----------------------------------------------------------------------
1012 
1013 /**
1014  * If in "by digits" mode, fills in the substitution one decimal digit
1015  * at a time using the rule set containing this substitution.
1016  * Otherwise, uses the superclass function.
1017  * @param number The number being formatted
1018  * @param toInsertInto The string to insert the result of formatting
1019  * the substitution into
1020  * @param pos The position of the owning rule's rule text in
1021  * toInsertInto
1022  */
1023 void
doSubstitution(double number,UnicodeString & toInsertInto,int32_t _pos,int32_t recursionCount,UErrorCode & status) const1024 FractionalPartSubstitution::doSubstitution(double number, UnicodeString& toInsertInto,
1025                                            int32_t _pos, int32_t recursionCount, UErrorCode& status) const
1026 {
1027   // if we're not in "byDigits" mode, just use the inherited
1028   // doSubstitution() routine
1029   if (!byDigits) {
1030     NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
1031 
1032     // if we're in "byDigits" mode, transform the value into an integer
1033     // by moving the decimal point eight places to the right and
1034     // pulling digits off the right one at a time, formatting each digit
1035     // as an integer using this substitution's owning rule set
1036     // (this is slower, but more accurate, than doing it from the
1037     // other end)
1038   } else {
1039     //          int32_t numberToFormat = (int32_t)uprv_round(transformNumber(number) * uprv_pow(10, kMaxDecimalDigits));
1040     //          // this flag keeps us from formatting trailing zeros.  It starts
1041     //          // out false because we're pulling from the right, and switches
1042     //          // to true the first time we encounter a non-zero digit
1043     //          UBool doZeros = FALSE;
1044     //          for (int32_t i = 0; i < kMaxDecimalDigits; i++) {
1045     //              int64_t digit = numberToFormat % 10;
1046     //              if (digit != 0 || doZeros) {
1047     //                  if (doZeros && useSpaces) {
1048     //                      toInsertInto.insert(_pos + getPos(), gSpace);
1049     //                  }
1050     //                  doZeros = TRUE;
1051     //                  getRuleSet()->format(digit, toInsertInto, _pos + getPos());
1052     //              }
1053     //              numberToFormat /= 10;
1054     //          }
1055 
1056     DigitList dl;
1057     dl.set(number);
1058     dl.roundFixedPoint(20);     // round to 20 fraction digits.
1059     dl.reduce();                // Removes any trailing zeros.
1060 
1061     UBool pad = FALSE;
1062     for (int32_t didx = dl.getCount()-1; didx>=dl.getDecimalAt(); didx--) {
1063       // Loop iterates over fraction digits, starting with the LSD.
1064       //   include both real digits from the number, and zeros
1065       //   to the left of the MSD but to the right of the decimal point.
1066       if (pad && useSpaces) {
1067         toInsertInto.insert(_pos + getPos(), gSpace);
1068       } else {
1069         pad = TRUE;
1070       }
1071       int64_t digit = didx>=0 ? dl.getDigit(didx) - '0' : 0;
1072       getRuleSet()->format(digit, toInsertInto, _pos + getPos(), recursionCount, status);
1073     }
1074 
1075     if (!pad) {
1076       // hack around lack of precision in digitlist. if we would end up with
1077       // "foo point" make sure we add a " zero" to the end.
1078       getRuleSet()->format((int64_t)0, toInsertInto, _pos + getPos(), recursionCount, status);
1079     }
1080   }
1081 }
1082 
1083 //-----------------------------------------------------------------------
1084 // parsing
1085 //-----------------------------------------------------------------------
1086 
1087 /**
1088  * If in "by digits" mode, parses the string as if it were a string
1089  * of individual digits; otherwise, uses the superclass function.
1090  * @param text The string to parse
1091  * @param parsePosition Ignored on entry, but updated on exit to point
1092  * to the first unmatched character
1093  * @param baseValue The partial parse result prior to entering this
1094  * function
1095  * @param upperBound Only consider rules with base values lower than
1096  * this when filling in the substitution
1097  * @param lenientParse If true, try matching the text as numerals if
1098  * matching as words doesn't work
1099  * @return If the match was successful, the current partial parse
1100  * result; otherwise new Long(0).  The result is either a Long or
1101  * a Double.
1102  */
1103 
1104 UBool
doParse(const UnicodeString & text,ParsePosition & parsePosition,double baseValue,double,UBool lenientParse,Formattable & resVal) const1105 FractionalPartSubstitution::doParse(const UnicodeString& text,
1106                 ParsePosition& parsePosition,
1107                 double baseValue,
1108                 double /*upperBound*/,
1109                 UBool lenientParse,
1110                 Formattable& resVal) const
1111 {
1112     // if we're not in byDigits mode, we can just use the inherited
1113     // doParse()
1114     if (!byDigits) {
1115         return NFSubstitution::doParse(text, parsePosition, baseValue, 0, lenientParse, resVal);
1116 
1117         // if we ARE in byDigits mode, parse the text one digit at a time
1118         // using this substitution's owning rule set (we do this by setting
1119         // upperBound to 10 when calling doParse() ) until we reach
1120         // nonmatching text
1121     } else {
1122         UnicodeString workText(text);
1123         ParsePosition workPos(1);
1124         double result = 0;
1125         int32_t digit;
1126 //          double p10 = 0.1;
1127 
1128         DigitList dl;
1129         NumberFormat* fmt = NULL;
1130         while (workText.length() > 0 && workPos.getIndex() != 0) {
1131             workPos.setIndex(0);
1132             Formattable temp;
1133             getRuleSet()->parse(workText, workPos, 10, temp);
1134             UErrorCode status = U_ZERO_ERROR;
1135             digit = temp.getLong(status);
1136 //            digit = temp.getType() == Formattable::kLong ?
1137 //               temp.getLong() :
1138 //            (int32_t)temp.getDouble();
1139 
1140             if (lenientParse && workPos.getIndex() == 0) {
1141                 if (!fmt) {
1142                     status = U_ZERO_ERROR;
1143                     fmt = NumberFormat::createInstance(status);
1144                     if (U_FAILURE(status)) {
1145                         delete fmt;
1146                         fmt = NULL;
1147                     }
1148                 }
1149                 if (fmt) {
1150                     fmt->parse(workText, temp, workPos);
1151                     digit = temp.getLong(status);
1152                 }
1153             }
1154 
1155             if (workPos.getIndex() != 0) {
1156                 dl.append((char)('0' + digit));
1157 //                  result += digit * p10;
1158 //                  p10 /= 10;
1159                 parsePosition.setIndex(parsePosition.getIndex() + workPos.getIndex());
1160                 workText.removeBetween(0, workPos.getIndex());
1161                 while (workText.length() > 0 && workText.charAt(0) == gSpace) {
1162                     workText.removeBetween(0, 1);
1163                     parsePosition.setIndex(parsePosition.getIndex() + 1);
1164                 }
1165             }
1166         }
1167         delete fmt;
1168 
1169         result = dl.getCount() == 0 ? 0 : dl.getDouble();
1170         result = composeRuleValue(result, baseValue);
1171         resVal.setDouble(result);
1172         return TRUE;
1173     }
1174 }
1175 
1176 UBool
operator ==(const NFSubstitution & rhs) const1177 FractionalPartSubstitution::operator==(const NFSubstitution& rhs) const
1178 {
1179   return NFSubstitution::operator==(rhs) &&
1180   ((const FractionalPartSubstitution*)&rhs)->byDigits == byDigits;
1181 }
1182 
1183 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FractionalPartSubstitution)
1184 
1185 
1186 //===================================================================
1187 // AbsoluteValueSubstitution
1188 //===================================================================
1189 
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(AbsoluteValueSubstitution)1190 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(AbsoluteValueSubstitution)
1191 
1192 //===================================================================
1193 // NumeratorSubstitution
1194 //===================================================================
1195 
1196 void
1197 NumeratorSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t apos, int32_t recursionCount, UErrorCode& status) const {
1198     // perform a transformation on the number being formatted that
1199     // is dependent on the type of substitution this is
1200 
1201     double numberToFormat = transformNumber(number);
1202     int64_t longNF = util64_fromDouble(numberToFormat);
1203 
1204     const NFRuleSet* aruleSet = getRuleSet();
1205     if (withZeros && aruleSet != NULL) {
1206         // if there are leading zeros in the decimal expansion then emit them
1207         int64_t nf =longNF;
1208         int32_t len = toInsertInto.length();
1209         while ((nf *= 10) < denominator) {
1210             toInsertInto.insert(apos + getPos(), gSpace);
1211             aruleSet->format((int64_t)0, toInsertInto, apos + getPos(), recursionCount, status);
1212         }
1213         apos += toInsertInto.length() - len;
1214     }
1215 
1216     // if the result is an integer, from here on out we work in integer
1217     // space (saving time and memory and preserving accuracy)
1218     if (numberToFormat == longNF && aruleSet != NULL) {
1219         aruleSet->format(longNF, toInsertInto, apos + getPos(), recursionCount, status);
1220 
1221         // if the result isn't an integer, then call either our rule set's
1222         // format() method or our DecimalFormat's format() method to
1223         // format the result
1224     } else {
1225         if (aruleSet != NULL) {
1226             aruleSet->format(numberToFormat, toInsertInto, apos + getPos(), recursionCount, status);
1227         } else {
1228             UnicodeString temp;
1229             getNumberFormat()->format(numberToFormat, temp, status);
1230             toInsertInto.insert(apos + getPos(), temp);
1231         }
1232     }
1233 }
1234 
1235 UBool
doParse(const UnicodeString & text,ParsePosition & parsePosition,double baseValue,double upperBound,UBool,Formattable & result) const1236 NumeratorSubstitution::doParse(const UnicodeString& text,
1237                                ParsePosition& parsePosition,
1238                                double baseValue,
1239                                double upperBound,
1240                                UBool /*lenientParse*/,
1241                                Formattable& result) const
1242 {
1243     // we don't have to do anything special to do the parsing here,
1244     // but we have to turn lenient parsing off-- if we leave it on,
1245     // it SERIOUSLY messes up the algorithm
1246 
1247     // if withZeros is true, we need to count the zeros
1248     // and use that to adjust the parse result
1249     UErrorCode status = U_ZERO_ERROR;
1250     int32_t zeroCount = 0;
1251     UnicodeString workText(text);
1252 
1253     if (withZeros) {
1254         ParsePosition workPos(1);
1255         Formattable temp;
1256 
1257         while (workText.length() > 0 && workPos.getIndex() != 0) {
1258             workPos.setIndex(0);
1259             getRuleSet()->parse(workText, workPos, 1, temp); // parse zero or nothing at all
1260             if (workPos.getIndex() == 0) {
1261                 // we failed, either there were no more zeros, or the number was formatted with digits
1262                 // either way, we're done
1263                 break;
1264             }
1265 
1266             ++zeroCount;
1267             parsePosition.setIndex(parsePosition.getIndex() + workPos.getIndex());
1268             workText.remove(0, workPos.getIndex());
1269             while (workText.length() > 0 && workText.charAt(0) == gSpace) {
1270                 workText.remove(0, 1);
1271                 parsePosition.setIndex(parsePosition.getIndex() + 1);
1272             }
1273         }
1274 
1275         workText = text;
1276         workText.remove(0, (int32_t)parsePosition.getIndex());
1277         parsePosition.setIndex(0);
1278     }
1279 
1280     // we've parsed off the zeros, now let's parse the rest from our current position
1281     NFSubstitution::doParse(workText, parsePosition, withZeros ? 1 : baseValue, upperBound, FALSE, result);
1282 
1283     if (withZeros) {
1284         // any base value will do in this case.  is there a way to
1285         // force this to not bother trying all the base values?
1286 
1287         // compute the 'effective' base and prescale the value down
1288         int64_t n = result.getLong(status); // force conversion!
1289         int64_t d = 1;
1290         int32_t pow = 0;
1291         while (d <= n) {
1292             d *= 10;
1293             ++pow;
1294         }
1295         // now add the zeros
1296         while (zeroCount > 0) {
1297             d *= 10;
1298             --zeroCount;
1299         }
1300         // d is now our true denominator
1301         result.setDouble((double)n/(double)d);
1302     }
1303 
1304     return TRUE;
1305 }
1306 
1307 UBool
operator ==(const NFSubstitution & rhs) const1308 NumeratorSubstitution::operator==(const NFSubstitution& rhs) const
1309 {
1310     return NFSubstitution::operator==(rhs) &&
1311         denominator == ((const NumeratorSubstitution*)&rhs)->denominator;
1312 }
1313 
1314 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NumeratorSubstitution)
1315 
1316 const UChar NumeratorSubstitution::LTLT[] = { 0x003c, 0x003c };
1317 
1318 U_NAMESPACE_END
1319 
1320 /* U_HAVE_RBNF */
1321 #endif
1322 
1323