]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f A |
3 | /* |
4 | ******************************************************************************** | |
2ca993e8 | 5 | * Copyright (C) 1997-2016, International Business Machines |
b75a7d8f A |
6 | * Corporation and others. All Rights Reserved. |
7 | ******************************************************************************** | |
8 | * | |
9 | * File DCFMTSYM.H | |
10 | * | |
11 | * Modification History: | |
729e4ab9 | 12 | * |
b75a7d8f A |
13 | * Date Name Description |
14 | * 02/19/97 aliu Converted from java. | |
15 | * 03/18/97 clhuang Updated per C++ implementation. | |
16 | * 03/27/97 helena Updated to pass the simple test after code review. | |
17 | * 08/26/97 aliu Added currency/intl currency symbol support. | |
729e4ab9 | 18 | * 07/22/98 stephen Changed to match C++ style |
b75a7d8f A |
19 | * currencySymbol -> fCurrencySymbol |
20 | * Constants changed from CAPS to kCaps | |
21 | * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes | |
22 | * 09/22/00 grhoten Marked deprecation tags with a pointer to replacement | |
23 | * functions. | |
24 | ******************************************************************************** | |
25 | */ | |
729e4ab9 | 26 | |
b75a7d8f A |
27 | #ifndef DCFMTSYM_H |
28 | #define DCFMTSYM_H | |
729e4ab9 | 29 | |
b75a7d8f | 30 | #include "unicode/utypes.h" |
729e4ab9 | 31 | #include "unicode/uchar.h" |
b75a7d8f A |
32 | |
33 | #if !UCONFIG_NO_FORMATTING | |
34 | ||
35 | #include "unicode/uobject.h" | |
36 | #include "unicode/locid.h" | |
4388f060 | 37 | #include "unicode/unum.h" |
f3c0d7a5 | 38 | #include "unicode/unistr.h" |
b75a7d8f | 39 | |
73c04bcf | 40 | /** |
729e4ab9 | 41 | * \file |
73c04bcf A |
42 | * \brief C++ API: Symbols for formatting numbers. |
43 | */ | |
44 | ||
45 | ||
f3c0d7a5 | 46 | #if U_SHOW_CPLUSPLUS_API |
b75a7d8f A |
47 | U_NAMESPACE_BEGIN |
48 | ||
49 | /** | |
50 | * This class represents the set of symbols needed by DecimalFormat | |
51 | * to format numbers. DecimalFormat creates for itself an instance of | |
52 | * DecimalFormatSymbols from its locale data. If you need to change any | |
53 | * of these symbols, you can get the DecimalFormatSymbols object from | |
54 | * your DecimalFormat and modify it. | |
55 | * <P> | |
56 | * Here are the special characters used in the parts of the | |
57 | * subpattern, with notes on their usage. | |
58 | * <pre> | |
59 | * \code | |
60 | * Symbol Meaning | |
61 | * 0 a digit | |
62 | * # a digit, zero shows as absent | |
63 | * . placeholder for decimal separator | |
64 | * , placeholder for grouping separator. | |
65 | * ; separates formats. | |
66 | * - default negative prefix. | |
67 | * % divide by 100 and show as percentage | |
68 | * X any other characters can be used in the prefix or suffix | |
69 | * ' used to quote special characters in a prefix or suffix. | |
70 | * \endcode | |
71 | * </pre> | |
72 | * [Notes] | |
73 | * <P> | |
74 | * If there is no explicit negative subpattern, - is prefixed to the | |
75 | * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00". | |
76 | * <P> | |
77 | * The grouping separator is commonly used for thousands, but in some | |
78 | * countries for ten-thousands. The interval is a constant number of | |
79 | * digits between the grouping characters, such as 100,000,000 or 1,0000,0000. | |
80 | * If you supply a pattern with multiple grouping characters, the interval | |
81 | * between the last one and the end of the integer is the one that is | |
82 | * used. So "#,##,###,####" == "######,####" == "##,####,####". | |
83 | * <P> | |
84 | * This class only handles localized digits where the 10 digits are | |
85 | * contiguous in Unicode, from 0 to 9. Other digits sets (such as | |
86 | * superscripts) would need a different subclass. | |
87 | */ | |
88 | class U_I18N_API DecimalFormatSymbols : public UObject { | |
89 | public: | |
90 | /** | |
91 | * Constants for specifying a number format symbol. | |
92 | * @stable ICU 2.0 | |
93 | */ | |
94 | enum ENumberFormatSymbol { | |
95 | /** The decimal separator */ | |
96 | kDecimalSeparatorSymbol, | |
97 | /** The grouping separator */ | |
98 | kGroupingSeparatorSymbol, | |
99 | /** The pattern separator */ | |
100 | kPatternSeparatorSymbol, | |
101 | /** The percent sign */ | |
102 | kPercentSymbol, | |
103 | /** Zero*/ | |
104 | kZeroDigitSymbol, | |
105 | /** Character representing a digit in the pattern */ | |
106 | kDigitSymbol, | |
107 | /** The minus sign */ | |
108 | kMinusSignSymbol, | |
109 | /** The plus sign */ | |
110 | kPlusSignSymbol, | |
111 | /** The currency symbol */ | |
112 | kCurrencySymbol, | |
113 | /** The international currency symbol */ | |
114 | kIntlCurrencySymbol, | |
115 | /** The monetary separator */ | |
116 | kMonetarySeparatorSymbol, | |
117 | /** The exponential symbol */ | |
118 | kExponentialSymbol, | |
119 | /** Per mill symbol - replaces kPermillSymbol */ | |
120 | kPerMillSymbol, | |
121 | /** Escape padding character */ | |
122 | kPadEscapeSymbol, | |
123 | /** Infinity symbol */ | |
124 | kInfinitySymbol, | |
125 | /** Nan symbol */ | |
126 | kNaNSymbol, | |
374ca955 | 127 | /** Significant digit symbol |
73c04bcf | 128 | * @stable ICU 3.0 */ |
374ca955 | 129 | kSignificantDigitSymbol, |
729e4ab9 | 130 | /** The monetary grouping separator |
46f4442e | 131 | * @stable ICU 3.6 |
73c04bcf A |
132 | */ |
133 | kMonetaryGroupingSeparatorSymbol, | |
729e4ab9 | 134 | /** One |
4388f060 | 135 | * @stable ICU 4.6 |
729e4ab9 A |
136 | */ |
137 | kOneDigitSymbol, | |
138 | /** Two | |
4388f060 | 139 | * @stable ICU 4.6 |
729e4ab9 A |
140 | */ |
141 | kTwoDigitSymbol, | |
142 | /** Three | |
4388f060 | 143 | * @stable ICU 4.6 |
729e4ab9 A |
144 | */ |
145 | kThreeDigitSymbol, | |
146 | /** Four | |
4388f060 | 147 | * @stable ICU 4.6 |
729e4ab9 A |
148 | */ |
149 | kFourDigitSymbol, | |
150 | /** Five | |
4388f060 | 151 | * @stable ICU 4.6 |
729e4ab9 A |
152 | */ |
153 | kFiveDigitSymbol, | |
154 | /** Six | |
4388f060 | 155 | * @stable ICU 4.6 |
729e4ab9 A |
156 | */ |
157 | kSixDigitSymbol, | |
158 | /** Seven | |
4388f060 | 159 | * @stable ICU 4.6 |
729e4ab9 A |
160 | */ |
161 | kSevenDigitSymbol, | |
162 | /** Eight | |
4388f060 | 163 | * @stable ICU 4.6 |
729e4ab9 A |
164 | */ |
165 | kEightDigitSymbol, | |
166 | /** Nine | |
4388f060 | 167 | * @stable ICU 4.6 |
729e4ab9 A |
168 | */ |
169 | kNineDigitSymbol, | |
b331163b | 170 | /** Multiplication sign. |
2ca993e8 | 171 | * @stable ICU 54 |
b331163b A |
172 | */ |
173 | kExponentMultiplicationSymbol, | |
b75a7d8f | 174 | /** count symbol constants */ |
b331163b | 175 | kFormatSymbolCount = kNineDigitSymbol + 2 |
b75a7d8f A |
176 | }; |
177 | ||
178 | /** | |
179 | * Create a DecimalFormatSymbols object for the given locale. | |
180 | * | |
181 | * @param locale The locale to get symbols for. | |
182 | * @param status Input/output parameter, set to success or | |
183 | * failure code upon return. | |
184 | * @stable ICU 2.0 | |
185 | */ | |
186 | DecimalFormatSymbols(const Locale& locale, UErrorCode& status); | |
187 | ||
188 | /** | |
189 | * Create a DecimalFormatSymbols object for the default locale. | |
190 | * This constructor will not fail. If the resource file data is | |
191 | * not available, it will use hard-coded last-resort data and | |
192 | * set status to U_USING_FALLBACK_ERROR. | |
193 | * | |
194 | * @param status Input/output parameter, set to success or | |
195 | * failure code upon return. | |
196 | * @stable ICU 2.0 | |
197 | */ | |
57a6839d A |
198 | DecimalFormatSymbols(UErrorCode& status); |
199 | ||
57a6839d A |
200 | /** |
201 | * Creates a DecimalFormatSymbols object with last-resort data. | |
202 | * Intended for callers who cache the symbols data and | |
203 | * set all symbols on the resulting object. | |
204 | * | |
205 | * The last-resort symbols are similar to those for the root data, | |
206 | * except that the grouping separators are empty, | |
207 | * the NaN symbol is U+FFFD rather than "NaN", | |
208 | * and the CurrencySpacing patterns are empty. | |
209 | * | |
210 | * @param status Input/output parameter, set to success or | |
211 | * failure code upon return. | |
212 | * @return last-resort symbols | |
b331163b | 213 | * @stable ICU 52 |
57a6839d A |
214 | */ |
215 | static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status); | |
b75a7d8f A |
216 | |
217 | /** | |
218 | * Copy constructor. | |
219 | * @stable ICU 2.0 | |
220 | */ | |
221 | DecimalFormatSymbols(const DecimalFormatSymbols&); | |
222 | ||
223 | /** | |
224 | * Assignment operator. | |
225 | * @stable ICU 2.0 | |
226 | */ | |
227 | DecimalFormatSymbols& operator=(const DecimalFormatSymbols&); | |
228 | ||
229 | /** | |
230 | * Destructor. | |
231 | * @stable ICU 2.0 | |
232 | */ | |
374ca955 | 233 | virtual ~DecimalFormatSymbols(); |
b75a7d8f A |
234 | |
235 | /** | |
236 | * Return true if another object is semantically equal to this one. | |
237 | * | |
238 | * @param other the object to be compared with. | |
239 | * @return true if another object is semantically equal to this one. | |
240 | * @stable ICU 2.0 | |
241 | */ | |
242 | UBool operator==(const DecimalFormatSymbols& other) const; | |
243 | ||
244 | /** | |
245 | * Return true if another object is semantically unequal to this one. | |
246 | * | |
247 | * @param other the object to be compared with. | |
248 | * @return true if another object is semantically unequal to this one. | |
249 | * @stable ICU 2.0 | |
250 | */ | |
251 | UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); } | |
252 | ||
253 | /** | |
254 | * Get one of the format symbols by its enum constant. | |
255 | * Each symbol is stored as a string so that graphemes | |
729e4ab9 | 256 | * (characters with modifier letters) can be used. |
b75a7d8f A |
257 | * |
258 | * @param symbol Constant to indicate a number format symbol. | |
259 | * @return the format symbols by the param 'symbol' | |
260 | * @stable ICU 2.0 | |
261 | */ | |
374ca955 | 262 | inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const; |
b75a7d8f A |
263 | |
264 | /** | |
265 | * Set one of the format symbols by its enum constant. | |
266 | * Each symbol is stored as a string so that graphemes | |
729e4ab9 | 267 | * (characters with modifier letters) can be used. |
b75a7d8f A |
268 | * |
269 | * @param symbol Constant to indicate a number format symbol. | |
729e4ab9 A |
270 | * @param value value of the format symbol |
271 | * @param propogateDigits If false, setting the zero digit will not automatically set 1-9. | |
272 | * The default behavior is to automatically set 1-9 if zero is being set and the value | |
273 | * it is being set to corresponds to a known Unicode zero digit. | |
b75a7d8f A |
274 | * @stable ICU 2.0 |
275 | */ | |
729e4ab9 | 276 | void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits); |
b75a7d8f A |
277 | |
278 | /** | |
279 | * Returns the locale for which this object was constructed. | |
280 | * @stable ICU 2.6 | |
281 | */ | |
282 | inline Locale getLocale() const; | |
283 | ||
374ca955 A |
284 | /** |
285 | * Returns the locale for this object. Two flavors are available: | |
286 | * valid and actual locale. | |
73c04bcf | 287 | * @stable ICU 2.8 |
374ca955 A |
288 | */ |
289 | Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const; | |
290 | ||
729e4ab9 A |
291 | /** |
292 | * Get pattern string for 'CurrencySpacing' that can be applied to | |
293 | * currency format. | |
294 | * This API gets the CurrencySpacing data from ResourceBundle. The pattern can | |
295 | * be empty if there is no data from current locale and its parent locales. | |
296 | * | |
4388f060 | 297 | * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT. |
729e4ab9 A |
298 | * @param beforeCurrency : true if the pattern is for before currency symbol. |
299 | * false if the pattern is for after currency symbol. | |
300 | * @param status: Input/output parameter, set to success or | |
301 | * failure code upon return. | |
302 | * @return pattern string for currencyMatch, surroundingMatch or spaceInsert. | |
303 | * Return empty string if there is no data for this locale and its parent | |
304 | * locales. | |
4388f060 | 305 | * @stable ICU 4.8 |
729e4ab9 | 306 | */ |
4388f060 | 307 | const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type, |
729e4ab9 A |
308 | UBool beforeCurrency, |
309 | UErrorCode& status) const; | |
310 | /** | |
311 | * Set pattern string for 'CurrencySpacing' that can be applied to | |
312 | * currency format. | |
313 | * | |
4388f060 | 314 | * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT. |
729e4ab9 A |
315 | * @param beforeCurrency : true if the pattern is for before currency symbol. |
316 | * false if the pattern is for after currency symbol. | |
317 | * @param pattern : pattern string to override current setting. | |
4388f060 | 318 | * @stable ICU 4.8 |
729e4ab9 | 319 | */ |
4388f060 | 320 | void setPatternForCurrencySpacing(UCurrencySpacing type, |
729e4ab9 A |
321 | UBool beforeCurrency, |
322 | const UnicodeString& pattern); | |
323 | ||
b75a7d8f A |
324 | /** |
325 | * ICU "poor man's RTTI", returns a UClassID for the actual class. | |
326 | * | |
374ca955 | 327 | * @stable ICU 2.2 |
b75a7d8f | 328 | */ |
374ca955 | 329 | virtual UClassID getDynamicClassID() const; |
b75a7d8f A |
330 | |
331 | /** | |
332 | * ICU "poor man's RTTI", returns a UClassID for this class. | |
333 | * | |
374ca955 | 334 | * @stable ICU 2.2 |
b75a7d8f | 335 | */ |
374ca955 | 336 | static UClassID U_EXPORT2 getStaticClassID(); |
b75a7d8f A |
337 | |
338 | private: | |
57a6839d | 339 | DecimalFormatSymbols(); |
b75a7d8f A |
340 | |
341 | /** | |
342 | * Initializes the symbols from the LocaleElements resource bundle. | |
343 | * Note: The organization of LocaleElements badly needs to be | |
344 | * cleaned up. | |
345 | * | |
346 | * @param locale The locale to get symbols for. | |
347 | * @param success Input/output parameter, set to success or | |
348 | * failure code upon return. | |
349 | * @param useLastResortData determine if use last resort data | |
350 | */ | |
351 | void initialize(const Locale& locale, UErrorCode& success, UBool useLastResortData = FALSE); | |
352 | ||
b75a7d8f A |
353 | /** |
354 | * Initialize the symbols with default values. | |
355 | */ | |
356 | void initialize(); | |
357 | ||
358 | void setCurrencyForSymbols(); | |
359 | ||
360 | public: | |
2ca993e8 A |
361 | |
362 | #ifndef U_HIDE_INTERNAL_API | |
363 | /** | |
364 | * @internal For ICU use only | |
365 | */ | |
366 | inline UBool isCustomCurrencySymbol() const { | |
367 | return fIsCustomCurrencySymbol; | |
368 | } | |
369 | ||
370 | /** | |
371 | * @internal For ICU use only | |
372 | */ | |
373 | inline UBool isCustomIntlCurrencySymbol() const { | |
374 | return fIsCustomIntlCurrencySymbol; | |
375 | } | |
376 | #endif /* U_HIDE_INTERNAL_API */ | |
377 | ||
b75a7d8f A |
378 | /** |
379 | * _Internal_ function - more efficient version of getSymbol, | |
380 | * returning a const reference to one of the symbol strings. | |
381 | * The returned reference becomes invalid when the symbol is changed | |
382 | * or when the DecimalFormatSymbols are destroyed. | |
383 | * ### TODO markus 2002oct11: Consider proposing getConstSymbol() to be really public. | |
b331163b | 384 | * Note: moved #ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat |
b75a7d8f A |
385 | * |
386 | * @param symbol Constant to indicate a number format symbol. | |
387 | * @return the format symbol by the param 'symbol' | |
388 | * @internal | |
389 | */ | |
390 | inline const UnicodeString &getConstSymbol(ENumberFormatSymbol symbol) const; | |
391 | ||
57a6839d | 392 | #ifndef U_HIDE_INTERNAL_API |
73c04bcf A |
393 | /** |
394 | * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API. | |
395 | * @internal | |
396 | */ | |
f3c0d7a5 | 397 | inline const char16_t* getCurrencyPattern(void) const; |
4388f060 | 398 | #endif /* U_HIDE_INTERNAL_API */ |
73c04bcf | 399 | |
b75a7d8f A |
400 | private: |
401 | /** | |
402 | * Private symbol strings. | |
403 | * They are either loaded from a resource bundle or otherwise owned. | |
404 | * setSymbol() clones the symbol string. | |
405 | * Readonly aliases can only come from a resource bundle, so that we can always | |
406 | * use fastCopyFrom() with them. | |
407 | * | |
408 | * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes | |
409 | * from private to protected, | |
410 | * or when fSymbols can be set any other way that allows them to be readonly aliases | |
411 | * to non-resource bundle strings, | |
412 | * then regular UnicodeString copies must be used instead of fastCopyFrom(). | |
413 | * | |
414 | * @internal | |
415 | */ | |
416 | UnicodeString fSymbols[kFormatSymbolCount]; | |
417 | ||
418 | /** | |
419 | * Non-symbol variable for getConstSymbol(). Always empty. | |
420 | * @internal | |
421 | */ | |
422 | UnicodeString fNoSymbol; | |
423 | ||
424 | Locale locale; | |
425 | ||
374ca955 A |
426 | char actualLocale[ULOC_FULLNAME_CAPACITY]; |
427 | char validLocale[ULOC_FULLNAME_CAPACITY]; | |
f3c0d7a5 | 428 | const char16_t* currPattern; |
729e4ab9 | 429 | |
4388f060 A |
430 | UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT]; |
431 | UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT]; | |
2ca993e8 A |
432 | UBool fIsCustomCurrencySymbol; |
433 | UBool fIsCustomIntlCurrencySymbol; | |
b75a7d8f A |
434 | }; |
435 | ||
b75a7d8f A |
436 | // ------------------------------------- |
437 | ||
438 | inline UnicodeString | |
439 | DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const { | |
374ca955 A |
440 | const UnicodeString *strPtr; |
441 | if(symbol < kFormatSymbolCount) { | |
442 | strPtr = &fSymbols[symbol]; | |
b75a7d8f | 443 | } else { |
374ca955 | 444 | strPtr = &fNoSymbol; |
b75a7d8f | 445 | } |
374ca955 | 446 | return *strPtr; |
b75a7d8f A |
447 | } |
448 | ||
2ca993e8 | 449 | // See comments above for this function. Not hidden with #ifndef U_HIDE_INTERNAL_API |
b75a7d8f A |
450 | inline const UnicodeString & |
451 | DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const { | |
374ca955 A |
452 | const UnicodeString *strPtr; |
453 | if(symbol < kFormatSymbolCount) { | |
454 | strPtr = &fSymbols[symbol]; | |
b75a7d8f | 455 | } else { |
374ca955 | 456 | strPtr = &fNoSymbol; |
b75a7d8f | 457 | } |
374ca955 | 458 | return *strPtr; |
b75a7d8f | 459 | } |
4388f060 | 460 | |
b75a7d8f A |
461 | // ------------------------------------- |
462 | ||
463 | inline void | |
729e4ab9 | 464 | DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) { |
2ca993e8 A |
465 | if (symbol == kCurrencySymbol) { |
466 | fIsCustomCurrencySymbol = TRUE; | |
467 | } | |
468 | else if (symbol == kIntlCurrencySymbol) { | |
469 | fIsCustomIntlCurrencySymbol = TRUE; | |
470 | } | |
b75a7d8f A |
471 | if(symbol<kFormatSymbolCount) { |
472 | fSymbols[symbol]=value; | |
473 | } | |
729e4ab9 A |
474 | |
475 | // If the zero digit is being set to a known zero digit according to Unicode, | |
476 | // then we automatically set the corresponding 1-9 digits | |
477 | if ( propogateDigits && symbol == kZeroDigitSymbol && value.countChar32() == 1 ) { | |
478 | UChar32 sym = value.char32At(0); | |
479 | if ( u_charDigitValue(sym) == 0 ) { | |
480 | for ( int8_t i = 1 ; i<= 9 ; i++ ) { | |
481 | sym++; | |
482 | fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym); | |
483 | } | |
484 | } | |
485 | } | |
b75a7d8f A |
486 | } |
487 | ||
488 | // ------------------------------------- | |
489 | ||
490 | inline Locale | |
491 | DecimalFormatSymbols::getLocale() const { | |
492 | return locale; | |
493 | } | |
494 | ||
57a6839d | 495 | #ifndef U_HIDE_INTERNAL_API |
f3c0d7a5 | 496 | inline const char16_t* |
73c04bcf A |
497 | DecimalFormatSymbols::getCurrencyPattern() const { |
498 | return currPattern; | |
499 | } | |
57a6839d | 500 | #endif /* U_HIDE_INTERNAL_API */ |
4388f060 | 501 | |
b75a7d8f | 502 | U_NAMESPACE_END |
f3c0d7a5 | 503 | #endif // U_SHOW_CPLUSPLUS_API |
b75a7d8f A |
504 | |
505 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
506 | ||
507 | #endif // _DCFMTSYM | |
508 | //eof |