numparse_compositions.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 __SOURCE_NUMPARSE_COMPOSITIONS__
  6. #define __SOURCE_NUMPARSE_COMPOSITIONS__
  7. #include "numparse_types.h"
  8. U_NAMESPACE_BEGIN
  9. // Export an explicit template instantiation of the MaybeStackArray that is used as a data member of ArraySeriesMatcher.
  10. // When building DLLs for Windows this is required even though no direct access to the MaybeStackArray leaks out of the i18n library.
  11. // (See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples.)
  12. #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
  13. template class U_I18N_API MaybeStackArray<const numparse::impl::NumberParseMatcher*, 3>;
  14. #endif
  15. namespace numparse {
  16. namespace impl {
  17. /**
  18. * Base class for AnyMatcher and SeriesMatcher.
  19. */
  20. // Exported as U_I18N_API for tests
  21. class U_I18N_API CompositionMatcher : public NumberParseMatcher {
  22. protected:
  23. // No construction except by subclasses!
  24. CompositionMatcher() = default;
  25. // To be overridden by subclasses (used for iteration):
  26. virtual const NumberParseMatcher* const* begin() const = 0;
  27. // To be overridden by subclasses (used for iteration):
  28. virtual const NumberParseMatcher* const* end() const = 0;
  29. };
  30. // NOTE: AnyMatcher is no longer being used. The previous definition is shown below.
  31. // The implementation can be found in SVN source control, deleted around March 30, 2018.
  32. ///**
  33. // * Composes a number of matchers, and succeeds if any of the matchers succeed. Always greedily chooses
  34. // * the first matcher in the list to succeed.
  35. // *
  36. // * NOTE: In C++, this is a base class, unlike ICU4J, which uses a factory-style interface.
  37. // *
  38. // * @author sffc
  39. // * @see SeriesMatcher
  40. // */
  41. //class AnyMatcher : public CompositionMatcher {
  42. // public:
  43. // bool match(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const override;
  44. //
  45. // bool smokeTest(const StringSegment& segment) const override;
  46. //
  47. // void postProcess(ParsedNumber& result) const override;
  48. //
  49. // protected:
  50. // // No construction except by subclasses!
  51. // AnyMatcher() = default;
  52. //};
  53. /**
  54. * Composes a number of matchers, running one after another. Matches the input string only if all of the
  55. * matchers in the series succeed. Performs greedy matches within the context of the series.
  56. *
  57. * @author sffc
  58. * @see AnyMatcher
  59. */
  60. // Exported as U_I18N_API for tests
  61. class U_I18N_API SeriesMatcher : public CompositionMatcher {
  62. public:
  63. bool match(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const override;
  64. bool smokeTest(const StringSegment& segment) const override;
  65. void postProcess(ParsedNumber& result) const override;
  66. virtual int32_t length() const = 0;
  67. protected:
  68. // No construction except by subclasses!
  69. SeriesMatcher() = default;
  70. };
  71. /**
  72. * An implementation of SeriesMatcher that references an array of matchers.
  73. *
  74. * The object adopts the array, but NOT the matchers contained inside the array.
  75. */
  76. // Exported as U_I18N_API for tests
  77. class U_I18N_API ArraySeriesMatcher : public SeriesMatcher {
  78. public:
  79. ArraySeriesMatcher(); // WARNING: Leaves the object in an unusable state
  80. typedef MaybeStackArray<const NumberParseMatcher*, 3> MatcherArray;
  81. /** The array is std::move'd */
  82. ArraySeriesMatcher(MatcherArray& matchers, int32_t matchersLen);
  83. UnicodeString toString() const override;
  84. int32_t length() const override;
  85. protected:
  86. const NumberParseMatcher* const* begin() const override;
  87. const NumberParseMatcher* const* end() const override;
  88. private:
  89. MatcherArray fMatchers;
  90. int32_t fMatchersLen;
  91. };
  92. } // namespace impl
  93. } // namespace numparse
  94. U_NAMESPACE_END
  95. #endif //__SOURCE_NUMPARSE_COMPOSITIONS__
  96. #endif /* #if !UCONFIG_NO_FORMATTING */