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