nfsubs.cpp 52 KB

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