Skip to content

Commit

Permalink
ICU-22435 Add C API for Locale
Browse files Browse the repository at this point in the history
Add implementation

Add pointer

Use Macro

ICU-22435 fix param
  • Loading branch information
FrankYFTang committed Jul 21, 2023
1 parent cd6ff4a commit 43a4ed7
Show file tree
Hide file tree
Showing 2 changed files with 409 additions and 0 deletions.
114 changes: 114 additions & 0 deletions icu4c/source/common/uloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "unicode/utypes.h"
#include "unicode/ustring.h"
#include "unicode/uloc.h"
#include "unicode/locid.h"

#include "bytesinkutil.h"
#include "putilimp.h"
Expand Down Expand Up @@ -2225,4 +2226,117 @@ uloc_toLegacyType(const char* keyword, const char* value)
return legacyType;
}

// ========== ULocale API ==========================

#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));
} 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 a bogus one
Locale bogus;
bogus.setToBogus();
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(
length < 0 ? StringPiece(tag) : StringPiece(tag, length), *err).clone());
}

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

#define IMPL_ULOCALE_STRING_GETTER(N1, N2) \
const char* ulocale_get ## N1(const ULocale* locale) { \
if (locale == nullptr) return nullptr; \
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 CONST_INTERNAL(locale)->is ## N(); \
}

#define IMPL_ULOCALE_GET_KEYWORD_VALUE(N) \
int32_t ulocale_get ##N ( \
const ULocale* locale, const char* keyword, int32_t keywordLength, \
char* valueBuffer, int32_t bufferCapacity, UErrorCode *err) { \
if (U_FAILURE(*err)) return 0; \
if (locale == nullptr) { \
*err = U_ILLEGAL_ARGUMENT_ERROR; \
return 0; \
} \
CheckedArrayByteSink sink(valueBuffer, bufferCapacity); \
CONST_INTERNAL(locale)->get ## N( \
keywordLength < 0 ? StringPiece(keyword) : StringPiece(keyword, keywordLength), \
sink, *err); \
if (U_FAILURE(*err)) return 0; \
if (sink.Overflowed()) { \
*err = U_BUFFER_OVERFLOW_ERROR; \
return sink.NumberOfBytesAppended()+1; \
} \
return sink.NumberOfBytesWritten(); \
}

#define IMPL_ULOCALE_GET_KEYWORDS(N) \
UEnumeration* ulocale_get ## N(const ULocale* locale, UErrorCode *err) { \
if (U_FAILURE(*err)) return nullptr; \
if (locale == nullptr) { \
*err = U_ILLEGAL_ARGUMENT_ERROR; \
return nullptr; \
} \
return uenum_openFromStringEnumeration( \
CONST_INTERNAL(locale)->create ## N(*err), err); \
}

#define IMPL_ULOCALE_PRODUCER(N) \
ULocale* ulocale_ ## N(const ULocale* locale, UErrorCode *err) { \
if (U_FAILURE(*err)) return nullptr; \
if (locale == nullptr) { \
*err = U_ILLEGAL_ARGUMENT_ERROR; \
return nullptr; \
} \
icu::Locale* that = CONST_INTERNAL(locale)->clone(); \
that->N(*err); \
return EXTERNAL(that); \
}

IMPL_ULOCALE_STRING_IDENTICAL_GETTER(Language)
IMPL_ULOCALE_STRING_IDENTICAL_GETTER(Script)
IMPL_ULOCALE_STRING_GETTER(Region, Country)
IMPL_ULOCALE_STRING_IDENTICAL_GETTER(Variant)
IMPL_ULOCALE_STRING_GETTER(LocaleID, Name)
IMPL_ULOCALE_STRING_IDENTICAL_GETTER(BaseName)
IMPL_ULOCALE_STRING_IDENTICAL_GETTER(ISO3Language)
IMPL_ULOCALE_STRING_GETTER(ISO3Region, ISO3Country)
IMPL_ULOCALE_BOOL_IDENTICAL_GETTER(RightToLeft)
IMPL_ULOCALE_BOOL_IDENTICAL_GETTER(Bogus)
IMPL_ULOCALE_GET_KEYWORD_VALUE(KeywordValue)
IMPL_ULOCALE_GET_KEYWORD_VALUE(UnicodeKeywordValue)
IMPL_ULOCALE_GET_KEYWORDS(Keywords)
IMPL_ULOCALE_GET_KEYWORDS(UnicodeKeywords)
IMPL_ULOCALE_PRODUCER(canonicalize)
IMPL_ULOCALE_PRODUCER(addLikelySubtags)
IMPL_ULOCALE_PRODUCER(minimizeSubtags)

/*eof*/
Loading

0 comments on commit 43a4ed7

Please sign in to comment.