diff options
Diffstat (limited to 'thirdparty/icu4c/common/ucurr.cpp')
-rw-r--r-- | thirdparty/icu4c/common/ucurr.cpp | 115 |
1 files changed, 43 insertions, 72 deletions
diff --git a/thirdparty/icu4c/common/ucurr.cpp b/thirdparty/icu4c/common/ucurr.cpp index 70b1bbf223..dbad1e5014 100644 --- a/thirdparty/icu4c/common/ucurr.cpp +++ b/thirdparty/icu4c/common/ucurr.cpp @@ -22,7 +22,6 @@ #include "unicode/usetiter.h" #include "unicode/utf16.h" #include "ustr_imp.h" -#include "bytesinkutil.h" #include "charstr.h" #include "cmemory.h" #include "cstring.h" @@ -296,7 +295,7 @@ myUCharsToChars(char* resultOfLen4, const char16_t* currency) { static const int32_t* _findMetaData(const char16_t* currency, UErrorCode& ec) { - if (currency == 0 || *currency == 0) { + if (currency == nullptr || *currency == 0) { if (U_SUCCESS(ec)) { ec = U_ILLEGAL_ARGUMENT_ERROR; } @@ -349,10 +348,10 @@ _findMetaData(const char16_t* currency, UErrorCode& ec) { // ------------------------------------- -static void -idForLocale(const char* locale, char* countryAndVariant, int capacity, UErrorCode* ec) +static CharString +idForLocale(const char* locale, UErrorCode* ec) { - ulocimp_getRegionForSupplementalData(locale, false, countryAndVariant, capacity, ec); + return ulocimp_getRegionForSupplementalData(locale, false, *ec); } // ------------------------------------------ @@ -371,7 +370,7 @@ U_CDECL_END struct CReg; static UMutex gCRegLock; -static CReg* gCRegHead = 0; +static CReg* gCRegHead = nullptr; struct CReg : public icu::UMemory { CReg *next; @@ -379,7 +378,7 @@ struct CReg : public icu::UMemory { char id[ULOC_FULLNAME_CAPACITY]; CReg(const char16_t* _iso, const char* _id) - : next(0) + : next(nullptr) { int32_t len = (int32_t)uprv_strlen(_id); if (len > (int32_t)(sizeof(id)-1)) { @@ -408,7 +407,7 @@ struct CReg : public icu::UMemory { } *status = U_MEMORY_ALLOCATION_ERROR; } - return 0; + return nullptr; } static UBool unreg(UCurrRegistryKey key) { @@ -464,9 +463,8 @@ U_CAPI UCurrRegistryKey U_EXPORT2 ucurr_register(const char16_t* isoCode, const char* locale, UErrorCode *status) { if (status && U_SUCCESS(*status)) { - char id[ULOC_FULLNAME_CAPACITY]; - idForLocale(locale, id, sizeof(id), status); - return CReg::reg(isoCode, id, status); + CharString id = idForLocale(locale, status); + return CReg::reg(isoCode, id.data(), status); } return nullptr; } @@ -524,11 +522,7 @@ ucurr_forLocale(const char* locale, } UErrorCode localStatus = U_ZERO_ERROR; - CharString currency; - { - CharStringByteSink sink(¤cy); - ulocimp_getKeywordValue(locale, "currency", sink, &localStatus); - } + CharString currency = ulocimp_getKeywordValue(locale, "currency", localStatus); int32_t resLen = currency.length(); if (U_SUCCESS(localStatus) && resLen == 3 && uprv_isInvariantString(currency.data(), resLen)) { @@ -540,14 +534,13 @@ ucurr_forLocale(const char* locale, } // get country or country_variant in `id' - char id[ULOC_FULLNAME_CAPACITY]; - idForLocale(locale, id, UPRV_LENGTHOF(id), ec); + CharString id = idForLocale(locale, ec); if (U_FAILURE(*ec)) { return 0; } #if !UCONFIG_NO_SERVICE - const char16_t* result = CReg::get(id); + const char16_t* result = CReg::get(id.data()); if (result) { if(buffCapacity > u_strlen(result)) { u_strcpy(buff, result); @@ -557,13 +550,13 @@ ucurr_forLocale(const char* locale, } #endif // Remove variants, which is only needed for registration. - char *idDelim = uprv_strchr(id, VAR_DELIM); + char *idDelim = uprv_strchr(id.data(), VAR_DELIM); if (idDelim) { - idDelim[0] = 0; + id.truncate(idDelim - id.data()); } const char16_t* s = nullptr; // Currency code from data file. - if (id[0] == 0) { + if (id.isEmpty()) { // No point looking in the data for an empty string. // This is what we would get. localStatus = U_MISSING_RESOURCE_ERROR; @@ -572,7 +565,7 @@ ucurr_forLocale(const char* locale, localStatus = U_ZERO_ERROR; UResourceBundle *rb = ures_openDirect(U_ICUDATA_CURR, CURRENCY_DATA, &localStatus); UResourceBundle *cm = ures_getByKey(rb, CURRENCY_MAP, rb, &localStatus); - UResourceBundle *countryArray = ures_getByKey(rb, id, cm, &localStatus); + UResourceBundle *countryArray = ures_getByKey(rb, id.data(), cm, &localStatus); // https://unicode-org.atlassian.net/browse/ICU-21997 // Prefer to use currencies that are legal tender. if (U_SUCCESS(localStatus)) { @@ -602,13 +595,9 @@ ucurr_forLocale(const char* locale, ures_close(countryArray); } - if ((U_FAILURE(localStatus)) && strchr(id, '_') != 0) { + if ((U_FAILURE(localStatus)) && strchr(id.data(), '_') != nullptr) { // We don't know about it. Check to see if we support the variant. - CharString parent; - { - CharStringByteSink sink(&parent); - ulocimp_getParent(locale, sink, ec); - } + CharString parent = ulocimp_getParent(locale, *ec); *ec = U_USING_FALLBACK_WARNING; // TODO: Loop over the parent rather than recursing and // looking again for a currency keyword. @@ -647,10 +636,7 @@ static UBool fallback(CharString& loc) { loc.truncate(3); loc.append("001", status); } else { - CharString tmp; - CharStringByteSink sink(&tmp); - ulocimp_getParent(loc.data(), sink, &status); - loc = std::move(tmp); + loc = ulocimp_getParent(loc.data(), status); } /* char *i = uprv_strrchr(loc, '_'); @@ -683,13 +669,13 @@ ucurr_getName(const char16_t* currency, //|} if (U_FAILURE(*ec)) { - return 0; + return nullptr; } int32_t choice = (int32_t) nameStyle; if (choice < 0 || choice > 4) { *ec = U_ILLEGAL_ARGUMENT_ERROR; - return 0; + return nullptr; } // In the future, resource bundles may implement multi-level @@ -705,14 +691,10 @@ ucurr_getName(const char16_t* currency, // this function. UErrorCode ec2 = U_ZERO_ERROR; - CharString loc; - { - CharStringByteSink sink(&loc); - ulocimp_getName(locale, sink, &ec2); - } + CharString loc = ulocimp_getName(locale, ec2); if (U_FAILURE(ec2)) { *ec = U_ILLEGAL_ARGUMENT_ERROR; - return 0; + return nullptr; } char buf[ISO_CURRENCY_CODE_LENGTH+1]; @@ -739,7 +721,7 @@ ucurr_getName(const char16_t* currency, break; default: *ec = U_UNSUPPORTED_ERROR; - return 0; + return nullptr; } key.append("/", ec2); key.append(buf, ec2); @@ -800,21 +782,17 @@ ucurr_getPluralName(const char16_t* currency, //|} if (U_FAILURE(*ec)) { - return 0; + return nullptr; } // Use a separate UErrorCode here that does not propagate out of // this function. UErrorCode ec2 = U_ZERO_ERROR; - CharString loc; - { - CharStringByteSink sink(&loc); - ulocimp_getName(locale, sink, &ec2); - } + CharString loc = ulocimp_getName(locale, ec2); if (U_FAILURE(ec2)) { *ec = U_ILLEGAL_ARGUMENT_ERROR; - return 0; + return nullptr; } char buf[ISO_CURRENCY_CODE_LENGTH+1]; @@ -1002,11 +980,7 @@ collectCurrencyNames(const char* locale, // Look up the Currencies resource for the given locale. UErrorCode ec2 = U_ZERO_ERROR; - CharString loc; - { - CharStringByteSink sink(&loc); - ulocimp_getName(locale, sink, &ec2); - } + CharString loc = ulocimp_getName(locale, ec2); if (U_FAILURE(ec2)) { ec = U_ILLEGAL_ARGUMENT_ERROR; } @@ -1390,7 +1364,6 @@ searchCurrencyName(const CurrencyNameStruct* currencyNames, break; } } - return; } //========================= currency name cache ===================== @@ -1447,7 +1420,7 @@ currency_cache_cleanup() { for (int32_t i = 0; i < CURRENCY_NAME_CACHE_NUM; ++i) { if (currCache[i]) { deleteCacheEntry(currCache[i]); - currCache[i] = 0; + currCache[i] = nullptr; } } return true; @@ -2030,6 +2003,7 @@ static const struct CurrencyList { {"XBC", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, {"XBD", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, {"XCD", UCURR_COMMON|UCURR_NON_DEPRECATED}, + {"XCG", UCURR_COMMON|UCURR_NON_DEPRECATED}, {"XDR", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, {"XEU", UCURR_UNCOMMON|UCURR_DEPRECATED}, {"XFO", UCURR_UNCOMMON|UCURR_NON_DEPRECATED}, @@ -2229,7 +2203,7 @@ static void U_CALLCONV initIsoCodes(UErrorCode &status) { static void populateCurrSymbolsEquiv(icu::Hashtable *hash, UErrorCode &status) { if (U_FAILURE(status)) { return; } - for (auto& entry : unisets::kCurrencyEntries) { + for (const auto& entry : unisets::kCurrencyEntries) { UnicodeString exemplar(entry.exemplar); const UnicodeSet* set = unisets::get(entry.key); if (set == nullptr) { return; } @@ -2325,10 +2299,9 @@ ucurr_countCurrencies(const char* locale, { // local variables UErrorCode localStatus = U_ZERO_ERROR; - char id[ULOC_FULLNAME_CAPACITY]; // get country or country_variant in `id' - idForLocale(locale, id, sizeof(id), ec); + CharString id = idForLocale(locale, ec); if (U_FAILURE(*ec)) { @@ -2336,10 +2309,10 @@ ucurr_countCurrencies(const char* locale, } // Remove variants, which is only needed for registration. - char *idDelim = strchr(id, VAR_DELIM); + char *idDelim = strchr(id.data(), VAR_DELIM); if (idDelim) { - idDelim[0] = 0; + id.truncate(idDelim - id.data()); } // Look up the CurrencyMap element in the root bundle. @@ -2347,7 +2320,7 @@ ucurr_countCurrencies(const char* locale, UResourceBundle *cm = ures_getByKey(rb, CURRENCY_MAP, rb, &localStatus); // Using the id derived from the local, get the currency data - UResourceBundle *countryArray = ures_getByKey(rb, id, cm, &localStatus); + UResourceBundle *countryArray = ures_getByKey(rb, id.data(), cm, &localStatus); // process each currency to see which one is valid for the given date if (U_SUCCESS(localStatus)) @@ -2440,20 +2413,19 @@ ucurr_forLocaleAndDate(const char* locale, { // local variables UErrorCode localStatus = U_ZERO_ERROR; - char id[ULOC_FULLNAME_CAPACITY]; // get country or country_variant in `id' - idForLocale(locale, id, sizeof(id), ec); + CharString id = idForLocale(locale, ec); if (U_FAILURE(*ec)) { return 0; } // Remove variants, which is only needed for registration. - char *idDelim = strchr(id, VAR_DELIM); + char *idDelim = strchr(id.data(), VAR_DELIM); if (idDelim) { - idDelim[0] = 0; + id.truncate(idDelim - id.data()); } // Look up the CurrencyMap element in the root bundle. @@ -2461,7 +2433,7 @@ ucurr_forLocaleAndDate(const char* locale, UResourceBundle *cm = ures_getByKey(rb, CURRENCY_MAP, rb, &localStatus); // Using the id derived from the local, get the currency data - UResourceBundle *countryArray = ures_getByKey(rb, id, cm, &localStatus); + UResourceBundle *countryArray = ures_getByKey(rb, id.data(), cm, &localStatus); // process each currency to see which one is valid for the given date bool matchFound = false; @@ -2587,9 +2559,8 @@ static const UEnumeration defaultKeywordValues = { U_CAPI UEnumeration *U_EXPORT2 ucurr_getKeywordValuesForLocale(const char *key, const char *locale, UBool commonlyUsed, UErrorCode* status) { // Resolve region - char prefRegion[ULOC_COUNTRY_CAPACITY]; - ulocimp_getRegionForSupplementalData(locale, true, prefRegion, sizeof(prefRegion), status); - + CharString prefRegion = ulocimp_getRegionForSupplementalData(locale, true, *status); + // Read value from supplementalData UList *values = ulist_createEmptyList(status); UList *otherValues = ulist_createEmptyList(status); @@ -2621,7 +2592,7 @@ U_CAPI UEnumeration *U_EXPORT2 ucurr_getKeywordValuesForLocale(const char *key, break; } const char *region = ures_getKey(&bundlekey); - UBool isPrefRegion = uprv_strcmp(region, prefRegion) == 0 ? true : false; + UBool isPrefRegion = prefRegion == region; if (!isPrefRegion && commonlyUsed) { // With commonlyUsed=true, we do not put // currencies for other regions in the @@ -2732,7 +2703,7 @@ ucurr_getNumericCode(const char16_t* currency) { if (currency && u_strlen(currency) == ISO_CURRENCY_CODE_LENGTH) { UErrorCode status = U_ZERO_ERROR; - UResourceBundle *bundle = ures_openDirect(0, "currencyNumericCodes", &status); + UResourceBundle *bundle = ures_openDirect(nullptr, "currencyNumericCodes", &status); ures_getByKey(bundle, "codeMap", bundle, &status); if (U_SUCCESS(status)) { char alphaCode[ISO_CURRENCY_CODE_LENGTH+1]; |