]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/dcfmtsym.h
c2207e7b2e97e2c91b07cd604aa9b42f93f5063e
[apple/icu.git] / icuSources / i18n / unicode / dcfmtsym.h
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 #if U_SHOW_CPLUSPLUS_API
48 U_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 "#,##,###,####" == "######,####" == "##,####,####".
84 */
85 class U_I18N_API DecimalFormatSymbols : public UObject {
86 public:
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,
124 /** Significant digit symbol
125 * @stable ICU 3.0 */
126 kSignificantDigitSymbol,
127 /** The monetary grouping separator
128 * @stable ICU 3.6
129 */
130 kMonetaryGroupingSeparatorSymbol,
131 /** One
132 * @stable ICU 4.6
133 */
134 kOneDigitSymbol,
135 /** Two
136 * @stable ICU 4.6
137 */
138 kTwoDigitSymbol,
139 /** Three
140 * @stable ICU 4.6
141 */
142 kThreeDigitSymbol,
143 /** Four
144 * @stable ICU 4.6
145 */
146 kFourDigitSymbol,
147 /** Five
148 * @stable ICU 4.6
149 */
150 kFiveDigitSymbol,
151 /** Six
152 * @stable ICU 4.6
153 */
154 kSixDigitSymbol,
155 /** Seven
156 * @stable ICU 4.6
157 */
158 kSevenDigitSymbol,
159 /** Eight
160 * @stable ICU 4.6
161 */
162 kEightDigitSymbol,
163 /** Nine
164 * @stable ICU 4.6
165 */
166 kNineDigitSymbol,
167 /** Multiplication sign.
168 * @stable ICU 54
169 */
170 kExponentMultiplicationSymbol,
171 /** count symbol constants */
172 kFormatSymbolCount = kNineDigitSymbol + 2
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
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.
199 * @stable ICU 60
200 */
201 DecimalFormatSymbols(const Locale& locale, const NumberingSystem& ns, UErrorCode& status);
202
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 */
213 DecimalFormatSymbols(UErrorCode& status);
214
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
228 * @stable ICU 52
229 */
230 static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
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 */
248 virtual ~DecimalFormatSymbols();
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
271 * (characters with modifier letters) can be used.
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 */
277 inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
278
279 /**
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.
283 *
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.
289 * @stable ICU 2.0
290 */
291 void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
292
293 /**
294 * Returns the locale for which this object was constructed.
295 * @stable ICU 2.6
296 */
297 inline Locale getLocale() const;
298
299 /**
300 * Returns the locale for this object. Two flavors are available:
301 * valid and actual locale.
302 * @stable ICU 2.8
303 */
304 Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
305
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 *
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
319 * locales.
320 * @stable ICU 4.8
321 */
322 const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
323 UBool beforeCurrency,
324 UErrorCode& status) const;
325 /**
326 * Set pattern string for 'CurrencySpacing' that can be applied to
327 * currency format.
328 *
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.
333 * @stable ICU 4.8
334 */
335 void setPatternForCurrencySpacing(UCurrencySpacing type,
336 UBool beforeCurrency,
337 const UnicodeString& pattern);
338
339 /**
340 * ICU "poor man's RTTI", returns a UClassID for the actual class.
341 *
342 * @stable ICU 2.2
343 */
344 virtual UClassID getDynamicClassID() const;
345
346 /**
347 * ICU "poor man's RTTI", returns a UClassID for this class.
348 *
349 * @stable ICU 2.2
350 */
351 static UClassID U_EXPORT2 getStaticClassID();
352
353 private:
354 DecimalFormatSymbols();
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
365 * @param ns The NumberingSystem to use; otherwise, fall
366 * back to the locale.
367 */
368 void initialize(const Locale& locale, UErrorCode& success,
369 UBool useLastResortData = FALSE, const NumberingSystem* ns = nullptr);
370
371 /**
372 * Initialize the symbols with default values.
373 */
374 void initialize();
375
376 void setCurrencyForSymbols();
377
378 public:
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 }
394
395 /**
396 * @internal For ICU use only
397 */
398 inline UChar32 getCodePointZero() const {
399 return fCodePointZero;
400 }
401 #endif /* U_HIDE_INTERNAL_API */
402
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.
408 * Note: moved \#ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
409 *
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 *
414 * @param symbol Constant to indicate a number format symbol.
415 * @return the format symbol by the param 'symbol'
416 * @internal
417 */
418 inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const;
419
420 #ifndef U_HIDE_INTERNAL_API
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
438 /**
439 * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
440 * @internal
441 */
442 inline const char16_t* getCurrencyPattern(void) const;
443
444 /**
445 * Returns the name of the numbering system used for this object.
446 * @internal
447 */
448 inline const char* getNSName() const;
449
450 #endif /* U_HIDE_INTERNAL_API */
451
452 private:
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
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
492 Locale locale;
493
494 char actualLocale[ULOC_FULLNAME_CAPACITY];
495 char validLocale[ULOC_FULLNAME_CAPACITY];
496 const char16_t* currPattern;
497
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
503 };
504
505 // -------------------------------------
506
507 inline UnicodeString
508 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
509 const UnicodeString *strPtr;
510 if(symbol < kFormatSymbolCount) {
511 strPtr = &fSymbols[symbol];
512 } else {
513 strPtr = &fNoSymbol;
514 }
515 return *strPtr;
516 }
517
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];
524 } else {
525 strPtr = &fNoSymbol;
526 }
527 return *strPtr;
528 }
529
530 #ifndef U_HIDE_INTERNAL_API
531 inline 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 }
541 #endif /* U_HIDE_INTERNAL_API */
542
543 // -------------------------------------
544
545 inline void
546 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
547 if (symbol == kCurrencySymbol) {
548 fIsCustomCurrencySymbol = TRUE;
549 }
550 else if (symbol == kIntlCurrencySymbol) {
551 fIsCustomIntlCurrencySymbol = TRUE;
552 }
553 if(symbol<kFormatSymbolCount) {
554 fSymbols[symbol]=value;
555 }
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
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++ ) {
565 sym++;
566 fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
567 }
568 } else {
569 fCodePointZero = -1;
570 }
571 } else if (symbol >= kOneDigitSymbol && symbol <= kNineDigitSymbol) {
572 fCodePointZero = -1;
573 }
574 }
575
576 // -------------------------------------
577
578 inline Locale
579 DecimalFormatSymbols::getLocale() const {
580 return locale;
581 }
582
583 #ifndef U_HIDE_INTERNAL_API
584 inline const char16_t*
585 DecimalFormatSymbols::getCurrencyPattern() const {
586 return currPattern;
587 }
588
589 inline const char*
590 DecimalFormatSymbols::getNSName() const {
591 return fNSName;
592 }
593 #endif /* U_HIDE_INTERNAL_API */
594
595 U_NAMESPACE_END
596 #endif // U_SHOW_CPLUSPLUS_API
597
598 #endif /* #if !UCONFIG_NO_FORMATTING */
599
600 #endif // _DCFMTSYM
601 //eof