Coverage Report

Created: 2023-02-22 06:51

/src/icu/source/i18n/numparse_affixes.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2018 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
4
#include "unicode/utypes.h"
5
6
#if !UCONFIG_NO_FORMATTING
7
8
// Allow implicit conversion from char16_t* to UnicodeString for this file:
9
// Helpful in toString methods and elsewhere.
10
#define UNISTR_FROM_STRING_EXPLICIT
11
12
#include "numparse_types.h"
13
#include "numparse_affixes.h"
14
#include "numparse_utils.h"
15
#include "number_utils.h"
16
#include "string_segment.h"
17
18
using namespace icu;
19
using namespace icu::numparse;
20
using namespace icu::numparse::impl;
21
using namespace icu::number;
22
using namespace icu::number::impl;
23
24
25
namespace {
26
27
/**
28
 * Helper method to return whether the given AffixPatternMatcher equals the given pattern string.
29
 * Either both arguments must be null or the pattern string inside the AffixPatternMatcher must equal
30
 * the given pattern string.
31
 */
32
0
static bool matched(const AffixPatternMatcher* affix, const UnicodeString& patternString) {
33
0
    return (affix == nullptr && patternString.isBogus()) ||
34
0
           (affix != nullptr && affix->getPattern() == patternString);
35
0
}
36
37
/**
38
 * Helper method to return the length of the given AffixPatternMatcher. Returns 0 for null.
39
 */
40
0
static int32_t length(const AffixPatternMatcher* matcher) {
41
0
    return matcher == nullptr ? 0 : matcher->getPattern().length();
42
0
}
43
44
/**
45
 * Helper method to return whether (1) both lhs and rhs are null/invalid, or (2) if they are both
46
 * valid, whether they are equal according to operator==.  Similar to Java Objects.equals()
47
 */
48
0
static bool equals(const AffixPatternMatcher* lhs, const AffixPatternMatcher* rhs) {
49
0
    if (lhs == nullptr && rhs == nullptr) {
50
0
        return true;
51
0
    }
52
0
    if (lhs == nullptr || rhs == nullptr) {
53
0
        return false;
54
0
    }
55
0
    return *lhs == *rhs;
56
0
}
57
58
}
59
60
61
AffixPatternMatcherBuilder::AffixPatternMatcherBuilder(const UnicodeString& pattern,
62
                                                       AffixTokenMatcherWarehouse& warehouse,
63
                                                       IgnorablesMatcher* ignorables)
64
        : fMatchersLen(0),
65
          fLastTypeOrCp(0),
66
          fPattern(pattern),
67
          fWarehouse(warehouse),
68
0
          fIgnorables(ignorables) {}
69
70
0
void AffixPatternMatcherBuilder::consumeToken(AffixPatternType type, UChar32 cp, UErrorCode& status) {
71
    // This is called by AffixUtils.iterateWithConsumer() for each token.
72
73
    // Add an ignorables matcher between tokens except between two literals, and don't put two
74
    // ignorables matchers in a row.
75
0
    if (fIgnorables != nullptr && fMatchersLen > 0 &&
76
0
        (fLastTypeOrCp < 0 || !fIgnorables->getSet()->contains(fLastTypeOrCp))) {
77
0
        addMatcher(*fIgnorables);
78
0
    }
79
80
0
    if (type != TYPE_CODEPOINT) {
81
        // Case 1: the token is a symbol.
82
0
        switch (type) {
83
0
            case TYPE_MINUS_SIGN:
84
0
                addMatcher(fWarehouse.minusSign());
85
0
                break;
86
0
            case TYPE_PLUS_SIGN:
87
0
                addMatcher(fWarehouse.plusSign());
88
0
                break;
89
0
            case TYPE_PERCENT:
90
0
                addMatcher(fWarehouse.percent());
91
0
                break;
92
0
            case TYPE_PERMILLE:
93
0
                addMatcher(fWarehouse.permille());
94
0
                break;
95
0
            case TYPE_CURRENCY_SINGLE:
96
0
            case TYPE_CURRENCY_DOUBLE:
97
0
            case TYPE_CURRENCY_TRIPLE:
98
0
            case TYPE_CURRENCY_QUAD:
99
0
            case TYPE_CURRENCY_QUINT:
100
                // All currency symbols use the same matcher
101
0
                addMatcher(fWarehouse.currency(status));
102
0
                break;
103
0
            default:
104
0
                UPRV_UNREACHABLE;
105
0
        }
106
107
0
    } else if (fIgnorables != nullptr && fIgnorables->getSet()->contains(cp)) {
108
        // Case 2: the token is an ignorable literal.
109
        // No action necessary: the ignorables matcher has already been added.
110
111
0
    } else {
112
        // Case 3: the token is a non-ignorable literal.
113
0
        if (auto* ptr = fWarehouse.nextCodePointMatcher(cp, status)) {
114
0
            addMatcher(*ptr);
115
0
        } else {
116
            // OOM; unwind the stack
117
0
            return;
118
0
        }
119
0
    }
120
0
    fLastTypeOrCp = type != TYPE_CODEPOINT ? type : cp;
121
0
}
122
123
0
void AffixPatternMatcherBuilder::addMatcher(NumberParseMatcher& matcher) {
124
0
    if (fMatchersLen >= fMatchers.getCapacity()) {
125
0
        fMatchers.resize(fMatchersLen * 2, fMatchersLen);
126
0
    }
127
0
    fMatchers[fMatchersLen++] = &matcher;
128
0
}
129
130
0
AffixPatternMatcher AffixPatternMatcherBuilder::build(UErrorCode& status) {
131
0
    return AffixPatternMatcher(fMatchers, fMatchersLen, fPattern, status);
132
0
}
133
134
AffixTokenMatcherWarehouse::AffixTokenMatcherWarehouse(const AffixTokenMatcherSetupData* setupData)
135
0
        : fSetupData(setupData) {}
136
137
0
NumberParseMatcher& AffixTokenMatcherWarehouse::minusSign() {
138
0
    return fMinusSign = {fSetupData->dfs, true};
139
0
}
140
141
0
NumberParseMatcher& AffixTokenMatcherWarehouse::plusSign() {
142
0
    return fPlusSign = {fSetupData->dfs, true};
143
0
}
144
145
0
NumberParseMatcher& AffixTokenMatcherWarehouse::percent() {
146
0
    return fPercent = {fSetupData->dfs};
147
0
}
148
149
0
NumberParseMatcher& AffixTokenMatcherWarehouse::permille() {
150
0
    return fPermille = {fSetupData->dfs};
151
0
}
152
153
0
NumberParseMatcher& AffixTokenMatcherWarehouse::currency(UErrorCode& status) {
154
0
    return fCurrency = {fSetupData->currencySymbols, fSetupData->dfs, fSetupData->parseFlags, status};
155
0
}
156
157
0
IgnorablesMatcher& AffixTokenMatcherWarehouse::ignorables() {
158
0
    return fSetupData->ignorables;
159
0
}
160
161
0
NumberParseMatcher* AffixTokenMatcherWarehouse::nextCodePointMatcher(UChar32 cp, UErrorCode& status) {
162
0
    if (U_FAILURE(status)) {
163
0
        return nullptr;
164
0
    }
165
0
    auto* result = fCodePoints.create(cp);
166
0
    if (result == nullptr) {
167
0
        status = U_MEMORY_ALLOCATION_ERROR;
168
0
    }
169
0
    return result;
170
0
}
171
172
173
CodePointMatcher::CodePointMatcher(UChar32 cp)
174
0
        : fCp(cp) {}
175
176
0
bool CodePointMatcher::match(StringSegment& segment, ParsedNumber& result, UErrorCode&) const {
177
0
    if (segment.startsWith(fCp)) {
178
0
        segment.adjustOffsetByCodePoint();
179
0
        result.setCharsConsumed(segment);
180
0
    }
181
0
    return false;
182
0
}
183
184
0
bool CodePointMatcher::smokeTest(const StringSegment& segment) const {
185
0
    return segment.startsWith(fCp);
186
0
}
187
188
0
UnicodeString CodePointMatcher::toString() const {
189
0
    return u"<CodePoint>";
190
0
}
191
192
193
AffixPatternMatcher AffixPatternMatcher::fromAffixPattern(const UnicodeString& affixPattern,
194
                                                          AffixTokenMatcherWarehouse& tokenWarehouse,
195
                                                          parse_flags_t parseFlags, bool* success,
196
0
                                                          UErrorCode& status) {
197
0
    if (affixPattern.isEmpty()) {
198
0
        *success = false;
199
0
        return {};
200
0
    }
201
0
    *success = true;
202
203
0
    IgnorablesMatcher* ignorables;
204
0
    if (0 != (parseFlags & PARSE_FLAG_EXACT_AFFIX)) {
205
0
        ignorables = nullptr;
206
0
    } else {
207
0
        ignorables = &tokenWarehouse.ignorables();
208
0
    }
209
210
0
    AffixPatternMatcherBuilder builder(affixPattern, tokenWarehouse, ignorables);
211
0
    AffixUtils::iterateWithConsumer(affixPattern, builder, status);
212
0
    return builder.build(status);
213
0
}
214
215
AffixPatternMatcher::AffixPatternMatcher(MatcherArray& matchers, int32_t matchersLen,
216
                                         const UnicodeString& pattern, UErrorCode& status)
217
0
    : ArraySeriesMatcher(matchers, matchersLen), fPattern(pattern, status) {
218
0
}
219
220
0
UnicodeString AffixPatternMatcher::getPattern() const {
221
0
    return fPattern.toAliasedUnicodeString();
222
0
}
223
224
0
bool AffixPatternMatcher::operator==(const AffixPatternMatcher& other) const {
225
0
    return fPattern == other.fPattern;
226
0
}
227
228
229
AffixMatcherWarehouse::AffixMatcherWarehouse(AffixTokenMatcherWarehouse* tokenWarehouse)
230
0
        : fTokenWarehouse(tokenWarehouse) {
231
0
}
232
233
bool AffixMatcherWarehouse::isInteresting(const AffixPatternProvider& patternInfo,
234
                                          const IgnorablesMatcher& ignorables, parse_flags_t parseFlags,
235
0
                                          UErrorCode& status) {
236
0
    UnicodeString posPrefixString = patternInfo.getString(AffixPatternProvider::AFFIX_POS_PREFIX);
237
0
    UnicodeString posSuffixString = patternInfo.getString(AffixPatternProvider::AFFIX_POS_SUFFIX);
238
0
    UnicodeString negPrefixString;
239
0
    UnicodeString negSuffixString;
240
0
    if (patternInfo.hasNegativeSubpattern()) {
241
0
        negPrefixString = patternInfo.getString(AffixPatternProvider::AFFIX_NEG_PREFIX);
242
0
        negSuffixString = patternInfo.getString(AffixPatternProvider::AFFIX_NEG_SUFFIX);
243
0
    }
244
245
0
    if (0 == (parseFlags & PARSE_FLAG_USE_FULL_AFFIXES) &&
246
0
        AffixUtils::containsOnlySymbolsAndIgnorables(posPrefixString, *ignorables.getSet(), status) &&
247
0
        AffixUtils::containsOnlySymbolsAndIgnorables(posSuffixString, *ignorables.getSet(), status) &&
248
0
        AffixUtils::containsOnlySymbolsAndIgnorables(negPrefixString, *ignorables.getSet(), status) &&
249
0
        AffixUtils::containsOnlySymbolsAndIgnorables(negSuffixString, *ignorables.getSet(), status)
250
        // HACK: Plus and minus sign are a special case: we accept them trailing only if they are
251
        // trailing in the pattern string.
252
0
        && !AffixUtils::containsType(posSuffixString, TYPE_PLUS_SIGN, status) &&
253
0
        !AffixUtils::containsType(posSuffixString, TYPE_MINUS_SIGN, status) &&
254
0
        !AffixUtils::containsType(negSuffixString, TYPE_PLUS_SIGN, status) &&
255
0
        !AffixUtils::containsType(negSuffixString, TYPE_MINUS_SIGN, status)) {
256
        // The affixes contain only symbols and ignorables.
257
        // No need to generate affix matchers.
258
0
        return false;
259
0
    }
260
0
    return true;
261
0
}
262
263
void AffixMatcherWarehouse::createAffixMatchers(const AffixPatternProvider& patternInfo,
264
                                                MutableMatcherCollection& output,
265
                                                const IgnorablesMatcher& ignorables,
266
0
                                                parse_flags_t parseFlags, UErrorCode& status) {
267
0
    if (!isInteresting(patternInfo, ignorables, parseFlags, status)) {
268
0
        return;
269
0
    }
270
271
    // The affixes have interesting characters, or we are in strict mode.
272
    // Use initial capacity of 6, the highest possible number of AffixMatchers.
273
0
    UnicodeString sb;
274
0
    bool includeUnpaired = 0 != (parseFlags & PARSE_FLAG_INCLUDE_UNPAIRED_AFFIXES);
275
276
0
    int32_t numAffixMatchers = 0;
277
0
    int32_t numAffixPatternMatchers = 0;
278
279
0
    AffixPatternMatcher* posPrefix = nullptr;
280
0
    AffixPatternMatcher* posSuffix = nullptr;
281
282
    // Pre-process the affix strings to resolve LDML rules like sign display.
283
0
    for (int8_t typeInt = 0; typeInt < PATTERN_SIGN_TYPE_COUNT; typeInt++) {
284
0
        auto type = static_cast<PatternSignType>(typeInt);
285
286
        // Skip affixes in some cases
287
0
        if (type == PATTERN_SIGN_TYPE_POS
288
0
                && 0 != (parseFlags & PARSE_FLAG_PLUS_SIGN_ALLOWED)) {
289
0
            continue;
290
0
        }
291
0
        if (type == PATTERN_SIGN_TYPE_POS_SIGN
292
0
                && 0 == (parseFlags & PARSE_FLAG_PLUS_SIGN_ALLOWED)) {
293
0
            continue;
294
0
        }
295
296
        // Generate Prefix
297
        // TODO: Handle approximately sign?
298
0
        bool hasPrefix = false;
299
0
        PatternStringUtils::patternInfoToStringBuilder(
300
0
                patternInfo, true, type, false, StandardPlural::OTHER, false, sb);
301
0
        fAffixPatternMatchers[numAffixPatternMatchers] = AffixPatternMatcher::fromAffixPattern(
302
0
                sb, *fTokenWarehouse, parseFlags, &hasPrefix, status);
303
0
        AffixPatternMatcher* prefix = hasPrefix ? &fAffixPatternMatchers[numAffixPatternMatchers++]
304
0
                                                : nullptr;
305
306
        // Generate Suffix
307
        // TODO: Handle approximately sign?
308
0
        bool hasSuffix = false;
309
0
        PatternStringUtils::patternInfoToStringBuilder(
310
0
                patternInfo, false, type, false, StandardPlural::OTHER, false, sb);
311
0
        fAffixPatternMatchers[numAffixPatternMatchers] = AffixPatternMatcher::fromAffixPattern(
312
0
                sb, *fTokenWarehouse, parseFlags, &hasSuffix, status);
313
0
        AffixPatternMatcher* suffix = hasSuffix ? &fAffixPatternMatchers[numAffixPatternMatchers++]
314
0
                                                : nullptr;
315
316
0
        if (type == PATTERN_SIGN_TYPE_POS) {
317
0
            posPrefix = prefix;
318
0
            posSuffix = suffix;
319
0
        } else if (equals(prefix, posPrefix) && equals(suffix, posSuffix)) {
320
            // Skip adding these matchers (we already have equivalents)
321
0
            continue;
322
0
        }
323
324
        // Flags for setting in the ParsedNumber; the token matchers may add more.
325
0
        int flags = (type == PATTERN_SIGN_TYPE_NEG) ? FLAG_NEGATIVE : 0;
326
327
        // Note: it is indeed possible for posPrefix and posSuffix to both be null.
328
        // We still need to add that matcher for strict mode to work.
329
0
        fAffixMatchers[numAffixMatchers++] = {prefix, suffix, flags};
330
0
        if (includeUnpaired && prefix != nullptr && suffix != nullptr) {
331
            // The following if statements are designed to prevent adding two identical matchers.
332
0
            if (type == PATTERN_SIGN_TYPE_POS || !equals(prefix, posPrefix)) {
333
0
                fAffixMatchers[numAffixMatchers++] = {prefix, nullptr, flags};
334
0
            }
335
0
            if (type == PATTERN_SIGN_TYPE_POS || !equals(suffix, posSuffix)) {
336
0
                fAffixMatchers[numAffixMatchers++] = {nullptr, suffix, flags};
337
0
            }
338
0
        }
339
0
    }
340
341
    // Put the AffixMatchers in order, and then add them to the output.
342
    // Since there are at most 9 elements, do a simple-to-implement bubble sort.
343
0
    bool madeChanges;
344
0
    do {
345
0
        madeChanges = false;
346
0
        for (int32_t i = 1; i < numAffixMatchers; i++) {
347
0
            if (fAffixMatchers[i - 1].compareTo(fAffixMatchers[i]) > 0) {
348
0
                madeChanges = true;
349
0
                AffixMatcher temp = std::move(fAffixMatchers[i - 1]);
350
0
                fAffixMatchers[i - 1] = std::move(fAffixMatchers[i]);
351
0
                fAffixMatchers[i] = std::move(temp);
352
0
            }
353
0
        }
354
0
    } while (madeChanges);
355
356
0
    for (int32_t i = 0; i < numAffixMatchers; i++) {
357
        // Enable the following line to debug affixes
358
        //std::cout << "Adding affix matcher: " << CStr(fAffixMatchers[i].toString())() << std::endl;
359
0
        output.addMatcher(fAffixMatchers[i]);
360
0
    }
361
0
}
362
363
364
AffixMatcher::AffixMatcher(AffixPatternMatcher* prefix, AffixPatternMatcher* suffix, result_flags_t flags)
365
0
        : fPrefix(prefix), fSuffix(suffix), fFlags(flags) {}
366
367
0
bool AffixMatcher::match(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const {
368
0
    if (!result.seenNumber()) {
369
        // Prefix
370
        // Do not match if:
371
        // 1. We have already seen a prefix (result.prefix != null)
372
        // 2. The prefix in this AffixMatcher is empty (prefix == null)
373
0
        if (!result.prefix.isBogus() || fPrefix == nullptr) {
374
0
            return false;
375
0
        }
376
377
        // Attempt to match the prefix.
378
0
        int initialOffset = segment.getOffset();
379
0
        bool maybeMore = fPrefix->match(segment, result, status);
380
0
        if (initialOffset != segment.getOffset()) {
381
0
            result.prefix = fPrefix->getPattern();
382
0
        }
383
0
        return maybeMore;
384
385
0
    } else {
386
        // Suffix
387
        // Do not match if:
388
        // 1. We have already seen a suffix (result.suffix != null)
389
        // 2. The suffix in this AffixMatcher is empty (suffix == null)
390
        // 3. The matched prefix does not equal this AffixMatcher's prefix
391
0
        if (!result.suffix.isBogus() || fSuffix == nullptr || !matched(fPrefix, result.prefix)) {
392
0
            return false;
393
0
        }
394
395
        // Attempt to match the suffix.
396
0
        int initialOffset = segment.getOffset();
397
0
        bool maybeMore = fSuffix->match(segment, result, status);
398
0
        if (initialOffset != segment.getOffset()) {
399
0
            result.suffix = fSuffix->getPattern();
400
0
        }
401
0
        return maybeMore;
402
0
    }
403
0
}
404
405
0
bool AffixMatcher::smokeTest(const StringSegment& segment) const {
406
0
    return (fPrefix != nullptr && fPrefix->smokeTest(segment)) ||
407
0
           (fSuffix != nullptr && fSuffix->smokeTest(segment));
408
0
}
409
410
0
void AffixMatcher::postProcess(ParsedNumber& result) const {
411
    // Check to see if our affix is the one that was matched. If so, set the flags in the result.
412
0
    if (matched(fPrefix, result.prefix) && matched(fSuffix, result.suffix)) {
413
        // Fill in the result prefix and suffix with non-null values (empty string).
414
        // Used by strict mode to determine whether an entire affix pair was matched.
415
0
        if (result.prefix.isBogus()) {
416
0
            result.prefix = UnicodeString();
417
0
        }
418
0
        if (result.suffix.isBogus()) {
419
0
            result.suffix = UnicodeString();
420
0
        }
421
0
        result.flags |= fFlags;
422
0
        if (fPrefix != nullptr) {
423
0
            fPrefix->postProcess(result);
424
0
        }
425
0
        if (fSuffix != nullptr) {
426
0
            fSuffix->postProcess(result);
427
0
        }
428
0
    }
429
0
}
430
431
0
int8_t AffixMatcher::compareTo(const AffixMatcher& rhs) const {
432
0
    const AffixMatcher& lhs = *this;
433
0
    if (length(lhs.fPrefix) != length(rhs.fPrefix)) {
434
0
        return length(lhs.fPrefix) > length(rhs.fPrefix) ? -1 : 1;
435
0
    } else if (length(lhs.fSuffix) != length(rhs.fSuffix)) {
436
0
        return length(lhs.fSuffix) > length(rhs.fSuffix) ? -1 : 1;
437
0
    } else {
438
0
        return 0;
439
0
    }
440
0
}
441
442
0
UnicodeString AffixMatcher::toString() const {
443
0
    bool isNegative = 0 != (fFlags & FLAG_NEGATIVE);
444
0
    return UnicodeString(u"<Affix") + (isNegative ? u":negative " : u" ") +
445
0
           (fPrefix ? fPrefix->getPattern() : u"null") + u"#" +
446
0
           (fSuffix ? fSuffix->getPattern() : u"null") + u">";
447
448
0
}
449
450
451
#endif /* #if !UCONFIG_NO_FORMATTING */