X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/2ca993e82fb37b597a3c73ecd1586a139a6579c5..4f1e1a09ce4daed860e35d359ce2fceccb0764e8:/icuSources/i18n/dcfmtsym.cpp?ds=sidebyside diff --git a/icuSources/i18n/dcfmtsym.cpp b/icuSources/i18n/dcfmtsym.cpp index 35d2f0d7..e6785ab7 100644 --- a/icuSources/i18n/dcfmtsym.cpp +++ b/icuSources/i18n/dcfmtsym.cpp @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************* -* Copyright (C) 1997-2015, International Business Machines Corporation and +* Copyright (C) 1997-2016, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* * @@ -35,6 +37,8 @@ #include "locbased.h" #include "uresimp.h" #include "ureslocs.h" +#include "charstr.h" +#include "uassert.h" // ***************************************************************************** // class DecimalFormatSymbols @@ -51,17 +55,50 @@ static const char gAfterCurrencyTag[] = "afterCurrency"; static const char gCurrencyMatchTag[] = "currencyMatch"; static const char gCurrencySudMatchTag[] = "surroundingMatch"; static const char gCurrencyInsertBtnTag[] = "insertBetween"; - +static const char gLatn[] = "latn"; +static const char gSymbols[] = "symbols"; +static const char gNumberElementsLatnSymbols[] = "NumberElements/latn/symbols"; static const UChar INTL_CURRENCY_SYMBOL_STR[] = {0xa4, 0xa4, 0}; +// List of field names to be loaded from the data files. +// These are parallel with the enum ENumberFormatSymbol in unicode/dcfmtsym.h. +static const char *gNumberElementKeys[DecimalFormatSymbols::kFormatSymbolCount] = { + "decimal", + "group", + NULL, /* #11897: the symbol is NOT the pattern separator symbol */ + "percentSign", + NULL, /* Native zero digit is deprecated from CLDR - get it from the numbering system */ + NULL, /* Pattern digit character is deprecated from CLDR - use # by default always */ + "minusSign", + "plusSign", + NULL, /* currency symbol - Wait until we know the currency before loading from CLDR */ + NULL, /* intl currency symbol - Wait until we know the currency before loading from CLDR */ + "currencyDecimal", + "exponential", + "perMille", + NULL, /* Escape padding character - not in CLDR */ + "infinity", + "nan", + NULL, /* Significant digit symbol - not in CLDR */ + "currencyGroup", + NULL, /* one digit - get it from the numbering system */ + NULL, /* two digit - get it from the numbering system */ + NULL, /* three digit - get it from the numbering system */ + NULL, /* four digit - get it from the numbering system */ + NULL, /* five digit - get it from the numbering system */ + NULL, /* six digit - get it from the numbering system */ + NULL, /* seven digit - get it from the numbering system */ + NULL, /* eight digit - get it from the numbering system */ + NULL, /* nine digit - get it from the numbering system */ + "superscriptingExponent", /* Multiplication (x) symbol for exponents */ +}; + // ------------------------------------- // Initializes this with the decimal format symbols in the default locale. DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode& status) - : UObject(), - locale() -{ + : UObject(), locale(), currPattern(NULL) { initialize(locale, status, TRUE); } @@ -69,16 +106,17 @@ DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode& status) // Initializes this with the decimal format symbols in the desired locale. DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, UErrorCode& status) - : UObject(), - locale(loc) -{ + : UObject(), locale(loc), currPattern(NULL) { initialize(locale, status); } +DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, const NumberingSystem& ns, UErrorCode& status) + : UObject(), locale(loc), currPattern(NULL) { + initialize(locale, status, FALSE, &ns); +} + DecimalFormatSymbols::DecimalFormatSymbols() - : UObject(), - locale(Locale::getRoot()), - currPattern(NULL) { + : UObject(), locale(Locale::getRoot()), currPattern(NULL) { *validLocale = *actualLocale = 0; initialize(); } @@ -128,6 +166,7 @@ DecimalFormatSymbols::operator=(const DecimalFormatSymbols& rhs) uprv_strcpy(actualLocale, rhs.actualLocale); fIsCustomCurrencySymbol = rhs.fIsCustomCurrencySymbol; fIsCustomIntlCurrencySymbol = rhs.fIsCustomIntlCurrencySymbol; + fCodePointZero = rhs.fCodePointZero; } return *this; } @@ -159,6 +198,7 @@ DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const return FALSE; } } + // No need to check fCodePointZero since it is based on fSymbols return locale == that.locale && uprv_strcmp(validLocale, that.validLocale) == 0 && uprv_strcmp(actualLocale, that.actualLocale) == 0; @@ -166,64 +206,150 @@ DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const // ------------------------------------- -void -DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, UBool useLastResortData) -{ - static const char *gNumberElementKeys[kFormatSymbolCount] = { - "decimal", - "group", - "list", - "percentSign", - NULL, /* Native zero digit is deprecated from CLDR - get it from the numbering system */ - NULL, /* Pattern digit character is deprecated from CLDR - use # by default always */ - "minusSign", - "plusSign", - NULL, /* currency symbol - We don't really try to load this directly from CLDR until we know the currency */ - NULL, /* intl currency symbol - We don't really try to load this directly from CLDR until we know the currency */ - "currencyDecimal", - "exponential", - "perMille", - NULL, /* Escape padding character - not in CLDR */ - "infinity", - "nan", - NULL, /* Significant digit symbol - not in CLDR */ - "currencyGroup", - NULL, /* one digit - get it from the numbering system */ - NULL, /* two digit - get it from the numbering system */ - NULL, /* three digit - get it from the numbering system */ - NULL, /* four digit - get it from the numbering system */ - NULL, /* five digit - get it from the numbering system */ - NULL, /* six digit - get it from the numbering system */ - NULL, /* seven digit - get it from the numbering system */ - NULL, /* eight digit - get it from the numbering system */ - NULL, /* nine digit - get it from the numbering system */ - "superscriptingExponent", /* Multiplication (x) symbol for exponents */ - }; - - static const char *gLatn = "latn"; - static const char *gSymbols = "symbols"; - const char *nsName; - const UChar *sym = NULL; - int32_t len = 0; +namespace { + +/** + * Sink for enumerating all of the decimal format symbols (more specifically, anything + * under the "NumberElements.symbols" tree). + * + * More specific bundles (en_GB) are enumerated before their parents (en_001, en, root): + * Only store a value if it is still missing, that is, it has not been overridden. + */ +struct DecFmtSymDataSink : public ResourceSink { + + // Destination for data, modified via setters. + DecimalFormatSymbols& dfs; + // Boolean array of whether or not we have seen a particular symbol yet. + // Can't simpy check fSymbols because it is pre-populated with defaults. + UBool seenSymbol[DecimalFormatSymbols::kFormatSymbolCount]; + + // Constructor/Destructor + DecFmtSymDataSink(DecimalFormatSymbols& _dfs) : dfs(_dfs) { + uprv_memset(seenSymbol, FALSE, sizeof(seenSymbol)); + } + virtual ~DecFmtSymDataSink(); + + virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/, + UErrorCode &errorCode) { + ResourceTable symbolsTable = value.getTable(errorCode); + if (U_FAILURE(errorCode)) { return; } + for (int32_t j = 0; symbolsTable.getKeyAndValue(j, key, value); ++j) { + for (int32_t i=0; i ns(NumberingSystem::createInstance(loc, status)); + LocalPointer nsLocal; + if (ns == nullptr) { + // Use the numbering system according to the locale. + // Save it into a LocalPointer so it gets cleaned up. + nsLocal.adoptInstead(NumberingSystem::createInstance(loc, status)); + ns = nsLocal.getAlias(); + } + const char *nsName; if (U_SUCCESS(status) && ns->getRadix() == 10 && !ns->isAlgorithmic()) { nsName = ns->getName(); UnicodeString digitString(ns->getDescription()); @@ -248,60 +380,78 @@ DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, UBool us nsName = gLatn; } - UBool isLatn = !uprv_strcmp(nsName,gLatn); + // Open resource bundles + const char* locStr = loc.getName(); + LocalUResourceBundlePointer resource(ures_open(NULL, locStr, &status)); + LocalUResourceBundlePointer numberElementsRes( + ures_getByKeyWithFallback(resource.getAlias(), gNumberElements, NULL, &status)); - UErrorCode nlStatus = U_ZERO_ERROR; - LocalUResourceBundlePointer nonLatnSymbols; - if ( !isLatn ) { - nonLatnSymbols.adoptInstead( - ures_getByKeyWithFallback(numberElementsRes.getAlias(), nsName, NULL, &nlStatus)); - ures_getByKeyWithFallback(nonLatnSymbols.getAlias(), gSymbols, nonLatnSymbols.getAlias(), &nlStatus); + if (U_FAILURE(status)) { + if ( useLastResortData ) { + status = U_USING_DEFAULT_WARNING; + initialize(); + } + return; } - LocalUResourceBundlePointer latnSymbols( - ures_getByKeyWithFallback(numberElementsRes.getAlias(), gLatn, NULL, &status)); - ures_getByKeyWithFallback(latnSymbols.getAlias(), gSymbols, latnSymbols.getAlias(), &status); - - UBool kMonetaryDecimalSet = FALSE; - UBool kMonetaryGroupingSet = FALSE; - for(int32_t i = 0; i 0) { char cc[4]={0}; u_UCharsToChars(ucc, cc, uccLen); @@ -361,38 +507,11 @@ DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, UBool us // else ignore the error if no currency // Currency Spacing. - localStatus = U_ZERO_ERROR; - LocalUResourceBundlePointer currencyResource(ures_open(U_ICUDATA_CURR, locStr, &localStatus)); - LocalUResourceBundlePointer currencySpcRes( - ures_getByKeyWithFallback(currencyResource.getAlias(), - gCurrencySpacingTag, NULL, &localStatus)); - - if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) { - const char* keywords[UNUM_CURRENCY_SPACING_COUNT] = { - gCurrencyMatchTag, gCurrencySudMatchTag, gCurrencyInsertBtnTag - }; - localStatus = U_ZERO_ERROR; - LocalUResourceBundlePointer dataRes( - ures_getByKeyWithFallback(currencySpcRes.getAlias(), - gBeforeCurrencyTag, NULL, &localStatus)); - if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) { - localStatus = U_ZERO_ERROR; - for (int32_t i = 0; i < UNUM_CURRENCY_SPACING_COUNT; i++) { - currencySpcBeforeSym[i] = - ures_getUnicodeStringByKey(dataRes.getAlias(), keywords[i], &localStatus); - } - } - dataRes.adoptInstead( - ures_getByKeyWithFallback(currencySpcRes.getAlias(), - gAfterCurrencyTag, NULL, &localStatus)); - if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) { - localStatus = U_ZERO_ERROR; - for (int32_t i = 0; i < UNUM_CURRENCY_SPACING_COUNT; i++) { - currencySpcAfterSym[i] = - ures_getUnicodeStringByKey(dataRes.getAlias(), keywords[i], &localStatus); - } - } - } + LocalUResourceBundlePointer currencyResource(ures_open(U_ICUDATA_CURR, locStr, &status)); + CurrencySpacingSink currencySink(*this); + ures_getAllItemsWithFallback(currencyResource.getAlias(), gCurrencySpacingTag, currencySink, status); + currencySink.resolveMissing(); + if (U_FAILURE(status)) { return; } } void @@ -432,6 +551,8 @@ DecimalFormatSymbols::initialize() { fSymbols[kExponentMultiplicationSymbol] = (UChar)0xd7; // 'x' multiplication symbol for exponents fIsCustomCurrencySymbol = FALSE; fIsCustomIntlCurrencySymbol = FALSE; + fCodePointZero = 0x30; + U_ASSERT(fCodePointZero == fSymbols[kZeroDigitSymbol].char32At(0)); }