From a53869e7226396ba2326b0740d1af83cdecf1d92 Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Tue, 11 Feb 2025 18:16:36 +0100 Subject: [PATCH] ICU-22901 Update Locale::init() to use StringPiece. --- icu4c/source/common/locid.cpp | 43 +++++++++++++++++++++-------- icu4c/source/common/ulocale.cpp | 5 +--- icu4c/source/common/unicode/locid.h | 9 +++++- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/icu4c/source/common/locid.cpp b/icu4c/source/common/locid.cpp index 4a73f5592052..e7e86079ae91 100644 --- a/icu4c/source/common/locid.cpp +++ b/icu4c/source/common/locid.cpp @@ -1828,8 +1828,13 @@ ulocimp_isCanonicalizedLocaleForTest(const char* localeName) U_NAMESPACE_BEGIN -/*This function initializes a Locale from a C locale ID*/ Locale& Locale::init(const char* localeID, UBool canonicalize) +{ + return localeID == nullptr ? *this = getDefault() : init(StringPiece{localeID}, canonicalize); +} + +/*This function initializes a Locale from a C locale ID*/ +Locale& Locale::init(StringPiece localeID, UBool canonicalize) { fIsBogus = false; /* Free our current storage */ @@ -1854,19 +1859,28 @@ Locale& Locale::init(const char* localeID, UBool canonicalize) int32_t length; UErrorCode err; - if(localeID == nullptr) { - // not an error, just set the default locale - return *this = getDefault(); - } - /* preset all fields to empty */ language[0] = script[0] = country[0] = 0; + const auto parse = [canonicalize](std::string_view localeID, + char* name, + int32_t nameCapacity, + UErrorCode& status) { + return ByteSinkUtil::viaByteSinkToTerminatedChars( + name, nameCapacity, + [&](ByteSink& sink, UErrorCode& status) { + if (canonicalize) { + ulocimp_canonicalize(localeID, sink, status); + } else { + ulocimp_getName(localeID, sink, status); + } + }, + status); + }; + // "canonicalize" the locale ID to ICU/Java format err = U_ZERO_ERROR; - length = canonicalize ? - uloc_canonicalize(localeID, fullName, sizeof(fullNameBuffer), &err) : - uloc_getName(localeID, fullName, sizeof(fullNameBuffer), &err); + length = parse(localeID, fullName, sizeof fullNameBuffer, err); if (err == U_BUFFER_OVERFLOW_ERROR || length >= static_cast(sizeof(fullNameBuffer))) { U_ASSERT(baseName == nullptr); @@ -1877,9 +1891,7 @@ Locale& Locale::init(const char* localeID, UBool canonicalize) } fullName = newFullName; err = U_ZERO_ERROR; - length = canonicalize ? - uloc_canonicalize(localeID, fullName, length+1, &err) : - uloc_getName(localeID, fullName, length+1, &err); + length = parse(localeID, fullName, length + 1, err); } if(U_FAILURE(err) || err == U_STRING_NOT_TERMINATED_WARNING) { /* should never occur */ @@ -2200,6 +2212,13 @@ Locale::createFromName (const char *name) } } +Locale U_EXPORT2 +Locale::createFromName(StringPiece name) { + Locale loc(""); + loc.init(name, false); + return loc; +} + Locale U_EXPORT2 Locale::createCanonical(const char* name) { Locale loc(""); diff --git a/icu4c/source/common/ulocale.cpp b/icu4c/source/common/ulocale.cpp index f2f81bc97109..33814713dc1b 100644 --- a/icu4c/source/common/ulocale.cpp +++ b/icu4c/source/common/ulocale.cpp @@ -10,7 +10,6 @@ #include "unicode/locid.h" #include "bytesinkutil.h" -#include "charstr.h" #include "cmemory.h" U_NAMESPACE_USE @@ -24,9 +23,7 @@ ulocale_openForLocaleID(const char* localeID, int32_t length, UErrorCode* err) { if (length < 0) { return EXTERNAL(icu::Locale::createFromName(localeID).clone()); } - CharString str(localeID, length, *err); // Make a NUL terminated copy. - if (U_FAILURE(*err)) { return nullptr; } - return EXTERNAL(icu::Locale::createFromName(str.data()).clone()); + return EXTERNAL(icu::Locale::createFromName(StringPiece{localeID, length}).clone()); } ULocale* diff --git a/icu4c/source/common/unicode/locid.h b/icu4c/source/common/unicode/locid.h index e1afd598cf9b..a394cd9347db 100644 --- a/icu4c/source/common/unicode/locid.h +++ b/icu4c/source/common/unicode/locid.h @@ -449,6 +449,11 @@ class U_COMMON_API Locale : public UObject { */ static Locale U_EXPORT2 createFromName(const char *name); +#ifndef U_HIDE_INTERNAL_API + /** @internal */ + static Locale U_EXPORT2 createFromName(StringPiece name); +#endif /* U_HIDE_INTERNAL_API */ + /** * Creates a locale from the given string after canonicalizing * the string according to CLDR by calling uloc_canonicalize(). @@ -1133,7 +1138,9 @@ class U_COMMON_API Locale : public UObject { * @param cLocaleID The new locale name. * @param canonicalize whether to call uloc_canonicalize on cLocaleID */ - Locale& init(const char* cLocaleID, UBool canonicalize); + Locale& init(const char* localeID, UBool canonicalize); + /** @internal */ + Locale& init(StringPiece localeID, UBool canonicalize); /* * Internal constructor to allow construction of a locale object with