Skip to content

Commit

Permalink
ICU-22520 Make icu::CharString comparable with itself.
Browse files Browse the repository at this point in the history
  • Loading branch information
roubert committed Jan 10, 2024
1 parent 87ca023 commit 906093f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
7 changes: 7 additions & 0 deletions icu4c/source/common/charstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ class U_COMMON_API CharString : public UMemory {
*/
int32_t extract(char *dest, int32_t capacity, UErrorCode &errorCode) const;

bool operator==(const CharString& other) const {
return len == other.length() && (len == 0 || uprv_memcmp(data(), other.data(), len) == 0);
}
bool operator!=(const CharString& other) const {
return !operator==(other);
}

bool operator==(StringPiece other) const {
return len == other.length() && (len == 0 || uprv_memcmp(data(), other.data(), len) == 0);
}
Expand Down
10 changes: 5 additions & 5 deletions icu4c/source/common/uresbund.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ static bool getParentLocaleID(char *name, const char *origName, UResOpenType ope
// if "name" has both script and region, is the script the default script?
// - if so, remove it and keep the region
// - if not, remove the region and keep the script
if (getDefaultScript(language, region) == script.toStringPiece()) {
if (getDefaultScript(language, region) == script) {
workingLocale.append(language, err).append("_", err).append(region, err);
} else {
workingLocale.append(language, err).append("_", err).append(script, err);
Expand Down Expand Up @@ -272,7 +272,7 @@ static bool getParentLocaleID(char *name, const char *origName, UResOpenType ope
// - if not, return false to continue up the chain
// (we don't do this for other open types for the same reason we don't look things up in the parent
// locale table for other open types-- see the reference to UTS #35 above)
if (openType != URES_OPEN_LOCALE_DEFAULT_ROOT || getDefaultScript(language, CharString()) == script.toStringPiece()) {
if (openType != URES_OPEN_LOCALE_DEFAULT_ROOT || getDefaultScript(language, CharString()) == script) {
workingLocale.append(language, err);
} else {
return false;
Expand Down Expand Up @@ -3168,7 +3168,7 @@ ures_getFunctionalEquivalent(char *result, int32_t resultCapacity,
found.clear().append(ures_getLocaleByType(res, ULOC_VALID_LOCALE, &subStatus), subStatus);
}

if (found != parent.toStringPiece()) {
if (found != parent) {
parent.copyFrom(found, subStatus);
} else {
getParentForFunctionalEquivalent(found.data(),res,&bund1,parent);
Expand Down Expand Up @@ -3277,7 +3277,7 @@ ures_getFunctionalEquivalent(char *result, int32_t resultCapacity,
subStatus = U_ZERO_ERROR;
} while(full.isEmpty() && !found.isEmpty() && U_SUCCESS(*status));

if(full.isEmpty() && kwVal != defVal.toStringPiece()) {
if(full.isEmpty() && kwVal != defVal) {
#if defined(URES_TREE_DEBUG)
fprintf(stderr, "Failed to locate kw %s - try default %s\n", kwVal.data(), defVal.data());
#endif
Expand Down Expand Up @@ -3361,7 +3361,7 @@ ures_getFunctionalEquivalent(char *result, int32_t resultCapacity,
#endif
if(defLoc.length() <= full.length()) {
/* found the keyword in a *child* of where the default tag was present. */
if(kwVal == defVal.toStringPiece()) { /* if the requested kw is default, */
if(kwVal == defVal) { /* if the requested kw is default, */
/* and the default is in or in an ancestor of the current locale */
#if defined(URES_TREE_DEBUG)
fprintf(stderr, "Removing unneeded var %s=%s\n", keyword, kwVal.data());
Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/i18n/units_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ void U_I18N_API getAllConversionRates(MaybeStackVector<ConversionRateInfo> &resu
const ConversionRateInfo *ConversionRates::extractConversionInfo(StringPiece source,
UErrorCode &status) const {
for (size_t i = 0, n = conversionInfo_.length(); i < n; ++i) {
if (conversionInfo_[i]->sourceUnit.toStringPiece() == source) return conversionInfo_[i];
if (conversionInfo_[i]->sourceUnit == source) return conversionInfo_[i];
}

status = U_INTERNAL_PROGRAM_ERROR;
Expand Down

0 comments on commit 906093f

Please sign in to comment.