2 ******************************************************************************
3 * Copyright (C) 1997-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ******************************************************************************
6 * file name: nfrule.cpp
8 * tab size: 8 (not used)
11 * Modification history
13 * 10/11/2001 Doug Ported from ICU4J
20 #include "unicode/rbnf.h"
21 #include "unicode/tblcoll.h"
22 #include "unicode/coleitr.h"
23 #include "unicode/uchar.h"
32 NFRule::NFRule(const RuleBasedNumberFormat
* _rbnf
)
33 : baseValue((int32_t)0)
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;
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" */
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}; /* ">>>" */
83 static const UChar
* const tokenStrings
[] = {
84 gLessLess
, gLessPercent
, gLessHash
, gLessZero
,
85 gGreaterGreater
, gGreaterPercent
,gGreaterHash
, gGreaterZero
,
86 gEqualPercent
, gEqualHash
, gEqualZero
, NULL
90 NFRule::makeRules(UnicodeString
& description
,
91 const NFRuleSet
*ruleSet
,
92 const NFRule
*predecessor
,
93 const RuleBasedNumberFormat
*rbnf
,
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
);
104 status
= U_MEMORY_ALLOCATION_ERROR
;
107 rule1
->parseRuleDescriptor(description
, status
);
109 // check the description to see whether there's text enclosed
111 int32_t brack1
= description
.indexOf(gLeftBracket
);
112 int32_t brack2
= description
.indexOf(gRightBracket
);
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
);
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
;
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
) {
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
);
146 status
= U_MEMORY_ALLOCATION_ERROR
;
149 if (rule1
->baseValue
>= 0) {
150 rule2
->baseValue
= rule1
->baseValue
;
151 if (!ruleSet
->isFractionRuleSet()) {
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
);
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
);
171 // both rules have the same radix and exponent (i.e., the
173 rule2
->radix
= rule1
->radix
;
174 rule2
->exponent
= rule1
->exponent
;
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);
182 rule2
->ruleText
.setTo(sbuf
);
183 rule2
->extractSubstitutions(ruleSet
, predecessor
, rbnf
, status
);
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);
194 rule1
->ruleText
.setTo(sbuf
);
195 rule1
->extractSubstitutions(ruleSet
, predecessor
, rbnf
, status
);
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
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.
222 NFRule::parseRuleDescriptor(UnicodeString
& description
, UErrorCode
& status
)
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
);
229 setBaseValue((int32_t)0, status
);
231 // copy the descriptor out into its own string and strip it,
232 // along with any trailing whitespace, out of the original
234 UnicodeString descriptor
;
235 descriptor
.setTo(description
, 0, p
);
238 while (p
< description
.length() && uprv_isRuleWhiteSpace(description
.charAt(p
))) {
241 description
.removeBetween(0, p
);
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
);
249 else if (descriptor
== gXDotX
) {
250 setType(kImproperFractionRule
);
252 else if (descriptor
== gZeroDotX
) {
253 setType(kProperFractionRule
);
255 else if (descriptor
== gXDotZero
) {
256 setType(kMasterRule
);
259 // if the rule descriptor begins with a digit, it's a descriptor
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
) {
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
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
);
278 else if (c
== gSlash
|| c
== gGreaterThan
) {
281 else if (uprv_isRuleWhiteSpace(c
) || c
== gComma
|| c
== gDot
) {
284 // throw new IllegalArgumentException("Illegal character in rule descriptor");
285 status
= U_PARSE_ERROR
;
291 // we have the base value, so set it
292 setBaseValue(val
, status
);
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
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
);
307 else if (c
== gGreaterThan
) {
310 else if (uprv_isRuleWhiteSpace(c
) || c
== gComma
|| c
== gDot
) {
313 // throw new IllegalArgumentException("Illegal character is rule descriptor");
314 status
= U_PARSE_ERROR
;
320 // tempValue now contain's the rule's radix. Set it
321 // accordingly, and recalculate the rule's exponent
322 radix
= (int32_t)val
;
324 // throw new IllegalArgumentException("Rule can't have radix of 0");
325 status
= U_PARSE_ERROR
;
328 exponent
= expectedExponent();
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) {
342 // throw new IllegalArgumentException("Illegal character in rule descriptor");
343 status
= U_PARSE_ERROR
;
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);
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;
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
373 NFRule::extractSubstitutions(const NFRuleSet
* ruleSet
,
374 const NFRule
* predecessor
,
375 const RuleBasedNumberFormat
* rbnf
,
378 if (U_SUCCESS(status
)) {
379 sub1
= extractSubstitution(ruleSet
, predecessor
, rbnf
, status
);
380 sub2
= extractSubstitution(ruleSet
, predecessor
, rbnf
, status
);
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
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.
397 NFRule::extractSubstitution(const NFRuleSet
* ruleSet
,
398 const NFRule
* predecessor
,
399 const RuleBasedNumberFormat
* rbnf
,
402 NFSubstitution
* result
= NULL
;
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
;
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
);
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;
421 // otherwise the substitution token ends with the same character
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...
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
440 return NFSubstitution::makeSubstitution(ruleText
.length(), this, predecessor
,
441 ruleSet
, rbnf
, gEmptyString
, status
);
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
);
452 // remove the substitution from the rule text
453 ruleText
.removeBetween(subStart
, subEnd
+1);
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.
466 NFRule::setBaseValue(int64_t newBaseValue
, UErrorCode
& status
)
468 // set the base value
469 baseValue
= newBaseValue
;
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) {
478 exponent
= expectedExponent();
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.
485 sub1
->setDivisor(radix
, exponent
, status
);
488 sub2
->setDivisor(radix
, exponent
, status
);
491 // if this is a special rule, its radix and exponent are basically
492 // ignored. Set them to "safe" default values
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.
505 NFRule::expectedExponent() const
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) {
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
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
) {
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
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.
535 NFRule::indexOfAny(const UChar
* const strings
[]) const
538 for (int i
= 0; strings
[i
]; i
++) {
539 int32_t pos
= ruleText
.indexOf(*strings
[i
]);
540 if (pos
!= -1 && (result
== -1 || pos
< result
)) {
547 //-----------------------------------------------------------------------
549 //-----------------------------------------------------------------------
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
557 NFRule::operator==(const NFRule
& rhs
) const
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
;
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
573 static void util_append64(UnicodeString
& result
, int64_t n
)
576 int32_t len
= util64_tou(n
, buffer
, sizeof(buffer
));
577 UnicodeString
temp(buffer
, len
);
582 NFRule::_appendRuleText(UnicodeString
& result
) const
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;
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
596 util_append64(result
, baseValue
);
598 result
.append(gSlash
);
599 util_append64(result
, radix
);
601 int numCarets
= expectedExponent() - exponent
;
602 for (int i
= 0; i
< numCarets
; i
++) {
603 result
.append(gGreaterThan
);
607 result
.append(gColon
);
608 result
.append(gSpace
);
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
);
617 // now, write the rule's rule text, inserting appropriate
618 // substitution tokens in the appropriate places
619 UnicodeString ruleTextCopy
;
620 ruleTextCopy
.setTo(ruleText
);
623 sub2
->toString(temp
);
624 ruleTextCopy
.insert(sub2
->getPos(), temp
);
625 sub1
->toString(temp
);
626 ruleTextCopy
.insert(sub1
->getPos(), temp
);
628 result
.append(ruleTextCopy
);
630 // and finally, top the whole thing off with a semicolon and
632 result
.append(gSemicolon
);
635 //-----------------------------------------------------------------------
637 //-----------------------------------------------------------------------
640 * Formats the number, and inserts the resulting text into
642 * @param number The number being formatted
643 * @param toInsertInto The string where the resultant text should
645 * @param pos The position in toInsertInto where the resultant text
649 NFRule::doFormat(int64_t number
, UnicodeString
& toInsertInto
, int32_t pos
) const
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
656 toInsertInto
.insert(pos
, ruleText
);
657 sub2
->doSubstitution(number
, toInsertInto
, pos
);
658 sub1
->doSubstitution(number
, toInsertInto
, pos
);
662 * Formats the number, and inserts the resulting text into
664 * @param number The number being formatted
665 * @param toInsertInto The string where the resultant text should
667 * @param pos The position in toInsertInto where the resultant text
671 NFRule::doFormat(double number
, UnicodeString
& toInsertInto
, int32_t pos
) const
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
679 toInsertInto
.insert(pos
, ruleText
);
680 sub2
->doSubstitution(number
, toInsertInto
, pos
);
681 sub1
->doSubstitution(number
, toInsertInto
, pos
);
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
693 NFRule::shouldRollBack(double number
) const
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
699 // In other words, if the original description had
700 // 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;
718 //-----------------------------------------------------------------------
720 //-----------------------------------------------------------------------
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
731 * @param isFractionRule True if this rule is contained within a
732 * fraction rule set. This is only used if the rule has no
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.
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 us
.extract(0, len
, buf
);
748 fprintf(f
, "%s", buf
);
749 uprv_free(buf
); //delete[] buf;
754 NFRule::doParse(const UnicodeString
& text
,
755 ParsePosition
& parsePosition
,
756 UBool isFractionRule
,
758 Formattable
& resVal
) const
760 // internally we operate on a copy of the string being parsed
761 // (because we're going to change it) and use our own ParsePosition
763 UnicodeString
workText(text
);
765 // check to see whether the text before the first substitution
766 // matches the text at the beginning of the string being
767 // parsed. If it does, strip that off the front of workText;
768 // otherwise, dump out with a mismatch
769 UnicodeString prefix
;
770 prefix
.setTo(ruleText
, 0, sub1
->getPos());
773 fprintf(stderr
, "doParse %x ", this);
780 fprintf(stderr
, " text: '", this);
781 dumpUS(stderr
, text
);
782 fprintf(stderr
, "' prefix: '");
783 dumpUS(stderr
, prefix
);
785 stripPrefix(workText
, prefix
, pp
);
786 int32_t prefixLength
= text
.length() - workText
.length();
789 fprintf(stderr
, "' pl: %d ppi: %d s1p: %d\n", prefixLength
, pp
.getIndex(), sub1
->getPos());
792 if (pp
.getIndex() == 0 && sub1
->getPos() != 0) {
793 // commented out because ParsePosition doesn't have error index in 1.1.x
794 // restored for ICU4C port
795 parsePosition
.setErrorIndex(pp
.getErrorIndex());
800 // this is the fun part. The basic guts of the rule-matching
801 // logic is matchToDelimiter(), which is called twice. The first
802 // time it searches the input string for the rule text BETWEEN
803 // the substitutions and tries to match the intervening text
804 // in the input string with the first substitution. If that
805 // succeeds, it then calls it again, this time to look for the
806 // rule text after the second substitution and to match the
807 // intervening input text against the second substitution.
809 // For example, say we have a rule that looks like this:
810 // first << middle >> last;
811 // and input text that looks like this:
812 // first one middle two last
813 // First we use stripPrefix() to match "first " in both places and
814 // strip it off the front, leaving
815 // one middle two last
816 // Then we use matchToDelimiter() to match " middle " and try to
817 // match "one" against a substitution. If it's successful, we now
820 // We use matchToDelimiter() a second time to match " last" and
821 // try to match "two" against a substitution. If "two" matches
822 // the substitution, we have a successful parse.
824 // Since it's possible in many cases to find multiple instances
825 // of each of these pieces of rule text in the input string,
826 // we need to try all the possible combinations of these
827 // locations. This prevents us from prematurely declaring a mismatch,
828 // and makes sure we match as much input text as we can.
829 int highWaterMark
= 0;
832 double tempBaseValue
= (double)(baseValue
<= 0 ? 0 : baseValue
);
836 // our partial parse result starts out as this rule's base
837 // value. If it finds a successful match, matchToDelimiter()
838 // will compose this in some way with what it gets back from
839 // the substitution, giving us a new partial parse result
842 temp
.setTo(ruleText
, sub1
->getPos(), sub2
->getPos() - sub1
->getPos());
843 double partialResult
= matchToDelimiter(workText
, start
, tempBaseValue
,
847 // if we got a successful match (or were trying to match a
848 // null substitution), pp is now pointing at the first unmatched
849 // character. Take note of that, and try matchToDelimiter()
850 // on the input text again
851 if (pp
.getIndex() != 0 || sub1
->isNullSubstitution()) {
852 start
= pp
.getIndex();
854 UnicodeString workText2
;
855 workText2
.setTo(workText
, pp
.getIndex(), workText
.length() - pp
.getIndex());
858 // the second matchToDelimiter() will compose our previous
859 // partial result with whatever it gets back from its
860 // substitution if there's a successful match, giving us
862 temp
.setTo(ruleText
, sub2
->getPos(), ruleText
.length() - sub2
->getPos());
863 partialResult
= matchToDelimiter(workText2
, 0, partialResult
,
867 // if we got a successful match on this second
868 // matchToDelimiter() call, update the high-water mark
869 // and result (if necessary)
870 if (pp2
.getIndex() != 0 || sub2
->isNullSubstitution()) {
871 if (prefixLength
+ pp
.getIndex() + pp2
.getIndex() > highWaterMark
) {
872 highWaterMark
= prefixLength
+ pp
.getIndex() + pp2
.getIndex();
873 result
= partialResult
;
876 // commented out because ParsePosition doesn't have error index in 1.1.x
877 // restored for ICU4C port
879 int32_t temp
= pp2
.getErrorIndex() + sub1
->getPos() + pp
.getIndex();
880 if (temp
> parsePosition
.getErrorIndex()) {
881 parsePosition
.setErrorIndex(temp
);
885 // commented out because ParsePosition doesn't have error index in 1.1.x
886 // restored for ICU4C port
888 int32_t temp
= sub1
->getPos() + pp
.getErrorIndex();
889 if (temp
> parsePosition
.getErrorIndex()) {
890 parsePosition
.setErrorIndex(temp
);
893 // keep trying to match things until the outer matchToDelimiter()
894 // call fails to make a match (each time, it picks up where it
895 // left off the previous time)
896 } while (sub1
->getPos() != sub2
->getPos()
898 && pp
.getIndex() < workText
.length()
899 && pp
.getIndex() != start
);
901 // update the caller's ParsePosition with our high-water mark
902 // (i.e., it now points at the first character this function
903 // didn't match-- the ParsePosition is therefore unchanged if
904 // we didn't match anything)
905 parsePosition
.setIndex(highWaterMark
);
906 // commented out because ParsePosition doesn't have error index in 1.1.x
907 // restored for ICU4C port
908 if (highWaterMark
> 0) {
909 parsePosition
.setErrorIndex(0);
912 // this is a hack for one unusual condition: Normally, whether this
913 // rule belong to a fraction rule set or not is handled by its
914 // substitutions. But if that rule HAS NO substitutions, then
915 // we have to account for it here. By definition, if the matching
916 // rule in a fraction rule set has no substitutions, its numerator
917 // is 1, and so the result is the reciprocal of its base value.
918 if (isFractionRule
&&
920 sub1
->isNullSubstitution()) {
924 resVal
.setDouble(result
);
925 return TRUE
; // ??? do we need to worry if it is a long or a double?
929 * This function is used by parse() to match the text being parsed
930 * against a possible prefix string. This function
931 * matches characters from the beginning of the string being parsed
932 * to characters from the prospective prefix. If they match, pp is
933 * updated to the first character not matched, and the result is
934 * the unparsed part of the string. If they don't match, the whole
935 * string is returned, and pp is left unchanged.
936 * @param text The string being parsed
937 * @param prefix The text to match against
938 * @param pp On entry, ignored and assumed to be 0. On exit, points
939 * to the first unmatched character (assuming the whole prefix matched),
940 * or is unchanged (if the whole prefix didn't match).
941 * @return If things match, this is the unparsed part of "text";
942 * if they didn't match, this is "text".
945 NFRule::stripPrefix(UnicodeString
& text
, const UnicodeString
& prefix
, ParsePosition
& pp
) const
947 // if the prefix text is empty, dump out without doing anything
948 if (prefix
.length() != 0) {
949 // use prefixLength() to match the beginning of
950 // "text" against "prefix". This function returns the
951 // number of characters from "text" that matched (or 0 if
952 // we didn't match the whole prefix)
953 int32_t pfl
= prefixLength(text
, prefix
);
955 // if we got a successful match, update the parse position
956 // and strip the prefix off of "text"
957 pp
.setIndex(pp
.getIndex() + pfl
);
964 * Used by parse() to match a substitution and any following text.
965 * "text" is searched for instances of "delimiter". For each instance
966 * of delimiter, the intervening text is tested to see whether it
967 * matches the substitution. The longest match wins.
968 * @param text The string being parsed
969 * @param startPos The position in "text" where we should start looking
971 * @param baseValue A partial parse result (often the rule's base value),
972 * which is combined with the result from matching the substitution
973 * @param delimiter The string to search "text" for.
974 * @param pp Ignored and presumed to be 0 on entry. If there's a match,
975 * on exit this will point to the first unmatched character.
976 * @param sub If we find "delimiter" in "text", this substitution is used
977 * to match the text between the beginning of the string and the
978 * position of "delimiter." (If "delimiter" is the empty string, then
979 * this function just matches against this substitution and updates
980 * everything accordingly.)
981 * @param upperBound When matching the substitution, it will only
982 * consider rules with base values lower than this value.
983 * @return If there's a match, this is the result of composing
984 * baseValue with the result of matching the substitution. Otherwise,
985 * this is new Long(0). It's never null. If the result is an integer,
986 * this will be an instance of Long; otherwise, it's an instance of
989 * !!! note {dlf} in point of fact, in the java code the caller always converts
990 * the result to a double, so we might as well return one.
993 NFRule::matchToDelimiter(const UnicodeString
& text
,
996 const UnicodeString
& delimiter
,
998 const NFSubstitution
* sub
,
999 double upperBound
) const
1001 // if "delimiter" contains real (i.e., non-ignorable) text, search
1002 // it for "delimiter" beginning at "start". If that succeeds, then
1003 // use "sub"'s doParse() method to match the text before the
1004 // instance of "delimiter" we just found.
1005 if (!allIgnorable(delimiter
)) {
1006 ParsePosition tempPP
;
1009 // use findText() to search for "delimiter". It returns a two-
1010 // element array: element 0 is the position of the match, and
1011 // element 1 is the number of characters that matched
1014 int32_t dPos
= findText(text
, delimiter
, startPos
, &dLen
);
1016 // if findText() succeeded, isolate the text preceding the
1017 // match, and use "sub" to match that text
1019 UnicodeString subText
;
1020 subText
.setTo(text
, 0, dPos
);
1021 if (subText
.length() > 0) {
1022 UBool success
= sub
->doParse(subText
, tempPP
, _baseValue
, upperBound
,
1023 #if UCONFIG_NO_COLLATION
1026 formatter
->isLenient(),
1030 // if the substitution could match all the text up to
1031 // where we found "delimiter", then this function has
1032 // a successful match. Bump the caller's parse position
1033 // to point to the first character after the text
1034 // that matches "delimiter", and return the result
1035 // we got from parsing the substitution.
1036 if (success
&& tempPP
.getIndex() == dPos
) {
1037 pp
.setIndex(dPos
+ dLen
);
1038 return result
.getDouble();
1040 // commented out because ParsePosition doesn't have error index in 1.1.x
1041 // restored for ICU4C port
1043 if (tempPP
.getErrorIndex() > 0) {
1044 pp
.setErrorIndex(tempPP
.getErrorIndex());
1046 pp
.setErrorIndex(tempPP
.getIndex());
1051 // if we didn't match the substitution, search for another
1052 // copy of "delimiter" in "text" and repeat the loop if
1055 dPos
= findText(text
, delimiter
, dPos
+ dLen
, &dLen
);
1057 // if we make it here, this was an unsuccessful match, and we
1058 // leave pp unchanged and return 0
1062 // if "delimiter" is empty, or consists only of ignorable characters
1063 // (i.e., is semantically empty), thwe we obviously can't search
1064 // for "delimiter". Instead, just use "sub" to parse as much of
1065 // "text" as possible.
1067 ParsePosition tempPP
;
1070 // try to match the whole string against the substitution
1071 UBool success
= sub
->doParse(text
, tempPP
, _baseValue
, upperBound
,
1072 #if UCONFIG_NO_COLLATION
1075 formatter
->isLenient(),
1078 if (success
&& (tempPP
.getIndex() != 0 || sub
->isNullSubstitution())) {
1079 // if there's a successful match (or it's a null
1080 // substitution), update pp to point to the first
1081 // character we didn't match, and pass the result from
1082 // sub.doParse() on through to the caller
1083 pp
.setIndex(tempPP
.getIndex());
1084 return result
.getDouble();
1086 // commented out because ParsePosition doesn't have error index in 1.1.x
1087 // restored for ICU4C port
1089 pp
.setErrorIndex(tempPP
.getErrorIndex());
1092 // and if we get to here, then nothing matched, so we return
1093 // 0 and leave pp alone
1099 * Used by stripPrefix() to match characters. If lenient parse mode
1100 * is off, this just calls startsWith(). If lenient parse mode is on,
1101 * this function uses CollationElementIterators to match characters in
1102 * the strings (only primary-order differences are significant in
1103 * determining whether there's a match).
1104 * @param str The string being tested
1105 * @param prefix The text we're hoping to see at the beginning
1107 * @return If "prefix" is found at the beginning of "str", this
1108 * is the number of characters in "str" that were matched (this
1109 * isn't necessarily the same as the length of "prefix" when matching
1110 * text with a collator). If there's no match, this is 0.
1113 NFRule::prefixLength(const UnicodeString
& str
, const UnicodeString
& prefix
) const
1115 // if we're looking for an empty prefix, it obviously matches
1116 // zero characters. Just go ahead and return 0.
1117 if (prefix
.length() == 0) {
1121 #if !UCONFIG_NO_COLLATION
1122 // go through all this grief if we're in lenient-parse mode
1123 if (formatter
->isLenient()) {
1124 // get the formatter's collator and use it to create two
1125 // collation element iterators, one over the target string
1126 // and another over the prefix (right now, we'll throw an
1127 // exception if the collator we get back from the formatter
1128 // isn't a RuleBasedCollator, because RuleBasedCollator defines
1129 // the CollationElementIterator protocol. Hopefully, this
1130 // will change someday.)
1131 RuleBasedCollator
* collator
= (RuleBasedCollator
*)formatter
->getCollator();
1132 CollationElementIterator
* strIter
= collator
->createCollationElementIterator(str
);
1133 CollationElementIterator
* prefixIter
= collator
->createCollationElementIterator(prefix
);
1135 UErrorCode err
= U_ZERO_ERROR
;
1137 // The original code was problematic. Consider this match:
1138 // prefix = "fifty-"
1139 // string = " fifty-7"
1140 // The intent is to match string up to the '7', by matching 'fifty-' at position 1
1141 // in the string. Unfortunately, we were getting a match, and then computing where
1142 // the match terminated by rematching the string. The rematch code was using as an
1143 // initial guess the substring of string between 0 and prefix.length. Because of
1144 // the leading space and trailing hyphen (both ignorable) this was succeeding, leaving
1145 // the position before the hyphen in the string. Recursing down, we then parsed the
1146 // remaining string '-7' as numeric. The resulting number turned out as 43 (50 - 7).
1147 // This was not pretty, especially since the string "fifty-7" parsed just fine.
1149 // We have newer APIs now, so we can use calls on the iterator to determine what we
1150 // matched up to. If we terminate because we hit the last element in the string,
1151 // our match terminates at this length. If we terminate because we hit the last element
1152 // in the target, our match terminates at one before the element iterator position.
1154 // match collation elements between the strings
1155 int32_t oStr
= strIter
->next(err
);
1156 int32_t oPrefix
= prefixIter
->next(err
);
1158 while (oPrefix
!= CollationElementIterator::NULLORDER
) {
1159 // skip over ignorable characters in the target string
1160 while (CollationElementIterator::primaryOrder(oStr
) == 0
1161 && oStr
!= CollationElementIterator::NULLORDER
) {
1162 oStr
= strIter
->next(err
);
1165 // skip over ignorable characters in the prefix
1166 while (CollationElementIterator::primaryOrder(oPrefix
) == 0
1167 && oPrefix
!= CollationElementIterator::NULLORDER
) {
1168 oPrefix
= prefixIter
->next(err
);
1171 // dlf: move this above following test, if we consume the
1172 // entire target, aren't we ok even if the source was also
1173 // entirely consumed?
1175 // if skipping over ignorables brought to the end of
1176 // the prefix, we DID match: drop out of the loop
1177 if (oPrefix
== CollationElementIterator::NULLORDER
) {
1181 // if skipping over ignorables brought us to the end
1182 // of the target string, we didn't match and return 0
1183 if (oStr
== CollationElementIterator::NULLORDER
) {
1189 // match collation elements from the two strings
1190 // (considering only primary differences). If we
1191 // get a mismatch, dump out and return 0
1192 if (CollationElementIterator::primaryOrder(oStr
)
1193 != CollationElementIterator::primaryOrder(oPrefix
)) {
1198 // otherwise, advance to the next character in each string
1199 // and loop (we drop out of the loop when we exhaust
1200 // collation elements in the prefix)
1202 oStr
= strIter
->next(err
);
1203 oPrefix
= prefixIter
->next(err
);
1207 int32_t result
= strIter
->getOffset();
1208 if (oStr
!= CollationElementIterator::NULLORDER
) {
1209 --result
; // back over character that we don't want to consume;
1213 fprintf(stderr
, "prefix length: %d\n", result
);
1220 //----------------------------------------------------------------
1221 // JDK 1.2-specific API call
1222 // return strIter.getOffset();
1223 //----------------------------------------------------------------
1224 // JDK 1.1 HACK (take out for 1.2-specific code)
1226 // if we make it to here, we have a successful match. Now we
1227 // have to find out HOW MANY characters from the target string
1228 // matched the prefix (there isn't necessarily a one-to-one
1229 // mapping between collation elements and characters).
1230 // In JDK 1.2, there's a simple getOffset() call we can use.
1231 // In JDK 1.1, on the other hand, we have to go through some
1232 // ugly contortions. First, use the collator to compare the
1233 // same number of characters from the prefix and target string.
1234 // If they're equal, we're done.
1235 collator
->setStrength(Collator::PRIMARY
);
1236 if (str
.length() >= prefix
.length()) {
1238 temp
.setTo(str
, 0, prefix
.length());
1239 if (collator
->equals(temp
, prefix
)) {
1241 fprintf(stderr
, "returning: %d\n", prefix
.length());
1243 return prefix
.length();
1247 // if they're not equal, then we have to compare successively
1248 // larger and larger substrings of the target string until we
1249 // get to one that matches the prefix. At that point, we know
1250 // how many characters matched the prefix, and we can return.
1252 while (p
<= str
.length()) {
1254 temp
.setTo(str
, 0, p
);
1255 if (collator
->equals(temp
, prefix
)) {
1262 // SHOULD NEVER GET HERE!!!
1264 //----------------------------------------------------------------
1267 // If lenient parsing is turned off, forget all that crap above.
1268 // Just use String.startsWith() and be done with it.
1272 if (str
.startsWith(prefix
)) {
1273 return prefix
.length();
1281 * Searches a string for another string. If lenient parsing is off,
1282 * this just calls indexOf(). If lenient parsing is on, this function
1283 * uses CollationElementIterator to match characters, and only
1284 * primary-order differences are significant in determining whether
1286 * @param str The string to search
1287 * @param key The string to search "str" for
1288 * @param startingAt The index into "str" where the search is to
1290 * @return A two-element array of ints. Element 0 is the position
1291 * of the match, or -1 if there was no match. Element 1 is the
1292 * number of characters in "str" that matched (which isn't necessarily
1293 * the same as the length of "key")
1296 NFRule::findText(const UnicodeString
& str
,
1297 const UnicodeString
& key
,
1299 int32_t* length
) const
1301 #if !UCONFIG_NO_COLLATION
1302 // if lenient parsing is turned off, this is easy: just call
1303 // String.indexOf() and we're done
1304 if (!formatter
->isLenient()) {
1305 *length
= key
.length();
1306 return str
.indexOf(key
, startingAt
);
1308 // but if lenient parsing is turned ON, we've got some work
1313 //----------------------------------------------------------------
1314 // JDK 1.1 HACK (take out of 1.2-specific code)
1316 // in JDK 1.2, CollationElementIterator provides us with an
1317 // API to map between character offsets and collation elements
1318 // and we can do this by marching through the string comparing
1319 // collation elements. We can't do that in JDK 1.1. Insted,
1320 // we have to go through this horrible slow mess:
1321 int32_t p
= startingAt
;
1324 // basically just isolate smaller and smaller substrings of
1325 // the target string (each running to the end of the string,
1326 // and with the first one running from startingAt to the end)
1327 // and then use prefixLength() to see if the search key is at
1328 // the beginning of each substring. This is excruciatingly
1329 // slow, but it will locate the key and tell use how long the
1330 // matching text was.
1332 while (p
< str
.length() && keyLen
== 0) {
1333 temp
.setTo(str
, p
, str
.length() - p
);
1334 keyLen
= prefixLength(temp
, key
);
1341 // if we make it to here, we didn't find it. Return -1 for the
1342 // location. The length should be ignored, but set it to 0,
1343 // which should be "safe"
1347 //----------------------------------------------------------------
1348 // JDK 1.2 version of this routine
1349 //RuleBasedCollator collator = (RuleBasedCollator)formatter.getCollator();
1351 //CollationElementIterator strIter = collator.getCollationElementIterator(str);
1352 //CollationElementIterator keyIter = collator.getCollationElementIterator(key);
1354 //int keyStart = -1;
1356 //str.setOffset(startingAt);
1358 //int oStr = strIter.next();
1359 //int oKey = keyIter.next();
1360 //while (oKey != CollationElementIterator.NULLORDER) {
1361 // while (oStr != CollationElementIterator.NULLORDER &&
1362 // CollationElementIterator.primaryOrder(oStr) == 0)
1363 // oStr = strIter.next();
1365 // while (oKey != CollationElementIterator.NULLORDER &&
1366 // CollationElementIterator.primaryOrder(oKey) == 0)
1367 // oKey = keyIter.next();
1369 // if (oStr == CollationElementIterator.NULLORDER) {
1370 // return new int[] { -1, 0 };
1373 // if (oKey == CollationElementIterator.NULLORDER) {
1377 // if (CollationElementIterator.primaryOrder(oStr) ==
1378 // CollationElementIterator.primaryOrder(oKey)) {
1379 // keyStart = strIter.getOffset();
1380 // oStr = strIter.next();
1381 // oKey = keyIter.next();
1383 // if (keyStart != -1) {
1387 // oStr = strIter.next();
1392 //if (oKey == CollationElementIterator.NULLORDER) {
1393 // return new int[] { keyStart, strIter.getOffset() - keyStart };
1395 // return new int[] { -1, 0 };
1401 * Checks to see whether a string consists entirely of ignorable
1403 * @param str The string to test.
1404 * @return true if the string is empty of consists entirely of
1405 * characters that the number formatter's collator says are
1406 * ignorable at the primary-order level. false otherwise.
1409 NFRule::allIgnorable(const UnicodeString
& str
) const
1411 // if the string is empty, we can just return true
1412 if (str
.length() == 0) {
1416 #if !UCONFIG_NO_COLLATION
1417 // if lenient parsing is turned on, walk through the string with
1418 // a collation element iterator and make sure each collation
1419 // element is 0 (ignorable) at the primary level
1420 if (formatter
->isLenient()) {
1421 RuleBasedCollator
* collator
= (RuleBasedCollator
*)(formatter
->getCollator());
1422 CollationElementIterator
* iter
= collator
->createCollationElementIterator(str
);
1424 UErrorCode err
= U_ZERO_ERROR
;
1425 int32_t o
= iter
->next(err
);
1426 while (o
!= CollationElementIterator::NULLORDER
1427 && CollationElementIterator::primaryOrder(o
) == 0) {
1428 o
= iter
->next(err
);
1432 return o
== CollationElementIterator::NULLORDER
;
1436 // if lenient parsing is turned off, there is no such thing as
1437 // an ignorable character: return true only if the string is empty