/src/icu/icu4c/source/i18n/numparse_validators.cpp
Line | Count | Source |
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_validators.h" |
14 | | #include "static_unicode_sets.h" |
15 | | |
16 | | using namespace icu; |
17 | | using namespace icu::numparse; |
18 | | using namespace icu::numparse::impl; |
19 | | |
20 | | |
21 | 6.78M | void RequireAffixValidator::postProcess(ParsedNumber& result) const { |
22 | 6.78M | if (result.prefix.isBogus() || result.suffix.isBogus()) { |
23 | | // We saw a prefix or a suffix but not both. Fail the parse. |
24 | 3.75M | result.flags |= FLAG_FAIL; |
25 | 3.75M | } |
26 | 6.78M | } |
27 | | |
28 | 0 | UnicodeString RequireAffixValidator::toString() const { |
29 | 0 | return u"<ReqAffix>"; |
30 | 0 | } |
31 | | |
32 | | |
33 | 0 | void RequireCurrencyValidator::postProcess(ParsedNumber& result) const { |
34 | 0 | if (result.currencyCode[0] == 0) { |
35 | 0 | result.flags |= FLAG_FAIL; |
36 | 0 | } |
37 | 0 | } |
38 | | |
39 | 0 | UnicodeString RequireCurrencyValidator::toString() const { |
40 | 0 | return u"<ReqCurrency>"; |
41 | 0 | } |
42 | | |
43 | | |
44 | | RequireDecimalSeparatorValidator::RequireDecimalSeparatorValidator(bool patternHasDecimalSeparator) |
45 | 0 | : fPatternHasDecimalSeparator(patternHasDecimalSeparator) { |
46 | 0 | } |
47 | | |
48 | 0 | void RequireDecimalSeparatorValidator::postProcess(ParsedNumber& result) const { |
49 | 0 | bool parseIsInfNaN = 0 != (result.flags & FLAG_INFINITY) || 0 != (result.flags & FLAG_NAN); |
50 | 0 | bool parseHasDecimalSeparator = 0 != (result.flags & FLAG_HAS_DECIMAL_SEPARATOR); |
51 | 0 | if (!parseIsInfNaN && parseHasDecimalSeparator != fPatternHasDecimalSeparator) { |
52 | 0 | result.flags |= FLAG_FAIL; |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | 0 | UnicodeString RequireDecimalSeparatorValidator::toString() const { |
57 | 0 | return u"<ReqDecimal>"; |
58 | 0 | } |
59 | | |
60 | | |
61 | 6.78M | void RequireNumberValidator::postProcess(ParsedNumber& result) const { |
62 | | // Require that a number is matched. |
63 | 6.78M | if (!result.seenNumber()) { |
64 | 5.00M | result.flags |= FLAG_FAIL; |
65 | 5.00M | } |
66 | 6.78M | } |
67 | | |
68 | 0 | UnicodeString RequireNumberValidator::toString() const { |
69 | 0 | return u"<ReqNumber>"; |
70 | 0 | } |
71 | | |
72 | | MultiplierParseHandler::MultiplierParseHandler(::icu::number::Scale multiplier) |
73 | 13.8k | : fMultiplier(std::move(multiplier)) {} |
74 | | |
75 | 547k | void MultiplierParseHandler::postProcess(ParsedNumber& result) const { |
76 | 547k | if (!result.quantity.bogus) { |
77 | 127k | fMultiplier.applyReciprocalTo(result.quantity); |
78 | | // NOTE: It is okay if the multiplier was negative. |
79 | 127k | } |
80 | 547k | } |
81 | | |
82 | 0 | UnicodeString MultiplierParseHandler::toString() const { |
83 | 0 | return u"<Scale>"; |
84 | 0 | } |
85 | | |
86 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |