Home | History | Annotate | Download | only in unicode
      1 /*
      2 ********************************************************************************
      3 *   Copyright (C) 1997-2013, 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 #ifndef U_HIDE_DRAFT_API
    193     /**
    194      * Creates a DecimalFormatSymbols object with last-resort data.
    195      * Intended for callers who cache the symbols data and
    196      * set all symbols on the resulting object.
    197      *
    198      * The last-resort symbols are similar to those for the root data,
    199      * except that the grouping separators are empty,
    200      * the NaN symbol is U+FFFD rather than "NaN",
    201      * and the CurrencySpacing patterns are empty.
    202      *
    203      * @param status    Input/output parameter, set to success or
    204      *                  failure code upon return.
    205      * @return last-resort symbols
    206      * @draft ICU 52
    207      */
    208     static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
    209 #endif  /* U_HIDE_DRAFT_API */
    210 
    211     /**
    212      * Copy constructor.
    213      * @stable ICU 2.0
    214      */
    215     DecimalFormatSymbols(const DecimalFormatSymbols&);
    216 
    217     /**
    218      * Assignment operator.
    219      * @stable ICU 2.0
    220      */
    221     DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
    222 
    223     /**
    224      * Destructor.
    225      * @stable ICU 2.0
    226      */
    227     virtual ~DecimalFormatSymbols();
    228 
    229     /**
    230      * Return true if another object is semantically equal to this one.
    231      *
    232      * @param other    the object to be compared with.
    233      * @return         true if another object is semantically equal to this one.
    234      * @stable ICU 2.0
    235      */
    236     UBool operator==(const DecimalFormatSymbols& other) const;
    237 
    238     /**
    239      * Return true if another object is semantically unequal to this one.
    240      *
    241      * @param other    the object to be compared with.
    242      * @return         true if another object is semantically unequal to this one.
    243      * @stable ICU 2.0
    244      */
    245     UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
    246 
    247     /**
    248      * Get one of the format symbols by its enum constant.
    249      * Each symbol is stored as a string so that graphemes
    250      * (characters with modifier letters) can be used.
    251      *
    252      * @param symbol    Constant to indicate a number format symbol.
    253      * @return    the format symbols by the param 'symbol'
    254      * @stable ICU 2.0
    255      */
    256     inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
    257 
    258     /**
    259      * Set one of the format symbols by its enum constant.
    260      * Each symbol is stored as a string so that graphemes
    261      * (characters with modifier letters) can be used.
    262      *
    263      * @param symbol    Constant to indicate a number format symbol.
    264      * @param value     value of the format symbol
    265      * @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
    266      *     The default behavior is to automatically set 1-9 if zero is being set and the value
    267      *     it is being set to corresponds to a known Unicode zero digit.
    268      * @stable ICU 2.0
    269      */
    270     void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
    271 
    272     /**
    273      * Returns the locale for which this object was constructed.
    274      * @stable ICU 2.6
    275      */
    276     inline Locale getLocale() const;
    277 
    278     /**
    279      * Returns the locale for this object. Two flavors are available:
    280      * valid and actual locale.
    281      * @stable ICU 2.8
    282      */
    283     Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
    284 
    285     /**
    286       * Get pattern string for 'CurrencySpacing' that can be applied to
    287       * currency format.
    288       * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
    289       * be empty if there is no data from current locale and its parent locales.
    290       *
    291       * @param type :  UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
    292       * @param beforeCurrency : true if the pattern is for before currency symbol.
    293       *                         false if the pattern is for after currency symbol.
    294       * @param status: Input/output parameter, set to success or
    295       *                  failure code upon return.
    296       * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
    297       *     Return empty string if there is no data for this locale and its parent
    298       *     locales.
    299       * @stable ICU 4.8
    300       */
    301      const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
    302                                                  UBool beforeCurrency,
    303                                                  UErrorCode& status) const;
    304      /**
    305        * Set pattern string for 'CurrencySpacing' that can be applied to
    306        * currency format.
    307        *
    308        * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
    309        * @param beforeCurrency : true if the pattern is for before currency symbol.
    310        *                         false if the pattern is for after currency symbol.
    311        * @param pattern : pattern string to override current setting.
    312        * @stable ICU 4.8
    313        */
    314      void setPatternForCurrencySpacing(UCurrencySpacing type,
    315                                        UBool beforeCurrency,
    316                                        const UnicodeString& pattern);
    317 
    318     /**
    319      * ICU "poor man's RTTI", returns a UClassID for the actual class.
    320      *
    321      * @stable ICU 2.2
    322      */
    323     virtual UClassID getDynamicClassID() const;
    324 
    325     /**
    326      * ICU "poor man's RTTI", returns a UClassID for this class.
    327      *
    328      * @stable ICU 2.2
    329      */
    330     static UClassID U_EXPORT2 getStaticClassID();
    331 
    332 private:
    333     DecimalFormatSymbols();
    334 
    335     /**
    336      * Initializes the symbols from the LocaleElements resource bundle.
    337      * Note: The organization of LocaleElements badly needs to be
    338      * cleaned up.
    339      *
    340      * @param locale               The locale to get symbols for.
    341      * @param success              Input/output parameter, set to success or
    342      *                             failure code upon return.
    343      * @param useLastResortData    determine if use last resort data
    344      */
    345     void initialize(const Locale& locale, UErrorCode& success, UBool useLastResortData = FALSE);
    346 
    347     /**
    348      * Initialize the symbols with default values.
    349      */
    350     void initialize();
    351 
    352     void setCurrencyForSymbols();
    353 
    354 public:
    355 #ifndef U_HIDE_INTERNAL_API
    356     /**
    357      * _Internal_ function - more efficient version of getSymbol,
    358      * returning a const reference to one of the symbol strings.
    359      * The returned reference becomes invalid when the symbol is changed
    360      * or when the DecimalFormatSymbols are destroyed.
    361      * ### TODO markus 2002oct11: Consider proposing getConstSymbol() to be really public.
    362      *
    363      * @param symbol Constant to indicate a number format symbol.
    364      * @return the format symbol by the param 'symbol'
    365      * @internal
    366      */
    367     inline const UnicodeString &getConstSymbol(ENumberFormatSymbol symbol) const;
    368 
    369     /**
    370      * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
    371      * @internal
    372      */
    373     inline const UChar* getCurrencyPattern(void) const;
    374 #endif  /* U_HIDE_INTERNAL_API */
    375 
    376 private:
    377     /**
    378      * Private symbol strings.
    379      * They are either loaded from a resource bundle or otherwise owned.
    380      * setSymbol() clones the symbol string.
    381      * Readonly aliases can only come from a resource bundle, so that we can always
    382      * use fastCopyFrom() with them.
    383      *
    384      * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
    385      * from private to protected,
    386      * or when fSymbols can be set any other way that allows them to be readonly aliases
    387      * to non-resource bundle strings,
    388      * then regular UnicodeString copies must be used instead of fastCopyFrom().
    389      *
    390      * @internal
    391      */
    392     UnicodeString fSymbols[kFormatSymbolCount];
    393 
    394     /**
    395      * Non-symbol variable for getConstSymbol(). Always empty.
    396      * @internal
    397      */
    398     UnicodeString fNoSymbol;
    399 
    400     Locale locale;
    401 
    402     char actualLocale[ULOC_FULLNAME_CAPACITY];
    403     char validLocale[ULOC_FULLNAME_CAPACITY];
    404     const UChar* currPattern;
    405 
    406     UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
    407     UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
    408 };
    409 
    410 // -------------------------------------
    411 
    412 inline UnicodeString
    413 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
    414     const UnicodeString *strPtr;
    415     if(symbol < kFormatSymbolCount) {
    416         strPtr = &fSymbols[symbol];
    417     } else {
    418         strPtr = &fNoSymbol;
    419     }
    420     return *strPtr;
    421 }
    422 
    423 #ifndef U_HIDE_INTERNAL_API
    424 
    425 inline const UnicodeString &
    426 DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
    427     const UnicodeString *strPtr;
    428     if(symbol < kFormatSymbolCount) {
    429         strPtr = &fSymbols[symbol];
    430     } else {
    431         strPtr = &fNoSymbol;
    432     }
    433     return *strPtr;
    434 }
    435 
    436 #endif  /* U_HIDE_INTERNAL_API */
    437 
    438 
    439 // -------------------------------------
    440 
    441 inline void
    442 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
    443     if(symbol<kFormatSymbolCount) {
    444         fSymbols[symbol]=value;
    445     }
    446 
    447     // If the zero digit is being set to a known zero digit according to Unicode,
    448     // then we automatically set the corresponding 1-9 digits
    449     if ( propogateDigits && symbol == kZeroDigitSymbol && value.countChar32() == 1 ) {
    450         UChar32 sym = value.char32At(0);
    451         if ( u_charDigitValue(sym) == 0 ) {
    452             for ( int8_t i = 1 ; i<= 9 ; i++ ) {
    453                 sym++;
    454                 fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
    455             }
    456         }
    457     }
    458 }
    459 
    460 // -------------------------------------
    461 
    462 inline Locale
    463 DecimalFormatSymbols::getLocale() const {
    464     return locale;
    465 }
    466 
    467 #ifndef U_HIDE_INTERNAL_API
    468 inline const UChar*
    469 DecimalFormatSymbols::getCurrencyPattern() const {
    470     return currPattern;
    471 }
    472 #endif /* U_HIDE_INTERNAL_API */
    473 
    474 U_NAMESPACE_END
    475 
    476 #endif /* #if !UCONFIG_NO_FORMATTING */
    477 
    478 #endif // _DCFMTSYM
    479 //eof
    480