]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/dcfmtsym.h
ICU-66108.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / dcfmtsym.h
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4********************************************************************************
2ca993e8 5* Copyright (C) 1997-2016, International Business Machines
b75a7d8f
A
6* Corporation and others. All Rights Reserved.
7********************************************************************************
8*
9* File DCFMTSYM.H
10*
11* Modification History:
729e4ab9 12*
b75a7d8f
A
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.
729e4ab9 18* 07/22/98 stephen Changed to match C++ style
b75a7d8f
A
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*/
729e4ab9 26
b75a7d8f
A
27#ifndef DCFMTSYM_H
28#define DCFMTSYM_H
729e4ab9 29
b75a7d8f 30#include "unicode/utypes.h"
340931cb
A
31
32#if U_SHOW_CPLUSPLUS_API
b75a7d8f
A
33
34#if !UCONFIG_NO_FORMATTING
35
340931cb 36#include "unicode/uchar.h"
b75a7d8f
A
37#include "unicode/uobject.h"
38#include "unicode/locid.h"
0f5d89e8 39#include "unicode/numsys.h"
4388f060 40#include "unicode/unum.h"
f3c0d7a5 41#include "unicode/unistr.h"
b75a7d8f 42
73c04bcf 43/**
729e4ab9 44 * \file
73c04bcf
A
45 * \brief C++ API: Symbols for formatting numbers.
46 */
47
48
b75a7d8f
A
49U_NAMESPACE_BEGIN
50
51/**
52 * This class represents the set of symbols needed by DecimalFormat
53 * to format numbers. DecimalFormat creates for itself an instance of
54 * DecimalFormatSymbols from its locale data. If you need to change any
55 * of these symbols, you can get the DecimalFormatSymbols object from
56 * your DecimalFormat and modify it.
57 * <P>
58 * Here are the special characters used in the parts of the
59 * subpattern, with notes on their usage.
60 * <pre>
61 * \code
62 * Symbol Meaning
63 * 0 a digit
64 * # a digit, zero shows as absent
65 * . placeholder for decimal separator
66 * , placeholder for grouping separator.
67 * ; separates formats.
68 * - default negative prefix.
69 * % divide by 100 and show as percentage
70 * X any other characters can be used in the prefix or suffix
71 * ' used to quote special characters in a prefix or suffix.
72 * \endcode
73 * </pre>
74 * [Notes]
75 * <P>
76 * If there is no explicit negative subpattern, - is prefixed to the
77 * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
78 * <P>
79 * The grouping separator is commonly used for thousands, but in some
80 * countries for ten-thousands. The interval is a constant number of
81 * digits between the grouping characters, such as 100,000,000 or 1,0000,0000.
82 * If you supply a pattern with multiple grouping characters, the interval
83 * between the last one and the end of the integer is the one that is
84 * used. So "#,##,###,####" == "######,####" == "##,####,####".
b75a7d8f
A
85 */
86class U_I18N_API DecimalFormatSymbols : public UObject {
87public:
88 /**
89 * Constants for specifying a number format symbol.
90 * @stable ICU 2.0
91 */
92 enum ENumberFormatSymbol {
93 /** The decimal separator */
94 kDecimalSeparatorSymbol,
95 /** The grouping separator */
96 kGroupingSeparatorSymbol,
97 /** The pattern separator */
98 kPatternSeparatorSymbol,
99 /** The percent sign */
100 kPercentSymbol,
101 /** Zero*/
102 kZeroDigitSymbol,
103 /** Character representing a digit in the pattern */
104 kDigitSymbol,
105 /** The minus sign */
106 kMinusSignSymbol,
107 /** The plus sign */
108 kPlusSignSymbol,
109 /** The currency symbol */
110 kCurrencySymbol,
111 /** The international currency symbol */
112 kIntlCurrencySymbol,
113 /** The monetary separator */
114 kMonetarySeparatorSymbol,
115 /** The exponential symbol */
116 kExponentialSymbol,
117 /** Per mill symbol - replaces kPermillSymbol */
118 kPerMillSymbol,
119 /** Escape padding character */
120 kPadEscapeSymbol,
121 /** Infinity symbol */
122 kInfinitySymbol,
123 /** Nan symbol */
124 kNaNSymbol,
374ca955 125 /** Significant digit symbol
73c04bcf 126 * @stable ICU 3.0 */
374ca955 127 kSignificantDigitSymbol,
729e4ab9 128 /** The monetary grouping separator
46f4442e 129 * @stable ICU 3.6
73c04bcf
A
130 */
131 kMonetaryGroupingSeparatorSymbol,
729e4ab9 132 /** One
4388f060 133 * @stable ICU 4.6
729e4ab9
A
134 */
135 kOneDigitSymbol,
136 /** Two
4388f060 137 * @stable ICU 4.6
729e4ab9
A
138 */
139 kTwoDigitSymbol,
140 /** Three
4388f060 141 * @stable ICU 4.6
729e4ab9
A
142 */
143 kThreeDigitSymbol,
144 /** Four
4388f060 145 * @stable ICU 4.6
729e4ab9
A
146 */
147 kFourDigitSymbol,
148 /** Five
4388f060 149 * @stable ICU 4.6
729e4ab9
A
150 */
151 kFiveDigitSymbol,
152 /** Six
4388f060 153 * @stable ICU 4.6
729e4ab9
A
154 */
155 kSixDigitSymbol,
156 /** Seven
4388f060 157 * @stable ICU 4.6
729e4ab9
A
158 */
159 kSevenDigitSymbol,
160 /** Eight
4388f060 161 * @stable ICU 4.6
729e4ab9
A
162 */
163 kEightDigitSymbol,
164 /** Nine
4388f060 165 * @stable ICU 4.6
729e4ab9
A
166 */
167 kNineDigitSymbol,
b331163b 168 /** Multiplication sign.
2ca993e8 169 * @stable ICU 54
b331163b
A
170 */
171 kExponentMultiplicationSymbol,
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
0f5d89e8
A
186 /**
187 * Creates a DecimalFormatSymbols instance for the given locale with digits and symbols
188 * corresponding to the given NumberingSystem.
189 *
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.
192 *
193 * In this constructor, the NumberingSystem argument will be used even if the locale has its own
194 * "numbers=xxxx" keyword.
195 *
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.
3d1f044b 200 * @stable ICU 60
0f5d89e8
A
201 */
202 DecimalFormatSymbols(const Locale& locale, const NumberingSystem& ns, UErrorCode& status);
0f5d89e8 203
b75a7d8f
A
204 /**
205 * Create a DecimalFormatSymbols object for the default locale.
206 * This constructor will not fail. If the resource file data is
207 * not available, it will use hard-coded last-resort data and
208 * set status to U_USING_FALLBACK_ERROR.
209 *
210 * @param status Input/output parameter, set to success or
211 * failure code upon return.
212 * @stable ICU 2.0
213 */
57a6839d
A
214 DecimalFormatSymbols(UErrorCode& status);
215
57a6839d
A
216 /**
217 * Creates a DecimalFormatSymbols object with last-resort data.
218 * Intended for callers who cache the symbols data and
219 * set all symbols on the resulting object.
220 *
221 * The last-resort symbols are similar to those for the root data,
222 * except that the grouping separators are empty,
223 * the NaN symbol is U+FFFD rather than "NaN",
224 * and the CurrencySpacing patterns are empty.
225 *
226 * @param status Input/output parameter, set to success or
227 * failure code upon return.
228 * @return last-resort symbols
b331163b 229 * @stable ICU 52
57a6839d
A
230 */
231 static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
b75a7d8f
A
232
233 /**
234 * Copy constructor.
235 * @stable ICU 2.0
236 */
237 DecimalFormatSymbols(const DecimalFormatSymbols&);
238
239 /**
240 * Assignment operator.
241 * @stable ICU 2.0
242 */
243 DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
244
245 /**
246 * Destructor.
247 * @stable ICU 2.0
248 */
374ca955 249 virtual ~DecimalFormatSymbols();
b75a7d8f
A
250
251 /**
252 * Return true if another object is semantically equal to this one.
253 *
254 * @param other the object to be compared with.
255 * @return true if another object is semantically equal to this one.
256 * @stable ICU 2.0
257 */
258 UBool operator==(const DecimalFormatSymbols& other) const;
259
260 /**
261 * Return true if another object is semantically unequal to this one.
262 *
263 * @param other the object to be compared with.
264 * @return true if another object is semantically unequal to this one.
265 * @stable ICU 2.0
266 */
267 UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
268
269 /**
270 * Get one of the format symbols by its enum constant.
271 * Each symbol is stored as a string so that graphemes
729e4ab9 272 * (characters with modifier letters) can be used.
b75a7d8f
A
273 *
274 * @param symbol Constant to indicate a number format symbol.
275 * @return the format symbols by the param 'symbol'
276 * @stable ICU 2.0
277 */
374ca955 278 inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
b75a7d8f
A
279
280 /**
281 * Set one of the format symbols by its enum constant.
282 * Each symbol is stored as a string so that graphemes
729e4ab9 283 * (characters with modifier letters) can be used.
b75a7d8f
A
284 *
285 * @param symbol Constant to indicate a number format symbol.
729e4ab9
A
286 * @param value value of the format symbol
287 * @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
288 * The default behavior is to automatically set 1-9 if zero is being set and the value
289 * it is being set to corresponds to a known Unicode zero digit.
b75a7d8f
A
290 * @stable ICU 2.0
291 */
729e4ab9 292 void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
b75a7d8f
A
293
294 /**
295 * Returns the locale for which this object was constructed.
296 * @stable ICU 2.6
297 */
298 inline Locale getLocale() const;
299
374ca955
A
300 /**
301 * Returns the locale for this object. Two flavors are available:
302 * valid and actual locale.
73c04bcf 303 * @stable ICU 2.8
374ca955
A
304 */
305 Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
306
729e4ab9
A
307 /**
308 * Get pattern string for 'CurrencySpacing' that can be applied to
309 * currency format.
310 * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
311 * be empty if there is no data from current locale and its parent locales.
312 *
4388f060 313 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
729e4ab9
A
314 * @param beforeCurrency : true if the pattern is for before currency symbol.
315 * false if the pattern is for after currency symbol.
316 * @param status: Input/output parameter, set to success or
317 * failure code upon return.
318 * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
319 * Return empty string if there is no data for this locale and its parent
320 * locales.
4388f060 321 * @stable ICU 4.8
729e4ab9 322 */
4388f060 323 const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
729e4ab9
A
324 UBool beforeCurrency,
325 UErrorCode& status) const;
326 /**
327 * Set pattern string for 'CurrencySpacing' that can be applied to
328 * currency format.
329 *
4388f060 330 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
729e4ab9
A
331 * @param beforeCurrency : true if the pattern is for before currency symbol.
332 * false if the pattern is for after currency symbol.
333 * @param pattern : pattern string to override current setting.
4388f060 334 * @stable ICU 4.8
729e4ab9 335 */
4388f060 336 void setPatternForCurrencySpacing(UCurrencySpacing type,
729e4ab9
A
337 UBool beforeCurrency,
338 const UnicodeString& pattern);
339
b75a7d8f
A
340 /**
341 * ICU "poor man's RTTI", returns a UClassID for the actual class.
342 *
374ca955 343 * @stable ICU 2.2
b75a7d8f 344 */
374ca955 345 virtual UClassID getDynamicClassID() const;
b75a7d8f
A
346
347 /**
348 * ICU "poor man's RTTI", returns a UClassID for this class.
349 *
374ca955 350 * @stable ICU 2.2
b75a7d8f 351 */
374ca955 352 static UClassID U_EXPORT2 getStaticClassID();
b75a7d8f
A
353
354private:
57a6839d 355 DecimalFormatSymbols();
b75a7d8f
A
356
357 /**
358 * Initializes the symbols from the LocaleElements resource bundle.
359 * Note: The organization of LocaleElements badly needs to be
360 * cleaned up.
361 *
362 * @param locale The locale to get symbols for.
363 * @param success Input/output parameter, set to success or
364 * failure code upon return.
365 * @param useLastResortData determine if use last resort data
0f5d89e8
A
366 * @param ns The NumberingSystem to use; otherwise, fall
367 * back to the locale.
b75a7d8f 368 */
0f5d89e8
A
369 void initialize(const Locale& locale, UErrorCode& success,
370 UBool useLastResortData = FALSE, const NumberingSystem* ns = nullptr);
b75a7d8f 371
b75a7d8f
A
372 /**
373 * Initialize the symbols with default values.
374 */
375 void initialize();
376
377 void setCurrencyForSymbols();
378
379public:
2ca993e8
A
380
381#ifndef U_HIDE_INTERNAL_API
382 /**
383 * @internal For ICU use only
384 */
385 inline UBool isCustomCurrencySymbol() const {
386 return fIsCustomCurrencySymbol;
387 }
388
389 /**
390 * @internal For ICU use only
391 */
392 inline UBool isCustomIntlCurrencySymbol() const {
393 return fIsCustomIntlCurrencySymbol;
394 }
0f5d89e8
A
395
396 /**
397 * @internal For ICU use only
398 */
399 inline UChar32 getCodePointZero() const {
400 return fCodePointZero;
401 }
2ca993e8
A
402#endif /* U_HIDE_INTERNAL_API */
403
b75a7d8f
A
404 /**
405 * _Internal_ function - more efficient version of getSymbol,
406 * returning a const reference to one of the symbol strings.
407 * The returned reference becomes invalid when the symbol is changed
408 * or when the DecimalFormatSymbols are destroyed.
3d1f044b 409 * Note: moved \#ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
b75a7d8f 410 *
0f5d89e8
A
411 * This is not currently stable API, but if you think it should be stable,
412 * post a comment on the following ticket and the ICU team will take a look:
413 * http://bugs.icu-project.org/trac/ticket/13580
414 *
b75a7d8f
A
415 * @param symbol Constant to indicate a number format symbol.
416 * @return the format symbol by the param 'symbol'
417 * @internal
418 */
0f5d89e8 419 inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const;
b75a7d8f 420
57a6839d 421#ifndef U_HIDE_INTERNAL_API
0f5d89e8
A
422 /**
423 * Returns the const UnicodeString reference, like getConstSymbol,
424 * corresponding to the digit with the given value. This is equivalent
425 * to accessing the symbol from getConstSymbol with the corresponding
426 * key, such as kZeroDigitSymbol or kOneDigitSymbol.
427 *
428 * This is not currently stable API, but if you think it should be stable,
429 * post a comment on the following ticket and the ICU team will take a look:
430 * http://bugs.icu-project.org/trac/ticket/13580
431 *
432 * @param digit The digit, an integer between 0 and 9 inclusive.
433 * If outside the range 0 to 9, the zero digit is returned.
434 * @return the format symbol for the given digit.
435 * @internal This API is currently for ICU use only.
436 */
437 inline const UnicodeString& getConstDigitSymbol(int32_t digit) const;
438
73c04bcf
A
439 /**
440 * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
441 * @internal
442 */
f3c0d7a5 443 inline const char16_t* getCurrencyPattern(void) const;
3d1f044b
A
444
445 /**
446 * Returns the name of the numbering system used for this object.
447 * @internal
448 */
449 inline const char* getNSName() const;
450
4388f060 451#endif /* U_HIDE_INTERNAL_API */
73c04bcf 452
b75a7d8f
A
453private:
454 /**
455 * Private symbol strings.
456 * They are either loaded from a resource bundle or otherwise owned.
457 * setSymbol() clones the symbol string.
458 * Readonly aliases can only come from a resource bundle, so that we can always
459 * use fastCopyFrom() with them.
460 *
461 * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
462 * from private to protected,
463 * or when fSymbols can be set any other way that allows them to be readonly aliases
464 * to non-resource bundle strings,
465 * then regular UnicodeString copies must be used instead of fastCopyFrom().
466 *
b75a7d8f
A
467 */
468 UnicodeString fSymbols[kFormatSymbolCount];
469
470 /**
471 * Non-symbol variable for getConstSymbol(). Always empty.
b75a7d8f
A
472 */
473 UnicodeString fNoSymbol;
474
0f5d89e8
A
475 /**
476 * Dealing with code points is faster than dealing with strings when formatting. Because of
477 * this, we maintain a value containing the zero code point that is used whenever digitStrings
478 * represents a sequence of ten code points in order.
479 *
480 * <p>If the value stored here is positive, it means that the code point stored in this value
481 * corresponds to the digitStrings array, and codePointZero can be used instead of the
482 * digitStrings array for the purposes of efficient formatting; if -1, then digitStrings does
483 * *not* contain a sequence of code points, and it must be used directly.
484 *
485 * <p>It is assumed that codePointZero always shadows the value in digitStrings. codePointZero
486 * should never be set directly; rather, it should be updated only when digitStrings mutates.
487 * That is, the flow of information is digitStrings -> codePointZero, not the other way.
488 */
489 UChar32 fCodePointZero;
490
b75a7d8f
A
491 Locale locale;
492
374ca955
A
493 char actualLocale[ULOC_FULLNAME_CAPACITY];
494 char validLocale[ULOC_FULLNAME_CAPACITY];
f3c0d7a5 495 const char16_t* currPattern;
729e4ab9 496
4388f060
A
497 UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
498 UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
2ca993e8
A
499 UBool fIsCustomCurrencySymbol;
500 UBool fIsCustomIntlCurrencySymbol;
3d1f044b 501 char fNSName[9]; // Apple rdar://51672521
b75a7d8f
A
502};
503
b75a7d8f
A
504// -------------------------------------
505
506inline UnicodeString
507DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
374ca955
A
508 const UnicodeString *strPtr;
509 if(symbol < kFormatSymbolCount) {
510 strPtr = &fSymbols[symbol];
b75a7d8f 511 } else {
374ca955 512 strPtr = &fNoSymbol;
b75a7d8f 513 }
374ca955 514 return *strPtr;
b75a7d8f
A
515}
516
0f5d89e8 517// See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API
b75a7d8f
A
518inline const UnicodeString &
519DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
374ca955
A
520 const UnicodeString *strPtr;
521 if(symbol < kFormatSymbolCount) {
522 strPtr = &fSymbols[symbol];
b75a7d8f 523 } else {
374ca955 524 strPtr = &fNoSymbol;
b75a7d8f 525 }
374ca955 526 return *strPtr;
b75a7d8f 527}
4388f060 528
0f5d89e8
A
529#ifndef U_HIDE_INTERNAL_API
530inline const UnicodeString& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit) const {
531 if (digit < 0 || digit > 9) {
532 digit = 0;
533 }
534 if (digit == 0) {
535 return fSymbols[kZeroDigitSymbol];
536 }
537 ENumberFormatSymbol key = static_cast<ENumberFormatSymbol>(kOneDigitSymbol + digit - 1);
538 return fSymbols[key];
539}
3d1f044b 540#endif /* U_HIDE_INTERNAL_API */
0f5d89e8 541
b75a7d8f
A
542// -------------------------------------
543
544inline void
729e4ab9 545DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
2ca993e8
A
546 if (symbol == kCurrencySymbol) {
547 fIsCustomCurrencySymbol = TRUE;
548 }
549 else if (symbol == kIntlCurrencySymbol) {
550 fIsCustomIntlCurrencySymbol = TRUE;
551 }
b75a7d8f
A
552 if(symbol<kFormatSymbolCount) {
553 fSymbols[symbol]=value;
554 }
729e4ab9
A
555
556 // If the zero digit is being set to a known zero digit according to Unicode,
557 // then we automatically set the corresponding 1-9 digits
0f5d89e8
A
558 // Also record updates to fCodePointZero. Be conservative if in doubt.
559 if (symbol == kZeroDigitSymbol) {
729e4ab9 560 UChar32 sym = value.char32At(0);
0f5d89e8
A
561 if ( propogateDigits && u_charDigitValue(sym) == 0 && value.countChar32() == 1 ) {
562 fCodePointZero = sym;
729e4ab9
A
563 for ( int8_t i = 1 ; i<= 9 ; i++ ) {
564 sym++;
565 fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
566 }
0f5d89e8
A
567 } else {
568 fCodePointZero = -1;
729e4ab9 569 }
0f5d89e8
A
570 } else if (symbol >= kOneDigitSymbol && symbol <= kNineDigitSymbol) {
571 fCodePointZero = -1;
729e4ab9 572 }
b75a7d8f
A
573}
574
575// -------------------------------------
576
577inline Locale
578DecimalFormatSymbols::getLocale() const {
579 return locale;
580}
581
57a6839d 582#ifndef U_HIDE_INTERNAL_API
f3c0d7a5 583inline const char16_t*
73c04bcf
A
584DecimalFormatSymbols::getCurrencyPattern() const {
585 return currPattern;
586}
3d1f044b
A
587
588inline const char*
589DecimalFormatSymbols::getNSName() const {
590 return fNSName;
591}
57a6839d 592#endif /* U_HIDE_INTERNAL_API */
4388f060 593
b75a7d8f
A
594U_NAMESPACE_END
595
596#endif /* #if !UCONFIG_NO_FORMATTING */
597
340931cb
A
598#endif /* U_SHOW_CPLUSPLUS_API */
599
b75a7d8f
A
600#endif // _DCFMTSYM
601//eof