From 9bce52f003496527b2e37e31ae08521cd35212ac Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Tue, 26 Sep 2023 01:08:04 +0200 Subject: [PATCH] ICU-21289 Switch to using CharString for calling uloc_toLanguageTag(). --- icu4c/source/common/locmap.cpp | 7 +++++-- icu4c/source/i18n/windtfmt.cpp | 10 ++++++++-- icu4c/source/i18n/winnmfmt.cpp | 10 ++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/icu4c/source/common/locmap.cpp b/icu4c/source/common/locmap.cpp index 1d5e1f2a75c..e41cfd1027d 100644 --- a/icu4c/source/common/locmap.cpp +++ b/icu4c/source/common/locmap.cpp @@ -1200,9 +1200,12 @@ uprv_convertToLCIDPlatform(const char* localeID, UErrorCode* status) } } - char asciiBCP47Tag[LOCALE_NAME_MAX_LENGTH] = {}; // this will change it from de_DE@collation=phonebook to de-DE-u-co-phonebk form - (void)uloc_toLanguageTag(mylocaleID, asciiBCP47Tag, UPRV_LENGTHOF(asciiBCP47Tag), false, status); + icu::CharString asciiBCP47Tag; + { + icu::CharStringByteSink sink(&asciiBCP47Tag); + ulocimp_toLanguageTag(mylocaleID, sink, false, status); + } if (U_SUCCESS(*status)) { diff --git a/icu4c/source/i18n/windtfmt.cpp b/icu4c/source/i18n/windtfmt.cpp index 2118f92dceb..6d705b299d7 100644 --- a/icu4c/source/i18n/windtfmt.cpp +++ b/icu4c/source/i18n/windtfmt.cpp @@ -30,7 +30,10 @@ #include "unicode/timezone.h" #include "unicode/utmscale.h" +#include "bytesinkutil.h" +#include "charstr.h" #include "cmemory.h" +#include "ulocimp.h" #include "uresimp.h" #include "windtfmt.h" #include "wintzimpl.h" @@ -99,10 +102,13 @@ UnicodeString* Win32DateFormat::getTimeDateFormat(const Calendar *cal, const Loc static UErrorCode GetEquivalentWindowsLocaleName(const Locale& locale, UnicodeString** buffer) { UErrorCode status = U_ZERO_ERROR; - char asciiBCP47Tag[LOCALE_NAME_MAX_LENGTH] = {}; // Convert from names like "en_CA" and "de_DE@collation=phonebook" to "en-CA" and "de-DE-u-co-phonebk". - (void)uloc_toLanguageTag(locale.getName(), asciiBCP47Tag, UPRV_LENGTHOF(asciiBCP47Tag), false, &status); + CharString asciiBCP47Tag; + { + CharStringByteSink sink(&asciiBCP47Tag); + ulocimp_toLanguageTag(locale.getName(), sink, false, &status); + } if (U_SUCCESS(status)) { diff --git a/icu4c/source/i18n/winnmfmt.cpp b/icu4c/source/i18n/winnmfmt.cpp index 6efa9a63d86..bf0bd20095c 100644 --- a/icu4c/source/i18n/winnmfmt.cpp +++ b/icu4c/source/i18n/winnmfmt.cpp @@ -24,8 +24,11 @@ #include "unicode/locid.h" #include "unicode/ustring.h" +#include "bytesinkutil.h" +#include "charstr.h" #include "cmemory.h" #include "uassert.h" +#include "ulocimp.h" #include "locmap.h" #ifndef WIN32_LEAN_AND_MEAN @@ -144,10 +147,13 @@ static void freeCurrencyFormat(CURRENCYFMTW *fmt) static UErrorCode GetEquivalentWindowsLocaleName(const Locale& locale, UnicodeString** buffer) { UErrorCode status = U_ZERO_ERROR; - char asciiBCP47Tag[LOCALE_NAME_MAX_LENGTH] = {}; // Convert from names like "en_CA" and "de_DE@collation=phonebook" to "en-CA" and "de-DE-u-co-phonebk". - (void) uloc_toLanguageTag(locale.getName(), asciiBCP47Tag, UPRV_LENGTHOF(asciiBCP47Tag), false, &status); + CharString asciiBCP47Tag; + { + CharStringByteSink sink(&asciiBCP47Tag); + ulocimp_toLanguageTag(locale.getName(), sink, false, &status); + } if (U_SUCCESS(status)) {