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/numsys.h"
38 #include "unicode/unum.h"
39 #include "unicode/unistr.h"
40
41 /**
42 * \file
43 * \brief C++ API: Symbols for formatting numbers.
44 */
45
46
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 */
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,
123 /** Significant digit symbol
124 * @stable ICU 3.0 */
125 kSignificantDigitSymbol,
126 /** The monetary grouping separator
127 * @stable ICU 3.6
128 */
129 kMonetaryGroupingSeparatorSymbol,
130 /** One
131 * @stable ICU 4.6
132 */
133 kOneDigitSymbol,
134 /** Two
135 * @stable ICU 4.6
136 */
137 kTwoDigitSymbol,
138 /** Three
139 * @stable ICU 4.6
140 */
141 kThreeDigitSymbol,
142 /** Four
143 * @stable ICU 4.6
144 */
145 kFourDigitSymbol,
146 /** Five
147 * @stable ICU 4.6
148 */
149 kFiveDigitSymbol,
150 /** Six
151 * @stable ICU 4.6
152 */
153 kSixDigitSymbol,
154 /** Seven
155 * @stable ICU 4.6
156 */
157 kSevenDigitSymbol,
158 /** Eight
159 * @stable ICU 4.6
160 */
161 kEightDigitSymbol,
162 /** Nine
163 * @stable ICU 4.6
164 */
165 kNineDigitSymbol,
166 /** Multiplication sign.
167 * @stable ICU 54
168 */
169 kExponentMultiplicationSymbol,
170 /** count symbol constants */
171 kFormatSymbolCount = kNineDigitSymbol + 2
172 };
173
174 /**
175 * Create a DecimalFormatSymbols object for the given locale.
176 *
177 * @param locale The locale to get symbols for.
178 * @param status Input/output parameter, set to success or
179 * failure code upon return.
180 * @stable ICU 2.0
181 */
182 DecimalFormatSymbols(const Locale& locale, UErrorCode& status);
183
184 /**
185 * Creates a DecimalFormatSymbols instance for the given locale with digits and symbols
186 * corresponding to the given NumberingSystem.
187 *
188 * This constructor behaves equivalently to the normal constructor called with a locale having a
189 * "numbers=xxxx" keyword specifying the numbering system by name.
190 *
191 * In this constructor, the NumberingSystem argument will be used even if the locale has its own
192 * "numbers=xxxx" keyword.
193 *
194 * @param locale The locale to get symbols for.
195 * @param ns The numbering system.
196 * @param status Input/output parameter, set to success or
197 * failure code upon return.
198 * @stable ICU 60
199 */
200 DecimalFormatSymbols(const Locale& locale, const NumberingSystem& ns, UErrorCode& status);
201
202 /**
203 * Create a DecimalFormatSymbols object for the default locale.
204 * This constructor will not fail. If the resource file data is
205 * not available, it will use hard-coded last-resort data and
206 * set status to U_USING_FALLBACK_ERROR.
207 *
208 * @param status Input/output parameter, set to success or
209 * failure code upon return.
210 * @stable ICU 2.0
211 */
212 DecimalFormatSymbols(UErrorCode& status);
213
214 /**
215 * Creates a DecimalFormatSymbols object with last-resort data.
216 * Intended for callers who cache the symbols data and
217 * set all symbols on the resulting object.
218 *
219 * The last-resort symbols are similar to those for the root data,
220 * except that the grouping separators are empty,
221 * the NaN symbol is U+FFFD rather than "NaN",
222 * and the CurrencySpacing patterns are empty.
223 *
224 * @param status Input/output parameter, set to success or
225 * failure code upon return.
226 * @return last-resort symbols
227 * @stable ICU 52
228 */
229 static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
230
231 /**
232 * Copy constructor.
233 * @stable ICU 2.0
234 */
235 DecimalFormatSymbols(const DecimalFormatSymbols&);
236
237 /**
238 * Assignment operator.
239 * @stable ICU 2.0
240 */
241 DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
242
243 /**
244 * Destructor.
245 * @stable ICU 2.0
246 */
247 virtual ~DecimalFormatSymbols();
248
249 /**
250 * Return true if another object is semantically equal to this one.
251 *
252 * @param other the object to be compared with.
253 * @return true if another object is semantically equal to this one.
254 * @stable ICU 2.0
255 */
256 UBool operator==(const DecimalFormatSymbols& other) const;
257
258 /**
259 * Return true if another object is semantically unequal to this one.
260 *
261 * @param other the object to be compared with.
262 * @return true if another object is semantically unequal to this one.
263 * @stable ICU 2.0
264 */
265 UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
266
267 /**
268 * Get one of the format symbols by its enum constant.
269 * Each symbol is stored as a string so that graphemes
270 * (characters with modifier letters) can be used.
271 *
272 * @param symbol Constant to indicate a number format symbol.
273 * @return the format symbols by the param 'symbol'
274 * @stable ICU 2.0
275 */
276 inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
277
278 /**
279 * Set one of the format symbols by its enum constant.
280 * Each symbol is stored as a string so that graphemes
281 * (characters with modifier letters) can be used.
282 *
283 * @param symbol Constant to indicate a number format symbol.
284 * @param value value of the format symbol
285 * @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
286 * The default behavior is to automatically set 1-9 if zero is being set and the value
287 * it is being set to corresponds to a known Unicode zero digit.
288 * @stable ICU 2.0
289 */
290 void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
291
292 /**
293 * Returns the locale for which this object was constructed.
294 * @stable ICU 2.6
295 */
296 inline Locale getLocale() const;
297
298 /**
299 * Returns the locale for this object. Two flavors are available:
300 * valid and actual locale.
301 * @stable ICU 2.8
302 */
303 Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
304
305 /**
306 * Get pattern string for 'CurrencySpacing' that can be applied to
307 * currency format.
308 * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
309 * be empty if there is no data from current locale and its parent locales.
310 *
311 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
312 * @param beforeCurrency : true if the pattern is for before currency symbol.
313 * false if the pattern is for after currency symbol.
314 * @param status: Input/output parameter, set to success or
315 * failure code upon return.
316 * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
317 * Return empty string if there is no data for this locale and its parent
318 * locales.
319 * @stable ICU 4.8
320 */
321 const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
322 UBool beforeCurrency,
323 UErrorCode& status) const;
324 /**
325 * Set pattern string for 'CurrencySpacing' that can be applied to
326 * currency format.
327 *
328 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
329 * @param beforeCurrency : true if the pattern is for before currency symbol.
330 * false if the pattern is for after currency symbol.
331 * @param pattern : pattern string to override current setting.
332 * @stable ICU 4.8
333 */
334 void setPatternForCurrencySpacing(UCurrencySpacing type,
335 UBool beforeCurrency,
336 const UnicodeString& pattern);
337
338 /**
339 * ICU "poor man's RTTI", returns a UClassID for the actual class.
340 *
341 * @stable ICU 2.2
342 */
343 virtual UClassID getDynamicClassID() const;
344
345 /**
346 * ICU "poor man's RTTI", returns a UClassID for this class.
347 *
348 * @stable ICU 2.2
349 */
350 static UClassID U_EXPORT2 getStaticClassID();
351
352 private:
353 DecimalFormatSymbols();
354
355 /**
356 * Initializes the symbols from the LocaleElements resource bundle.
357 * Note: The organization of LocaleElements badly needs to be
358 * cleaned up.
359 *
360 * @param locale The locale to get symbols for.
361 * @param success Input/output parameter, set to success or
362 * failure code upon return.
363 * @param useLastResortData determine if use last resort data
364 * @param ns The NumberingSystem to use; otherwise, fall
365 * back to the locale.
366 */
367 void initialize(const Locale& locale, UErrorCode& success,
368 UBool useLastResortData = FALSE, const NumberingSystem* ns = nullptr);
369
370 /**
371 * Initialize the symbols with default values.
372 */
373 void initialize();
374
375 void setCurrencyForSymbols();
376
377 public:
378
379 #ifndef U_HIDE_INTERNAL_API
380 /**
381 * @internal For ICU use only
382 */
isCustomCurrencySymbol()383 inline UBool isCustomCurrencySymbol() const {
384 return fIsCustomCurrencySymbol;
385 }
386
387 /**
388 * @internal For ICU use only
389 */
isCustomIntlCurrencySymbol()390 inline UBool isCustomIntlCurrencySymbol() const {
391 return fIsCustomIntlCurrencySymbol;
392 }
393
394 /**
395 * @internal For ICU use only
396 */
getCodePointZero()397 inline UChar32 getCodePointZero() const {
398 return fCodePointZero;
399 }
400 #endif /* U_HIDE_INTERNAL_API */
401
402 /**
403 * _Internal_ function - more efficient version of getSymbol,
404 * returning a const reference to one of the symbol strings.
405 * The returned reference becomes invalid when the symbol is changed
406 * or when the DecimalFormatSymbols are destroyed.
407 * Note: moved \#ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
408 *
409 * This is not currently stable API, but if you think it should be stable,
410 * post a comment on the following ticket and the ICU team will take a look:
411 * http://bugs.icu-project.org/trac/ticket/13580
412 *
413 * @param symbol Constant to indicate a number format symbol.
414 * @return the format symbol by the param 'symbol'
415 * @internal
416 */
417 inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const;
418
419 #ifndef U_HIDE_INTERNAL_API
420 /**
421 * Returns the const UnicodeString reference, like getConstSymbol,
422 * corresponding to the digit with the given value. This is equivalent
423 * to accessing the symbol from getConstSymbol with the corresponding
424 * key, such as kZeroDigitSymbol or kOneDigitSymbol.
425 *
426 * This is not currently stable API, but if you think it should be stable,
427 * post a comment on the following ticket and the ICU team will take a look:
428 * http://bugs.icu-project.org/trac/ticket/13580
429 *
430 * @param digit The digit, an integer between 0 and 9 inclusive.
431 * If outside the range 0 to 9, the zero digit is returned.
432 * @return the format symbol for the given digit.
433 * @internal This API is currently for ICU use only.
434 */
435 inline const UnicodeString& getConstDigitSymbol(int32_t digit) const;
436
437 /**
438 * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
439 * @internal
440 */
441 inline const char16_t* getCurrencyPattern(void) const;
442 #endif /* U_HIDE_INTERNAL_API */
443
444 private:
445 /**
446 * Private symbol strings.
447 * They are either loaded from a resource bundle or otherwise owned.
448 * setSymbol() clones the symbol string.
449 * Readonly aliases can only come from a resource bundle, so that we can always
450 * use fastCopyFrom() with them.
451 *
452 * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
453 * from private to protected,
454 * or when fSymbols can be set any other way that allows them to be readonly aliases
455 * to non-resource bundle strings,
456 * then regular UnicodeString copies must be used instead of fastCopyFrom().
457 *
458 * @internal
459 */
460 UnicodeString fSymbols[kFormatSymbolCount];
461
462 /**
463 * Non-symbol variable for getConstSymbol(). Always empty.
464 * @internal
465 */
466 UnicodeString fNoSymbol;
467
468 /**
469 * Dealing with code points is faster than dealing with strings when formatting. Because of
470 * this, we maintain a value containing the zero code point that is used whenever digitStrings
471 * represents a sequence of ten code points in order.
472 *
473 * <p>If the value stored here is positive, it means that the code point stored in this value
474 * corresponds to the digitStrings array, and codePointZero can be used instead of the
475 * digitStrings array for the purposes of efficient formatting; if -1, then digitStrings does
476 * *not* contain a sequence of code points, and it must be used directly.
477 *
478 * <p>It is assumed that codePointZero always shadows the value in digitStrings. codePointZero
479 * should never be set directly; rather, it should be updated only when digitStrings mutates.
480 * That is, the flow of information is digitStrings -> codePointZero, not the other way.
481 */
482 UChar32 fCodePointZero;
483
484 Locale locale;
485
486 char actualLocale[ULOC_FULLNAME_CAPACITY];
487 char validLocale[ULOC_FULLNAME_CAPACITY];
488 const char16_t* currPattern;
489
490 UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
491 UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
492 UBool fIsCustomCurrencySymbol;
493 UBool fIsCustomIntlCurrencySymbol;
494 };
495
496 // -------------------------------------
497
498 inline UnicodeString
getSymbol(ENumberFormatSymbol symbol)499 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
500 const UnicodeString *strPtr;
501 if(symbol < kFormatSymbolCount) {
502 strPtr = &fSymbols[symbol];
503 } else {
504 strPtr = &fNoSymbol;
505 }
506 return *strPtr;
507 }
508
509 // See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API
510 inline const UnicodeString &
getConstSymbol(ENumberFormatSymbol symbol)511 DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
512 const UnicodeString *strPtr;
513 if(symbol < kFormatSymbolCount) {
514 strPtr = &fSymbols[symbol];
515 } else {
516 strPtr = &fNoSymbol;
517 }
518 return *strPtr;
519 }
520
521 #ifndef U_HIDE_INTERNAL_API
getConstDigitSymbol(int32_t digit)522 inline const UnicodeString& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit) const {
523 if (digit < 0 || digit > 9) {
524 digit = 0;
525 }
526 if (digit == 0) {
527 return fSymbols[kZeroDigitSymbol];
528 }
529 ENumberFormatSymbol key = static_cast<ENumberFormatSymbol>(kOneDigitSymbol + digit - 1);
530 return fSymbols[key];
531 }
532 #endif /* U_HIDE_INTERNAL_API */
533
534 // -------------------------------------
535
536 inline void
537 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
538 if (symbol == kCurrencySymbol) {
539 fIsCustomCurrencySymbol = TRUE;
540 }
541 else if (symbol == kIntlCurrencySymbol) {
542 fIsCustomIntlCurrencySymbol = TRUE;
543 }
544 if(symbol<kFormatSymbolCount) {
545 fSymbols[symbol]=value;
546 }
547
548 // If the zero digit is being set to a known zero digit according to Unicode,
549 // then we automatically set the corresponding 1-9 digits
550 // Also record updates to fCodePointZero. Be conservative if in doubt.
551 if (symbol == kZeroDigitSymbol) {
552 UChar32 sym = value.char32At(0);
553 if ( propogateDigits && u_charDigitValue(sym) == 0 && value.countChar32() == 1 ) {
554 fCodePointZero = sym;
555 for ( int8_t i = 1 ; i<= 9 ; i++ ) {
556 sym++;
557 fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
558 }
559 } else {
560 fCodePointZero = -1;
561 }
562 } else if (symbol >= kOneDigitSymbol && symbol <= kNineDigitSymbol) {
563 fCodePointZero = -1;
564 }
565 }
566
567 // -------------------------------------
568
569 inline Locale
getLocale()570 DecimalFormatSymbols::getLocale() const {
571 return locale;
572 }
573
574 #ifndef U_HIDE_INTERNAL_API
575 inline const char16_t*
getCurrencyPattern()576 DecimalFormatSymbols::getCurrencyPattern() const {
577 return currPattern;
578 }
579 #endif /* U_HIDE_INTERNAL_API */
580
581 U_NAMESPACE_END
582
583 #endif /* #if !UCONFIG_NO_FORMATTING */
584
585 #endif // _DCFMTSYM
586 //eof
587