jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ******************************************************************************** jpayne@69: * Copyright (C) 1997-2016, International Business Machines jpayne@69: * Corporation and others. All Rights Reserved. jpayne@69: ******************************************************************************** jpayne@69: * jpayne@69: * File DCFMTSYM.H jpayne@69: * jpayne@69: * Modification History: jpayne@69: * jpayne@69: * Date Name Description jpayne@69: * 02/19/97 aliu Converted from java. jpayne@69: * 03/18/97 clhuang Updated per C++ implementation. jpayne@69: * 03/27/97 helena Updated to pass the simple test after code review. jpayne@69: * 08/26/97 aliu Added currency/intl currency symbol support. jpayne@69: * 07/22/98 stephen Changed to match C++ style jpayne@69: * currencySymbol -> fCurrencySymbol jpayne@69: * Constants changed from CAPS to kCaps jpayne@69: * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes jpayne@69: * 09/22/00 grhoten Marked deprecation tags with a pointer to replacement jpayne@69: * functions. jpayne@69: ******************************************************************************** jpayne@69: */ jpayne@69: jpayne@69: #ifndef DCFMTSYM_H jpayne@69: #define DCFMTSYM_H jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: #if !UCONFIG_NO_FORMATTING jpayne@69: jpayne@69: #include "unicode/uchar.h" jpayne@69: #include "unicode/uobject.h" jpayne@69: #include "unicode/locid.h" jpayne@69: #include "unicode/numsys.h" jpayne@69: #include "unicode/unum.h" jpayne@69: #include "unicode/unistr.h" jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C++ API: Symbols for formatting numbers. jpayne@69: */ jpayne@69: jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: /** jpayne@69: * This class represents the set of symbols needed by DecimalFormat jpayne@69: * to format numbers. DecimalFormat creates for itself an instance of jpayne@69: * DecimalFormatSymbols from its locale data. If you need to change any jpayne@69: * of these symbols, you can get the DecimalFormatSymbols object from jpayne@69: * your DecimalFormat and modify it. jpayne@69: *
jpayne@69: * Here are the special characters used in the parts of the jpayne@69: * subpattern, with notes on their usage. jpayne@69: *
jpayne@69: * \code jpayne@69: * Symbol Meaning jpayne@69: * 0 a digit jpayne@69: * # a digit, zero shows as absent jpayne@69: * . placeholder for decimal separator jpayne@69: * , placeholder for grouping separator. jpayne@69: * ; separates formats. jpayne@69: * - default negative prefix. jpayne@69: * % divide by 100 and show as percentage jpayne@69: * X any other characters can be used in the prefix or suffix jpayne@69: * ' used to quote special characters in a prefix or suffix. jpayne@69: * \endcode jpayne@69: *jpayne@69: * [Notes] jpayne@69: *
jpayne@69: * If there is no explicit negative subpattern, - is prefixed to the jpayne@69: * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00". jpayne@69: *
jpayne@69: * The grouping separator is commonly used for thousands, but in some jpayne@69: * countries for ten-thousands. The interval is a constant number of jpayne@69: * digits between the grouping characters, such as 100,000,000 or 1,0000,0000. jpayne@69: * If you supply a pattern with multiple grouping characters, the interval jpayne@69: * between the last one and the end of the integer is the one that is jpayne@69: * used. So "#,##,###,####" == "######,####" == "##,####,####". jpayne@69: */ jpayne@69: class U_I18N_API DecimalFormatSymbols : public UObject { jpayne@69: public: jpayne@69: /** jpayne@69: * Constants for specifying a number format symbol. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: enum ENumberFormatSymbol { jpayne@69: /** The decimal separator */ jpayne@69: kDecimalSeparatorSymbol, jpayne@69: /** The grouping separator */ jpayne@69: kGroupingSeparatorSymbol, jpayne@69: /** The pattern separator */ jpayne@69: kPatternSeparatorSymbol, jpayne@69: /** The percent sign */ jpayne@69: kPercentSymbol, jpayne@69: /** Zero*/ jpayne@69: kZeroDigitSymbol, jpayne@69: /** Character representing a digit in the pattern */ jpayne@69: kDigitSymbol, jpayne@69: /** The minus sign */ jpayne@69: kMinusSignSymbol, jpayne@69: /** The plus sign */ jpayne@69: kPlusSignSymbol, jpayne@69: /** The currency symbol */ jpayne@69: kCurrencySymbol, jpayne@69: /** The international currency symbol */ jpayne@69: kIntlCurrencySymbol, jpayne@69: /** The monetary separator */ jpayne@69: kMonetarySeparatorSymbol, jpayne@69: /** The exponential symbol */ jpayne@69: kExponentialSymbol, jpayne@69: /** Per mill symbol - replaces kPermillSymbol */ jpayne@69: kPerMillSymbol, jpayne@69: /** Escape padding character */ jpayne@69: kPadEscapeSymbol, jpayne@69: /** Infinity symbol */ jpayne@69: kInfinitySymbol, jpayne@69: /** Nan symbol */ jpayne@69: kNaNSymbol, jpayne@69: /** Significant digit symbol jpayne@69: * @stable ICU 3.0 */ jpayne@69: kSignificantDigitSymbol, jpayne@69: /** The monetary grouping separator jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: kMonetaryGroupingSeparatorSymbol, jpayne@69: /** One jpayne@69: * @stable ICU 4.6 jpayne@69: */ jpayne@69: kOneDigitSymbol, jpayne@69: /** Two jpayne@69: * @stable ICU 4.6 jpayne@69: */ jpayne@69: kTwoDigitSymbol, jpayne@69: /** Three jpayne@69: * @stable ICU 4.6 jpayne@69: */ jpayne@69: kThreeDigitSymbol, jpayne@69: /** Four jpayne@69: * @stable ICU 4.6 jpayne@69: */ jpayne@69: kFourDigitSymbol, jpayne@69: /** Five jpayne@69: * @stable ICU 4.6 jpayne@69: */ jpayne@69: kFiveDigitSymbol, jpayne@69: /** Six jpayne@69: * @stable ICU 4.6 jpayne@69: */ jpayne@69: kSixDigitSymbol, jpayne@69: /** Seven jpayne@69: * @stable ICU 4.6 jpayne@69: */ jpayne@69: kSevenDigitSymbol, jpayne@69: /** Eight jpayne@69: * @stable ICU 4.6 jpayne@69: */ jpayne@69: kEightDigitSymbol, jpayne@69: /** Nine jpayne@69: * @stable ICU 4.6 jpayne@69: */ jpayne@69: kNineDigitSymbol, jpayne@69: /** Multiplication sign. jpayne@69: * @stable ICU 54 jpayne@69: */ jpayne@69: kExponentMultiplicationSymbol, jpayne@69: /** count symbol constants */ jpayne@69: kFormatSymbolCount = kNineDigitSymbol + 2 jpayne@69: }; jpayne@69: jpayne@69: /** jpayne@69: * Create a DecimalFormatSymbols object for the given locale. jpayne@69: * jpayne@69: * @param locale The locale to get symbols for. jpayne@69: * @param status Input/output parameter, set to success or jpayne@69: * failure code upon return. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: DecimalFormatSymbols(const Locale& locale, UErrorCode& status); jpayne@69: jpayne@69: /** jpayne@69: * Creates a DecimalFormatSymbols instance for the given locale with digits and symbols jpayne@69: * corresponding to the given NumberingSystem. jpayne@69: * jpayne@69: * This constructor behaves equivalently to the normal constructor called with a locale having a jpayne@69: * "numbers=xxxx" keyword specifying the numbering system by name. jpayne@69: * jpayne@69: * In this constructor, the NumberingSystem argument will be used even if the locale has its own jpayne@69: * "numbers=xxxx" keyword. jpayne@69: * jpayne@69: * @param locale The locale to get symbols for. jpayne@69: * @param ns The numbering system. jpayne@69: * @param status Input/output parameter, set to success or jpayne@69: * failure code upon return. jpayne@69: * @stable ICU 60 jpayne@69: */ jpayne@69: DecimalFormatSymbols(const Locale& locale, const NumberingSystem& ns, UErrorCode& status); jpayne@69: jpayne@69: /** jpayne@69: * Create a DecimalFormatSymbols object for the default locale. jpayne@69: * This constructor will not fail. If the resource file data is jpayne@69: * not available, it will use hard-coded last-resort data and jpayne@69: * set status to U_USING_FALLBACK_ERROR. jpayne@69: * jpayne@69: * @param status Input/output parameter, set to success or jpayne@69: * failure code upon return. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: DecimalFormatSymbols(UErrorCode& status); jpayne@69: jpayne@69: /** jpayne@69: * Creates a DecimalFormatSymbols object with last-resort data. jpayne@69: * Intended for callers who cache the symbols data and jpayne@69: * set all symbols on the resulting object. jpayne@69: * jpayne@69: * The last-resort symbols are similar to those for the root data, jpayne@69: * except that the grouping separators are empty, jpayne@69: * the NaN symbol is U+FFFD rather than "NaN", jpayne@69: * and the CurrencySpacing patterns are empty. jpayne@69: * jpayne@69: * @param status Input/output parameter, set to success or jpayne@69: * failure code upon return. jpayne@69: * @return last-resort symbols jpayne@69: * @stable ICU 52 jpayne@69: */ jpayne@69: static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status); jpayne@69: jpayne@69: /** jpayne@69: * Copy constructor. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: DecimalFormatSymbols(const DecimalFormatSymbols&); jpayne@69: jpayne@69: /** jpayne@69: * Assignment operator. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: DecimalFormatSymbols& operator=(const DecimalFormatSymbols&); jpayne@69: jpayne@69: /** jpayne@69: * Destructor. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual ~DecimalFormatSymbols(); jpayne@69: jpayne@69: /** jpayne@69: * Return true if another object is semantically equal to this one. jpayne@69: * jpayne@69: * @param other the object to be compared with. jpayne@69: * @return true if another object is semantically equal to this one. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: UBool operator==(const DecimalFormatSymbols& other) const; jpayne@69: jpayne@69: /** jpayne@69: * Return true if another object is semantically unequal to this one. jpayne@69: * jpayne@69: * @param other the object to be compared with. jpayne@69: * @return true if another object is semantically unequal to this one. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: UBool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); } jpayne@69: jpayne@69: /** jpayne@69: * Get one of the format symbols by its enum constant. jpayne@69: * Each symbol is stored as a string so that graphemes jpayne@69: * (characters with modifier letters) can be used. jpayne@69: * jpayne@69: * @param symbol Constant to indicate a number format symbol. jpayne@69: * @return the format symbols by the param 'symbol' jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const; jpayne@69: jpayne@69: /** jpayne@69: * Set one of the format symbols by its enum constant. jpayne@69: * Each symbol is stored as a string so that graphemes jpayne@69: * (characters with modifier letters) can be used. jpayne@69: * jpayne@69: * @param symbol Constant to indicate a number format symbol. jpayne@69: * @param value value of the format symbol jpayne@69: * @param propogateDigits If false, setting the zero digit will not automatically set 1-9. jpayne@69: * The default behavior is to automatically set 1-9 if zero is being set and the value jpayne@69: * it is being set to corresponds to a known Unicode zero digit. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits); jpayne@69: jpayne@69: #ifndef U_HIDE_INTERNAL_API jpayne@69: /** jpayne@69: * Loads symbols for the specified currency into this instance. jpayne@69: * jpayne@69: * This method is internal. If you think it should be public, file a ticket. jpayne@69: * jpayne@69: * @internal jpayne@69: */ jpayne@69: void setCurrency(const UChar* currency, UErrorCode& status); jpayne@69: #endif // U_HIDE_INTERNAL_API jpayne@69: jpayne@69: /** jpayne@69: * Returns the locale for which this object was constructed. jpayne@69: * @stable ICU 2.6 jpayne@69: */ jpayne@69: inline Locale getLocale() const; jpayne@69: jpayne@69: /** jpayne@69: * Returns the locale for this object. Two flavors are available: jpayne@69: * valid and actual locale. jpayne@69: * @stable ICU 2.8 jpayne@69: */ jpayne@69: Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const; jpayne@69: jpayne@69: /** jpayne@69: * Get pattern string for 'CurrencySpacing' that can be applied to jpayne@69: * currency format. jpayne@69: * This API gets the CurrencySpacing data from ResourceBundle. The pattern can jpayne@69: * be empty if there is no data from current locale and its parent locales. jpayne@69: * jpayne@69: * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT. jpayne@69: * @param beforeCurrency : true if the pattern is for before currency symbol. jpayne@69: * false if the pattern is for after currency symbol. jpayne@69: * @param status: Input/output parameter, set to success or jpayne@69: * failure code upon return. jpayne@69: * @return pattern string for currencyMatch, surroundingMatch or spaceInsert. jpayne@69: * Return empty string if there is no data for this locale and its parent jpayne@69: * locales. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type, jpayne@69: UBool beforeCurrency, jpayne@69: UErrorCode& status) const; jpayne@69: /** jpayne@69: * Set pattern string for 'CurrencySpacing' that can be applied to jpayne@69: * currency format. jpayne@69: * jpayne@69: * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT. jpayne@69: * @param beforeCurrency : true if the pattern is for before currency symbol. jpayne@69: * false if the pattern is for after currency symbol. jpayne@69: * @param pattern : pattern string to override current setting. jpayne@69: * @stable ICU 4.8 jpayne@69: */ jpayne@69: void setPatternForCurrencySpacing(UCurrencySpacing type, jpayne@69: UBool beforeCurrency, jpayne@69: const UnicodeString& pattern); jpayne@69: jpayne@69: /** jpayne@69: * ICU "poor man's RTTI", returns a UClassID for the actual class. jpayne@69: * jpayne@69: * @stable ICU 2.2 jpayne@69: */ jpayne@69: virtual UClassID getDynamicClassID() const; jpayne@69: jpayne@69: /** jpayne@69: * ICU "poor man's RTTI", returns a UClassID for this class. jpayne@69: * jpayne@69: * @stable ICU 2.2 jpayne@69: */ jpayne@69: static UClassID U_EXPORT2 getStaticClassID(); jpayne@69: jpayne@69: private: jpayne@69: DecimalFormatSymbols(); jpayne@69: jpayne@69: /** jpayne@69: * Initializes the symbols from the LocaleElements resource bundle. jpayne@69: * Note: The organization of LocaleElements badly needs to be jpayne@69: * cleaned up. jpayne@69: * jpayne@69: * @param locale The locale to get symbols for. jpayne@69: * @param success Input/output parameter, set to success or jpayne@69: * failure code upon return. jpayne@69: * @param useLastResortData determine if use last resort data jpayne@69: * @param ns The NumberingSystem to use; otherwise, fall jpayne@69: * back to the locale. jpayne@69: */ jpayne@69: void initialize(const Locale& locale, UErrorCode& success, jpayne@69: UBool useLastResortData = FALSE, const NumberingSystem* ns = nullptr); jpayne@69: jpayne@69: /** jpayne@69: * Initialize the symbols with default values. jpayne@69: */ jpayne@69: void initialize(); jpayne@69: jpayne@69: public: jpayne@69: jpayne@69: #ifndef U_HIDE_INTERNAL_API jpayne@69: /** jpayne@69: * @internal For ICU use only jpayne@69: */ jpayne@69: inline UBool isCustomCurrencySymbol() const { jpayne@69: return fIsCustomCurrencySymbol; jpayne@69: } jpayne@69: jpayne@69: /** jpayne@69: * @internal For ICU use only jpayne@69: */ jpayne@69: inline UBool isCustomIntlCurrencySymbol() const { jpayne@69: return fIsCustomIntlCurrencySymbol; jpayne@69: } jpayne@69: jpayne@69: /** jpayne@69: * @internal For ICU use only jpayne@69: */ jpayne@69: inline UChar32 getCodePointZero() const { jpayne@69: return fCodePointZero; jpayne@69: } jpayne@69: #endif /* U_HIDE_INTERNAL_API */ jpayne@69: jpayne@69: /** jpayne@69: * _Internal_ function - more efficient version of getSymbol, jpayne@69: * returning a const reference to one of the symbol strings. jpayne@69: * The returned reference becomes invalid when the symbol is changed jpayne@69: * or when the DecimalFormatSymbols are destroyed. jpayne@69: * Note: moved \#ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat jpayne@69: * jpayne@69: * This is not currently stable API, but if you think it should be stable, jpayne@69: * post a comment on the following ticket and the ICU team will take a look: jpayne@69: * http://bugs.icu-project.org/trac/ticket/13580 jpayne@69: * jpayne@69: * @param symbol Constant to indicate a number format symbol. jpayne@69: * @return the format symbol by the param 'symbol' jpayne@69: * @internal jpayne@69: */ jpayne@69: inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const; jpayne@69: jpayne@69: #ifndef U_HIDE_INTERNAL_API jpayne@69: /** jpayne@69: * Returns the const UnicodeString reference, like getConstSymbol, jpayne@69: * corresponding to the digit with the given value. This is equivalent jpayne@69: * to accessing the symbol from getConstSymbol with the corresponding jpayne@69: * key, such as kZeroDigitSymbol or kOneDigitSymbol. jpayne@69: * jpayne@69: * This is not currently stable API, but if you think it should be stable, jpayne@69: * post a comment on the following ticket and the ICU team will take a look: jpayne@69: * http://bugs.icu-project.org/trac/ticket/13580 jpayne@69: * jpayne@69: * @param digit The digit, an integer between 0 and 9 inclusive. jpayne@69: * If outside the range 0 to 9, the zero digit is returned. jpayne@69: * @return the format symbol for the given digit. jpayne@69: * @internal This API is currently for ICU use only. jpayne@69: */ jpayne@69: inline const UnicodeString& getConstDigitSymbol(int32_t digit) const; jpayne@69: jpayne@69: /** jpayne@69: * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API. jpayne@69: * @internal jpayne@69: */ jpayne@69: inline const char16_t* getCurrencyPattern(void) const; jpayne@69: #endif /* U_HIDE_INTERNAL_API */ jpayne@69: jpayne@69: private: jpayne@69: /** jpayne@69: * Private symbol strings. jpayne@69: * They are either loaded from a resource bundle or otherwise owned. jpayne@69: * setSymbol() clones the symbol string. jpayne@69: * Readonly aliases can only come from a resource bundle, so that we can always jpayne@69: * use fastCopyFrom() with them. jpayne@69: * jpayne@69: * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes jpayne@69: * from private to protected, jpayne@69: * or when fSymbols can be set any other way that allows them to be readonly aliases jpayne@69: * to non-resource bundle strings, jpayne@69: * then regular UnicodeString copies must be used instead of fastCopyFrom(). jpayne@69: * jpayne@69: */ jpayne@69: UnicodeString fSymbols[kFormatSymbolCount]; jpayne@69: jpayne@69: /** jpayne@69: * Non-symbol variable for getConstSymbol(). Always empty. jpayne@69: */ jpayne@69: UnicodeString fNoSymbol; jpayne@69: jpayne@69: /** jpayne@69: * Dealing with code points is faster than dealing with strings when formatting. Because of jpayne@69: * this, we maintain a value containing the zero code point that is used whenever digitStrings jpayne@69: * represents a sequence of ten code points in order. jpayne@69: * jpayne@69: *
If the value stored here is positive, it means that the code point stored in this value jpayne@69: * corresponds to the digitStrings array, and codePointZero can be used instead of the jpayne@69: * digitStrings array for the purposes of efficient formatting; if -1, then digitStrings does jpayne@69: * *not* contain a sequence of code points, and it must be used directly. jpayne@69: * jpayne@69: *
It is assumed that codePointZero always shadows the value in digitStrings. codePointZero
jpayne@69: * should never be set directly; rather, it should be updated only when digitStrings mutates.
jpayne@69: * That is, the flow of information is digitStrings -> codePointZero, not the other way.
jpayne@69: */
jpayne@69: UChar32 fCodePointZero;
jpayne@69:
jpayne@69: Locale locale;
jpayne@69:
jpayne@69: char actualLocale[ULOC_FULLNAME_CAPACITY];
jpayne@69: char validLocale[ULOC_FULLNAME_CAPACITY];
jpayne@69: const char16_t* currPattern;
jpayne@69:
jpayne@69: UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
jpayne@69: UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
jpayne@69: UBool fIsCustomCurrencySymbol;
jpayne@69: UBool fIsCustomIntlCurrencySymbol;
jpayne@69: };
jpayne@69:
jpayne@69: // -------------------------------------
jpayne@69:
jpayne@69: inline UnicodeString
jpayne@69: DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
jpayne@69: const UnicodeString *strPtr;
jpayne@69: if(symbol < kFormatSymbolCount) {
jpayne@69: strPtr = &fSymbols[symbol];
jpayne@69: } else {
jpayne@69: strPtr = &fNoSymbol;
jpayne@69: }
jpayne@69: return *strPtr;
jpayne@69: }
jpayne@69:
jpayne@69: // See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API
jpayne@69: inline const UnicodeString &
jpayne@69: DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
jpayne@69: const UnicodeString *strPtr;
jpayne@69: if(symbol < kFormatSymbolCount) {
jpayne@69: strPtr = &fSymbols[symbol];
jpayne@69: } else {
jpayne@69: strPtr = &fNoSymbol;
jpayne@69: }
jpayne@69: return *strPtr;
jpayne@69: }
jpayne@69:
jpayne@69: #ifndef U_HIDE_INTERNAL_API
jpayne@69: inline const UnicodeString& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit) const {
jpayne@69: if (digit < 0 || digit > 9) {
jpayne@69: digit = 0;
jpayne@69: }
jpayne@69: if (digit == 0) {
jpayne@69: return fSymbols[kZeroDigitSymbol];
jpayne@69: }
jpayne@69: ENumberFormatSymbol key = static_cast