Skip to content

Commit

Permalink
ICU-22365 C API for ULocaleBuilder
Browse files Browse the repository at this point in the history
See #2520

ICU-22365 Fix comments
  • Loading branch information
FrankYFTang committed Aug 3, 2023
1 parent 9511fff commit e3b3171
Show file tree
Hide file tree
Showing 13 changed files with 2,304 additions and 1 deletion.
1 change: 1 addition & 0 deletions icu4c/source/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ cc_library(
"uloc.cpp",
"uloc_tag.cpp",
"uloc_keytype.cpp",
"ulocbuilder.cpp",
"uresbund.cpp",
"uresdata.cpp",
"wintz.cpp",
Expand Down
1 change: 1 addition & 0 deletions icu4c/source/common/common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
<ClCompile Include="uidna.cpp" />
<ClCompile Include="uts46.cpp" />
<ClCompile Include="localebuilder.cpp" />
<ClCompile Include="ulocbuilder.cpp" />
<ClCompile Include="localematcher.cpp" />
<ClCompile Include="localeprioritylist.cpp" />
<ClCompile Include="locavailable.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions icu4c/source/common/common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@
<ClCompile Include="localebuilder.cpp">
<Filter>locales &amp; resources</Filter>
</ClCompile>
<ClCompile Include="ulocbuilder.cpp">
<Filter>locales &amp; resources</Filter>
</ClCompile>
<ClCompile Include="localematcher.cpp">
<Filter>locales &amp; resources</Filter>
</ClCompile>
Expand Down Expand Up @@ -1132,6 +1135,9 @@
<CustomBuild Include="unicode\localebuilder.h">
<Filter>locales &amp; resources</Filter>
</CustomBuild>
<CustomBuild Include="unicode\ulocbuilder.h">
<Filter>locales &amp; resources</Filter>
</CustomBuild>
<CustomBuild Include="unicode\localematcher.h">
<Filter>locales &amp; resources</Filter>
</CustomBuild>
Expand Down
1 change: 1 addition & 0 deletions icu4c/source/common/common_uwp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@
<ClCompile Include="uidna.cpp" />
<ClCompile Include="uts46.cpp" />
<ClCompile Include="localebuilder.cpp" />
<ClCompile Include="ulocbuilder.cpp" />
<ClCompile Include="localematcher.cpp" />
<ClCompile Include="localeprioritylist.cpp" />
<ClCompile Include="locavailable.cpp" />
Expand Down
1 change: 1 addition & 0 deletions icu4c/source/common/sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ ulist.cpp
uloc.cpp
uloc_keytype.cpp
uloc_tag.cpp
ulocbuilder.cpp
umapfile.cpp
umath.cpp
umutablecptrie.cpp
Expand Down
145 changes: 145 additions & 0 deletions icu4c/source/common/ulocbuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// © 2023 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

#include <utility>

#include "unicode/bytestream.h"
#include "unicode/localebuilder.h"
#include "unicode/locid.h"
#include "unicode/stringpiece.h"
#include "unicode/umachine.h"
#include "unicode/ulocbuilder.h"
#include "cstring.h"

using icu::CheckedArrayByteSink;
using icu::StringPiece;

#define EXTERNAL(i) ((ULocaleBuilder*)(i))
#define INTERNAL(e) ((icu::LocaleBuilder*)(e))

ULocaleBuilder* ulocbld_open() {
return EXTERNAL(new icu::LocaleBuilder());
}

void ulocbld_close(ULocaleBuilder* builder) {
if (builder == nullptr) return;
delete INTERNAL(builder);
}

void ulocbld_setLocale(ULocaleBuilder* builder, const char* locale, int32_t length) {
if (builder == nullptr) return;
icu::Locale l;
if (length < 0 || locale[length] == '\0') {
l = icu::Locale(locale);
} else {
if (length >= ULOC_FULLNAME_CAPACITY) {
l.setToBogus();
} else {
// locale is not null termined but Locale API require one.
// Create a null termined version in buf.
char buf[ULOC_FULLNAME_CAPACITY];
uprv_memcpy(buf, locale, length);
buf[length] = '\0';
l = icu::Locale(buf);
}
}
INTERNAL(builder)->setLocale(l);
}

#define STRING_PIECE(s, l) ((l)<0 ? StringPiece(s) : StringPiece((s), (l)))

#define IMPL_ULOCBLD_SETTER(N) \
void ulocbld_##N(ULocaleBuilder* bld, const char* s, int32_t l) { \
if (bld == nullptr) return; \
INTERNAL(bld)->N(STRING_PIECE(s,l)); \
}

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, STRING_PIECE(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(
STRING_PIECE(key, keyLength), STRING_PIECE(type, typeLength));
}

void ulocbld_clear(ULocaleBuilder* builder) {
if (builder == nullptr) return;
INTERNAL(builder)->clear();
}

void ulocbld_clearExtensions(ULocaleBuilder* builder) {
if (builder == nullptr) return;
INTERNAL(builder)->clearExtensions();
}

int32_t ulocbld_buildLocaleID(ULocaleBuilder* builder,
char* buffer, int32_t bufferCapacity, UErrorCode* err) {
if (builder == nullptr) {
*err = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
icu::Locale l = INTERNAL(builder)->build(*err);
if (U_FAILURE(*err)) return 0;
int32_t length = (int32_t)(uprv_strlen(l.getName()));
if (length <= bufferCapacity) {
uprv_strncpy(buffer, l.getName(), length);
if (length < bufferCapacity) {
buffer[length] = '\0';
} else {
*err = U_STRING_NOT_TERMINATED_WARNING;
}
return length;
}
*err = U_BUFFER_OVERFLOW_ERROR;
uprv_memcpy(buffer, l.getName(), bufferCapacity);
return length;
}

int32_t ulocbld_buildLanguageTag(ULocaleBuilder* builder,
char* buffer, int32_t bufferCapacity, UErrorCode* err) {
if (builder == nullptr) {
*err = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
icu::Locale l = INTERNAL(builder)->build(*err);
if (U_FAILURE(*err)) return 0;
CheckedArrayByteSink sink(buffer, bufferCapacity);
l.toLanguageTag(sink, *err);
if (U_FAILURE(*err)) {
return 0;
}
if (sink.Overflowed()) {
*err = U_BUFFER_OVERFLOW_ERROR;
return sink.NumberOfBytesAppended();
}
int32_t written = sink.NumberOfBytesWritten();

if (written < bufferCapacity) {
// null terminate
buffer[written] = '\0';
} else {
*err = U_STRING_NOT_TERMINATED_WARNING;
}
return written;
}

UBool ulocbld_copyErrorTo(const ULocaleBuilder* builder, UErrorCode *outErrorCode) {
if (builder == nullptr) {
*outErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
return true;
}
return INTERNAL(builder)->copyErrorTo(*outErrorCode);
}
Loading

0 comments on commit e3b3171

Please sign in to comment.