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