Skip to content

Commit

Permalink
ICU-22365 simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Jul 21, 2023
1 parent fb61f2e commit 5da5022
Showing 1 changed file with 24 additions and 62 deletions.
86 changes: 24 additions & 62 deletions icu4c/source/common/ulocbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -52,76 +43,47 @@ 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));
}

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,
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
}

0 comments on commit 5da5022

Please sign in to comment.