xref: /aosp_15_r20/external/icu/libicu/cts_headers/numparse_affixes.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
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 #ifndef __NUMPARSE_AFFIXES_H__
8 #define __NUMPARSE_AFFIXES_H__
9 
10 #include "cmemory.h"
11 
12 #include "numparse_types.h"
13 #include "numparse_symbols.h"
14 #include "numparse_currency.h"
15 #include "number_affixutils.h"
16 #include "number_currencysymbols.h"
17 
18 U_NAMESPACE_BEGIN
19 
20 namespace numparse::impl {
21 
22 // Forward-declaration of implementation classes for friending
23 class AffixPatternMatcherBuilder;
24 class AffixPatternMatcher;
25 
26 using ::icu::number::impl::AffixPatternProvider;
27 using ::icu::number::impl::TokenConsumer;
28 using ::icu::number::impl::CurrencySymbols;
29 
30 
31 class U_I18N_API CodePointMatcher : public NumberParseMatcher, public UMemory {
32   public:
33     CodePointMatcher() = default;  // WARNING: Leaves the object in an unusable state
34 
35     CodePointMatcher(UChar32 cp);
36 
37     bool match(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const override;
38 
39     bool smokeTest(const StringSegment& segment) const override;
40 
41     UnicodeString toString() const override;
42 
43   private:
44     UChar32 fCp;
45 };
46 
47 } // namespace numparse::impl
48 
49 // Export a explicit template instantiations of MaybeStackArray, MemoryPool and CompactUnicodeString.
50 // When building DLLs for Windows this is required even though no direct access leaks out of the i18n library.
51 // (See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples.)
52 // Note: These need to be outside of the numparse::impl namespace, or Clang will generate a compile error.
53 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
54 template class U_I18N_API MaybeStackArray<numparse::impl::CodePointMatcher*, 8>;
55 template class U_I18N_API MaybeStackArray<char16_t, 4>;
56 template class U_I18N_API MemoryPool<numparse::impl::CodePointMatcher, 8>;
57 template class U_I18N_API numparse::impl::CompactUnicodeString<4>;
58 #endif
59 
60 namespace numparse::impl {
61 
62 struct AffixTokenMatcherSetupData {
63     const CurrencySymbols& currencySymbols;
64     const DecimalFormatSymbols& dfs;
65     IgnorablesMatcher& ignorables;
66     const Locale& locale;
67     parse_flags_t parseFlags;
68 };
69 
70 
71 /**
72  * Small helper class that generates matchers for individual tokens for AffixPatternMatcher.
73  *
74  * In Java, this is called AffixTokenMatcherFactory (a "factory"). However, in C++, it is called a
75  * "warehouse", because in addition to generating the matchers, it also retains ownership of them. The
76  * warehouse must stay in scope for the whole lifespan of the AffixPatternMatcher that uses matchers from
77  * the warehouse.
78  *
79  * @author sffc
80  */
81 // Exported as U_I18N_API for tests
82 class U_I18N_API AffixTokenMatcherWarehouse : public UMemory {
83   public:
84     AffixTokenMatcherWarehouse() = default;  // WARNING: Leaves the object in an unusable state
85 
86     AffixTokenMatcherWarehouse(const AffixTokenMatcherSetupData* setupData);
87 
88     NumberParseMatcher& minusSign();
89 
90     NumberParseMatcher& plusSign();
91 
92     NumberParseMatcher& percent();
93 
94     NumberParseMatcher& permille();
95 
96     NumberParseMatcher& currency(UErrorCode& status);
97 
98     IgnorablesMatcher& ignorables();
99 
100     NumberParseMatcher* nextCodePointMatcher(UChar32 cp, UErrorCode& status);
101 
102     bool hasEmptyCurrencySymbol() const;
103 
104   private:
105     // NOTE: The following field may be unsafe to access after construction is done!
106     const AffixTokenMatcherSetupData* fSetupData;
107 
108     // NOTE: These are default-constructed and should not be used until initialized.
109     MinusSignMatcher fMinusSign;
110     PlusSignMatcher fPlusSign;
111     PercentMatcher fPercent;
112     PermilleMatcher fPermille;
113     CombinedCurrencyMatcher fCurrency;
114 
115     // Use a child class for code point matchers, since it requires non-default operators.
116     MemoryPool<CodePointMatcher> fCodePoints;
117 
118     friend class AffixPatternMatcherBuilder;
119     friend class AffixPatternMatcher;
120 };
121 
122 
123 class AffixPatternMatcherBuilder : public TokenConsumer, public MutableMatcherCollection {
124   public:
125     AffixPatternMatcherBuilder(const UnicodeString& pattern, AffixTokenMatcherWarehouse& warehouse,
126                                IgnorablesMatcher* ignorables);
127 
128     void consumeToken(::icu::number::impl::AffixPatternType type, UChar32 cp, UErrorCode& status) override;
129 
130     /** NOTE: You can build only once! */
131     AffixPatternMatcher build(UErrorCode& status);
132 
133   private:
134     ArraySeriesMatcher::MatcherArray fMatchers;
135     int32_t fMatchersLen;
136     int32_t fLastTypeOrCp;
137 
138     const UnicodeString& fPattern;
139     AffixTokenMatcherWarehouse& fWarehouse;
140     IgnorablesMatcher* fIgnorables;
141 
142     void addMatcher(NumberParseMatcher& matcher) override;
143 };
144 
145 
146 // Exported as U_I18N_API for tests
147 class U_I18N_API AffixPatternMatcher : public ArraySeriesMatcher {
148   public:
149     AffixPatternMatcher() = default;  // WARNING: Leaves the object in an unusable state
150 
151     static AffixPatternMatcher fromAffixPattern(const UnicodeString& affixPattern,
152                                                 AffixTokenMatcherWarehouse& warehouse,
153                                                 parse_flags_t parseFlags, bool* success,
154                                                 UErrorCode& status);
155 
156     UnicodeString getPattern() const;
157 
158     bool operator==(const AffixPatternMatcher& other) const;
159 
160   private:
161     CompactUnicodeString<4> fPattern;
162 
163     AffixPatternMatcher(MatcherArray& matchers, int32_t matchersLen, const UnicodeString& pattern,
164                         UErrorCode& status);
165 
166     friend class AffixPatternMatcherBuilder;
167 };
168 
169 
170 class AffixMatcher : public NumberParseMatcher, public UMemory {
171   public:
172     AffixMatcher() = default;  // WARNING: Leaves the object in an unusable state
173 
174     AffixMatcher(AffixPatternMatcher* prefix, AffixPatternMatcher* suffix, result_flags_t flags);
175 
176     bool match(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const override;
177 
178     void postProcess(ParsedNumber& result) const override;
179 
180     bool smokeTest(const StringSegment& segment) const override;
181 
182     int8_t compareTo(const AffixMatcher& rhs) const;
183 
184     UnicodeString toString() const override;
185 
186   private:
187     AffixPatternMatcher* fPrefix;
188     AffixPatternMatcher* fSuffix;
189     result_flags_t fFlags;
190 };
191 
192 
193 /**
194  * A C++-only class to retain ownership of the AffixMatchers needed for parsing.
195  */
196 class AffixMatcherWarehouse {
197   public:
198     AffixMatcherWarehouse() = default;  // WARNING: Leaves the object in an unusable state
199 
200     AffixMatcherWarehouse(AffixTokenMatcherWarehouse* tokenWarehouse);
201 
202     void createAffixMatchers(const AffixPatternProvider& patternInfo, MutableMatcherCollection& output,
203                              const IgnorablesMatcher& ignorables, parse_flags_t parseFlags,
204                              UErrorCode& status);
205 
206   private:
207     // 18 is the limit: positive, zero, and negative, each with prefix, suffix, and prefix+suffix,
208     // and doubled since there may be an empty currency symbol
209     AffixMatcher fAffixMatchers[18];
210     // 6 is the limit: positive, zero, and negative, a prefix and a suffix for each,
211     // and doubled since there may be an empty currency symbol
212     AffixPatternMatcher fAffixPatternMatchers[12];
213     // Reference to the warehouse for tokens used by the AffixPatternMatchers
214     AffixTokenMatcherWarehouse* fTokenWarehouse;
215 
216     friend class AffixMatcher;
217 
218     static bool isInteresting(const AffixPatternProvider& patternInfo, const IgnorablesMatcher& ignorables,
219                               parse_flags_t parseFlags, UErrorCode& status);
220 };
221 
222 } // namespace numparse::impl
223 
224 U_NAMESPACE_END
225 
226 #endif //__NUMPARSE_AFFIXES_H__
227 #endif /* #if !UCONFIG_NO_FORMATTING */
228