nfrule.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  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: nfrule.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 "nfrule.h"
  18. #if U_HAVE_RBNF
  19. #include "unicode/localpointer.h"
  20. #include "unicode/rbnf.h"
  21. #include "unicode/tblcoll.h"
  22. #include "unicode/plurfmt.h"
  23. #include "unicode/upluralrules.h"
  24. #include "unicode/coleitr.h"
  25. #include "unicode/uchar.h"
  26. #include "nfrs.h"
  27. #include "nfrlist.h"
  28. #include "nfsubs.h"
  29. #include "patternprops.h"
  30. #include "putilimp.h"
  31. U_NAMESPACE_BEGIN
  32. NFRule::NFRule(const RuleBasedNumberFormat* _rbnf, const UnicodeString &_ruleText, UErrorCode &status)
  33. : baseValue((int32_t)0)
  34. , radix(10)
  35. , exponent(0)
  36. , decimalPoint(0)
  37. , fRuleText(_ruleText)
  38. , sub1(nullptr)
  39. , sub2(nullptr)
  40. , formatter(_rbnf)
  41. , rulePatternFormat(nullptr)
  42. {
  43. if (!fRuleText.isEmpty()) {
  44. parseRuleDescriptor(fRuleText, status);
  45. }
  46. }
  47. NFRule::~NFRule()
  48. {
  49. if (sub1 != sub2) {
  50. delete sub2;
  51. sub2 = nullptr;
  52. }
  53. delete sub1;
  54. sub1 = nullptr;
  55. delete rulePatternFormat;
  56. rulePatternFormat = nullptr;
  57. }
  58. static const char16_t gLeftBracket = 0x005b;
  59. static const char16_t gRightBracket = 0x005d;
  60. static const char16_t gColon = 0x003a;
  61. static const char16_t gZero = 0x0030;
  62. static const char16_t gNine = 0x0039;
  63. static const char16_t gSpace = 0x0020;
  64. static const char16_t gSlash = 0x002f;
  65. static const char16_t gGreaterThan = 0x003e;
  66. static const char16_t gLessThan = 0x003c;
  67. static const char16_t gComma = 0x002c;
  68. static const char16_t gDot = 0x002e;
  69. static const char16_t gTick = 0x0027;
  70. //static const char16_t gMinus = 0x002d;
  71. static const char16_t gSemicolon = 0x003b;
  72. static const char16_t gX = 0x0078;
  73. static const char16_t gMinusX[] = {0x2D, 0x78, 0}; /* "-x" */
  74. static const char16_t gInf[] = {0x49, 0x6E, 0x66, 0}; /* "Inf" */
  75. static const char16_t gNaN[] = {0x4E, 0x61, 0x4E, 0}; /* "NaN" */
  76. static const char16_t gDollarOpenParenthesis[] = {0x24, 0x28, 0}; /* "$(" */
  77. static const char16_t gClosedParenthesisDollar[] = {0x29, 0x24, 0}; /* ")$" */
  78. static const char16_t gLessLess[] = {0x3C, 0x3C, 0}; /* "<<" */
  79. static const char16_t gLessPercent[] = {0x3C, 0x25, 0}; /* "<%" */
  80. static const char16_t gLessHash[] = {0x3C, 0x23, 0}; /* "<#" */
  81. static const char16_t gLessZero[] = {0x3C, 0x30, 0}; /* "<0" */
  82. static const char16_t gGreaterGreater[] = {0x3E, 0x3E, 0}; /* ">>" */
  83. static const char16_t gGreaterPercent[] = {0x3E, 0x25, 0}; /* ">%" */
  84. static const char16_t gGreaterHash[] = {0x3E, 0x23, 0}; /* ">#" */
  85. static const char16_t gGreaterZero[] = {0x3E, 0x30, 0}; /* ">0" */
  86. static const char16_t gEqualPercent[] = {0x3D, 0x25, 0}; /* "=%" */
  87. static const char16_t gEqualHash[] = {0x3D, 0x23, 0}; /* "=#" */
  88. static const char16_t gEqualZero[] = {0x3D, 0x30, 0}; /* "=0" */
  89. static const char16_t gGreaterGreaterGreater[] = {0x3E, 0x3E, 0x3E, 0}; /* ">>>" */
  90. static const char16_t * const RULE_PREFIXES[] = {
  91. gLessLess, gLessPercent, gLessHash, gLessZero,
  92. gGreaterGreater, gGreaterPercent,gGreaterHash, gGreaterZero,
  93. gEqualPercent, gEqualHash, gEqualZero, nullptr
  94. };
  95. void
  96. NFRule::makeRules(UnicodeString& description,
  97. NFRuleSet *owner,
  98. const NFRule *predecessor,
  99. const RuleBasedNumberFormat *rbnf,
  100. NFRuleList& rules,
  101. UErrorCode& status)
  102. {
  103. // we know we're making at least one rule, so go ahead and
  104. // new it up and initialize its basevalue and divisor
  105. // (this also strips the rule descriptor, if any, off the
  106. // description string)
  107. NFRule* rule1 = new NFRule(rbnf, description, status);
  108. /* test for nullptr */
  109. if (rule1 == 0) {
  110. status = U_MEMORY_ALLOCATION_ERROR;
  111. return;
  112. }
  113. description = rule1->fRuleText;
  114. // check the description to see whether there's text enclosed
  115. // in brackets
  116. int32_t brack1 = description.indexOf(gLeftBracket);
  117. int32_t brack2 = brack1 < 0 ? -1 : description.indexOf(gRightBracket);
  118. // if the description doesn't contain a matched pair of brackets,
  119. // or if it's of a type that doesn't recognize bracketed text,
  120. // then leave the description alone, initialize the rule's
  121. // rule text and substitutions, and return that rule
  122. if (brack2 < 0 || brack1 > brack2
  123. || rule1->getType() == kProperFractionRule
  124. || rule1->getType() == kNegativeNumberRule
  125. || rule1->getType() == kInfinityRule
  126. || rule1->getType() == kNaNRule)
  127. {
  128. rule1->extractSubstitutions(owner, description, predecessor, status);
  129. }
  130. else {
  131. // if the description does contain a matched pair of brackets,
  132. // then it's really shorthand for two rules (with one exception)
  133. NFRule* rule2 = nullptr;
  134. UnicodeString sbuf;
  135. // we'll actually only split the rule into two rules if its
  136. // base value is an even multiple of its divisor (or it's one
  137. // of the special rules)
  138. if ((rule1->baseValue > 0
  139. && (rule1->baseValue % util64_pow(rule1->radix, rule1->exponent)) == 0)
  140. || rule1->getType() == kImproperFractionRule
  141. || rule1->getType() == kDefaultRule) {
  142. // if it passes that test, new up the second rule. If the
  143. // rule set both rules will belong to is a fraction rule
  144. // set, they both have the same base value; otherwise,
  145. // increment the original rule's base value ("rule1" actually
  146. // goes SECOND in the rule set's rule list)
  147. rule2 = new NFRule(rbnf, UnicodeString(), status);
  148. /* test for nullptr */
  149. if (rule2 == 0) {
  150. status = U_MEMORY_ALLOCATION_ERROR;
  151. return;
  152. }
  153. if (rule1->baseValue >= 0) {
  154. rule2->baseValue = rule1->baseValue;
  155. if (!owner->isFractionRuleSet()) {
  156. ++rule1->baseValue;
  157. }
  158. }
  159. // if the description began with "x.x" and contains bracketed
  160. // text, it describes both the improper fraction rule and
  161. // the proper fraction rule
  162. else if (rule1->getType() == kImproperFractionRule) {
  163. rule2->setType(kProperFractionRule);
  164. }
  165. // if the description began with "x.0" and contains bracketed
  166. // text, it describes both the default rule and the
  167. // improper fraction rule
  168. else if (rule1->getType() == kDefaultRule) {
  169. rule2->baseValue = rule1->baseValue;
  170. rule1->setType(kImproperFractionRule);
  171. }
  172. // both rules have the same radix and exponent (i.e., the
  173. // same divisor)
  174. rule2->radix = rule1->radix;
  175. rule2->exponent = rule1->exponent;
  176. // rule2's rule text omits the stuff in brackets: initialize
  177. // its rule text and substitutions accordingly
  178. sbuf.append(description, 0, brack1);
  179. if (brack2 + 1 < description.length()) {
  180. sbuf.append(description, brack2 + 1, description.length() - brack2 - 1);
  181. }
  182. rule2->extractSubstitutions(owner, sbuf, predecessor, status);
  183. }
  184. // rule1's text includes the text in the brackets but omits
  185. // the brackets themselves: initialize _its_ rule text and
  186. // substitutions accordingly
  187. sbuf.setTo(description, 0, brack1);
  188. sbuf.append(description, brack1 + 1, brack2 - brack1 - 1);
  189. if (brack2 + 1 < description.length()) {
  190. sbuf.append(description, brack2 + 1, description.length() - brack2 - 1);
  191. }
  192. rule1->extractSubstitutions(owner, sbuf, predecessor, status);
  193. // if we only have one rule, return it; if we have two, return
  194. // a two-element array containing them (notice that rule2 goes
  195. // BEFORE rule1 in the list: in all cases, rule2 OMITS the
  196. // material in the brackets and rule1 INCLUDES the material
  197. // in the brackets)
  198. if (rule2 != nullptr) {
  199. if (rule2->baseValue >= kNoBase) {
  200. rules.add(rule2);
  201. }
  202. else {
  203. owner->setNonNumericalRule(rule2);
  204. }
  205. }
  206. }
  207. if (rule1->baseValue >= kNoBase) {
  208. rules.add(rule1);
  209. }
  210. else {
  211. owner->setNonNumericalRule(rule1);
  212. }
  213. }
  214. /**
  215. * This function parses the rule's rule descriptor (i.e., the base
  216. * value and/or other tokens that precede the rule's rule text
  217. * in the description) and sets the rule's base value, radix, and
  218. * exponent according to the descriptor. (If the description doesn't
  219. * include a rule descriptor, then this function sets everything to
  220. * default values and the rule set sets the rule's real base value).
  221. * @param description The rule's description
  222. * @return If "description" included a rule descriptor, this is
  223. * "description" with the descriptor and any trailing whitespace
  224. * stripped off. Otherwise; it's "descriptor" unchangd.
  225. */
  226. void
  227. NFRule::parseRuleDescriptor(UnicodeString& description, UErrorCode& status)
  228. {
  229. // the description consists of a rule descriptor and a rule body,
  230. // separated by a colon. The rule descriptor is optional. If
  231. // it's omitted, just set the base value to 0.
  232. int32_t p = description.indexOf(gColon);
  233. if (p != -1) {
  234. // copy the descriptor out into its own string and strip it,
  235. // along with any trailing whitespace, out of the original
  236. // description
  237. UnicodeString descriptor;
  238. descriptor.setTo(description, 0, p);
  239. ++p;
  240. while (p < description.length() && PatternProps::isWhiteSpace(description.charAt(p))) {
  241. ++p;
  242. }
  243. description.removeBetween(0, p);
  244. // check first to see if the rule descriptor matches the token
  245. // for one of the special rules. If it does, set the base
  246. // value to the correct identifier value
  247. int descriptorLength = descriptor.length();
  248. char16_t firstChar = descriptor.charAt(0);
  249. char16_t lastChar = descriptor.charAt(descriptorLength - 1);
  250. if (firstChar >= gZero && firstChar <= gNine && lastChar != gX) {
  251. // if the rule descriptor begins with a digit, it's a descriptor
  252. // for a normal rule
  253. // since we don't have Long.parseLong, and this isn't much work anyway,
  254. // just build up the value as we encounter the digits.
  255. int64_t val = 0;
  256. p = 0;
  257. char16_t c = gSpace;
  258. // begin parsing the descriptor: copy digits
  259. // into "tempValue", skip periods, commas, and spaces,
  260. // stop on a slash or > sign (or at the end of the string),
  261. // and throw an exception on any other character
  262. int64_t ll_10 = 10;
  263. while (p < descriptorLength) {
  264. c = descriptor.charAt(p);
  265. if (c >= gZero && c <= gNine) {
  266. val = val * ll_10 + (int32_t)(c - gZero);
  267. }
  268. else if (c == gSlash || c == gGreaterThan) {
  269. break;
  270. }
  271. else if (PatternProps::isWhiteSpace(c) || c == gComma || c == gDot) {
  272. }
  273. else {
  274. // throw new IllegalArgumentException("Illegal character in rule descriptor");
  275. status = U_PARSE_ERROR;
  276. return;
  277. }
  278. ++p;
  279. }
  280. // we have the base value, so set it
  281. setBaseValue(val, status);
  282. // if we stopped the previous loop on a slash, we're
  283. // now parsing the rule's radix. Again, accumulate digits
  284. // in tempValue, skip punctuation, stop on a > mark, and
  285. // throw an exception on anything else
  286. if (c == gSlash) {
  287. val = 0;
  288. ++p;
  289. ll_10 = 10;
  290. while (p < descriptorLength) {
  291. c = descriptor.charAt(p);
  292. if (c >= gZero && c <= gNine) {
  293. val = val * ll_10 + (int32_t)(c - gZero);
  294. }
  295. else if (c == gGreaterThan) {
  296. break;
  297. }
  298. else if (PatternProps::isWhiteSpace(c) || c == gComma || c == gDot) {
  299. }
  300. else {
  301. // throw new IllegalArgumentException("Illegal character is rule descriptor");
  302. status = U_PARSE_ERROR;
  303. return;
  304. }
  305. ++p;
  306. }
  307. // tempValue now contain's the rule's radix. Set it
  308. // accordingly, and recalculate the rule's exponent
  309. radix = (int32_t)val;
  310. if (radix == 0) {
  311. // throw new IllegalArgumentException("Rule can't have radix of 0");
  312. status = U_PARSE_ERROR;
  313. }
  314. exponent = expectedExponent();
  315. }
  316. // if we stopped the previous loop on a > sign, then continue
  317. // for as long as we still see > signs. For each one,
  318. // decrement the exponent (unless the exponent is already 0).
  319. // If we see another character before reaching the end of
  320. // the descriptor, that's also a syntax error.
  321. if (c == gGreaterThan) {
  322. while (p < descriptor.length()) {
  323. c = descriptor.charAt(p);
  324. if (c == gGreaterThan && exponent > 0) {
  325. --exponent;
  326. } else {
  327. // throw new IllegalArgumentException("Illegal character in rule descriptor");
  328. status = U_PARSE_ERROR;
  329. return;
  330. }
  331. ++p;
  332. }
  333. }
  334. }
  335. else if (0 == descriptor.compare(gMinusX, 2)) {
  336. setType(kNegativeNumberRule);
  337. }
  338. else if (descriptorLength == 3) {
  339. if (firstChar == gZero && lastChar == gX) {
  340. setBaseValue(kProperFractionRule, status);
  341. decimalPoint = descriptor.charAt(1);
  342. }
  343. else if (firstChar == gX && lastChar == gX) {
  344. setBaseValue(kImproperFractionRule, status);
  345. decimalPoint = descriptor.charAt(1);
  346. }
  347. else if (firstChar == gX && lastChar == gZero) {
  348. setBaseValue(kDefaultRule, status);
  349. decimalPoint = descriptor.charAt(1);
  350. }
  351. else if (descriptor.compare(gNaN, 3) == 0) {
  352. setBaseValue(kNaNRule, status);
  353. }
  354. else if (descriptor.compare(gInf, 3) == 0) {
  355. setBaseValue(kInfinityRule, status);
  356. }
  357. }
  358. }
  359. // else use the default base value for now.
  360. // finally, if the rule body begins with an apostrophe, strip it off
  361. // (this is generally used to put whitespace at the beginning of
  362. // a rule's rule text)
  363. if (description.length() > 0 && description.charAt(0) == gTick) {
  364. description.removeBetween(0, 1);
  365. }
  366. // return the description with all the stuff we've just waded through
  367. // stripped off the front. It now contains just the rule body.
  368. // return description;
  369. }
  370. /**
  371. * Searches the rule's rule text for the substitution tokens,
  372. * creates the substitutions, and removes the substitution tokens
  373. * from the rule's rule text.
  374. * @param owner The rule set containing this rule
  375. * @param predecessor The rule preseding this one in "owners" rule list
  376. * @param ownersOwner The RuleBasedFormat that owns this rule
  377. */
  378. void
  379. NFRule::extractSubstitutions(const NFRuleSet* ruleSet,
  380. const UnicodeString &ruleText,
  381. const NFRule* predecessor,
  382. UErrorCode& status)
  383. {
  384. if (U_FAILURE(status)) {
  385. return;
  386. }
  387. fRuleText = ruleText;
  388. sub1 = extractSubstitution(ruleSet, predecessor, status);
  389. if (sub1 == nullptr) {
  390. // Small optimization. There is no need to create a redundant NullSubstitution.
  391. sub2 = nullptr;
  392. }
  393. else {
  394. sub2 = extractSubstitution(ruleSet, predecessor, status);
  395. }
  396. int32_t pluralRuleStart = fRuleText.indexOf(gDollarOpenParenthesis, -1, 0);
  397. int32_t pluralRuleEnd = (pluralRuleStart >= 0 ? fRuleText.indexOf(gClosedParenthesisDollar, -1, pluralRuleStart) : -1);
  398. if (pluralRuleEnd >= 0) {
  399. int32_t endType = fRuleText.indexOf(gComma, pluralRuleStart);
  400. if (endType < 0) {
  401. status = U_PARSE_ERROR;
  402. return;
  403. }
  404. UnicodeString type(fRuleText.tempSubString(pluralRuleStart + 2, endType - pluralRuleStart - 2));
  405. UPluralType pluralType;
  406. if (type.startsWith(UNICODE_STRING_SIMPLE("cardinal"))) {
  407. pluralType = UPLURAL_TYPE_CARDINAL;
  408. }
  409. else if (type.startsWith(UNICODE_STRING_SIMPLE("ordinal"))) {
  410. pluralType = UPLURAL_TYPE_ORDINAL;
  411. }
  412. else {
  413. status = U_ILLEGAL_ARGUMENT_ERROR;
  414. return;
  415. }
  416. rulePatternFormat = formatter->createPluralFormat(pluralType,
  417. fRuleText.tempSubString(endType + 1, pluralRuleEnd - endType - 1), status);
  418. }
  419. }
  420. /**
  421. * Searches the rule's rule text for the first substitution token,
  422. * creates a substitution based on it, and removes the token from
  423. * the rule's rule text.
  424. * @param owner The rule set containing this rule
  425. * @param predecessor The rule preceding this one in the rule set's
  426. * rule list
  427. * @param ownersOwner The RuleBasedNumberFormat that owns this rule
  428. * @return The newly-created substitution. This is never null; if
  429. * the rule text doesn't contain any substitution tokens, this will
  430. * be a NullSubstitution.
  431. */
  432. NFSubstitution *
  433. NFRule::extractSubstitution(const NFRuleSet* ruleSet,
  434. const NFRule* predecessor,
  435. UErrorCode& status)
  436. {
  437. NFSubstitution* result = nullptr;
  438. // search the rule's rule text for the first two characters of
  439. // a substitution token
  440. int32_t subStart = indexOfAnyRulePrefix();
  441. int32_t subEnd = subStart;
  442. // if we didn't find one, create a null substitution positioned
  443. // at the end of the rule text
  444. if (subStart == -1) {
  445. return nullptr;
  446. }
  447. // special-case the ">>>" token, since searching for the > at the
  448. // end will actually find the > in the middle
  449. if (fRuleText.indexOf(gGreaterGreaterGreater, 3, 0) == subStart) {
  450. subEnd = subStart + 2;
  451. // otherwise the substitution token ends with the same character
  452. // it began with
  453. } else {
  454. char16_t c = fRuleText.charAt(subStart);
  455. subEnd = fRuleText.indexOf(c, subStart + 1);
  456. // special case for '<%foo<<'
  457. if (c == gLessThan && subEnd != -1 && subEnd < fRuleText.length() - 1 && fRuleText.charAt(subEnd+1) == c) {
  458. // ordinals use "=#,##0==%abbrev=" as their rule. Notice that the '==' in the middle
  459. // occurs because of the juxtaposition of two different rules. The check for '<' is a hack
  460. // to get around this. Having the duplicate at the front would cause problems with
  461. // rules like "<<%" to format, say, percents...
  462. ++subEnd;
  463. }
  464. }
  465. // if we don't find the end of the token (i.e., if we're on a single,
  466. // unmatched token character), create a null substitution positioned
  467. // at the end of the rule
  468. if (subEnd == -1) {
  469. return nullptr;
  470. }
  471. // if we get here, we have a real substitution token (or at least
  472. // some text bounded by substitution token characters). Use
  473. // makeSubstitution() to create the right kind of substitution
  474. UnicodeString subToken;
  475. subToken.setTo(fRuleText, subStart, subEnd + 1 - subStart);
  476. result = NFSubstitution::makeSubstitution(subStart, this, predecessor, ruleSet,
  477. this->formatter, subToken, status);
  478. // remove the substitution from the rule text
  479. fRuleText.removeBetween(subStart, subEnd+1);
  480. return result;
  481. }
  482. /**
  483. * Sets the rule's base value, and causes the radix and exponent
  484. * to be recalculated. This is used during construction when we
  485. * don't know the rule's base value until after it's been
  486. * constructed. It should be used at any other time.
  487. * @param The new base value for the rule.
  488. */
  489. void
  490. NFRule::setBaseValue(int64_t newBaseValue, UErrorCode& status)
  491. {
  492. // set the base value
  493. baseValue = newBaseValue;
  494. radix = 10;
  495. // if this isn't a special rule, recalculate the radix and exponent
  496. // (the radix always defaults to 10; if it's supposed to be something
  497. // else, it's cleaned up by the caller and the exponent is
  498. // recalculated again-- the only function that does this is
  499. // NFRule.parseRuleDescriptor() )
  500. if (baseValue >= 1) {
  501. exponent = expectedExponent();
  502. // this function gets called on a fully-constructed rule whose
  503. // description didn't specify a base value. This means it
  504. // has substitutions, and some substitutions hold on to copies
  505. // of the rule's divisor. Fix their copies of the divisor.
  506. if (sub1 != nullptr) {
  507. sub1->setDivisor(radix, exponent, status);
  508. }
  509. if (sub2 != nullptr) {
  510. sub2->setDivisor(radix, exponent, status);
  511. }
  512. // if this is a special rule, its radix and exponent are basically
  513. // ignored. Set them to "safe" default values
  514. } else {
  515. exponent = 0;
  516. }
  517. }
  518. /**
  519. * This calculates the rule's exponent based on its radix and base
  520. * value. This will be the highest power the radix can be raised to
  521. * and still produce a result less than or equal to the base value.
  522. */
  523. int16_t
  524. NFRule::expectedExponent() const
  525. {
  526. // since the log of 0, or the log base 0 of something, causes an
  527. // error, declare the exponent in these cases to be 0 (we also
  528. // deal with the special-rule identifiers here)
  529. if (radix == 0 || baseValue < 1) {
  530. return 0;
  531. }
  532. // we get rounding error in some cases-- for example, log 1000 / log 10
  533. // gives us 1.9999999996 instead of 2. The extra logic here is to take
  534. // that into account
  535. int16_t tempResult = (int16_t)(uprv_log((double)baseValue) / uprv_log((double)radix));
  536. int64_t temp = util64_pow(radix, tempResult + 1);
  537. if (temp <= baseValue) {
  538. tempResult += 1;
  539. }
  540. return tempResult;
  541. }
  542. /**
  543. * Searches the rule's rule text for any of the specified strings.
  544. * @return The index of the first match in the rule's rule text
  545. * (i.e., the first substring in the rule's rule text that matches
  546. * _any_ of the strings in "strings"). If none of the strings in
  547. * "strings" is found in the rule's rule text, returns -1.
  548. */
  549. int32_t
  550. NFRule::indexOfAnyRulePrefix() const
  551. {
  552. int result = -1;
  553. for (int i = 0; RULE_PREFIXES[i]; i++) {
  554. int32_t pos = fRuleText.indexOf(*RULE_PREFIXES[i]);
  555. if (pos != -1 && (result == -1 || pos < result)) {
  556. result = pos;
  557. }
  558. }
  559. return result;
  560. }
  561. //-----------------------------------------------------------------------
  562. // boilerplate
  563. //-----------------------------------------------------------------------
  564. static UBool
  565. util_equalSubstitutions(const NFSubstitution* sub1, const NFSubstitution* sub2)
  566. {
  567. if (sub1) {
  568. if (sub2) {
  569. return *sub1 == *sub2;
  570. }
  571. } else if (!sub2) {
  572. return true;
  573. }
  574. return false;
  575. }
  576. /**
  577. * Tests two rules for equality.
  578. * @param that The rule to compare this one against
  579. * @return True is the two rules are functionally equivalent
  580. */
  581. bool
  582. NFRule::operator==(const NFRule& rhs) const
  583. {
  584. return baseValue == rhs.baseValue
  585. && radix == rhs.radix
  586. && exponent == rhs.exponent
  587. && fRuleText == rhs.fRuleText
  588. && util_equalSubstitutions(sub1, rhs.sub1)
  589. && util_equalSubstitutions(sub2, rhs.sub2);
  590. }
  591. /**
  592. * Returns a textual representation of the rule. This won't
  593. * necessarily be the same as the description that this rule
  594. * was created with, but it will produce the same result.
  595. * @return A textual description of the rule
  596. */
  597. static void util_append64(UnicodeString& result, int64_t n)
  598. {
  599. char16_t buffer[256];
  600. int32_t len = util64_tou(n, buffer, sizeof(buffer));
  601. UnicodeString temp(buffer, len);
  602. result.append(temp);
  603. }
  604. void
  605. NFRule::_appendRuleText(UnicodeString& result) const
  606. {
  607. switch (getType()) {
  608. case kNegativeNumberRule: result.append(gMinusX, 2); break;
  609. case kImproperFractionRule: result.append(gX).append(decimalPoint == 0 ? gDot : decimalPoint).append(gX); break;
  610. case kProperFractionRule: result.append(gZero).append(decimalPoint == 0 ? gDot : decimalPoint).append(gX); break;
  611. case kDefaultRule: result.append(gX).append(decimalPoint == 0 ? gDot : decimalPoint).append(gZero); break;
  612. case kInfinityRule: result.append(gInf, 3); break;
  613. case kNaNRule: result.append(gNaN, 3); break;
  614. default:
  615. // for a normal rule, write out its base value, and if the radix is
  616. // something other than 10, write out the radix (with the preceding
  617. // slash, of course). Then calculate the expected exponent and if
  618. // if isn't the same as the actual exponent, write an appropriate
  619. // number of > signs. Finally, terminate the whole thing with
  620. // a colon.
  621. util_append64(result, baseValue);
  622. if (radix != 10) {
  623. result.append(gSlash);
  624. util_append64(result, radix);
  625. }
  626. int numCarets = expectedExponent() - exponent;
  627. for (int i = 0; i < numCarets; i++) {
  628. result.append(gGreaterThan);
  629. }
  630. break;
  631. }
  632. result.append(gColon);
  633. result.append(gSpace);
  634. // if the rule text begins with a space, write an apostrophe
  635. // (whitespace after the rule descriptor is ignored; the
  636. // apostrophe is used to make the whitespace significant)
  637. if (fRuleText.charAt(0) == gSpace && (sub1 == nullptr || sub1->getPos() != 0)) {
  638. result.append(gTick);
  639. }
  640. // now, write the rule's rule text, inserting appropriate
  641. // substitution tokens in the appropriate places
  642. UnicodeString ruleTextCopy;
  643. ruleTextCopy.setTo(fRuleText);
  644. UnicodeString temp;
  645. if (sub2 != nullptr) {
  646. sub2->toString(temp);
  647. ruleTextCopy.insert(sub2->getPos(), temp);
  648. }
  649. if (sub1 != nullptr) {
  650. sub1->toString(temp);
  651. ruleTextCopy.insert(sub1->getPos(), temp);
  652. }
  653. result.append(ruleTextCopy);
  654. // and finally, top the whole thing off with a semicolon and
  655. // return the result
  656. result.append(gSemicolon);
  657. }
  658. int64_t NFRule::getDivisor() const
  659. {
  660. return util64_pow(radix, exponent);
  661. }
  662. //-----------------------------------------------------------------------
  663. // formatting
  664. //-----------------------------------------------------------------------
  665. /**
  666. * Formats the number, and inserts the resulting text into
  667. * toInsertInto.
  668. * @param number The number being formatted
  669. * @param toInsertInto The string where the resultant text should
  670. * be inserted
  671. * @param pos The position in toInsertInto where the resultant text
  672. * should be inserted
  673. */
  674. void
  675. NFRule::doFormat(int64_t number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const
  676. {
  677. // first, insert the rule's rule text into toInsertInto at the
  678. // specified position, then insert the results of the substitutions
  679. // into the right places in toInsertInto (notice we do the
  680. // substitutions in reverse order so that the offsets don't get
  681. // messed up)
  682. int32_t pluralRuleStart = fRuleText.length();
  683. int32_t lengthOffset = 0;
  684. if (!rulePatternFormat) {
  685. toInsertInto.insert(pos, fRuleText);
  686. }
  687. else {
  688. pluralRuleStart = fRuleText.indexOf(gDollarOpenParenthesis, -1, 0);
  689. int pluralRuleEnd = fRuleText.indexOf(gClosedParenthesisDollar, -1, pluralRuleStart);
  690. int initialLength = toInsertInto.length();
  691. if (pluralRuleEnd < fRuleText.length() - 1) {
  692. toInsertInto.insert(pos, fRuleText.tempSubString(pluralRuleEnd + 2));
  693. }
  694. toInsertInto.insert(pos,
  695. rulePatternFormat->format((int32_t)(number/util64_pow(radix, exponent)), status));
  696. if (pluralRuleStart > 0) {
  697. toInsertInto.insert(pos, fRuleText.tempSubString(0, pluralRuleStart));
  698. }
  699. lengthOffset = fRuleText.length() - (toInsertInto.length() - initialLength);
  700. }
  701. if (sub2 != nullptr) {
  702. sub2->doSubstitution(number, toInsertInto, pos - (sub2->getPos() > pluralRuleStart ? lengthOffset : 0), recursionCount, status);
  703. }
  704. if (sub1 != nullptr) {
  705. sub1->doSubstitution(number, toInsertInto, pos - (sub1->getPos() > pluralRuleStart ? lengthOffset : 0), recursionCount, status);
  706. }
  707. }
  708. /**
  709. * Formats the number, and inserts the resulting text into
  710. * toInsertInto.
  711. * @param number The number being formatted
  712. * @param toInsertInto The string where the resultant text should
  713. * be inserted
  714. * @param pos The position in toInsertInto where the resultant text
  715. * should be inserted
  716. */
  717. void
  718. NFRule::doFormat(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const
  719. {
  720. // first, insert the rule's rule text into toInsertInto at the
  721. // specified position, then insert the results of the substitutions
  722. // into the right places in toInsertInto
  723. // [again, we have two copies of this routine that do the same thing
  724. // so that we don't sacrifice precision in a long by casting it
  725. // to a double]
  726. int32_t pluralRuleStart = fRuleText.length();
  727. int32_t lengthOffset = 0;
  728. if (!rulePatternFormat) {
  729. toInsertInto.insert(pos, fRuleText);
  730. }
  731. else {
  732. pluralRuleStart = fRuleText.indexOf(gDollarOpenParenthesis, -1, 0);
  733. int pluralRuleEnd = fRuleText.indexOf(gClosedParenthesisDollar, -1, pluralRuleStart);
  734. int initialLength = toInsertInto.length();
  735. if (pluralRuleEnd < fRuleText.length() - 1) {
  736. toInsertInto.insert(pos, fRuleText.tempSubString(pluralRuleEnd + 2));
  737. }
  738. double pluralVal = number;
  739. if (0 <= pluralVal && pluralVal < 1) {
  740. // We're in a fractional rule, and we have to match the NumeratorSubstitution behavior.
  741. // 2.3 can become 0.2999999999999998 for the fraction due to rounding errors.
  742. pluralVal = uprv_round(pluralVal * util64_pow(radix, exponent));
  743. }
  744. else {
  745. pluralVal = pluralVal / util64_pow(radix, exponent);
  746. }
  747. toInsertInto.insert(pos, rulePatternFormat->format((int32_t)(pluralVal), status));
  748. if (pluralRuleStart > 0) {
  749. toInsertInto.insert(pos, fRuleText.tempSubString(0, pluralRuleStart));
  750. }
  751. lengthOffset = fRuleText.length() - (toInsertInto.length() - initialLength);
  752. }
  753. if (sub2 != nullptr) {
  754. sub2->doSubstitution(number, toInsertInto, pos - (sub2->getPos() > pluralRuleStart ? lengthOffset : 0), recursionCount, status);
  755. }
  756. if (sub1 != nullptr) {
  757. sub1->doSubstitution(number, toInsertInto, pos - (sub1->getPos() > pluralRuleStart ? lengthOffset : 0), recursionCount, status);
  758. }
  759. }
  760. /**
  761. * Used by the owning rule set to determine whether to invoke the
  762. * rollback rule (i.e., whether this rule or the one that precedes
  763. * it in the rule set's list should be used to format the number)
  764. * @param The number being formatted
  765. * @return True if the rule set should use the rule that precedes
  766. * this one in its list; false if it should use this rule
  767. */
  768. UBool
  769. NFRule::shouldRollBack(int64_t number) const
  770. {
  771. // we roll back if the rule contains a modulus substitution,
  772. // the number being formatted is an even multiple of the rule's
  773. // divisor, and the rule's base value is NOT an even multiple
  774. // of its divisor
  775. // In other words, if the original description had
  776. // 100: << hundred[ >>];
  777. // that expands into
  778. // 100: << hundred;
  779. // 101: << hundred >>;
  780. // internally. But when we're formatting 200, if we use the rule
  781. // at 101, which would normally apply, we get "two hundred zero".
  782. // To prevent this, we roll back and use the rule at 100 instead.
  783. // This is the logic that makes this happen: the rule at 101 has
  784. // a modulus substitution, its base value isn't an even multiple
  785. // of 100, and the value we're trying to format _is_ an even
  786. // multiple of 100. This is called the "rollback rule."
  787. if ((sub1 != nullptr && sub1->isModulusSubstitution()) || (sub2 != nullptr && sub2->isModulusSubstitution())) {
  788. int64_t re = util64_pow(radix, exponent);
  789. return (number % re) == 0 && (baseValue % re) != 0;
  790. }
  791. return false;
  792. }
  793. //-----------------------------------------------------------------------
  794. // parsing
  795. //-----------------------------------------------------------------------
  796. /**
  797. * Attempts to parse the string with this rule.
  798. * @param text The string being parsed
  799. * @param parsePosition On entry, the value is ignored and assumed to
  800. * be 0. On exit, this has been updated with the position of the first
  801. * character not consumed by matching the text against this rule
  802. * (if this rule doesn't match the text at all, the parse position
  803. * if left unchanged (presumably at 0) and the function returns
  804. * new Long(0)).
  805. * @param isFractionRule True if this rule is contained within a
  806. * fraction rule set. This is only used if the rule has no
  807. * substitutions.
  808. * @return If this rule matched the text, this is the rule's base value
  809. * combined appropriately with the results of parsing the substitutions.
  810. * If nothing matched, this is new Long(0) and the parse position is
  811. * left unchanged. The result will be an instance of Long if the
  812. * result is an integer and Double otherwise. The result is never null.
  813. */
  814. #ifdef RBNF_DEBUG
  815. #include <stdio.h>
  816. static void dumpUS(FILE* f, const UnicodeString& us) {
  817. int len = us.length();
  818. char* buf = (char *)uprv_malloc((len+1)*sizeof(char)); //new char[len+1];
  819. if (buf != nullptr) {
  820. us.extract(0, len, buf);
  821. buf[len] = 0;
  822. fprintf(f, "%s", buf);
  823. uprv_free(buf); //delete[] buf;
  824. }
  825. }
  826. #endif
  827. UBool
  828. NFRule::doParse(const UnicodeString& text,
  829. ParsePosition& parsePosition,
  830. UBool isFractionRule,
  831. double upperBound,
  832. uint32_t nonNumericalExecutedRuleMask,
  833. Formattable& resVal) const
  834. {
  835. // internally we operate on a copy of the string being parsed
  836. // (because we're going to change it) and use our own ParsePosition
  837. ParsePosition pp;
  838. UnicodeString workText(text);
  839. int32_t sub1Pos = sub1 != nullptr ? sub1->getPos() : fRuleText.length();
  840. int32_t sub2Pos = sub2 != nullptr ? sub2->getPos() : fRuleText.length();
  841. // check to see whether the text before the first substitution
  842. // matches the text at the beginning of the string being
  843. // parsed. If it does, strip that off the front of workText;
  844. // otherwise, dump out with a mismatch
  845. UnicodeString prefix;
  846. prefix.setTo(fRuleText, 0, sub1Pos);
  847. #ifdef RBNF_DEBUG
  848. fprintf(stderr, "doParse %p ", this);
  849. {
  850. UnicodeString rt;
  851. _appendRuleText(rt);
  852. dumpUS(stderr, rt);
  853. }
  854. fprintf(stderr, " text: '");
  855. dumpUS(stderr, text);
  856. fprintf(stderr, "' prefix: '");
  857. dumpUS(stderr, prefix);
  858. #endif
  859. stripPrefix(workText, prefix, pp);
  860. int32_t prefixLength = text.length() - workText.length();
  861. #ifdef RBNF_DEBUG
  862. fprintf(stderr, "' pl: %d ppi: %d s1p: %d\n", prefixLength, pp.getIndex(), sub1Pos);
  863. #endif
  864. if (pp.getIndex() == 0 && sub1Pos != 0) {
  865. // commented out because ParsePosition doesn't have error index in 1.1.x
  866. // restored for ICU4C port
  867. parsePosition.setErrorIndex(pp.getErrorIndex());
  868. resVal.setLong(0);
  869. return true;
  870. }
  871. if (baseValue == kInfinityRule) {
  872. // If you match this, don't try to perform any calculations on it.
  873. parsePosition.setIndex(pp.getIndex());
  874. resVal.setDouble(uprv_getInfinity());
  875. return true;
  876. }
  877. if (baseValue == kNaNRule) {
  878. // If you match this, don't try to perform any calculations on it.
  879. parsePosition.setIndex(pp.getIndex());
  880. resVal.setDouble(uprv_getNaN());
  881. return true;
  882. }
  883. // this is the fun part. The basic guts of the rule-matching
  884. // logic is matchToDelimiter(), which is called twice. The first
  885. // time it searches the input string for the rule text BETWEEN
  886. // the substitutions and tries to match the intervening text
  887. // in the input string with the first substitution. If that
  888. // succeeds, it then calls it again, this time to look for the
  889. // rule text after the second substitution and to match the
  890. // intervening input text against the second substitution.
  891. //
  892. // For example, say we have a rule that looks like this:
  893. // first << middle >> last;
  894. // and input text that looks like this:
  895. // first one middle two last
  896. // First we use stripPrefix() to match "first " in both places and
  897. // strip it off the front, leaving
  898. // one middle two last
  899. // Then we use matchToDelimiter() to match " middle " and try to
  900. // match "one" against a substitution. If it's successful, we now
  901. // have
  902. // two last
  903. // We use matchToDelimiter() a second time to match " last" and
  904. // try to match "two" against a substitution. If "two" matches
  905. // the substitution, we have a successful parse.
  906. //
  907. // Since it's possible in many cases to find multiple instances
  908. // of each of these pieces of rule text in the input string,
  909. // we need to try all the possible combinations of these
  910. // locations. This prevents us from prematurely declaring a mismatch,
  911. // and makes sure we match as much input text as we can.
  912. int highWaterMark = 0;
  913. double result = 0;
  914. int start = 0;
  915. double tempBaseValue = (double)(baseValue <= 0 ? 0 : baseValue);
  916. UnicodeString temp;
  917. do {
  918. // our partial parse result starts out as this rule's base
  919. // value. If it finds a successful match, matchToDelimiter()
  920. // will compose this in some way with what it gets back from
  921. // the substitution, giving us a new partial parse result
  922. pp.setIndex(0);
  923. temp.setTo(fRuleText, sub1Pos, sub2Pos - sub1Pos);
  924. double partialResult = matchToDelimiter(workText, start, tempBaseValue,
  925. temp, pp, sub1,
  926. nonNumericalExecutedRuleMask,
  927. upperBound);
  928. // if we got a successful match (or were trying to match a
  929. // null substitution), pp is now pointing at the first unmatched
  930. // character. Take note of that, and try matchToDelimiter()
  931. // on the input text again
  932. if (pp.getIndex() != 0 || sub1 == nullptr) {
  933. start = pp.getIndex();
  934. UnicodeString workText2;
  935. workText2.setTo(workText, pp.getIndex(), workText.length() - pp.getIndex());
  936. ParsePosition pp2;
  937. // the second matchToDelimiter() will compose our previous
  938. // partial result with whatever it gets back from its
  939. // substitution if there's a successful match, giving us
  940. // a real result
  941. temp.setTo(fRuleText, sub2Pos, fRuleText.length() - sub2Pos);
  942. partialResult = matchToDelimiter(workText2, 0, partialResult,
  943. temp, pp2, sub2,
  944. nonNumericalExecutedRuleMask,
  945. upperBound);
  946. // if we got a successful match on this second
  947. // matchToDelimiter() call, update the high-water mark
  948. // and result (if necessary)
  949. if (pp2.getIndex() != 0 || sub2 == nullptr) {
  950. if (prefixLength + pp.getIndex() + pp2.getIndex() > highWaterMark) {
  951. highWaterMark = prefixLength + pp.getIndex() + pp2.getIndex();
  952. result = partialResult;
  953. }
  954. }
  955. else {
  956. // commented out because ParsePosition doesn't have error index in 1.1.x
  957. // restored for ICU4C port
  958. int32_t i_temp = pp2.getErrorIndex() + sub1Pos + pp.getIndex();
  959. if (i_temp> parsePosition.getErrorIndex()) {
  960. parsePosition.setErrorIndex(i_temp);
  961. }
  962. }
  963. }
  964. else {
  965. // commented out because ParsePosition doesn't have error index in 1.1.x
  966. // restored for ICU4C port
  967. int32_t i_temp = sub1Pos + pp.getErrorIndex();
  968. if (i_temp > parsePosition.getErrorIndex()) {
  969. parsePosition.setErrorIndex(i_temp);
  970. }
  971. }
  972. // keep trying to match things until the outer matchToDelimiter()
  973. // call fails to make a match (each time, it picks up where it
  974. // left off the previous time)
  975. } while (sub1Pos != sub2Pos
  976. && pp.getIndex() > 0
  977. && pp.getIndex() < workText.length()
  978. && pp.getIndex() != start);
  979. // update the caller's ParsePosition with our high-water mark
  980. // (i.e., it now points at the first character this function
  981. // didn't match-- the ParsePosition is therefore unchanged if
  982. // we didn't match anything)
  983. parsePosition.setIndex(highWaterMark);
  984. // commented out because ParsePosition doesn't have error index in 1.1.x
  985. // restored for ICU4C port
  986. if (highWaterMark > 0) {
  987. parsePosition.setErrorIndex(0);
  988. }
  989. // this is a hack for one unusual condition: Normally, whether this
  990. // rule belong to a fraction rule set or not is handled by its
  991. // substitutions. But if that rule HAS NO substitutions, then
  992. // we have to account for it here. By definition, if the matching
  993. // rule in a fraction rule set has no substitutions, its numerator
  994. // is 1, and so the result is the reciprocal of its base value.
  995. if (isFractionRule && highWaterMark > 0 && sub1 == nullptr) {
  996. result = 1 / result;
  997. }
  998. resVal.setDouble(result);
  999. return true; // ??? do we need to worry if it is a long or a double?
  1000. }
  1001. /**
  1002. * This function is used by parse() to match the text being parsed
  1003. * against a possible prefix string. This function
  1004. * matches characters from the beginning of the string being parsed
  1005. * to characters from the prospective prefix. If they match, pp is
  1006. * updated to the first character not matched, and the result is
  1007. * the unparsed part of the string. If they don't match, the whole
  1008. * string is returned, and pp is left unchanged.
  1009. * @param text The string being parsed
  1010. * @param prefix The text to match against
  1011. * @param pp On entry, ignored and assumed to be 0. On exit, points
  1012. * to the first unmatched character (assuming the whole prefix matched),
  1013. * or is unchanged (if the whole prefix didn't match).
  1014. * @return If things match, this is the unparsed part of "text";
  1015. * if they didn't match, this is "text".
  1016. */
  1017. void
  1018. NFRule::stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const
  1019. {
  1020. // if the prefix text is empty, dump out without doing anything
  1021. if (prefix.length() != 0) {
  1022. UErrorCode status = U_ZERO_ERROR;
  1023. // use prefixLength() to match the beginning of
  1024. // "text" against "prefix". This function returns the
  1025. // number of characters from "text" that matched (or 0 if
  1026. // we didn't match the whole prefix)
  1027. int32_t pfl = prefixLength(text, prefix, status);
  1028. if (U_FAILURE(status)) { // Memory allocation error.
  1029. return;
  1030. }
  1031. if (pfl != 0) {
  1032. // if we got a successful match, update the parse position
  1033. // and strip the prefix off of "text"
  1034. pp.setIndex(pp.getIndex() + pfl);
  1035. text.remove(0, pfl);
  1036. }
  1037. }
  1038. }
  1039. /**
  1040. * Used by parse() to match a substitution and any following text.
  1041. * "text" is searched for instances of "delimiter". For each instance
  1042. * of delimiter, the intervening text is tested to see whether it
  1043. * matches the substitution. The longest match wins.
  1044. * @param text The string being parsed
  1045. * @param startPos The position in "text" where we should start looking
  1046. * for "delimiter".
  1047. * @param baseValue A partial parse result (often the rule's base value),
  1048. * which is combined with the result from matching the substitution
  1049. * @param delimiter The string to search "text" for.
  1050. * @param pp Ignored and presumed to be 0 on entry. If there's a match,
  1051. * on exit this will point to the first unmatched character.
  1052. * @param sub If we find "delimiter" in "text", this substitution is used
  1053. * to match the text between the beginning of the string and the
  1054. * position of "delimiter." (If "delimiter" is the empty string, then
  1055. * this function just matches against this substitution and updates
  1056. * everything accordingly.)
  1057. * @param upperBound When matching the substitution, it will only
  1058. * consider rules with base values lower than this value.
  1059. * @return If there's a match, this is the result of composing
  1060. * baseValue with the result of matching the substitution. Otherwise,
  1061. * this is new Long(0). It's never null. If the result is an integer,
  1062. * this will be an instance of Long; otherwise, it's an instance of
  1063. * Double.
  1064. *
  1065. * !!! note {dlf} in point of fact, in the java code the caller always converts
  1066. * the result to a double, so we might as well return one.
  1067. */
  1068. double
  1069. NFRule::matchToDelimiter(const UnicodeString& text,
  1070. int32_t startPos,
  1071. double _baseValue,
  1072. const UnicodeString& delimiter,
  1073. ParsePosition& pp,
  1074. const NFSubstitution* sub,
  1075. uint32_t nonNumericalExecutedRuleMask,
  1076. double upperBound) const
  1077. {
  1078. UErrorCode status = U_ZERO_ERROR;
  1079. // if "delimiter" contains real (i.e., non-ignorable) text, search
  1080. // it for "delimiter" beginning at "start". If that succeeds, then
  1081. // use "sub"'s doParse() method to match the text before the
  1082. // instance of "delimiter" we just found.
  1083. if (!allIgnorable(delimiter, status)) {
  1084. if (U_FAILURE(status)) { //Memory allocation error.
  1085. return 0;
  1086. }
  1087. ParsePosition tempPP;
  1088. Formattable result;
  1089. // use findText() to search for "delimiter". It returns a two-
  1090. // element array: element 0 is the position of the match, and
  1091. // element 1 is the number of characters that matched
  1092. // "delimiter".
  1093. int32_t dLen;
  1094. int32_t dPos = findText(text, delimiter, startPos, &dLen);
  1095. // if findText() succeeded, isolate the text preceding the
  1096. // match, and use "sub" to match that text
  1097. while (dPos >= 0) {
  1098. UnicodeString subText;
  1099. subText.setTo(text, 0, dPos);
  1100. if (subText.length() > 0) {
  1101. UBool success = sub->doParse(subText, tempPP, _baseValue, upperBound,
  1102. #if UCONFIG_NO_COLLATION
  1103. false,
  1104. #else
  1105. formatter->isLenient(),
  1106. #endif
  1107. nonNumericalExecutedRuleMask,
  1108. result);
  1109. // if the substitution could match all the text up to
  1110. // where we found "delimiter", then this function has
  1111. // a successful match. Bump the caller's parse position
  1112. // to point to the first character after the text
  1113. // that matches "delimiter", and return the result
  1114. // we got from parsing the substitution.
  1115. if (success && tempPP.getIndex() == dPos) {
  1116. pp.setIndex(dPos + dLen);
  1117. return result.getDouble();
  1118. }
  1119. else {
  1120. // commented out because ParsePosition doesn't have error index in 1.1.x
  1121. // restored for ICU4C port
  1122. if (tempPP.getErrorIndex() > 0) {
  1123. pp.setErrorIndex(tempPP.getErrorIndex());
  1124. } else {
  1125. pp.setErrorIndex(tempPP.getIndex());
  1126. }
  1127. }
  1128. }
  1129. // if we didn't match the substitution, search for another
  1130. // copy of "delimiter" in "text" and repeat the loop if
  1131. // we find it
  1132. tempPP.setIndex(0);
  1133. dPos = findText(text, delimiter, dPos + dLen, &dLen);
  1134. }
  1135. // if we make it here, this was an unsuccessful match, and we
  1136. // leave pp unchanged and return 0
  1137. pp.setIndex(0);
  1138. return 0;
  1139. // if "delimiter" is empty, or consists only of ignorable characters
  1140. // (i.e., is semantically empty), thwe we obviously can't search
  1141. // for "delimiter". Instead, just use "sub" to parse as much of
  1142. // "text" as possible.
  1143. }
  1144. else if (sub == nullptr) {
  1145. return _baseValue;
  1146. }
  1147. else {
  1148. ParsePosition tempPP;
  1149. Formattable result;
  1150. // try to match the whole string against the substitution
  1151. UBool success = sub->doParse(text, tempPP, _baseValue, upperBound,
  1152. #if UCONFIG_NO_COLLATION
  1153. false,
  1154. #else
  1155. formatter->isLenient(),
  1156. #endif
  1157. nonNumericalExecutedRuleMask,
  1158. result);
  1159. if (success && (tempPP.getIndex() != 0)) {
  1160. // if there's a successful match (or it's a null
  1161. // substitution), update pp to point to the first
  1162. // character we didn't match, and pass the result from
  1163. // sub.doParse() on through to the caller
  1164. pp.setIndex(tempPP.getIndex());
  1165. return result.getDouble();
  1166. }
  1167. else {
  1168. // commented out because ParsePosition doesn't have error index in 1.1.x
  1169. // restored for ICU4C port
  1170. pp.setErrorIndex(tempPP.getErrorIndex());
  1171. }
  1172. // and if we get to here, then nothing matched, so we return
  1173. // 0 and leave pp alone
  1174. return 0;
  1175. }
  1176. }
  1177. /**
  1178. * Used by stripPrefix() to match characters. If lenient parse mode
  1179. * is off, this just calls startsWith(). If lenient parse mode is on,
  1180. * this function uses CollationElementIterators to match characters in
  1181. * the strings (only primary-order differences are significant in
  1182. * determining whether there's a match).
  1183. * @param str The string being tested
  1184. * @param prefix The text we're hoping to see at the beginning
  1185. * of "str"
  1186. * @return If "prefix" is found at the beginning of "str", this
  1187. * is the number of characters in "str" that were matched (this
  1188. * isn't necessarily the same as the length of "prefix" when matching
  1189. * text with a collator). If there's no match, this is 0.
  1190. */
  1191. int32_t
  1192. NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErrorCode& status) const
  1193. {
  1194. // if we're looking for an empty prefix, it obviously matches
  1195. // zero characters. Just go ahead and return 0.
  1196. if (prefix.length() == 0) {
  1197. return 0;
  1198. }
  1199. #if !UCONFIG_NO_COLLATION
  1200. // go through all this grief if we're in lenient-parse mode
  1201. if (formatter->isLenient()) {
  1202. // Check if non-lenient rule finds the text before call lenient parsing
  1203. if (str.startsWith(prefix)) {
  1204. return prefix.length();
  1205. }
  1206. // get the formatter's collator and use it to create two
  1207. // collation element iterators, one over the target string
  1208. // and another over the prefix (right now, we'll throw an
  1209. // exception if the collator we get back from the formatter
  1210. // isn't a RuleBasedCollator, because RuleBasedCollator defines
  1211. // the CollationElementIterator protocol. Hopefully, this
  1212. // will change someday.)
  1213. const RuleBasedCollator* collator = formatter->getCollator();
  1214. if (collator == nullptr) {
  1215. status = U_MEMORY_ALLOCATION_ERROR;
  1216. return 0;
  1217. }
  1218. LocalPointer<CollationElementIterator> strIter(collator->createCollationElementIterator(str));
  1219. LocalPointer<CollationElementIterator> prefixIter(collator->createCollationElementIterator(prefix));
  1220. // Check for memory allocation error.
  1221. if (strIter.isNull() || prefixIter.isNull()) {
  1222. status = U_MEMORY_ALLOCATION_ERROR;
  1223. return 0;
  1224. }
  1225. UErrorCode err = U_ZERO_ERROR;
  1226. // The original code was problematic. Consider this match:
  1227. // prefix = "fifty-"
  1228. // string = " fifty-7"
  1229. // The intent is to match string up to the '7', by matching 'fifty-' at position 1
  1230. // in the string. Unfortunately, we were getting a match, and then computing where
  1231. // the match terminated by rematching the string. The rematch code was using as an
  1232. // initial guess the substring of string between 0 and prefix.length. Because of
  1233. // the leading space and trailing hyphen (both ignorable) this was succeeding, leaving
  1234. // the position before the hyphen in the string. Recursing down, we then parsed the
  1235. // remaining string '-7' as numeric. The resulting number turned out as 43 (50 - 7).
  1236. // This was not pretty, especially since the string "fifty-7" parsed just fine.
  1237. //
  1238. // We have newer APIs now, so we can use calls on the iterator to determine what we
  1239. // matched up to. If we terminate because we hit the last element in the string,
  1240. // our match terminates at this length. If we terminate because we hit the last element
  1241. // in the target, our match terminates at one before the element iterator position.
  1242. // match collation elements between the strings
  1243. int32_t oStr = strIter->next(err);
  1244. int32_t oPrefix = prefixIter->next(err);
  1245. while (oPrefix != CollationElementIterator::NULLORDER) {
  1246. // skip over ignorable characters in the target string
  1247. while (CollationElementIterator::primaryOrder(oStr) == 0
  1248. && oStr != CollationElementIterator::NULLORDER) {
  1249. oStr = strIter->next(err);
  1250. }
  1251. // skip over ignorable characters in the prefix
  1252. while (CollationElementIterator::primaryOrder(oPrefix) == 0
  1253. && oPrefix != CollationElementIterator::NULLORDER) {
  1254. oPrefix = prefixIter->next(err);
  1255. }
  1256. // dlf: move this above following test, if we consume the
  1257. // entire target, aren't we ok even if the source was also
  1258. // entirely consumed?
  1259. // if skipping over ignorables brought to the end of
  1260. // the prefix, we DID match: drop out of the loop
  1261. if (oPrefix == CollationElementIterator::NULLORDER) {
  1262. break;
  1263. }
  1264. // if skipping over ignorables brought us to the end
  1265. // of the target string, we didn't match and return 0
  1266. if (oStr == CollationElementIterator::NULLORDER) {
  1267. return 0;
  1268. }
  1269. // match collation elements from the two strings
  1270. // (considering only primary differences). If we
  1271. // get a mismatch, dump out and return 0
  1272. if (CollationElementIterator::primaryOrder(oStr)
  1273. != CollationElementIterator::primaryOrder(oPrefix)) {
  1274. return 0;
  1275. // otherwise, advance to the next character in each string
  1276. // and loop (we drop out of the loop when we exhaust
  1277. // collation elements in the prefix)
  1278. } else {
  1279. oStr = strIter->next(err);
  1280. oPrefix = prefixIter->next(err);
  1281. }
  1282. }
  1283. int32_t result = strIter->getOffset();
  1284. if (oStr != CollationElementIterator::NULLORDER) {
  1285. --result; // back over character that we don't want to consume;
  1286. }
  1287. #ifdef RBNF_DEBUG
  1288. fprintf(stderr, "prefix length: %d\n", result);
  1289. #endif
  1290. return result;
  1291. #if 0
  1292. //----------------------------------------------------------------
  1293. // JDK 1.2-specific API call
  1294. // return strIter.getOffset();
  1295. //----------------------------------------------------------------
  1296. // JDK 1.1 HACK (take out for 1.2-specific code)
  1297. // if we make it to here, we have a successful match. Now we
  1298. // have to find out HOW MANY characters from the target string
  1299. // matched the prefix (there isn't necessarily a one-to-one
  1300. // mapping between collation elements and characters).
  1301. // In JDK 1.2, there's a simple getOffset() call we can use.
  1302. // In JDK 1.1, on the other hand, we have to go through some
  1303. // ugly contortions. First, use the collator to compare the
  1304. // same number of characters from the prefix and target string.
  1305. // If they're equal, we're done.
  1306. collator->setStrength(Collator::PRIMARY);
  1307. if (str.length() >= prefix.length()) {
  1308. UnicodeString temp;
  1309. temp.setTo(str, 0, prefix.length());
  1310. if (collator->equals(temp, prefix)) {
  1311. #ifdef RBNF_DEBUG
  1312. fprintf(stderr, "returning: %d\n", prefix.length());
  1313. #endif
  1314. return prefix.length();
  1315. }
  1316. }
  1317. // if they're not equal, then we have to compare successively
  1318. // larger and larger substrings of the target string until we
  1319. // get to one that matches the prefix. At that point, we know
  1320. // how many characters matched the prefix, and we can return.
  1321. int32_t p = 1;
  1322. while (p <= str.length()) {
  1323. UnicodeString temp;
  1324. temp.setTo(str, 0, p);
  1325. if (collator->equals(temp, prefix)) {
  1326. return p;
  1327. } else {
  1328. ++p;
  1329. }
  1330. }
  1331. // SHOULD NEVER GET HERE!!!
  1332. return 0;
  1333. //----------------------------------------------------------------
  1334. #endif
  1335. // If lenient parsing is turned off, forget all that crap above.
  1336. // Just use String.startsWith() and be done with it.
  1337. } else
  1338. #endif
  1339. {
  1340. if (str.startsWith(prefix)) {
  1341. return prefix.length();
  1342. } else {
  1343. return 0;
  1344. }
  1345. }
  1346. }
  1347. /**
  1348. * Searches a string for another string. If lenient parsing is off,
  1349. * this just calls indexOf(). If lenient parsing is on, this function
  1350. * uses CollationElementIterator to match characters, and only
  1351. * primary-order differences are significant in determining whether
  1352. * there's a match.
  1353. * @param str The string to search
  1354. * @param key The string to search "str" for
  1355. * @param startingAt The index into "str" where the search is to
  1356. * begin
  1357. * @return A two-element array of ints. Element 0 is the position
  1358. * of the match, or -1 if there was no match. Element 1 is the
  1359. * number of characters in "str" that matched (which isn't necessarily
  1360. * the same as the length of "key")
  1361. */
  1362. int32_t
  1363. NFRule::findText(const UnicodeString& str,
  1364. const UnicodeString& key,
  1365. int32_t startingAt,
  1366. int32_t* length) const
  1367. {
  1368. if (rulePatternFormat) {
  1369. Formattable result;
  1370. FieldPosition position(UNUM_INTEGER_FIELD);
  1371. position.setBeginIndex(startingAt);
  1372. rulePatternFormat->parseType(str, this, result, position);
  1373. int start = position.getBeginIndex();
  1374. if (start >= 0) {
  1375. int32_t pluralRuleStart = fRuleText.indexOf(gDollarOpenParenthesis, -1, 0);
  1376. int32_t pluralRuleSuffix = fRuleText.indexOf(gClosedParenthesisDollar, -1, pluralRuleStart) + 2;
  1377. int32_t matchLen = position.getEndIndex() - start;
  1378. UnicodeString prefix(fRuleText.tempSubString(0, pluralRuleStart));
  1379. UnicodeString suffix(fRuleText.tempSubString(pluralRuleSuffix));
  1380. if (str.compare(start - prefix.length(), prefix.length(), prefix, 0, prefix.length()) == 0
  1381. && str.compare(start + matchLen, suffix.length(), suffix, 0, suffix.length()) == 0)
  1382. {
  1383. *length = matchLen + prefix.length() + suffix.length();
  1384. return start - prefix.length();
  1385. }
  1386. }
  1387. *length = 0;
  1388. return -1;
  1389. }
  1390. if (!formatter->isLenient()) {
  1391. // if lenient parsing is turned off, this is easy: just call
  1392. // String.indexOf() and we're done
  1393. *length = key.length();
  1394. return str.indexOf(key, startingAt);
  1395. }
  1396. else {
  1397. // Check if non-lenient rule finds the text before call lenient parsing
  1398. *length = key.length();
  1399. int32_t pos = str.indexOf(key, startingAt);
  1400. if(pos >= 0) {
  1401. return pos;
  1402. } else {
  1403. // but if lenient parsing is turned ON, we've got some work ahead of us
  1404. return findTextLenient(str, key, startingAt, length);
  1405. }
  1406. }
  1407. }
  1408. int32_t
  1409. NFRule::findTextLenient(const UnicodeString& str,
  1410. const UnicodeString& key,
  1411. int32_t startingAt,
  1412. int32_t* length) const
  1413. {
  1414. //----------------------------------------------------------------
  1415. // JDK 1.1 HACK (take out of 1.2-specific code)
  1416. // in JDK 1.2, CollationElementIterator provides us with an
  1417. // API to map between character offsets and collation elements
  1418. // and we can do this by marching through the string comparing
  1419. // collation elements. We can't do that in JDK 1.1. Instead,
  1420. // we have to go through this horrible slow mess:
  1421. int32_t p = startingAt;
  1422. int32_t keyLen = 0;
  1423. // basically just isolate smaller and smaller substrings of
  1424. // the target string (each running to the end of the string,
  1425. // and with the first one running from startingAt to the end)
  1426. // and then use prefixLength() to see if the search key is at
  1427. // the beginning of each substring. This is excruciatingly
  1428. // slow, but it will locate the key and tell use how long the
  1429. // matching text was.
  1430. UnicodeString temp;
  1431. UErrorCode status = U_ZERO_ERROR;
  1432. while (p < str.length() && keyLen == 0) {
  1433. temp.setTo(str, p, str.length() - p);
  1434. keyLen = prefixLength(temp, key, status);
  1435. if (U_FAILURE(status)) {
  1436. break;
  1437. }
  1438. if (keyLen != 0) {
  1439. *length = keyLen;
  1440. return p;
  1441. }
  1442. ++p;
  1443. }
  1444. // if we make it to here, we didn't find it. Return -1 for the
  1445. // location. The length should be ignored, but set it to 0,
  1446. // which should be "safe"
  1447. *length = 0;
  1448. return -1;
  1449. }
  1450. /**
  1451. * Checks to see whether a string consists entirely of ignorable
  1452. * characters.
  1453. * @param str The string to test.
  1454. * @return true if the string is empty of consists entirely of
  1455. * characters that the number formatter's collator says are
  1456. * ignorable at the primary-order level. false otherwise.
  1457. */
  1458. UBool
  1459. NFRule::allIgnorable(const UnicodeString& str, UErrorCode& status) const
  1460. {
  1461. // if the string is empty, we can just return true
  1462. if (str.length() == 0) {
  1463. return true;
  1464. }
  1465. #if !UCONFIG_NO_COLLATION
  1466. // if lenient parsing is turned on, walk through the string with
  1467. // a collation element iterator and make sure each collation
  1468. // element is 0 (ignorable) at the primary level
  1469. if (formatter->isLenient()) {
  1470. const RuleBasedCollator* collator = formatter->getCollator();
  1471. if (collator == nullptr) {
  1472. status = U_MEMORY_ALLOCATION_ERROR;
  1473. return false;
  1474. }
  1475. LocalPointer<CollationElementIterator> iter(collator->createCollationElementIterator(str));
  1476. // Memory allocation error check.
  1477. if (iter.isNull()) {
  1478. status = U_MEMORY_ALLOCATION_ERROR;
  1479. return false;
  1480. }
  1481. UErrorCode err = U_ZERO_ERROR;
  1482. int32_t o = iter->next(err);
  1483. while (o != CollationElementIterator::NULLORDER
  1484. && CollationElementIterator::primaryOrder(o) == 0) {
  1485. o = iter->next(err);
  1486. }
  1487. return o == CollationElementIterator::NULLORDER;
  1488. }
  1489. #endif
  1490. // if lenient parsing is turned off, there is no such thing as
  1491. // an ignorable character: return true only if the string is empty
  1492. return false;
  1493. }
  1494. void
  1495. NFRule::setDecimalFormatSymbols(const DecimalFormatSymbols& newSymbols, UErrorCode& status) {
  1496. if (sub1 != nullptr) {
  1497. sub1->setDecimalFormatSymbols(newSymbols, status);
  1498. }
  1499. if (sub2 != nullptr) {
  1500. sub2->setDecimalFormatSymbols(newSymbols, status);
  1501. }
  1502. }
  1503. U_NAMESPACE_END
  1504. /* U_HAVE_RBNF */
  1505. #endif