]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/dcfmtsym.h
ICU-62141.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#ifndef U_HIDE_DRAFT_API
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.
200 * @draft ICU 60
201 */
202 DecimalFormatSymbols(const Locale& locale, const NumberingSystem& ns, UErrorCode& status);
203#endif /* U_HIDE_DRAFT_API */
204
b75a7d8f
A
205 /**
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.
210 *
211 * @param status Input/output parameter, set to success or
212 * failure code upon return.
213 * @stable ICU 2.0
214 */
57a6839d
A
215 DecimalFormatSymbols(UErrorCode& status);
216
57a6839d
A
217 /**
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.
221 *
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.
226 *
227 * @param status Input/output parameter, set to success or
228 * failure code upon return.
229 * @return last-resort symbols
b331163b 230 * @stable ICU 52
57a6839d
A
231 */
232 static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
b75a7d8f
A
233
234 /**
235 * Copy constructor.
236 * @stable ICU 2.0
237 */
238 DecimalFormatSymbols(const DecimalFormatSymbols&);
239
240 /**
241 * Assignment operator.
242 * @stable ICU 2.0
243 */
244 DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
245
246 /**
247 * Destructor.
248 * @stable ICU 2.0
249 */
374ca955 250 virtual ~DecimalFormatSymbols();
b75a7d8f
A
251
252 /**
253 * Return true if another object is semantically equal to this one.
254 *
255 * @param other the object to be compared with.
256 * @return true if another object is semantically equal to this one.
257 * @stable ICU 2.0
258 */
259 UBool operator==(const DecimalFormatSymbols& other) const;
260
261 /**
262 * Return true if another object is semantically unequal to this one.
263 *
264 * @param other the object to be compared with.
265 * @return true if another object is semantically unequal to this one.
266 * @stable ICU 2.0
267 */
268 UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
269
270 /**
271 * Get one of the format symbols by its enum constant.
272 * Each symbol is stored as a string so that graphemes
729e4ab9 273 * (characters with modifier letters) can be used.
b75a7d8f
A
274 *
275 * @param symbol Constant to indicate a number format symbol.
276 * @return the format symbols by the param 'symbol'
277 * @stable ICU 2.0
278 */
374ca955 279 inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
b75a7d8f
A
280
281 /**
282 * Set one of the format symbols by its enum constant.
283 * Each symbol is stored as a string so that graphemes
729e4ab9 284 * (characters with modifier letters) can be used.
b75a7d8f
A
285 *
286 * @param symbol Constant to indicate a number format symbol.
729e4ab9
A
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.
b75a7d8f
A
291 * @stable ICU 2.0
292 */
729e4ab9 293 void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
b75a7d8f
A
294
295 /**
296 * Returns the locale for which this object was constructed.
297 * @stable ICU 2.6
298 */
299 inline Locale getLocale() const;
300
374ca955
A
301 /**
302 * Returns the locale for this object. Two flavors are available:
303 * valid and actual locale.
73c04bcf 304 * @stable ICU 2.8
374ca955
A
305 */
306 Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
307
729e4ab9
A
308 /**
309 * Get pattern string for 'CurrencySpacing' that can be applied to
310 * currency format.
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.
313 *
4388f060 314 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
729e4ab9
A
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
321 * locales.
4388f060 322 * @stable ICU 4.8
729e4ab9 323 */
4388f060 324 const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
729e4ab9
A
325 UBool beforeCurrency,
326 UErrorCode& status) const;
327 /**
328 * Set pattern string for 'CurrencySpacing' that can be applied to
329 * currency format.
330 *
4388f060 331 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
729e4ab9
A
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.
4388f060 335 * @stable ICU 4.8
729e4ab9 336 */
4388f060 337 void setPatternForCurrencySpacing(UCurrencySpacing type,
729e4ab9
A
338 UBool beforeCurrency,
339 const UnicodeString& pattern);
340
b75a7d8f
A
341 /**
342 * ICU "poor man's RTTI", returns a UClassID for the actual class.
343 *
374ca955 344 * @stable ICU 2.2
b75a7d8f 345 */
374ca955 346 virtual UClassID getDynamicClassID() const;
b75a7d8f
A
347
348 /**
349 * ICU "poor man's RTTI", returns a UClassID for this class.
350 *
374ca955 351 * @stable ICU 2.2
b75a7d8f 352 */
374ca955 353 static UClassID U_EXPORT2 getStaticClassID();
b75a7d8f
A
354
355private:
57a6839d 356 DecimalFormatSymbols();
b75a7d8f
A
357
358 /**
359 * Initializes the symbols from the LocaleElements resource bundle.
360 * Note: The organization of LocaleElements badly needs to be
361 * cleaned up.
362 *
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
0f5d89e8
A
367 * @param ns The NumberingSystem to use; otherwise, fall
368 * back to the locale.
b75a7d8f 369 */
0f5d89e8
A
370 void initialize(const Locale& locale, UErrorCode& success,
371 UBool useLastResortData = FALSE, const NumberingSystem* ns = nullptr);
b75a7d8f 372
b75a7d8f
A
373 /**
374 * Initialize the symbols with default values.
375 */
376 void initialize();
377
378 void setCurrencyForSymbols();
379
380public:
2ca993e8
A
381
382#ifndef U_HIDE_INTERNAL_API
383 /**
384 * @internal For ICU use only
385 */
386 inline UBool isCustomCurrencySymbol() const {
387 return fIsCustomCurrencySymbol;
388 }
389
390 /**
391 * @internal For ICU use only
392 */
393 inline UBool isCustomIntlCurrencySymbol() const {
394 return fIsCustomIntlCurrencySymbol;
395 }
0f5d89e8
A
396
397 /**
398 * @internal For ICU use only
399 */
400 inline UChar32 getCodePointZero() const {
401 return fCodePointZero;
402 }
2ca993e8
A
403#endif /* U_HIDE_INTERNAL_API */
404
b75a7d8f
A
405 /**
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.
b331163b 410 * Note: moved #ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
b75a7d8f 411 *
0f5d89e8
A
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
415 *
b75a7d8f
A
416 * @param symbol Constant to indicate a number format symbol.
417 * @return the format symbol by the param 'symbol'
418 * @internal
419 */
0f5d89e8 420 inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const;
b75a7d8f 421
57a6839d 422#ifndef U_HIDE_INTERNAL_API
0f5d89e8
A
423 /**
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.
428 *
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
432 *
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.
437 */
438 inline const UnicodeString& getConstDigitSymbol(int32_t digit) const;
439
73c04bcf
A
440 /**
441 * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
442 * @internal
443 */
f3c0d7a5 444 inline const char16_t* getCurrencyPattern(void) const;
4388f060 445#endif /* U_HIDE_INTERNAL_API */
73c04bcf 446
b75a7d8f
A
447private:
448 /**
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.
454 *
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().
460 *
461 * @internal
462 */
463 UnicodeString fSymbols[kFormatSymbolCount];
464
465 /**
466 * Non-symbol variable for getConstSymbol(). Always empty.
467 * @internal
468 */
469 UnicodeString fNoSymbol;
470
0f5d89e8
A
471 /**
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.
475 *
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.
480 *
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.
484 */
485 UChar32 fCodePointZero;
486
b75a7d8f
A
487 Locale locale;
488
374ca955
A
489 char actualLocale[ULOC_FULLNAME_CAPACITY];
490 char validLocale[ULOC_FULLNAME_CAPACITY];
f3c0d7a5 491 const char16_t* currPattern;
729e4ab9 492
4388f060
A
493 UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
494 UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
2ca993e8
A
495 UBool fIsCustomCurrencySymbol;
496 UBool fIsCustomIntlCurrencySymbol;
b75a7d8f
A
497};
498
b75a7d8f
A
499// -------------------------------------
500
501inline UnicodeString
502DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
374ca955
A
503 const UnicodeString *strPtr;
504 if(symbol < kFormatSymbolCount) {
505 strPtr = &fSymbols[symbol];
b75a7d8f 506 } else {
374ca955 507 strPtr = &fNoSymbol;
b75a7d8f 508 }
374ca955 509 return *strPtr;
b75a7d8f
A
510}
511
0f5d89e8 512// See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API
b75a7d8f
A
513inline const UnicodeString &
514DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
374ca955
A
515 const UnicodeString *strPtr;
516 if(symbol < kFormatSymbolCount) {
517 strPtr = &fSymbols[symbol];
b75a7d8f 518 } else {
374ca955 519 strPtr = &fNoSymbol;
b75a7d8f 520 }
374ca955 521 return *strPtr;
b75a7d8f 522}
4388f060 523
0f5d89e8
A
524#ifndef U_HIDE_INTERNAL_API
525inline const UnicodeString& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit) const {
526 if (digit < 0 || digit > 9) {
527 digit = 0;
528 }
529 if (digit == 0) {
530 return fSymbols[kZeroDigitSymbol];
531 }
532 ENumberFormatSymbol key = static_cast<ENumberFormatSymbol>(kOneDigitSymbol + digit - 1);
533 return fSymbols[key];
534}
535#endif
536
b75a7d8f
A
537// -------------------------------------
538
539inline void
729e4ab9 540DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
2ca993e8
A
541 if (symbol == kCurrencySymbol) {
542 fIsCustomCurrencySymbol = TRUE;
543 }
544 else if (symbol == kIntlCurrencySymbol) {
545 fIsCustomIntlCurrencySymbol = TRUE;
546 }
b75a7d8f
A
547 if(symbol<kFormatSymbolCount) {
548 fSymbols[symbol]=value;
549 }
729e4ab9
A
550
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
0f5d89e8
A
553 // Also record updates to fCodePointZero. Be conservative if in doubt.
554 if (symbol == kZeroDigitSymbol) {
729e4ab9 555 UChar32 sym = value.char32At(0);
0f5d89e8
A
556 if ( propogateDigits && u_charDigitValue(sym) == 0 && value.countChar32() == 1 ) {
557 fCodePointZero = sym;
729e4ab9
A
558 for ( int8_t i = 1 ; i<= 9 ; i++ ) {
559 sym++;
560 fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
561 }
0f5d89e8
A
562 } else {
563 fCodePointZero = -1;
729e4ab9 564 }
0f5d89e8
A
565 } else if (symbol >= kOneDigitSymbol && symbol <= kNineDigitSymbol) {
566 fCodePointZero = -1;
729e4ab9 567 }
b75a7d8f
A
568}
569
570// -------------------------------------
571
572inline Locale
573DecimalFormatSymbols::getLocale() const {
574 return locale;
575}
576
57a6839d 577#ifndef U_HIDE_INTERNAL_API
f3c0d7a5 578inline const char16_t*
73c04bcf
A
579DecimalFormatSymbols::getCurrencyPattern() const {
580 return currPattern;
581}
57a6839d 582#endif /* U_HIDE_INTERNAL_API */
4388f060 583
b75a7d8f 584U_NAMESPACE_END
f3c0d7a5 585#endif // U_SHOW_CPLUSPLUS_API
b75a7d8f
A
586
587#endif /* #if !UCONFIG_NO_FORMATTING */
588
589#endif // _DCFMTSYM
590//eof