Skip to content

Commit

Permalink
Use Macro
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Jul 21, 2023
1 parent 475697c commit 9fefa1a
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions icu4c/source/common/uloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2227,61 +2227,54 @@ uloc_toLegacyType(const char* keyword, const char* value)
}

// ========== ULocale API ==========================
ULocale* external(icu::Locale* internal) {
return (ULocale*)(internal);
}

const icu::Locale* internal(const ULocale* external) {
return (const icu::Locale*)(external);
}

icu::Locale* internal(ULocale* external) {
return (icu::Locale*)(external);
}
#define EXTERNAL(i) ((ULocale*)(i))
#define CONST_INTERNAL(e) ((const icu::Locale*)(e))
#define INTERNAL(e) ((icu::Locale*)(e))

ULocale*
ulocale_openLocaleID(const char* localeID, int32_t length) {
if (length < 0 || localeID[length] == '\0') {
return external(new icu::Locale(localeID));
return EXTERNAL(new icu::Locale(localeID));
} else {
if (length < ULOC_FULLNAME_CAPACITY-1) {
char buf[ULOC_FULLNAME_CAPACITY];
uprv_memcpy(buf, localeID, length);
buf[length] = '\0';
return external(new icu::Locale(buf));
return EXTERNAL(new icu::Locale(buf));
}
// return a bogus one
Locale bogus;
bogus.setToBogus();
return external(bogus.clone());
return EXTERNAL(bogus.clone());
}
}

ULocale*
ulocale_openForLanguageTag(const char* tag, int32_t length, UErrorCode* err) {
if (U_FAILURE(*err)) return nullptr;
return external(icu::Locale::forLanguageTag(
return EXTERNAL(icu::Locale::forLanguageTag(
length < 0 ? StringPiece(tag) : StringPiece(tag, length), *err).clone());
}

void
ulocale_close(ULocale* locale) {
if (locale == nullptr) return;
delete internal(locale);
delete INTERNAL(locale);
}

#define IMPL_ULOCALE_STRING_GETTER(N1, N2) \
const char* ulocale_get ## N1(const ULocale* locale) { \
if (locale == nullptr) return nullptr; \
return internal(locale)->get ## N2(); \
return CONST_INTERNAL(locale)->get ## N2(); \
}

#define IMPL_ULOCALE_STRING_IDENTICAL_GETTER(N) IMPL_ULOCALE_STRING_GETTER(N, N)

#define IMPL_ULOCALE_BOOL_IDENTICAL_GETTER(N) \
bool ulocale_is ## N(const ULocale* locale) { \
if (locale == nullptr) return false; \
return internal(locale)->is ## N(); \
return CONST_INTERNAL(locale)->is ## N(); \
}

#define IMPL_ULOCALE_GET_KEYWORD_VALUE(N) \
Expand All @@ -2294,7 +2287,7 @@ int32_t ulocale_get ##N ( \
return 0; \
} \
CheckedArrayByteSink sink(valueBuffer, bufferCapacity); \
internal(locale)->get ## N( \
CONST_INTERNAL(locale)->get ## N( \
keywordLength < 0 ? StringPiece(keyword) : StringPiece(keyword, keywordLength), \
sink, *err); \
if (U_FAILURE(*err)) return 0; \
Expand All @@ -2313,7 +2306,7 @@ UEnumeration* ulocale_get ## N(const ULocale* locale, UErrorCode *err) { \
return nullptr; \
} \
return uenum_openFromStringEnumeration( \
internal(locale)->create ## N(*err), err); \
CONST_INTERNAL(locale)->create ## N(*err), err); \
}

#define IMPL_ULOCALE_PRODUCER(N) \
Expand All @@ -2323,9 +2316,9 @@ ULocale* ulocale_ ## N(const ULocale* locale, UErrorCode *err) { \
*err = U_ILLEGAL_ARGUMENT_ERROR; \
return nullptr; \
} \
icu::Locale* that = internal(locale)->clone(); \
icu::Locale* that = CONST_INTERNAL(locale)->clone(); \
that->N(*err); \
return external(that); \
return EXTERNAL(that); \
}

IMPL_ULOCALE_STRING_IDENTICAL_GETTER(Language)
Expand All @@ -2346,11 +2339,4 @@ IMPL_ULOCALE_PRODUCER(canonicalize)
IMPL_ULOCALE_PRODUCER(addLikelySubtags)
IMPL_ULOCALE_PRODUCER(minimizeSubtags)

#undef IMPL_ULOCALE_STRING_IDENTICAL_GETTER
#undef IMPL_ULOCALE_STRING_GETTER
#undef IMPL_ULOCALE_BOOL_IDENTICAL_GETTER
#undef IMPL_ULOCALE_GET_KEYWORD_VALUE
#undef IMPL_ULOCALE_GET_KEYWORDS
#undef IMPL_ULOCALE_PRODUCER

/*eof*/

0 comments on commit 9fefa1a

Please sign in to comment.