LEFT | RIGHT |
1 // © 2017 and later: Unicode, Inc. and others. | 1 // © 2017 and later: Unicode, Inc. and others. |
2 // License & terms of use: http://www.unicode.org/copyright.html#License | 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
3 package com.ibm.icu.impl.number.parse; | 3 package com.ibm.icu.impl.number.parse; |
4 | 4 |
5 import java.text.ParsePosition; | 5 import java.text.ParsePosition; |
6 import java.util.ArrayList; | 6 import java.util.ArrayList; |
7 import java.util.Collection; | 7 import java.util.Collection; |
8 import java.util.List; | 8 import java.util.List; |
9 | 9 |
10 import com.ibm.icu.impl.StringSegment; | 10 import com.ibm.icu.impl.StringSegment; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 NumberParserImpl parser = new NumberParserImpl(parseFlags); | 42 NumberParserImpl parser = new NumberParserImpl(parseFlags); |
43 Currency currency = Currency.getInstance("USD"); | 43 Currency currency = Currency.getInstance("USD"); |
44 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale); | 44 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale); |
45 IgnorablesMatcher ignorables = IgnorablesMatcher.DEFAULT; | 45 IgnorablesMatcher ignorables = IgnorablesMatcher.DEFAULT; |
46 | 46 |
47 AffixTokenMatcherFactory factory = new AffixTokenMatcherFactory(); | 47 AffixTokenMatcherFactory factory = new AffixTokenMatcherFactory(); |
48 factory.currency = currency; | 48 factory.currency = currency; |
49 factory.symbols = symbols; | 49 factory.symbols = symbols; |
50 factory.ignorables = ignorables; | 50 factory.ignorables = ignorables; |
51 factory.locale = locale; | 51 factory.locale = locale; |
| 52 factory.parseFlags = parseFlags; |
52 | 53 |
53 ParsedPatternInfo patternInfo = PatternStringParser.parseToPatternInfo(p
attern); | 54 ParsedPatternInfo patternInfo = PatternStringParser.parseToPatternInfo(p
attern); |
54 AffixMatcher.createMatchers(patternInfo, parser, factory, ignorables, pa
rseFlags); | 55 AffixMatcher.createMatchers(patternInfo, parser, factory, ignorables, pa
rseFlags); |
55 | 56 |
56 Grouper grouper = Grouper.forStrategy(GroupingStrategy.AUTO).withLocaleD
ata(locale, patternInfo); | 57 Grouper grouper = Grouper.forStrategy(GroupingStrategy.AUTO).withLocaleD
ata(locale, patternInfo); |
57 | 58 |
58 parser.addMatcher(ignorables); | 59 parser.addMatcher(ignorables); |
59 parser.addMatcher(DecimalMatcher.getInstance(symbols, grouper, parseFlag
s)); | 60 parser.addMatcher(DecimalMatcher.getInstance(symbols, grouper, parseFlag
s)); |
60 parser.addMatcher(MinusSignMatcher.getInstance(symbols, false)); | 61 parser.addMatcher(MinusSignMatcher.getInstance(symbols, false)); |
61 parser.addMatcher(PlusSignMatcher.getInstance(symbols, false)); | 62 parser.addMatcher(PlusSignMatcher.getInstance(symbols, false)); |
62 parser.addMatcher(PercentMatcher.getInstance(symbols)); | 63 parser.addMatcher(PercentMatcher.getInstance(symbols)); |
63 parser.addMatcher(PermilleMatcher.getInstance(symbols)); | 64 parser.addMatcher(PermilleMatcher.getInstance(symbols)); |
64 parser.addMatcher(NanMatcher.getInstance(symbols, parseFlags)); | 65 parser.addMatcher(NanMatcher.getInstance(symbols, parseFlags)); |
65 parser.addMatcher(InfinityMatcher.getInstance(symbols)); | 66 parser.addMatcher(InfinityMatcher.getInstance(symbols)); |
66 parser.addMatcher(PaddingMatcher.getInstance("@")); | 67 parser.addMatcher(PaddingMatcher.getInstance("@")); |
67 parser.addMatcher(ScientificMatcher.getInstance(symbols, grouper)); | 68 parser.addMatcher(ScientificMatcher.getInstance(symbols, grouper)); |
68 parser.addMatcher(CombinedCurrencyMatcher.getInstance(currency, symbols)
); | 69 parser.addMatcher(CombinedCurrencyMatcher.getInstance(currency, symbols,
parseFlags)); |
69 parser.addMatcher(new RequireNumberValidator()); | 70 parser.addMatcher(new RequireNumberValidator()); |
70 | 71 |
71 parser.freeze(); | 72 parser.freeze(); |
72 return parser; | 73 return parser; |
73 } | 74 } |
74 | 75 |
75 /** | 76 /** |
76 * Parses the string without returning a NumberParserImpl. Used for testing,
not production. | 77 * Parses the string without returning a NumberParserImpl. Used for testing,
not production. |
77 */ | 78 */ |
78 public static Number parseStatic( | 79 public static Number parseStatic( |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 parseFlags |= ParsingUtils.PARSE_FLAG_EXACT_AFFIX; | 169 parseFlags |= ParsingUtils.PARSE_FLAG_EXACT_AFFIX; |
169 } else { | 170 } else { |
170 parseFlags |= ParsingUtils.PARSE_FLAG_INCLUDE_UNPAIRED_AFFIXES; | 171 parseFlags |= ParsingUtils.PARSE_FLAG_INCLUDE_UNPAIRED_AFFIXES; |
171 } | 172 } |
172 if (grouper.getPrimary() <= 0) { | 173 if (grouper.getPrimary() <= 0) { |
173 parseFlags |= ParsingUtils.PARSE_FLAG_GROUPING_DISABLED; | 174 parseFlags |= ParsingUtils.PARSE_FLAG_GROUPING_DISABLED; |
174 } | 175 } |
175 if (parseCurrency || affixProvider.hasCurrencySign()) { | 176 if (parseCurrency || affixProvider.hasCurrencySign()) { |
176 parseFlags |= ParsingUtils.PARSE_FLAG_MONETARY_SEPARATORS; | 177 parseFlags |= ParsingUtils.PARSE_FLAG_MONETARY_SEPARATORS; |
177 } | 178 } |
| 179 if (!parseCurrency) { |
| 180 parseFlags |= ParsingUtils.PARSE_FLAG_NO_FOREIGN_CURRENCIES; |
| 181 } |
178 IgnorablesMatcher ignorables = isStrict ? IgnorablesMatcher.STRICT : Ign
orablesMatcher.DEFAULT; | 182 IgnorablesMatcher ignorables = isStrict ? IgnorablesMatcher.STRICT : Ign
orablesMatcher.DEFAULT; |
179 | 183 |
180 NumberParserImpl parser = new NumberParserImpl(parseFlags); | 184 NumberParserImpl parser = new NumberParserImpl(parseFlags); |
181 | 185 |
182 AffixTokenMatcherFactory factory = new AffixTokenMatcherFactory(); | 186 AffixTokenMatcherFactory factory = new AffixTokenMatcherFactory(); |
183 factory.currency = currency; | 187 factory.currency = currency; |
184 factory.symbols = symbols; | 188 factory.symbols = symbols; |
185 factory.ignorables = ignorables; | 189 factory.ignorables = ignorables; |
186 factory.locale = locale; | 190 factory.locale = locale; |
| 191 factory.parseFlags = parseFlags; |
187 | 192 |
188 ////////////////////// | 193 ////////////////////// |
189 /// AFFIX MATCHERS /// | 194 /// AFFIX MATCHERS /// |
190 ////////////////////// | 195 ////////////////////// |
191 | 196 |
192 // Set up a pattern modifier with mostly defaults to generate AffixMatch
ers. | 197 // Set up a pattern modifier with mostly defaults to generate AffixMatch
ers. |
193 AffixMatcher.createMatchers(affixProvider, parser, factory, ignorables,
parseFlags); | 198 AffixMatcher.createMatchers(affixProvider, parser, factory, ignorables,
parseFlags); |
194 | 199 |
195 //////////////////////// | 200 //////////////////////// |
196 /// CURRENCY MATCHER /// | 201 /// CURRENCY MATCHER /// |
197 //////////////////////// | 202 //////////////////////// |
198 | 203 |
199 if (parseCurrency || affixProvider.hasCurrencySign()) { | 204 if (parseCurrency || affixProvider.hasCurrencySign()) { |
200 parser.addMatcher(CombinedCurrencyMatcher.getInstance(currency, symb
ols)); | 205 parser.addMatcher(CombinedCurrencyMatcher.getInstance(currency, symb
ols, parseFlags)); |
201 } | 206 } |
202 | 207 |
203 /////////////// | 208 /////////////// |
204 /// PERCENT /// | 209 /// PERCENT /// |
205 /////////////// | 210 /////////////// |
206 | 211 |
207 // ICU-TC meeting, April 11, 2018: accept percent/permille only if it is
in the pattern, | 212 // ICU-TC meeting, April 11, 2018: accept percent/permille only if it is
in the pattern, |
208 // and to maintain regressive behavior, divide by 100 even if no percent
sign is present. | 213 // and to maintain regressive behavior, divide by 100 even if no percent
sign is present. |
209 if (affixProvider.containsSymbolType(AffixUtils.TYPE_PERCENT)) { | 214 if (affixProvider.containsSymbolType(AffixUtils.TYPE_PERCENT)) { |
210 parser.addMatcher(PercentMatcher.getInstance(symbols)); | 215 parser.addMatcher(PercentMatcher.getInstance(symbols)); |
(...skipping 23 matching lines...) Expand all Loading... |
234 parser.addMatcher(ScientificMatcher.getInstance(symbols, grouper)); | 239 parser.addMatcher(ScientificMatcher.getInstance(symbols, grouper)); |
235 } | 240 } |
236 | 241 |
237 ////////////////// | 242 ////////////////// |
238 /// VALIDATORS /// | 243 /// VALIDATORS /// |
239 ////////////////// | 244 ////////////////// |
240 | 245 |
241 parser.addMatcher(new RequireNumberValidator()); | 246 parser.addMatcher(new RequireNumberValidator()); |
242 if (isStrict) { | 247 if (isStrict) { |
243 parser.addMatcher(new RequireAffixValidator()); | 248 parser.addMatcher(new RequireAffixValidator()); |
244 } | |
245 if (isStrict && properties.getMinimumExponentDigits() > 0) { | |
246 parser.addMatcher(new RequireExponentValidator()); | |
247 } | 249 } |
248 if (parseCurrency) { | 250 if (parseCurrency) { |
249 parser.addMatcher(new RequireCurrencyValidator()); | 251 parser.addMatcher(new RequireCurrencyValidator()); |
250 } | 252 } |
251 if (properties.getDecimalPatternMatchRequired()) { | 253 if (properties.getDecimalPatternMatchRequired()) { |
252 boolean patternHasDecimalSeparator = properties.getDecimalSeparatorA
lwaysShown() | 254 boolean patternHasDecimalSeparator = properties.getDecimalSeparatorA
lwaysShown() |
253 || properties.getMaximumFractionDigits() != 0; | 255 || properties.getMaximumFractionDigits() != 0; |
254 parser.addMatcher(RequireDecimalSeparatorValidator.getInstance(patte
rnHasDecimalSeparator)); | 256 parser.addMatcher(RequireDecimalSeparatorValidator.getInstance(patte
rnHasDecimalSeparator)); |
255 } | 257 } |
256 // The multiplier takes care of scaling percentages. | 258 // The multiplier takes care of scaling percentages. |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 } | 406 } |
405 } | 407 } |
406 } | 408 } |
407 } | 409 } |
408 | 410 |
409 @Override | 411 @Override |
410 public String toString() { | 412 public String toString() { |
411 return "<NumberParserImpl matchers=" + matchers.toString() + ">"; | 413 return "<NumberParserImpl matchers=" + matchers.toString() + ">"; |
412 } | 414 } |
413 } | 415 } |
LEFT | RIGHT |