Imported Upstream version 58.1
[platform/upstream/icu.git] / source / i18n / unicode / dcfmtsym.h
1 // Copyright (C) 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/unum.h"
38
39 /**
40  * \file
41  * \brief C++ API: Symbols for formatting numbers.
42  */
43
44
45 U_NAMESPACE_BEGIN
46
47 /**
48  * This class represents the set of symbols needed by DecimalFormat
49  * to format numbers. DecimalFormat creates for itself an instance of
50  * DecimalFormatSymbols from its locale data.  If you need to change any
51  * of these symbols, you can get the DecimalFormatSymbols object from
52  * your DecimalFormat and modify it.
53  * <P>
54  * Here are the special characters used in the parts of the
55  * subpattern, with notes on their usage.
56  * <pre>
57  * \code
58  *        Symbol   Meaning
59  *          0      a digit
60  *          #      a digit, zero shows as absent
61  *          .      placeholder for decimal separator
62  *          ,      placeholder for grouping separator.
63  *          ;      separates formats.
64  *          -      default negative prefix.
65  *          %      divide by 100 and show as percentage
66  *          X      any other characters can be used in the prefix or suffix
67  *          '      used to quote special characters in a prefix or suffix.
68  * \endcode
69  *  </pre>
70  * [Notes]
71  * <P>
72  * If there is no explicit negative subpattern, - is prefixed to the
73  * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
74  * <P>
75  * The grouping separator is commonly used for thousands, but in some
76  * countries for ten-thousands. The interval is a constant number of
77  * digits between the grouping characters, such as 100,000,000 or 1,0000,0000.
78  * If you supply a pattern with multiple grouping characters, the interval
79  * between the last one and the end of the integer is the one that is
80  * used. So "#,##,###,####" == "######,####" == "##,####,####".
81  * <P>
82  * This class only handles localized digits where the 10 digits are
83  * contiguous in Unicode, from 0 to 9. Other digits sets (such as
84  * superscripts) would need a different subclass.
85  */
86 class U_I18N_API DecimalFormatSymbols : public UObject {
87 public:
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,
125         /** Significant digit symbol
126          * @stable ICU 3.0 */
127         kSignificantDigitSymbol,
128         /** The monetary grouping separator
129          * @stable ICU 3.6
130          */
131         kMonetaryGroupingSeparatorSymbol,
132         /** One
133          * @stable ICU 4.6
134          */
135         kOneDigitSymbol,
136         /** Two
137          * @stable ICU 4.6
138          */
139         kTwoDigitSymbol,
140         /** Three
141          * @stable ICU 4.6
142          */
143         kThreeDigitSymbol,
144         /** Four
145          * @stable ICU 4.6
146          */
147         kFourDigitSymbol,
148         /** Five
149          * @stable ICU 4.6
150          */
151         kFiveDigitSymbol,
152         /** Six
153          * @stable ICU 4.6
154          */
155         kSixDigitSymbol,
156         /** Seven
157          * @stable ICU 4.6
158          */
159         kSevenDigitSymbol,
160         /** Eight
161          * @stable ICU 4.6
162          */
163         kEightDigitSymbol,
164         /** Nine
165          * @stable ICU 4.6
166          */
167         kNineDigitSymbol,
168         /** Multiplication sign.
169          * @stable ICU 54
170          */
171         kExponentMultiplicationSymbol,
172         /** count symbol constants */
173         kFormatSymbolCount = kNineDigitSymbol + 2
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
186     /**
187      * Create a DecimalFormatSymbols object for the default locale.
188      * This constructor will not fail.  If the resource file data is
189      * not available, it will use hard-coded last-resort data and
190      * set status to U_USING_FALLBACK_ERROR.
191      *
192      * @param status    Input/output parameter, set to success or
193      *                  failure code upon return.
194      * @stable ICU 2.0
195      */
196     DecimalFormatSymbols(UErrorCode& status);
197
198     /**
199      * Creates a DecimalFormatSymbols object with last-resort data.
200      * Intended for callers who cache the symbols data and
201      * set all symbols on the resulting object.
202      *
203      * The last-resort symbols are similar to those for the root data,
204      * except that the grouping separators are empty,
205      * the NaN symbol is U+FFFD rather than "NaN",
206      * and the CurrencySpacing patterns are empty.
207      *
208      * @param status    Input/output parameter, set to success or
209      *                  failure code upon return.
210      * @return last-resort symbols
211      * @stable ICU 52
212      */
213     static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
214
215     /**
216      * Copy constructor.
217      * @stable ICU 2.0
218      */
219     DecimalFormatSymbols(const DecimalFormatSymbols&);
220
221     /**
222      * Assignment operator.
223      * @stable ICU 2.0
224      */
225     DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
226
227     /**
228      * Destructor.
229      * @stable ICU 2.0
230      */
231     virtual ~DecimalFormatSymbols();
232
233     /**
234      * Return true if another object is semantically equal to this one.
235      *
236      * @param other    the object to be compared with.
237      * @return         true if another object is semantically equal to this one.
238      * @stable ICU 2.0
239      */
240     UBool operator==(const DecimalFormatSymbols& other) const;
241
242     /**
243      * Return true if another object is semantically unequal to this one.
244      *
245      * @param other    the object to be compared with.
246      * @return         true if another object is semantically unequal to this one.
247      * @stable ICU 2.0
248      */
249     UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
250
251     /**
252      * Get one of the format symbols by its enum constant.
253      * Each symbol is stored as a string so that graphemes
254      * (characters with modifier letters) can be used.
255      *
256      * @param symbol    Constant to indicate a number format symbol.
257      * @return    the format symbols by the param 'symbol'
258      * @stable ICU 2.0
259      */
260     inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
261
262     /**
263      * Set one of the format symbols by its enum constant.
264      * Each symbol is stored as a string so that graphemes
265      * (characters with modifier letters) can be used.
266      *
267      * @param symbol    Constant to indicate a number format symbol.
268      * @param value     value of the format symbol
269      * @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
270      *     The default behavior is to automatically set 1-9 if zero is being set and the value
271      *     it is being set to corresponds to a known Unicode zero digit.
272      * @stable ICU 2.0
273      */
274     void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
275
276     /**
277      * Returns the locale for which this object was constructed.
278      * @stable ICU 2.6
279      */
280     inline Locale getLocale() const;
281
282     /**
283      * Returns the locale for this object. Two flavors are available:
284      * valid and actual locale.
285      * @stable ICU 2.8
286      */
287     Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
288
289     /**
290       * Get pattern string for 'CurrencySpacing' that can be applied to
291       * currency format.
292       * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
293       * be empty if there is no data from current locale and its parent locales.
294       *
295       * @param type :  UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
296       * @param beforeCurrency : true if the pattern is for before currency symbol.
297       *                         false if the pattern is for after currency symbol.
298       * @param status: Input/output parameter, set to success or
299       *                  failure code upon return.
300       * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
301       *     Return empty string if there is no data for this locale and its parent
302       *     locales.
303       * @stable ICU 4.8
304       */
305      const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
306                                                  UBool beforeCurrency,
307                                                  UErrorCode& status) const;
308      /**
309        * Set pattern string for 'CurrencySpacing' that can be applied to
310        * currency format.
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 pattern : pattern string to override current setting.
316        * @stable ICU 4.8
317        */
318      void setPatternForCurrencySpacing(UCurrencySpacing type,
319                                        UBool beforeCurrency,
320                                        const UnicodeString& pattern);
321
322     /**
323      * ICU "poor man's RTTI", returns a UClassID for the actual class.
324      *
325      * @stable ICU 2.2
326      */
327     virtual UClassID getDynamicClassID() const;
328
329     /**
330      * ICU "poor man's RTTI", returns a UClassID for this class.
331      *
332      * @stable ICU 2.2
333      */
334     static UClassID U_EXPORT2 getStaticClassID();
335
336 private:
337     DecimalFormatSymbols();
338
339     /**
340      * Initializes the symbols from the LocaleElements resource bundle.
341      * Note: The organization of LocaleElements badly needs to be
342      * cleaned up.
343      *
344      * @param locale               The locale to get symbols for.
345      * @param success              Input/output parameter, set to success or
346      *                             failure code upon return.
347      * @param useLastResortData    determine if use last resort data
348      */
349     void initialize(const Locale& locale, UErrorCode& success, UBool useLastResortData = FALSE);
350
351     /**
352      * Initialize the symbols with default values.
353      */
354     void initialize();
355
356     void setCurrencyForSymbols();
357
358 public:
359
360 #ifndef U_HIDE_INTERNAL_API
361     /**
362      * @internal For ICU use only
363      */
364     inline UBool isCustomCurrencySymbol() const {
365         return fIsCustomCurrencySymbol;
366     }
367
368     /**
369      * @internal For ICU use only
370      */
371     inline UBool isCustomIntlCurrencySymbol() const {
372         return fIsCustomIntlCurrencySymbol;
373     }
374 #endif  /* U_HIDE_INTERNAL_API */
375
376     /**
377      * _Internal_ function - more efficient version of getSymbol,
378      * returning a const reference to one of the symbol strings.
379      * The returned reference becomes invalid when the symbol is changed
380      * or when the DecimalFormatSymbols are destroyed.
381      * ### TODO markus 2002oct11: Consider proposing getConstSymbol() to be really public.
382      * Note: moved #ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
383      *
384      * @param symbol Constant to indicate a number format symbol.
385      * @return the format symbol by the param 'symbol'
386      * @internal
387      */
388     inline const UnicodeString &getConstSymbol(ENumberFormatSymbol symbol) const;
389
390 #ifndef U_HIDE_INTERNAL_API
391     /**
392      * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
393      * @internal
394      */
395     inline const UChar* getCurrencyPattern(void) const;
396 #endif  /* U_HIDE_INTERNAL_API */
397
398 private:
399     /**
400      * Private symbol strings.
401      * They are either loaded from a resource bundle or otherwise owned.
402      * setSymbol() clones the symbol string.
403      * Readonly aliases can only come from a resource bundle, so that we can always
404      * use fastCopyFrom() with them.
405      *
406      * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
407      * from private to protected,
408      * or when fSymbols can be set any other way that allows them to be readonly aliases
409      * to non-resource bundle strings,
410      * then regular UnicodeString copies must be used instead of fastCopyFrom().
411      *
412      * @internal
413      */
414     UnicodeString fSymbols[kFormatSymbolCount];
415
416     /**
417      * Non-symbol variable for getConstSymbol(). Always empty.
418      * @internal
419      */
420     UnicodeString fNoSymbol;
421
422     Locale locale;
423
424     char actualLocale[ULOC_FULLNAME_CAPACITY];
425     char validLocale[ULOC_FULLNAME_CAPACITY];
426     const UChar* currPattern;
427
428     UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
429     UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
430     UBool fIsCustomCurrencySymbol;
431     UBool fIsCustomIntlCurrencySymbol;
432 };
433
434 // -------------------------------------
435
436 inline UnicodeString
437 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
438     const UnicodeString *strPtr;
439     if(symbol < kFormatSymbolCount) {
440         strPtr = &fSymbols[symbol];
441     } else {
442         strPtr = &fNoSymbol;
443     }
444     return *strPtr;
445 }
446
447 // See comments above for this function. Not hidden with #ifndef U_HIDE_INTERNAL_API
448 inline const UnicodeString &
449 DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
450     const UnicodeString *strPtr;
451     if(symbol < kFormatSymbolCount) {
452         strPtr = &fSymbols[symbol];
453     } else {
454         strPtr = &fNoSymbol;
455     }
456     return *strPtr;
457 }
458
459 // -------------------------------------
460
461 inline void
462 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
463     if (symbol == kCurrencySymbol) {
464         fIsCustomCurrencySymbol = TRUE;
465     }
466     else if (symbol == kIntlCurrencySymbol) {
467         fIsCustomIntlCurrencySymbol = TRUE;
468     }
469     if(symbol<kFormatSymbolCount) {
470         fSymbols[symbol]=value;
471     }
472
473     // If the zero digit is being set to a known zero digit according to Unicode,
474     // then we automatically set the corresponding 1-9 digits
475     if ( propogateDigits && symbol == kZeroDigitSymbol && value.countChar32() == 1 ) {
476         UChar32 sym = value.char32At(0);
477         if ( u_charDigitValue(sym) == 0 ) {
478             for ( int8_t i = 1 ; i<= 9 ; i++ ) {
479                 sym++;
480                 fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
481             }
482         }
483     }
484 }
485
486 // -------------------------------------
487
488 inline Locale
489 DecimalFormatSymbols::getLocale() const {
490     return locale;
491 }
492
493 #ifndef U_HIDE_INTERNAL_API
494 inline const UChar*
495 DecimalFormatSymbols::getCurrencyPattern() const {
496     return currPattern;
497 }
498 #endif /* U_HIDE_INTERNAL_API */
499
500 U_NAMESPACE_END
501
502 #endif /* #if !UCONFIG_NO_FORMATTING */
503
504 #endif // _DCFMTSYM
505 //eof