numparse_validators.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #include "unicode/utypes.h"
  4. #if !UCONFIG_NO_FORMATTING
  5. // Allow implicit conversion from char16_t* to UnicodeString for this file:
  6. // Helpful in toString methods and elsewhere.
  7. #define UNISTR_FROM_STRING_EXPLICIT
  8. #include "numparse_types.h"
  9. #include "numparse_validators.h"
  10. #include "static_unicode_sets.h"
  11. using namespace icu;
  12. using namespace icu::numparse;
  13. using namespace icu::numparse::impl;
  14. void RequireAffixValidator::postProcess(ParsedNumber& result) const {
  15. if (result.prefix.isBogus() || result.suffix.isBogus()) {
  16. // We saw a prefix or a suffix but not both. Fail the parse.
  17. result.flags |= FLAG_FAIL;
  18. }
  19. }
  20. UnicodeString RequireAffixValidator::toString() const {
  21. return u"<ReqAffix>";
  22. }
  23. void RequireCurrencyValidator::postProcess(ParsedNumber& result) const {
  24. if (result.currencyCode[0] == 0) {
  25. result.flags |= FLAG_FAIL;
  26. }
  27. }
  28. UnicodeString RequireCurrencyValidator::toString() const {
  29. return u"<ReqCurrency>";
  30. }
  31. RequireDecimalSeparatorValidator::RequireDecimalSeparatorValidator(bool patternHasDecimalSeparator)
  32. : fPatternHasDecimalSeparator(patternHasDecimalSeparator) {
  33. }
  34. void RequireDecimalSeparatorValidator::postProcess(ParsedNumber& result) const {
  35. bool parseHasDecimalSeparator = 0 != (result.flags & FLAG_HAS_DECIMAL_SEPARATOR);
  36. if (parseHasDecimalSeparator != fPatternHasDecimalSeparator) {
  37. result.flags |= FLAG_FAIL;
  38. }
  39. }
  40. UnicodeString RequireDecimalSeparatorValidator::toString() const {
  41. return u"<ReqDecimal>";
  42. }
  43. void RequireNumberValidator::postProcess(ParsedNumber& result) const {
  44. // Require that a number is matched.
  45. if (!result.seenNumber()) {
  46. result.flags |= FLAG_FAIL;
  47. }
  48. }
  49. UnicodeString RequireNumberValidator::toString() const {
  50. return u"<ReqNumber>";
  51. }
  52. MultiplierParseHandler::MultiplierParseHandler(::icu::number::Scale multiplier)
  53. : fMultiplier(std::move(multiplier)) {}
  54. void MultiplierParseHandler::postProcess(ParsedNumber& result) const {
  55. if (!result.quantity.bogus) {
  56. fMultiplier.applyReciprocalTo(result.quantity);
  57. // NOTE: It is okay if the multiplier was negative.
  58. }
  59. }
  60. UnicodeString MultiplierParseHandler::toString() const {
  61. return u"<Scale>";
  62. }
  63. #endif /* #if !UCONFIG_NO_FORMATTING */