]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/dcfmtsym.h
ICU-62107.0.1.tar.gz
[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 #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
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 */
215 DecimalFormatSymbols(UErrorCode& status);
216
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
230 * @stable ICU 52
231 */
232 static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
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 */
250 virtual ~DecimalFormatSymbols();
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
273 * (characters with modifier letters) can be used.
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 */
279 inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
280
281 /**
282 * Set one of the format symbols by its enum constant.
283 * Each symbol is stored as a string so that graphemes
284 * (characters with modifier letters) can be used.
285 *
286 * @param symbol Constant to indicate a number format symbol.
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.
291 * @stable ICU 2.0
292 */
293 void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
294
295 /**
296 * Returns the locale for which this object was constructed.
297 * @stable ICU 2.6
298 */
299 inline Locale getLocale() const;
300
301 /**
302 * Returns the locale for this object. Two flavors are available:
303 * valid and actual locale.
304 * @stable ICU 2.8
305 */
306 Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
307
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 *
314 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
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.
322 * @stable ICU 4.8
323 */
324 const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
325 UBool beforeCurrency,
326 UErrorCode& status) const;
327 /**
328 * Set pattern string for 'CurrencySpacing' that can be applied to
329 * currency format.
330 *
331 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
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.
335 * @stable ICU 4.8
336 */
337 void setPatternForCurrencySpacing(UCurrencySpacing type,
338 UBool beforeCurrency,
339 const UnicodeString& pattern);
340
341 /**
342 * ICU "poor man's RTTI", returns a UClassID for the actual class.
343 *
344 * @stable ICU 2.2
345 */
346 virtual UClassID getDynamicClassID() const;
347
348 /**
349 * ICU "poor man's RTTI", returns a UClassID for this class.
350 *
351 * @stable ICU 2.2
352 */
353 static UClassID U_EXPORT2 getStaticClassID();
354
355 private:
356 DecimalFormatSymbols();
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
367 * @param ns The NumberingSystem to use; otherwise, fall
368 * back to the locale.
369 */
370 void initialize(const Locale& locale, UErrorCode& success,
371 UBool useLastResortData = FALSE, const NumberingSystem* ns = nullptr);
372
373 /**
374 * Initialize the symbols with default values.
375 */
376 void initialize();
377
378 void setCurrencyForSymbols();
379
380 public:
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 }
396
397 /**
398 * @internal For ICU use only
399 */
400 inline UChar32 getCodePointZero() const {
401 return fCodePointZero;
402 }
403 #endif /* U_HIDE_INTERNAL_API */
404
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.
410 * Note: moved #ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
411 *
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 *
416 * @param symbol Constant to indicate a number format symbol.
417 * @return the format symbol by the param 'symbol'
418 * @internal
419 */
420 inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const;
421
422 #ifndef U_HIDE_INTERNAL_API
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
440 /**
441 * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
442 * @internal
443 */
444 inline const char16_t* getCurrencyPattern(void) const;
445 #endif /* U_HIDE_INTERNAL_API */
446
447 private:
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
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
487 Locale locale;
488
489 char actualLocale[ULOC_FULLNAME_CAPACITY];
490 char validLocale[ULOC_FULLNAME_CAPACITY];
491 const char16_t* currPattern;
492
493 UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
494 UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
495 UBool fIsCustomCurrencySymbol;
496 UBool fIsCustomIntlCurrencySymbol;
497 };
498
499 // -------------------------------------
500
501 inline UnicodeString
502 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
503 const UnicodeString *strPtr;
504 if(symbol < kFormatSymbolCount) {
505 strPtr = &fSymbols[symbol];
506 } else {
507 strPtr = &fNoSymbol;
508 }
509 return *strPtr;
510 }
511
512 // See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API
513 inline const UnicodeString &
514 DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
515 const UnicodeString *strPtr;
516 if(symbol < kFormatSymbolCount) {
517 strPtr = &fSymbols[symbol];
518 } else {
519 strPtr = &fNoSymbol;
520 }
521 return *strPtr;
522 }
523
524 #ifndef U_HIDE_INTERNAL_API
525 inline 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
537 // -------------------------------------
538
539 inline void
540 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
541 if (symbol == kCurrencySymbol) {
542 fIsCustomCurrencySymbol = TRUE;
543 }
544 else if (symbol == kIntlCurrencySymbol) {
545 fIsCustomIntlCurrencySymbol = TRUE;
546 }
547 if(symbol<kFormatSymbolCount) {
548 fSymbols[symbol]=value;
549 }
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
553 // Also record updates to fCodePointZero. Be conservative if in doubt.
554 if (symbol == kZeroDigitSymbol) {
555 UChar32 sym = value.char32At(0);
556 if ( propogateDigits && u_charDigitValue(sym) == 0 && value.countChar32() == 1 ) {
557 fCodePointZero = sym;
558 for ( int8_t i = 1 ; i<= 9 ; i++ ) {
559 sym++;
560 fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
561 }
562 } else {
563 fCodePointZero = -1;
564 }
565 } else if (symbol >= kOneDigitSymbol && symbol <= kNineDigitSymbol) {
566 fCodePointZero = -1;
567 }
568 }
569
570 // -------------------------------------
571
572 inline Locale
573 DecimalFormatSymbols::getLocale() const {
574 return locale;
575 }
576
577 #ifndef U_HIDE_INTERNAL_API
578 inline const char16_t*
579 DecimalFormatSymbols::getCurrencyPattern() const {
580 return currPattern;
581 }
582 #endif /* U_HIDE_INTERNAL_API */
583
584 U_NAMESPACE_END
585 #endif // U_SHOW_CPLUSPLUS_API
586
587 #endif /* #if !UCONFIG_NO_FORMATTING */
588
589 #endif // _DCFMTSYM
590 //eof