Skip to content

Commit

Permalink
ICU-22727 fix addLikelySubtags for 5-8 chars lang codes
Browse files Browse the repository at this point in the history
Fix C++ code, add tests for C, C++ and Java API
  • Loading branch information
FrankYFTang committed Apr 11, 2024
1 parent 9e1c66d commit 6d4cea5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
7 changes: 5 additions & 2 deletions icu4c/source/common/loclikely.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,17 @@ _uloc_addLikelySubtags(const char* localeID,
return;
}

if (lang.length() > 3) {
if (lang.length() == 4 && script.isEmpty()) {
if (lang.length() == 4) {
if (script.isEmpty()) {
script = std::move(lang);
lang.clear();
} else {
err = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
} else if (lang.length() > 8) {
err = U_ILLEGAL_ARGUMENT_ERROR;
return;
}

int32_t trailingLength = (int32_t)uprv_strlen(trailing);
Expand Down
31 changes: 31 additions & 0 deletions icu4c/source/test/cintltst/cloctst.c
Original file line number Diff line number Diff line change
Expand Up @@ -3867,6 +3867,27 @@ const char* const basic_maximize_data[][2] = {
// so "aaaa" is a well-formed unicode_language_id
"aaaa",
"aaaa",
}, {
// ICU-22727
// unicode_language_subtag = alpha{2,3} | alpha{5,8};
// so "bbbbb", "cccccc", "ddddddd", "eeeeeeee" are
// well-formed unicode_language_subtag and therefore
// well-formed unicode_language_id
// but "fffffffff" is not.
"bbbbb",
"bbbbb",
}, {
// ICU-22727
"cccccc",
"cccccc",
}, {
// ICU-22727
"ddddddd",
"ddddddd",
}, {
// ICU-22727
"eeeeeeee",
"eeeeeeee",
}, {
// ICU-22546
"und-Zzzz",
Expand Down Expand Up @@ -6048,6 +6069,16 @@ const errorData maximizeErrors[] = {
"en_Latn_US_POSIX@currency=EURO",
U_STRING_NOT_TERMINATED_WARNING,
30
},
{
// ICU-22727
// unicode_language_subtag = alpha{2,3} | alpha{5,8};
// so "bbbbb", "cccccc", "ddddddd", "eeeeeeee" are
// well-formed unicode_language_id but "fffffffff" is not.
"fffffffff",
NULL,
U_ILLEGAL_ARGUMENT_ERROR,
0
}
};

Expand Down
25 changes: 25 additions & 0 deletions icu4c/source/test/intltest/loctest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3915,6 +3915,31 @@ LocaleTest::TestAddLikelyAndMinimizeSubtags() {
"aaaa",
"aaaa",
"aaaa",
}, {
// ICU-22727
// unicode_language_subtag = alpha{2,3} | alpha{5,8};
// so "bbbbb", "cccccc", "ddddddd", "eeeeeeee" are
// well-formed unicode_language_subtag and therefore
// well-formed unicode_language_id
// but "fffffffff" is not.
"bbbbb",
"bbbbb",
"bbbbb",
}, {
// ICU-22727
"cccccc",
"cccccc",
"cccccc",
}, {
// ICU-22727
"ddddddd",
"ddddddd",
"ddddddd",
}, {
// ICU-22727
"eeeeeeee",
"eeeeeeee",
"eeeeeeee",
}, {
// ICU-22546
"und-Zzzz",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,26 @@ public void TestAddLikelySubtags() {
// so "aaaa" is a well-formed unicode_language_id
"aaaa",
"aaaa",
}, {
// ICU-22727
// unicode_language_subtag = alpha{2,3} | alpha{5,8};
// so "bbbbb", "cccccc", "ddddddd", "eeeeeeee" are
// well-formed unicode_language_subtag and therefore
// well-formed unicode_language_id
"bbbbb",
"bbbbb",
}, {
// ICU-22727
"cccccc",
"cccccc",
}, {
// ICU-22727
"ddddddd",
"ddddddd",
}, {
// ICU-22727
"eeeeeeee",
"eeeeeeee",
}, {
// ICU-22546
"und-Zzzz",
Expand Down

0 comments on commit 6d4cea5

Please sign in to comment.