numparse_impl.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef __NUMPARSE_IMPL_H__
  6. #define __NUMPARSE_IMPL_H__
  7. #include "numparse_types.h"
  8. #include "numparse_decimal.h"
  9. #include "numparse_symbols.h"
  10. #include "numparse_scientific.h"
  11. #include "unicode/uniset.h"
  12. #include "numparse_currency.h"
  13. #include "numparse_affixes.h"
  14. #include "number_decimfmtprops.h"
  15. #include "unicode/localpointer.h"
  16. #include "numparse_validators.h"
  17. #include "number_multiplier.h"
  18. #include "string_segment.h"
  19. U_NAMESPACE_BEGIN
  20. // Export an explicit template instantiation of the MaybeStackArray that is used as a data member of NumberParserImpl.
  21. // When building DLLs for Windows this is required even though no direct access to the MaybeStackArray leaks out of the i18n library.
  22. // (See numparse_compositions.h, numparse_affixes.h, datefmt.h, and others for similar examples.)
  23. #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
  24. template class U_I18N_API MaybeStackArray<const numparse::impl::NumberParseMatcher*, 10>;
  25. #endif
  26. namespace numparse {
  27. namespace impl {
  28. // Exported as U_I18N_API for tests
  29. class U_I18N_API NumberParserImpl : public MutableMatcherCollection, public UMemory {
  30. public:
  31. virtual ~NumberParserImpl();
  32. static NumberParserImpl* createSimpleParser(const Locale& locale, const UnicodeString& patternString,
  33. parse_flags_t parseFlags, UErrorCode& status);
  34. static NumberParserImpl* createParserFromProperties(
  35. const number::impl::DecimalFormatProperties& properties, const DecimalFormatSymbols& symbols,
  36. bool parseCurrency, UErrorCode& status);
  37. /**
  38. * Does NOT take ownership of the matcher. The matcher MUST remain valid for the lifespan of the
  39. * NumberParserImpl.
  40. * @param matcher The matcher to reference.
  41. */
  42. void addMatcher(NumberParseMatcher& matcher) override;
  43. void freeze();
  44. parse_flags_t getParseFlags() const;
  45. void parse(const UnicodeString& input, bool greedy, ParsedNumber& result, UErrorCode& status) const;
  46. void parse(const UnicodeString& input, int32_t start, bool greedy, ParsedNumber& result,
  47. UErrorCode& status) const;
  48. UnicodeString toString() const;
  49. private:
  50. parse_flags_t fParseFlags;
  51. int32_t fNumMatchers = 0;
  52. // NOTE: The stack capacity for fMatchers and fLeads should be the same
  53. MaybeStackArray<const NumberParseMatcher*, 10> fMatchers;
  54. bool fFrozen = false;
  55. // WARNING: All of these matchers start in an undefined state (default-constructed).
  56. // You must use an assignment operator on them before using.
  57. struct {
  58. IgnorablesMatcher ignorables;
  59. InfinityMatcher infinity;
  60. MinusSignMatcher minusSign;
  61. NanMatcher nan;
  62. PaddingMatcher padding;
  63. PercentMatcher percent;
  64. PermilleMatcher permille;
  65. PlusSignMatcher plusSign;
  66. DecimalMatcher decimal;
  67. ScientificMatcher scientific;
  68. CombinedCurrencyMatcher currency;
  69. AffixMatcherWarehouse affixMatcherWarehouse;
  70. AffixTokenMatcherWarehouse affixTokenMatcherWarehouse;
  71. } fLocalMatchers;
  72. struct {
  73. RequireAffixValidator affix;
  74. RequireCurrencyValidator currency;
  75. RequireDecimalSeparatorValidator decimalSeparator;
  76. RequireNumberValidator number;
  77. MultiplierParseHandler multiplier;
  78. } fLocalValidators;
  79. explicit NumberParserImpl(parse_flags_t parseFlags);
  80. void parseGreedy(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const;
  81. void parseLongestRecursive(
  82. StringSegment& segment, ParsedNumber& result, int32_t recursionLevels, UErrorCode& status) const;
  83. };
  84. } // namespace impl
  85. } // namespace numparse
  86. U_NAMESPACE_END
  87. #endif //__NUMPARSE_IMPL_H__
  88. #endif /* #if !UCONFIG_NO_FORMATTING */