]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/dcfmtsym.h
ICU-64260.0.1.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"
729e4ab9 31#include "unicode/uchar.h"
b75a7d8f
A
32
33#if !UCONFIG_NO_FORMATTING
34
35#include "unicode/uobject.h"
36#include "unicode/locid.h"
0f5d89e8 37#include "unicode/numsys.h"
4388f060 38#include "unicode/unum.h"
f3c0d7a5 39#include "unicode/unistr.h"
b75a7d8f 40
73c04bcf 41/**
729e4ab9 42 * \file
73c04bcf
A
43 * \brief C++ API: Symbols for formatting numbers.
44 */
45
46
f3c0d7a5 47#if U_SHOW_CPLUSPLUS_API
b75a7d8f
A
48U_NAMESPACE_BEGIN
49
50/**
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.
56 * <P>
57 * Here are the special characters used in the parts of the
58 * subpattern, with notes on their usage.
59 * <pre>
60 * \code
61 * Symbol Meaning
62 * 0 a digit
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.
71 * \endcode
72 * </pre>
73 * [Notes]
74 * <P>
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".
77 * <P>
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 "#,##,###,####" == "######,####" == "##,####,####".
b75a7d8f
A
84 */
85class U_I18N_API DecimalFormatSymbols : public UObject {
86public:
87 /**
88 * Constants for specifying a number format symbol.
89 * @stable ICU 2.0
90 */
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 */
99 kPercentSymbol,
100 /** Zero*/
101 kZeroDigitSymbol,
102 /** Character representing a digit in the pattern */
103 kDigitSymbol,
104 /** The minus sign */
105 kMinusSignSymbol,
106 /** The plus sign */
107 kPlusSignSymbol,
108 /** The currency symbol */
109 kCurrencySymbol,
110 /** The international currency symbol */
111 kIntlCurrencySymbol,
112 /** The monetary separator */
113 kMonetarySeparatorSymbol,
114 /** The exponential symbol */
115 kExponentialSymbol,
116 /** Per mill symbol - replaces kPermillSymbol */
117 kPerMillSymbol,
118 /** Escape padding character */
119 kPadEscapeSymbol,
120 /** Infinity symbol */
121 kInfinitySymbol,
122 /** Nan symbol */
123 kNaNSymbol,
374ca955 124 /** Significant digit symbol
73c04bcf 125 * @stable ICU 3.0 */
374ca955 126 kSignificantDigitSymbol,
729e4ab9 127 /** The monetary grouping separator
46f4442e 128 * @stable ICU 3.6
73c04bcf
A
129 */
130 kMonetaryGroupingSeparatorSymbol,
729e4ab9 131 /** One
4388f060 132 * @stable ICU 4.6
729e4ab9
A
133 */
134 kOneDigitSymbol,
135 /** Two
4388f060 136 * @stable ICU 4.6
729e4ab9
A
137 */
138 kTwoDigitSymbol,
139 /** Three
4388f060 140 * @stable ICU 4.6
729e4ab9
A
141 */
142 kThreeDigitSymbol,
143 /** Four
4388f060 144 * @stable ICU 4.6
729e4ab9
A
145 */
146 kFourDigitSymbol,
147 /** Five
4388f060 148 * @stable ICU 4.6
729e4ab9
A
149 */
150 kFiveDigitSymbol,
151 /** Six
4388f060 152 * @stable ICU 4.6
729e4ab9
A
153 */
154 kSixDigitSymbol,
155 /** Seven
4388f060 156 * @stable ICU 4.6
729e4ab9
A
157 */
158 kSevenDigitSymbol,
159 /** Eight
4388f060 160 * @stable ICU 4.6
729e4ab9
A
161 */
162 kEightDigitSymbol,
163 /** Nine
4388f060 164 * @stable ICU 4.6
729e4ab9
A
165 */
166 kNineDigitSymbol,
b331163b 167 /** Multiplication sign.
2ca993e8 168 * @stable ICU 54
b331163b
A
169 */
170 kExponentMultiplicationSymbol,
b75a7d8f 171 /** count symbol constants */
b331163b 172 kFormatSymbolCount = kNineDigitSymbol + 2
b75a7d8f
A
173 };
174
175 /**
176 * Create a DecimalFormatSymbols object for the given locale.
177 *
178 * @param locale The locale to get symbols for.
179 * @param status Input/output parameter, set to success or
180 * failure code upon return.
181 * @stable ICU 2.0
182 */
183 DecimalFormatSymbols(const Locale& locale, UErrorCode& status);
184
0f5d89e8
A
185 /**
186 * Creates a DecimalFormatSymbols instance for the given locale with digits and symbols
187 * corresponding to the given NumberingSystem.
188 *
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.
191 *
192 * In this constructor, the NumberingSystem argument will be used even if the locale has its own
193 * "numbers=xxxx" keyword.
194 *
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.
3d1f044b 199 * @stable ICU 60
0f5d89e8
A
200 */
201 DecimalFormatSymbols(const Locale& locale, const NumberingSystem& ns, UErrorCode& status);
0f5d89e8 202
b75a7d8f
A
203 /**
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.
208 *
209 * @param status Input/output parameter, set to success or
210 * failure code upon return.
211 * @stable ICU 2.0
212 */
57a6839d
A
213 DecimalFormatSymbols(UErrorCode& status);
214
57a6839d
A
215 /**
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.
219 *
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.
224 *
225 * @param status Input/output parameter, set to success or
226 * failure code upon return.
227 * @return last-resort symbols
b331163b 228 * @stable ICU 52
57a6839d
A
229 */
230 static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
b75a7d8f
A
231
232 /**
233 * Copy constructor.
234 * @stable ICU 2.0
235 */
236 DecimalFormatSymbols(const DecimalFormatSymbols&);
237
238 /**
239 * Assignment operator.
240 * @stable ICU 2.0
241 */
242 DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
243
244 /**
245 * Destructor.
246 * @stable ICU 2.0
247 */
374ca955 248 virtual ~DecimalFormatSymbols();
b75a7d8f
A
249
250 /**
251 * Return true if another object is semantically equal to this one.
252 *
253 * @param other the object to be compared with.
254 * @return true if another object is semantically equal to this one.
255 * @stable ICU 2.0
256 */
257 UBool operator==(const DecimalFormatSymbols& other) const;
258
259 /**
260 * Return true if another object is semantically unequal to this one.
261 *
262 * @param other the object to be compared with.
263 * @return true if another object is semantically unequal to this one.
264 * @stable ICU 2.0
265 */
266 UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
267
268 /**
269 * Get one of the format symbols by its enum constant.
270 * Each symbol is stored as a string so that graphemes
729e4ab9 271 * (characters with modifier letters) can be used.
b75a7d8f
A
272 *
273 * @param symbol Constant to indicate a number format symbol.
274 * @return the format symbols by the param 'symbol'
275 * @stable ICU 2.0
276 */
374ca955 277 inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
b75a7d8f
A
278
279 /**
280 * Set one of the format symbols by its enum constant.
281 * Each symbol is stored as a string so that graphemes
729e4ab9 282 * (characters with modifier letters) can be used.
b75a7d8f
A
283 *
284 * @param symbol Constant to indicate a number format symbol.
729e4ab9
A
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.
b75a7d8f
A
289 * @stable ICU 2.0
290 */
729e4ab9 291 void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
b75a7d8f
A
292
293 /**
294 * Returns the locale for which this object was constructed.
295 * @stable ICU 2.6
296 */
297 inline Locale getLocale() const;
298
374ca955
A
299 /**
300 * Returns the locale for this object. Two flavors are available:
301 * valid and actual locale.
73c04bcf 302 * @stable ICU 2.8
374ca955
A
303 */
304 Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
305
729e4ab9
A
306 /**
307 * Get pattern string for 'CurrencySpacing' that can be applied to
308 * currency format.
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.
311 *
4388f060 312 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
729e4ab9
A
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
319 * locales.
4388f060 320 * @stable ICU 4.8
729e4ab9 321 */
4388f060 322 const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
729e4ab9
A
323 UBool beforeCurrency,
324 UErrorCode& status) const;
325 /**
326 * Set pattern string for 'CurrencySpacing' that can be applied to
327 * currency format.
328 *
4388f060 329 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
729e4ab9
A
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.
4388f060 333 * @stable ICU 4.8
729e4ab9 334 */
4388f060 335 void setPatternForCurrencySpacing(UCurrencySpacing type,
729e4ab9
A
336 UBool beforeCurrency,
337 const UnicodeString& pattern);
338
b75a7d8f
A
339 /**
340 * ICU "poor man's RTTI", returns a UClassID for the actual class.
341 *
374ca955 342 * @stable ICU 2.2
b75a7d8f 343 */
374ca955 344 virtual UClassID getDynamicClassID() const;
b75a7d8f
A
345
346 /**
347 * ICU "poor man's RTTI", returns a UClassID for this class.
348 *
374ca955 349 * @stable ICU 2.2
b75a7d8f 350 */
374ca955 351 static UClassID U_EXPORT2 getStaticClassID();
b75a7d8f
A
352
353private:
57a6839d 354 DecimalFormatSymbols();
b75a7d8f
A
355
356 /**
357 * Initializes the symbols from the LocaleElements resource bundle.
358 * Note: The organization of LocaleElements badly needs to be
359 * cleaned up.
360 *
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
0f5d89e8
A
365 * @param ns The NumberingSystem to use; otherwise, fall
366 * back to the locale.
b75a7d8f 367 */
0f5d89e8
A
368 void initialize(const Locale& locale, UErrorCode& success,
369 UBool useLastResortData = FALSE, const NumberingSystem* ns = nullptr);
b75a7d8f 370
b75a7d8f
A
371 /**
372 * Initialize the symbols with default values.
373 */
374 void initialize();
375
376 void setCurrencyForSymbols();
377
378public:
2ca993e8
A
379
380#ifndef U_HIDE_INTERNAL_API
381 /**
382 * @internal For ICU use only
383 */
384 inline UBool isCustomCurrencySymbol() const {
385 return fIsCustomCurrencySymbol;
386 }
387
388 /**
389 * @internal For ICU use only
390 */
391 inline UBool isCustomIntlCurrencySymbol() const {
392 return fIsCustomIntlCurrencySymbol;
393 }
0f5d89e8
A
394
395 /**
396 * @internal For ICU use only
397 */
398 inline UChar32 getCodePointZero() const {
399 return fCodePointZero;
400 }
2ca993e8
A
401#endif /* U_HIDE_INTERNAL_API */
402
b75a7d8f
A
403 /**
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.
3d1f044b 408 * Note: moved \#ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
b75a7d8f 409 *
0f5d89e8
A
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
413 *
b75a7d8f
A
414 * @param symbol Constant to indicate a number format symbol.
415 * @return the format symbol by the param 'symbol'
416 * @internal
417 */
0f5d89e8 418 inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const;
b75a7d8f 419
57a6839d 420#ifndef U_HIDE_INTERNAL_API
0f5d89e8
A
421 /**
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.
426 *
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
430 *
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.
435 */
436 inline const UnicodeString& getConstDigitSymbol(int32_t digit) const;
437
73c04bcf
A
438 /**
439 * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
440 * @internal
441 */
f3c0d7a5 442 inline const char16_t* getCurrencyPattern(void) const;
3d1f044b
A
443
444 /**
445 * Returns the name of the numbering system used for this object.
446 * @internal
447 */
448 inline const char* getNSName() const;
449
4388f060 450#endif /* U_HIDE_INTERNAL_API */
73c04bcf 451
b75a7d8f
A
452private:
453 /**
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.
459 *
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().
465 *
466 * @internal
467 */
468 UnicodeString fSymbols[kFormatSymbolCount];
469
470 /**
471 * Non-symbol variable for getConstSymbol(). Always empty.
472 * @internal
473 */
474 UnicodeString fNoSymbol;
475
0f5d89e8
A
476 /**
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.
480 *
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.
485 *
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.
489 */
490 UChar32 fCodePointZero;
491
b75a7d8f
A
492 Locale locale;
493
374ca955
A
494 char actualLocale[ULOC_FULLNAME_CAPACITY];
495 char validLocale[ULOC_FULLNAME_CAPACITY];
f3c0d7a5 496 const char16_t* currPattern;
729e4ab9 497
4388f060
A
498 UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
499 UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
2ca993e8
A
500 UBool fIsCustomCurrencySymbol;
501 UBool fIsCustomIntlCurrencySymbol;
3d1f044b 502 char fNSName[9]; // Apple rdar://51672521
b75a7d8f
A
503};
504
b75a7d8f
A
505// -------------------------------------
506
507inline UnicodeString
508DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
374ca955
A
509 const UnicodeString *strPtr;
510 if(symbol < kFormatSymbolCount) {
511 strPtr = &fSymbols[symbol];
b75a7d8f 512 } else {
374ca955 513 strPtr = &fNoSymbol;
b75a7d8f 514 }
374ca955 515 return *strPtr;
b75a7d8f
A
516}
517
0f5d89e8 518// See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API
b75a7d8f
A
519inline const UnicodeString &
520DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
374ca955
A
521 const UnicodeString *strPtr;
522 if(symbol < kFormatSymbolCount) {
523 strPtr = &fSymbols[symbol];
b75a7d8f 524 } else {
374ca955 525 strPtr = &fNoSymbol;
b75a7d8f 526 }
374ca955 527 return *strPtr;
b75a7d8f 528}
4388f060 529
0f5d89e8
A
530#ifndef U_HIDE_INTERNAL_API
531inline const UnicodeString& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit) const {
532 if (digit < 0 || digit > 9) {
533 digit = 0;
534 }
535 if (digit == 0) {
536 return fSymbols[kZeroDigitSymbol];
537 }
538 ENumberFormatSymbol key = static_cast<ENumberFormatSymbol>(kOneDigitSymbol + digit - 1);
539 return fSymbols[key];
540}
3d1f044b 541#endif /* U_HIDE_INTERNAL_API */
0f5d89e8 542
b75a7d8f
A
543// -------------------------------------
544
545inline void
729e4ab9 546DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
2ca993e8
A
547 if (symbol == kCurrencySymbol) {
548 fIsCustomCurrencySymbol = TRUE;
549 }
550 else if (symbol == kIntlCurrencySymbol) {
551 fIsCustomIntlCurrencySymbol = TRUE;
552 }
b75a7d8f
A
553 if(symbol<kFormatSymbolCount) {
554 fSymbols[symbol]=value;
555 }
729e4ab9
A
556
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
0f5d89e8
A
559 // Also record updates to fCodePointZero. Be conservative if in doubt.
560 if (symbol == kZeroDigitSymbol) {
729e4ab9 561 UChar32 sym = value.char32At(0);
0f5d89e8
A
562 if ( propogateDigits && u_charDigitValue(sym) == 0 && value.countChar32() == 1 ) {
563 fCodePointZero = sym;
729e4ab9
A
564 for ( int8_t i = 1 ; i<= 9 ; i++ ) {
565 sym++;
566 fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
567 }
0f5d89e8
A
568 } else {
569 fCodePointZero = -1;
729e4ab9 570 }
0f5d89e8
A
571 } else if (symbol >= kOneDigitSymbol && symbol <= kNineDigitSymbol) {
572 fCodePointZero = -1;
729e4ab9 573 }
b75a7d8f
A
574}
575
576// -------------------------------------
577
578inline Locale
579DecimalFormatSymbols::getLocale() const {
580 return locale;
581}
582
57a6839d 583#ifndef U_HIDE_INTERNAL_API
f3c0d7a5 584inline const char16_t*
73c04bcf
A
585DecimalFormatSymbols::getCurrencyPattern() const {
586 return currPattern;
587}
3d1f044b
A
588
589inline const char*
590DecimalFormatSymbols::getNSName() const {
591 return fNSName;
592}
57a6839d 593#endif /* U_HIDE_INTERNAL_API */
4388f060 594
b75a7d8f 595U_NAMESPACE_END
f3c0d7a5 596#endif // U_SHOW_CPLUSPLUS_API
b75a7d8f
A
597
598#endif /* #if !UCONFIG_NO_FORMATTING */
599
600#endif // _DCFMTSYM
601//eof