Skip to content

Commit

Permalink
ICU-22494 Avoid adding empty or duplicate variants during locale cano…
Browse files Browse the repository at this point in the history
…ncalization.

It change the failure case (see the bug) from 35s to 0.126s on a very
fast developement machine.
  • Loading branch information
FrankYFTang committed Sep 11, 2023
1 parent 687feb1 commit 35645ab
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions icu4c/source/common/locid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,13 @@ class AliasReplacer {
public:
AliasReplacer(UErrorCode status) :
language(nullptr), script(nullptr), region(nullptr),
extensions(nullptr), variants(status),
extensions(nullptr),
// store value in variants only once
variants(nullptr,
([](UElement e1, UElement e2) -> UBool {
return 0==uprv_strcmp((const char*)e1.pointer,
(const char*)e2.pointer);}),
status),
data(nullptr) {
}
~AliasReplacer() {
Expand Down Expand Up @@ -1653,10 +1659,16 @@ AliasReplacer::replace(const Locale& locale, CharString& out, UErrorCode& status
while ((end = uprv_strchr(start, SEP_CHAR)) != nullptr &&
U_SUCCESS(status)) {
*end = NULL_CHAR; // null terminate inside variantsBuff
variants.addElement(start, status);
// do not add "" or duplicate data to variants
if (*start && !variants.contains(start)) {
variants.addElement(start, status);
}
start = end + 1;
}
variants.addElement(start, status);
// do not add "" or duplicate data to variants
if (*start && !variants.contains(start)) {
variants.addElement(start, status);
}
}
if (U_FAILURE(status)) { return false; }

Expand Down

0 comments on commit 35645ab

Please sign in to comment.