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