From 558df3aeb86a6e7cfd80d6f3be4ba88fbaa753c8 Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Mon, 25 Sep 2023 19:32:18 +0200 Subject: [PATCH] ICU-21289 Switch to using CharString for calling uloc_getName(). --- icu4c/source/common/ucurr.cpp | 37 ++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/icu4c/source/common/ucurr.cpp b/icu4c/source/common/ucurr.cpp index 0f254a6f267..a2f4fb30e1f 100644 --- a/icu4c/source/common/ucurr.cpp +++ b/icu4c/source/common/ucurr.cpp @@ -697,9 +697,12 @@ ucurr_getName(const char16_t* currency, // this function. UErrorCode ec2 = U_ZERO_ERROR; - char loc[ULOC_FULLNAME_CAPACITY]; - uloc_getName(locale, loc, sizeof(loc), &ec2); - if (U_FAILURE(ec2) || ec2 == U_STRING_NOT_TERMINATED_WARNING) { + CharString loc; + { + CharStringByteSink sink(&loc); + ulocimp_getName(locale, sink, &ec2); + } + if (U_FAILURE(ec2)) { *ec = U_ILLEGAL_ARGUMENT_ERROR; return 0; } @@ -712,7 +715,7 @@ ucurr_getName(const char16_t* currency, const char16_t* s = nullptr; ec2 = U_ZERO_ERROR; - LocalUResourceBundlePointer rb(ures_open(U_ICUDATA_CURR, loc, &ec2)); + LocalUResourceBundlePointer rb(ures_open(U_ICUDATA_CURR, loc.data(), &ec2)); if (nameStyle == UCURR_NARROW_SYMBOL_NAME || nameStyle == UCURR_FORMAL_SYMBOL_NAME || nameStyle == UCURR_VARIANT_SYMBOL_NAME) { CharString key; @@ -796,9 +799,12 @@ ucurr_getPluralName(const char16_t* currency, // this function. UErrorCode ec2 = U_ZERO_ERROR; - char loc[ULOC_FULLNAME_CAPACITY]; - uloc_getName(locale, loc, sizeof(loc), &ec2); - if (U_FAILURE(ec2) || ec2 == U_STRING_NOT_TERMINATED_WARNING) { + CharString loc; + { + CharStringByteSink sink(&loc); + ulocimp_getName(locale, sink, &ec2); + } + if (U_FAILURE(ec2)) { *ec = U_ILLEGAL_ARGUMENT_ERROR; return 0; } @@ -808,7 +814,7 @@ ucurr_getPluralName(const char16_t* currency, const char16_t* s = nullptr; ec2 = U_ZERO_ERROR; - UResourceBundle* rb = ures_open(U_ICUDATA_CURR, loc, &ec2); + UResourceBundle* rb = ures_open(U_ICUDATA_CURR, loc.data(), &ec2); rb = ures_getByKey(rb, CURRENCYPLURALS, rb, &ec2); @@ -984,14 +990,17 @@ collectCurrencyNames(const char* locale, // Look up the Currencies resource for the given locale. UErrorCode ec2 = U_ZERO_ERROR; - char loc[ULOC_FULLNAME_CAPACITY] = ""; - uloc_getName(locale, loc, sizeof(loc), &ec2); - if (U_FAILURE(ec2) || ec2 == U_STRING_NOT_TERMINATED_WARNING) { + CharString loc; + { + CharStringByteSink sink(&loc); + ulocimp_getName(locale, sink, &ec2); + } + if (U_FAILURE(ec2)) { ec = U_ILLEGAL_ARGUMENT_ERROR; } // Get maximum currency name count first. - getCurrencyNameCount(loc, total_currency_name_count, total_currency_symbol_count); + getCurrencyNameCount(loc.data(), total_currency_name_count, total_currency_symbol_count); *currencyNames = (CurrencyNameStruct*)uprv_malloc (sizeof(CurrencyNameStruct) * (*total_currency_name_count)); @@ -1019,7 +1028,7 @@ collectCurrencyNames(const char* locale, for (int32_t localeLevel = 0; ; ++localeLevel) { ec2 = U_ZERO_ERROR; // TODO: ures_openDirect - UResourceBundle* rb = ures_open(U_ICUDATA_CURR, loc, &ec2); + UResourceBundle* rb = ures_open(U_ICUDATA_CURR, loc.data(), &ec2); UResourceBundle* curr = ures_getByKey(rb, CURRENCIES, nullptr, &ec2); int32_t n = ures_getSize(curr); for (int32_t i=0; i