]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/dcfmtsym.h
ICU-59180.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / dcfmtsym.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ********************************************************************************
5 * Copyright (C) 1997-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
8 *
9 * File DCFMTSYM.H
10 *
11 * Modification History:
12 *
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.
18 * 07/22/98 stephen Changed to match C++ style
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 */
26
27 #ifndef DCFMTSYM_H
28 #define DCFMTSYM_H
29
30 #include "unicode/utypes.h"
31 #include "unicode/uchar.h"
32
33 #if !UCONFIG_NO_FORMATTING
34
35 #include "unicode/uobject.h"
36 #include "unicode/locid.h"
37 #include "unicode/unum.h"
38 #include "unicode/unistr.h"
39
40 /**
41 * \file
42 * \brief C++ API: Symbols for formatting numbers.
43 */
44
45
46 #if U_SHOW_CPLUSPLUS_API
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,
127 /** Significant digit symbol
128 * @stable ICU 3.0 */
129 kSignificantDigitSymbol,
130 /** The monetary grouping separator
131 * @stable ICU 3.6
132 */
133 kMonetaryGroupingSeparatorSymbol,
134 /** One
135 * @stable ICU 4.6
136 */
137 kOneDigitSymbol,
138 /** Two
139 * @stable ICU 4.6
140 */
141 kTwoDigitSymbol,
142 /** Three
143 * @stable ICU 4.6
144 */
145 kThreeDigitSymbol,
146 /** Four
147 * @stable ICU 4.6
148 */
149 kFourDigitSymbol,
150 /** Five
151 * @stable ICU 4.6
152 */
153 kFiveDigitSymbol,
154 /** Six
155 * @stable ICU 4.6
156 */
157 kSixDigitSymbol,
158 /** Seven
159 * @stable ICU 4.6
160 */
161 kSevenDigitSymbol,
162 /** Eight
163 * @stable ICU 4.6
164 */
165 kEightDigitSymbol,
166 /** Nine
167 * @stable ICU 4.6
168 */
169 kNineDigitSymbol,
170 /** Multiplication sign.
171 * @stable ICU 54
172 */
173 kExponentMultiplicationSymbol,
174 /** count symbol constants */
175 kFormatSymbolCount = kNineDigitSymbol + 2
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 */
198 DecimalFormatSymbols(UErrorCode& status);
199
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
213 * @stable ICU 52
214 */
215 static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
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 */
233 virtual ~DecimalFormatSymbols();
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
256 * (characters with modifier letters) can be used.
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 */
262 inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
263
264 /**
265 * Set one of the format symbols by its enum constant.
266 * Each symbol is stored as a string so that graphemes
267 * (characters with modifier letters) can be used.
268 *
269 * @param symbol Constant to indicate a number format symbol.
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.
274 * @stable ICU 2.0
275 */
276 void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
277
278 /**
279 * Returns the locale for which this object was constructed.
280 * @stable ICU 2.6
281 */
282 inline Locale getLocale() const;
283
284 /**
285 * Returns the locale for this object. Two flavors are available:
286 * valid and actual locale.
287 * @stable ICU 2.8
288 */
289 Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
290
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 *
297 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
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.
305 * @stable ICU 4.8
306 */
307 const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
308 UBool beforeCurrency,
309 UErrorCode& status) const;
310 /**
311 * Set pattern string for 'CurrencySpacing' that can be applied to
312 * currency format.
313 *
314 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
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.
318 * @stable ICU 4.8
319 */
320 void setPatternForCurrencySpacing(UCurrencySpacing type,
321 UBool beforeCurrency,
322 const UnicodeString& pattern);
323
324 /**
325 * ICU "poor man's RTTI", returns a UClassID for the actual class.
326 *
327 * @stable ICU 2.2
328 */
329 virtual UClassID getDynamicClassID() const;
330
331 /**
332 * ICU "poor man's RTTI", returns a UClassID for this class.
333 *
334 * @stable ICU 2.2
335 */
336 static UClassID U_EXPORT2 getStaticClassID();
337
338 private:
339 DecimalFormatSymbols();
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
353 /**
354 * Initialize the symbols with default values.
355 */
356 void initialize();
357
358 void setCurrencyForSymbols();
359
360 public:
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
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.
384 * Note: moved #ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
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
392 #ifndef U_HIDE_INTERNAL_API
393 /**
394 * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
395 * @internal
396 */
397 inline const char16_t* getCurrencyPattern(void) const;
398 #endif /* U_HIDE_INTERNAL_API */
399
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
426 char actualLocale[ULOC_FULLNAME_CAPACITY];
427 char validLocale[ULOC_FULLNAME_CAPACITY];
428 const char16_t* currPattern;
429
430 UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
431 UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
432 UBool fIsCustomCurrencySymbol;
433 UBool fIsCustomIntlCurrencySymbol;
434 };
435
436 // -------------------------------------
437
438 inline UnicodeString
439 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
440 const UnicodeString *strPtr;
441 if(symbol < kFormatSymbolCount) {
442 strPtr = &fSymbols[symbol];
443 } else {
444 strPtr = &fNoSymbol;
445 }
446 return *strPtr;
447 }
448
449 // See comments above for this function. Not hidden with #ifndef U_HIDE_INTERNAL_API
450 inline const UnicodeString &
451 DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
452 const UnicodeString *strPtr;
453 if(symbol < kFormatSymbolCount) {
454 strPtr = &fSymbols[symbol];
455 } else {
456 strPtr = &fNoSymbol;
457 }
458 return *strPtr;
459 }
460
461 // -------------------------------------
462
463 inline void
464 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
465 if (symbol == kCurrencySymbol) {
466 fIsCustomCurrencySymbol = TRUE;
467 }
468 else if (symbol == kIntlCurrencySymbol) {
469 fIsCustomIntlCurrencySymbol = TRUE;
470 }
471 if(symbol<kFormatSymbolCount) {
472 fSymbols[symbol]=value;
473 }
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 }
486 }
487
488 // -------------------------------------
489
490 inline Locale
491 DecimalFormatSymbols::getLocale() const {
492 return locale;
493 }
494
495 #ifndef U_HIDE_INTERNAL_API
496 inline const char16_t*
497 DecimalFormatSymbols::getCurrencyPattern() const {
498 return currPattern;
499 }
500 #endif /* U_HIDE_INTERNAL_API */
501
502 U_NAMESPACE_END
503 #endif // U_SHOW_CPLUSPLUS_API
504
505 #endif /* #if !UCONFIG_NO_FORMATTING */
506
507 #endif // _DCFMTSYM
508 //eof