Skip to content

Commit

Permalink
ICU-22531 Remove X from XLikelySubtags*
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Nov 17, 2023
1 parent 102ed8b commit 8d3d214
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 89 deletions.
8 changes: 4 additions & 4 deletions icu4c/source/common/localematcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ LocaleMatcher LocaleMatcher::Builder::build(UErrorCode &errorCode) const {

namespace {

LSR getMaximalLsrOrUnd(const XLikelySubtags &likelySubtags, const Locale &locale,
LSR getMaximalLsrOrUnd(const LikelySubtags &likelySubtags, const Locale &locale,
UErrorCode &errorCode) {
if (U_FAILURE(errorCode) || locale.isBogus() || *locale.getName() == 0 /* "und" */) {
return UND_LSR;
Expand Down Expand Up @@ -338,7 +338,7 @@ int32_t LocaleMatcher::putIfAbsent(const LSR &lsr, int32_t i, int32_t suppLength
}

LocaleMatcher::LocaleMatcher(const Builder &builder, UErrorCode &errorCode) :
likelySubtags(*XLikelySubtags::getSingleton(errorCode)),
likelySubtags(*LikelySubtags::getSingleton(errorCode)),
localeDistance(*LocaleDistance::getSingleton(errorCode)),
thresholdDistance(builder.thresholdDistance_),
demotionPerDesiredLocale(0),
Expand Down Expand Up @@ -551,7 +551,7 @@ LocaleMatcher &LocaleMatcher::operator=(LocaleMatcher &&src) noexcept {

class LocaleLsrIterator {
public:
LocaleLsrIterator(const XLikelySubtags &likelySubtags, Locale::Iterator &locales,
LocaleLsrIterator(const LikelySubtags &likelySubtags, Locale::Iterator &locales,
ULocMatchLifetime lifetime) :
likelySubtags(likelySubtags), locales(locales), lifetime(lifetime) {}

Expand Down Expand Up @@ -596,7 +596,7 @@ class LocaleLsrIterator {
}

private:
const XLikelySubtags &likelySubtags;
const LikelySubtags &likelySubtags;
Locale::Iterator &locales;
ULocMatchLifetime lifetime;
const Locale *current = nullptr, *remembered = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions icu4c/source/common/locdistance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ UBool U_CALLCONV cleanup() {
void U_CALLCONV LocaleDistance::initLocaleDistance(UErrorCode &errorCode) {
// This function is invoked only via umtx_initOnce().
U_ASSERT(gLocaleDistance == nullptr);
const XLikelySubtags &likely = *XLikelySubtags::getSingleton(errorCode);
const LikelySubtags &likely = *LikelySubtags::getSingleton(errorCode);
if (U_FAILURE(errorCode)) { return; }
const LocaleDistanceData &data = likely.getDistanceData();
if (data.distanceTrieBytes == nullptr ||
Expand All @@ -83,7 +83,7 @@ const LocaleDistance *LocaleDistance::getSingleton(UErrorCode &errorCode) {
return gLocaleDistance;
}

LocaleDistance::LocaleDistance(const LocaleDistanceData &data, const XLikelySubtags &likely) :
LocaleDistance::LocaleDistance(const LocaleDistanceData &data, const LikelySubtags &likely) :
likelySubtags(likely),
trie(data.distanceTrieBytes),
regionToPartitionsIndex(data.regionToPartitions), partitionArrays(data.partitions),
Expand Down Expand Up @@ -119,7 +119,7 @@ int32_t LocaleDistance::getBestIndexAndDistance(
uint64_t desLangState = desLangDistance >= 0 && supportedLSRsLength > 1 ? iter.getState64() : 0;
// Index of the supported LSR with the lowest distance.
int32_t bestIndex = -1;
// Cached lookup info from XLikelySubtags.compareLikely().
// Cached lookup info from LikelySubtags.compareLikely().
int32_t bestLikelyInfo = -1;
for (int32_t slIndex = 0; slIndex < supportedLSRsLength; ++slIndex) {
const LSR &supported = *supportedLSRs[slIndex];
Expand Down
4 changes: 2 additions & 2 deletions icu4c/source/common/locdistance.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class LocaleDistance final : public UMemory {
// tic constexpr int32_t MAX_INDEX = 0x1fffff; // avoids sign bit
static constexpr int32_t INDEX_NEG_1 = 0xfffffc00;

LocaleDistance(const LocaleDistanceData &data, const XLikelySubtags &likely);
LocaleDistance(const LocaleDistanceData &data, const LikelySubtags &likely);
LocaleDistance(const LocaleDistance &other) = delete;
LocaleDistance &operator=(const LocaleDistance &other) = delete;

Expand Down Expand Up @@ -119,7 +119,7 @@ class LocaleDistance final : public UMemory {
return defaultRegionDistance;
}

const XLikelySubtags &likelySubtags;
const LikelySubtags &likelySubtags;

// The trie maps each dlang+slang+dscript+sscript+dregion+sregion
// (encoded in ASCII with bit 7 set on the last character of each subtag) to a distance.
Expand Down
4 changes: 2 additions & 2 deletions icu4c/source/common/loclikely.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ _uloc_addLikelySubtags(const char* localeID,

CHECK_TRAILING_VARIANT_SIZE(trailing, trailingLength);
{
const icu::XLikelySubtags* likelySubtags = icu::XLikelySubtags::getSingleton(*err);
const icu::LikelySubtags* likelySubtags = icu::LikelySubtags::getSingleton(*err);
if(U_FAILURE(*err)) {
goto error;
}
Expand Down Expand Up @@ -590,7 +590,7 @@ _uloc_minimizeSubtags(const char* localeID,
CHECK_TRAILING_VARIANT_SIZE(trailing, trailingLength);

{
const icu::XLikelySubtags* likelySubtags = icu::XLikelySubtags::getSingleton(*err);
const icu::LikelySubtags* likelySubtags = icu::LikelySubtags::getSingleton(*err);
if(U_FAILURE(*err)) {
goto error;
}
Expand Down
45 changes: 22 additions & 23 deletions icu4c/source/common/loclikelysubtags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ LocaleDistanceData::~LocaleDistanceData() {
delete[] paradigms;
}

// TODO(ICU-20777): Rename to just LikelySubtagsData.
struct XLikelySubtagsData {
struct LikelySubtagsData {
UResourceBundle *langInfoBundle = nullptr;
UniqueCharStrings strings;
CharStringMap languageAliases;
Expand All @@ -63,9 +62,9 @@ struct XLikelySubtagsData {

LocaleDistanceData distanceData;

XLikelySubtagsData(UErrorCode &errorCode) : strings(errorCode) {}
LikelySubtagsData(UErrorCode &errorCode) : strings(errorCode) {}

~XLikelySubtagsData() {
~LikelySubtagsData() {
ures_close(langInfoBundle);
delete[] lsrs;
}
Expand Down Expand Up @@ -339,7 +338,7 @@ struct XLikelySubtagsData {

namespace {

XLikelySubtags *gLikelySubtags = nullptr;
LikelySubtags *gLikelySubtags = nullptr;
UVector *gMacroregions = nullptr;
UInitOnce gInitOnce {};

Expand Down Expand Up @@ -443,13 +442,13 @@ UVector* getStaticMacroregions(UErrorCode &status) {

} // namespace

void U_CALLCONV XLikelySubtags::initLikelySubtags(UErrorCode &errorCode) {
void U_CALLCONV LikelySubtags::initLikelySubtags(UErrorCode &errorCode) {
// This function is invoked only via umtx_initOnce().
U_ASSERT(gLikelySubtags == nullptr);
XLikelySubtagsData data(errorCode);
LikelySubtagsData data(errorCode);
data.load(errorCode);
if (U_FAILURE(errorCode)) { return; }
gLikelySubtags = new XLikelySubtags(data);
gLikelySubtags = new LikelySubtags(data);
gMacroregions = getStaticMacroregions(errorCode);
#if U_DEBUG
auto macroregionsFromData = loadMacroregions(errorCode);
Expand All @@ -466,13 +465,13 @@ void U_CALLCONV XLikelySubtags::initLikelySubtags(UErrorCode &errorCode) {
ucln_common_registerCleanup(UCLN_COMMON_LIKELY_SUBTAGS, cleanup);
}

const XLikelySubtags *XLikelySubtags::getSingleton(UErrorCode &errorCode) {
const LikelySubtags *LikelySubtags::getSingleton(UErrorCode &errorCode) {
if (U_FAILURE(errorCode)) { return nullptr; }
umtx_initOnce(gInitOnce, &XLikelySubtags::initLikelySubtags, errorCode);
umtx_initOnce(gInitOnce, &LikelySubtags::initLikelySubtags, errorCode);
return gLikelySubtags;
}

XLikelySubtags::XLikelySubtags(XLikelySubtagsData &data) :
LikelySubtags::LikelySubtags(LikelySubtagsData &data) :
langInfoBundle(data.langInfoBundle),
strings(data.strings.orphanCharStrings()),
languageAliases(std::move(data.languageAliases)),
Expand Down Expand Up @@ -507,13 +506,13 @@ XLikelySubtags::XLikelySubtags(XLikelySubtagsData &data) :
}
}

XLikelySubtags::~XLikelySubtags() {
LikelySubtags::~LikelySubtags() {
ures_close(langInfoBundle);
delete strings;
delete[] lsrs;
}

LSR XLikelySubtags::makeMaximizedLsrFrom(const Locale &locale,
LSR LikelySubtags::makeMaximizedLsrFrom(const Locale &locale,
bool returnInputIfUnmatch,
UErrorCode &errorCode) const {
if (locale.isBogus()) {
Expand Down Expand Up @@ -550,7 +549,7 @@ const char *getCanonical(const CharStringMap &aliases, const char *alias) {

} // namespace

LSR XLikelySubtags::makeMaximizedLsr(const char *language, const char *script, const char *region,
LSR LikelySubtags::makeMaximizedLsr(const char *language, const char *script, const char *region,
const char *variant,
bool returnInputIfUnmatch,
UErrorCode &errorCode) const {
Expand Down Expand Up @@ -605,7 +604,7 @@ LSR XLikelySubtags::makeMaximizedLsr(const char *language, const char *script, c
return maximize(language, script, region, returnInputIfUnmatch, errorCode);
}

LSR XLikelySubtags::maximize(const char *language, const char *script, const char *region,
LSR LikelySubtags::maximize(const char *language, const char *script, const char *region,
bool returnInputIfUnmatch,
UErrorCode &errorCode) const {
return maximize({language, (int32_t)uprv_strlen(language)},
Expand All @@ -615,18 +614,18 @@ LSR XLikelySubtags::maximize(const char *language, const char *script, const cha
errorCode);
}

bool XLikelySubtags::isMacroregion(StringPiece& region, UErrorCode& errorCode) const {
bool LikelySubtags::isMacroregion(StringPiece& region, UErrorCode& errorCode) const {
// In Java, we use Region class. In C++, since Region is under i18n,
// we read the same data used by Region into gMacroregions avoid dependency
// from common to i18n/region.cpp
if (U_FAILURE(errorCode)) { return false; }
umtx_initOnce(gInitOnce, &XLikelySubtags::initLikelySubtags, errorCode);
umtx_initOnce(gInitOnce, &LikelySubtags::initLikelySubtags, errorCode);
if (U_FAILURE(errorCode)) { return false; }
UnicodeString str(UnicodeString::fromUTF8(region));
return gMacroregions->contains((void *)&str);
}

LSR XLikelySubtags::maximize(StringPiece language, StringPiece script, StringPiece region,
LSR LikelySubtags::maximize(StringPiece language, StringPiece script, StringPiece region,
bool returnInputIfUnmatch,
UErrorCode &errorCode) const {
if (U_FAILURE(errorCode)) {
Expand Down Expand Up @@ -750,7 +749,7 @@ LSR XLikelySubtags::maximize(StringPiece language, StringPiece script, StringPie
return LSR(language, script, region, retainMask, errorCode);
}

int32_t XLikelySubtags::compareLikely(const LSR &lsr, const LSR &other, int32_t likelyInfo) const {
int32_t LikelySubtags::compareLikely(const LSR &lsr, const LSR &other, int32_t likelyInfo) const {
// If likelyInfo >= 0:
// likelyInfo bit 1 is set if the previous comparison with lsr
// was for equal language and script.
Expand Down Expand Up @@ -792,7 +791,7 @@ int32_t XLikelySubtags::compareLikely(const LSR &lsr, const LSR &other, int32_t
}

// Subset of maximize().
int32_t XLikelySubtags::getLikelyIndex(const char *language, const char *script) const {
int32_t LikelySubtags::getLikelyIndex(const char *language, const char *script) const {
if (uprv_strcmp(language, "und") == 0) {
language = "";
}
Expand Down Expand Up @@ -850,7 +849,7 @@ int32_t XLikelySubtags::getLikelyIndex(const char *language, const char *script)
return value;
}

int32_t XLikelySubtags::trieNext(BytesTrie &iter, const char *s, int32_t i) {
int32_t LikelySubtags::trieNext(BytesTrie &iter, const char *s, int32_t i) {
UStringTrieResult result;
uint8_t c;
if ((c = s[i]) == 0) {
Expand Down Expand Up @@ -883,7 +882,7 @@ int32_t XLikelySubtags::trieNext(BytesTrie &iter, const char *s, int32_t i) {
default: return -1;
}
}
int32_t XLikelySubtags::trieNext(BytesTrie &iter, StringPiece s, int32_t i) {
int32_t LikelySubtags::trieNext(BytesTrie &iter, StringPiece s, int32_t i) {
UStringTrieResult result;
uint8_t c;
if (s.length() == i) {
Expand Down Expand Up @@ -917,7 +916,7 @@ int32_t XLikelySubtags::trieNext(BytesTrie &iter, StringPiece s, int32_t i) {
}
}

LSR XLikelySubtags::minimizeSubtags(StringPiece language, StringPiece script,
LSR LikelySubtags::minimizeSubtags(StringPiece language, StringPiece script,
StringPiece region,
bool favorScript,
UErrorCode &errorCode) const {
Expand Down
17 changes: 8 additions & 9 deletions icu4c/source/common/loclikelysubtags.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

U_NAMESPACE_BEGIN

struct XLikelySubtagsData;
struct LikelySubtagsData;

struct LocaleDistanceData {
LocaleDistanceData() = default;
Expand All @@ -37,15 +37,14 @@ struct LocaleDistanceData {
LocaleDistanceData &operator=(const LocaleDistanceData &) = delete;
};

// TODO(ICU-20777): Rename to just LikelySubtags.
class XLikelySubtags final : public UMemory {
class LikelySubtags final : public UMemory {
public:
~XLikelySubtags();
~LikelySubtags();

static constexpr int32_t SKIP_SCRIPT = 1;

// VisibleForTesting
static const XLikelySubtags *getSingleton(UErrorCode &errorCode);
static const LikelySubtags *getSingleton(UErrorCode &errorCode);

// VisibleForTesting
LSR makeMaximizedLsrFrom(const Locale &locale,
Expand All @@ -72,9 +71,9 @@ class XLikelySubtags final : public UMemory {
const LocaleDistanceData &getDistanceData() const { return distanceData; }

private:
XLikelySubtags(XLikelySubtagsData &data);
XLikelySubtags(const XLikelySubtags &other) = delete;
XLikelySubtags &operator=(const XLikelySubtags &other) = delete;
LikelySubtags(LikelySubtagsData &data);
LikelySubtags(const LikelySubtags &other) = delete;
LikelySubtags &operator=(const LikelySubtags &other) = delete;

static void initLikelySubtags(UErrorCode &errorCode);

Expand Down Expand Up @@ -120,7 +119,7 @@ class XLikelySubtags final : public UMemory {
int32_t lsrsLength;
#endif

// distance/matcher data: see comment in XLikelySubtagsData::load()
// distance/matcher data: see comment in LikelySubtagsData::load()
LocaleDistanceData distanceData;
};

Expand Down
4 changes: 2 additions & 2 deletions icu4c/source/common/unicode/localematcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ U_NAMESPACE_BEGIN

struct LSR;

class LikelySubtags;
class LocaleDistance;
class LocaleLsrIterator;
class UVector;
class XLikelySubtags;

/**
* Immutable class that picks the best match between a user's desired locales and
Expand Down Expand Up @@ -680,7 +680,7 @@ class U_COMMON_API LocaleMatcher : public UMemory {

int32_t getBestSuppIndex(LSR desiredLSR, LocaleLsrIterator *remainingIter, UErrorCode &errorCode) const;

const XLikelySubtags &likelySubtags;
const LikelySubtags &likelySubtags;
const LocaleDistance &localeDistance;
int32_t thresholdDistance;
int32_t demotionPerDesiredLocale;
Expand Down
22 changes: 11 additions & 11 deletions icu4j/coverage-exclusion.txt
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ com/ibm/icu/impl/locale/LanguageTag#canonicalizeExtensionSubtag:(Ljava/lang/Stri
com/ibm/icu/impl/locale/LanguageTag#canonicalizeExtlang:(Ljava/lang/String;)Ljava/lang/String;
com/ibm/icu/impl/locale/LanguageTag#canonicalizePrivateuseSubtag:(Ljava/lang/String;)Ljava/lang/String;
com/ibm/icu/impl/locale/LanguageTag#toString:()Ljava/lang/String;
com/ibm/icu/impl/locale/LikelySubtags#getDefault:()Lcom/ibm/icu/impl/locale/LikelySubtags;
com/ibm/icu/impl/locale/LikelySubtags#maximize:(Lcom/ibm/icu/impl/locale/LikelySubtags$LSR;)Lcom/ibm/icu/impl/locale/LikelySubtags$LSR;
com/ibm/icu/impl/locale/LikelySubtags#maximize:(Lcom/ibm/icu/util/ULocale;)Lcom/ibm/icu/impl/locale/LikelySubtags$LSR;
com/ibm/icu/impl/locale/LikelySubtags#maximize:(Ljava/lang/String;)Lcom/ibm/icu/impl/locale/LikelySubtags$LSR;
com/ibm/icu/impl/locale/LikelySubtags#minimizeSubtags:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lcom/ibm/icu/util/ULocale$Minimize;)Lcom/ibm/icu/impl/locale/LikelySubtags$LSR;
com/ibm/icu/impl/locale/LikelySubtags#show:(Ljava/util/Map;Ljava/lang/String;Ljava/lang/StringBuilder;)Ljava/lang/StringBuilder;
com/ibm/icu/impl/locale/LikelySubtags#toString:()Ljava/lang/String;
com/ibm/icu/impl/locale/LikelySubtags$LSR#from:(Lcom/ibm/icu/util/ULocale;)Lcom/ibm/icu/impl/locale/LikelySubtags$LSR;
com/ibm/icu/impl/locale/LikelySubtags$LSR#from:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lcom/ibm/icu/impl/locale/LikelySubtags$LSR;
com/ibm/icu/impl/locale/LikelySubtags$LSR#toString:()Ljava/lang/String;
com/ibm/icu/impl/locale/LikelySubtags$Maker$1#make:()Ljava/util/Map;
com/ibm/icu/impl/locale/LocaleExtensions#equals:(Ljava/lang/Object;)Z
com/ibm/icu/impl/locale/LocaleExtensions#getID:()Ljava/lang/String;
com/ibm/icu/impl/locale/LocaleExtensions#hashCode:()I
Expand Down Expand Up @@ -307,17 +318,6 @@ com/ibm/icu/impl/locale/XCldrStub$RegexUtilities#<init>:()V
com/ibm/icu/impl/locale/XCldrStub$RegexUtilities#findMismatch:(Ljava/util/regex/Matcher;Ljava/lang/CharSequence;)I
com/ibm/icu/impl/locale/XCldrStub$RegexUtilities#showMismatch:(Ljava/util/regex/Matcher;Ljava/lang/CharSequence;)Ljava/lang/String;
com/ibm/icu/impl/locale/XCldrStub$ReusableEntry#setValue:(Ljava/lang/Object;)Ljava/lang/Object;
com/ibm/icu/impl/locale/XLikelySubtags#getDefault:()Lcom/ibm/icu/impl/locale/XLikelySubtags;
com/ibm/icu/impl/locale/XLikelySubtags#maximize:(Lcom/ibm/icu/impl/locale/XLikelySubtags$LSR;)Lcom/ibm/icu/impl/locale/XLikelySubtags$LSR;
com/ibm/icu/impl/locale/XLikelySubtags#maximize:(Lcom/ibm/icu/util/ULocale;)Lcom/ibm/icu/impl/locale/XLikelySubtags$LSR;
com/ibm/icu/impl/locale/XLikelySubtags#maximize:(Ljava/lang/String;)Lcom/ibm/icu/impl/locale/XLikelySubtags$LSR;
com/ibm/icu/impl/locale/XLikelySubtags#minimizeSubtags:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lcom/ibm/icu/util/ULocale$Minimize;)Lcom/ibm/icu/impl/locale/XLikelySubtags$LSR;
com/ibm/icu/impl/locale/XLikelySubtags#show:(Ljava/util/Map;Ljava/lang/String;Ljava/lang/StringBuilder;)Ljava/lang/StringBuilder;
com/ibm/icu/impl/locale/XLikelySubtags#toString:()Ljava/lang/String;
com/ibm/icu/impl/locale/XLikelySubtags$LSR#from:(Lcom/ibm/icu/util/ULocale;)Lcom/ibm/icu/impl/locale/XLikelySubtags$LSR;
com/ibm/icu/impl/locale/XLikelySubtags$LSR#from:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lcom/ibm/icu/impl/locale/XLikelySubtags$LSR;
com/ibm/icu/impl/locale/XLikelySubtags$LSR#toString:()Ljava/lang/String;
com/ibm/icu/impl/locale/XLikelySubtags$Maker$1#make:()Ljava/util/Map;
com/ibm/icu/impl/locale/XLocaleDistance#fixedName:(Ljava/util/List;)Ljava/lang/String;
com/ibm/icu/impl/locale/XLocaleDistance#getContainingMacrosFor:(Ljava/util/Collection;Ljava/util/Set;)Ljava/util/Set;
com/ibm/icu/impl/locale/XLocaleDistance#getDefaultLanguageDistance:()I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.ibm.icu.util.Region;
import com.ibm.icu.util.ULocale;

public final class XLikelySubtags {
public final class LikelySubtags {
private static final String PSEUDO_ACCENTS_PREFIX = "'"; // -XA, -PSACCENT
private static final String PSEUDO_BIDI_PREFIX = "+"; // -XB, -PSBIDI
private static final String PSEUDO_CRACKED_PREFIX = ","; // -XC, -PSCRACK
Expand Down Expand Up @@ -109,7 +109,7 @@ public boolean equals(Object other) {
}

// VisibleForTesting
public static final XLikelySubtags INSTANCE = new XLikelySubtags(Data.load());
public static final LikelySubtags INSTANCE = new LikelySubtags(Data.load());

private final Map<String, String> languageAliases;
private final Map<String, String> regionAliases;
Expand All @@ -124,7 +124,7 @@ public boolean equals(Object other) {
private final long[] trieFirstLetterStates = new long[26];
private final LSR[] lsrs;

private XLikelySubtags(XLikelySubtags.Data data) {
private LikelySubtags(LikelySubtags.Data data) {
languageAliases = data.languageAliases;
regionAliases = data.regionAliases;
trie = new BytesTrie(data.trie, 0);
Expand Down
Loading

0 comments on commit 8d3d214

Please sign in to comment.