diff options
author | romankoshelev <romankoshelev@yandex-team.com> | 2024-05-13 11:00:27 +0300 |
---|---|---|
committer | romankoshelev <romankoshelev@yandex-team.com> | 2024-05-13 11:13:05 +0300 |
commit | 5b22fadb0f035a3b82c328e0ae710ad2b92f6eac (patch) | |
tree | e15dc649c79c4fb78f35cd6694dfe9af9bfcc0ad /contrib/libs/icu/common/locdspnm.cpp | |
parent | 5946aa7d3cbca62f6bcf074e8a2b9346e7a96af4 (diff) | |
download | ydb-5b22fadb0f035a3b82c328e0ae710ad2b92f6eac.tar.gz |
Update ICU to 75.1
904da4ae1c86fc5542eac7f1cd18d97b72eb8517
Diffstat (limited to 'contrib/libs/icu/common/locdspnm.cpp')
-rw-r--r-- | contrib/libs/icu/common/locdspnm.cpp | 156 |
1 files changed, 64 insertions, 92 deletions
diff --git a/contrib/libs/icu/common/locdspnm.cpp b/contrib/libs/icu/common/locdspnm.cpp index 73fe531c54..c65a349dbd 100644 --- a/contrib/libs/icu/common/locdspnm.cpp +++ b/contrib/libs/icu/common/locdspnm.cpp @@ -18,45 +18,17 @@ #include "unicode/udisplaycontext.h" #include "unicode/brkiter.h" #include "unicode/ucurr.h" +#include "bytesinkutil.h" +#include "charstr.h" #include "cmemory.h" #include "cstring.h" #include "mutex.h" +#include "uassert.h" #include "ulocimp.h" #include "umutex.h" #include "ureslocs.h" #include "uresimp.h" -#include <stdarg.h> - -/** - * Concatenate a number of null-terminated strings to buffer, leaving a - * null-terminated string. The last argument should be the null pointer. - * Return the length of the string in the buffer, not counting the trailing - * null. Return -1 if there is an error (buffer is null, or buflen < 1). - */ -static int32_t ncat(char *buffer, uint32_t buflen, ...) { - va_list args; - char *str; - char *p = buffer; - const char* e = buffer + buflen - 1; - - if (buffer == nullptr || buflen < 1) { - return -1; - } - - va_start(args, buflen); - while ((str = va_arg(args, char *)) != 0) { - char c; - while (p != e && (c = *str++) != 0) { - *p++ = c; - } - } - *p = 0; - va_end(args); - - return static_cast<int32_t>(p - buffer); -} - U_NAMESPACE_BEGIN //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -64,12 +36,13 @@ U_NAMESPACE_BEGIN // Access resource data for locale components. // Wrap code in uloc.c for now. class ICUDataTable { - const char* path; + const char* const path; Locale locale; public: + // Note: path should be a pointer to a statically allocated string. ICUDataTable(const char* path, const Locale& locale); - ~ICUDataTable(); + ~ICUDataTable() = default; const Locale& getLocale(); @@ -95,23 +68,9 @@ ICUDataTable::getNoFallback(const char* tableKey, const char* itemKey, UnicodeSt } ICUDataTable::ICUDataTable(const char* path, const Locale& locale) - : path(nullptr), locale(Locale::getRoot()) + : path(path), locale(locale) { - if (path) { - int32_t len = static_cast<int32_t>(uprv_strlen(path)); - this->path = (const char*) uprv_malloc(len + 1); - if (this->path) { - uprv_strcpy((char *)this->path, path); - this->locale = locale; - } - } -} - -ICUDataTable::~ICUDataTable() { - if (path) { - uprv_free((void*) path); - path = nullptr; - } + U_ASSERT(path != nullptr); } const Locale& @@ -305,7 +264,7 @@ class LocaleDisplayNamesImpl : public LocaleDisplayNames { }; // Capitalization transforms. For each usage type, indicates whether to titlecase for // the context specified in capitalizationContext (which we know at construction time) - UBool fCapitalization[kCapContextUsageCount]; + bool fCapitalization[kCapContextUsageCount]; public: // constructor @@ -341,12 +300,12 @@ private: UnicodeString& result, bool substitute) const; UnicodeString& appendWithSep(UnicodeString& buffer, const UnicodeString& src) const; UnicodeString& adjustForUsageAndContext(CapContextUsage usage, UnicodeString& result) const; - UnicodeString& scriptDisplayName(const char* script, UnicodeString& result, UBool skipAdjust) const; - UnicodeString& regionDisplayName(const char* region, UnicodeString& result, UBool skipAdjust) const; - UnicodeString& variantDisplayName(const char* variant, UnicodeString& result, UBool skipAdjust) const; - UnicodeString& keyDisplayName(const char* key, UnicodeString& result, UBool skipAdjust) const; + UnicodeString& scriptDisplayName(const char* script, UnicodeString& result, bool skipAdjust) const; + UnicodeString& regionDisplayName(const char* region, UnicodeString& result, bool skipAdjust) const; + UnicodeString& variantDisplayName(const char* variant, UnicodeString& result, bool skipAdjust) const; + UnicodeString& keyDisplayName(const char* key, UnicodeString& result, bool skipAdjust) const; UnicodeString& keyValueDisplayName(const char* key, const char* value, - UnicodeString& result, UBool skipAdjust) const; + UnicodeString& result, bool skipAdjust) const; void initialize(); struct CapitalizationContextSink; @@ -399,7 +358,7 @@ LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale, } struct LocaleDisplayNamesImpl::CapitalizationContextSink : public ResourceSink { - UBool hasCapitalizationUsage; + bool hasCapitalizationUsage; LocaleDisplayNamesImpl& parent; CapitalizationContextSink(LocaleDisplayNamesImpl& _parent) @@ -490,7 +449,7 @@ LocaleDisplayNamesImpl::initialize() { #if !UCONFIG_NO_BREAK_ITERATION // Only get the context data if we need it! This is a const object so we know now... // Also check whether we will need a break iterator (depends on the data) - UBool needBrkIter = false; + bool needBrkIter = false; if (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_STANDALONE) { LocalUResourceBundlePointer resource(ures_open(nullptr, locale.getName(), &status)); if (U_FAILURE(status)) { return; } @@ -582,36 +541,51 @@ LocaleDisplayNamesImpl::localeDisplayName(const Locale& loc, const char* country = loc.getCountry(); const char* variant = loc.getVariant(); - UBool hasScript = uprv_strlen(script) > 0; - UBool hasCountry = uprv_strlen(country) > 0; - UBool hasVariant = uprv_strlen(variant) > 0; + bool hasScript = uprv_strlen(script) > 0; + bool hasCountry = uprv_strlen(country) > 0; + bool hasVariant = uprv_strlen(variant) > 0; if (dialectHandling == ULDN_DIALECT_NAMES) { - char buffer[ULOC_FULLNAME_CAPACITY]; + UErrorCode status = U_ZERO_ERROR; + CharString buffer; do { // loop construct is so we can break early out of search if (hasScript && hasCountry) { - ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, "_", country, (char *)0); - localeIdName(buffer, resultName, false); - if (!resultName.isBogus()) { - hasScript = false; - hasCountry = false; - break; + buffer.append(lang, status) + .append('_', status) + .append(script, status) + .append('_', status) + .append(country, status); + if (U_SUCCESS(status)) { + localeIdName(buffer.data(), resultName, false); + if (!resultName.isBogus()) { + hasScript = false; + hasCountry = false; + break; + } } } if (hasScript) { - ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, (char *)0); - localeIdName(buffer, resultName, false); - if (!resultName.isBogus()) { - hasScript = false; - break; + buffer.append(lang, status) + .append('_', status) + .append(script, status); + if (U_SUCCESS(status)) { + localeIdName(buffer.data(), resultName, false); + if (!resultName.isBogus()) { + hasScript = false; + break; + } } } if (hasCountry) { - ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", country, (char*)0); - localeIdName(buffer, resultName, false); - if (!resultName.isBogus()) { - hasCountry = false; - break; + buffer.append(lang, status) + .append('_', status) + .append(country, status); + if (U_SUCCESS(status)) { + localeIdName(buffer.data(), resultName, false); + if (!resultName.isBogus()) { + hasCountry = false; + break; + } } } } while (false); @@ -658,21 +632,19 @@ LocaleDisplayNamesImpl::localeDisplayName(const Locale& loc, LocalPointer<StringEnumeration> e(loc.createKeywords(status)); if (e.isValid() && U_SUCCESS(status)) { UnicodeString temp2; - char value[ULOC_KEYWORD_AND_VALUES_CAPACITY]; // sigh, no ULOC_VALUE_CAPACITY const char* key; - while ((key = e->next((int32_t *)0, status)) != nullptr) { - value[0] = 0; - loc.getKeywordValue(key, value, ULOC_KEYWORD_AND_VALUES_CAPACITY, status); - if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) { - return result; + while ((key = e->next((int32_t*)nullptr, status)) != nullptr) { + auto value = loc.getKeywordValue<CharString>(key, status); + if (U_FAILURE(status)) { + return result; } keyDisplayName(key, temp, true); temp.findAndReplace(formatOpenParen, formatReplaceOpenParen); temp.findAndReplace(formatCloseParen, formatReplaceCloseParen); - keyValueDisplayName(key, value, temp2, true); + keyValueDisplayName(key, value.data(), temp2, true); temp2.findAndReplace(formatOpenParen, formatReplaceOpenParen); temp2.findAndReplace(formatCloseParen, formatReplaceCloseParen); - if (temp2 != UnicodeString(value, -1, US_INV)) { + if (temp2 != UnicodeString(value.data(), -1, US_INV)) { appendWithSep(resultRemainder, temp2); } else if (temp != UnicodeString(key, -1, US_INV)) { UnicodeString temp3; @@ -779,7 +751,7 @@ LocaleDisplayNamesImpl::languageDisplayName(const char* lang, UnicodeString& LocaleDisplayNamesImpl::scriptDisplayName(const char* script, UnicodeString& result, - UBool skipAdjust) const { + bool skipAdjust) const { if (nameLength == UDISPCTX_LENGTH_SHORT) { langData.getNoFallback("Scripts%short", script, result); if (!result.isBogus()) { @@ -809,7 +781,7 @@ LocaleDisplayNamesImpl::scriptDisplayName(UScriptCode scriptCode, UnicodeString& LocaleDisplayNamesImpl::regionDisplayName(const char* region, UnicodeString& result, - UBool skipAdjust) const { + bool skipAdjust) const { if (nameLength == UDISPCTX_LENGTH_SHORT) { regionData.getNoFallback("Countries%short", region, result); if (!result.isBogus()) { @@ -834,7 +806,7 @@ LocaleDisplayNamesImpl::regionDisplayName(const char* region, UnicodeString& LocaleDisplayNamesImpl::variantDisplayName(const char* variant, UnicodeString& result, - UBool skipAdjust) const { + bool skipAdjust) const { // don't have a resource for short variant names if (substitute == UDISPCTX_SUBSTITUTE) { langData.get("Variants", variant, result); @@ -853,7 +825,7 @@ LocaleDisplayNamesImpl::variantDisplayName(const char* variant, UnicodeString& LocaleDisplayNamesImpl::keyDisplayName(const char* key, UnicodeString& result, - UBool skipAdjust) const { + bool skipAdjust) const { // don't have a resource for short key names if (substitute == UDISPCTX_SUBSTITUTE) { langData.get("Keys", key, result); @@ -873,7 +845,7 @@ UnicodeString& LocaleDisplayNamesImpl::keyValueDisplayName(const char* key, const char* value, UnicodeString& result, - UBool skipAdjust) const { + bool skipAdjust) const { if (uprv_strcmp(key, "currency") == 0) { // ICU4C does not have ICU4J CurrencyDisplayInfo equivalent for now. UErrorCode sts = U_ZERO_ERROR; @@ -939,7 +911,7 @@ uldn_open(const char * locale, UDialectHandling dialectHandling, UErrorCode *pErrorCode) { if (U_FAILURE(*pErrorCode)) { - return 0; + return nullptr; } if (locale == nullptr) { locale = uloc_getDefault(); @@ -952,7 +924,7 @@ uldn_openForContext(const char * locale, UDisplayContext *contexts, int32_t length, UErrorCode *pErrorCode) { if (U_FAILURE(*pErrorCode)) { - return 0; + return nullptr; } if (locale == nullptr) { locale = uloc_getDefault(); |