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__
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"
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, MemoryPool 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 numparse::impl 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
<numparse::impl::CodePointMatcher
*, 8>;
56 template class U_I18N_API MaybeStackArray
<UChar
, 4>;
57 template class U_I18N_API MemoryPool
<numparse::impl::CodePointMatcher
, 8>;
58 template class U_I18N_API
numparse::impl::CompactUnicodeString
<4>;
64 struct AffixTokenMatcherSetupData
{
65 const CurrencySymbols
& currencySymbols
;
66 const DecimalFormatSymbols
& dfs
;
67 IgnorablesMatcher
& ignorables
;
69 parse_flags_t parseFlags
;
74 * Small helper class that generates matchers for individual tokens for AffixPatternMatcher.
76 * In Java, this is called AffixTokenMatcherFactory (a "factory"). However, in C++, it is called a
77 * "warehouse", because in addition to generating the matchers, it also retains ownership of them. The
78 * warehouse must stay in scope for the whole lifespan of the AffixPatternMatcher that uses matchers from
83 // Exported as U_I18N_API for tests
84 class U_I18N_API AffixTokenMatcherWarehouse
: public UMemory
{
86 AffixTokenMatcherWarehouse() = default; // WARNING: Leaves the object in an unusable state
88 AffixTokenMatcherWarehouse(const AffixTokenMatcherSetupData
* setupData
);
90 NumberParseMatcher
& minusSign();
92 NumberParseMatcher
& plusSign();
94 NumberParseMatcher
& percent();
96 NumberParseMatcher
& permille();
98 NumberParseMatcher
& currency(UErrorCode
& status
);
100 IgnorablesMatcher
& ignorables();
102 NumberParseMatcher
* nextCodePointMatcher(UChar32 cp
, UErrorCode
& status
);
105 // NOTE: The following field may be unsafe to access after construction is done!
106 const AffixTokenMatcherSetupData
* fSetupData
;
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
;
115 // Use a child class for code point matchers, since it requires non-default operators.
116 MemoryPool
<CodePointMatcher
> fCodePoints
;
118 friend class AffixPatternMatcherBuilder
;
119 friend class AffixPatternMatcher
;
123 class AffixPatternMatcherBuilder
: public TokenConsumer
, public MutableMatcherCollection
{
125 AffixPatternMatcherBuilder(const UnicodeString
& pattern
, AffixTokenMatcherWarehouse
& warehouse
,
126 IgnorablesMatcher
* ignorables
);
128 void consumeToken(::icu::number::impl::AffixPatternType type
, UChar32 cp
, UErrorCode
& status
) override
;
130 /** NOTE: You can build only once! */
131 AffixPatternMatcher
build();
134 ArraySeriesMatcher::MatcherArray fMatchers
;
135 int32_t fMatchersLen
;
136 int32_t fLastTypeOrCp
;
138 const UnicodeString
& fPattern
;
139 AffixTokenMatcherWarehouse
& fWarehouse
;
140 IgnorablesMatcher
* fIgnorables
;
142 void addMatcher(NumberParseMatcher
& matcher
) override
;
146 // Exported as U_I18N_API for tests
147 class U_I18N_API AffixPatternMatcher
: public ArraySeriesMatcher
{
149 AffixPatternMatcher() = default; // WARNING: Leaves the object in an unusable state
151 static AffixPatternMatcher
fromAffixPattern(const UnicodeString
& affixPattern
,
152 AffixTokenMatcherWarehouse
& warehouse
,
153 parse_flags_t parseFlags
, bool* success
,
156 UnicodeString
getPattern() const;
158 bool operator==(const AffixPatternMatcher
& other
) const;
161 CompactUnicodeString
<4> fPattern
;
163 AffixPatternMatcher(MatcherArray
& matchers
, int32_t matchersLen
, const UnicodeString
& pattern
);
165 friend class AffixPatternMatcherBuilder
;
169 class AffixMatcher
: public NumberParseMatcher
, public UMemory
{
171 AffixMatcher() = default; // WARNING: Leaves the object in an unusable state
173 AffixMatcher(AffixPatternMatcher
* prefix
, AffixPatternMatcher
* suffix
, result_flags_t flags
);
175 bool match(StringSegment
& segment
, ParsedNumber
& result
, UErrorCode
& status
) const override
;
177 void postProcess(ParsedNumber
& result
) const override
;
179 bool smokeTest(const StringSegment
& segment
) const override
;
181 int8_t compareTo(const AffixMatcher
& rhs
) const;
183 UnicodeString
toString() const override
;
186 AffixPatternMatcher
* fPrefix
;
187 AffixPatternMatcher
* fSuffix
;
188 result_flags_t fFlags
;
193 * A C++-only class to retain ownership of the AffixMatchers needed for parsing.
195 class AffixMatcherWarehouse
{
197 AffixMatcherWarehouse() = default; // WARNING: Leaves the object in an unusable state
199 AffixMatcherWarehouse(AffixTokenMatcherWarehouse
* tokenWarehouse
);
201 void createAffixMatchers(const AffixPatternProvider
& patternInfo
, MutableMatcherCollection
& output
,
202 const IgnorablesMatcher
& ignorables
, parse_flags_t parseFlags
,
206 // 9 is the limit: positive, zero, and negative, each with prefix, suffix, and prefix+suffix
207 AffixMatcher fAffixMatchers
[9];
208 // 6 is the limit: positive, zero, and negative, a prefix and a suffix for each
209 AffixPatternMatcher fAffixPatternMatchers
[6];
210 // Reference to the warehouse for tokens used by the AffixPatternMatchers
211 AffixTokenMatcherWarehouse
* fTokenWarehouse
;
213 friend class AffixMatcher
;
215 static bool isInteresting(const AffixPatternProvider
& patternInfo
, const IgnorablesMatcher
& ignorables
,
216 parse_flags_t parseFlags
, UErrorCode
& status
);
221 } // namespace numparse
224 #endif //__NUMPARSE_AFFIXES_H__
225 #endif /* #if !UCONFIG_NO_FORMATTING */