intl/icu/source/i18n/unicode/dcfmtsym.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/i18n/unicode/dcfmtsym.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,479 @@
     1.4 +/*
     1.5 +********************************************************************************
     1.6 +*   Copyright (C) 1997-2013, International Business Machines
     1.7 +*   Corporation and others.  All Rights Reserved.
     1.8 +********************************************************************************
     1.9 +*
    1.10 +* File DCFMTSYM.H
    1.11 +*
    1.12 +* Modification History:
    1.13 +*
    1.14 +*   Date        Name        Description
    1.15 +*   02/19/97    aliu        Converted from java.
    1.16 +*   03/18/97    clhuang     Updated per C++ implementation.
    1.17 +*   03/27/97    helena      Updated to pass the simple test after code review.
    1.18 +*   08/26/97    aliu        Added currency/intl currency symbol support.
    1.19 +*   07/22/98    stephen     Changed to match C++ style
    1.20 +*                            currencySymbol -> fCurrencySymbol
    1.21 +*                            Constants changed from CAPS to kCaps
    1.22 +*   06/24/99    helena      Integrated Alan's NF enhancements and Java2 bug fixes
    1.23 +*   09/22/00    grhoten     Marked deprecation tags with a pointer to replacement
    1.24 +*                            functions.
    1.25 +********************************************************************************
    1.26 +*/
    1.27 +
    1.28 +#ifndef DCFMTSYM_H
    1.29 +#define DCFMTSYM_H
    1.30 +
    1.31 +#include "unicode/utypes.h"
    1.32 +#include "unicode/uchar.h"
    1.33 +
    1.34 +#if !UCONFIG_NO_FORMATTING
    1.35 +
    1.36 +#include "unicode/uobject.h"
    1.37 +#include "unicode/locid.h"
    1.38 +#include "unicode/unum.h"
    1.39 +
    1.40 +/**
    1.41 + * \file
    1.42 + * \brief C++ API: Symbols for formatting numbers.
    1.43 + */
    1.44 +
    1.45 +
    1.46 +U_NAMESPACE_BEGIN
    1.47 +
    1.48 +/**
    1.49 + * This class represents the set of symbols needed by DecimalFormat
    1.50 + * to format numbers. DecimalFormat creates for itself an instance of
    1.51 + * DecimalFormatSymbols from its locale data.  If you need to change any
    1.52 + * of these symbols, you can get the DecimalFormatSymbols object from
    1.53 + * your DecimalFormat and modify it.
    1.54 + * <P>
    1.55 + * Here are the special characters used in the parts of the
    1.56 + * subpattern, with notes on their usage.
    1.57 + * <pre>
    1.58 + * \code
    1.59 + *        Symbol   Meaning
    1.60 + *          0      a digit
    1.61 + *          #      a digit, zero shows as absent
    1.62 + *          .      placeholder for decimal separator
    1.63 + *          ,      placeholder for grouping separator.
    1.64 + *          ;      separates formats.
    1.65 + *          -      default negative prefix.
    1.66 + *          %      divide by 100 and show as percentage
    1.67 + *          X      any other characters can be used in the prefix or suffix
    1.68 + *          '      used to quote special characters in a prefix or suffix.
    1.69 + * \endcode
    1.70 + *  </pre>
    1.71 + * [Notes]
    1.72 + * <P>
    1.73 + * If there is no explicit negative subpattern, - is prefixed to the
    1.74 + * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
    1.75 + * <P>
    1.76 + * The grouping separator is commonly used for thousands, but in some
    1.77 + * countries for ten-thousands. The interval is a constant number of
    1.78 + * digits between the grouping characters, such as 100,000,000 or 1,0000,0000.
    1.79 + * If you supply a pattern with multiple grouping characters, the interval
    1.80 + * between the last one and the end of the integer is the one that is
    1.81 + * used. So "#,##,###,####" == "######,####" == "##,####,####".
    1.82 + * <P>
    1.83 + * This class only handles localized digits where the 10 digits are
    1.84 + * contiguous in Unicode, from 0 to 9. Other digits sets (such as
    1.85 + * superscripts) would need a different subclass.
    1.86 + */
    1.87 +class U_I18N_API DecimalFormatSymbols : public UObject {
    1.88 +public:
    1.89 +    /**
    1.90 +     * Constants for specifying a number format symbol.
    1.91 +     * @stable ICU 2.0
    1.92 +     */
    1.93 +    enum ENumberFormatSymbol {
    1.94 +        /** The decimal separator */
    1.95 +        kDecimalSeparatorSymbol,
    1.96 +        /** The grouping separator */
    1.97 +        kGroupingSeparatorSymbol,
    1.98 +        /** The pattern separator */
    1.99 +        kPatternSeparatorSymbol,
   1.100 +        /** The percent sign */
   1.101 +        kPercentSymbol,
   1.102 +        /** Zero*/
   1.103 +        kZeroDigitSymbol,
   1.104 +        /** Character representing a digit in the pattern */
   1.105 +        kDigitSymbol,
   1.106 +        /** The minus sign */
   1.107 +        kMinusSignSymbol,
   1.108 +        /** The plus sign */
   1.109 +        kPlusSignSymbol,
   1.110 +        /** The currency symbol */
   1.111 +        kCurrencySymbol,
   1.112 +        /** The international currency symbol */
   1.113 +        kIntlCurrencySymbol,
   1.114 +        /** The monetary separator */
   1.115 +        kMonetarySeparatorSymbol,
   1.116 +        /** The exponential symbol */
   1.117 +        kExponentialSymbol,
   1.118 +        /** Per mill symbol - replaces kPermillSymbol */
   1.119 +        kPerMillSymbol,
   1.120 +        /** Escape padding character */
   1.121 +        kPadEscapeSymbol,
   1.122 +        /** Infinity symbol */
   1.123 +        kInfinitySymbol,
   1.124 +        /** Nan symbol */
   1.125 +        kNaNSymbol,
   1.126 +        /** Significant digit symbol
   1.127 +         * @stable ICU 3.0 */
   1.128 +        kSignificantDigitSymbol,
   1.129 +        /** The monetary grouping separator
   1.130 +         * @stable ICU 3.6
   1.131 +         */
   1.132 +        kMonetaryGroupingSeparatorSymbol,
   1.133 +        /** One
   1.134 +         * @stable ICU 4.6
   1.135 +         */
   1.136 +        kOneDigitSymbol,
   1.137 +        /** Two
   1.138 +         * @stable ICU 4.6
   1.139 +         */
   1.140 +        kTwoDigitSymbol,
   1.141 +        /** Three
   1.142 +         * @stable ICU 4.6
   1.143 +         */
   1.144 +        kThreeDigitSymbol,
   1.145 +        /** Four
   1.146 +         * @stable ICU 4.6
   1.147 +         */
   1.148 +        kFourDigitSymbol,
   1.149 +        /** Five
   1.150 +         * @stable ICU 4.6
   1.151 +         */
   1.152 +        kFiveDigitSymbol,
   1.153 +        /** Six
   1.154 +         * @stable ICU 4.6
   1.155 +         */
   1.156 +        kSixDigitSymbol,
   1.157 +        /** Seven
   1.158 +         * @stable ICU 4.6
   1.159 +         */
   1.160 +        kSevenDigitSymbol,
   1.161 +        /** Eight
   1.162 +         * @stable ICU 4.6
   1.163 +         */
   1.164 +        kEightDigitSymbol,
   1.165 +        /** Nine
   1.166 +         * @stable ICU 4.6
   1.167 +         */
   1.168 +        kNineDigitSymbol,
   1.169 +        /** count symbol constants */
   1.170 +        kFormatSymbolCount
   1.171 +    };
   1.172 +
   1.173 +    /**
   1.174 +     * Create a DecimalFormatSymbols object for the given locale.
   1.175 +     *
   1.176 +     * @param locale    The locale to get symbols for.
   1.177 +     * @param status    Input/output parameter, set to success or
   1.178 +     *                  failure code upon return.
   1.179 +     * @stable ICU 2.0
   1.180 +     */
   1.181 +    DecimalFormatSymbols(const Locale& locale, UErrorCode& status);
   1.182 +
   1.183 +    /**
   1.184 +     * Create a DecimalFormatSymbols object for the default locale.
   1.185 +     * This constructor will not fail.  If the resource file data is
   1.186 +     * not available, it will use hard-coded last-resort data and
   1.187 +     * set status to U_USING_FALLBACK_ERROR.
   1.188 +     *
   1.189 +     * @param status    Input/output parameter, set to success or
   1.190 +     *                  failure code upon return.
   1.191 +     * @stable ICU 2.0
   1.192 +     */
   1.193 +    DecimalFormatSymbols(UErrorCode& status);
   1.194 +
   1.195 +#ifndef U_HIDE_DRAFT_API
   1.196 +    /**
   1.197 +     * Creates a DecimalFormatSymbols object with last-resort data.
   1.198 +     * Intended for callers who cache the symbols data and
   1.199 +     * set all symbols on the resulting object.
   1.200 +     *
   1.201 +     * The last-resort symbols are similar to those for the root data,
   1.202 +     * except that the grouping separators are empty,
   1.203 +     * the NaN symbol is U+FFFD rather than "NaN",
   1.204 +     * and the CurrencySpacing patterns are empty.
   1.205 +     *
   1.206 +     * @param status    Input/output parameter, set to success or
   1.207 +     *                  failure code upon return.
   1.208 +     * @return last-resort symbols
   1.209 +     * @draft ICU 52
   1.210 +     */
   1.211 +    static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
   1.212 +#endif  /* U_HIDE_DRAFT_API */
   1.213 +
   1.214 +    /**
   1.215 +     * Copy constructor.
   1.216 +     * @stable ICU 2.0
   1.217 +     */
   1.218 +    DecimalFormatSymbols(const DecimalFormatSymbols&);
   1.219 +
   1.220 +    /**
   1.221 +     * Assignment operator.
   1.222 +     * @stable ICU 2.0
   1.223 +     */
   1.224 +    DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
   1.225 +
   1.226 +    /**
   1.227 +     * Destructor.
   1.228 +     * @stable ICU 2.0
   1.229 +     */
   1.230 +    virtual ~DecimalFormatSymbols();
   1.231 +
   1.232 +    /**
   1.233 +     * Return true if another object is semantically equal to this one.
   1.234 +     *
   1.235 +     * @param other    the object to be compared with.
   1.236 +     * @return         true if another object is semantically equal to this one.
   1.237 +     * @stable ICU 2.0
   1.238 +     */
   1.239 +    UBool operator==(const DecimalFormatSymbols& other) const;
   1.240 +
   1.241 +    /**
   1.242 +     * Return true if another object is semantically unequal to this one.
   1.243 +     *
   1.244 +     * @param other    the object to be compared with.
   1.245 +     * @return         true if another object is semantically unequal to this one.
   1.246 +     * @stable ICU 2.0
   1.247 +     */
   1.248 +    UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
   1.249 +
   1.250 +    /**
   1.251 +     * Get one of the format symbols by its enum constant.
   1.252 +     * Each symbol is stored as a string so that graphemes
   1.253 +     * (characters with modifier letters) can be used.
   1.254 +     *
   1.255 +     * @param symbol    Constant to indicate a number format symbol.
   1.256 +     * @return    the format symbols by the param 'symbol'
   1.257 +     * @stable ICU 2.0
   1.258 +     */
   1.259 +    inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
   1.260 +
   1.261 +    /**
   1.262 +     * Set one of the format symbols by its enum constant.
   1.263 +     * Each symbol is stored as a string so that graphemes
   1.264 +     * (characters with modifier letters) can be used.
   1.265 +     *
   1.266 +     * @param symbol    Constant to indicate a number format symbol.
   1.267 +     * @param value     value of the format symbol
   1.268 +     * @param propogateDigits If false, setting the zero digit will not automatically set 1-9.
   1.269 +     *     The default behavior is to automatically set 1-9 if zero is being set and the value
   1.270 +     *     it is being set to corresponds to a known Unicode zero digit.
   1.271 +     * @stable ICU 2.0
   1.272 +     */
   1.273 +    void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits);
   1.274 +
   1.275 +    /**
   1.276 +     * Returns the locale for which this object was constructed.
   1.277 +     * @stable ICU 2.6
   1.278 +     */
   1.279 +    inline Locale getLocale() const;
   1.280 +
   1.281 +    /**
   1.282 +     * Returns the locale for this object. Two flavors are available:
   1.283 +     * valid and actual locale.
   1.284 +     * @stable ICU 2.8
   1.285 +     */
   1.286 +    Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
   1.287 +
   1.288 +    /**
   1.289 +      * Get pattern string for 'CurrencySpacing' that can be applied to
   1.290 +      * currency format.
   1.291 +      * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
   1.292 +      * be empty if there is no data from current locale and its parent locales.
   1.293 +      *
   1.294 +      * @param type :  UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
   1.295 +      * @param beforeCurrency : true if the pattern is for before currency symbol.
   1.296 +      *                         false if the pattern is for after currency symbol.
   1.297 +      * @param status: Input/output parameter, set to success or
   1.298 +      *                  failure code upon return.
   1.299 +      * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
   1.300 +      *     Return empty string if there is no data for this locale and its parent
   1.301 +      *     locales.
   1.302 +      * @stable ICU 4.8
   1.303 +      */
   1.304 +     const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
   1.305 +                                                 UBool beforeCurrency,
   1.306 +                                                 UErrorCode& status) const;
   1.307 +     /**
   1.308 +       * Set pattern string for 'CurrencySpacing' that can be applied to
   1.309 +       * currency format.
   1.310 +       *
   1.311 +       * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
   1.312 +       * @param beforeCurrency : true if the pattern is for before currency symbol.
   1.313 +       *                         false if the pattern is for after currency symbol.
   1.314 +       * @param pattern : pattern string to override current setting.
   1.315 +       * @stable ICU 4.8
   1.316 +       */
   1.317 +     void setPatternForCurrencySpacing(UCurrencySpacing type,
   1.318 +                                       UBool beforeCurrency,
   1.319 +                                       const UnicodeString& pattern);
   1.320 +
   1.321 +    /**
   1.322 +     * ICU "poor man's RTTI", returns a UClassID for the actual class.
   1.323 +     *
   1.324 +     * @stable ICU 2.2
   1.325 +     */
   1.326 +    virtual UClassID getDynamicClassID() const;
   1.327 +
   1.328 +    /**
   1.329 +     * ICU "poor man's RTTI", returns a UClassID for this class.
   1.330 +     *
   1.331 +     * @stable ICU 2.2
   1.332 +     */
   1.333 +    static UClassID U_EXPORT2 getStaticClassID();
   1.334 +
   1.335 +private:
   1.336 +    DecimalFormatSymbols();
   1.337 +
   1.338 +    /**
   1.339 +     * Initializes the symbols from the LocaleElements resource bundle.
   1.340 +     * Note: The organization of LocaleElements badly needs to be
   1.341 +     * cleaned up.
   1.342 +     *
   1.343 +     * @param locale               The locale to get symbols for.
   1.344 +     * @param success              Input/output parameter, set to success or
   1.345 +     *                             failure code upon return.
   1.346 +     * @param useLastResortData    determine if use last resort data
   1.347 +     */
   1.348 +    void initialize(const Locale& locale, UErrorCode& success, UBool useLastResortData = FALSE);
   1.349 +
   1.350 +    /**
   1.351 +     * Initialize the symbols with default values.
   1.352 +     */
   1.353 +    void initialize();
   1.354 +
   1.355 +    void setCurrencyForSymbols();
   1.356 +
   1.357 +public:
   1.358 +#ifndef U_HIDE_INTERNAL_API
   1.359 +    /**
   1.360 +     * _Internal_ function - more efficient version of getSymbol,
   1.361 +     * returning a const reference to one of the symbol strings.
   1.362 +     * The returned reference becomes invalid when the symbol is changed
   1.363 +     * or when the DecimalFormatSymbols are destroyed.
   1.364 +     * ### TODO markus 2002oct11: Consider proposing getConstSymbol() to be really public.
   1.365 +     *
   1.366 +     * @param symbol Constant to indicate a number format symbol.
   1.367 +     * @return the format symbol by the param 'symbol'
   1.368 +     * @internal
   1.369 +     */
   1.370 +    inline const UnicodeString &getConstSymbol(ENumberFormatSymbol symbol) const;
   1.371 +
   1.372 +    /**
   1.373 +     * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
   1.374 +     * @internal
   1.375 +     */
   1.376 +    inline const UChar* getCurrencyPattern(void) const;
   1.377 +#endif  /* U_HIDE_INTERNAL_API */
   1.378 +
   1.379 +private:
   1.380 +    /**
   1.381 +     * Private symbol strings.
   1.382 +     * They are either loaded from a resource bundle or otherwise owned.
   1.383 +     * setSymbol() clones the symbol string.
   1.384 +     * Readonly aliases can only come from a resource bundle, so that we can always
   1.385 +     * use fastCopyFrom() with them.
   1.386 +     *
   1.387 +     * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
   1.388 +     * from private to protected,
   1.389 +     * or when fSymbols can be set any other way that allows them to be readonly aliases
   1.390 +     * to non-resource bundle strings,
   1.391 +     * then regular UnicodeString copies must be used instead of fastCopyFrom().
   1.392 +     *
   1.393 +     * @internal
   1.394 +     */
   1.395 +    UnicodeString fSymbols[kFormatSymbolCount];
   1.396 +
   1.397 +    /**
   1.398 +     * Non-symbol variable for getConstSymbol(). Always empty.
   1.399 +     * @internal
   1.400 +     */
   1.401 +    UnicodeString fNoSymbol;
   1.402 +
   1.403 +    Locale locale;
   1.404 +
   1.405 +    char actualLocale[ULOC_FULLNAME_CAPACITY];
   1.406 +    char validLocale[ULOC_FULLNAME_CAPACITY];
   1.407 +    const UChar* currPattern;
   1.408 +
   1.409 +    UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
   1.410 +    UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
   1.411 +};
   1.412 +
   1.413 +// -------------------------------------
   1.414 +
   1.415 +inline UnicodeString
   1.416 +DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
   1.417 +    const UnicodeString *strPtr;
   1.418 +    if(symbol < kFormatSymbolCount) {
   1.419 +        strPtr = &fSymbols[symbol];
   1.420 +    } else {
   1.421 +        strPtr = &fNoSymbol;
   1.422 +    }
   1.423 +    return *strPtr;
   1.424 +}
   1.425 +
   1.426 +#ifndef U_HIDE_INTERNAL_API
   1.427 +
   1.428 +inline const UnicodeString &
   1.429 +DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
   1.430 +    const UnicodeString *strPtr;
   1.431 +    if(symbol < kFormatSymbolCount) {
   1.432 +        strPtr = &fSymbols[symbol];
   1.433 +    } else {
   1.434 +        strPtr = &fNoSymbol;
   1.435 +    }
   1.436 +    return *strPtr;
   1.437 +}
   1.438 +
   1.439 +#endif  /* U_HIDE_INTERNAL_API */
   1.440 +
   1.441 +
   1.442 +// -------------------------------------
   1.443 +
   1.444 +inline void
   1.445 +DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
   1.446 +    if(symbol<kFormatSymbolCount) {
   1.447 +        fSymbols[symbol]=value;
   1.448 +    }
   1.449 +
   1.450 +    // If the zero digit is being set to a known zero digit according to Unicode,
   1.451 +    // then we automatically set the corresponding 1-9 digits
   1.452 +    if ( propogateDigits && symbol == kZeroDigitSymbol && value.countChar32() == 1 ) {
   1.453 +        UChar32 sym = value.char32At(0);
   1.454 +        if ( u_charDigitValue(sym) == 0 ) {
   1.455 +            for ( int8_t i = 1 ; i<= 9 ; i++ ) {
   1.456 +                sym++;
   1.457 +                fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
   1.458 +            }
   1.459 +        }
   1.460 +    }
   1.461 +}
   1.462 +
   1.463 +// -------------------------------------
   1.464 +
   1.465 +inline Locale
   1.466 +DecimalFormatSymbols::getLocale() const {
   1.467 +    return locale;
   1.468 +}
   1.469 +
   1.470 +#ifndef U_HIDE_INTERNAL_API
   1.471 +inline const UChar*
   1.472 +DecimalFormatSymbols::getCurrencyPattern() const {
   1.473 +    return currPattern;
   1.474 +}
   1.475 +#endif /* U_HIDE_INTERNAL_API */
   1.476 +
   1.477 +U_NAMESPACE_END
   1.478 +
   1.479 +#endif /* #if !UCONFIG_NO_FORMATTING */
   1.480 +
   1.481 +#endif // _DCFMTSYM
   1.482 +//eof

mercurial