2
******************************************************************************
3
* Copyright (C) 1997-2001, 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"
30
extern const UChar* CSleftBracket;
31
extern const UChar* CSrightBracket;
33
NFRule::NFRule(const RuleBasedNumberFormat* _rbnf)
34
: baseValue((int32_t)0)
50
static const UChar gLeftBracket = 0x005b;
51
static const UChar gRightBracket = 0x005d;
52
static const UChar gColon = 0x003a;
53
static const UChar gZero = 0x0030;
54
static const UChar gNine = 0x0039;
55
static const UChar gSpace = 0x0020;
56
static const UChar gSlash = 0x002f;
57
static const UChar gGreaterThan = 0x003e;
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);
102
rule1->parseRuleDescriptor(description, status);
104
// check the description to see whether there's text enclosed
106
int32_t brack1 = description.indexOf(gLeftBracket);
107
int32_t brack2 = description.indexOf(gRightBracket);
109
// if the description doesn't contain a matched pair of brackets,
110
// or if it's of a type that doesn't recognize bracketed text,
111
// then leave the description alone, initialize the rule's
112
// rule text and substitutions, and return that rule
113
if (brack1 == -1 || brack2 == -1 || brack1 > brack2
114
|| rule1->getType() == kProperFractionRule
115
|| rule1->getType() == kNegativeNumberRule) {
116
rule1->ruleText = description;
117
rule1->extractSubstitutions(ruleSet, predecessor, rbnf, status);
120
// if the description does contain a matched pair of brackets,
121
// then it's really shorthand for two rules (with one exception)
122
NFRule* rule2 = NULL;
125
// we'll actually only split the rule into two rules if its
126
// base value is an even multiple of its divisor (or it's one
127
// of the special rules)
128
if ((rule1->baseValue > 0
129
&& (rule1->baseValue % util64_pow(rule1->radix, rule1->exponent)) == 0)
130
|| rule1->getType() == kImproperFractionRule
131
|| rule1->getType() == kMasterRule) {
133
// if it passes that test, new up the second rule. If the
134
// rule set both rules will belong to is a fraction rule
135
// set, they both have the same base value; otherwise,
136
// increment the original rule's base value ("rule1" actually
137
// goes SECOND in the rule set's rule list)
138
rule2 = new NFRule(rbnf);
139
if (rule1->baseValue >= 0) {
140
rule2->baseValue = rule1->baseValue;
141
if (!ruleSet->isFractionRuleSet()) {
146
// if the description began with "x.x" and contains bracketed
147
// text, it describes both the improper fraction rule and
148
// the proper fraction rule
149
else if (rule1->getType() == kImproperFractionRule) {
150
rule2->setType(kProperFractionRule);
153
// if the description began with "x.0" and contains bracketed
154
// text, it describes both the master rule and the
155
// improper fraction rule
156
else if (rule1->getType() == kMasterRule) {
157
rule2->baseValue = rule1->baseValue;
158
rule1->setType(kImproperFractionRule);
161
// both rules have the same radix and exponent (i.e., the
163
rule2->radix = rule1->radix;
164
rule2->exponent = rule1->exponent;
166
// rule2's rule text omits the stuff in brackets: initalize
167
// its rule text and substitutions accordingly
168
sbuf.append(description, 0, brack1);
169
if (brack2 + 1 < description.length()) {
170
sbuf.append(description, brack2 + 1, description.length() - brack2 - 1);
172
rule2->ruleText.setTo(sbuf);
173
rule2->extractSubstitutions(ruleSet, predecessor, rbnf, status);
176
// rule1's text includes the text in the brackets but omits
177
// the brackets themselves: initialize _its_ rule text and
178
// substitutions accordingly
179
sbuf.setTo(description, 0, brack1);
180
sbuf.append(description, brack1 + 1, brack2 - brack1 - 1);
181
if (brack2 + 1 < description.length()) {
182
sbuf.append(description, brack2 + 1, description.length() - brack2 - 1);
184
rule1->ruleText.setTo(sbuf);
185
rule1->extractSubstitutions(ruleSet, predecessor, rbnf, status);
187
// if we only have one rule, return it; if we have two, return
188
// a two-element array containing them (notice that rule2 goes
189
// BEFORE rule1 in the list: in all cases, rule2 OMITS the
190
// material in the brackets and rule1 INCLUDES the material
200
* This function parses the rule's rule descriptor (i.e., the base
201
* value and/or other tokens that precede the rule's rule text
202
* in the description) and sets the rule's base value, radix, and
203
* exponent according to the descriptor. (If the description doesn't
204
* include a rule descriptor, then this function sets everything to
205
* default values and the rule set sets the rule's real base value).
206
* @param description The rule's description
207
* @return If "description" included a rule descriptor, this is
208
* "description" with the descriptor and any trailing whitespace
209
* stripped off. Otherwise; it's "descriptor" unchangd.
212
NFRule::parseRuleDescriptor(UnicodeString& description, UErrorCode& status)
214
// the description consists of a rule descriptor and a rule body,
215
// separated by a colon. The rule descriptor is optional. If
216
// it's omitted, just set the base value to 0.
217
int32_t p = description.indexOf(gColon);
219
setBaseValue((int32_t)0);
221
// copy the descriptor out into its own string and strip it,
222
// along with any trailing whitespace, out of the original
224
UnicodeString descriptor;
225
descriptor.setTo(description, 0, p);
228
while (p < description.length() && u_isWhitespace(description.charAt(p))) {
231
description.removeBetween(0, p);
233
// check first to see if the rule descriptor matches the token
234
// for one of the special rules. If it does, set the base
235
// value to the correct identfier value
236
if (descriptor == gMinusX) {
237
setType(kNegativeNumberRule);
239
else if (descriptor == gXDotX) {
240
setType(kImproperFractionRule);
242
else if (descriptor == gZeroDotX) {
243
setType(kProperFractionRule);
245
else if (descriptor == gXDotZero) {
246
setType(kMasterRule);
249
// if the rule descriptor begins with a digit, it's a descriptor
251
// since we don't have Long.parseLong, and this isn't much work anyway,
252
// just build up the value as we encounter the digits.
253
else if (descriptor.charAt(0) >= gZero && descriptor.charAt(0) <= gNine) {
258
// begin parsing the descriptor: copy digits
259
// into "tempValue", skip periods, commas, and spaces,
260
// stop on a slash or > sign (or at the end of the string),
261
// and throw an exception on any other character
263
while (p < descriptor.length()) {
264
c = descriptor.charAt(p);
265
if (c >= gZero && c <= gNine) {
266
val = val * ll_10 + (int32_t)(c - gZero);
268
else if (c == gSlash || c == gGreaterThan) {
271
else if (u_isWhitespace(c) || c == gComma || c == gDot) {
274
// throw new IllegalArgumentException("Illegal character in rule descriptor");
275
status = U_PARSE_ERROR;
281
// we have the base value, so set it
284
// if we stopped the previous loop on a slash, we're
285
// now parsing the rule's radix. Again, accumulate digits
286
// in tempValue, skip punctuation, stop on a > mark, and
287
// throw an exception on anything else
292
while (p < descriptor.length()) {
293
c = descriptor.charAt(p);
294
if (c >= gZero && c <= gNine) {
295
val = val * ll_10 + (int32_t)(c - gZero);
297
else if (c == gGreaterThan) {
300
else if (u_isWhitespace(c) || c == gComma || c == gDot) {
303
// throw new IllegalArgumentException("Illegal character is rule descriptor");
304
status = U_PARSE_ERROR;
310
// tempValue now contain's the rule's radix. Set it
311
// accordingly, and recalculate the rule's exponent
312
radix = (int16_t)val;
314
// throw new IllegalArgumentException("Rule can't have radix of 0");
315
status = U_PARSE_ERROR;
318
exponent = expectedExponent();
321
// if we stopped the previous loop on a > sign, then continue
322
// for as long as we still see > signs. For each one,
323
// decrement the exponent (unless the exponent is already 0).
324
// If we see another character before reaching the end of
325
// the descriptor, that's also a syntax error.
326
if (c == gGreaterThan) {
327
while (p < descriptor.length()) {
328
c = descriptor.charAt(p);
329
if (c == gGreaterThan && exponent > 0) {
332
// throw new IllegalArgumentException("Illegal character in rule descriptor");
333
status = U_PARSE_ERROR;
342
// finally, if the rule body begins with an apostrophe, strip it off
343
// (this is generally used to put whitespace at the beginning of
344
// a rule's rule text)
345
if (description.length() > 0 && description.charAt(0) == gTick) {
346
description.removeBetween(0, 1);
349
// return the description with all the stuff we've just waded through
350
// stripped off the front. It now contains just the rule body.
351
// return description;
355
* Searches the rule's rule text for the substitution tokens,
356
* creates the substitutions, and removes the substitution tokens
357
* from the rule's rule text.
358
* @param owner The rule set containing this rule
359
* @param predecessor The rule preseding this one in "owners" rule list
360
* @param ownersOwner The RuleBasedFormat that owns this rule
363
NFRule::extractSubstitutions(const NFRuleSet* ruleSet,
364
const NFRule* predecessor,
365
const RuleBasedNumberFormat* rbnf,
368
if (U_SUCCESS(status)) {
369
sub1 = extractSubstitution(ruleSet, predecessor, rbnf, status);
370
sub2 = extractSubstitution(ruleSet, predecessor, rbnf, status);
375
* Searches the rule's rule text for the first substitution token,
376
* creates a substitution based on it, and removes the token from
377
* the rule's rule text.
378
* @param owner The rule set containing this rule
379
* @param predecessor The rule preceding this one in the rule set's
381
* @param ownersOwner The RuleBasedNumberFormat that owns this rule
382
* @return The newly-created substitution. This is never null; if
383
* the rule text doesn't contain any substitution tokens, this will
384
* be a NullSubstitution.
387
NFRule::extractSubstitution(const NFRuleSet* ruleSet,
388
const NFRule* predecessor,
389
const RuleBasedNumberFormat* rbnf,
392
NFSubstitution* result = NULL;
394
// search the rule's rule text for the first two characters of
395
// a substitution token
396
int32_t subStart = indexOfAny(tokenStrings);
397
int32_t subEnd = subStart;
399
// if we didn't find one, create a null substitution positioned
400
// at the end of the rule text
401
if (subStart == -1) {
402
return NFSubstitution::makeSubstitution(ruleText.length(), this, predecessor,
403
ruleSet, rbnf, gEmptyString, status);
406
// special-case the ">>>" token, since searching for the > at the
407
// end will actually find the > in the middle
408
if (ruleText.indexOf(gGreaterGreaterGreater) == subStart) {
409
subEnd = subStart + 2;
411
// otherwise the substitution token ends with the same character
414
subEnd = ruleText.indexOf(ruleText.charAt(subStart), subStart + 1);
417
// if we don't find the end of the token (i.e., if we're on a single,
418
// unmatched token character), create a null substitution positioned
419
// at the end of the rule
421
return NFSubstitution::makeSubstitution(ruleText.length(), this, predecessor,
422
ruleSet, rbnf, gEmptyString, status);
425
// if we get here, we have a real substitution token (or at least
426
// some text bounded by substitution token characters). Use
427
// makeSubstitution() to create the right kind of substitution
428
UnicodeString subToken;
429
subToken.setTo(ruleText, subStart, subEnd + 1 - subStart);
430
result = NFSubstitution::makeSubstitution(subStart, this, predecessor, ruleSet,
431
rbnf, subToken, status);
433
// remove the substitution from the rule text
434
ruleText.removeBetween(subStart, subEnd+1);
440
* Sets the rule's base value, and causes the radix and exponent
441
* to be recalculated. This is used during construction when we
442
* don't know the rule's base value until after it's been
443
* constructed. It should be used at any other time.
444
* @param The new base value for the rule.
447
NFRule::setBaseValue(int64_t newBaseValue)
449
// set the base value
450
baseValue = newBaseValue;
452
// if this isn't a special rule, recalculate the radix and exponent
453
// (the radix always defaults to 10; if it's supposed to be something
454
// else, it's cleaned up by the caller and the exponent is
455
// recalculated again-- the only function that does this is
456
// NFRule.parseRuleDescriptor() )
457
if (baseValue >= 1) {
459
exponent = expectedExponent();
461
// this function gets called on a fully-constructed rule whose
462
// description didn't specify a base value. This means it
463
// has substitutions, and some substitutions hold on to copies
464
// of the rule's divisor. Fix their copies of the divisor.
466
sub1->setDivisor(radix, exponent);
469
sub2->setDivisor(radix, exponent);
472
// if this is a special rule, its radix and exponent are basically
473
// ignored. Set them to "safe" default values
481
* This calculates the rule's exponent based on its radix and base
482
* value. This will be the highest power the radix can be raised to
483
* and still produce a result less than or equal to the base value.
486
NFRule::expectedExponent() const
488
// since the log of 0, or the log base 0 of something, causes an
489
// error, declare the exponent in these cases to be 0 (we also
490
// deal with the special-rule identifiers here)
491
if (radix == 0 || baseValue < 1) {
495
// we get rounding error in some cases-- for example, log 1000 / log 10
496
// gives us 1.9999999996 instead of 2. The extra logic here is to take
498
int16_t tempResult = (int16_t)(uprv_log((double)baseValue) / uprv_log((double)radix));
499
int64_t temp = util64_pow(radix, tempResult + 1);
500
if (temp <= baseValue) {
507
* Searches the rule's rule text for any of the specified strings.
508
* @param strings An array of strings to search the rule's rule
510
* @return The index of the first match in the rule's rule text
511
* (i.e., the first substring in the rule's rule text that matches
512
* _any_ of the strings in "strings"). If none of the strings in
513
* "strings" is found in the rule's rule text, returns -1.
516
NFRule::indexOfAny(const UChar* const strings[]) const
519
for (int i = 0; strings[i]; i++) {
520
int32_t pos = ruleText.indexOf(*strings[i]);
521
if (pos != -1 && (result == -1 || pos < result)) {
528
//-----------------------------------------------------------------------
530
//-----------------------------------------------------------------------
533
* Tests two rules for equality.
534
* @param that The rule to compare this one against
535
* @return True is the two rules are functionally equivalent
538
NFRule::operator==(const NFRule& rhs) const
540
return baseValue == rhs.baseValue
541
&& radix == rhs.radix
542
&& exponent == rhs.exponent
543
&& ruleText == rhs.ruleText
544
&& *sub1 == *rhs.sub1
545
&& *sub2 == *rhs.sub2;
549
* Returns a textual representation of the rule. This won't
550
* necessarily be the same as the description that this rule
551
* was created with, but it will produce the same result.
552
* @return A textual description of the rule
554
static void util_append64(UnicodeString& result, int64_t n)
557
int32_t len = util64_tou(n, buffer, sizeof(buffer));
558
UnicodeString temp(buffer, len);
563
NFRule::appendRuleText(UnicodeString& result) const
566
case kNegativeNumberRule: result.append(gMinusX); break;
567
case kImproperFractionRule: result.append(gXDotX); break;
568
case kProperFractionRule: result.append(gZeroDotX); break;
569
case kMasterRule: result.append(gXDotZero); break;
571
// for a normal rule, write out its base value, and if the radix is
572
// something other than 10, write out the radix (with the preceding
573
// slash, of course). Then calculate the expected exponent and if
574
// if isn't the same as the actual exponent, write an appropriate
575
// number of > signs. Finally, terminate the whole thing with
577
util_append64(result, baseValue);
579
result.append(gSlash);
580
util_append64(result, radix);
582
int numCarets = expectedExponent() - exponent;
583
for (int i = 0; i < numCarets; i++) {
584
result.append(gGreaterThan);
588
result.append(gColon);
589
result.append(gSpace);
591
// if the rule text begins with a space, write an apostrophe
592
// (whitespace after the rule descriptor is ignored; the
593
// apostrophe is used to make the whitespace significant)
594
if (ruleText.startsWith(gSpace) && sub1->getPos() != 0) {
595
result.append(gTick);
598
// now, write the rule's rule text, inserting appropriate
599
// substitution tokens in the appropriate places
600
UnicodeString ruleTextCopy;
601
ruleTextCopy.setTo(ruleText);
604
sub2->toString(temp);
605
ruleTextCopy.insert(sub2->getPos(), temp);
606
sub1->toString(temp);
607
ruleTextCopy.insert(sub1->getPos(), temp);
609
result.append(ruleTextCopy);
611
// and finally, top the whole thing off with a semicolon and
613
result.append(gSemicolon);
616
//-----------------------------------------------------------------------
618
//-----------------------------------------------------------------------
621
* Formats the number, and inserts the resulting text into
623
* @param number The number being formatted
624
* @param toInsertInto The string where the resultant text should
626
* @param pos The position in toInsertInto where the resultant text
630
NFRule::doFormat(int64_t number, UnicodeString& toInsertInto, int32_t pos) const
632
// first, insert the rule's rule text into toInsertInto at the
633
// specified position, then insert the results of the substitutions
634
// into the right places in toInsertInto (notice we do the
635
// substitutions in reverse order so that the offsets don't get
637
toInsertInto.insert(pos, ruleText);
638
sub2->doSubstitution(number, toInsertInto, pos);
639
sub1->doSubstitution(number, toInsertInto, pos);
643
* Formats the number, and inserts the resulting text into
645
* @param number The number being formatted
646
* @param toInsertInto The string where the resultant text should
648
* @param pos The position in toInsertInto where the resultant text
652
NFRule::doFormat(double number, UnicodeString& toInsertInto, int32_t pos) const
654
// first, insert the rule's rule text into toInsertInto at the
655
// specified position, then insert the results of the substitutions
656
// into the right places in toInsertInto
657
// [again, we have two copies of this routine that do the same thing
658
// so that we don't sacrifice precision in a long by casting it
660
toInsertInto.insert(pos, ruleText);
661
sub2->doSubstitution(number, toInsertInto, pos);
662
sub1->doSubstitution(number, toInsertInto, pos);
666
* Used by the owning rule set to determine whether to invoke the
667
* rollback rule (i.e., whether this rule or the one that precedes
668
* it in the rule set's list should be used to format the number)
669
* @param The number being formatted
670
* @return True if the rule set should use the rule that precedes
671
* this one in its list; false if it should use this rule
674
NFRule::shouldRollBack(double number) const
676
// we roll back if the rule contains a modulus substitution,
677
// the number being formatted is an even multiple of the rule's
678
// divisor, and the rule's base value is NOT an even multiple
680
// In other words, if the original description had
681
// 100: << hundred[ >>];
684
// 101: << hundred >>;
685
// internally. But when we're formatting 200, if we use the rule
686
// at 101, which would normally apply, we get "two hundred zero".
687
// To prevent this, we roll back and use the rule at 100 instead.
688
// This is the logic that makes this happen: the rule at 101 has
689
// a modulus substitution, its base value isn't an even multiple
690
// of 100, and the value we're trying to format _is_ an even
691
// multiple of 100. This is called the "rollback rule."
692
if ((sub1->isModulusSubstitution()) || (sub2->isModulusSubstitution())) {
693
int64_t re = util64_pow(radix, exponent);
694
return uprv_fmod(number, (double)re) == 0 && (baseValue % re) != 0;
699
//-----------------------------------------------------------------------
701
//-----------------------------------------------------------------------
704
* Attempts to parse the string with this rule.
705
* @param text The string being parsed
706
* @param parsePosition On entry, the value is ignored and assumed to
707
* be 0. On exit, this has been updated with the position of the first
708
* character not consumed by matching the text against this rule
709
* (if this rule doesn't match the text at all, the parse position
710
* if left unchanged (presumably at 0) and the function returns
712
* @param isFractionRule True if this rule is contained within a
713
* fraction rule set. This is only used if the rule has no
715
* @return If this rule matched the text, this is the rule's base value
716
* combined appropriately with the results of parsing the substitutions.
717
* If nothing matched, this is new Long(0) and the parse position is
718
* left unchanged. The result will be an instance of Long if the
719
* result is an integer and Double otherwise. The result is never null.
724
static void dumpUS(FILE* f, const UnicodeString& us) {
725
int len = us.length();
726
char* buf = new char[len+1];
727
us.extract(0, len, buf);
729
fprintf(f, "%s", buf);
735
NFRule::doParse(const UnicodeString& text,
736
ParsePosition& parsePosition,
737
UBool isFractionRule,
739
Formattable& resVal) const
741
// internally we operate on a copy of the string being parsed
742
// (because we're going to change it) and use our own ParsePosition
744
UnicodeString workText(text);
746
// check to see whether the text before the first substitution
747
// matches the text at the beginning of the string being
748
// parsed. If it does, strip that off the front of workText;
749
// otherwise, dump out with a mismatch
750
UnicodeString prefix;
751
prefix.setTo(ruleText, 0, sub1->getPos());
754
fprintf(stderr, "doParse %x ", this);
761
fprintf(stderr, " text: '", this);
762
dumpUS(stderr, text);
763
fprintf(stderr, "' prefix: '");
764
dumpUS(stderr, prefix);
766
stripPrefix(workText, prefix, pp);
767
int32_t prefixLength = text.length() - workText.length();
770
fprintf(stderr, "' pl: %d ppi: %d s1p: %d\n", prefixLength, pp.getIndex(), sub1->getPos());
773
if (pp.getIndex() == 0 && sub1->getPos() != 0) {
774
// commented out because ParsePosition doesn't have error index in 1.1.x
775
// restored for ICU4C port
776
parsePosition.setErrorIndex(pp.getErrorIndex());
781
// this is the fun part. The basic guts of the rule-matching
782
// logic is matchToDelimiter(), which is called twice. The first
783
// time it searches the input string for the rule text BETWEEN
784
// the substitutions and tries to match the intervening text
785
// in the input string with the first substitution. If that
786
// succeeds, it then calls it again, this time to look for the
787
// rule text after the second substitution and to match the
788
// intervening input text against the second substitution.
790
// For example, say we have a rule that looks like this:
791
// first << middle >> last;
792
// and input text that looks like this:
793
// first one middle two last
794
// First we use stripPrefix() to match "first " in both places and
795
// strip it off the front, leaving
796
// one middle two last
797
// Then we use matchToDelimiter() to match " middle " and try to
798
// match "one" against a substitution. If it's successful, we now
801
// We use matchToDelimiter() a second time to match " last" and
802
// try to match "two" against a substitution. If "two" matches
803
// the substitution, we have a successful parse.
805
// Since it's possible in many cases to find multiple instances
806
// of each of these pieces of rule text in the input string,
807
// we need to try all the possible combinations of these
808
// locations. This prevents us from prematurely declaring a mismatch,
809
// and makes sure we match as much input text as we can.
810
int highWaterMark = 0;
813
double tempBaseValue = (double)(baseValue <= 0 ? 0 : baseValue);
817
// our partial parse result starts out as this rule's base
818
// value. If it finds a successful match, matchToDelimiter()
819
// will compose this in some way with what it gets back from
820
// the substitution, giving us a new partial parse result
823
temp.setTo(ruleText, sub1->getPos(), sub2->getPos() - sub1->getPos());
824
double partialResult = matchToDelimiter(workText, start, tempBaseValue,
828
// if we got a successful match (or were trying to match a
829
// null substitution), pp is now pointing at the first unmatched
830
// character. Take note of that, and try matchToDelimiter()
831
// on the input text again
832
if (pp.getIndex() != 0 || sub1->isNullSubstitution()) {
833
start = pp.getIndex();
835
UnicodeString workText2;
836
workText2.setTo(workText, pp.getIndex(), workText.length() - pp.getIndex());
839
// the second matchToDelimiter() will compose our previous
840
// partial result with whatever it gets back from its
841
// substitution if there's a successful match, giving us
843
temp.setTo(ruleText, sub2->getPos(), ruleText.length() - sub2->getPos());
844
partialResult = matchToDelimiter(workText2, 0, partialResult,
848
// if we got a successful match on this second
849
// matchToDelimiter() call, update the high-water mark
850
// and result (if necessary)
851
if (pp2.getIndex() != 0 || sub2->isNullSubstitution()) {
852
if (prefixLength + pp.getIndex() + pp2.getIndex() > highWaterMark) {
853
highWaterMark = prefixLength + pp.getIndex() + pp2.getIndex();
854
result = partialResult;
857
// commented out because ParsePosition doesn't have error index in 1.1.x
858
// restored for ICU4C port
860
int32_t temp = pp2.getErrorIndex() + sub1->getPos() + pp.getIndex();
861
if (temp> parsePosition.getErrorIndex()) {
862
parsePosition.setErrorIndex(temp);
866
// commented out because ParsePosition doesn't have error index in 1.1.x
867
// restored for ICU4C port
869
int32_t temp = sub1->getPos() + pp.getErrorIndex();
870
if (temp > parsePosition.getErrorIndex()) {
871
parsePosition.setErrorIndex(temp);
874
// keep trying to match things until the outer matchToDelimiter()
875
// call fails to make a match (each time, it picks up where it
876
// left off the previous time)
877
} while (sub1->getPos() != sub2->getPos()
879
&& pp.getIndex() < workText.length()
880
&& pp.getIndex() != start);
882
// update the caller's ParsePosition with our high-water mark
883
// (i.e., it now points at the first character this function
884
// didn't match-- the ParsePosition is therefore unchanged if
885
// we didn't match anything)
886
parsePosition.setIndex(highWaterMark);
887
// commented out because ParsePosition doesn't have error index in 1.1.x
888
// restored for ICU4C port
889
if (highWaterMark > 0) {
890
parsePosition.setErrorIndex(0);
893
// this is a hack for one unusual condition: Normally, whether this
894
// rule belong to a fraction rule set or not is handled by its
895
// substitutions. But if that rule HAS NO substitutions, then
896
// we have to account for it here. By definition, if the matching
897
// rule in a fraction rule set has no substitutions, its numerator
898
// is 1, and so the result is the reciprocal of its base value.
899
if (isFractionRule &&
901
sub1->isNullSubstitution()) {
905
resVal.setDouble(result);
906
return TRUE; // ??? do we need to worry if it is a long or a double?
910
* This function is used by parse() to match the text being parsed
911
* against a possible prefix string. This function
912
* matches characters from the beginning of the string being parsed
913
* to characters from the prospective prefix. If they match, pp is
914
* updated to the first character not matched, and the result is
915
* the unparsed part of the string. If they don't match, the whole
916
* string is returned, and pp is left unchanged.
917
* @param text The string being parsed
918
* @param prefix The text to match against
919
* @param pp On entry, ignored and assumed to be 0. On exit, points
920
* to the first unmatched character (assuming the whole prefix matched),
921
* or is unchanged (if the whole prefix didn't match).
922
* @return If things match, this is the unparsed part of "text";
923
* if they didn't match, this is "text".
926
NFRule::stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const
928
// if the prefix text is empty, dump out without doing anything
929
if (prefix.length() != 0) {
930
// use prefixLength() to match the beginning of
931
// "text" against "prefix". This function returns the
932
// number of characters from "text" that matched (or 0 if
933
// we didn't match the whole prefix)
934
int32_t pfl = prefixLength(text, prefix);
936
// if we got a successful match, update the parse position
937
// and strip the prefix off of "text"
938
pp.setIndex(pp.getIndex() + pfl);
945
* Used by parse() to match a substitution and any following text.
946
* "text" is searched for instances of "delimiter". For each instance
947
* of delimiter, the intervening text is tested to see whether it
948
* matches the substitution. The longest match wins.
949
* @param text The string being parsed
950
* @param startPos The position in "text" where we should start looking
952
* @param baseValue A partial parse result (often the rule's base value),
953
* which is combined with the result from matching the substitution
954
* @param delimiter The string to search "text" for.
955
* @param pp Ignored and presumed to be 0 on entry. If there's a match,
956
* on exit this will point to the first unmatched character.
957
* @param sub If we find "delimiter" in "text", this substitution is used
958
* to match the text between the beginning of the string and the
959
* position of "delimiter." (If "delimiter" is the empty string, then
960
* this function just matches against this substitution and updates
961
* everything accordingly.)
962
* @param upperBound When matching the substitution, it will only
963
* consider rules with base values lower than this value.
964
* @return If there's a match, this is the result of composing
965
* baseValue with the result of matching the substitution. Otherwise,
966
* this is new Long(0). It's never null. If the result is an integer,
967
* this will be an instance of Long; otherwise, it's an instance of
970
* !!! note {dlf} in point of fact, in the java code the caller always converts
971
* the result to a double, so we might as well return one.
974
NFRule::matchToDelimiter(const UnicodeString& text,
977
const UnicodeString& delimiter,
979
const NFSubstitution* sub,
980
double upperBound) const
982
// if "delimiter" contains real (i.e., non-ignorable) text, search
983
// it for "delimiter" beginning at "start". If that succeeds, then
984
// use "sub"'s doParse() method to match the text before the
985
// instance of "delimiter" we just found.
986
if (!allIgnorable(delimiter)) {
987
ParsePosition tempPP;
990
// use findText() to search for "delimiter". It returns a two-
991
// element array: element 0 is the position of the match, and
992
// element 1 is the number of characters that matched
995
int32_t dPos = findText(text, delimiter, startPos, &dLen);
997
// if findText() succeeded, isolate the text preceding the
998
// match, and use "sub" to match that text
1000
UnicodeString subText;
1001
subText.setTo(text, 0, dPos);
1002
if (subText.length() > 0) {
1003
UBool success = sub->doParse(subText, tempPP, _baseValue, upperBound,
1004
formatter->isLenient(), result);
1006
// if the substitution could match all the text up to
1007
// where we found "delimiter", then this function has
1008
// a successful match. Bump the caller's parse position
1009
// to point to the first character after the text
1010
// that matches "delimiter", and return the result
1011
// we got from parsing the substitution.
1012
if (success && tempPP.getIndex() == dPos) {
1013
pp.setIndex(dPos + dLen);
1014
return result.getDouble();
1016
// commented out because ParsePosition doesn't have error index in 1.1.x
1017
// restored for ICU4C port
1019
if (tempPP.getErrorIndex() > 0) {
1020
pp.setErrorIndex(tempPP.getErrorIndex());
1022
pp.setErrorIndex(tempPP.getIndex());
1027
// if we didn't match the substitution, search for another
1028
// copy of "delimiter" in "text" and repeat the loop if
1031
dPos = findText(text, delimiter, dPos + dLen, &dLen);
1033
// if we make it here, this was an unsuccessful match, and we
1034
// leave pp unchanged and return 0
1038
// if "delimiter" is empty, or consists only of ignorable characters
1039
// (i.e., is semantically empty), thwe we obviously can't search
1040
// for "delimiter". Instead, just use "sub" to parse as much of
1041
// "text" as possible.
1043
ParsePosition tempPP;
1046
// try to match the whole string against the substitution
1047
UBool success = sub->doParse(text, tempPP, _baseValue, upperBound,
1048
formatter->isLenient(), result);
1049
if (success && (tempPP.getIndex() != 0 || sub->isNullSubstitution())) {
1050
// if there's a successful match (or it's a null
1051
// substitution), update pp to point to the first
1052
// character we didn't match, and pass the result from
1053
// sub.doParse() on through to the caller
1054
pp.setIndex(tempPP.getIndex());
1055
return result.getDouble();
1057
// commented out because ParsePosition doesn't have error index in 1.1.x
1058
// restored for ICU4C port
1060
pp.setErrorIndex(tempPP.getErrorIndex());
1063
// and if we get to here, then nothing matched, so we return
1064
// 0 and leave pp alone
1070
* Used by stripPrefix() to match characters. If lenient parse mode
1071
* is off, this just calls startsWith(). If lenient parse mode is on,
1072
* this function uses CollationElementIterators to match characters in
1073
* the strings (only primary-order differences are significant in
1074
* determining whether there's a match).
1075
* @param str The string being tested
1076
* @param prefix The text we're hoping to see at the beginning
1078
* @return If "prefix" is found at the beginning of "str", this
1079
* is the number of characters in "str" that were matched (this
1080
* isn't necessarily the same as the length of "prefix" when matching
1081
* text with a collator). If there's no match, this is 0.
1084
NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix) const
1086
// if we're looking for an empty prefix, it obviously matches
1087
// zero characters. Just go ahead and return 0.
1088
if (prefix.length() == 0) {
1092
// go through all this grief if we're in lenient-parse mode
1093
if (formatter->isLenient()) {
1094
// get the formatter's collator and use it to create two
1095
// collation element iterators, one over the target string
1096
// and another over the prefix (right now, we'll throw an
1097
// exception if the collator we get back from the formatter
1098
// isn't a RuleBasedCollator, because RuleBasedCollator defines
1099
// the CollationElementIterator protocol. Hopefully, this
1100
// will change someday.)
1101
RuleBasedCollator* collator = (RuleBasedCollator*)formatter->getCollator();
1102
CollationElementIterator* strIter = collator->createCollationElementIterator(str);
1103
CollationElementIterator* prefixIter = collator->createCollationElementIterator(prefix);
1105
UErrorCode err = U_ZERO_ERROR;
1107
// The original code was problematic. Consider this match:
1108
// prefix = "fifty-"
1109
// string = " fifty-7"
1110
// The intent is to match string up to the '7', by matching 'fifty-' at position 1
1111
// in the string. Unfortunately, we were getting a match, and then computing where
1112
// the match terminated by rematching the string. The rematch code was using as an
1113
// initial guess the substring of string between 0 and prefix.length. Because of
1114
// the leading space and trailing hyphen (both ignorable) this was succeeding, leaving
1115
// the position before the hyphen in the string. Recursing down, we then parsed the
1116
// remaining string '-7' as numeric. The resulting number turned out as 43 (50 - 7).
1117
// This was not pretty, especially since the string "fifty-7" parsed just fine.
1119
// We have newer APIs now, so we can use calls on the iterator to determine what we
1120
// matched up to. If we terminate because we hit the last element in the string,
1121
// our match terminates at this length. If we terminate because we hit the last element
1122
// in the target, our match terminates at one before the element iterator position.
1124
// match collation elements between the strings
1125
int32_t oStr = strIter->next(err);
1126
int32_t oPrefix = prefixIter->next(err);
1128
while (oPrefix != CollationElementIterator::NULLORDER) {
1129
// skip over ignorable characters in the target string
1130
while (CollationElementIterator::primaryOrder(oStr) == 0
1131
&& oStr != CollationElementIterator::NULLORDER) {
1132
oStr = strIter->next(err);
1135
// skip over ignorable characters in the prefix
1136
while (CollationElementIterator::primaryOrder(oPrefix) == 0
1137
&& oPrefix != CollationElementIterator::NULLORDER) {
1138
oPrefix = prefixIter->next(err);
1141
// dlf: move this above following test, if we consume the
1142
// entire target, aren't we ok even if the source was also
1143
// entirely consumed?
1145
// if skipping over ignorables brought to the end of
1146
// the prefix, we DID match: drop out of the loop
1147
if (oPrefix == CollationElementIterator::NULLORDER) {
1151
// if skipping over ignorables brought us to the end
1152
// of the target string, we didn't match and return 0
1153
if (oStr == CollationElementIterator::NULLORDER) {
1159
// match collation elements from the two strings
1160
// (considering only primary differences). If we
1161
// get a mismatch, dump out and return 0
1162
if (CollationElementIterator::primaryOrder(oStr)
1163
!= CollationElementIterator::primaryOrder(oPrefix)) {
1168
// otherwise, advance to the next character in each string
1169
// and loop (we drop out of the loop when we exhaust
1170
// collation elements in the prefix)
1172
oStr = strIter->next(err);
1173
oPrefix = prefixIter->next(err);
1177
int32_t result = strIter->getOffset();
1178
if (oStr != CollationElementIterator::NULLORDER) {
1179
--result; // back over character that we don't want to consume;
1183
fprintf(stderr, "prefix length: %d\n", result);
1190
//----------------------------------------------------------------
1191
// JDK 1.2-specific API call
1192
// return strIter.getOffset();
1193
//----------------------------------------------------------------
1194
// JDK 1.1 HACK (take out for 1.2-specific code)
1196
// if we make it to here, we have a successful match. Now we
1197
// have to find out HOW MANY characters from the target string
1198
// matched the prefix (there isn't necessarily a one-to-one
1199
// mapping between collation elements and characters).
1200
// In JDK 1.2, there's a simple getOffset() call we can use.
1201
// In JDK 1.1, on the other hand, we have to go through some
1202
// ugly contortions. First, use the collator to compare the
1203
// same number of characters from the prefix and target string.
1204
// If they're equal, we're done.
1205
collator->setStrength(Collator::PRIMARY);
1206
if (str.length() >= prefix.length()) {
1208
temp.setTo(str, 0, prefix.length());
1209
if (collator->equals(temp, prefix)) {
1211
fprintf(stderr, "returning: %d\n", prefix.length());
1213
return prefix.length();
1217
// if they're not equal, then we have to compare successively
1218
// larger and larger substrings of the target string until we
1219
// get to one that matches the prefix. At that point, we know
1220
// how many characters matched the prefix, and we can return.
1222
while (p <= str.length()) {
1224
temp.setTo(str, 0, p);
1225
if (collator->equals(temp, prefix)) {
1232
// SHOULD NEVER GET HERE!!!
1234
//----------------------------------------------------------------
1237
// If lenient parsing is turned off, forget all that crap above.
1238
// Just use String.startsWith() and be done with it.
1240
if (str.startsWith(prefix)) {
1241
return prefix.length();
1249
* Searches a string for another string. If lenient parsing is off,
1250
* this just calls indexOf(). If lenient parsing is on, this function
1251
* uses CollationElementIterator to match characters, and only
1252
* primary-order differences are significant in determining whether
1254
* @param str The string to search
1255
* @param key The string to search "str" for
1256
* @param startingAt The index into "str" where the search is to
1258
* @return A two-element array of ints. Element 0 is the position
1259
* of the match, or -1 if there was no match. Element 1 is the
1260
* number of characters in "str" that matched (which isn't necessarily
1261
* the same as the length of "key")
1264
NFRule::findText(const UnicodeString& str,
1265
const UnicodeString& key,
1267
int32_t* length) const
1269
// if lenient parsing is turned off, this is easy: just call
1270
// String.indexOf() and we're done
1271
if (!formatter->isLenient()) {
1272
*length = key.length();
1273
return str.indexOf(key, startingAt);
1275
// but if lenient parsing is turned ON, we've got some work
1278
//----------------------------------------------------------------
1279
// JDK 1.1 HACK (take out of 1.2-specific code)
1281
// in JDK 1.2, CollationElementIterator provides us with an
1282
// API to map between character offsets and collation elements
1283
// and we can do this by marching through the string comparing
1284
// collation elements. We can't do that in JDK 1.1. Insted,
1285
// we have to go through this horrible slow mess:
1286
int32_t p = startingAt;
1289
// basically just isolate smaller and smaller substrings of
1290
// the target string (each running to the end of the string,
1291
// and with the first one running from startingAt to the end)
1292
// and then use prefixLength() to see if the search key is at
1293
// the beginning of each substring. This is excruciatingly
1294
// slow, but it will locate the key and tell use how long the
1295
// matching text was.
1297
while (p < str.length() && keyLen == 0) {
1298
temp.setTo(str, p, str.length() - p);
1299
keyLen = prefixLength(temp, key);
1306
// if we make it to here, we didn't find it. Return -1 for the
1307
// location. The length should be ignored, but set it to 0,
1308
// which should be "safe"
1312
//----------------------------------------------------------------
1313
// JDK 1.2 version of this routine
1314
//RuleBasedCollator collator = (RuleBasedCollator)formatter.getCollator();
1316
//CollationElementIterator strIter = collator.getCollationElementIterator(str);
1317
//CollationElementIterator keyIter = collator.getCollationElementIterator(key);
1319
//int keyStart = -1;
1321
//str.setOffset(startingAt);
1323
//int oStr = strIter.next();
1324
//int oKey = keyIter.next();
1325
//while (oKey != CollationElementIterator.NULLORDER) {
1326
// while (oStr != CollationElementIterator.NULLORDER &&
1327
// CollationElementIterator.primaryOrder(oStr) == 0)
1328
// oStr = strIter.next();
1330
// while (oKey != CollationElementIterator.NULLORDER &&
1331
// CollationElementIterator.primaryOrder(oKey) == 0)
1332
// oKey = keyIter.next();
1334
// if (oStr == CollationElementIterator.NULLORDER) {
1335
// return new int[] { -1, 0 };
1338
// if (oKey == CollationElementIterator.NULLORDER) {
1342
// if (CollationElementIterator.primaryOrder(oStr) ==
1343
// CollationElementIterator.primaryOrder(oKey)) {
1344
// keyStart = strIter.getOffset();
1345
// oStr = strIter.next();
1346
// oKey = keyIter.next();
1348
// if (keyStart != -1) {
1352
// oStr = strIter.next();
1357
//if (oKey == CollationElementIterator.NULLORDER) {
1358
// return new int[] { keyStart, strIter.getOffset() - keyStart };
1360
// return new int[] { -1, 0 };
1366
* Checks to see whether a string consists entirely of ignorable
1368
* @param str The string to test.
1369
* @return true if the string is empty of consists entirely of
1370
* characters that the number formatter's collator says are
1371
* ignorable at the primary-order level. false otherwise.
1374
NFRule::allIgnorable(const UnicodeString& str) const
1376
// if the string is empty, we can just return true
1377
if (str.length() == 0) {
1381
// if lenient parsing is turned on, walk through the string with
1382
// a collation element iterator and make sure each collation
1383
// element is 0 (ignorable) at the primary level
1384
if (formatter->isLenient()) {
1385
RuleBasedCollator* collator = (RuleBasedCollator*)(formatter->getCollator());
1386
CollationElementIterator* iter = collator->createCollationElementIterator(str);
1388
UErrorCode err = U_ZERO_ERROR;
1389
int32_t o = iter->next(err);
1390
while (o != CollationElementIterator::NULLORDER
1391
&& CollationElementIterator::primaryOrder(o) == 0) {
1392
o = iter->next(err);
1396
return o == CollationElementIterator::NULLORDER;
1398
// if lenient parsing is turned off, there is no such thing as
1399
// an ignorable character: return true only if the string is empty