godot/thirdparty/icu4c/common/ulocale.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
3.2 KiB
C++
Raw Normal View History

2023-11-01 06:56:12 +00:00
// © 2023 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
//
2024-05-14 08:41:19 +00:00
#include "unicode/bytestream.h"
2023-11-01 06:56:12 +00:00
#include "unicode/errorcode.h"
#include "unicode/stringpiece.h"
#include "unicode/utypes.h"
#include "unicode/ustring.h"
#include "unicode/ulocale.h"
#include "unicode/locid.h"
2024-05-14 08:41:19 +00:00
#include "bytesinkutil.h"
2023-11-01 06:56:12 +00:00
#include "charstr.h"
#include "cmemory.h"
U_NAMESPACE_USE
#define EXTERNAL(i) (reinterpret_cast<ULocale*>(i))
#define CONST_INTERNAL(e) (reinterpret_cast<const icu::Locale*>(e))
#define INTERNAL(e) (reinterpret_cast<icu::Locale*>(e))
ULocale*
ulocale_openForLocaleID(const char* localeID, int32_t length, UErrorCode* err) {
2024-05-14 08:41:19 +00:00
if (U_FAILURE(*err)) { return nullptr; }
2023-11-01 06:56:12 +00:00
CharString str(length < 0 ? StringPiece(localeID) : StringPiece(localeID, length), *err);
2024-05-14 08:41:19 +00:00
if (U_FAILURE(*err)) { return nullptr; }
2023-11-01 06:56:12 +00:00
return EXTERNAL(icu::Locale::createFromName(str.data()).clone());
}
ULocale*
ulocale_openForLanguageTag(const char* tag, int32_t length, UErrorCode* err) {
2024-05-14 08:41:19 +00:00
if (U_FAILURE(*err)) { return nullptr; }
2023-11-01 06:56:12 +00:00
Locale l = icu::Locale::forLanguageTag(length < 0 ? StringPiece(tag) : StringPiece(tag, length), *err);
2024-05-14 08:41:19 +00:00
if (U_FAILURE(*err)) { return nullptr; }
2023-11-01 06:56:12 +00:00
return EXTERNAL(l.clone());
}
void
ulocale_close(ULocale* 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 CONST_INTERNAL(locale)->get ## N2(); \
}
#define IMPL_ULOCALE_STRING_IDENTICAL_GETTER(N) IMPL_ULOCALE_STRING_GETTER(N, 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; \
} \
2024-05-14 08:41:19 +00:00
return ByteSinkUtil::viaByteSinkToTerminatedChars( \
valueBuffer, bufferCapacity, \
[&](ByteSink& sink, UErrorCode& status) { \
CONST_INTERNAL(locale)->get ## N( \
keywordLength < 0 ? StringPiece(keyword) : StringPiece(keyword, keywordLength), \
sink, status); \
}, \
*err); \
2023-11-01 06:56:12 +00:00
}
#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); \
}
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_GET_KEYWORD_VALUE(KeywordValue)
IMPL_ULOCALE_GET_KEYWORD_VALUE(UnicodeKeywordValue)
IMPL_ULOCALE_GET_KEYWORDS(Keywords)
IMPL_ULOCALE_GET_KEYWORDS(UnicodeKeywords)
bool ulocale_isBogus(const ULocale* locale) {
if (locale == nullptr) return false;
return CONST_INTERNAL(locale)->isBogus();
}
/*eof*/