Coverage Report

Created: 2026-02-05 06:34

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