]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/nfsubs.cpp
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / nfsubs.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4******************************************************************************
2ca993e8 5* Copyright (C) 1997-2015, International Business Machines
b75a7d8f
A
6* Corporation and others. All Rights Reserved.
7******************************************************************************
8* file name: nfsubs.cpp
f3c0d7a5 9* encoding: UTF-8
b75a7d8f
A
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
729e4ab9 18#include <stdio.h>
51004dcb 19#include "utypeinfo.h" // for 'typeid' to work
729e4ab9 20
b75a7d8f 21#include "nfsubs.h"
f3c0d7a5 22#include "fmtableimp.h"
3d1f044b
A
23#include "putilimp.h"
24#include "number_decimalquantity.h"
374ca955 25
b75a7d8f
A
26#if U_HAVE_RBNF
27
28static const UChar gLessThan = 0x003c;
29static const UChar gEquals = 0x003d;
30static const UChar gGreaterThan = 0x003e;
31static const UChar gPercent = 0x0025;
32static const UChar gPound = 0x0023;
33static const UChar gZero = 0x0030;
34static const UChar gSpace = 0x0020;
35
36static const UChar gEqualsEquals[] =
37{
38 0x3D, 0x3D, 0
39}; /* "==" */
40static const UChar gGreaterGreaterGreaterThan[] =
41{
42 0x3E, 0x3E, 0x3E, 0
43}; /* ">>>" */
44static const UChar gGreaterGreaterThan[] =
45{
46 0x3E, 0x3E, 0
47}; /* ">>" */
48
46f4442e
A
49U_NAMESPACE_BEGIN
50
3d1f044b
A
51using number::impl::DecimalQuantity;
52
46f4442e
A
53class SameValueSubstitution : public NFSubstitution {
54public:
55 SameValueSubstitution(int32_t pos,
56 const NFRuleSet* ruleset,
46f4442e
A
57 const UnicodeString& description,
58 UErrorCode& status);
4388f060 59 virtual ~SameValueSubstitution();
46f4442e
A
60
61 virtual int64_t transformNumber(int64_t number) const { return number; }
62 virtual double transformNumber(double number) const { return number; }
63 virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const { return newRuleValue; }
64 virtual double calcUpperBound(double oldUpperBound) const { return oldUpperBound; }
65 virtual UChar tokenChar() const { return (UChar)0x003d; } // '='
66
67public:
68 static UClassID getStaticClassID(void);
69 virtual UClassID getDynamicClassID(void) const;
70};
71
4388f060
A
72SameValueSubstitution::~SameValueSubstitution() {}
73
46f4442e 74class MultiplierSubstitution : public NFSubstitution {
f3c0d7a5 75 int64_t divisor;
46f4442e
A
76
77public:
78 MultiplierSubstitution(int32_t _pos,
f3c0d7a5 79 const NFRule *rule,
46f4442e 80 const NFRuleSet* _ruleSet,
46f4442e
A
81 const UnicodeString& description,
82 UErrorCode& status)
f3c0d7a5 83 : NFSubstitution(_pos, _ruleSet, description, status), divisor(rule->getDivisor())
46f4442e 84 {
46f4442e
A
85 if (divisor == 0) {
86 status = U_PARSE_ERROR;
87 }
88 }
4388f060 89 virtual ~MultiplierSubstitution();
46f4442e 90
f3c0d7a5
A
91 virtual void setDivisor(int32_t radix, int16_t exponent, UErrorCode& status) {
92 divisor = util64_pow(radix, exponent);
46f4442e
A
93
94 if(divisor == 0) {
95 status = U_PARSE_ERROR;
96 }
97 }
98
99 virtual UBool operator==(const NFSubstitution& rhs) const;
100
101 virtual int64_t transformNumber(int64_t number) const {
f3c0d7a5 102 return number / divisor;
46f4442e
A
103 }
104
105 virtual double transformNumber(double number) const {
106 if (getRuleSet()) {
107 return uprv_floor(number / divisor);
108 } else {
f3c0d7a5 109 return number / divisor;
46f4442e
A
110 }
111 }
112
113 virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const {
114 return newRuleValue * divisor;
115 }
116
0f5d89e8 117 virtual double calcUpperBound(double /*oldUpperBound*/) const { return static_cast<double>(divisor); }
46f4442e
A
118
119 virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
120
121public:
122 static UClassID getStaticClassID(void);
123 virtual UClassID getDynamicClassID(void) const;
124};
125
4388f060
A
126MultiplierSubstitution::~MultiplierSubstitution() {}
127
46f4442e 128class ModulusSubstitution : public NFSubstitution {
f3c0d7a5 129 int64_t divisor;
46f4442e
A
130 const NFRule* ruleToUse;
131public:
132 ModulusSubstitution(int32_t pos,
f3c0d7a5 133 const NFRule* rule,
46f4442e
A
134 const NFRule* rulePredecessor,
135 const NFRuleSet* ruleSet,
46f4442e
A
136 const UnicodeString& description,
137 UErrorCode& status);
4388f060 138 virtual ~ModulusSubstitution();
46f4442e 139
f3c0d7a5
A
140 virtual void setDivisor(int32_t radix, int16_t exponent, UErrorCode& status) {
141 divisor = util64_pow(radix, exponent);
46f4442e
A
142
143 if (divisor == 0) {
144 status = U_PARSE_ERROR;
145 }
146 }
147
148 virtual UBool operator==(const NFSubstitution& rhs) const;
149
2ca993e8
A
150 virtual void doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
151 virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
46f4442e 152
f3c0d7a5 153 virtual int64_t transformNumber(int64_t number) const { return number % divisor; }
0f5d89e8 154 virtual double transformNumber(double number) const { return uprv_fmod(number, static_cast<double>(divisor)); }
46f4442e
A
155
156 virtual UBool doParse(const UnicodeString& text,
157 ParsePosition& parsePosition,
158 double baseValue,
159 double upperBound,
160 UBool lenientParse,
0f5d89e8 161 uint32_t nonNumericalExecutedRuleMask,
46f4442e
A
162 Formattable& result) const;
163
164 virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const {
0f5d89e8 165 return oldRuleValue - uprv_fmod(oldRuleValue, static_cast<double>(divisor)) + newRuleValue;
46f4442e
A
166 }
167
0f5d89e8 168 virtual double calcUpperBound(double /*oldUpperBound*/) const { return static_cast<double>(divisor); }
46f4442e
A
169
170 virtual UBool isModulusSubstitution() const { return TRUE; }
171
172 virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
173
4388f060
A
174 virtual void toString(UnicodeString& result) const;
175
46f4442e
A
176public:
177 static UClassID getStaticClassID(void);
178 virtual UClassID getDynamicClassID(void) const;
179};
180
4388f060
A
181ModulusSubstitution::~ModulusSubstitution() {}
182
46f4442e
A
183class IntegralPartSubstitution : public NFSubstitution {
184public:
185 IntegralPartSubstitution(int32_t _pos,
186 const NFRuleSet* _ruleSet,
46f4442e
A
187 const UnicodeString& description,
188 UErrorCode& status)
2ca993e8 189 : NFSubstitution(_pos, _ruleSet, description, status) {}
4388f060 190 virtual ~IntegralPartSubstitution();
46f4442e
A
191
192 virtual int64_t transformNumber(int64_t number) const { return number; }
193 virtual double transformNumber(double number) const { return uprv_floor(number); }
194 virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue + oldRuleValue; }
195 virtual double calcUpperBound(double /*oldUpperBound*/) const { return DBL_MAX; }
196 virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
197
198public:
199 static UClassID getStaticClassID(void);
200 virtual UClassID getDynamicClassID(void) const;
201};
202
4388f060
A
203IntegralPartSubstitution::~IntegralPartSubstitution() {}
204
46f4442e
A
205class FractionalPartSubstitution : public NFSubstitution {
206 UBool byDigits;
207 UBool useSpaces;
208 enum { kMaxDecimalDigits = 8 };
209public:
210 FractionalPartSubstitution(int32_t pos,
211 const NFRuleSet* ruleSet,
46f4442e
A
212 const UnicodeString& description,
213 UErrorCode& status);
4388f060 214 virtual ~FractionalPartSubstitution();
46f4442e
A
215
216 virtual UBool operator==(const NFSubstitution& rhs) const;
217
2ca993e8
A
218 virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
219 virtual void doSubstitution(int64_t /*number*/, UnicodeString& /*toInsertInto*/, int32_t /*_pos*/, int32_t /*recursionCount*/, UErrorCode& /*status*/) const {}
46f4442e
A
220 virtual int64_t transformNumber(int64_t /*number*/) const { return 0; }
221 virtual double transformNumber(double number) const { return number - uprv_floor(number); }
222
223 virtual UBool doParse(const UnicodeString& text,
224 ParsePosition& parsePosition,
225 double baseValue,
226 double upperBound,
227 UBool lenientParse,
0f5d89e8 228 uint32_t nonNumericalExecutedRuleMask,
46f4442e
A
229 Formattable& result) const;
230
231 virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue + oldRuleValue; }
232 virtual double calcUpperBound(double /*oldUpperBound*/) const { return 0.0; }
233 virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
234
235public:
236 static UClassID getStaticClassID(void);
237 virtual UClassID getDynamicClassID(void) const;
238};
239
4388f060
A
240FractionalPartSubstitution::~FractionalPartSubstitution() {}
241
46f4442e
A
242class AbsoluteValueSubstitution : public NFSubstitution {
243public:
244 AbsoluteValueSubstitution(int32_t _pos,
245 const NFRuleSet* _ruleSet,
46f4442e
A
246 const UnicodeString& description,
247 UErrorCode& status)
2ca993e8 248 : NFSubstitution(_pos, _ruleSet, description, status) {}
4388f060 249 virtual ~AbsoluteValueSubstitution();
46f4442e
A
250
251 virtual int64_t transformNumber(int64_t number) const { return number >= 0 ? number : -number; }
252 virtual double transformNumber(double number) const { return uprv_fabs(number); }
253 virtual double composeRuleValue(double newRuleValue, double /*oldRuleValue*/) const { return -newRuleValue; }
254 virtual double calcUpperBound(double /*oldUpperBound*/) const { return DBL_MAX; }
255 virtual UChar tokenChar() const { return (UChar)0x003e; } // '>'
256
257public:
258 static UClassID getStaticClassID(void);
259 virtual UClassID getDynamicClassID(void) const;
260};
261
4388f060
A
262AbsoluteValueSubstitution::~AbsoluteValueSubstitution() {}
263
46f4442e
A
264class NumeratorSubstitution : public NFSubstitution {
265 double denominator;
266 int64_t ldenominator;
267 UBool withZeros;
268public:
269 static inline UnicodeString fixdesc(const UnicodeString& desc) {
270 if (desc.endsWith(LTLT, 2)) {
271 UnicodeString result(desc, 0, desc.length()-1);
272 return result;
273 }
274 return desc;
275 }
276 NumeratorSubstitution(int32_t _pos,
277 double _denominator,
2ca993e8 278 NFRuleSet* _ruleSet,
46f4442e
A
279 const UnicodeString& description,
280 UErrorCode& status)
2ca993e8 281 : NFSubstitution(_pos, _ruleSet, fixdesc(description), status), denominator(_denominator)
46f4442e
A
282 {
283 ldenominator = util64_fromDouble(denominator);
284 withZeros = description.endsWith(LTLT, 2);
285 }
4388f060 286 virtual ~NumeratorSubstitution();
46f4442e
A
287
288 virtual UBool operator==(const NFSubstitution& rhs) const;
289
290 virtual int64_t transformNumber(int64_t number) const { return number * ldenominator; }
291 virtual double transformNumber(double number) const { return uprv_round(number * denominator); }
292
2ca993e8
A
293 virtual void doSubstitution(int64_t /*number*/, UnicodeString& /*toInsertInto*/, int32_t /*_pos*/, int32_t /*recursionCount*/, UErrorCode& /*status*/) const {}
294 virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
46f4442e
A
295 virtual UBool doParse(const UnicodeString& text,
296 ParsePosition& parsePosition,
297 double baseValue,
298 double upperBound,
299 UBool /*lenientParse*/,
0f5d89e8 300 uint32_t nonNumericalExecutedRuleMask,
46f4442e
A
301 Formattable& result) const;
302
303 virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const { return newRuleValue / oldRuleValue; }
304 virtual double calcUpperBound(double /*oldUpperBound*/) const { return denominator; }
305 virtual UChar tokenChar() const { return (UChar)0x003c; } // '<'
306private:
307 static const UChar LTLT[2];
308
309public:
310 static UClassID getStaticClassID(void);
311 virtual UClassID getDynamicClassID(void) const;
312};
313
4388f060
A
314NumeratorSubstitution::~NumeratorSubstitution() {}
315
b75a7d8f
A
316NFSubstitution*
317NFSubstitution::makeSubstitution(int32_t pos,
318 const NFRule* rule,
319 const NFRule* predecessor,
320 const NFRuleSet* ruleSet,
321 const RuleBasedNumberFormat* formatter,
322 const UnicodeString& description,
323 UErrorCode& status)
324{
325 // if the description is empty, return a NullSubstitution
326 if (description.length() == 0) {
2ca993e8 327 return NULL;
b75a7d8f
A
328 }
329
330 switch (description.charAt(0)) {
331 // if the description begins with '<'...
332 case gLessThan:
333 // throw an exception if the rule is a negative number
334 // rule
335 if (rule->getBaseValue() == NFRule::kNegativeNumberRule) {
336 // throw new IllegalArgumentException("<< not allowed in negative-number rule");
337 status = U_PARSE_ERROR;
338 return NULL;
339 }
340
341 // if the rule is a fraction rule, return an
342 // IntegralPartSubstitution
343 else if (rule->getBaseValue() == NFRule::kImproperFractionRule
344 || rule->getBaseValue() == NFRule::kProperFractionRule
345 || rule->getBaseValue() == NFRule::kMasterRule) {
2ca993e8 346 return new IntegralPartSubstitution(pos, ruleSet, description, status);
b75a7d8f
A
347 }
348
349 // if the rule set containing the rule is a fraction
350 // rule set, return a NumeratorSubstitution
351 else if (ruleSet->isFractionRuleSet()) {
352 return new NumeratorSubstitution(pos, (double)rule->getBaseValue(),
2ca993e8 353 formatter->getDefaultRuleSet(), description, status);
b75a7d8f
A
354 }
355
356 // otherwise, return a MultiplierSubstitution
357 else {
f3c0d7a5 358 return new MultiplierSubstitution(pos, rule, ruleSet,
2ca993e8 359 description, status);
b75a7d8f
A
360 }
361
362 // if the description begins with '>'...
363 case gGreaterThan:
364 // if the rule is a negative-number rule, return
365 // an AbsoluteValueSubstitution
366 if (rule->getBaseValue() == NFRule::kNegativeNumberRule) {
2ca993e8 367 return new AbsoluteValueSubstitution(pos, ruleSet, description, status);
b75a7d8f
A
368 }
369
370 // if the rule is a fraction rule, return a
371 // FractionalPartSubstitution
372 else if (rule->getBaseValue() == NFRule::kImproperFractionRule
373 || rule->getBaseValue() == NFRule::kProperFractionRule
374 || rule->getBaseValue() == NFRule::kMasterRule) {
2ca993e8 375 return new FractionalPartSubstitution(pos, ruleSet, description, status);
b75a7d8f
A
376 }
377
378 // if the rule set owning the rule is a fraction rule set,
379 // throw an exception
380 else if (ruleSet->isFractionRuleSet()) {
381 // throw new IllegalArgumentException(">> not allowed in fraction rule set");
382 status = U_PARSE_ERROR;
383 return NULL;
384 }
385
386 // otherwise, return a ModulusSubstitution
387 else {
f3c0d7a5 388 return new ModulusSubstitution(pos, rule, predecessor,
2ca993e8 389 ruleSet, description, status);
b75a7d8f
A
390 }
391
392 // if the description begins with '=', always return a
393 // SameValueSubstitution
394 case gEquals:
2ca993e8 395 return new SameValueSubstitution(pos, ruleSet, description, status);
b75a7d8f
A
396
397 // and if it's anything else, throw an exception
398 default:
399 // throw new IllegalArgumentException("Illegal substitution character");
400 status = U_PARSE_ERROR;
401 }
402 return NULL;
403}
404
405NFSubstitution::NFSubstitution(int32_t _pos,
406 const NFRuleSet* _ruleSet,
b75a7d8f
A
407 const UnicodeString& description,
408 UErrorCode& status)
409 : pos(_pos), ruleSet(NULL), numberFormat(NULL)
410{
411 // the description should begin and end with the same character.
412 // If it doesn't that's a syntax error. Otherwise,
413 // makeSubstitution() was the only thing that needed to know
414 // about these characters, so strip them off
415 UnicodeString workingDescription(description);
416 if (description.length() >= 2
417 && description.charAt(0) == description.charAt(description.length() - 1))
418 {
419 workingDescription.remove(description.length() - 1, 1);
420 workingDescription.remove(0, 1);
421 }
422 else if (description.length() != 0) {
423 // throw new IllegalArgumentException("Illegal substitution syntax");
424 status = U_PARSE_ERROR;
425 return;
426 }
427
b75a7d8f 428 if (workingDescription.length() == 0) {
2ca993e8
A
429 // if the description was just two paired token characters
430 // (i.e., "<<" or ">>"), it uses the rule set it belongs to to
431 // format its result
b75a7d8f
A
432 this->ruleSet = _ruleSet;
433 }
b75a7d8f 434 else if (workingDescription.charAt(0) == gPercent) {
2ca993e8
A
435 // if the description contains a rule set name, that's the rule
436 // set we use to format the result: get a reference to the
437 // names rule set
438 this->ruleSet = _ruleSet->getOwner()->findRuleSet(workingDescription, status);
b75a7d8f 439 }
b75a7d8f 440 else if (workingDescription.charAt(0) == gPound || workingDescription.charAt(0) ==gZero) {
2ca993e8
A
441 // if the description begins with 0 or #, treat it as a
442 // DecimalFormat pattern, and initialize a DecimalFormat with
443 // that pattern (then set it to use the DecimalFormatSymbols
444 // belonging to our formatter)
445 const DecimalFormatSymbols* sym = _ruleSet->getOwner()->getDecimalFormatSymbols();
374ca955
A
446 if (!sym) {
447 status = U_MISSING_RESOURCE_ERROR;
448 return;
449 }
2ca993e8 450 DecimalFormat *tempNumberFormat = new DecimalFormat(workingDescription, *sym, status);
b75a7d8f 451 /* test for NULL */
2ca993e8 452 if (!tempNumberFormat) {
b75a7d8f
A
453 status = U_MEMORY_ALLOCATION_ERROR;
454 return;
455 }
374ca955 456 if (U_FAILURE(status)) {
2ca993e8 457 delete tempNumberFormat;
374ca955
A
458 return;
459 }
2ca993e8 460 this->numberFormat = tempNumberFormat;
b75a7d8f 461 }
b75a7d8f 462 else if (workingDescription.charAt(0) == gGreaterThan) {
2ca993e8
A
463 // if the description is ">>>", this substitution bypasses the
464 // usual rule-search process and always uses the rule that precedes
465 // it in its own rule set's rule list (this is used for place-value
466 // notations: formats where you want to see a particular part of
467 // a number even when it's 0)
468
b75a7d8f
A
469 // this causes problems when >>> is used in a frationalPartSubstitution
470 // this->ruleSet = NULL;
471 this->ruleSet = _ruleSet;
472 this->numberFormat = NULL;
473 }
b75a7d8f 474 else {
2ca993e8
A
475 // and of the description is none of these things, it's a syntax error
476
b75a7d8f
A
477 // throw new IllegalArgumentException("Illegal substitution syntax");
478 status = U_PARSE_ERROR;
479 }
480}
481
482NFSubstitution::~NFSubstitution()
483{
2ca993e8
A
484 delete numberFormat;
485 numberFormat = NULL;
b75a7d8f
A
486}
487
488/**
489 * Set's the substitution's divisor. Used by NFRule.setBaseValue().
490 * A no-op for all substitutions except multiplier and modulus
491 * substitutions.
492 * @param radix The radix of the divisor
493 * @param exponent The exponent of the divisor
494 */
495void
f3c0d7a5 496NFSubstitution::setDivisor(int32_t /*radix*/, int16_t /*exponent*/, UErrorCode& /*status*/) {
b75a7d8f
A
497 // a no-op for all substitutions except multiplier and modulus substitutions
498}
499
2ca993e8
A
500void
501NFSubstitution::setDecimalFormatSymbols(const DecimalFormatSymbols &newSymbols, UErrorCode& /*status*/) {
502 if (numberFormat != NULL) {
503 numberFormat->setDecimalFormatSymbols(newSymbols);
504 }
505}
b75a7d8f
A
506
507//-----------------------------------------------------------------------
508// boilerplate
509//-----------------------------------------------------------------------
510
46f4442e 511UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NFSubstitution)
b75a7d8f 512
46f4442e
A
513/**
514 * Compares two substitutions for equality
515 * @param The substitution to compare this one to
516 * @return true if the two substitutions are functionally equivalent
517 */
b75a7d8f
A
518UBool
519NFSubstitution::operator==(const NFSubstitution& rhs) const
520{
521 // compare class and all of the fields all substitutions have
522 // in common
523 // this should be called by subclasses before their own equality tests
729e4ab9 524 return typeid(*this) == typeid(rhs)
b75a7d8f
A
525 && pos == rhs.pos
526 && (ruleSet == NULL) == (rhs.ruleSet == NULL)
527 // && ruleSet == rhs.ruleSet causes circularity, other checks to make instead?
528 && (numberFormat == NULL
529 ? (rhs.numberFormat == NULL)
530 : (*numberFormat == *rhs.numberFormat));
531}
532
46f4442e
A
533/**
534 * Returns a textual description of the substitution
535 * @return A textual description of the substitution. This might
536 * not be identical to the description it was created from, but
537 * it'll produce the same result.
538 */
b75a7d8f
A
539void
540NFSubstitution::toString(UnicodeString& text) const
541{
542 // use tokenChar() to get the character at the beginning and
543 // end of the substitutin token. In between them will go
544 // either the name of the rule set it uses, or the pattern of
545 // the DecimalFormat it uses
546 text.remove();
547 text.append(tokenChar());
548
549 UnicodeString temp;
550 if (ruleSet != NULL) {
551 ruleSet->getName(temp);
73c04bcf 552 } else if (numberFormat != NULL) {
b75a7d8f
A
553 numberFormat->toPattern(temp);
554 }
555 text.append(temp);
556 text.append(tokenChar());
557}
558
559//-----------------------------------------------------------------------
560// formatting
561//-----------------------------------------------------------------------
562
563/**
564 * Performs a mathematical operation on the number, formats it using
565 * either ruleSet or decimalFormat, and inserts the result into
566 * toInsertInto.
567 * @param number The number being formatted.
568 * @param toInsertInto The string we insert the result into
569 * @param pos The position in toInsertInto where the owning rule's
570 * rule text begins (this value is added to this substitution's
571 * position to determine exactly where to insert the new text)
572 */
573void
2ca993e8 574NFSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
b75a7d8f
A
575{
576 if (ruleSet != NULL) {
f3c0d7a5 577 // Perform a transformation on the number that is dependent
b75a7d8f
A
578 // on the type of substitution this is, then just call its
579 // rule set's format() method to format the result
2ca993e8 580 ruleSet->format(transformNumber(number), toInsertInto, _pos + this->pos, recursionCount, status);
73c04bcf 581 } else if (numberFormat != NULL) {
f3c0d7a5
A
582 if (number <= MAX_INT64_IN_DOUBLE) {
583 // or perform the transformation on the number (preserving
584 // the result's fractional part if the formatter it set
585 // to show it), then use that formatter's format() method
586 // to format the result
587 double numberToFormat = transformNumber((double)number);
588 if (numberFormat->getMaximumFractionDigits() == 0) {
589 numberToFormat = uprv_floor(numberToFormat);
590 }
b75a7d8f 591
f3c0d7a5
A
592 UnicodeString temp;
593 numberFormat->format(numberToFormat, temp, status);
594 toInsertInto.insert(_pos + this->pos, temp);
595 }
596 else {
597 // We have gone beyond double precision. Something has to give.
598 // We're favoring accuracy of the large number over potential rules
599 // that round like a CompactDecimalFormat, which is not a common use case.
600 //
601 // Perform a transformation on the number that is dependent
602 // on the type of substitution this is, then just call its
603 // rule set's format() method to format the result
604 int64_t numberToFormat = transformNumber(number);
605 UnicodeString temp;
606 numberFormat->format(numberToFormat, temp, status);
607 toInsertInto.insert(_pos + this->pos, temp);
608 }
b75a7d8f
A
609 }
610}
611
612/**
613 * Performs a mathematical operation on the number, formats it using
614 * either ruleSet or decimalFormat, and inserts the result into
615 * toInsertInto.
616 * @param number The number being formatted.
617 * @param toInsertInto The string we insert the result into
618 * @param pos The position in toInsertInto where the owning rule's
619 * rule text begins (this value is added to this substitution's
620 * position to determine exactly where to insert the new text)
621 */
622void
2ca993e8 623NFSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const {
b75a7d8f
A
624 // perform a transformation on the number being formatted that
625 // is dependent on the type of substitution this is
626 double numberToFormat = transformNumber(number);
2ca993e8
A
627
628 if (uprv_isInfinite(numberToFormat)) {
629 // This is probably a minus rule. Combine it with an infinite rule.
630 const NFRule *infiniteRule = ruleSet->findDoubleRule(uprv_getInfinity());
631 infiniteRule->doFormat(numberToFormat, toInsertInto, _pos + this->pos, recursionCount, status);
632 return;
633 }
b75a7d8f
A
634
635 // if the result is an integer, from here on out we work in integer
636 // space (saving time and memory and preserving accuracy)
2ca993e8
A
637 if (numberToFormat == uprv_floor(numberToFormat) && ruleSet != NULL) {
638 ruleSet->format(util64_fromDouble(numberToFormat), toInsertInto, _pos + this->pos, recursionCount, status);
b75a7d8f
A
639
640 // if the result isn't an integer, then call either our rule set's
641 // format() method or our DecimalFormat's format() method to
642 // format the result
643 } else {
644 if (ruleSet != NULL) {
2ca993e8 645 ruleSet->format(numberToFormat, toInsertInto, _pos + this->pos, recursionCount, status);
73c04bcf 646 } else if (numberFormat != NULL) {
b75a7d8f
A
647 UnicodeString temp;
648 numberFormat->format(numberToFormat, temp);
649 toInsertInto.insert(_pos + this->pos, temp);
650 }
651 }
652}
653
654
655 //-----------------------------------------------------------------------
656 // parsing
657 //-----------------------------------------------------------------------
658
659#ifdef RBNF_DEBUG
660#include <stdio.h>
661#endif
662
663/**
664 * Parses a string using the rule set or DecimalFormat belonging
665 * to this substitution. If there's a match, a mathematical
666 * operation (the inverse of the one used in formatting) is
667 * performed on the result of the parse and the value passed in
668 * and returned as the result. The parse position is updated to
669 * point to the first unmatched character in the string.
670 * @param text The string to parse
671 * @param parsePosition On entry, ignored, but assumed to be 0.
672 * On exit, this is updated to point to the first unmatched
673 * character (or 0 if the substitution didn't match)
674 * @param baseValue A partial parse result that should be
675 * combined with the result of this parse
676 * @param upperBound When searching the rule set for a rule
677 * matching the string passed in, only rules with base values
678 * lower than this are considered
679 * @param lenientParse If true and matching against rules fails,
680 * the substitution will also try matching the text against
681 * numerals using a default-costructed NumberFormat. If false,
682 * no extra work is done. (This value is false whenever the
683 * formatter isn't in lenient-parse mode, but is also false
684 * under some conditions even when the formatter _is_ in
685 * lenient-parse mode.)
686 * @return If there's a match, this is the result of composing
687 * baseValue with whatever was returned from matching the
688 * characters. This will be either a Long or a Double. If there's
689 * no match this is new Long(0) (not null), and parsePosition
690 * is left unchanged.
691 */
692UBool
693NFSubstitution::doParse(const UnicodeString& text,
694 ParsePosition& parsePosition,
695 double baseValue,
696 double upperBound,
697 UBool lenientParse,
0f5d89e8 698 uint32_t nonNumericalExecutedRuleMask,
b75a7d8f
A
699 Formattable& result) const
700{
701#ifdef RBNF_DEBUG
702 fprintf(stderr, "<nfsubs> %x bv: %g ub: %g\n", this, baseValue, upperBound);
703#endif
704 // figure out the highest base value a rule can have and match
705 // the text being parsed (this varies according to the type of
706 // substitutions: multiplier, modulus, and numerator substitutions
707 // restrict the search to rules with base values lower than their
708 // own; same-value substitutions leave the upper bound wherever
709 // it was, and the others allow any rule to match
710 upperBound = calcUpperBound(upperBound);
711
712 // use our rule set to parse the text. If that fails and
713 // lenient parsing is enabled (this is always false if the
714 // formatter's lenient-parsing mode is off, but it may also
715 // be false even when the formatter's lenient-parse mode is
716 // on), then also try parsing the text using a default-
717 // constructed NumberFormat
718 if (ruleSet != NULL) {
0f5d89e8 719 ruleSet->parse(text, parsePosition, upperBound, nonNumericalExecutedRuleMask, result);
b75a7d8f
A
720 if (lenientParse && !ruleSet->isFractionRuleSet() && parsePosition.getIndex() == 0) {
721 UErrorCode status = U_ZERO_ERROR;
722 NumberFormat* fmt = NumberFormat::createInstance(status);
723 if (U_SUCCESS(status)) {
724 fmt->parse(text, result, parsePosition);
725 }
726 delete fmt;
727 }
728
729 // ...or use our DecimalFormat to parse the text
73c04bcf 730 } else if (numberFormat != NULL) {
b75a7d8f
A
731 numberFormat->parse(text, result, parsePosition);
732 }
733
734 // if the parse was successful, we've already advanced the caller's
735 // parse position (this is the one function that doesn't have one
736 // of its own). Derive a parse result and return it as a Long,
737 // if possible, or a Double
738 if (parsePosition.getIndex() != 0) {
374ca955
A
739 UErrorCode status = U_ZERO_ERROR;
740 double tempResult = result.getDouble(status);
b75a7d8f
A
741
742 // composeRuleValue() produces a full parse result from
743 // the partial parse result passed to this function from
744 // the caller (this is either the owning rule's base value
745 // or the partial result obtained from composing the
746 // owning rule's base value with its other substitution's
747 // parse result) and the partial parse result obtained by
748 // matching the substitution (which will be the same value
749 // the caller would get by parsing just this part of the
750 // text with RuleBasedNumberFormat.parse() ). How the two
751 // values are used to derive the full parse result depends
752 // on the types of substitutions: For a regular rule, the
753 // ultimate result is its multiplier substitution's result
754 // times the rule's divisor (or the rule's base value) plus
755 // the modulus substitution's result (which will actually
756 // supersede part of the rule's base value). For a negative-
757 // number rule, the result is the negative of its substitution's
758 // result. For a fraction rule, it's the sum of its two
759 // substitution results. For a rule in a fraction rule set,
760 // it's the numerator substitution's result divided by
761 // the rule's base value. Results from same-value substitutions
762 // propagate back upard, and null substitutions don't affect
763 // the result.
764 tempResult = composeRuleValue(tempResult, baseValue);
765 result.setDouble(tempResult);
766 return TRUE;
767 // if the parse was UNsuccessful, return 0
768 } else {
769 result.setLong(0);
770 return FALSE;
771 }
772}
773
b75a7d8f
A
774 /**
775 * Returns true if this is a modulus substitution. (We didn't do this
776 * with instanceof partially because it causes source files to
777 * proliferate and partially because we have to port this to C++.)
778 * @return true if this object is an instance of ModulusSubstitution
779 */
780UBool
781NFSubstitution::isModulusSubstitution() const {
782 return FALSE;
783}
784
729e4ab9 785 /**
2ca993e8 786 * Apple addition
729e4ab9
A
787 * @return true if this is a decimal format-only substitution
788 */
789UBool
790NFSubstitution::isDecimalFormatSubstitutionOnly() const {
791 return (ruleSet == NULL && getNumberFormat() != NULL);
792}
793
794 /**
2ca993e8 795 * Apple addition, not currently used
729e4ab9
A
796 * @return true if this substitution uses another ruleSet
797 */
2ca993e8
A
798//UBool
799//NFSubstitution::isRuleSetSubstitutionOnly() const {
800// return (getNumberFormat() == NULL && ruleSet != NULL);
801//}
729e4ab9 802
b75a7d8f
A
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 */
811SameValueSubstitution::SameValueSubstitution(int32_t _pos,
812 const NFRuleSet* _ruleSet,
b75a7d8f
A
813 const UnicodeString& description,
814 UErrorCode& status)
2ca993e8 815: NFSubstitution(_pos, _ruleSet, description, status)
b75a7d8f 816{
4388f060 817 if (0 == description.compare(gEqualsEquals, 2)) {
b75a7d8f
A
818 // throw new IllegalArgumentException("== is not a legal token");
819 status = U_PARSE_ERROR;
820 }
821}
822
46f4442e 823UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SameValueSubstitution)
b75a7d8f
A
824
825//===================================================================
826// MultiplierSubstitution
827//===================================================================
828
46f4442e 829UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MultiplierSubstitution)
b75a7d8f
A
830
831UBool 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 */
847ModulusSubstitution::ModulusSubstitution(int32_t _pos,
f3c0d7a5 848 const NFRule* rule,
b75a7d8f
A
849 const NFRule* predecessor,
850 const NFRuleSet* _ruleSet,
b75a7d8f
A
851 const UnicodeString& description,
852 UErrorCode& status)
2ca993e8 853 : NFSubstitution(_pos, _ruleSet, description, status)
f3c0d7a5 854 , divisor(rule->getDivisor())
b75a7d8f
A
855 , ruleToUse(NULL)
856{
b75a7d8f
A
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
f3c0d7a5 861 if (divisor == 0) {
374ca955
A
862 status = U_PARSE_ERROR;
863 }
864
4388f060 865 if (0 == description.compare(gGreaterGreaterGreaterThan, 3)) {
b75a7d8f
A
866 // the >>> token doesn't alter how this substituion calculates the
867 // values it uses for formatting and parsing, but it changes
868 // what's done with that value after it's obtained: >>> short-
869 // circuits the rule-search process and goes straight to the
870 // specified rule to format the substitution value
871 ruleToUse = predecessor;
872 }
873}
874
46f4442e 875UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ModulusSubstitution)
b75a7d8f
A
876
877UBool 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 */
897void
2ca993e8 898ModulusSubstitution::doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
b75a7d8f
A
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 == NULL) {
2ca993e8 904 NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
b75a7d8f
A
905
906 // a >>> substitution goes straight to a particular rule to
907 // format the substitution value
908 } else {
909 int64_t numberToFormat = transformNumber(number);
2ca993e8 910 ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos(), recursionCount, status);
b75a7d8f
A
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*/
922void
2ca993e8 923ModulusSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t _pos, int32_t recursionCount, UErrorCode& status) const
b75a7d8f
A
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 == NULL) {
2ca993e8 929 NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
b75a7d8f
A
930
931 // a >>> substitution goes straight to a particular rule to
932 // format the substitution value
933 } else {
934 double numberToFormat = transformNumber(number);
935
2ca993e8 936 ruleToUse->doFormat(numberToFormat, toInsertInto, _pos + getPos(), recursionCount, status);
b75a7d8f
A
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 */
953UBool
954ModulusSubstitution::doParse(const UnicodeString& text,
955 ParsePosition& parsePosition,
956 double baseValue,
957 double upperBound,
958 UBool lenientParse,
0f5d89e8 959 uint32_t nonNumericalExecutedRuleMask,
b75a7d8f
A
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 == NULL) {
0f5d89e8 965 return NFSubstitution::doParse(text, parsePosition, baseValue, upperBound, lenientParse, nonNumericalExecutedRuleMask, result);
b75a7d8f
A
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 {
0f5d89e8 971 ruleToUse->doParse(text, parsePosition, FALSE, upperBound, nonNumericalExecutedRuleMask, result);
b75a7d8f
A
972
973 if (parsePosition.getIndex() != 0) {
73c04bcf
A
974 UErrorCode status = U_ZERO_ERROR;
975 double tempResult = result.getDouble(status);
b75a7d8f
A
976 tempResult = composeRuleValue(tempResult, baseValue);
977 result.setDouble(tempResult);
978 }
979
980 return TRUE;
981 }
982}
4388f060
A
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 */
989void
990ModulusSubstitution::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
b75a7d8f 996
4388f060
A
997 if ( ruleToUse != NULL ) { // 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}
b75a7d8f
A
1006//===================================================================
1007// IntegralPartSubstitution
1008//===================================================================
1009
46f4442e 1010UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IntegralPartSubstitution)
b75a7d8f
A
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 */
1023FractionalPartSubstitution::FractionalPartSubstitution(int32_t _pos,
1024 const NFRuleSet* _ruleSet,
b75a7d8f
A
1025 const UnicodeString& description,
1026 UErrorCode& status)
2ca993e8 1027 : NFSubstitution(_pos, _ruleSet, description, status)
b75a7d8f
A
1028 , byDigits(FALSE)
1029 , useSpaces(TRUE)
1030
1031{
1032 // akk, ruleSet can change in superclass constructor
4388f060
A
1033 if (0 == description.compare(gGreaterGreaterThan, 2) ||
1034 0 == description.compare(gGreaterGreaterGreaterThan, 3) ||
b75a7d8f
A
1035 _ruleSet == getRuleSet()) {
1036 byDigits = TRUE;
4388f060 1037 if (0 == description.compare(gGreaterGreaterGreaterThan, 3)) {
b75a7d8f
A
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 */
1060void
b331163b 1061FractionalPartSubstitution::doSubstitution(double number, UnicodeString& toInsertInto,
2ca993e8 1062 int32_t _pos, int32_t recursionCount, UErrorCode& status) const
b75a7d8f 1063{
374ca955
A
1064 // if we're not in "byDigits" mode, just use the inherited
1065 // doSubstitution() routine
1066 if (!byDigits) {
2ca993e8 1067 NFSubstitution::doSubstitution(number, toInsertInto, _pos, recursionCount, status);
374ca955
A
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
3d1f044b
A
1093 DecimalQuantity dl;
1094 dl.setToDouble(number);
1095 dl.roundToMagnitude(-20, UNUM_ROUND_HALFEVEN, status); // round to 20 fraction digits.
374ca955
A
1096
1097 UBool pad = FALSE;
3d1f044b 1098 for (int32_t didx = dl.getLowerDisplayMagnitude(); didx<0; didx++) {
729e4ab9
A
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.
374ca955
A
1102 if (pad && useSpaces) {
1103 toInsertInto.insert(_pos + getPos(), gSpace);
1104 } else {
1105 pad = TRUE;
1106 }
3d1f044b 1107 int64_t digit = dl.getDigit(didx);
2ca993e8 1108 getRuleSet()->format(digit, toInsertInto, _pos + getPos(), recursionCount, status);
374ca955 1109 }
b75a7d8f 1110
374ca955
A
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.
2ca993e8 1114 getRuleSet()->format((int64_t)0, toInsertInto, _pos + getPos(), recursionCount, status);
b75a7d8f 1115 }
374ca955 1116 }
b75a7d8f
A
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
1140UBool
1141FractionalPartSubstitution::doParse(const UnicodeString& text,
1142 ParsePosition& parsePosition,
1143 double baseValue,
1144 double /*upperBound*/,
1145 UBool lenientParse,
0f5d89e8 1146 uint32_t nonNumericalExecutedRuleMask,
b75a7d8f
A
1147 Formattable& resVal) const
1148{
1149 // if we're not in byDigits mode, we can just use the inherited
1150 // doParse()
1151 if (!byDigits) {
0f5d89e8 1152 return NFSubstitution::doParse(text, parsePosition, baseValue, 0, lenientParse, nonNumericalExecutedRuleMask, resVal);
b75a7d8f
A
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;
374ca955 1163// double p10 = 0.1;
b75a7d8f 1164
3d1f044b
A
1165 DecimalQuantity dl;
1166 int32_t totalDigits = 0;
b75a7d8f
A
1167 NumberFormat* fmt = NULL;
1168 while (workText.length() > 0 && workPos.getIndex() != 0) {
1169 workPos.setIndex(0);
1170 Formattable temp;
0f5d89e8 1171 getRuleSet()->parse(workText, workPos, 10, nonNumericalExecutedRuleMask, temp);
374ca955
A
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();
b75a7d8f
A
1177
1178 if (lenientParse && workPos.getIndex() == 0) {
1179 if (!fmt) {
374ca955 1180 status = U_ZERO_ERROR;
b75a7d8f
A
1181 fmt = NumberFormat::createInstance(status);
1182 if (U_FAILURE(status)) {
1183 delete fmt;
1184 fmt = NULL;
1185 }
1186 }
1187 if (fmt) {
1188 fmt->parse(workText, temp, workPos);
73c04bcf 1189 digit = temp.getLong(status);
b75a7d8f
A
1190 }
1191 }
1192
1193 if (workPos.getIndex() != 0) {
3d1f044b
A
1194 dl.appendDigit(static_cast<int8_t>(digit), 0, true);
1195 totalDigits++;
374ca955
A
1196// result += digit * p10;
1197// p10 /= 10;
b75a7d8f
A
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
3d1f044b
A
1208 dl.adjustMagnitude(-totalDigits);
1209 result = dl.toDouble();
b75a7d8f
A
1210 result = composeRuleValue(result, baseValue);
1211 resVal.setDouble(result);
1212 return TRUE;
1213 }
1214}
1215
1216UBool
1217FractionalPartSubstitution::operator==(const NFSubstitution& rhs) const
1218{
1219 return NFSubstitution::operator==(rhs) &&
1220 ((const FractionalPartSubstitution*)&rhs)->byDigits == byDigits;
1221}
1222
46f4442e 1223UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FractionalPartSubstitution)
b75a7d8f
A
1224
1225
1226//===================================================================
1227// AbsoluteValueSubstitution
1228//===================================================================
1229
46f4442e 1230UOBJECT_DEFINE_RTTI_IMPLEMENTATION(AbsoluteValueSubstitution)
b75a7d8f
A
1231
1232//===================================================================
1233// NumeratorSubstitution
1234//===================================================================
1235
73c04bcf 1236void
2ca993e8 1237NumeratorSubstitution::doSubstitution(double number, UnicodeString& toInsertInto, int32_t apos, int32_t recursionCount, UErrorCode& status) const {
73c04bcf
A
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 != NULL) {
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);
2ca993e8 1251 aruleSet->format((int64_t)0, toInsertInto, apos + getPos(), recursionCount, status);
73c04bcf
A
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 != NULL) {
2ca993e8 1259 aruleSet->format(longNF, toInsertInto, apos + getPos(), recursionCount, status);
73c04bcf
A
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 != NULL) {
2ca993e8 1266 aruleSet->format(numberToFormat, toInsertInto, apos + getPos(), recursionCount, status);
73c04bcf 1267 } else {
73c04bcf
A
1268 UnicodeString temp;
1269 getNumberFormat()->format(numberToFormat, temp, status);
1270 toInsertInto.insert(apos + getPos(), temp);
1271 }
1272 }
1273}
1274
1275UBool
1276NumeratorSubstitution::doParse(const UnicodeString& text,
1277 ParsePosition& parsePosition,
1278 double baseValue,
1279 double upperBound,
1280 UBool /*lenientParse*/,
0f5d89e8 1281 uint32_t nonNumericalExecutedRuleMask,
73c04bcf
A
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);
0f5d89e8 1300 getRuleSet()->parse(workText, workPos, 1, nonNumericalExecutedRuleMask, temp); // parse zero or nothing at all
73c04bcf
A
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
0f5d89e8 1322 NFSubstitution::doParse(workText, parsePosition, withZeros ? 1 : baseValue, upperBound, FALSE, nonNumericalExecutedRuleMask, result);
73c04bcf
A
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 int32_t pow = 0;
1332 while (d <= n) {
1333 d *= 10;
1334 ++pow;
1335 }
1336 // now add the zeros
1337 while (zeroCount > 0) {
1338 d *= 10;
1339 --zeroCount;
1340 }
1341 // d is now our true denominator
1342 result.setDouble((double)n/(double)d);
1343 }
1344
1345 return TRUE;
1346}
1347
b75a7d8f
A
1348UBool
1349NumeratorSubstitution::operator==(const NFSubstitution& rhs) const
1350{
1351 return NFSubstitution::operator==(rhs) &&
1352 denominator == ((const NumeratorSubstitution*)&rhs)->denominator;
1353}
1354
46f4442e 1355UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NumeratorSubstitution)
b75a7d8f 1356
73c04bcf
A
1357const UChar NumeratorSubstitution::LTLT[] = { 0x003c, 0x003c };
1358
46f4442e 1359U_NAMESPACE_END
b75a7d8f
A
1360
1361/* U_HAVE_RBNF */
1362#endif
1363