Debian Codegraph @

intl/icu/source/i18n/number_types.h

raw
1 // © 2017 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_TYPES_H__
8 #define __NUMBER_TYPES_H__
9
10 #include <cstdint>
11 #include "unicode/decimfmt.h"
12 #include "unicode/unum.h"
13 #include "unicode/numsys.h"
14 #include "unicode/numberformatter.h"
15 #include "unicode/utf16.h"
16 #include "uassert.h"
17 #include "unicode/platform.h"
18 #include "unicode/uniset.h"
19 #include "standardplural.h"
20 #include "formatted_string_builder.h"
21
22 U_NAMESPACE_BEGIN
23 namespace number::impl {
24
25 // For convenience and historical reasons, import the Field typedef to the namespace.
26 typedef FormattedStringBuilder::Field Field;
27
28 // Typedef several enums for brevity and for easier comparison to Java.
29
30 typedef UNumberFormatRoundingMode RoundingMode;
31
32 typedef UNumberFormatPadPosition PadPosition;
33
34 typedef UNumberCompactStyle CompactStyle;
35
36 // ICU4J Equivalent: RoundingUtils.MAX_INT_FRAC_SIG
37 static constexpr int32_t kMaxIntFracSig = 999;
38
39 // ICU4J Equivalent: RoundingUtils.DEFAULT_ROUNDING_MODE
40 static constexpr RoundingMode kDefaultMode = RoundingMode::UNUM_FOUND_HALFEVEN;
41
42 // ICU4J Equivalent: Padder.FALLBACK_PADDING_STRING
43 static constexpr char16_t kFallbackPaddingString[] = u" ";
44
45 // Forward declarations:
46
47 class Modifier;
48 class MutablePatternModifier;
49 class DecimalQuantity;
50 class ModifierStore;
51 struct MicroProps;
52
53
54 enum AffixPatternType {
55 // Represents a literal character; the value is stored in the code point field.
56 TYPE_CODEPOINT = 0,
57
58 // Represents a minus sign symbol '-'.
59 TYPE_MINUS_SIGN = -1,
60
61 // Represents a plus sign symbol '+'.
62 TYPE_PLUS_SIGN = -2,
63
64 // Represents an approximately sign symbol '~'.
65 TYPE_APPROXIMATELY_SIGN = -3,
66
67 // Represents a percent sign symbol '%'.
68 TYPE_PERCENT = -4,
69
70 // Represents a permille sign symbol '‰'.
71 TYPE_PERMILLE = -5,
72
73 // Represents a single currency symbol '¤'.
74 TYPE_CURRENCY_SINGLE = -6,
75
76 // Represents a double currency symbol '¤¤'.
77 TYPE_CURRENCY_DOUBLE = -7,
78
79 // Represents a triple currency symbol '¤¤¤'.
80 TYPE_CURRENCY_TRIPLE = -8,
81
82 // Represents a quadruple currency symbol '¤¤¤¤'.
83 TYPE_CURRENCY_QUAD = -9,
84
85 // Represents a quintuple currency symbol '¤¤¤¤¤'.
86 TYPE_CURRENCY_QUINT = -10,
87
88 // Represents a sequence of six or more currency symbols.
89 TYPE_CURRENCY_OVERFLOW = -15
90 };
91
92 enum CompactType {
93 TYPE_DECIMAL, TYPE_CURRENCY
94 };
95
96 enum Signum {
97 SIGNUM_NEG = 0,
98 SIGNUM_NEG_ZERO = 1,
99 SIGNUM_POS_ZERO = 2,
100 SIGNUM_POS = 3,
101 SIGNUM_COUNT = 4,
102 };
103
104
105 class U_I18N_API AffixPatternProvider {
106 public:
107 static const int32_t AFFIX_PLURAL_MASK = 0xff;
108 static const int32_t AFFIX_PREFIX = 0x100;
109 static const int32_t AFFIX_NEGATIVE_SUBPATTERN = 0x200;
110 static const int32_t AFFIX_PADDING = 0x400;
111
112 // Convenience compound flags
113 static const int32_t AFFIX_POS_PREFIX = AFFIX_PREFIX;
114 static const int32_t AFFIX_POS_SUFFIX = 0;
115 static const int32_t AFFIX_NEG_PREFIX = AFFIX_PREFIX | AFFIX_NEGATIVE_SUBPATTERN;
116 static const int32_t AFFIX_NEG_SUFFIX = AFFIX_NEGATIVE_SUBPATTERN;
117
118 virtual ~AffixPatternProvider();
119
120 virtual char16_t charAt(int flags, int i) const = 0;
121
122 virtual int length(int flags) const = 0;
123
124 virtual UnicodeString getString(int flags) const = 0;
125
126 virtual bool hasCurrencySign() const = 0;
127
128 virtual bool positiveHasPlusSign() const = 0;
129
130 virtual bool hasNegativeSubpattern() const = 0;
131
132 virtual bool negativeHasMinusSign() const = 0;
133
134 virtual bool containsSymbolType(AffixPatternType, UErrorCode&) const = 0;
135
136 /**
137 * True if the pattern has a number placeholder like "0" or "#,##0.00"; false if the pattern does not
138 * have one. This is used in cases like compact notation, where the pattern replaces the entire
139 * number instead of rendering the number.
140 */
141 virtual bool hasBody() const = 0;
142
143 /**
144 * True if the currency symbol should replace the decimal separator.
145 */
146 virtual bool currencyAsDecimal() const = 0;
147 };
148
149
150 /**
151 * A Modifier is an object that can be passed through the formatting pipeline until it is finally applied to the string
152 * builder. A Modifier usually contains a prefix and a suffix that are applied, but it could contain something else,
153 * like a {@link com.ibm.icu.text.SimpleFormatter} pattern.
154 *
155 * A Modifier is usually immutable, except in cases such as {@link MutablePatternModifier}, which are mutable for performance
156 * reasons.
157 *
158 * Exported as U_I18N_API because it is a base class for other exported types
159 */
160 class U_I18N_API Modifier {
161 public:
162 virtual ~Modifier();
163
164 /**
165 * Apply this Modifier to the string builder.
166 *
167 * @param output
168 * The string builder to which to apply this modifier.
169 * @param leftIndex
170 * The left index of the string within the builder. Equal to 0 when only one number is being formatted.
171 * @param rightIndex
172 * The right index of the string within the string builder. Equal to length when only one number is being
173 * formatted.
174 * @return The number of characters (UTF-16 code units) that were added to the string builder.
175 */
176 virtual int32_t apply(FormattedStringBuilder& output, int leftIndex, int rightIndex,
177 UErrorCode& status) const = 0;
178
179 /**
180 * Gets the length of the prefix. This information can be used in combination with {@link #apply} to extract the
181 * prefix and suffix strings.
182 *
183 * @return The number of characters (UTF-16 code units) in the prefix.
184 */
185 virtual int32_t getPrefixLength() const = 0;
186
187 /**
188 * Returns the number of code points in the modifier, prefix plus suffix.
189 */
190 virtual int32_t getCodePointCount() const = 0;
191
192 /**
193 * Whether this modifier is strong. If a modifier is strong, it should always be applied immediately and not allowed
194 * to bubble up. With regard to padding, strong modifiers are considered to be on the inside of the prefix and
195 * suffix.
196 *
197 * @return Whether the modifier is strong.
198 */
199 virtual bool isStrong() const = 0;
200
201 /**
202 * Whether the modifier contains at least one occurrence of the given field.
203 */
204 virtual bool containsField(Field field) const = 0;
205
206 /**
207 * A fill-in for getParameters(). obj will always be set; if non-null, the other
208 * two fields are also safe to read.
209 */
210 struct U_I18N_API Parameters {
211 const ModifierStore* obj = nullptr;
212 Signum signum;
213 StandardPlural::Form plural;
214
215 Parameters();
216 Parameters(const ModifierStore* _obj, Signum _signum, StandardPlural::Form _plural);
217 };
218
219 /**
220 * Gets a set of "parameters" for this Modifier.
221 *
222 * TODO: Make this return a `const Parameters*` more like Java?
223 */
224 virtual void getParameters(Parameters& output) const = 0;
225
226 /**
227 * Returns whether this Modifier equals another Modifier.
228 */
229 virtual bool strictEquals(const Modifier& other) const = 0;
230
231 /**
232 * Returns whether this Modifier is *semantically equivalent* to the other Modifier;
233 * in many cases, this is the same as equal, but parameters should be ignored.
234 */
235 bool semanticallyEquivalent(const Modifier& other) const;
236 };
237
238
239 /**
240 * This is *not* a modifier; rather, it is an object that can return modifiers
241 * based on given parameters.
242 *
243 * Exported as U_I18N_API because it is a base class for other exported types.
244 */
245 class U_I18N_API ModifierStore {
246 public:
247 virtual ~ModifierStore();
248
249 /**
250 * Returns a Modifier with the given parameters (best-effort).
251 */
252 virtual const Modifier* getModifier(Signum signum, StandardPlural::Form plural) const = 0;
253 };
254
255
256 /**
257 * This interface is used when all number formatting settings, including the locale, are known, except for the quantity
258 * itself. The {@link #processQuantity} method performs the final step in the number processing pipeline: it uses the
259 * quantity to generate a finalized {@link MicroProps}, which can be used to render the number to output.
260 *
261 * In other words, this interface is used for the parts of number processing that are <em>quantity-dependent</em>.
262 *
263 * In order to allow for multiple different objects to all mutate the same MicroProps, a "chain" of MicroPropsGenerators
264 * are linked together, and each one is responsible for manipulating a certain quantity-dependent part of the
265 * MicroProps. At the tail of the linked list is a base instance of {@link MicroProps} with properties that are not
266 * quantity-dependent. Each element in the linked list calls {@link #processQuantity} on its "parent", then does its
267 * work, and then returns the result.
268 *
269 * This chain of MicroPropsGenerators is typically constructed by NumberFormatterImpl::macrosToMicroGenerator() when
270 * constructing a NumberFormatter.
271 *
272 * Exported as U_I18N_API because it is a base class for other exported types
273 *
274 */
275 class U_I18N_API MicroPropsGenerator {
276 public:
277 virtual ~MicroPropsGenerator() = default;
278
279 /**
280 * Considers the given {@link DecimalQuantity}, optionally mutates it, and
281 * populates a {@link MicroProps} instance.
282 *
283 * @param quantity The quantity for consideration and optional mutation.
284 * @param micros The MicroProps instance to populate. It will be modified as
285 * needed for the given quantity.
286 */
287 virtual void processQuantity(DecimalQuantity& quantity, MicroProps& micros,
288 UErrorCode& status) const = 0;
289 };
290
291 /**
292 * An interface used by compact notation and scientific notation to choose a multiplier while rounding.
293 */
294 class MultiplierProducer {
295 public:
296 virtual ~MultiplierProducer();
297
298 /**
299 * Maps a magnitude to a multiplier in powers of ten. For example, in compact notation in English, a magnitude of 5
300 * (e.g., 100,000) should return a multiplier of -3, since the number is displayed in thousands.
301 *
302 * @param magnitude
303 * The power of ten of the input number.
304 * @return The shift in powers of ten.
305 */
306 virtual int32_t getMultiplier(int32_t magnitude) const = 0;
307 };
308
309 // Exported as U_I18N_API because it is a public member field of exported DecimalFormatProperties
310 template<typename T>
311 class U_I18N_API NullableValue {
312 public:
313 NullableValue()
314 : fNull(true) {}
315
316 NullableValue(const NullableValue<T>& other) = default;
317
318 explicit NullableValue(const T& other) {
319 fValue = other;
320 fNull = false;
321 }
322
323 NullableValue<T>& operator=(const NullableValue<T>& other) {
324 fNull = other.fNull;
325 if (!fNull) {
326 fValue = other.fValue;
327 }
328 return *this;
329 }
330
331 NullableValue<T>& operator=(const T& other) {
332 fValue = other;
333 fNull = false;
334 return *this;
335 }
336
337 bool operator==(const NullableValue& other) const {
338 // "fValue == other.fValue" returns UBool, not bool (causes compiler warnings)
339 return fNull ? other.fNull : (other.fNull ? false : static_cast<bool>(fValue == other.fValue));
340 }
341
342 void nullify() {
343 // TODO: It might be nice to call the destructor here.
344 fNull = true;
345 }
346
347 bool isNull() const {
348 return fNull;
349 }
350
351 T get(UErrorCode& status) const {
352 if (fNull) {
353 status = U_UNDEFINED_VARIABLE;
354 }
355 return fValue;
356 }
357
358 T getNoError() const {
359 return fValue;
360 }
361
362 T getOrDefault(T defaultValue) const {
363 return fNull ? defaultValue : fValue;
364 }
365
366 private:
367 bool fNull;
368 T fValue;
369 };
370
371 } // namespace number::impl
372 U_NAMESPACE_END
373
374 #endif //__NUMBER_TYPES_H__
375
376 #endif /* #if !UCONFIG_NO_FORMATTING */