Home | History | Annotate | Download | only in unicode
      1 /*
      2 ********************************************************************************
      3 *   Copyright (C) 1997-2014, International Business Machines
      4 *   Corporation and others.  All Rights Reserved.
      5 ********************************************************************************
      6 *
      7 * File DCFMTSYM.H
      8 *
      9 * Modification History:
     10 *
     11 *   Date        Name        Description
     12 *   02/19/97    aliu        Converted from java.
     13 *   03/18/97    clhuang     Updated per C++ implementation.
     14 *   03/27/97    helena      Updated to pass the simple test after code review.
     15 *   08/26/97    aliu        Added currency/intl currency symbol support.
     16 *   07/22/98    stephen     Changed to match C++ style
     17 *                            currencySymbol -> fCurrencySymbol
     18 *                            Constants changed from CAPS to kCaps
     19 *   06/24/99    helena      Integrated Alan's NF enhancements and Java2 bug fixes
     20 *   09/22/00    grhoten     Marked deprecation tags with a pointer to replacement
     21 *                            functions.
     22 ********************************************************************************
     23 */
     24 
     25 #ifndef DCFMTSYM_H
     26 #define DCFMTSYM_H
     27 
     28 #include "unicode/utypes.h"
     29 #include "unicode/uchar.h"
     30 
     31 #if !UCONFIG_NO_FORMATTING
     32 
     33 #include "unicode/uobject.h"
     34 #include "unicode/locid.h"
     35 #include "unicode/unum.h"
     36 
     37 /**
     38  * \file
     39  * \brief C++ API: Symbols for formatting numbers.
     40  */
     41 
     42 
     43 U_NAMESPACE_BEGIN
     44 
     45 /**
     46  * This class represents the set of symbols needed by DecimalFormat
     47  * to format numbers. DecimalFormat creates for itself an instance of
     48  * DecimalFormatSymbols from its locale data.  If you need to change any
     49  * of these symbols, you can get the DecimalFormatSymbols object from
     50  * your DecimalFormat and modify it.
     51  * <P>
     52  * Here are the special characters used in the parts of the
     53  * subpattern, with notes on their usage.
     54  * <pre>
     55  * \code
     56  *        Symbol   Meaning
     57  *          0      a digit
     58  *          #      a digit, zero shows as absent
     59  *          .      placeholder for decimal separator
     60  *          ,      placeholder for grouping separator.
     61  *          ;      separates formats.
     62  *          -      default negative prefix.
     63  *          %      divide by 100 and show as percentage
     64  *          X      any other characters can be used in the prefix or suffix
     65  *          '      used to quote special characters in a prefix or suffix.
     66  * \endcode
     67  *  </pre>
     68  * [Notes]
     69  * <P>
     70  * If there is no explicit negative subpattern, - is prefixed to the
     71  * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
     72  * <P>
     73  * The grouping separator is commonly used for thousands, but in some
     74  * countries for ten-thousands. The interval is a constant number of
     75  * digits between the grouping characters, such as 100,000,000 or 1,0000,0000.
     76  * If you supply a pattern with multiple grouping characters, the interval
     77  * between the last one and the end of the integer is the one that is
     78  * used. So "#,##,###,####" == "######,####" == "##,####,####".
     79  * <P>
     80  * This class only handles localized digits where the 10 digits are
     81  * contiguous in Unicode, from 0 to 9. Other digits sets (such as
     82  * superscripts) would need a different subclass.
     83  */
     84 class U_I18N_API DecimalFormatSymbols : public UObject {
     85 public:
     86     /**
     87      * Constants for specifying a number format symbol.
     88      * @stable ICU 2.0
     89      */
     90     enum ENumberFormatSymbol {
     91         /** The decimal separator */
     92         kDecimalSeparatorSymbol,
     93         /** The grouping separator */
     94         kGroupingSeparatorSymbol,
     95         /** The pattern separator */
     96         kPatternSeparatorSymbol,
     97         /** The percent sign */
     98         kPercentSymbol,
     99         /** Zero*/
    100         kZeroDigitSymbol,
    101         /** Character representing a digit in the pattern */
    102         kDigitSymbol,
    103         /** The minus sign */
    104         kMinusSignSymbol,
    105         /** The plus sign */
    106         kPlusSignSymbol,
    107         /** The currency symbol */
    108         kCurrencySymbol,
    109         /** The international currency symbol */
    110         kIntlCurrencySymbol,
    111         /** The monetary separator */
    112         kMonetarySeparatorSymbol,
    113         /** The exponential symbol */
    114         kExponentialSymbol,
    115         /** Per mill symbol - replaces kPermillSymbol */
    116         kPerMillSymbol,
    117         /** Escape padding character */
    118         kPadEscapeSymbol,
    119         /** Infinity symbol */
    120         kInfinitySymbol,
    121         /** Nan symbol */
    122         kNaNSymbol,
    123         /** Significant digit symbol
    124          * @stable ICU 3.0 */
    125         kSignificantDigitSymbol,
    126         /** The monetary grouping separator
    127          * @stable ICU 3.6
    128          */
    129         kMonetaryGroupingSeparatorSymbol,
    130         /** One
    131          * @stable ICU 4.6
    132          */
    133         kOneDigitSymbol,
    134         /** Two
    135          * @stable ICU 4.6
    136          */
    137         kTwoDigitSymbol,
    138         /** Three
    139          * @stable ICU 4.6
    140          */
    141         kThreeDigitSymbol,
    142         /** Four
    143          * @stable ICU 4.6
    144          */
    145         kFourDigitSymbol,
    146         /** Five
    147          * @stable ICU 4.6
    148          */
    149         kFiveDigitSymbol,
    150         /** Six
    151          * @stable ICU 4.6
    152          */
    153         kSixDigitSymbol,
    154         /** Seven
    155          * @stable ICU 4.6
    156          */
    157         kSevenDigitSymbol,
    158         /** Eight
    159          * @stable ICU 4.6
    160          */
    161         kEightDigitSymbol,
    162         /** Nine
    163          * @stable ICU 4.6
    164          */
    165         kNineDigitSymbol,
    166         /** count symbol constants */
    167         kFormatSymbolCount
    168     };
    169 
    170     /**
    171      * Create a DecimalFormatSymbols object for the given locale.
    172      *
    173      * @param locale    The locale to get symbols for.
    174      * @param status    Input/output parameter, set to success or
    175      *                  failure code upon return.
    176      * @stable ICU 2.0
    177      */
    178     DecimalFormatSymbols(const Locale& locale, UErrorCode& status);
    179 
    180     /**
    181      * Create a DecimalFormatSymbols object for the default locale.
    182      * This constructor will not fail.  If the resource file data is
    183      * not available, it will use hard-coded last-resort data and
    184      * set status to U_USING_FALLBACK_ERROR.
    185      *
    186      * @param status    Input/output parameter, set to success or
    187      *                  failure code upon return.
    188      * @stable ICU 2.0
    189      */
    190     DecimalFormatSymbols(UErrorCode& status);
    191 
    192     // BEGIN android-added: we need a default constructor for performance.
    193     // Proposed for ICU 4.8: http://icu-project.org/trac/ticket/7392
    194     DecimalFormatSymbols();
    195     // END android-added
    196 
    197 #ifndef U_HIDE_DRAFT_API
    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      * @draft ICU 52
    212      */
    213     static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
    214 #endif  /* U_HIDE_DRAFT_API */
    215 
    216     /**
    217      * Copy constructor.
    218      * @stable ICU 2.0
    219      */
    220     DecimalFormatSymbols(const DecimalFormatSymbols&);
    221 
    222     /**
    223      * Assignment operator.
    224      * @stable ICU 2.0
    225      */
    226     DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
    227 
    228     /**
    229      * Destructor.
    230      * @stable ICU 2.0
    231      */
    232     virtual ~DecimalFormatSymbols();
    233 
    234     /**
    235      * Return true if another object is semantically equal to this one.
    236      *
    237      * @param other    the object to be compared with.
    238      * @return         true if another object is semantically equal to this one.
    239      * @stable ICU 2.0
    240      */
    241     UBool operator==(const DecimalFormatSymbols& other) const;
    242 
    243     /**
    244      * Return true if another object is semantically unequal to this one.
    245      *
    246      * @param other    the object to be compared with.
    247      * @return         true if another object is semantically unequal to this one.
    248      * @stable ICU 2.0
    249      */
    250     UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
    251 
    252     /**
    253      * Get one of the format symbols by its enum constant.
    254      * Each symbol is stored as a string so that graphemes
    255      * (characters with modifier letters) can be used.
    256      *
    257      * @param symbol    Constant to indicate a number format symbol.
    258      * @return    the format symbols by the param 'symbol'
    259      * @stable ICU 2.0
    260      */
    261     inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
    262 
    263     /**
    264      * Set one of the format symbols by its enum constant.
    265      * Each symbol is stored as a string so that graphemes
    266      * (characters with modifier letters) can be used.
    267      *
    268      * @param symbol    Constant to indicate a number format symbol.
    269      * @param value     value of the format symbol
    270      * @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
    271      *     The default behavior is to automatically set 1-9 if zero is being set and the value
    272      *     it is being set to corresponds to a known Unicode zero digit.
    273      * @stable ICU 2.0
    274      */
    275     void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
    276 
    277     /**
    278      * Returns the locale for which this object was constructed.
    279      * @stable ICU 2.6
    280      */
    281     inline Locale getLocale() const;
    282 
    283     /**
    284      * Returns the locale for this object. Two flavors are available:
    285      * valid and actual locale.
    286      * @stable ICU 2.8
    287      */
    288     Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
    289 
    290     /**
    291       * Get pattern string for 'CurrencySpacing' that can be applied to
    292       * currency format.
    293       * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
    294       * be empty if there is no data from current locale and its parent locales.
    295       *
    296       * @param type :  UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
    297       * @param beforeCurrency : true if the pattern is for before currency symbol.
    298       *                         false if the pattern is for after currency symbol.
    299       * @param status: Input/output parameter, set to success or
    300       *                  failure code upon return.
    301       * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
    302       *     Return empty string if there is no data for this locale and its parent
    303       *     locales.
    304       * @stable ICU 4.8
    305       */
    306      const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
    307                                                  UBool beforeCurrency,
    308                                                  UErrorCode& status) const;
    309      /**
    310        * Set pattern string for 'CurrencySpacing' that can be applied to
    311        * currency format.
    312        *
    313        * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
    314        * @param beforeCurrency : true if the pattern is for before currency symbol.
    315        *                         false if the pattern is for after currency symbol.
    316        * @param pattern : pattern string to override current setting.
    317        * @stable ICU 4.8
    318        */
    319      void setPatternForCurrencySpacing(UCurrencySpacing type,
    320                                        UBool beforeCurrency,
    321                                        const UnicodeString& pattern);
    322 
    323     /**
    324      * ICU "poor man's RTTI", returns a UClassID for the actual class.
    325      *
    326      * @stable ICU 2.2
    327      */
    328     virtual UClassID getDynamicClassID() const;
    329 
    330     /**
    331      * ICU "poor man's RTTI", returns a UClassID for this class.
    332      *
    333      * @stable ICU 2.2
    334      */
    335     static UClassID U_EXPORT2 getStaticClassID();
    336 
    337 private:
    338     // BEGIN android-removed: we need a default constructor for performance.
    339     // DecimalFormatSymbols(); // default constructor not implemented
    340     // END android-removed
    341 
    342     /**
    343      * Initializes the symbols from the LocaleElements resource bundle.
    344      * Note: The organization of LocaleElements badly needs to be
    345      * cleaned up.
    346      *
    347      * @param locale               The locale to get symbols for.
    348      * @param success              Input/output parameter, set to success or
    349      *                             failure code upon return.
    350      * @param useLastResortData    determine if use last resort data
    351      */
    352     void initialize(const Locale& locale, UErrorCode& success, UBool useLastResortData = FALSE);
    353 
    354     /**
    355      * Initialize the symbols with default values.
    356      */
    357     void initialize();
    358 
    359     void setCurrencyForSymbols();
    360 
    361 public:
    362     /**
    363      * _Internal_ function - more efficient version of getSymbol,
    364      * returning a const reference to one of the symbol strings.
    365      * The returned reference becomes invalid when the symbol is changed
    366      * or when the DecimalFormatSymbols are destroyed.
    367      * ### TODO markus 2002oct11: Consider proposing getConstSymbol() to be really public.
    368      * Note: moved #ifndef U_HIDE_INTERNAL_API after this, it is needed for inline in DecimalFormat
    369      *
    370      * @param symbol Constant to indicate a number format symbol.
    371      * @return the format symbol by the param 'symbol'
    372      * @internal
    373      */
    374     inline const UnicodeString &getConstSymbol(ENumberFormatSymbol symbol) const;
    375 
    376 #ifndef U_HIDE_INTERNAL_API
    377     /**
    378      * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
    379      * @internal
    380      */
    381     inline const UChar* getCurrencyPattern(void) const;
    382 #endif  /* U_HIDE_INTERNAL_API */
    383 
    384 private:
    385     /**
    386      * Private symbol strings.
    387      * They are either loaded from a resource bundle or otherwise owned.
    388      * setSymbol() clones the symbol string.
    389      * Readonly aliases can only come from a resource bundle, so that we can always
    390      * use fastCopyFrom() with them.
    391      *
    392      * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
    393      * from private to protected,
    394      * or when fSymbols can be set any other way that allows them to be readonly aliases
    395      * to non-resource bundle strings,
    396      * then regular UnicodeString copies must be used instead of fastCopyFrom().
    397      *
    398      * @internal
    399      */
    400     UnicodeString fSymbols[kFormatSymbolCount];
    401 
    402     /**
    403      * Non-symbol variable for getConstSymbol(). Always empty.
    404      * @internal
    405      */
    406     UnicodeString fNoSymbol;
    407 
    408     Locale locale;
    409 
    410     char actualLocale[ULOC_FULLNAME_CAPACITY];
    411     char validLocale[ULOC_FULLNAME_CAPACITY];
    412     const UChar* currPattern;
    413 
    414     UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
    415     UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
    416 };
    417 
    418 // -------------------------------------
    419 
    420 inline UnicodeString
    421 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
    422     const UnicodeString *strPtr;
    423     if(symbol < kFormatSymbolCount) {
    424         strPtr = &fSymbols[symbol];
    425     } else {
    426         strPtr = &fNoSymbol;
    427     }
    428     return *strPtr;
    429 }
    430 
    431 #ifndef U_HIDE_INTERNAL_API
    432 
    433 inline const UnicodeString &
    434 DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
    435     const UnicodeString *strPtr;
    436     if(symbol < kFormatSymbolCount) {
    437         strPtr = &fSymbols[symbol];
    438     } else {
    439         strPtr = &fNoSymbol;
    440     }
    441     return *strPtr;
    442 }
    443 
    444 #endif  /* U_HIDE_INTERNAL_API */
    445 
    446 
    447 // -------------------------------------
    448 
    449 inline void
    450 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
    451     if(symbol<kFormatSymbolCount) {
    452         fSymbols[symbol]=value;
    453     }
    454 
    455     // If the zero digit is being set to a known zero digit according to Unicode,
    456     // then we automatically set the corresponding 1-9 digits
    457     if ( propogateDigits && symbol == kZeroDigitSymbol && value.countChar32() == 1 ) {
    458         UChar32 sym = value.char32At(0);
    459         if ( u_charDigitValue(sym) == 0 ) {
    460             for ( int8_t i = 1 ; i<= 9 ; i++ ) {
    461                 sym++;
    462                 fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
    463             }
    464         }
    465     }
    466 }
    467 
    468 // -------------------------------------
    469 
    470 inline Locale
    471 DecimalFormatSymbols::getLocale() const {
    472     return locale;
    473 }
    474 
    475 #ifndef U_HIDE_INTERNAL_API
    476 inline const UChar*
    477 DecimalFormatSymbols::getCurrencyPattern() const {
    478     return currPattern;
    479 }
    480 #endif /* U_HIDE_INTERNAL_API */
    481 
    482 U_NAMESPACE_END
    483 
    484 #endif /* #if !UCONFIG_NO_FORMATTING */
    485 
    486 #endif // _DCFMTSYM
    487 //eof
    488