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