X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/374ca955a76ecab1204ca8bfa63ff9238d998416..0f5d89e82340278ed3d7d50029f37cab2c41a57e:/icuSources/i18n/unicode/dcfmtsym.h diff --git a/icuSources/i18n/unicode/dcfmtsym.h b/icuSources/i18n/unicode/dcfmtsym.h index 80375d70..090976d6 100644 --- a/icuSources/i18n/unicode/dcfmtsym.h +++ b/icuSources/i18n/unicode/dcfmtsym.h @@ -1,19 +1,21 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************** -* Copyright (C) 1997-2004, International Business Machines +* Copyright (C) 1997-2016, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************** * * File DCFMTSYM.H * * Modification History: -* +* * Date Name Description * 02/19/97 aliu Converted from java. * 03/18/97 clhuang Updated per C++ implementation. * 03/27/97 helena Updated to pass the simple test after code review. * 08/26/97 aliu Added currency/intl currency symbol support. -* 07/22/98 stephen Changed to match C++ style +* 07/22/98 stephen Changed to match C++ style * currencySymbol -> fCurrencySymbol * Constants changed from CAPS to kCaps * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes @@ -21,17 +23,28 @@ * functions. ******************************************************************************** */ - + #ifndef DCFMTSYM_H #define DCFMTSYM_H - + #include "unicode/utypes.h" +#include "unicode/uchar.h" #if !UCONFIG_NO_FORMATTING #include "unicode/uobject.h" #include "unicode/locid.h" +#include "unicode/numsys.h" +#include "unicode/unum.h" +#include "unicode/unistr.h" +/** + * \file + * \brief C++ API: Symbols for formatting numbers. + */ + + +#if U_SHOW_CPLUSPLUS_API U_NAMESPACE_BEGIN /** @@ -68,10 +81,6 @@ U_NAMESPACE_BEGIN * If you supply a pattern with multiple grouping characters, the interval * between the last one and the end of the integer is the one that is * used. So "#,##,###,####" == "######,####" == "##,####,####". - *

- * This class only handles localized digits where the 10 digits are - * contiguous in Unicode, from 0 to 9. Other digits sets (such as - * superscripts) would need a different subclass. */ class U_I18N_API DecimalFormatSymbols : public UObject { public: @@ -113,10 +122,54 @@ public: /** Nan symbol */ kNaNSymbol, /** Significant digit symbol - * @draft ICU 3.0 */ + * @stable ICU 3.0 */ kSignificantDigitSymbol, + /** The monetary grouping separator + * @stable ICU 3.6 + */ + kMonetaryGroupingSeparatorSymbol, + /** One + * @stable ICU 4.6 + */ + kOneDigitSymbol, + /** Two + * @stable ICU 4.6 + */ + kTwoDigitSymbol, + /** Three + * @stable ICU 4.6 + */ + kThreeDigitSymbol, + /** Four + * @stable ICU 4.6 + */ + kFourDigitSymbol, + /** Five + * @stable ICU 4.6 + */ + kFiveDigitSymbol, + /** Six + * @stable ICU 4.6 + */ + kSixDigitSymbol, + /** Seven + * @stable ICU 4.6 + */ + kSevenDigitSymbol, + /** Eight + * @stable ICU 4.6 + */ + kEightDigitSymbol, + /** Nine + * @stable ICU 4.6 + */ + kNineDigitSymbol, + /** Multiplication sign. + * @stable ICU 54 + */ + kExponentMultiplicationSymbol, /** count symbol constants */ - kFormatSymbolCount + kFormatSymbolCount = kNineDigitSymbol + 2 }; /** @@ -129,6 +182,26 @@ public: */ DecimalFormatSymbols(const Locale& locale, UErrorCode& status); +#ifndef U_HIDE_DRAFT_API + /** + * Creates a DecimalFormatSymbols instance for the given locale with digits and symbols + * corresponding to the given NumberingSystem. + * + * This constructor behaves equivalently to the normal constructor called with a locale having a + * "numbers=xxxx" keyword specifying the numbering system by name. + * + * In this constructor, the NumberingSystem argument will be used even if the locale has its own + * "numbers=xxxx" keyword. + * + * @param locale The locale to get symbols for. + * @param ns The numbering system. + * @param status Input/output parameter, set to success or + * failure code upon return. + * @draft ICU 60 + */ + DecimalFormatSymbols(const Locale& locale, const NumberingSystem& ns, UErrorCode& status); +#endif /* U_HIDE_DRAFT_API */ + /** * Create a DecimalFormatSymbols object for the default locale. * This constructor will not fail. If the resource file data is @@ -139,7 +212,24 @@ public: * failure code upon return. * @stable ICU 2.0 */ - DecimalFormatSymbols( UErrorCode& status); + DecimalFormatSymbols(UErrorCode& status); + + /** + * Creates a DecimalFormatSymbols object with last-resort data. + * Intended for callers who cache the symbols data and + * set all symbols on the resulting object. + * + * The last-resort symbols are similar to those for the root data, + * except that the grouping separators are empty, + * the NaN symbol is U+FFFD rather than "NaN", + * and the CurrencySpacing patterns are empty. + * + * @param status Input/output parameter, set to success or + * failure code upon return. + * @return last-resort symbols + * @stable ICU 52 + */ + static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status); /** * Copy constructor. @@ -180,7 +270,7 @@ public: /** * Get one of the format symbols by its enum constant. * Each symbol is stored as a string so that graphemes - * (characters with modifyer letters) can be used. + * (characters with modifier letters) can be used. * * @param symbol Constant to indicate a number format symbol. * @return the format symbols by the param 'symbol' @@ -191,13 +281,16 @@ public: /** * Set one of the format symbols by its enum constant. * Each symbol is stored as a string so that graphemes - * (characters with modifyer letters) can be used. + * (characters with modifier letters) can be used. * * @param symbol Constant to indicate a number format symbol. - * @param value value of the format sybmol + * @param value value of the format symbol + * @param propogateDigits If false, setting the zero digit will not automatically set 1-9. + * The default behavior is to automatically set 1-9 if zero is being set and the value + * it is being set to corresponds to a known Unicode zero digit. * @stable ICU 2.0 */ - void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value); + void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits); /** * Returns the locale for which this object was constructed. @@ -208,10 +301,43 @@ public: /** * Returns the locale for this object. Two flavors are available: * valid and actual locale. - * @draft ICU 2.8 likely to change in ICU 3.0, based on feedback + * @stable ICU 2.8 */ Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const; + /** + * Get pattern string for 'CurrencySpacing' that can be applied to + * currency format. + * This API gets the CurrencySpacing data from ResourceBundle. The pattern can + * be empty if there is no data from current locale and its parent locales. + * + * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT. + * @param beforeCurrency : true if the pattern is for before currency symbol. + * false if the pattern is for after currency symbol. + * @param status: Input/output parameter, set to success or + * failure code upon return. + * @return pattern string for currencyMatch, surroundingMatch or spaceInsert. + * Return empty string if there is no data for this locale and its parent + * locales. + * @stable ICU 4.8 + */ + const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type, + UBool beforeCurrency, + UErrorCode& status) const; + /** + * Set pattern string for 'CurrencySpacing' that can be applied to + * currency format. + * + * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT. + * @param beforeCurrency : true if the pattern is for before currency symbol. + * false if the pattern is for after currency symbol. + * @param pattern : pattern string to override current setting. + * @stable ICU 4.8 + */ + void setPatternForCurrencySpacing(UCurrencySpacing type, + UBool beforeCurrency, + const UnicodeString& pattern); + /** * ICU "poor man's RTTI", returns a UClassID for the actual class. * @@ -227,7 +353,7 @@ public: static UClassID U_EXPORT2 getStaticClassID(); private: - DecimalFormatSymbols(); // default constructor not implemented + DecimalFormatSymbols(); /** * Initializes the symbols from the LocaleElements resource bundle. @@ -238,17 +364,11 @@ private: * @param success Input/output parameter, set to success or * failure code upon return. * @param useLastResortData determine if use last resort data + * @param ns The NumberingSystem to use; otherwise, fall + * back to the locale. */ - void initialize(const Locale& locale, UErrorCode& success, UBool useLastResortData = FALSE); - - /** - * Initialize the symbols from the given array of UnicodeStrings. - * The array must be of the correct size. - * - * @param numberElements the number format symbols - * @param numberElementsLength length of numberElements - */ - void initialize(const UChar** numberElements, int32_t *numberElementsStrLen, int32_t numberElementsLength); + void initialize(const Locale& locale, UErrorCode& success, + UBool useLastResortData = FALSE, const NumberingSystem* ns = nullptr); /** * Initialize the symbols with default values. @@ -258,18 +378,71 @@ private: void setCurrencyForSymbols(); public: + +#ifndef U_HIDE_INTERNAL_API + /** + * @internal For ICU use only + */ + inline UBool isCustomCurrencySymbol() const { + return fIsCustomCurrencySymbol; + } + + /** + * @internal For ICU use only + */ + inline UBool isCustomIntlCurrencySymbol() const { + return fIsCustomIntlCurrencySymbol; + } + + /** + * @internal For ICU use only + */ + inline UChar32 getCodePointZero() const { + return fCodePointZero; + } +#endif /* U_HIDE_INTERNAL_API */ + /** * _Internal_ function - more efficient version of getSymbol, * returning a const reference to one of the symbol strings. * The returned reference becomes invalid when the symbol is changed * or when the DecimalFormatSymbols are destroyed. - * ### TODO markus 2002oct11: Consider proposing getConstSymbol() to be really public. + * Note: moved #ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat + * + * This is not currently stable API, but if you think it should be stable, + * post a comment on the following ticket and the ICU team will take a look: + * http://bugs.icu-project.org/trac/ticket/13580 * * @param symbol Constant to indicate a number format symbol. * @return the format symbol by the param 'symbol' * @internal */ - inline const UnicodeString &getConstSymbol(ENumberFormatSymbol symbol) const; + inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const; + +#ifndef U_HIDE_INTERNAL_API + /** + * Returns the const UnicodeString reference, like getConstSymbol, + * corresponding to the digit with the given value. This is equivalent + * to accessing the symbol from getConstSymbol with the corresponding + * key, such as kZeroDigitSymbol or kOneDigitSymbol. + * + * This is not currently stable API, but if you think it should be stable, + * post a comment on the following ticket and the ICU team will take a look: + * http://bugs.icu-project.org/trac/ticket/13580 + * + * @param digit The digit, an integer between 0 and 9 inclusive. + * If outside the range 0 to 9, the zero digit is returned. + * @return the format symbol for the given digit. + * @internal This API is currently for ICU use only. + */ + inline const UnicodeString& getConstDigitSymbol(int32_t digit) const; + + /** + * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API. + * @internal + */ + inline const char16_t* getCurrencyPattern(void) const; +#endif /* U_HIDE_INTERNAL_API */ private: /** @@ -295,10 +468,32 @@ private: */ UnicodeString fNoSymbol; + /** + * Dealing with code points is faster than dealing with strings when formatting. Because of + * this, we maintain a value containing the zero code point that is used whenever digitStrings + * represents a sequence of ten code points in order. + * + *

If the value stored here is positive, it means that the code point stored in this value + * corresponds to the digitStrings array, and codePointZero can be used instead of the + * digitStrings array for the purposes of efficient formatting; if -1, then digitStrings does + * *not* contain a sequence of code points, and it must be used directly. + * + *

It is assumed that codePointZero always shadows the value in digitStrings. codePointZero + * should never be set directly; rather, it should be updated only when digitStrings mutates. + * That is, the flow of information is digitStrings -> codePointZero, not the other way. + */ + UChar32 fCodePointZero; + Locale locale; char actualLocale[ULOC_FULLNAME_CAPACITY]; char validLocale[ULOC_FULLNAME_CAPACITY]; + const char16_t* currPattern; + + UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT]; + UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT]; + UBool fIsCustomCurrencySymbol; + UBool fIsCustomIntlCurrencySymbol; }; // ------------------------------------- @@ -314,6 +509,7 @@ DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const { return *strPtr; } +// See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API inline const UnicodeString & DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const { const UnicodeString *strPtr; @@ -325,13 +521,50 @@ DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const { return *strPtr; } +#ifndef U_HIDE_INTERNAL_API +inline const UnicodeString& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit) const { + if (digit < 0 || digit > 9) { + digit = 0; + } + if (digit == 0) { + return fSymbols[kZeroDigitSymbol]; + } + ENumberFormatSymbol key = static_cast(kOneDigitSymbol + digit - 1); + return fSymbols[key]; +} +#endif + // ------------------------------------- inline void -DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value) { +DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) { + if (symbol == kCurrencySymbol) { + fIsCustomCurrencySymbol = TRUE; + } + else if (symbol == kIntlCurrencySymbol) { + fIsCustomIntlCurrencySymbol = TRUE; + } if(symbol= kOneDigitSymbol && symbol <= kNineDigitSymbol) { + fCodePointZero = -1; + } } // ------------------------------------- @@ -341,8 +574,15 @@ DecimalFormatSymbols::getLocale() const { return locale; } +#ifndef U_HIDE_INTERNAL_API +inline const char16_t* +DecimalFormatSymbols::getCurrencyPattern() const { + return currPattern; +} +#endif /* U_HIDE_INTERNAL_API */ U_NAMESPACE_END +#endif // U_SHOW_CPLUSPLUS_API #endif /* #if !UCONFIG_NO_FORMATTING */