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