xref: /aosp_15_r20/external/cronet/third_party/icu/source/i18n/nfsubs.cpp (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *   Copyright (C) 1997-2015, International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 ******************************************************************************
8 *   file name:  nfsubs.cpp
9 *   encoding:   UTF-8
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 * Modification history
14 * Date        Name      Comments
15 * 10/11/2001  Doug      Ported from ICU4J
16 */
17 
18 #include <stdio.h>
19 #include "utypeinfo.h"  // for 'typeid' to work
20 
21 #include "nfsubs.h"
22 #include "fmtableimp.h"
23 #include "putilimp.h"
24 #include "number_decimalquantity.h"
25 
26 #if U_HAVE_RBNF
27 
28 static const char16_t gLessThan = 0x003c;
29 static const char16_t gEquals = 0x003d;
30 static const char16_t gGreaterThan = 0x003e;
31 static const char16_t gPercent = 0x0025;
32 static const char16_t gPound = 0x0023;
33 static const char16_t gZero = 0x0030;
34 static const char16_t gSpace = 0x0020;
35 
36 static const char16_t gEqualsEquals[] =
37 {
38     0x3D, 0x3D, 0
39 }; /* "==" */
40 static const char16_t gGreaterGreaterGreaterThan[] =
41 {
42     0x3E, 0x3E, 0x3E, 0
43 }; /* ">>>" */
44 static const char16_t gGreaterGreaterThan[] =
45 {
46     0x3E, 0x3E, 0
47 }; /* ">>" */
48 
49 U_NAMESPACE_BEGIN
50 
51 using number::impl::DecimalQuantity;
52 
53 class SameValueSubstitution : public NFSubstitution {
54 public:
55     SameValueSubstitution(int32_t pos,
56         const NFRuleSet* ruleset,
57         const UnicodeString& description,
58         UErrorCode& status);
59     virtual ~SameValueSubstitution();
60 
transformNumber(int64_t number) const61     virtual int64_t transformNumber(int64_t number) const override { return number; }
transformNumber(double number) const62     virtual double transformNumber(double number) const override { return number; }
composeRuleValue(double newRuleValue,double) const63     virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const override { return newRuleValue; }
calcUpperBound(double oldUpperBound) const64     virtual double calcUpperBound(double oldUpperBound) const override { return oldUpperBound; }
tokenChar() const65     virtual char16_t tokenChar() const override { return (char16_t)0x003d; } // '='
66 
67 public:
68     static UClassID getStaticClassID();
69     virtual UClassID getDynamicClassID() const override;
70 };
71 
~SameValueSubstitution()72 SameValueSubstitution::~SameValueSubstitution() {}
73 
74 class MultiplierSubstitution : public NFSubstitution {
75     int64_t divisor;
76 
77 public:
MultiplierSubstitution(int32_t _pos,const NFRule * rule,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)78     MultiplierSubstitution(int32_t _pos,
79         const NFRule *rule,
80         const NFRuleSet* _ruleSet,
81         const UnicodeString& description,
82         UErrorCode& status)
83         : NFSubstitution(_pos, _ruleSet, description, status), divisor(rule->getDivisor())
84     {
85         if (divisor == 0) {
86             status = U_PARSE_ERROR;
87         }
88     }
89     virtual ~MultiplierSubstitution();
90 
setDivisor(int32_t radix,int16_t exponent,UErrorCode & status)91     virtual void setDivisor(int32_t radix, int16_t exponent, UErrorCode& status) override {
92         divisor = util64_pow(radix, exponent);
93 
94         if(divisor == 0) {
95             status = U_PARSE_ERROR;
96         }
97     }
98 
99     virtual bool operator==(const NFSubstitution& rhs) const override;
100 
transformNumber(int64_t number) const101     virtual int64_t transformNumber(int64_t number) const override {
102         return number / divisor;
103     }
104 
transformNumber(double number) const105     virtual double transformNumber(double number) const override {
106         bool doFloor = getRuleSet() != nullptr;
107         if (!doFloor) {
108             // This is a HACK that partially addresses ICU-22313.  The original code wanted us to do
109             // floor() on the result if we were passing it to another rule set, but not if we were passing
110             // it to a DecimalFormat.  But the DurationRules rule set has multiplier substitutions where
111             // we DO want to do the floor() operation.  What we REALLY want is to do floor() any time
112             // the owning rule also has a ModulusSubsitution, but we don't have access to that information
113             // here, so instead we're doing a floor() any time the DecimalFormat has maxFracDigits equal to
114             // 0.  This seems to work with our existing rule sets, but could be a problem in the future,
115             // but the "real" fix for DurationRules isn't worth doing, since we're deprecating DurationRules
116             // anyway.  This is enough to keep it from being egregiously wrong, without obvious side
117             // effects.     --rtg 8/16/23
118             const DecimalFormat* decimalFormat = getNumberFormat();
119             if (decimalFormat == nullptr || decimalFormat->getMaximumFractionDigits() == 0) {
120                 doFloor = true;
121             }
122         }
123 
124         if (doFloor) {
125             return uprv_floor(number / divisor);
126         } else {
127             return number / divisor;
128         }
129     }
130 
composeRuleValue(double newRuleValue,double) const131     virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const override {
132         return newRuleValue * divisor;
133     }
134 
calcUpperBound(double) const135     virtual double calcUpperBound(double /*oldUpperBound*/) const override { return static_cast<double>(divisor); }
136 
tokenChar() const137     virtual char16_t tokenChar() const override { return (char16_t)0x003c; } // '<'
138 
139 public:
140     static UClassID getStaticClassID();
141     virtual UClassID getDynamicClassID() const override;
142 };
143 
~MultiplierSubstitution()144 MultiplierSubstitution::~MultiplierSubstitution() {}
145 
146 class ModulusSubstitution : public NFSubstitution {
147     int64_t  divisor;
148     const NFRule* ruleToUse;
149 public:
150     ModulusSubstitution(int32_t pos,
151         const NFRule* rule,
152         const NFRule* rulePredecessor,
153         const NFRuleSet* ruleSet,
154         const UnicodeString& description,
155         UErrorCode& status);
156     virtual ~ModulusSubstitution();
157 
setDivisor(int32_t radix,int16_t exponent,UErrorCode & status)158     virtual void setDivisor(int32_t radix, int16_t exponent, UErrorCode& status) override {
159         divisor = util64_pow(radix, exponent);
160 
161         if (divisor == 0) {
162             status = U_PARSE_ERROR;
163         }
164     }
165 
166     virtual bool operator==(const NFSubstitution& rhs) const override;
167 
168     virtual void doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const override;
169     virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const override;
170 
transformNumber(int64_t number) const171     virtual int64_t transformNumber(int64_t number) const override { return number % divisor; }
transformNumber(double number) const172     virtual double transformNumber(double number) const override { return uprv_fmod(number, static_cast<double>(divisor)); }
173 
174     virtual UBool doParse(const UnicodeString& text,
175         ParsePosition& parsePosition,
176         double baseValue,
177         double upperBound,
178         UBool lenientParse,
179         uint32_t nonNumericalExecutedRuleMask,
180         Formattable& result) const override;
181 
composeRuleValue(double newRuleValue,double oldRuleValue) const182     virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const override {
183         return oldRuleValue - uprv_fmod(oldRuleValue, static_cast<double>(divisor)) + newRuleValue;
184     }
185 
calcUpperBound(double) const186     virtual double calcUpperBound(double /*oldUpperBound*/) const override { return static_cast<double>(divisor); }
187 
isModulusSubstitution() const188     virtual UBool isModulusSubstitution() const override { return true; }
189 
tokenChar() const190     virtual char16_t tokenChar() const override { return (char16_t)0x003e; } // '>'
191 
192     virtual void toString(UnicodeString& result) const override;
193 
194 public:
195     static UClassID getStaticClassID();
196     virtual UClassID getDynamicClassID() const override;
197 };
198 
~ModulusSubstitution()199 ModulusSubstitution::~ModulusSubstitution() {}
200 
201 class IntegralPartSubstitution : public NFSubstitution {
202 public:
IntegralPartSubstitution(int32_t _pos,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)203     IntegralPartSubstitution(int32_t _pos,
204         const NFRuleSet* _ruleSet,
205         const UnicodeString& description,
206         UErrorCode& status)
207         : NFSubstitution(_pos, _ruleSet, description, status) {}
208     virtual ~IntegralPartSubstitution();
209 
transformNumber(int64_t number) const210     virtual int64_t transformNumber(int64_t number) const override { return number; }
transformNumber(double number) const211     virtual double transformNumber(double number) const override { return uprv_floor(number); }
composeRuleValue(double newRuleValue,double oldRuleValue) const212     virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const override { return newRuleValue + oldRuleValue; }
calcUpperBound(double) const213     virtual double calcUpperBound(double /*oldUpperBound*/) const override { return DBL_MAX; }
tokenChar() const214     virtual char16_t tokenChar() const override { return (char16_t)0x003c; } // '<'
215 
216 public:
217     static UClassID getStaticClassID();
218     virtual UClassID getDynamicClassID() const override;
219 };
220 
~IntegralPartSubstitution()221 IntegralPartSubstitution::~IntegralPartSubstitution() {}
222 
223 class FractionalPartSubstitution : public NFSubstitution {
224     UBool byDigits;
225     UBool useSpaces;
226     enum { kMaxDecimalDigits = 8 };
227 public:
228     FractionalPartSubstitution(int32_t pos,
229         const NFRuleSet* ruleSet,
230         const UnicodeString& description,
231         UErrorCode& status);
232     virtual ~FractionalPartSubstitution();
233 
234     virtual bool operator==(const NFSubstitution& rhs) const override;
235 
236     virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const override;
doSubstitution(int64_t,UnicodeString &,int32_t,int32_t,UErrorCode &) const237     virtual void doSubstitution(int64_t /*number*/, UnicodeString& /*toInsertInto*/, int32_t /*_pos*/, int32_t /*recursionCount*/, UErrorCode& /*status*/) const override {}
transformNumber(int64_t) const238     virtual int64_t transformNumber(int64_t /*number*/) const override { return 0; }
transformNumber(double number) const239     virtual double transformNumber(double number) const override { return number - uprv_floor(number); }
240 
241     virtual UBool doParse(const UnicodeString& text,
242         ParsePosition& parsePosition,
243         double baseValue,
244         double upperBound,
245         UBool lenientParse,
246         uint32_t nonNumericalExecutedRuleMask,
247         Formattable& result) const override;
248 
composeRuleValue(double newRuleValue,double oldRuleValue) const249     virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const override { return newRuleValue + oldRuleValue; }
calcUpperBound(double) const250     virtual double calcUpperBound(double /*oldUpperBound*/) const override { return 0.0; }
tokenChar() const251     virtual char16_t tokenChar() const override { return (char16_t)0x003e; } // '>'
252 
253 public:
254     static UClassID getStaticClassID();
255     virtual UClassID getDynamicClassID() const override;
256 };
257 
~FractionalPartSubstitution()258 FractionalPartSubstitution::~FractionalPartSubstitution() {}
259 
260 class AbsoluteValueSubstitution : public NFSubstitution {
261 public:
AbsoluteValueSubstitution(int32_t _pos,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)262     AbsoluteValueSubstitution(int32_t _pos,
263         const NFRuleSet* _ruleSet,
264         const UnicodeString& description,
265         UErrorCode& status)
266         : NFSubstitution(_pos, _ruleSet, description, status) {}
267     virtual ~AbsoluteValueSubstitution();
268 
transformNumber(int64_t number) const269     virtual int64_t transformNumber(int64_t number) const override { return number >= 0 ? number : -number; }
transformNumber(double number) const270     virtual double transformNumber(double number) const override { return uprv_fabs(number); }
composeRuleValue(double newRuleValue,double) const271     virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const override { return -newRuleValue; }
calcUpperBound(double) const272     virtual double calcUpperBound(double /*oldUpperBound*/) const override { return DBL_MAX; }
tokenChar() const273     virtual char16_t tokenChar() const override { return (char16_t)0x003e; } // '>'
274 
275 public:
276     static UClassID getStaticClassID();
277     virtual UClassID getDynamicClassID() const override;
278 };
279 
~AbsoluteValueSubstitution()280 AbsoluteValueSubstitution::~AbsoluteValueSubstitution() {}
281 
282 class NumeratorSubstitution : public NFSubstitution {
283     double denominator;
284     int64_t ldenominator;
285     UBool withZeros;
286 public:
fixdesc(const UnicodeString & desc)287     static inline UnicodeString fixdesc(const UnicodeString& desc) {
288         if (desc.endsWith(LTLT, 2)) {
289             UnicodeString result(desc, 0, desc.length()-1);
290             return result;
291         }
292         return desc;
293     }
NumeratorSubstitution(int32_t _pos,double _denominator,NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)294     NumeratorSubstitution(int32_t _pos,
295         double _denominator,
296         NFRuleSet* _ruleSet,
297         const UnicodeString& description,
298         UErrorCode& status)
299         : NFSubstitution(_pos, _ruleSet, fixdesc(description), status), denominator(_denominator)
300     {
301         ldenominator = util64_fromDouble(denominator);
302         withZeros = description.endsWith(LTLT, 2);
303     }
304     virtual ~NumeratorSubstitution();
305 
306     virtual bool operator==(const NFSubstitution& rhs) const override;
307 
transformNumber(int64_t number) const308     virtual int64_t transformNumber(int64_t number) const override { return number * ldenominator; }
transformNumber(double number) const309     virtual double transformNumber(double number) const override { return uprv_round(number * denominator); }
310 
doSubstitution(int64_t,UnicodeString &,int32_t,int32_t,UErrorCode &) const311     virtual void doSubstitution(int64_t /*number*/, UnicodeString& /*toInsertInto*/, int32_t /*_pos*/, int32_t /*recursionCount*/, UErrorCode& /*status*/) const override {}
312     virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const override;
313     virtual UBool doParse(const UnicodeString& text,
314         ParsePosition& parsePosition,
315         double baseValue,
316         double upperBound,
317         UBool /*lenientParse*/,
318         uint32_t nonNumericalExecutedRuleMask,
319         Formattable& result) const override;
320 
composeRuleValue(double newRuleValue,double oldRuleValue) const321     virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const override { return newRuleValue / oldRuleValue; }
calcUpperBound(double) const322     virtual double calcUpperBound(double /*oldUpperBound*/) const override { return denominator; }
tokenChar() const323     virtual char16_t tokenChar() const override { return (char16_t)0x003c; } // '<'
324 private:
325     static const char16_t LTLT[2];
326 
327 public:
328     static UClassID getStaticClassID();
329     virtual UClassID getDynamicClassID() const override;
330 };
331 
~NumeratorSubstitution()332 NumeratorSubstitution::~NumeratorSubstitution() {}
333 
334 NFSubstitution*
makeSubstitution(int32_t pos,const NFRule * rule,const NFRule * predecessor,const NFRuleSet * ruleSet,const RuleBasedNumberFormat * formatter,const UnicodeString & description,UErrorCode & status)335 NFSubstitution::makeSubstitution(int32_t pos,
336                                  const NFRule* rule,
337                                  const NFRule* predecessor,
338                                  const NFRuleSet* ruleSet,
339                                  const RuleBasedNumberFormat* formatter,
340                                  const UnicodeString& description,
341                                  UErrorCode& status)
342 {
343     // if the description is empty, return a NullSubstitution
344     if (description.length() == 0) {
345         return nullptr;
346     }
347 
348     switch (description.charAt(0)) {
349         // if the description begins with '<'...
350     case gLessThan:
351         // throw an exception if the rule is a negative number
352         // rule
353         if (rule->getBaseValue() == NFRule::kNegativeNumberRule) {
354             // throw new IllegalArgumentException("<< not allowed in negative-number rule");
355             status = U_PARSE_ERROR;
356             return nullptr;
357         }
358 
359         // if the rule is a fraction rule, return an
360         // IntegralPartSubstitution
361         else if (rule->getBaseValue() == NFRule::kImproperFractionRule
362             || rule->getBaseValue() == NFRule::kProperFractionRule
363             || rule->getBaseValue() == NFRule::kDefaultRule) {
364             return new IntegralPartSubstitution(pos, ruleSet, description, status);
365         }
366 
367         // if the rule set containing the rule is a fraction
368         // rule set, return a NumeratorSubstitution
369         else if (ruleSet->isFractionRuleSet()) {
370             return new NumeratorSubstitution(pos, (double)rule->getBaseValue(),
371                 formatter->getDefaultRuleSet(), description, status);
372         }
373 
374         // otherwise, return a MultiplierSubstitution
375         else {
376             return new MultiplierSubstitution(pos, rule, ruleSet,
377                 description, status);
378         }
379 
380         // if the description begins with '>'...
381     case gGreaterThan:
382         // if the rule is a negative-number rule, return
383         // an AbsoluteValueSubstitution
384         if (rule->getBaseValue() == NFRule::kNegativeNumberRule) {
385             return new AbsoluteValueSubstitution(pos, ruleSet, description, status);
386         }
387 
388         // if the rule is a fraction rule, return a
389         // FractionalPartSubstitution
390         else if (rule->getBaseValue() == NFRule::kImproperFractionRule
391             || rule->getBaseValue() == NFRule::kProperFractionRule
392             || rule->getBaseValue() == NFRule::kDefaultRule) {
393             return new FractionalPartSubstitution(pos, ruleSet, description, status);
394         }
395 
396         // if the rule set owning the rule is a fraction rule set,
397         // throw an exception
398         else if (ruleSet->isFractionRuleSet()) {
399             // throw new IllegalArgumentException(">> not allowed in fraction rule set");
400             status = U_PARSE_ERROR;
401             return nullptr;
402         }
403 
404         // otherwise, return a ModulusSubstitution
405         else {
406             return new ModulusSubstitution(pos, rule, predecessor,
407                 ruleSet, description, status);
408         }
409 
410         // if the description begins with '=', always return a
411         // SameValueSubstitution
412     case gEquals:
413         return new SameValueSubstitution(pos, ruleSet, description, status);
414 
415         // and if it's anything else, throw an exception
416     default:
417         // throw new IllegalArgumentException("Illegal substitution character");
418         status = U_PARSE_ERROR;
419     }
420     return nullptr;
421 }
422 
NFSubstitution(int32_t _pos,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)423 NFSubstitution::NFSubstitution(int32_t _pos,
424                                const NFRuleSet* _ruleSet,
425                                const UnicodeString& description,
426                                UErrorCode& status)
427                                : pos(_pos), ruleSet(nullptr), numberFormat(nullptr)
428 {
429     // the description should begin and end with the same character.
430     // If it doesn't that's a syntax error.  Otherwise,
431     // makeSubstitution() was the only thing that needed to know
432     // about these characters, so strip them off
433     UnicodeString workingDescription(description);
434     if (description.length() >= 2
435         && description.charAt(0) == description.charAt(description.length() - 1))
436     {
437         workingDescription.remove(description.length() - 1, 1);
438         workingDescription.remove(0, 1);
439     }
440     else if (description.length() != 0) {
441         // throw new IllegalArgumentException("Illegal substitution syntax");
442         status = U_PARSE_ERROR;
443         return;
444     }
445 
446     if (workingDescription.length() == 0) {
447         // if the description was just two paired token characters
448         // (i.e., "<<" or ">>"), it uses the rule set it belongs to to
449         // format its result
450         this->ruleSet = _ruleSet;
451     }
452     else if (workingDescription.charAt(0) == gPercent) {
453         // if the description contains a rule set name, that's the rule
454         // set we use to format the result: get a reference to the
455         // names rule set
456         this->ruleSet = _ruleSet->getOwner()->findRuleSet(workingDescription, status);
457     }
458     else if (workingDescription.charAt(0) == gPound || workingDescription.charAt(0) ==gZero) {
459         // if the description begins with 0 or #, treat it as a
460         // DecimalFormat pattern, and initialize a DecimalFormat with
461         // that pattern (then set it to use the DecimalFormatSymbols
462         // belonging to our formatter)
463         const DecimalFormatSymbols* sym = _ruleSet->getOwner()->getDecimalFormatSymbols();
464         if (!sym) {
465             status = U_MISSING_RESOURCE_ERROR;
466             return;
467         }
468         DecimalFormat *tempNumberFormat = new DecimalFormat(workingDescription, *sym, status);
469         /* test for nullptr */
470         if (!tempNumberFormat) {
471             status = U_MEMORY_ALLOCATION_ERROR;
472             return;
473         }
474         if (U_FAILURE(status)) {
475             delete tempNumberFormat;
476             return;
477         }
478         this->numberFormat = tempNumberFormat;
479     }
480     else if (workingDescription.charAt(0) == gGreaterThan) {
481         // if the description is ">>>", this substitution bypasses the
482         // usual rule-search process and always uses the rule that precedes
483         // it in its own rule set's rule list (this is used for place-value
484         // notations: formats where you want to see a particular part of
485         // a number even when it's 0)
486 
487         // this causes problems when >>> is used in a frationalPartSubstitution
488         // this->ruleSet = nullptr;
489         this->ruleSet = _ruleSet;
490         this->numberFormat = nullptr;
491     }
492     else {
493         // and of the description is none of these things, it's a syntax error
494 
495         // throw new IllegalArgumentException("Illegal substitution syntax");
496         status = U_PARSE_ERROR;
497     }
498 }
499 
~NFSubstitution()500 NFSubstitution::~NFSubstitution()
501 {
502     delete numberFormat;
503     numberFormat = nullptr;
504 }
505 
506 /**
507  * Set's the substitution's divisor.  Used by NFRule.setBaseValue().
508  * A no-op for all substitutions except multiplier and modulus
509  * substitutions.
510  * @param radix The radix of the divisor
511  * @param exponent The exponent of the divisor
512  */
513 void
setDivisor(int32_t,int16_t,UErrorCode &)514 NFSubstitution::setDivisor(int32_t /*radix*/, int16_t /*exponent*/, UErrorCode& /*status*/) {
515   // a no-op for all substitutions except multiplier and modulus substitutions
516 }
517 
518 void
setDecimalFormatSymbols(const DecimalFormatSymbols & newSymbols,UErrorCode &)519 NFSubstitution::setDecimalFormatSymbols(const DecimalFormatSymbols &newSymbols, UErrorCode& /*status*/) {
520     if (numberFormat != nullptr) {
521         numberFormat->setDecimalFormatSymbols(newSymbols);
522     }
523 }
524 
525 //-----------------------------------------------------------------------
526 // boilerplate
527 //-----------------------------------------------------------------------
528 
529 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NFSubstitution)
530 
531 /**
532  * Compares two substitutions for equality
533  * @param The substitution to compare this one to
534  * @return true if the two substitutions are functionally equivalent
535  */
536 bool
537 NFSubstitution::operator==(const NFSubstitution& rhs) const
538 {
539   // compare class and all of the fields all substitutions have
540   // in common
541   // this should be called by subclasses before their own equality tests
542   return typeid(*this) == typeid(rhs)
543   && pos == rhs.pos
544   && (ruleSet == nullptr) == (rhs.ruleSet == nullptr)
545   // && ruleSet == rhs.ruleSet causes circularity, other checks to make instead?
546   && (numberFormat == nullptr
547       ? (rhs.numberFormat == nullptr)
548       : (*numberFormat == *rhs.numberFormat));
549 }
550 
551 /**
552  * Returns a textual description of the substitution
553  * @return A textual description of the substitution.  This might
554  * not be identical to the description it was created from, but
555  * it'll produce the same result.
556  */
557 void
toString(UnicodeString & text) const558 NFSubstitution::toString(UnicodeString& text) const
559 {
560   // use tokenChar() to get the character at the beginning and
561   // end of the substitutin token.  In between them will go
562   // either the name of the rule set it uses, or the pattern of
563   // the DecimalFormat it uses
564   text.remove();
565   text.append(tokenChar());
566 
567   UnicodeString temp;
568   if (ruleSet != nullptr) {
569     ruleSet->getName(temp);
570   } else if (numberFormat != nullptr) {
571     numberFormat->toPattern(temp);
572   }
573   text.append(temp);
574   text.append(tokenChar());
575 }
576 
577 //-----------------------------------------------------------------------
578 // formatting
579 //-----------------------------------------------------------------------
580 
581 /**
582  * Performs a mathematical operation on the number, formats it using
583  * either ruleSet or decimalFormat, and inserts the result into
584  * toInsertInto.
585  * @param number The number being formatted.
586  * @param toInsertInto The string we insert the result into
587  * @param pos The position in toInsertInto where the owning rule's
588  * rule text begins (this value is added to this substitution's
589  * position to determine exactly where to insert the new text)
590  */
591 void
doSubstitution(int64_t number,UnicodeString & toInsertInto,int32_t _pos,int32_t recursionCount,UErrorCode & status) const592 NFSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
593 {
594     if (ruleSet != nullptr) {
595         // Perform a transformation on the number that is dependent
596         // on the type of substitution this is, then just call its
597         // rule set's format() method to format the result
598         ruleSet->format(transformNumber(number), toInsertInto, _pos + this->pos, recursionCount, status);
599     } else if (numberFormat != nullptr) {
600         if (number <= MAX_INT64_IN_DOUBLE) {
601             // or perform the transformation on the number (preserving
602             // the result's fractional part if the formatter it set
603             // to show it), then use that formatter's format() method
604             // to format the result
605             double numberToFormat = transformNumber((double)number);
606             if (numberFormat->getMaximumFractionDigits() == 0) {
607                 numberToFormat = uprv_floor(numberToFormat);
608             }
609 
610             UnicodeString temp;
611             numberFormat->format(numberToFormat, temp, status);
612             toInsertInto.insert(_pos + this->pos, temp);
613         }
614         else {
615             // We have gone beyond double precision. Something has to give.
616             // We're favoring accuracy of the large number over potential rules
617             // that round like a CompactDecimalFormat, which is not a common use case.
618             //
619             // Perform a transformation on the number that is dependent
620             // on the type of substitution this is, then just call its
621             // rule set's format() method to format the result
622             int64_t numberToFormat = transformNumber(number);
623             UnicodeString temp;
624             numberFormat->format(numberToFormat, temp, status);
625             toInsertInto.insert(_pos + this->pos, temp);
626         }
627     }
628 }
629 
630 /**
631  * Performs a mathematical operation on the number, formats it using
632  * either ruleSet or decimalFormat, and inserts the result into
633  * toInsertInto.
634  * @param number The number being formatted.
635  * @param toInsertInto The string we insert the result into
636  * @param pos The position in toInsertInto where the owning rule's
637  * rule text begins (this value is added to this substitution's
638  * position to determine exactly where to insert the new text)
639  */
640 void
doSubstitution(double number,UnicodeString & toInsertInto,int32_t _pos,int32_t recursionCount,UErrorCode & status) const641 NFSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const {
642     // perform a transformation on the number being formatted that
643     // is dependent on the type of substitution this is
644     double numberToFormat = transformNumber(number);
645 
646     if (uprv_isInfinite(numberToFormat)) {
647         // This is probably a minus rule. Combine it with an infinite rule.
648         const NFRule *infiniteRule = ruleSet->findDoubleRule(uprv_getInfinity());
649         infiniteRule->doFormat(numberToFormat, toInsertInto, _pos + this->pos, recursionCount, status);
650         return;
651     }
652 
653     // if the result is an integer, from here on out we work in integer
654     // space (saving time and memory and preserving accuracy)
655     if (numberToFormat == uprv_floor(numberToFormat) && ruleSet != nullptr) {
656         ruleSet->format(util64_fromDouble(numberToFormat), toInsertInto, _pos + this->pos, recursionCount, status);
657 
658         // if the result isn't an integer, then call either our rule set's
659         // format() method or our DecimalFormat's format() method to
660         // format the result
661     } else {
662         if (ruleSet != nullptr) {
663             ruleSet->format(numberToFormat, toInsertInto, _pos + this->pos, recursionCount, status);
664         } else if (numberFormat != nullptr) {
665             UnicodeString temp;
666             numberFormat->format(numberToFormat, temp);
667             toInsertInto.insert(_pos + this->pos, temp);
668         }
669     }
670 }
671 
672 
673     //-----------------------------------------------------------------------
674     // parsing
675     //-----------------------------------------------------------------------
676 
677 #ifdef RBNF_DEBUG
678 #include <stdio.h>
679 #endif
680 
681 /**
682  * Parses a string using the rule set or DecimalFormat belonging
683  * to this substitution.  If there's a match, a mathematical
684  * operation (the inverse of the one used in formatting) is
685  * performed on the result of the parse and the value passed in
686  * and returned as the result.  The parse position is updated to
687  * point to the first unmatched character in the string.
688  * @param text The string to parse
689  * @param parsePosition On entry, ignored, but assumed to be 0.
690  * On exit, this is updated to point to the first unmatched
691  * character (or 0 if the substitution didn't match)
692  * @param baseValue A partial parse result that should be
693  * combined with the result of this parse
694  * @param upperBound When searching the rule set for a rule
695  * matching the string passed in, only rules with base values
696  * lower than this are considered
697  * @param lenientParse If true and matching against rules fails,
698  * the substitution will also try matching the text against
699  * numerals using a default-costructed NumberFormat.  If false,
700  * no extra work is done.  (This value is false whenever the
701  * formatter isn't in lenient-parse mode, but is also false
702  * under some conditions even when the formatter _is_ in
703  * lenient-parse mode.)
704  * @return If there's a match, this is the result of composing
705  * baseValue with whatever was returned from matching the
706  * characters.  This will be either a Long or a Double.  If there's
707  * no match this is new Long(0) (not null), and parsePosition
708  * is left unchanged.
709  */
710 UBool
doParse(const UnicodeString & text,ParsePosition & parsePosition,double baseValue,double upperBound,UBool lenientParse,uint32_t nonNumericalExecutedRuleMask,Formattable & result) const711 NFSubstitution::doParse(const UnicodeString& text,
712                         ParsePosition& parsePosition,
713                         double baseValue,
714                         double upperBound,
715                         UBool lenientParse,
716                         uint32_t nonNumericalExecutedRuleMask,
717                         Formattable& result) const
718 {
719 #ifdef RBNF_DEBUG
720     fprintf(stderr, "<nfsubs> %x bv: %g ub: %g\n", this, baseValue, upperBound);
721 #endif
722     // figure out the highest base value a rule can have and match
723     // the text being parsed (this varies according to the type of
724     // substitutions: multiplier, modulus, and numerator substitutions
725     // restrict the search to rules with base values lower than their
726     // own; same-value substitutions leave the upper bound wherever
727     // it was, and the others allow any rule to match
728     upperBound = calcUpperBound(upperBound);
729 
730     // use our rule set to parse the text.  If that fails and
731     // lenient parsing is enabled (this is always false if the
732     // formatter's lenient-parsing mode is off, but it may also
733     // be false even when the formatter's lenient-parse mode is
734     // on), then also try parsing the text using a default-
735     // constructed NumberFormat
736     if (ruleSet != nullptr) {
737         ruleSet->parse(text, parsePosition, upperBound, nonNumericalExecutedRuleMask, result);
738         if (lenientParse && !ruleSet->isFractionRuleSet() && parsePosition.getIndex() == 0) {
739             UErrorCode status = U_ZERO_ERROR;
740             NumberFormat* fmt = NumberFormat::createInstance(status);
741             if (U_SUCCESS(status)) {
742                 fmt->parse(text, result, parsePosition);
743             }
744             delete fmt;
745         }
746 
747         // ...or use our DecimalFormat to parse the text
748     } else if (numberFormat != nullptr) {
749         numberFormat->parse(text, result, parsePosition);
750     }
751 
752     // if the parse was successful, we've already advanced the caller's
753     // parse position (this is the one function that doesn't have one
754     // of its own).  Derive a parse result and return it as a Long,
755     // if possible, or a Double
756     if (parsePosition.getIndex() != 0) {
757         UErrorCode status = U_ZERO_ERROR;
758         double tempResult = result.getDouble(status);
759 
760         // composeRuleValue() produces a full parse result from
761         // the partial parse result passed to this function from
762         // the caller (this is either the owning rule's base value
763         // or the partial result obtained from composing the
764         // owning rule's base value with its other substitution's
765         // parse result) and the partial parse result obtained by
766         // matching the substitution (which will be the same value
767         // the caller would get by parsing just this part of the
768         // text with RuleBasedNumberFormat.parse() ).  How the two
769         // values are used to derive the full parse result depends
770         // on the types of substitutions: For a regular rule, the
771         // ultimate result is its multiplier substitution's result
772         // times the rule's divisor (or the rule's base value) plus
773         // the modulus substitution's result (which will actually
774         // supersede part of the rule's base value).  For a negative-
775         // number rule, the result is the negative of its substitution's
776         // result.  For a fraction rule, it's the sum of its two
777         // substitution results.  For a rule in a fraction rule set,
778         // it's the numerator substitution's result divided by
779         // the rule's base value.  Results from same-value substitutions
780         // propagate back upard, and null substitutions don't affect
781         // the result.
782         tempResult = composeRuleValue(tempResult, baseValue);
783         result.setDouble(tempResult);
784         return true;
785         // if the parse was UNsuccessful, return 0
786     } else {
787         result.setLong(0);
788         return false;
789     }
790 }
791 
792     /**
793      * Returns true if this is a modulus substitution.  (We didn't do this
794      * with instanceof partially because it causes source files to
795      * proliferate and partially because we have to port this to C++.)
796      * @return true if this object is an instance of ModulusSubstitution
797      */
798 UBool
isModulusSubstitution() const799 NFSubstitution::isModulusSubstitution() const {
800     return false;
801 }
802 
803 //===================================================================
804 // SameValueSubstitution
805 //===================================================================
806 
807 /**
808  * A substitution that passes the value passed to it through unchanged.
809  * Represented by == in rule descriptions.
810  */
SameValueSubstitution(int32_t _pos,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)811 SameValueSubstitution::SameValueSubstitution(int32_t _pos,
812                         const NFRuleSet* _ruleSet,
813                         const UnicodeString& description,
814                         UErrorCode& status)
815 : NFSubstitution(_pos, _ruleSet, description, status)
816 {
817     if (0 == description.compare(gEqualsEquals, 2)) {
818         // throw new IllegalArgumentException("== is not a legal token");
819         status = U_PARSE_ERROR;
820     }
821 }
822 
823 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SameValueSubstitution)
824 
825 //===================================================================
826 // MultiplierSubstitution
827 //===================================================================
828 
829 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MultiplierSubstitution)
830 
831 bool MultiplierSubstitution::operator==(const NFSubstitution& rhs) const
832 {
833     return NFSubstitution::operator==(rhs) &&
834         divisor == ((const MultiplierSubstitution*)&rhs)->divisor;
835 }
836 
837 
838 //===================================================================
839 // ModulusSubstitution
840 //===================================================================
841 
842 /**
843  * A substitution that divides the number being formatted by the its rule's
844  * divisor and formats the remainder.  Represented by "&gt;&gt;" in a
845  * regular rule.
846  */
ModulusSubstitution(int32_t _pos,const NFRule * rule,const NFRule * predecessor,const NFRuleSet * _ruleSet,const UnicodeString & description,UErrorCode & status)847 ModulusSubstitution::ModulusSubstitution(int32_t _pos,
848                                          const NFRule* rule,
849                                          const NFRule* predecessor,
850                                          const NFRuleSet* _ruleSet,
851                                          const UnicodeString& description,
852                                          UErrorCode& status)
853  : NFSubstitution(_pos, _ruleSet, description, status)
854  , divisor(rule->getDivisor())
855  , ruleToUse(nullptr)
856 {
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
860 
861   if (divisor == 0) {
862       status = U_PARSE_ERROR;
863   }
864 
865   if (0 == description.compare(gGreaterGreaterGreaterThan, 3)) {
866     // the >>> token doesn't alter how this substitution 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;
872   }
873 }
874 
875 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ModulusSubstitution)
876 
877 bool ModulusSubstitution::operator==(const NFSubstitution& rhs) const
878 {
879   return NFSubstitution::operator==(rhs) &&
880   divisor == ((const ModulusSubstitution*)&rhs)->divisor &&
881   ruleToUse == ((const ModulusSubstitution*)&rhs)->ruleToUse;
882 }
883 
884 //-----------------------------------------------------------------------
885 // formatting
886 //-----------------------------------------------------------------------
887 
888 
889 /**
890  * If this is a &gt;&gt;&gt; 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
894  * into
895  * @param pos The position of the rule text in toInsertInto
896  */
897 void
doSubstitution(int64_t number,UnicodeString & toInsertInto,int32_t _pos,int32_t recursionCount,UErrorCode & status) const898 ModulusSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
899 {
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 == nullptr) {
904         NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
905 
906         // a >>> substitution goes straight to a particular rule to
907         // format the substitution value
908     } else {
909         int64_t numberToFormat = transformNumber(number);
910         ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos(), recursionCount, status);
911     }
912 }
913 
914 /**
915 * If this is a &gt;&gt;&gt; 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
919 * into
920 * @param pos The position of the rule text in toInsertInto
921 */
922 void
doSubstitution(double number,UnicodeString & toInsertInto,int32_t _pos,int32_t recursionCount,UErrorCode & status) const923 ModulusSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
924 {
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 == nullptr) {
929         NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
930 
931         // a >>> substitution goes straight to a particular rule to
932         // format the substitution value
933     } else {
934         double numberToFormat = transformNumber(number);
935 
936         ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos(), recursionCount, status);
937     }
938 }
939 
940 //-----------------------------------------------------------------------
941 // parsing
942 //-----------------------------------------------------------------------
943 
944 /**
945  * If this is a &gt;&gt;&gt; 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
951  * routine.
952  */
953 UBool
doParse(const UnicodeString & text,ParsePosition & parsePosition,double baseValue,double upperBound,UBool lenientParse,uint32_t nonNumericalExecutedRuleMask,Formattable & result) const954 ModulusSubstitution::doParse(const UnicodeString& text,
955                              ParsePosition& parsePosition,
956                              double baseValue,
957                              double upperBound,
958                              UBool lenientParse,
959                              uint32_t nonNumericalExecutedRuleMask,
960                              Formattable& result) const
961 {
962     // if this isn't a >>> substitution, we can just use the
963     // inherited parse() routine to do the parsing
964     if (ruleToUse == nullptr) {
965         return NFSubstitution::doParse(text, parsePosition, baseValue, upperBound, lenientParse, nonNumericalExecutedRuleMask, result);
966 
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()
970     } else {
971         ruleToUse->doParse(text, parsePosition, false, upperBound, nonNumericalExecutedRuleMask, result);
972 
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);
978         }
979 
980         return true;
981     }
982 }
983 /**
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.
988  */
989 void
toString(UnicodeString & text) const990 ModulusSubstitution::toString(UnicodeString& text) const
991 {
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
996 
997   if ( ruleToUse != nullptr ) { // Must have been a >>> substitution.
998       text.remove();
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);
1004   }
1005 }
1006 //===================================================================
1007 // IntegralPartSubstitution
1008 //===================================================================
1009 
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IntegralPartSubstitution)1010 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IntegralPartSubstitution)
1011 
1012 
1013 //===================================================================
1014 // FractionalPartSubstitution
1015 //===================================================================
1016 
1017 
1018     /**
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.
1022      */
1023 FractionalPartSubstitution::FractionalPartSubstitution(int32_t _pos,
1024                              const NFRuleSet* _ruleSet,
1025                              const UnicodeString& description,
1026                              UErrorCode& status)
1027  : NFSubstitution(_pos, _ruleSet, description, status)
1028  , byDigits(false)
1029  , useSpaces(true)
1030 
1031 {
1032     // akk, ruleSet can change in superclass constructor
1033     if (0 == description.compare(gGreaterGreaterThan, 2) ||
1034         0 == description.compare(gGreaterGreaterGreaterThan, 3) ||
1035         _ruleSet == getRuleSet()) {
1036         byDigits = true;
1037         if (0 == description.compare(gGreaterGreaterGreaterThan, 3)) {
1038             useSpaces = false;
1039         }
1040     } else {
1041         // cast away const
1042         ((NFRuleSet*)getRuleSet())->makeIntoFractionRuleSet();
1043     }
1044 }
1045 
1046 //-----------------------------------------------------------------------
1047 // formatting
1048 //-----------------------------------------------------------------------
1049 
1050 /**
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
1058  * toInsertInto
1059  */
1060 void
doSubstitution(double number,UnicodeString & toInsertInto,int32_t _pos,int32_t recursionCount,UErrorCode & status) const1061 FractionalPartSubstitution::doSubstitution(double number, UnicodeString& toInsertInto,
1062                                            int32_t _pos, int32_t recursionCount, UErrorCode& status) const
1063 {
1064   // if we're not in "byDigits" mode, just use the inherited
1065   // doSubstitution() routine
1066   if (!byDigits) {
1067     NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
1068 
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
1074     // other end)
1075   } else {
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);
1086     //                  }
1087     //                  doZeros = true;
1088     //                  getRuleSet()->format(digit, toInsertInto, _pos + getPos());
1089     //              }
1090     //              numberToFormat /= 10;
1091     //          }
1092 
1093     DecimalQuantity dl;
1094     dl.setToDouble(number);
1095     dl.roundToMagnitude(-20, UNUM_ROUND_HALFEVEN, status);     // round to 20 fraction digits.
1096 
1097     UBool pad = false;
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);
1104       } else {
1105         pad = true;
1106       }
1107       int64_t digit = dl.getDigit(didx);
1108       getRuleSet()->format(digit, toInsertInto, _pos + getPos(), recursionCount, status);
1109     }
1110 
1111     if (!pad) {
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);
1115     }
1116   }
1117 }
1118 
1119 //-----------------------------------------------------------------------
1120 // parsing
1121 //-----------------------------------------------------------------------
1122 
1123 /**
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
1130  * function
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
1137  * a Double.
1138  */
1139 
1140 UBool
doParse(const UnicodeString & text,ParsePosition & parsePosition,double baseValue,double,UBool lenientParse,uint32_t nonNumericalExecutedRuleMask,Formattable & resVal) const1141 FractionalPartSubstitution::doParse(const UnicodeString& text,
1142                 ParsePosition& parsePosition,
1143                 double baseValue,
1144                 double /*upperBound*/,
1145                 UBool lenientParse,
1146                 uint32_t nonNumericalExecutedRuleMask,
1147                 Formattable& resVal) const
1148 {
1149     // if we're not in byDigits mode, we can just use the inherited
1150     // doParse()
1151     if (!byDigits) {
1152         return NFSubstitution::doParse(text, parsePosition, baseValue, 0, lenientParse, nonNumericalExecutedRuleMask, resVal);
1153 
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
1157         // nonmatching text
1158     } else {
1159         UnicodeString workText(text);
1160         ParsePosition workPos(1);
1161         double result = 0;
1162         int32_t digit;
1163 //          double p10 = 0.1;
1164 
1165         DecimalQuantity dl;
1166         int32_t totalDigits = 0;
1167         NumberFormat* fmt = nullptr;
1168         while (workText.length() > 0 && workPos.getIndex() != 0) {
1169             workPos.setIndex(0);
1170             Formattable temp;
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 ?
1175 //               temp.getLong() :
1176 //            (int32_t)temp.getDouble();
1177 
1178             if (lenientParse && workPos.getIndex() == 0) {
1179                 if (!fmt) {
1180                     status = U_ZERO_ERROR;
1181                     fmt = NumberFormat::createInstance(status);
1182                     if (U_FAILURE(status)) {
1183                         delete fmt;
1184                         fmt = nullptr;
1185                     }
1186                 }
1187                 if (fmt) {
1188                     fmt->parse(workText, temp, workPos);
1189                     digit = temp.getLong(status);
1190                 }
1191             }
1192 
1193             if (workPos.getIndex() != 0) {
1194                 dl.appendDigit(static_cast<int8_t>(digit), 0, true);
1195                 totalDigits++;
1196 //                  result += digit * p10;
1197 //                  p10 /= 10;
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);
1203                 }
1204             }
1205         }
1206         delete fmt;
1207 
1208         dl.adjustMagnitude(-totalDigits);
1209         result = dl.toDouble();
1210         result = composeRuleValue(result, baseValue);
1211         resVal.setDouble(result);
1212         return true;
1213     }
1214 }
1215 
1216 bool
operator ==(const NFSubstitution & rhs) const1217 FractionalPartSubstitution::operator==(const NFSubstitution& rhs) const
1218 {
1219   return NFSubstitution::operator==(rhs) &&
1220   ((const FractionalPartSubstitution*)&rhs)->byDigits == byDigits;
1221 }
1222 
1223 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FractionalPartSubstitution)
1224 
1225 
1226 //===================================================================
1227 // AbsoluteValueSubstitution
1228 //===================================================================
1229 
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(AbsoluteValueSubstitution)1230 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(AbsoluteValueSubstitution)
1231 
1232 //===================================================================
1233 // NumeratorSubstitution
1234 //===================================================================
1235 
1236 void
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
1240 
1241     double numberToFormat = transformNumber(number);
1242     int64_t longNF = util64_fromDouble(numberToFormat);
1243 
1244     const NFRuleSet* aruleSet = getRuleSet();
1245     if (withZeros && aruleSet != nullptr) {
1246         // if there are leading zeros in the decimal expansion then emit them
1247         int64_t nf =longNF;
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);
1252         }
1253         apos += toInsertInto.length() - len;
1254     }
1255 
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 != nullptr) {
1259         aruleSet->format(longNF, toInsertInto, apos + getPos(), recursionCount, status);
1260 
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
1264     } else {
1265         if (aruleSet != nullptr) {
1266             aruleSet->format(numberToFormat, toInsertInto, apos + getPos(), recursionCount, status);
1267         } else {
1268             UnicodeString temp;
1269             getNumberFormat()->format(numberToFormat, temp, status);
1270             toInsertInto.insert(apos + getPos(), temp);
1271         }
1272     }
1273 }
1274 
1275 UBool
doParse(const UnicodeString & text,ParsePosition & parsePosition,double baseValue,double upperBound,UBool,uint32_t nonNumericalExecutedRuleMask,Formattable & result) const1276 NumeratorSubstitution::doParse(const UnicodeString& text,
1277                                ParsePosition& parsePosition,
1278                                double baseValue,
1279                                double upperBound,
1280                                UBool /*lenientParse*/,
1281                                uint32_t nonNumericalExecutedRuleMask,
1282                                Formattable& result) const
1283 {
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
1287 
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);
1293 
1294     if (withZeros) {
1295         ParsePosition workPos(1);
1296         Formattable temp;
1297 
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
1304                 break;
1305             }
1306 
1307             ++zeroCount;
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);
1313             }
1314         }
1315 
1316         workText = text;
1317         workText.remove(0, (int32_t)parsePosition.getIndex());
1318         parsePosition.setIndex(0);
1319     }
1320 
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);
1323 
1324     if (withZeros) {
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?
1327 
1328         // compute the 'effective' base and prescale the value down
1329         int64_t n = result.getLong(status); // force conversion!
1330         int64_t d = 1;
1331         while (d <= n) {
1332             d *= 10;
1333         }
1334         // now add the zeros
1335         while (zeroCount > 0) {
1336             d *= 10;
1337             --zeroCount;
1338         }
1339         // d is now our true denominator
1340         result.setDouble((double)n/(double)d);
1341     }
1342 
1343     return true;
1344 }
1345 
1346 bool
operator ==(const NFSubstitution & rhs) const1347 NumeratorSubstitution::operator==(const NFSubstitution& rhs) const
1348 {
1349     return NFSubstitution::operator==(rhs) &&
1350         denominator == ((const NumeratorSubstitution*)&rhs)->denominator;
1351 }
1352 
1353 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NumeratorSubstitution)
1354 
1355 const char16_t NumeratorSubstitution::LTLT[] = { 0x003c, 0x003c };
1356 
1357 U_NAMESPACE_END
1358 
1359 /* U_HAVE_RBNF */
1360 #endif
1361 
1362