Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/i18n/numparse_impl.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 <typeinfo>
13
#include <array>
14
#include "number_types.h"
15
#include "number_patternstring.h"
16
#include "numparse_types.h"
17
#include "numparse_impl.h"
18
#include "numparse_symbols.h"
19
#include "numparse_decimal.h"
20
#include "unicode/numberformatter.h"
21
#include "cstr.h"
22
#include "number_mapper.h"
23
#include "static_unicode_sets.h"
24
25
using namespace icu;
26
using namespace icu::number;
27
using namespace icu::number::impl;
28
using namespace icu::numparse;
29
using namespace icu::numparse::impl;
30
31
32
0
NumberParseMatcher::~NumberParseMatcher() = default;
33
34
35
NumberParserImpl*
36
NumberParserImpl::createSimpleParser(const Locale& locale, const UnicodeString& patternString,
37
0
                                     parse_flags_t parseFlags, UErrorCode& status) {
38
0
39
0
    LocalPointer<NumberParserImpl> parser(new NumberParserImpl(parseFlags));
40
0
    DecimalFormatSymbols symbols(locale, status);
41
0
42
0
    parser->fLocalMatchers.ignorables = {unisets::DEFAULT_IGNORABLES};
43
0
    IgnorablesMatcher& ignorables = parser->fLocalMatchers.ignorables;
44
0
45
0
    DecimalFormatSymbols dfs(locale, status);
46
0
    dfs.setSymbol(DecimalFormatSymbols::kCurrencySymbol, u"IU$");
47
0
    dfs.setSymbol(DecimalFormatSymbols::kIntlCurrencySymbol, u"ICU");
48
0
    CurrencySymbols currencySymbols({u"ICU", status}, locale, dfs, status);
49
0
50
0
    ParsedPatternInfo patternInfo;
51
0
    PatternParser::parseToPatternInfo(patternString, patternInfo, status);
52
0
53
0
    // The following statements set up the affix matchers.
54
0
    AffixTokenMatcherSetupData affixSetupData = {
55
0
            currencySymbols, symbols, ignorables, locale, parseFlags};
56
0
    parser->fLocalMatchers.affixTokenMatcherWarehouse = {&affixSetupData};
57
0
    parser->fLocalMatchers.affixMatcherWarehouse = {&parser->fLocalMatchers.affixTokenMatcherWarehouse};
58
0
    parser->fLocalMatchers.affixMatcherWarehouse.createAffixMatchers(
59
0
            patternInfo, *parser, ignorables, parseFlags, status);
60
0
61
0
    Grouper grouper = Grouper::forStrategy(UNUM_GROUPING_AUTO);
62
0
    grouper.setLocaleData(patternInfo, locale);
63
0
64
0
    parser->addMatcher(parser->fLocalMatchers.ignorables);
65
0
    parser->addMatcher(parser->fLocalMatchers.decimal = {symbols, grouper, parseFlags});
66
0
    parser->addMatcher(parser->fLocalMatchers.minusSign = {symbols, false});
67
0
    parser->addMatcher(parser->fLocalMatchers.plusSign = {symbols, false});
68
0
    parser->addMatcher(parser->fLocalMatchers.percent = {symbols});
69
0
    parser->addMatcher(parser->fLocalMatchers.permille = {symbols});
70
0
    parser->addMatcher(parser->fLocalMatchers.nan = {symbols});
71
0
    parser->addMatcher(parser->fLocalMatchers.infinity = {symbols});
72
0
    parser->addMatcher(parser->fLocalMatchers.padding = {u"@"});
73
0
    parser->addMatcher(parser->fLocalMatchers.scientific = {symbols, grouper});
74
0
    parser->addMatcher(parser->fLocalMatchers.currency = {currencySymbols, symbols, parseFlags, status});
75
0
//    parser.addMatcher(new RequireNumberMatcher());
76
0
77
0
    parser->freeze();
78
0
    return parser.orphan();
79
0
}
80
81
NumberParserImpl*
82
NumberParserImpl::createParserFromProperties(const number::impl::DecimalFormatProperties& properties,
83
                                             const DecimalFormatSymbols& symbols, bool parseCurrency,
84
0
                                             UErrorCode& status) {
85
0
    Locale locale = symbols.getLocale();
86
0
    PropertiesAffixPatternProvider localPAPP;
87
0
    CurrencyPluralInfoAffixProvider localCPIAP;
88
0
    AffixPatternProvider* affixProvider;
89
0
    if (properties.currencyPluralInfo.fPtr.isNull()) {
90
0
        localPAPP.setTo(properties, status);
91
0
        affixProvider = &localPAPP;
92
0
    } else {
93
0
        localCPIAP.setTo(*properties.currencyPluralInfo.fPtr, properties, status);
94
0
        affixProvider = &localCPIAP;
95
0
    }
96
0
    if (affixProvider == nullptr || U_FAILURE(status)) { return nullptr; }
97
0
    CurrencyUnit currency = resolveCurrency(properties, locale, status);
98
0
    CurrencySymbols currencySymbols(currency, locale, symbols, status);
99
0
    bool isStrict = properties.parseMode.getOrDefault(PARSE_MODE_STRICT) == PARSE_MODE_STRICT;
100
0
    Grouper grouper = Grouper::forProperties(properties);
101
0
    int parseFlags = 0;
102
0
    if (affixProvider == nullptr || U_FAILURE(status)) { return nullptr; }
103
0
    if (!properties.parseCaseSensitive) {
104
0
        parseFlags |= PARSE_FLAG_IGNORE_CASE;
105
0
    }
106
0
    if (properties.parseIntegerOnly) {
107
0
        parseFlags |= PARSE_FLAG_INTEGER_ONLY;
108
0
    }
109
0
    if (properties.signAlwaysShown) {
110
0
        parseFlags |= PARSE_FLAG_PLUS_SIGN_ALLOWED;
111
0
    }
112
0
    if (isStrict) {
113
0
        parseFlags |= PARSE_FLAG_STRICT_GROUPING_SIZE;
114
0
        parseFlags |= PARSE_FLAG_STRICT_SEPARATORS;
115
0
        parseFlags |= PARSE_FLAG_USE_FULL_AFFIXES;
116
0
        parseFlags |= PARSE_FLAG_EXACT_AFFIX;
117
0
    } else {
118
0
        parseFlags |= PARSE_FLAG_INCLUDE_UNPAIRED_AFFIXES;
119
0
    }
120
0
    if (grouper.getPrimary() <= 0) {
121
0
        parseFlags |= PARSE_FLAG_GROUPING_DISABLED;
122
0
    }
123
0
    if (parseCurrency || affixProvider->hasCurrencySign()) {
124
0
        parseFlags |= PARSE_FLAG_MONETARY_SEPARATORS;
125
0
    }
126
0
    if (!parseCurrency) {
127
0
        parseFlags |= PARSE_FLAG_NO_FOREIGN_CURRENCY;
128
0
    }
129
0
130
0
    LocalPointer<NumberParserImpl> parser(new NumberParserImpl(parseFlags));
131
0
132
0
    parser->fLocalMatchers.ignorables = {
133
0
            isStrict ? unisets::STRICT_IGNORABLES : unisets::DEFAULT_IGNORABLES};
134
0
    IgnorablesMatcher& ignorables = parser->fLocalMatchers.ignorables;
135
0
136
0
    //////////////////////
137
0
    /// AFFIX MATCHERS ///
138
0
    //////////////////////
139
0
140
0
    // The following statements set up the affix matchers.
141
0
    AffixTokenMatcherSetupData affixSetupData = {
142
0
            currencySymbols, symbols, ignorables, locale, parseFlags};
143
0
    parser->fLocalMatchers.affixTokenMatcherWarehouse = {&affixSetupData};
144
0
    parser->fLocalMatchers.affixMatcherWarehouse = {&parser->fLocalMatchers.affixTokenMatcherWarehouse};
145
0
    parser->fLocalMatchers.affixMatcherWarehouse.createAffixMatchers(
146
0
            *affixProvider, *parser, ignorables, parseFlags, status);
147
0
148
0
    ////////////////////////
149
0
    /// CURRENCY MATCHER ///
150
0
    ////////////////////////
151
0
152
0
    if (parseCurrency || affixProvider->hasCurrencySign()) {
153
0
        parser->addMatcher(parser->fLocalMatchers.currency = {currencySymbols, symbols, parseFlags, status});
154
0
    }
155
0
156
0
    ///////////////
157
0
    /// PERCENT ///
158
0
    ///////////////
159
0
160
0
    // ICU-TC meeting, April 11, 2018: accept percent/permille only if it is in the pattern,
161
0
    // and to maintain regressive behavior, divide by 100 even if no percent sign is present.
162
0
    if (affixProvider->containsSymbolType(AffixPatternType::TYPE_PERCENT, status)) {
163
0
        parser->addMatcher(parser->fLocalMatchers.percent = {symbols});
164
0
    }
165
0
    if (affixProvider->containsSymbolType(AffixPatternType::TYPE_PERMILLE, status)) {
166
0
        parser->addMatcher(parser->fLocalMatchers.permille = {symbols});
167
0
    }
168
0
169
0
    ///////////////////////////////
170
0
    /// OTHER STANDARD MATCHERS ///
171
0
    ///////////////////////////////
172
0
173
0
    if (!isStrict) {
174
0
        parser->addMatcher(parser->fLocalMatchers.plusSign = {symbols, false});
175
0
        parser->addMatcher(parser->fLocalMatchers.minusSign = {symbols, false});
176
0
    }
177
0
    parser->addMatcher(parser->fLocalMatchers.nan = {symbols});
178
0
    parser->addMatcher(parser->fLocalMatchers.infinity = {symbols});
179
0
    UnicodeString padString = properties.padString;
180
0
    if (!padString.isBogus() && !ignorables.getSet()->contains(padString)) {
181
0
        parser->addMatcher(parser->fLocalMatchers.padding = {padString});
182
0
    }
183
0
    parser->addMatcher(parser->fLocalMatchers.ignorables);
184
0
    parser->addMatcher(parser->fLocalMatchers.decimal = {symbols, grouper, parseFlags});
185
0
    // NOTE: parseNoExponent doesn't disable scientific parsing if we have a scientific formatter
186
0
    if (!properties.parseNoExponent || properties.minimumExponentDigits > 0) {
187
0
        parser->addMatcher(parser->fLocalMatchers.scientific = {symbols, grouper});
188
0
    }
189
0
190
0
    //////////////////
191
0
    /// VALIDATORS ///
192
0
    //////////////////
193
0
194
0
    parser->addMatcher(parser->fLocalValidators.number = {});
195
0
    if (isStrict) {
196
0
        parser->addMatcher(parser->fLocalValidators.affix = {});
197
0
    }
198
0
    if (parseCurrency) {
199
0
        parser->addMatcher(parser->fLocalValidators.currency = {});
200
0
    }
201
0
    if (properties.decimalPatternMatchRequired) {
202
0
        bool patternHasDecimalSeparator =
203
0
                properties.decimalSeparatorAlwaysShown || properties.maximumFractionDigits != 0;
204
0
        parser->addMatcher(parser->fLocalValidators.decimalSeparator = {patternHasDecimalSeparator});
205
0
    }
206
0
    // The multiplier takes care of scaling percentages.
207
0
    Scale multiplier = scaleFromProperties(properties);
208
0
    if (multiplier.isValid()) {
209
0
        parser->addMatcher(parser->fLocalValidators.multiplier = {multiplier});
210
0
    }
211
0
212
0
    parser->freeze();
213
0
    return parser.orphan();
214
0
}
215
216
NumberParserImpl::NumberParserImpl(parse_flags_t parseFlags)
217
0
        : fParseFlags(parseFlags) {
218
0
}
219
220
0
NumberParserImpl::~NumberParserImpl() {
221
0
    fNumMatchers = 0;
222
0
}
223
224
0
void NumberParserImpl::addMatcher(NumberParseMatcher& matcher) {
225
0
    if (fNumMatchers + 1 > fMatchers.getCapacity()) {
226
0
        fMatchers.resize(fNumMatchers * 2, fNumMatchers);
227
0
    }
228
0
    fMatchers[fNumMatchers] = &matcher;
229
0
    fNumMatchers++;
230
0
}
231
232
0
void NumberParserImpl::freeze() {
233
0
    fFrozen = true;
234
0
}
235
236
0
parse_flags_t NumberParserImpl::getParseFlags() const {
237
0
    return fParseFlags;
238
0
}
239
240
void NumberParserImpl::parse(const UnicodeString& input, bool greedy, ParsedNumber& result,
241
0
                             UErrorCode& status) const {
242
0
    return parse(input, 0, greedy, result, status);
243
0
}
244
245
void NumberParserImpl::parse(const UnicodeString& input, int32_t start, bool greedy, ParsedNumber& result,
246
0
                             UErrorCode& status) const {
247
0
    if (U_FAILURE(status)) {
248
0
        return;
249
0
    }
250
0
    U_ASSERT(fFrozen);
251
0
    // TODO: Check start >= 0 and start < input.length()
252
0
    StringSegment segment(input, 0 != (fParseFlags & PARSE_FLAG_IGNORE_CASE));
253
0
    segment.adjustOffset(start);
254
0
    if (greedy) {
255
0
        parseGreedyRecursive(segment, result, status);
256
0
    } else {
257
0
        parseLongestRecursive(segment, result, status);
258
0
    }
259
0
    for (int32_t i = 0; i < fNumMatchers; i++) {
260
0
        fMatchers[i]->postProcess(result);
261
0
    }
262
0
    result.postProcess();
263
0
}
264
265
void NumberParserImpl::parseGreedyRecursive(StringSegment& segment, ParsedNumber& result,
266
0
                                            UErrorCode& status) const {
267
0
    // Base Case
268
0
    if (segment.length() == 0) {
269
0
        return;
270
0
    }
271
0
272
0
    int initialOffset = segment.getOffset();
273
0
    for (int32_t i = 0; i < fNumMatchers; i++) {
274
0
        const NumberParseMatcher* matcher = fMatchers[i];
275
0
        if (!matcher->smokeTest(segment)) {
276
0
            continue;
277
0
        }
278
0
        matcher->match(segment, result, status);
279
0
        if (U_FAILURE(status)) {
280
0
            return;
281
0
        }
282
0
        if (segment.getOffset() != initialOffset) {
283
0
            // In a greedy parse, recurse on only the first match.
284
0
            parseGreedyRecursive(segment, result, status);
285
0
            // The following line resets the offset so that the StringSegment says the same across
286
0
            // the function
287
0
            // call boundary. Since we recurse only once, this line is not strictly necessary.
288
0
            segment.setOffset(initialOffset);
289
0
            return;
290
0
        }
291
0
    }
292
0
293
0
    // NOTE: If we get here, the greedy parse completed without consuming the entire string.
294
0
}
295
296
void NumberParserImpl::parseLongestRecursive(StringSegment& segment, ParsedNumber& result,
297
0
                                             UErrorCode& status) const {
298
0
    // Base Case
299
0
    if (segment.length() == 0) {
300
0
        return;
301
0
    }
302
0
303
0
    // TODO: Give a nice way for the matcher to reset the ParsedNumber?
304
0
    ParsedNumber initial(result);
305
0
    ParsedNumber candidate;
306
0
307
0
    int initialOffset = segment.getOffset();
308
0
    for (int32_t i = 0; i < fNumMatchers; i++) {
309
0
        const NumberParseMatcher* matcher = fMatchers[i];
310
0
        if (!matcher->smokeTest(segment)) {
311
0
            continue;
312
0
        }
313
0
314
0
        // In a non-greedy parse, we attempt all possible matches and pick the best.
315
0
        for (int32_t charsToConsume = 0; charsToConsume < segment.length();) {
316
0
            charsToConsume += U16_LENGTH(segment.codePointAt(charsToConsume));
317
0
318
0
            // Run the matcher on a segment of the current length.
319
0
            candidate = initial;
320
0
            segment.setLength(charsToConsume);
321
0
            bool maybeMore = matcher->match(segment, candidate, status);
322
0
            segment.resetLength();
323
0
            if (U_FAILURE(status)) {
324
0
                return;
325
0
            }
326
0
327
0
            // If the entire segment was consumed, recurse.
328
0
            if (segment.getOffset() - initialOffset == charsToConsume) {
329
0
                parseLongestRecursive(segment, candidate, status);
330
0
                if (U_FAILURE(status)) {
331
0
                    return;
332
0
                }
333
0
                if (candidate.isBetterThan(result)) {
334
0
                    result = candidate;
335
0
                }
336
0
            }
337
0
338
0
            // Since the segment can be re-used, reset the offset.
339
0
            // This does not have an effect if the matcher did not consume any chars.
340
0
            segment.setOffset(initialOffset);
341
0
342
0
            // Unless the matcher wants to see the next char, continue to the next matcher.
343
0
            if (!maybeMore) {
344
0
                break;
345
0
            }
346
0
        }
347
0
    }
348
0
}
349
350
0
UnicodeString NumberParserImpl::toString() const {
351
0
    UnicodeString result(u"<NumberParserImpl matchers:[");
352
0
    for (int32_t i = 0; i < fNumMatchers; i++) {
353
0
        result.append(u' ');
354
0
        result.append(fMatchers[i]->toString());
355
0
    }
356
0
    result.append(u" ]>", -1);
357
0
    return result;
358
0
}
359
360
361
#endif /* #if !UCONFIG_NO_FORMATTING */