1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
5 * Copyright (C) 1997-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
11 * Modification History:
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
24 ********************************************************************************
30 #include "unicode/utypes.h"
31 #include "unicode/uchar.h"
33 #if !UCONFIG_NO_FORMATTING
35 #include "unicode/uobject.h"
36 #include "unicode/locid.h"
37 #include "unicode/numsys.h"
38 #include "unicode/unum.h"
39 #include "unicode/unistr.h"
43 * \brief C++ API: Symbols for formatting numbers.
47 #if U_SHOW_CPLUSPLUS_API
51 * This class represents the set of symbols needed by DecimalFormat
52 * to format numbers. DecimalFormat creates for itself an instance of
53 * DecimalFormatSymbols from its locale data. If you need to change any
54 * of these symbols, you can get the DecimalFormatSymbols object from
55 * your DecimalFormat and modify it.
57 * Here are the special characters used in the parts of the
58 * subpattern, with notes on their usage.
63 * # a digit, zero shows as absent
64 * . placeholder for decimal separator
65 * , placeholder for grouping separator.
66 * ; separates formats.
67 * - default negative prefix.
68 * % divide by 100 and show as percentage
69 * X any other characters can be used in the prefix or suffix
70 * ' used to quote special characters in a prefix or suffix.
75 * If there is no explicit negative subpattern, - is prefixed to the
76 * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
78 * The grouping separator is commonly used for thousands, but in some
79 * countries for ten-thousands. The interval is a constant number of
80 * digits between the grouping characters, such as 100,000,000 or 1,0000,0000.
81 * If you supply a pattern with multiple grouping characters, the interval
82 * between the last one and the end of the integer is the one that is
83 * used. So "#,##,###,####" == "######,####" == "##,####,####".
85 class U_I18N_API DecimalFormatSymbols
: public UObject
{
88 * Constants for specifying a number format symbol.
91 enum ENumberFormatSymbol
{
92 /** The decimal separator */
93 kDecimalSeparatorSymbol
,
94 /** The grouping separator */
95 kGroupingSeparatorSymbol
,
96 /** The pattern separator */
97 kPatternSeparatorSymbol
,
98 /** The percent sign */
102 /** Character representing a digit in the pattern */
104 /** The minus sign */
108 /** The currency symbol */
110 /** The international currency symbol */
112 /** The monetary separator */
113 kMonetarySeparatorSymbol
,
114 /** The exponential symbol */
116 /** Per mill symbol - replaces kPermillSymbol */
118 /** Escape padding character */
120 /** Infinity symbol */
124 /** Significant digit symbol
126 kSignificantDigitSymbol
,
127 /** The monetary grouping separator
130 kMonetaryGroupingSeparatorSymbol
,
167 /** Multiplication sign.
170 kExponentMultiplicationSymbol
,
171 /** count symbol constants */
172 kFormatSymbolCount
= kNineDigitSymbol
+ 2
176 * Create a DecimalFormatSymbols object for the given locale.
178 * @param locale The locale to get symbols for.
179 * @param status Input/output parameter, set to success or
180 * failure code upon return.
183 DecimalFormatSymbols(const Locale
& locale
, UErrorCode
& status
);
185 #ifndef U_HIDE_DRAFT_API
187 * Creates a DecimalFormatSymbols instance for the given locale with digits and symbols
188 * corresponding to the given NumberingSystem.
190 * This constructor behaves equivalently to the normal constructor called with a locale having a
191 * "numbers=xxxx" keyword specifying the numbering system by name.
193 * In this constructor, the NumberingSystem argument will be used even if the locale has its own
194 * "numbers=xxxx" keyword.
196 * @param locale The locale to get symbols for.
197 * @param ns The numbering system.
198 * @param status Input/output parameter, set to success or
199 * failure code upon return.
202 DecimalFormatSymbols(const Locale
& locale
, const NumberingSystem
& ns
, UErrorCode
& status
);
203 #endif /* U_HIDE_DRAFT_API */
206 * Create a DecimalFormatSymbols object for the default locale.
207 * This constructor will not fail. If the resource file data is
208 * not available, it will use hard-coded last-resort data and
209 * set status to U_USING_FALLBACK_ERROR.
211 * @param status Input/output parameter, set to success or
212 * failure code upon return.
215 DecimalFormatSymbols(UErrorCode
& status
);
218 * Creates a DecimalFormatSymbols object with last-resort data.
219 * Intended for callers who cache the symbols data and
220 * set all symbols on the resulting object.
222 * The last-resort symbols are similar to those for the root data,
223 * except that the grouping separators are empty,
224 * the NaN symbol is U+FFFD rather than "NaN",
225 * and the CurrencySpacing patterns are empty.
227 * @param status Input/output parameter, set to success or
228 * failure code upon return.
229 * @return last-resort symbols
232 static DecimalFormatSymbols
* createWithLastResortData(UErrorCode
& status
);
238 DecimalFormatSymbols(const DecimalFormatSymbols
&);
241 * Assignment operator.
244 DecimalFormatSymbols
& operator=(const DecimalFormatSymbols
&);
250 virtual ~DecimalFormatSymbols();
253 * Return true if another object is semantically equal to this one.
255 * @param other the object to be compared with.
256 * @return true if another object is semantically equal to this one.
259 UBool
operator==(const DecimalFormatSymbols
& other
) const;
262 * Return true if another object is semantically unequal to this one.
264 * @param other the object to be compared with.
265 * @return true if another object is semantically unequal to this one.
268 UBool
operator!=(const DecimalFormatSymbols
& other
) const { return !operator==(other
); }
271 * Get one of the format symbols by its enum constant.
272 * Each symbol is stored as a string so that graphemes
273 * (characters with modifier letters) can be used.
275 * @param symbol Constant to indicate a number format symbol.
276 * @return the format symbols by the param 'symbol'
279 inline UnicodeString
getSymbol(ENumberFormatSymbol symbol
) const;
282 * Set one of the format symbols by its enum constant.
283 * Each symbol is stored as a string so that graphemes
284 * (characters with modifier letters) can be used.
286 * @param symbol Constant to indicate a number format symbol.
287 * @param value value of the format symbol
288 * @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
289 * The default behavior is to automatically set 1-9 if zero is being set and the value
290 * it is being set to corresponds to a known Unicode zero digit.
293 void setSymbol(ENumberFormatSymbol symbol
, const UnicodeString
&value
, const UBool propogateDigits
);
296 * Returns the locale for which this object was constructed.
299 inline Locale
getLocale() const;
302 * Returns the locale for this object. Two flavors are available:
303 * valid and actual locale.
306 Locale
getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const;
309 * Get pattern string for 'CurrencySpacing' that can be applied to
311 * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
312 * be empty if there is no data from current locale and its parent locales.
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 status: Input/output parameter, set to success or
318 * failure code upon return.
319 * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
320 * Return empty string if there is no data for this locale and its parent
324 const UnicodeString
& getPatternForCurrencySpacing(UCurrencySpacing type
,
325 UBool beforeCurrency
,
326 UErrorCode
& status
) const;
328 * Set pattern string for 'CurrencySpacing' that can be applied to
331 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
332 * @param beforeCurrency : true if the pattern is for before currency symbol.
333 * false if the pattern is for after currency symbol.
334 * @param pattern : pattern string to override current setting.
337 void setPatternForCurrencySpacing(UCurrencySpacing type
,
338 UBool beforeCurrency
,
339 const UnicodeString
& pattern
);
342 * ICU "poor man's RTTI", returns a UClassID for the actual class.
346 virtual UClassID
getDynamicClassID() const;
349 * ICU "poor man's RTTI", returns a UClassID for this class.
353 static UClassID U_EXPORT2
getStaticClassID();
356 DecimalFormatSymbols();
359 * Initializes the symbols from the LocaleElements resource bundle.
360 * Note: The organization of LocaleElements badly needs to be
363 * @param locale The locale to get symbols for.
364 * @param success Input/output parameter, set to success or
365 * failure code upon return.
366 * @param useLastResortData determine if use last resort data
367 * @param ns The NumberingSystem to use; otherwise, fall
368 * back to the locale.
370 void initialize(const Locale
& locale
, UErrorCode
& success
,
371 UBool useLastResortData
= FALSE
, const NumberingSystem
* ns
= nullptr);
374 * Initialize the symbols with default values.
378 void setCurrencyForSymbols();
382 #ifndef U_HIDE_INTERNAL_API
384 * @internal For ICU use only
386 inline UBool
isCustomCurrencySymbol() const {
387 return fIsCustomCurrencySymbol
;
391 * @internal For ICU use only
393 inline UBool
isCustomIntlCurrencySymbol() const {
394 return fIsCustomIntlCurrencySymbol
;
398 * @internal For ICU use only
400 inline UChar32
getCodePointZero() const {
401 return fCodePointZero
;
403 #endif /* U_HIDE_INTERNAL_API */
406 * _Internal_ function - more efficient version of getSymbol,
407 * returning a const reference to one of the symbol strings.
408 * The returned reference becomes invalid when the symbol is changed
409 * or when the DecimalFormatSymbols are destroyed.
410 * Note: moved #ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
412 * This is not currently stable API, but if you think it should be stable,
413 * post a comment on the following ticket and the ICU team will take a look:
414 * http://bugs.icu-project.org/trac/ticket/13580
416 * @param symbol Constant to indicate a number format symbol.
417 * @return the format symbol by the param 'symbol'
420 inline const UnicodeString
& getConstSymbol(ENumberFormatSymbol symbol
) const;
422 #ifndef U_HIDE_INTERNAL_API
424 * Returns the const UnicodeString reference, like getConstSymbol,
425 * corresponding to the digit with the given value. This is equivalent
426 * to accessing the symbol from getConstSymbol with the corresponding
427 * key, such as kZeroDigitSymbol or kOneDigitSymbol.
429 * This is not currently stable API, but if you think it should be stable,
430 * post a comment on the following ticket and the ICU team will take a look:
431 * http://bugs.icu-project.org/trac/ticket/13580
433 * @param digit The digit, an integer between 0 and 9 inclusive.
434 * If outside the range 0 to 9, the zero digit is returned.
435 * @return the format symbol for the given digit.
436 * @internal This API is currently for ICU use only.
438 inline const UnicodeString
& getConstDigitSymbol(int32_t digit
) const;
441 * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
444 inline const char16_t* getCurrencyPattern(void) const;
445 #endif /* U_HIDE_INTERNAL_API */
449 * Private symbol strings.
450 * They are either loaded from a resource bundle or otherwise owned.
451 * setSymbol() clones the symbol string.
452 * Readonly aliases can only come from a resource bundle, so that we can always
453 * use fastCopyFrom() with them.
455 * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
456 * from private to protected,
457 * or when fSymbols can be set any other way that allows them to be readonly aliases
458 * to non-resource bundle strings,
459 * then regular UnicodeString copies must be used instead of fastCopyFrom().
463 UnicodeString fSymbols
[kFormatSymbolCount
];
466 * Non-symbol variable for getConstSymbol(). Always empty.
469 UnicodeString fNoSymbol
;
472 * Dealing with code points is faster than dealing with strings when formatting. Because of
473 * this, we maintain a value containing the zero code point that is used whenever digitStrings
474 * represents a sequence of ten code points in order.
476 * <p>If the value stored here is positive, it means that the code point stored in this value
477 * corresponds to the digitStrings array, and codePointZero can be used instead of the
478 * digitStrings array for the purposes of efficient formatting; if -1, then digitStrings does
479 * *not* contain a sequence of code points, and it must be used directly.
481 * <p>It is assumed that codePointZero always shadows the value in digitStrings. codePointZero
482 * should never be set directly; rather, it should be updated only when digitStrings mutates.
483 * That is, the flow of information is digitStrings -> codePointZero, not the other way.
485 UChar32 fCodePointZero
;
489 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
490 char validLocale
[ULOC_FULLNAME_CAPACITY
];
491 const char16_t* currPattern
;
493 UnicodeString currencySpcBeforeSym
[UNUM_CURRENCY_SPACING_COUNT
];
494 UnicodeString currencySpcAfterSym
[UNUM_CURRENCY_SPACING_COUNT
];
495 UBool fIsCustomCurrencySymbol
;
496 UBool fIsCustomIntlCurrencySymbol
;
499 // -------------------------------------
502 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol
) const {
503 const UnicodeString
*strPtr
;
504 if(symbol
< kFormatSymbolCount
) {
505 strPtr
= &fSymbols
[symbol
];
512 // See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API
513 inline const UnicodeString
&
514 DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol
) const {
515 const UnicodeString
*strPtr
;
516 if(symbol
< kFormatSymbolCount
) {
517 strPtr
= &fSymbols
[symbol
];
524 #ifndef U_HIDE_INTERNAL_API
525 inline const UnicodeString
& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit
) const {
526 if (digit
< 0 || digit
> 9) {
530 return fSymbols
[kZeroDigitSymbol
];
532 ENumberFormatSymbol key
= static_cast<ENumberFormatSymbol
>(kOneDigitSymbol
+ digit
- 1);
533 return fSymbols
[key
];
537 // -------------------------------------
540 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol
, const UnicodeString
&value
, const UBool propogateDigits
= TRUE
) {
541 if (symbol
== kCurrencySymbol
) {
542 fIsCustomCurrencySymbol
= TRUE
;
544 else if (symbol
== kIntlCurrencySymbol
) {
545 fIsCustomIntlCurrencySymbol
= TRUE
;
547 if(symbol
<kFormatSymbolCount
) {
548 fSymbols
[symbol
]=value
;
551 // If the zero digit is being set to a known zero digit according to Unicode,
552 // then we automatically set the corresponding 1-9 digits
553 // Also record updates to fCodePointZero. Be conservative if in doubt.
554 if (symbol
== kZeroDigitSymbol
) {
555 UChar32 sym
= value
.char32At(0);
556 if ( propogateDigits
&& u_charDigitValue(sym
) == 0 && value
.countChar32() == 1 ) {
557 fCodePointZero
= sym
;
558 for ( int8_t i
= 1 ; i
<= 9 ; i
++ ) {
560 fSymbols
[(int)kOneDigitSymbol
+i
-1] = UnicodeString(sym
);
565 } else if (symbol
>= kOneDigitSymbol
&& symbol
<= kNineDigitSymbol
) {
570 // -------------------------------------
573 DecimalFormatSymbols::getLocale() const {
577 #ifndef U_HIDE_INTERNAL_API
578 inline const char16_t*
579 DecimalFormatSymbols::getCurrencyPattern() const {
582 #endif /* U_HIDE_INTERNAL_API */
585 #endif // U_SHOW_CPLUSPLUS_API
587 #endif /* #if !UCONFIG_NO_FORMATTING */