1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __NUMPARSE_AFFIXES_H__
8 #define __NUMPARSE_AFFIXES_H__
10 #include "numparse_types.h"
11 #include "numparse_symbols.h"
12 #include "numparse_currency.h"
13 #include "number_affixutils.h"
14 #include "number_currencysymbols.h"
22 // Forward-declaration of implementation classes for friending
23 class AffixPatternMatcherBuilder
;
24 class AffixPatternMatcher
;
26 using ::icu::number::impl::AffixPatternProvider
;
27 using ::icu::number::impl::TokenConsumer
;
28 using ::icu::number::impl::CurrencySymbols
;
31 class CodePointMatcher
: public NumberParseMatcher
, public UMemory
{
33 CodePointMatcher() = default; // WARNING: Leaves the object in an unusable state
35 CodePointMatcher(UChar32 cp
);
37 bool match(StringSegment
& segment
, ParsedNumber
& result
, UErrorCode
& status
) const override
;
39 bool smokeTest(const StringSegment
& segment
) const override
;
41 UnicodeString
toString() const override
;
48 } // namespace numparse
50 // Export a explicit template instantiations of MaybeStackArray and CompactUnicodeString.
51 // When building DLLs for Windows this is required even though no direct access leaks out of the i18n library.
52 // (See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples.)
53 // Note: These need to be outside of the impl::numparse namespace, or Clang will generate a compile error.
54 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
55 template class U_I18N_API MaybeStackArray
<UChar
, 4>;
56 template class U_I18N_API MaybeStackArray
<numparse::impl::CodePointMatcher
*, 3>;
57 template class U_I18N_API
numparse::impl::CompactUnicodeString
<4>;
64 * A warehouse to retain ownership of CodePointMatchers.
66 // Exported as U_I18N_API for tests
67 class U_I18N_API CodePointMatcherWarehouse
: public UMemory
{
69 static constexpr int32_t CODE_POINT_STACK_CAPACITY
= 5; // Number of entries directly on the stack
70 static constexpr int32_t CODE_POINT_BATCH_SIZE
= 10; // Number of entries per heap allocation
73 CodePointMatcherWarehouse();
75 // A custom destructor is needed to free the memory from MaybeStackArray.
76 // A custom move constructor and move assignment seem to be needed because of the custom destructor.
78 ~CodePointMatcherWarehouse();
80 CodePointMatcherWarehouse(CodePointMatcherWarehouse
&& src
) U_NOEXCEPT
;
82 CodePointMatcherWarehouse
& operator=(CodePointMatcherWarehouse
&& src
) U_NOEXCEPT
;
84 NumberParseMatcher
& nextCodePointMatcher(UChar32 cp
);
87 std::array
<CodePointMatcher
, CODE_POINT_STACK_CAPACITY
> codePoints
; // By value
88 MaybeStackArray
<CodePointMatcher
*, 3> codePointsOverflow
; // On heap in "batches"
89 int32_t codePointCount
; // Total for both the ones by value and on heap
90 int32_t codePointNumBatches
; // Number of batches in codePointsOverflow
94 struct AffixTokenMatcherSetupData
{
95 const CurrencySymbols
& currencySymbols
;
96 const DecimalFormatSymbols
& dfs
;
97 IgnorablesMatcher
& ignorables
;
99 parse_flags_t parseFlags
;
104 * Small helper class that generates matchers for individual tokens for AffixPatternMatcher.
106 * In Java, this is called AffixTokenMatcherFactory (a "factory"). However, in C++, it is called a
107 * "warehouse", because in addition to generating the matchers, it also retains ownership of them. The
108 * warehouse must stay in scope for the whole lifespan of the AffixPatternMatcher that uses matchers from
113 // Exported as U_I18N_API for tests
114 class U_I18N_API AffixTokenMatcherWarehouse
: public UMemory
{
116 AffixTokenMatcherWarehouse() = default; // WARNING: Leaves the object in an unusable state
118 AffixTokenMatcherWarehouse(const AffixTokenMatcherSetupData
* setupData
);
120 NumberParseMatcher
& minusSign();
122 NumberParseMatcher
& plusSign();
124 NumberParseMatcher
& percent();
126 NumberParseMatcher
& permille();
128 NumberParseMatcher
& currency(UErrorCode
& status
);
130 IgnorablesMatcher
& ignorables();
132 NumberParseMatcher
& nextCodePointMatcher(UChar32 cp
);
135 // NOTE: The following field may be unsafe to access after construction is done!
136 const AffixTokenMatcherSetupData
* fSetupData
;
138 // NOTE: These are default-constructed and should not be used until initialized.
139 MinusSignMatcher fMinusSign
;
140 PlusSignMatcher fPlusSign
;
141 PercentMatcher fPercent
;
142 PermilleMatcher fPermille
;
143 CombinedCurrencyMatcher fCurrency
;
145 // Use a child class for code point matchers, since it requires non-default operators.
146 CodePointMatcherWarehouse fCodePoints
;
148 friend class AffixPatternMatcherBuilder
;
149 friend class AffixPatternMatcher
;
153 class AffixPatternMatcherBuilder
: public TokenConsumer
, public MutableMatcherCollection
{
155 AffixPatternMatcherBuilder(const UnicodeString
& pattern
, AffixTokenMatcherWarehouse
& warehouse
,
156 IgnorablesMatcher
* ignorables
);
158 void consumeToken(::icu::number::impl::AffixPatternType type
, UChar32 cp
, UErrorCode
& status
) override
;
160 /** NOTE: You can build only once! */
161 AffixPatternMatcher
build();
164 ArraySeriesMatcher::MatcherArray fMatchers
;
165 int32_t fMatchersLen
;
166 int32_t fLastTypeOrCp
;
168 const UnicodeString
& fPattern
;
169 AffixTokenMatcherWarehouse
& fWarehouse
;
170 IgnorablesMatcher
* fIgnorables
;
172 void addMatcher(NumberParseMatcher
& matcher
) override
;
176 // Exported as U_I18N_API for tests
177 class U_I18N_API AffixPatternMatcher
: public ArraySeriesMatcher
{
179 AffixPatternMatcher() = default; // WARNING: Leaves the object in an unusable state
181 static AffixPatternMatcher
fromAffixPattern(const UnicodeString
& affixPattern
,
182 AffixTokenMatcherWarehouse
& warehouse
,
183 parse_flags_t parseFlags
, bool* success
,
186 UnicodeString
getPattern() const;
188 bool operator==(const AffixPatternMatcher
& other
) const;
191 CompactUnicodeString
<4> fPattern
;
193 AffixPatternMatcher(MatcherArray
& matchers
, int32_t matchersLen
, const UnicodeString
& pattern
);
195 friend class AffixPatternMatcherBuilder
;
199 class AffixMatcher
: public NumberParseMatcher
, public UMemory
{
201 AffixMatcher() = default; // WARNING: Leaves the object in an unusable state
203 AffixMatcher(AffixPatternMatcher
* prefix
, AffixPatternMatcher
* suffix
, result_flags_t flags
);
205 bool match(StringSegment
& segment
, ParsedNumber
& result
, UErrorCode
& status
) const override
;
207 void postProcess(ParsedNumber
& result
) const override
;
209 bool smokeTest(const StringSegment
& segment
) const override
;
211 int8_t compareTo(const AffixMatcher
& rhs
) const;
213 UnicodeString
toString() const override
;
216 AffixPatternMatcher
* fPrefix
;
217 AffixPatternMatcher
* fSuffix
;
218 result_flags_t fFlags
;
223 * A C++-only class to retain ownership of the AffixMatchers needed for parsing.
225 class AffixMatcherWarehouse
{
227 AffixMatcherWarehouse() = default; // WARNING: Leaves the object in an unusable state
229 AffixMatcherWarehouse(AffixTokenMatcherWarehouse
* tokenWarehouse
);
231 void createAffixMatchers(const AffixPatternProvider
& patternInfo
, MutableMatcherCollection
& output
,
232 const IgnorablesMatcher
& ignorables
, parse_flags_t parseFlags
,
236 // 9 is the limit: positive, zero, and negative, each with prefix, suffix, and prefix+suffix
237 AffixMatcher fAffixMatchers
[9];
238 // 6 is the limit: positive, zero, and negative, a prefix and a suffix for each
239 AffixPatternMatcher fAffixPatternMatchers
[6];
240 // Reference to the warehouse for tokens used by the AffixPatternMatchers
241 AffixTokenMatcherWarehouse
* fTokenWarehouse
;
243 friend class AffixMatcher
;
245 static bool isInteresting(const AffixPatternProvider
& patternInfo
, const IgnorablesMatcher
& ignorables
,
246 parse_flags_t parseFlags
, UErrorCode
& status
);
251 } // namespace numparse
254 #endif //__NUMPARSE_AFFIXES_H__
255 #endif /* #if !UCONFIG_NO_FORMATTING */