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
);
186 * Creates a DecimalFormatSymbols instance for the given locale with digits and symbols
187 * corresponding to the given NumberingSystem.
189 * This constructor behaves equivalently to the normal constructor called with a locale having a
190 * "numbers=xxxx" keyword specifying the numbering system by name.
192 * In this constructor, the NumberingSystem argument will be used even if the locale has its own
193 * "numbers=xxxx" keyword.
195 * @param locale The locale to get symbols for.
196 * @param ns The numbering system.
197 * @param status Input/output parameter, set to success or
198 * failure code upon return.
201 DecimalFormatSymbols(const Locale
& locale
, const NumberingSystem
& ns
, UErrorCode
& status
);
204 * Create a DecimalFormatSymbols object for the default locale.
205 * This constructor will not fail. If the resource file data is
206 * not available, it will use hard-coded last-resort data and
207 * set status to U_USING_FALLBACK_ERROR.
209 * @param status Input/output parameter, set to success or
210 * failure code upon return.
213 DecimalFormatSymbols(UErrorCode
& status
);
216 * Creates a DecimalFormatSymbols object with last-resort data.
217 * Intended for callers who cache the symbols data and
218 * set all symbols on the resulting object.
220 * The last-resort symbols are similar to those for the root data,
221 * except that the grouping separators are empty,
222 * the NaN symbol is U+FFFD rather than "NaN",
223 * and the CurrencySpacing patterns are empty.
225 * @param status Input/output parameter, set to success or
226 * failure code upon return.
227 * @return last-resort symbols
230 static DecimalFormatSymbols
* createWithLastResortData(UErrorCode
& status
);
236 DecimalFormatSymbols(const DecimalFormatSymbols
&);
239 * Assignment operator.
242 DecimalFormatSymbols
& operator=(const DecimalFormatSymbols
&);
248 virtual ~DecimalFormatSymbols();
251 * Return true if another object is semantically equal to this one.
253 * @param other the object to be compared with.
254 * @return true if another object is semantically equal to this one.
257 UBool
operator==(const DecimalFormatSymbols
& other
) const;
260 * Return true if another object is semantically unequal to this one.
262 * @param other the object to be compared with.
263 * @return true if another object is semantically unequal to this one.
266 UBool
operator!=(const DecimalFormatSymbols
& other
) const { return !operator==(other
); }
269 * Get one of the format symbols by its enum constant.
270 * Each symbol is stored as a string so that graphemes
271 * (characters with modifier letters) can be used.
273 * @param symbol Constant to indicate a number format symbol.
274 * @return the format symbols by the param 'symbol'
277 inline UnicodeString
getSymbol(ENumberFormatSymbol symbol
) const;
280 * Set one of the format symbols by its enum constant.
281 * Each symbol is stored as a string so that graphemes
282 * (characters with modifier letters) can be used.
284 * @param symbol Constant to indicate a number format symbol.
285 * @param value value of the format symbol
286 * @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
287 * The default behavior is to automatically set 1-9 if zero is being set and the value
288 * it is being set to corresponds to a known Unicode zero digit.
291 void setSymbol(ENumberFormatSymbol symbol
, const UnicodeString
&value
, const UBool propogateDigits
);
294 * Returns the locale for which this object was constructed.
297 inline Locale
getLocale() const;
300 * Returns the locale for this object. Two flavors are available:
301 * valid and actual locale.
304 Locale
getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const;
307 * Get pattern string for 'CurrencySpacing' that can be applied to
309 * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
310 * be empty if there is no data from current locale and its parent locales.
312 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
313 * @param beforeCurrency : true if the pattern is for before currency symbol.
314 * false if the pattern is for after currency symbol.
315 * @param status: Input/output parameter, set to success or
316 * failure code upon return.
317 * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
318 * Return empty string if there is no data for this locale and its parent
322 const UnicodeString
& getPatternForCurrencySpacing(UCurrencySpacing type
,
323 UBool beforeCurrency
,
324 UErrorCode
& status
) const;
326 * Set pattern string for 'CurrencySpacing' that can be applied to
329 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
330 * @param beforeCurrency : true if the pattern is for before currency symbol.
331 * false if the pattern is for after currency symbol.
332 * @param pattern : pattern string to override current setting.
335 void setPatternForCurrencySpacing(UCurrencySpacing type
,
336 UBool beforeCurrency
,
337 const UnicodeString
& pattern
);
340 * ICU "poor man's RTTI", returns a UClassID for the actual class.
344 virtual UClassID
getDynamicClassID() const;
347 * ICU "poor man's RTTI", returns a UClassID for this class.
351 static UClassID U_EXPORT2
getStaticClassID();
354 DecimalFormatSymbols();
357 * Initializes the symbols from the LocaleElements resource bundle.
358 * Note: The organization of LocaleElements badly needs to be
361 * @param locale The locale to get symbols for.
362 * @param success Input/output parameter, set to success or
363 * failure code upon return.
364 * @param useLastResortData determine if use last resort data
365 * @param ns The NumberingSystem to use; otherwise, fall
366 * back to the locale.
368 void initialize(const Locale
& locale
, UErrorCode
& success
,
369 UBool useLastResortData
= FALSE
, const NumberingSystem
* ns
= nullptr);
372 * Initialize the symbols with default values.
376 void setCurrencyForSymbols();
380 #ifndef U_HIDE_INTERNAL_API
382 * @internal For ICU use only
384 inline UBool
isCustomCurrencySymbol() const {
385 return fIsCustomCurrencySymbol
;
389 * @internal For ICU use only
391 inline UBool
isCustomIntlCurrencySymbol() const {
392 return fIsCustomIntlCurrencySymbol
;
396 * @internal For ICU use only
398 inline UChar32
getCodePointZero() const {
399 return fCodePointZero
;
401 #endif /* U_HIDE_INTERNAL_API */
404 * _Internal_ function - more efficient version of getSymbol,
405 * returning a const reference to one of the symbol strings.
406 * The returned reference becomes invalid when the symbol is changed
407 * or when the DecimalFormatSymbols are destroyed.
408 * Note: moved \#ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
410 * This is not currently stable API, but if you think it should be stable,
411 * post a comment on the following ticket and the ICU team will take a look:
412 * http://bugs.icu-project.org/trac/ticket/13580
414 * @param symbol Constant to indicate a number format symbol.
415 * @return the format symbol by the param 'symbol'
418 inline const UnicodeString
& getConstSymbol(ENumberFormatSymbol symbol
) const;
420 #ifndef U_HIDE_INTERNAL_API
422 * Returns the const UnicodeString reference, like getConstSymbol,
423 * corresponding to the digit with the given value. This is equivalent
424 * to accessing the symbol from getConstSymbol with the corresponding
425 * key, such as kZeroDigitSymbol or kOneDigitSymbol.
427 * This is not currently stable API, but if you think it should be stable,
428 * post a comment on the following ticket and the ICU team will take a look:
429 * http://bugs.icu-project.org/trac/ticket/13580
431 * @param digit The digit, an integer between 0 and 9 inclusive.
432 * If outside the range 0 to 9, the zero digit is returned.
433 * @return the format symbol for the given digit.
434 * @internal This API is currently for ICU use only.
436 inline const UnicodeString
& getConstDigitSymbol(int32_t digit
) const;
439 * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
442 inline const char16_t* getCurrencyPattern(void) const;
445 * Returns the name of the numbering system used for this object.
448 inline const char* getNSName() const;
450 #endif /* U_HIDE_INTERNAL_API */
454 * Private symbol strings.
455 * They are either loaded from a resource bundle or otherwise owned.
456 * setSymbol() clones the symbol string.
457 * Readonly aliases can only come from a resource bundle, so that we can always
458 * use fastCopyFrom() with them.
460 * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
461 * from private to protected,
462 * or when fSymbols can be set any other way that allows them to be readonly aliases
463 * to non-resource bundle strings,
464 * then regular UnicodeString copies must be used instead of fastCopyFrom().
468 UnicodeString fSymbols
[kFormatSymbolCount
];
471 * Non-symbol variable for getConstSymbol(). Always empty.
474 UnicodeString fNoSymbol
;
477 * Dealing with code points is faster than dealing with strings when formatting. Because of
478 * this, we maintain a value containing the zero code point that is used whenever digitStrings
479 * represents a sequence of ten code points in order.
481 * <p>If the value stored here is positive, it means that the code point stored in this value
482 * corresponds to the digitStrings array, and codePointZero can be used instead of the
483 * digitStrings array for the purposes of efficient formatting; if -1, then digitStrings does
484 * *not* contain a sequence of code points, and it must be used directly.
486 * <p>It is assumed that codePointZero always shadows the value in digitStrings. codePointZero
487 * should never be set directly; rather, it should be updated only when digitStrings mutates.
488 * That is, the flow of information is digitStrings -> codePointZero, not the other way.
490 UChar32 fCodePointZero
;
494 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
495 char validLocale
[ULOC_FULLNAME_CAPACITY
];
496 const char16_t* currPattern
;
498 UnicodeString currencySpcBeforeSym
[UNUM_CURRENCY_SPACING_COUNT
];
499 UnicodeString currencySpcAfterSym
[UNUM_CURRENCY_SPACING_COUNT
];
500 UBool fIsCustomCurrencySymbol
;
501 UBool fIsCustomIntlCurrencySymbol
;
502 char fNSName
[9]; // Apple rdar://51672521
505 // -------------------------------------
508 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol
) const {
509 const UnicodeString
*strPtr
;
510 if(symbol
< kFormatSymbolCount
) {
511 strPtr
= &fSymbols
[symbol
];
518 // See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API
519 inline const UnicodeString
&
520 DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol
) const {
521 const UnicodeString
*strPtr
;
522 if(symbol
< kFormatSymbolCount
) {
523 strPtr
= &fSymbols
[symbol
];
530 #ifndef U_HIDE_INTERNAL_API
531 inline const UnicodeString
& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit
) const {
532 if (digit
< 0 || digit
> 9) {
536 return fSymbols
[kZeroDigitSymbol
];
538 ENumberFormatSymbol key
= static_cast<ENumberFormatSymbol
>(kOneDigitSymbol
+ digit
- 1);
539 return fSymbols
[key
];
541 #endif /* U_HIDE_INTERNAL_API */
543 // -------------------------------------
546 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol
, const UnicodeString
&value
, const UBool propogateDigits
= TRUE
) {
547 if (symbol
== kCurrencySymbol
) {
548 fIsCustomCurrencySymbol
= TRUE
;
550 else if (symbol
== kIntlCurrencySymbol
) {
551 fIsCustomIntlCurrencySymbol
= TRUE
;
553 if(symbol
<kFormatSymbolCount
) {
554 fSymbols
[symbol
]=value
;
557 // If the zero digit is being set to a known zero digit according to Unicode,
558 // then we automatically set the corresponding 1-9 digits
559 // Also record updates to fCodePointZero. Be conservative if in doubt.
560 if (symbol
== kZeroDigitSymbol
) {
561 UChar32 sym
= value
.char32At(0);
562 if ( propogateDigits
&& u_charDigitValue(sym
) == 0 && value
.countChar32() == 1 ) {
563 fCodePointZero
= sym
;
564 for ( int8_t i
= 1 ; i
<= 9 ; i
++ ) {
566 fSymbols
[(int)kOneDigitSymbol
+i
-1] = UnicodeString(sym
);
571 } else if (symbol
>= kOneDigitSymbol
&& symbol
<= kNineDigitSymbol
) {
576 // -------------------------------------
579 DecimalFormatSymbols::getLocale() const {
583 #ifndef U_HIDE_INTERNAL_API
584 inline const char16_t*
585 DecimalFormatSymbols::getCurrencyPattern() const {
590 DecimalFormatSymbols::getNSName() const {
593 #endif /* U_HIDE_INTERNAL_API */
596 #endif // U_SHOW_CPLUSPLUS_API
598 #endif /* #if !UCONFIG_NO_FORMATTING */