~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/i18n/nfrule.cpp

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
******************************************************************************
3
 
*   Copyright (C) 1997-2001, International Business Machines
4
 
*   Corporation and others.  All Rights Reserved.
5
 
******************************************************************************
6
 
*   file name:  nfrule.cpp
7
 
*   encoding:   US-ASCII
8
 
*   tab size:   8 (not used)
9
 
*   indentation:4
10
 
*
11
 
* Modification history
12
 
* Date        Name      Comments
13
 
* 10/11/2001  Doug      Ported from ICU4J
14
 
*/
15
 
 
16
 
#include "nfrule.h"
17
 
 
18
 
#if U_HAVE_RBNF
19
 
 
20
 
#include "unicode/rbnf.h"
21
 
#include "unicode/tblcoll.h"
22
 
#include "unicode/coleitr.h"
23
 
#include "unicode/uchar.h"
24
 
#include "nfrs.h"
25
 
#include "nfrlist.h"
26
 
#include "nfsubs.h"
27
 
 
28
 
U_NAMESPACE_BEGIN
29
 
 
30
 
extern const UChar* CSleftBracket;
31
 
extern const UChar* CSrightBracket;
32
 
 
33
 
NFRule::NFRule(const RuleBasedNumberFormat* _rbnf)
34
 
  : baseValue((int32_t)0)
35
 
  , radix(0)
36
 
  , exponent(0)
37
 
  , ruleText()
38
 
  , sub1(NULL)
39
 
  , sub2(NULL)
40
 
  , formatter(_rbnf)
41
 
{
42
 
}
43
 
 
44
 
NFRule::~NFRule()
45
 
{
46
 
  delete sub1;
47
 
  delete sub2;
48
 
}
49
 
 
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;
63
 
 
64
 
static const UChar gMinusX[] =                  {0x2D, 0x78, 0};    /* "-x" */
65
 
static const UChar gXDotX[] =                   {0x78, 0x2E, 0x78, 0}; /* "x.x" */
66
 
static const UChar gXDotZero[] =                {0x78, 0x2E, 0x30, 0}; /* "x.0" */
67
 
static const UChar gZeroDotX[] =                {0x30, 0x2E, 0x78, 0}; /* "0.x" */
68
 
 
69
 
static const UChar gLessLess[] =                {0x3C, 0x3C, 0};    /* "<<" */
70
 
static const UChar gLessPercent[] =             {0x3C, 0x25, 0};    /* "<%" */
71
 
static const UChar gLessHash[] =                {0x3C, 0x23, 0};    /* "<#" */
72
 
static const UChar gLessZero[] =                {0x3C, 0x30, 0};    /* "<0" */
73
 
static const UChar gGreaterGreater[] =          {0x3E, 0x3E, 0};    /* ">>" */
74
 
static const UChar gGreaterPercent[] =          {0x3E, 0x25, 0};    /* ">%" */
75
 
static const UChar gGreaterHash[] =             {0x3E, 0x23, 0};    /* ">#" */
76
 
static const UChar gGreaterZero[] =             {0x3E, 0x30, 0};    /* ">0" */
77
 
static const UChar gEqualPercent[] =            {0x3D, 0x25, 0};    /* "=%" */
78
 
static const UChar gEqualHash[] =               {0x3D, 0x23, 0};    /* "=#" */
79
 
static const UChar gEqualZero[] =               {0x3D, 0x30, 0};    /* "=0" */
80
 
static const UChar gEmptyString[] =             {0};                /* "" */
81
 
static const UChar gGreaterGreaterGreater[] =   {0x3E, 0x3E, 0x3E, 0}; /* ">>>" */
82
 
 
83
 
static const UChar * const tokenStrings[] = {
84
 
    gLessLess, gLessPercent, gLessHash, gLessZero,
85
 
    gGreaterGreater, gGreaterPercent,gGreaterHash, gGreaterZero,
86
 
    gEqualPercent, gEqualHash, gEqualZero, NULL
87
 
};
88
 
 
89
 
void
90
 
NFRule::makeRules(UnicodeString& description,
91
 
                  const NFRuleSet *ruleSet,
92
 
                  const NFRule *predecessor,
93
 
                  const RuleBasedNumberFormat *rbnf,
94
 
                  NFRuleList& rules,
95
 
                  UErrorCode& status)
96
 
{
97
 
    // we know we're making at least one rule, so go ahead and
98
 
    // new it up and initialize its basevalue and divisor
99
 
    // (this also strips the rule descriptor, if any, off the
100
 
    // descripton string)
101
 
    NFRule* rule1 = new NFRule(rbnf);
102
 
    rule1->parseRuleDescriptor(description, status);
103
 
 
104
 
    // check the description to see whether there's text enclosed
105
 
    // in brackets
106
 
    int32_t brack1 = description.indexOf(gLeftBracket);
107
 
    int32_t brack2 = description.indexOf(gRightBracket);
108
 
 
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);
118
 
        rules.add(rule1);
119
 
    } else {
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;
123
 
        UnicodeString sbuf;
124
 
 
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) {
132
 
 
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()) {
142
 
                    ++rule1->baseValue;
143
 
                }
144
 
            }
145
 
 
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);
151
 
            }
152
 
 
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);
159
 
            }
160
 
 
161
 
            // both rules have the same radix and exponent (i.e., the
162
 
            // same divisor)
163
 
            rule2->radix = rule1->radix;
164
 
            rule2->exponent = rule1->exponent;
165
 
 
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);
171
 
            }
172
 
            rule2->ruleText.setTo(sbuf);
173
 
            rule2->extractSubstitutions(ruleSet, predecessor, rbnf, status);
174
 
        }
175
 
 
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);
183
 
        }
184
 
        rule1->ruleText.setTo(sbuf);
185
 
        rule1->extractSubstitutions(ruleSet, predecessor, rbnf, status);
186
 
 
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
191
 
        // in the brackets)
192
 
        if (rule2 != NULL) {
193
 
            rules.add(rule2);
194
 
        }
195
 
        rules.add(rule1);
196
 
    }
197
 
}
198
 
 
199
 
/**
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.
210
 
 */
211
 
void
212
 
NFRule::parseRuleDescriptor(UnicodeString& description, UErrorCode& status)
213
 
{
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);
218
 
    if (p == -1) {
219
 
        setBaseValue((int32_t)0);
220
 
    } else {
221
 
        // copy the descriptor out into its own string and strip it,
222
 
        // along with any trailing whitespace, out of the original
223
 
        // description
224
 
        UnicodeString descriptor;
225
 
        descriptor.setTo(description, 0, p);
226
 
 
227
 
        ++p;
228
 
        while (p < description.length() && u_isWhitespace(description.charAt(p))) {
229
 
            ++p;
230
 
        }
231
 
        description.removeBetween(0, p);
232
 
 
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);
238
 
        }
239
 
        else if (descriptor == gXDotX) {
240
 
            setType(kImproperFractionRule);
241
 
        }
242
 
        else if (descriptor == gZeroDotX) {
243
 
            setType(kProperFractionRule);
244
 
        }
245
 
        else if (descriptor == gXDotZero) {
246
 
            setType(kMasterRule);
247
 
        }
248
 
 
249
 
        // if the rule descriptor begins with a digit, it's a descriptor
250
 
        // for a normal rule
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) {
254
 
            int64_t val = 0;
255
 
            p = 0;
256
 
            UChar c = gSpace;
257
 
 
258
 
            // begin parsing the descriptor: copy digits
259
 
            // into "tempValue", skip periods, commas, and spaces,
260
 
            // stop on a slash or > sign (or at the end of the string),
261
 
            // and throw an exception on any other character
262
 
            int64_t ll_10 = 10;
263
 
            while (p < descriptor.length()) {
264
 
                c = descriptor.charAt(p);
265
 
                if (c >= gZero && c <= gNine) {
266
 
                    val = val * ll_10 + (int32_t)(c - gZero);
267
 
                }
268
 
                else if (c == gSlash || c == gGreaterThan) {
269
 
                    break;
270
 
                }
271
 
                else if (u_isWhitespace(c) || c == gComma || c == gDot) {
272
 
                }
273
 
                else {
274
 
                    // throw new IllegalArgumentException("Illegal character in rule descriptor");
275
 
                    status = U_PARSE_ERROR;
276
 
                    return;
277
 
                }
278
 
                ++p;
279
 
            }
280
 
 
281
 
            // we have the base value, so set it
282
 
            setBaseValue(val);
283
 
 
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
288
 
            if (c == gSlash) {
289
 
                val = 0;
290
 
                ++p;
291
 
                int64_t ll_10 = 10;
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);
296
 
                    }
297
 
                    else if (c == gGreaterThan) {
298
 
                        break;
299
 
                    }
300
 
                    else if (u_isWhitespace(c) || c == gComma || c == gDot) {
301
 
                    }
302
 
                    else {
303
 
                        // throw new IllegalArgumentException("Illegal character is rule descriptor");
304
 
                        status = U_PARSE_ERROR;
305
 
                        return;
306
 
                    }
307
 
                    ++p;
308
 
                }
309
 
 
310
 
                // tempValue now contain's the rule's radix.  Set it
311
 
                // accordingly, and recalculate the rule's exponent
312
 
                radix = (int16_t)val;
313
 
                if (radix == 0) {
314
 
                    // throw new IllegalArgumentException("Rule can't have radix of 0");
315
 
                    status = U_PARSE_ERROR;
316
 
                }
317
 
 
318
 
                exponent = expectedExponent();
319
 
            }
320
 
 
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) {
330
 
                        --exponent;
331
 
                    } else {
332
 
                        // throw new IllegalArgumentException("Illegal character in rule descriptor");
333
 
                        status = U_PARSE_ERROR;
334
 
                        return;
335
 
                    }
336
 
                    ++p;
337
 
                }
338
 
            }
339
 
        }
340
 
    }
341
 
 
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);
347
 
    }
348
 
 
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;
352
 
}
353
 
 
354
 
/**
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
361
 
*/
362
 
void
363
 
NFRule::extractSubstitutions(const NFRuleSet* ruleSet,
364
 
                             const NFRule* predecessor,
365
 
                             const RuleBasedNumberFormat* rbnf,
366
 
                             UErrorCode& status)
367
 
{
368
 
    if (U_SUCCESS(status)) {
369
 
        sub1 = extractSubstitution(ruleSet, predecessor, rbnf, status);
370
 
        sub2 = extractSubstitution(ruleSet, predecessor, rbnf, status);
371
 
    }
372
 
}
373
 
 
374
 
/**
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
380
 
* rule list
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.
385
 
*/
386
 
NFSubstitution *
387
 
NFRule::extractSubstitution(const NFRuleSet* ruleSet,
388
 
                            const NFRule* predecessor,
389
 
                            const RuleBasedNumberFormat* rbnf,
390
 
                            UErrorCode& status)
391
 
{
392
 
    NFSubstitution* result = NULL;
393
 
 
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;
398
 
 
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);
404
 
    }
405
 
 
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;
410
 
 
411
 
        // otherwise the substitution token ends with the same character
412
 
        // it began with
413
 
    } else {
414
 
        subEnd = ruleText.indexOf(ruleText.charAt(subStart), subStart + 1);
415
 
    }
416
 
 
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
420
 
    if (subEnd == -1) {
421
 
        return NFSubstitution::makeSubstitution(ruleText.length(), this, predecessor,
422
 
            ruleSet, rbnf, gEmptyString, status);
423
 
    }
424
 
 
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);
432
 
 
433
 
    // remove the substitution from the rule text
434
 
    ruleText.removeBetween(subStart, subEnd+1);
435
 
 
436
 
    return result;
437
 
}
438
 
 
439
 
/**
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.
445
 
 */
446
 
void
447
 
NFRule::setBaseValue(int64_t newBaseValue)
448
 
{
449
 
    // set the base value
450
 
    baseValue = newBaseValue;
451
 
 
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) {
458
 
        radix = 10;
459
 
        exponent = expectedExponent();
460
 
 
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.
465
 
        if (sub1 != NULL) {
466
 
            sub1->setDivisor(radix, exponent);
467
 
        }
468
 
        if (sub2 != NULL) {
469
 
            sub2->setDivisor(radix, exponent);
470
 
        }
471
 
 
472
 
        // if this is a special rule, its radix and exponent are basically
473
 
        // ignored.  Set them to "safe" default values
474
 
    } else {
475
 
        radix = 10;
476
 
        exponent = 0;
477
 
    }
478
 
}
479
 
 
480
 
/**
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.
484
 
*/
485
 
int16_t
486
 
NFRule::expectedExponent() const
487
 
{
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) {
492
 
        return 0;
493
 
    }
494
 
 
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
497
 
    // that into account
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) {
501
 
        tempResult += 1;
502
 
    }
503
 
    return tempResult;
504
 
}
505
 
 
506
 
/**
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
509
 
 * text for
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.
514
 
 */
515
 
int32_t
516
 
NFRule::indexOfAny(const UChar* const strings[]) const
517
 
{
518
 
    int result = -1;
519
 
    for (int i = 0; strings[i]; i++) {
520
 
        int32_t pos = ruleText.indexOf(*strings[i]);
521
 
        if (pos != -1 && (result == -1 || pos < result)) {
522
 
            result = pos;
523
 
        }
524
 
    }
525
 
    return result;
526
 
}
527
 
 
528
 
//-----------------------------------------------------------------------
529
 
// boilerplate
530
 
//-----------------------------------------------------------------------
531
 
 
532
 
/**
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
536
 
*/
537
 
UBool
538
 
NFRule::operator==(const NFRule& rhs) const
539
 
{
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;
546
 
}
547
 
 
548
 
/**
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
553
 
*/
554
 
static void util_append64(UnicodeString& result, int64_t n)
555
 
{
556
 
    UChar buffer[256];
557
 
    int32_t len = util64_tou(n, buffer, sizeof(buffer));
558
 
    UnicodeString temp(buffer, len);
559
 
    result.append(temp);
560
 
}
561
 
 
562
 
void
563
 
NFRule::appendRuleText(UnicodeString& result) const
564
 
{
565
 
    switch (getType()) {
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;
570
 
    default:
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
576
 
        // a colon.
577
 
        util_append64(result, baseValue);
578
 
        if (radix != 10) {
579
 
            result.append(gSlash);
580
 
            util_append64(result, radix);
581
 
        }
582
 
        int numCarets = expectedExponent() - exponent;
583
 
        for (int i = 0; i < numCarets; i++) {
584
 
            result.append(gGreaterThan);
585
 
        }
586
 
        break;
587
 
    }
588
 
    result.append(gColon);
589
 
    result.append(gSpace);
590
 
 
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);
596
 
    }
597
 
 
598
 
    // now, write the rule's rule text, inserting appropriate
599
 
    // substitution tokens in the appropriate places
600
 
    UnicodeString ruleTextCopy;
601
 
    ruleTextCopy.setTo(ruleText);
602
 
 
603
 
    UnicodeString temp;
604
 
    sub2->toString(temp);
605
 
    ruleTextCopy.insert(sub2->getPos(), temp);
606
 
    sub1->toString(temp);
607
 
    ruleTextCopy.insert(sub1->getPos(), temp);
608
 
 
609
 
    result.append(ruleTextCopy);
610
 
 
611
 
    // and finally, top the whole thing off with a semicolon and
612
 
    // return the result
613
 
    result.append(gSemicolon);
614
 
}
615
 
 
616
 
//-----------------------------------------------------------------------
617
 
// formatting
618
 
//-----------------------------------------------------------------------
619
 
 
620
 
/**
621
 
* Formats the number, and inserts the resulting text into
622
 
* toInsertInto.
623
 
* @param number The number being formatted
624
 
* @param toInsertInto The string where the resultant text should
625
 
* be inserted
626
 
* @param pos The position in toInsertInto where the resultant text
627
 
* should be inserted
628
 
*/
629
 
void
630
 
NFRule::doFormat(int64_t number, UnicodeString& toInsertInto, int32_t pos) const
631
 
{
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
636
 
    // messed up)
637
 
    toInsertInto.insert(pos, ruleText);
638
 
    sub2->doSubstitution(number, toInsertInto, pos);
639
 
    sub1->doSubstitution(number, toInsertInto, pos);
640
 
}
641
 
 
642
 
/**
643
 
* Formats the number, and inserts the resulting text into
644
 
* toInsertInto.
645
 
* @param number The number being formatted
646
 
* @param toInsertInto The string where the resultant text should
647
 
* be inserted
648
 
* @param pos The position in toInsertInto where the resultant text
649
 
* should be inserted
650
 
*/
651
 
void
652
 
NFRule::doFormat(double number, UnicodeString& toInsertInto, int32_t pos) const
653
 
{
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
659
 
    // to a double]
660
 
    toInsertInto.insert(pos, ruleText);
661
 
    sub2->doSubstitution(number, toInsertInto, pos);
662
 
    sub1->doSubstitution(number, toInsertInto, pos);
663
 
}
664
 
 
665
 
/**
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
672
 
*/
673
 
UBool
674
 
NFRule::shouldRollBack(double number) const
675
 
{
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
679
 
    // of its divisor
680
 
    // In other words, if the original description had
681
 
    //    100: << hundred[ >>];
682
 
    // that expands into
683
 
    //    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;
695
 
    }
696
 
    return FALSE;
697
 
}
698
 
 
699
 
//-----------------------------------------------------------------------
700
 
// parsing
701
 
//-----------------------------------------------------------------------
702
 
 
703
 
/**
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
711
 
* new Long(0)).
712
 
* @param isFractionRule True if this rule is contained within a
713
 
* fraction rule set.  This is only used if the rule has no
714
 
* substitutions.
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.
720
 
*/
721
 
#ifdef RBNF_DEBUG
722
 
#include <stdio.h>
723
 
 
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);
728
 
  buf[len] = 0;
729
 
  fprintf(f, "%s", buf);
730
 
  delete[] buf;
731
 
}
732
 
#endif
733
 
 
734
 
UBool
735
 
NFRule::doParse(const UnicodeString& text,
736
 
                ParsePosition& parsePosition,
737
 
                UBool isFractionRule,
738
 
                double upperBound,
739
 
                Formattable& resVal) const
740
 
{
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
743
 
    ParsePosition pp;
744
 
    UnicodeString workText(text);
745
 
 
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());
752
 
 
753
 
#ifdef RBNF_DEBUG
754
 
    fprintf(stderr, "doParse %x ", this);
755
 
    {
756
 
        UnicodeString rt;
757
 
        appendRuleText(rt);
758
 
        dumpUS(stderr, rt);
759
 
    }
760
 
 
761
 
    fprintf(stderr, " text: '", this);
762
 
    dumpUS(stderr, text);
763
 
    fprintf(stderr, "' prefix: '");
764
 
    dumpUS(stderr, prefix);
765
 
#endif
766
 
    stripPrefix(workText, prefix, pp);
767
 
    int32_t prefixLength = text.length() - workText.length();
768
 
 
769
 
#ifdef RBNF_DEBUG
770
 
    fprintf(stderr, "' pl: %d ppi: %d s1p: %d\n", prefixLength, pp.getIndex(), sub1->getPos());
771
 
#endif
772
 
 
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());
777
 
        resVal.setLong(0);
778
 
        return TRUE;
779
 
    }
780
 
 
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.
789
 
    //
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
799
 
    // have
800
 
    //    two last
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.
804
 
    //
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;
811
 
    double result = 0;
812
 
    int start = 0;
813
 
    double tempBaseValue = (double)(baseValue <= 0 ? 0 : baseValue);
814
 
 
815
 
    UnicodeString temp;
816
 
    do {
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
821
 
        pp.setIndex(0);
822
 
 
823
 
        temp.setTo(ruleText, sub1->getPos(), sub2->getPos() - sub1->getPos());
824
 
        double partialResult = matchToDelimiter(workText, start, tempBaseValue,
825
 
            temp, pp, sub1,
826
 
            upperBound);
827
 
 
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();
834
 
 
835
 
            UnicodeString workText2;
836
 
            workText2.setTo(workText, pp.getIndex(), workText.length() - pp.getIndex());
837
 
            ParsePosition pp2;
838
 
 
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
842
 
            // a real result
843
 
            temp.setTo(ruleText, sub2->getPos(), ruleText.length() - sub2->getPos());
844
 
            partialResult = matchToDelimiter(workText2, 0, partialResult,
845
 
                temp, pp2, sub2,
846
 
                upperBound);
847
 
 
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;
855
 
                }
856
 
            }
857
 
            // commented out because ParsePosition doesn't have error index in 1.1.x
858
 
            // restored for ICU4C port
859
 
            else {
860
 
                int32_t temp = pp2.getErrorIndex() + sub1->getPos() + pp.getIndex();
861
 
                if (temp> parsePosition.getErrorIndex()) {
862
 
                    parsePosition.setErrorIndex(temp);
863
 
                }
864
 
            }
865
 
        }
866
 
        // commented out because ParsePosition doesn't have error index in 1.1.x
867
 
        // restored for ICU4C port
868
 
        else {
869
 
            int32_t temp = sub1->getPos() + pp.getErrorIndex();
870
 
            if (temp > parsePosition.getErrorIndex()) {
871
 
                parsePosition.setErrorIndex(temp);
872
 
            }
873
 
        }
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()
878
 
        && pp.getIndex() > 0
879
 
        && pp.getIndex() < workText.length()
880
 
        && pp.getIndex() != start);
881
 
 
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);
891
 
    }
892
 
 
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 &&
900
 
        highWaterMark > 0 &&
901
 
        sub1->isNullSubstitution()) {
902
 
        result = 1 / result;
903
 
    }
904
 
 
905
 
    resVal.setDouble(result);
906
 
    return TRUE; // ??? do we need to worry if it is a long or a double?
907
 
}
908
 
 
909
 
/**
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".
924
 
*/
925
 
void
926
 
NFRule::stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const
927
 
{
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);
935
 
        if (pfl != 0) {
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);
939
 
            text.remove(0, pfl);
940
 
        }
941
 
    }
942
 
}
943
 
 
944
 
/**
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
951
 
* for "delimiter".
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
968
 
* Double.
969
 
*
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.
972
 
*/
973
 
double
974
 
NFRule::matchToDelimiter(const UnicodeString& text,
975
 
                         int32_t startPos,
976
 
                         double _baseValue,
977
 
                         const UnicodeString& delimiter,
978
 
                         ParsePosition& pp,
979
 
                         const NFSubstitution* sub,
980
 
                         double upperBound) const
981
 
{
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;
988
 
        Formattable result;
989
 
 
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
993
 
        // "delimiter".
994
 
        int32_t dLen;
995
 
        int32_t dPos = findText(text, delimiter, startPos, &dLen);
996
 
 
997
 
        // if findText() succeeded, isolate the text preceding the
998
 
        // match, and use "sub" to match that text
999
 
        while (dPos >= 0) {
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);
1005
 
 
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();
1015
 
                }
1016
 
                // commented out because ParsePosition doesn't have error index in 1.1.x
1017
 
                // restored for ICU4C port
1018
 
                else {
1019
 
                    if (tempPP.getErrorIndex() > 0) {
1020
 
                        pp.setErrorIndex(tempPP.getErrorIndex());
1021
 
                    } else {
1022
 
                        pp.setErrorIndex(tempPP.getIndex());
1023
 
                    }
1024
 
                }
1025
 
            }
1026
 
 
1027
 
            // if we didn't match the substitution, search for another
1028
 
            // copy of "delimiter" in "text" and repeat the loop if
1029
 
            // we find it
1030
 
            tempPP.setIndex(0);
1031
 
            dPos = findText(text, delimiter, dPos + dLen, &dLen);
1032
 
        }
1033
 
        // if we make it here, this was an unsuccessful match, and we
1034
 
        // leave pp unchanged and return 0
1035
 
        pp.setIndex(0);
1036
 
        return 0;
1037
 
 
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.
1042
 
    } else {
1043
 
        ParsePosition tempPP;
1044
 
        Formattable result;
1045
 
 
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();
1056
 
        }
1057
 
        // commented out because ParsePosition doesn't have error index in 1.1.x
1058
 
        // restored for ICU4C port
1059
 
        else {
1060
 
            pp.setErrorIndex(tempPP.getErrorIndex());
1061
 
        }
1062
 
 
1063
 
        // and if we get to here, then nothing matched, so we return
1064
 
        // 0 and leave pp alone
1065
 
        return 0;
1066
 
    }
1067
 
}
1068
 
 
1069
 
/**
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
1077
 
* of "str"
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.
1082
 
*/
1083
 
int32_t
1084
 
NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix) const
1085
 
{
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) {
1089
 
        return 0;
1090
 
    }
1091
 
 
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);
1104
 
 
1105
 
        UErrorCode err = U_ZERO_ERROR;
1106
 
 
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.
1118
 
        //
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.
1123
 
 
1124
 
        // match collation elements between the strings
1125
 
        int32_t oStr = strIter->next(err);
1126
 
        int32_t oPrefix = prefixIter->next(err);
1127
 
 
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);
1133
 
            }
1134
 
 
1135
 
            // skip over ignorable characters in the prefix
1136
 
            while (CollationElementIterator::primaryOrder(oPrefix) == 0
1137
 
                && oPrefix != CollationElementIterator::NULLORDER) {
1138
 
                oPrefix = prefixIter->next(err);
1139
 
            }
1140
 
 
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?
1144
 
 
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) {
1148
 
                break;
1149
 
            }
1150
 
 
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) {
1154
 
                delete prefixIter;
1155
 
                delete strIter;
1156
 
                return 0;
1157
 
            }
1158
 
 
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)) {
1164
 
                delete prefixIter;
1165
 
                delete strIter;
1166
 
                return 0;
1167
 
 
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)
1171
 
            } else {
1172
 
                oStr = strIter->next(err);
1173
 
                oPrefix = prefixIter->next(err);
1174
 
            }
1175
 
        }
1176
 
 
1177
 
        int32_t result = strIter->getOffset();
1178
 
        if (oStr != CollationElementIterator::NULLORDER) {
1179
 
            --result; // back over character that we don't want to consume;
1180
 
        }
1181
 
 
1182
 
#ifdef RBNF_DEBUG
1183
 
        fprintf(stderr, "prefix length: %d\n", result);
1184
 
#endif
1185
 
        delete prefixIter;
1186
 
        delete strIter;
1187
 
 
1188
 
        return result;
1189
 
#if 0
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)
1195
 
 
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()) {
1207
 
            UnicodeString temp;
1208
 
            temp.setTo(str, 0, prefix.length());
1209
 
            if (collator->equals(temp, prefix)) {
1210
 
#ifdef RBNF_DEBUG
1211
 
                fprintf(stderr, "returning: %d\n", prefix.length());
1212
 
#endif
1213
 
                return prefix.length();
1214
 
            }
1215
 
        }
1216
 
 
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.
1221
 
        int32_t p = 1;
1222
 
        while (p <= str.length()) {
1223
 
            UnicodeString temp;
1224
 
            temp.setTo(str, 0, p);
1225
 
            if (collator->equals(temp, prefix)) {
1226
 
                return p;
1227
 
            } else {
1228
 
                ++p;
1229
 
            }
1230
 
        }
1231
 
 
1232
 
        // SHOULD NEVER GET HERE!!!
1233
 
        return 0;
1234
 
        //----------------------------------------------------------------
1235
 
#endif
1236
 
 
1237
 
        // If lenient parsing is turned off, forget all that crap above.
1238
 
        // Just use String.startsWith() and be done with it.
1239
 
  } else {
1240
 
      if (str.startsWith(prefix)) {
1241
 
          return prefix.length();
1242
 
      } else {
1243
 
          return 0;
1244
 
      }
1245
 
  }
1246
 
}
1247
 
 
1248
 
/**
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
1253
 
* there's a match.
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
1257
 
* begin
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")
1262
 
*/
1263
 
int32_t
1264
 
NFRule::findText(const UnicodeString& str,
1265
 
                 const UnicodeString& key,
1266
 
                 int32_t startingAt,
1267
 
                 int32_t* length) const
1268
 
{
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);
1274
 
 
1275
 
        // but if lenient parsing is turned ON, we've got some work
1276
 
        // ahead of us
1277
 
    } else {
1278
 
        //----------------------------------------------------------------
1279
 
        // JDK 1.1 HACK (take out of 1.2-specific code)
1280
 
 
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;
1287
 
        int32_t keyLen = 0;
1288
 
 
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.
1296
 
        UnicodeString temp;
1297
 
        while (p < str.length() && keyLen == 0) {
1298
 
            temp.setTo(str, p, str.length() - p);
1299
 
            keyLen = prefixLength(temp, key);
1300
 
            if (keyLen != 0) {
1301
 
                *length = keyLen;
1302
 
                return p;
1303
 
            }
1304
 
            ++p;
1305
 
        }
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"
1309
 
        *length = 0;
1310
 
        return -1;
1311
 
 
1312
 
        //----------------------------------------------------------------
1313
 
        // JDK 1.2 version of this routine
1314
 
        //RuleBasedCollator collator = (RuleBasedCollator)formatter.getCollator();
1315
 
        //
1316
 
        //CollationElementIterator strIter = collator.getCollationElementIterator(str);
1317
 
        //CollationElementIterator keyIter = collator.getCollationElementIterator(key);
1318
 
        //
1319
 
        //int keyStart = -1;
1320
 
        //
1321
 
        //str.setOffset(startingAt);
1322
 
        //
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();
1329
 
        //
1330
 
        //    while (oKey != CollationElementIterator.NULLORDER &&
1331
 
        //                CollationElementIterator.primaryOrder(oKey) == 0)
1332
 
        //        oKey = keyIter.next();
1333
 
        //
1334
 
        //    if (oStr == CollationElementIterator.NULLORDER) {
1335
 
        //        return new int[] { -1, 0 };
1336
 
        //    }
1337
 
        //
1338
 
        //    if (oKey == CollationElementIterator.NULLORDER) {
1339
 
        //        break;
1340
 
        //    }
1341
 
        //
1342
 
        //    if (CollationElementIterator.primaryOrder(oStr) ==
1343
 
        //            CollationElementIterator.primaryOrder(oKey)) {
1344
 
        //        keyStart = strIter.getOffset();
1345
 
        //        oStr = strIter.next();
1346
 
        //        oKey = keyIter.next();
1347
 
        //    } else {
1348
 
        //        if (keyStart != -1) {
1349
 
        //            keyStart = -1;
1350
 
        //            keyIter.reset();
1351
 
        //        } else {
1352
 
        //            oStr = strIter.next();
1353
 
        //        }
1354
 
        //    }
1355
 
        //}
1356
 
        //
1357
 
        //if (oKey == CollationElementIterator.NULLORDER) {
1358
 
        //    return new int[] { keyStart, strIter.getOffset() - keyStart };
1359
 
        //} else {
1360
 
        //    return new int[] { -1, 0 };
1361
 
        //}
1362
 
    }
1363
 
}
1364
 
 
1365
 
/**
1366
 
* Checks to see whether a string consists entirely of ignorable
1367
 
* characters.
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.
1372
 
*/
1373
 
UBool
1374
 
NFRule::allIgnorable(const UnicodeString& str) const
1375
 
{
1376
 
    // if the string is empty, we can just return true
1377
 
    if (str.length() == 0) {
1378
 
        return TRUE;
1379
 
    }
1380
 
 
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);
1387
 
 
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);
1393
 
        }
1394
 
 
1395
 
        delete iter;
1396
 
        return o == CollationElementIterator::NULLORDER;
1397
 
    }
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
1400
 
    return FALSE;
1401
 
}
1402
 
 
1403
 
U_NAMESPACE_END
1404
 
 
1405
 
/* U_HAVE_RBNF */
1406
 
#endif
1407
 
 
1408