]>
Commit | Line | Data |
---|---|---|
0f5d89e8 A |
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 __NUMBER_MAPPER_H__ | |
8 | #define __NUMBER_MAPPER_H__ | |
9 | ||
10 | #include <atomic> | |
11 | #include "number_types.h" | |
12 | #include "unicode/currpinf.h" | |
13 | #include "standardplural.h" | |
14 | #include "number_patternstring.h" | |
15 | #include "number_currencysymbols.h" | |
16 | #include "numparse_impl.h" | |
17 | ||
18 | U_NAMESPACE_BEGIN | |
19 | namespace number { | |
20 | namespace impl { | |
21 | ||
22 | ||
23 | class PropertiesAffixPatternProvider : public AffixPatternProvider, public UMemory { | |
24 | public: | |
25 | bool isBogus() const { | |
26 | return fBogus; | |
27 | } | |
28 | ||
29 | void setToBogus() { | |
30 | fBogus = true; | |
31 | } | |
32 | ||
33 | void setTo(const DecimalFormatProperties& properties, UErrorCode& status); | |
34 | ||
35 | PropertiesAffixPatternProvider() = default; // puts instance in valid but undefined state | |
36 | ||
37 | PropertiesAffixPatternProvider(const DecimalFormatProperties& properties, UErrorCode& status) { | |
38 | setTo(properties, status); | |
39 | } | |
40 | ||
41 | // AffixPatternProvider Methods: | |
42 | ||
43 | char16_t charAt(int32_t flags, int32_t i) const U_OVERRIDE; | |
44 | ||
45 | int32_t length(int32_t flags) const U_OVERRIDE; | |
46 | ||
47 | UnicodeString getString(int32_t flags) const U_OVERRIDE; | |
48 | ||
49 | bool hasCurrencySign() const U_OVERRIDE; | |
50 | ||
51 | bool positiveHasPlusSign() const U_OVERRIDE; | |
52 | ||
53 | bool hasNegativeSubpattern() const U_OVERRIDE; | |
54 | ||
55 | bool negativeHasMinusSign() const U_OVERRIDE; | |
56 | ||
57 | bool containsSymbolType(AffixPatternType, UErrorCode&) const U_OVERRIDE; | |
58 | ||
59 | bool hasBody() const U_OVERRIDE; | |
60 | ||
61 | private: | |
62 | UnicodeString posPrefix; | |
63 | UnicodeString posSuffix; | |
64 | UnicodeString negPrefix; | |
65 | UnicodeString negSuffix; | |
3d1f044b | 66 | bool isCurrencyPattern; |
0f5d89e8 A |
67 | |
68 | const UnicodeString& getStringInternal(int32_t flags) const; | |
69 | ||
70 | bool fBogus{true}; | |
71 | }; | |
72 | ||
73 | ||
74 | class CurrencyPluralInfoAffixProvider : public AffixPatternProvider, public UMemory { | |
75 | public: | |
76 | bool isBogus() const { | |
77 | return fBogus; | |
78 | } | |
79 | ||
80 | void setToBogus() { | |
81 | fBogus = true; | |
82 | } | |
83 | ||
84 | void setTo(const CurrencyPluralInfo& cpi, const DecimalFormatProperties& properties, | |
85 | UErrorCode& status); | |
86 | ||
87 | // AffixPatternProvider Methods: | |
88 | ||
89 | char16_t charAt(int32_t flags, int32_t i) const U_OVERRIDE; | |
90 | ||
91 | int32_t length(int32_t flags) const U_OVERRIDE; | |
92 | ||
93 | UnicodeString getString(int32_t flags) const U_OVERRIDE; | |
94 | ||
95 | bool hasCurrencySign() const U_OVERRIDE; | |
96 | ||
97 | bool positiveHasPlusSign() const U_OVERRIDE; | |
98 | ||
99 | bool hasNegativeSubpattern() const U_OVERRIDE; | |
100 | ||
101 | bool negativeHasMinusSign() const U_OVERRIDE; | |
102 | ||
103 | bool containsSymbolType(AffixPatternType, UErrorCode&) const U_OVERRIDE; | |
104 | ||
105 | bool hasBody() const U_OVERRIDE; | |
106 | ||
107 | private: | |
108 | PropertiesAffixPatternProvider affixesByPlural[StandardPlural::COUNT]; | |
109 | ||
110 | bool fBogus{true}; | |
111 | }; | |
112 | ||
113 | ||
114 | /** | |
115 | * A struct for ownership of a few objects needed for formatting. | |
116 | */ | |
117 | struct DecimalFormatWarehouse { | |
118 | PropertiesAffixPatternProvider propertiesAPP; | |
119 | CurrencyPluralInfoAffixProvider currencyPluralInfoAPP; | |
120 | CurrencySymbols currencySymbols; | |
121 | }; | |
122 | ||
123 | ||
124 | /** | |
125 | * Internal fields for DecimalFormat. | |
126 | * TODO: Make some of these fields by value instead of by LocalPointer? | |
127 | */ | |
128 | struct DecimalFormatFields : public UMemory { | |
129 | /** The property bag corresponding to user-specified settings and settings from the pattern string. */ | |
130 | LocalPointer<DecimalFormatProperties> properties; | |
131 | ||
132 | /** The symbols for the current locale. */ | |
133 | LocalPointer<const DecimalFormatSymbols> symbols; | |
134 | ||
135 | /** | |
136 | * The pre-computed formatter object. Setters cause this to be re-computed atomically. The {@link | |
137 | * #format} method uses the formatter directly without needing to synchronize. | |
138 | */ | |
3d1f044b | 139 | LocalPointer<LocalizedNumberFormatter> formatter; |
0f5d89e8 A |
140 | |
141 | /** The lazy-computed parser for .parse() */ | |
142 | std::atomic<::icu::numparse::impl::NumberParserImpl*> atomicParser = {}; | |
143 | ||
144 | /** The lazy-computed parser for .parseCurrency() */ | |
145 | std::atomic<::icu::numparse::impl::NumberParserImpl*> atomicCurrencyParser = {}; | |
146 | ||
147 | /** Small object ownership warehouse for the formatter and parser */ | |
148 | DecimalFormatWarehouse warehouse; | |
149 | ||
150 | /** The effective properties as exported from the formatter object. Used by some getters. */ | |
151 | LocalPointer<DecimalFormatProperties> exportedProperties; | |
152 | ||
153 | // Data for fastpath | |
154 | bool canUseFastFormat = false; | |
155 | struct FastFormatData { | |
156 | char16_t cpZero; | |
157 | char16_t cpGroupingSeparator; | |
158 | char16_t cpMinusSign; | |
159 | int8_t minInt; | |
160 | int8_t maxInt; | |
161 | } fastData; | |
162 | }; | |
163 | ||
164 | ||
165 | /** | |
166 | * Utilities for converting between a DecimalFormatProperties and a MacroProps. | |
167 | */ | |
168 | class NumberPropertyMapper { | |
169 | public: | |
170 | /** Convenience method to create a NumberFormatter directly from Properties. */ | |
171 | static UnlocalizedNumberFormatter create(const DecimalFormatProperties& properties, | |
172 | const DecimalFormatSymbols& symbols, | |
173 | DecimalFormatWarehouse& warehouse, UErrorCode& status); | |
174 | ||
175 | /** Convenience method to create a NumberFormatter directly from Properties. */ | |
176 | static UnlocalizedNumberFormatter create(const DecimalFormatProperties& properties, | |
177 | const DecimalFormatSymbols& symbols, | |
178 | DecimalFormatWarehouse& warehouse, | |
179 | DecimalFormatProperties& exportedProperties, | |
180 | UErrorCode& status); | |
181 | ||
182 | /** | |
183 | * Creates a new {@link MacroProps} object based on the content of a {@link DecimalFormatProperties} | |
184 | * object. In other words, maps Properties to MacroProps. This function is used by the | |
185 | * JDK-compatibility API to call into the ICU 60 fluent number formatting pipeline. | |
186 | * | |
187 | * @param properties | |
188 | * The property bag to be mapped. | |
189 | * @param symbols | |
190 | * The symbols associated with the property bag. | |
191 | * @param exportedProperties | |
192 | * A property bag in which to store validated properties. Used by some DecimalFormat | |
193 | * getters. | |
194 | * @return A new MacroProps containing all of the information in the Properties. | |
195 | */ | |
196 | static MacroProps oldToNew(const DecimalFormatProperties& properties, | |
197 | const DecimalFormatSymbols& symbols, DecimalFormatWarehouse& warehouse, | |
198 | DecimalFormatProperties* exportedProperties, UErrorCode& status); | |
199 | }; | |
200 | ||
201 | ||
202 | } // namespace impl | |
203 | } // namespace numparse | |
204 | U_NAMESPACE_END | |
205 | ||
206 | #endif //__NUMBER_MAPPER_H__ | |
207 | #endif /* #if !UCONFIG_NO_FORMATTING */ |