From 5da502247f4ff052c319c15ce37f7bc975a91090 Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Thu, 20 Jul 2023 23:21:09 -0700 Subject: [PATCH] ICU-22365 simplified --- icu4c/source/common/ulocbuilder.cpp | 86 ++++++++--------------------- 1 file changed, 24 insertions(+), 62 deletions(-) diff --git a/icu4c/source/common/ulocbuilder.cpp b/icu4c/source/common/ulocbuilder.cpp index ab7636e8a098..d182da1181de 100644 --- a/icu4c/source/common/ulocbuilder.cpp +++ b/icu4c/source/common/ulocbuilder.cpp @@ -14,25 +14,16 @@ using icu::CheckedArrayByteSink; using icu::StringPiece; -ULocaleBuilder* external(icu::LocaleBuilder* internal) { - return (ULocaleBuilder*)(internal); -} - -const icu::LocaleBuilder* internal(const ULocaleBuilder* external) { - return (const icu::LocaleBuilder*)(external); -} - -icu::LocaleBuilder* internal(ULocaleBuilder* external) { - return (icu::LocaleBuilder*)(external); -} +#define EXTERNAL(i) ((ULocaleBuilder*)(i)) +#define INTERNAL(e) ((icu::LocaleBuilder*)(e)) ULocaleBuilder* ulocbld_open() { - return external(new icu::LocaleBuilder()); + return EXTERNAL(new icu::LocaleBuilder()); } void ulocbld_close(ULocaleBuilder* builder) { if (builder == nullptr) return; - delete internal(builder); + delete INTERNAL(builder); } void ulocbld_setLocale(ULocaleBuilder* builder, const char* locale, int32_t length) { @@ -52,42 +43,27 @@ void ulocbld_setLocale(ULocaleBuilder* builder, const char* locale, int32_t leng l = icu::Locale(buf); } } - internal(builder)->setLocale(l); -} - -void ulocbld_setLanguageTag(ULocaleBuilder* builder, const char* tag, int32_t length) { - if (builder == nullptr) return; - internal(builder)->setLanguageTag( - length < 0 ? StringPiece(tag) : StringPiece(tag, length)); + INTERNAL(builder)->setLocale(l); } -void ulocbld_setLanguage(ULocaleBuilder* builder, const char* language, int32_t length) { - if (builder == nullptr) return; - internal(builder)->setLanguage( - length < 0 ? StringPiece(language) : StringPiece(language, length)); +#define IMPL_ULOCBLD_SETTER(N) \ +void ulocbld_##N(ULocaleBuilder* bld, const char* str, int32_t length) { \ + if (bld == nullptr) return; \ + INTERNAL(bld)->N( \ + length < 0 ? StringPiece(str) : StringPiece(str, length)); \ } -void ulocbld_setScript(ULocaleBuilder* builder, const char* script, int32_t length) { - if (builder == nullptr) return; - internal(builder)->setScript( - length < 0 ? StringPiece(script) : StringPiece(script, length)); -} - -void ulocbld_setRegion(ULocaleBuilder* builder, const char* region, int32_t length) { - if (builder == nullptr) return; - internal(builder)->setRegion( - length < 0 ? StringPiece(region) : StringPiece(region, length)); -} - -void ulocbld_setVariant(ULocaleBuilder* builder, const char* variant, int32_t length) { - if (builder == nullptr) return; - internal(builder)->setVariant( - length < 0 ? StringPiece(variant) : StringPiece(variant, length)); -} +IMPL_ULOCBLD_SETTER(setLanguageTag) +IMPL_ULOCBLD_SETTER(setLanguage) +IMPL_ULOCBLD_SETTER(setScript) +IMPL_ULOCBLD_SETTER(setRegion) +IMPL_ULOCBLD_SETTER(setVariant) +IMPL_ULOCBLD_SETTER(addUnicodeLocaleAttribute) +IMPL_ULOCBLD_SETTER(removeUnicodeLocaleAttribute) void ulocbld_setExtension(ULocaleBuilder* builder, char key, const char* value, int32_t length) { if (builder == nullptr) return; - internal(builder)->setExtension(key, + INTERNAL(builder)->setExtension(key, length < 0 ? StringPiece(value) : StringPiece(value, length)); } @@ -95,33 +71,19 @@ void ulocbld_setUnicodeLocaleKeyword( ULocaleBuilder* builder, const char* key, int32_t keyLength, const char* type, int32_t typeLength) { if (builder == nullptr) return; - internal(builder)->setUnicodeLocaleKeyword( + INTERNAL(builder)->setUnicodeLocaleKeyword( keyLength < 0 ? StringPiece(key) : StringPiece(key, keyLength), typeLength < 0 ? StringPiece(type) : StringPiece(type, typeLength)); } -void ulocbld_addUnicodeLocaleAttribute( - ULocaleBuilder* builder, const char* attribute, int32_t length) { - if (builder == nullptr) return; - internal(builder)->addUnicodeLocaleAttribute( - length < 0 ? StringPiece(attribute) : StringPiece(attribute, length)); -} - -void ulocbld_removeUnicodeLocaleAttribute( - ULocaleBuilder* builder, const char* attribute, int32_t length) { - if (builder == nullptr) return; - internal(builder)->removeUnicodeLocaleAttribute( - length < 0 ? StringPiece(attribute) : StringPiece(attribute, length)); -} - void ulocbld_clear(ULocaleBuilder* builder) { if (builder == nullptr) return; - internal(builder)->clear(); + INTERNAL(builder)->clear(); } void ulocbld_clearExtensions(ULocaleBuilder* builder) { if (builder == nullptr) return; - internal(builder)->clearExtensions(); + INTERNAL(builder)->clearExtensions(); } int32_t ulocbld_buildLocaleID(ULocaleBuilder* builder, @@ -130,7 +92,7 @@ int32_t ulocbld_buildLocaleID(ULocaleBuilder* builder, *err = U_ILLEGAL_ARGUMENT_ERROR; return 0; } - icu::Locale l = internal(builder)->build(*err); + icu::Locale l = INTERNAL(builder)->build(*err); if (U_FAILURE(*err)) return 0; int32_t length = (int32_t)(uprv_strlen(l.getName())); if (length + 1 > bufferCapacity) { @@ -152,7 +114,7 @@ int32_t ulocbld_buildLanguageTag(ULocaleBuilder* builder, *err = U_ILLEGAL_ARGUMENT_ERROR; return 0; } - icu::Locale l = internal(builder)->build(*err); + icu::Locale l = INTERNAL(builder)->build(*err); if (U_FAILURE(*err)) return 0; CheckedArrayByteSink sink(buffer, bufferCapacity); l.toLanguageTag(sink, *err); @@ -169,5 +131,5 @@ UBool ulocbld_copyErrorTo(const ULocaleBuilder* builder, UErrorCode *outErrorCod *outErrorCode = U_ILLEGAL_ARGUMENT_ERROR; return true; } - return internal(builder)->copyErrorTo(*outErrorCode); + return INTERNAL(builder)->copyErrorTo(*outErrorCode); }