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