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