Skip to content

Commit bfbea97

Browse files
committed
ICU-23005 Replace use of CharString with Locale to simplify the code.
This brings a lot of the code that uses LocaleBased back to how it was before the refactoring of ICU-23000, accessing the validLocale and actualLocale fields directly instead of through LocaleBased helper functions that now no longer are necessary. This also makes it possible to remove the U_LOCALE_BASED() macro and LocaleBased then ends up becoming a trivial class that now contains nothing more than two static helper functions.
1 parent f362ced commit bfbea97

File tree

12 files changed

+80
-222
lines changed

12 files changed

+80
-222
lines changed

icu4c/source/common/brkiter.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,9 @@ BreakIterator::buildInstance(const Locale& loc, const char *type, UErrorCode &st
121121

122122
// If there is a result, set the valid locale and actual locale, and the kind
123123
if (U_SUCCESS(status) && result != nullptr) {
124-
U_LOCALE_BASED(locBased, *(BreakIterator*)result);
125-
126-
locBased.setLocaleIDs(ures_getLocaleByType(b, ULOC_VALID_LOCALE, &status),
127-
actual.data(), status);
128-
LocaleBased::setLocaleID(loc.getName(), result->requestLocale, status);
124+
result->actualLocale = Locale(actual.data());
125+
result->validLocale = Locale(ures_getLocaleByType(b, ULOC_VALID_LOCALE, &status));
126+
result->requestLocale = loc;
129127
}
130128

131129
ures_close(b);
@@ -204,33 +202,28 @@ BreakIterator::getAvailableLocales(int32_t& count)
204202
//-------------------------------------------
205203

206204
BreakIterator::BreakIterator()
205+
: actualLocale(Locale::getRoot()), validLocale(Locale::getRoot()), requestLocale(Locale::getRoot())
207206
{
208207
}
209208

210-
BreakIterator::BreakIterator(const BreakIterator &other) : UObject(other) {
211-
UErrorCode status = U_ZERO_ERROR;
212-
U_LOCALE_BASED(locBased, *this);
213-
locBased.setLocaleIDs(other.validLocale, other.actualLocale, status);
214-
LocaleBased::setLocaleID(other.requestLocale, requestLocale, status);
215-
U_ASSERT(U_SUCCESS(status));
209+
BreakIterator::BreakIterator(const BreakIterator &other)
210+
: UObject(other),
211+
actualLocale(other.actualLocale),
212+
validLocale(other.validLocale),
213+
requestLocale(other.requestLocale) {
216214
}
217215

218216
BreakIterator &BreakIterator::operator =(const BreakIterator &other) {
219217
if (this != &other) {
220-
UErrorCode status = U_ZERO_ERROR;
221-
U_LOCALE_BASED(locBased, *this);
222-
locBased.setLocaleIDs(other.validLocale, other.actualLocale, status);
223-
LocaleBased::setLocaleID(other.requestLocale, requestLocale, status);
224-
U_ASSERT(U_SUCCESS(status));
218+
actualLocale = other.actualLocale;
219+
validLocale = other.validLocale;
220+
requestLocale = other.requestLocale;
225221
}
226222
return *this;
227223
}
228224

229225
BreakIterator::~BreakIterator()
230226
{
231-
delete validLocale;
232-
delete actualLocale;
233-
delete requestLocale;
234227
}
235228

236229
// ------------------------------------------
@@ -398,8 +391,8 @@ BreakIterator::createInstance(const Locale& loc, int32_t kind, UErrorCode& statu
398391
// THIS LONG is a sign of bad code -- so the action item is to
399392
// revisit this in ICU 3.0 and clean it up/fix it/remove it.
400393
if (U_SUCCESS(status) && (result != nullptr) && *actualLoc.getName() != 0) {
401-
U_LOCALE_BASED(locBased, *result);
402-
locBased.setLocaleIDs(actualLoc.getName(), actualLoc.getName(), status);
394+
result->actualLocale = actualLoc;
395+
result->validLocale = actualLoc;
403396
}
404397
return result;
405398
}
@@ -506,8 +499,7 @@ BreakIterator::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
506499
return Locale::getRoot();
507500
}
508501
if (type == ULOC_REQUESTED_LOCALE) {
509-
return requestLocale == nullptr ?
510-
Locale::getRoot() : Locale(requestLocale->data());
502+
return requestLocale;
511503
}
512504
return LocaleBased::getLocale(validLocale, actualLocale, type, status);
513505
}
@@ -518,7 +510,7 @@ BreakIterator::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
518510
return nullptr;
519511
}
520512
if (type == ULOC_REQUESTED_LOCALE) {
521-
return requestLocale == nullptr ? "" : requestLocale->data();
513+
return requestLocale.getName();
522514
}
523515
return LocaleBased::getLocaleID(validLocale, actualLocale, type, status);
524516
}
@@ -546,11 +538,8 @@ int32_t BreakIterator::getRuleStatusVec(int32_t *fillInVec, int32_t capacity, UE
546538
return 1;
547539
}
548540

549-
BreakIterator::BreakIterator (const Locale& valid, const Locale& actual) {
550-
UErrorCode status = U_ZERO_ERROR;
551-
U_LOCALE_BASED(locBased, (*this));
552-
locBased.setLocaleIDs(valid.getName(), actual.getName(), status);
553-
U_ASSERT(U_SUCCESS(status));
541+
BreakIterator::BreakIterator(const Locale& valid, const Locale& actual)
542+
: actualLocale(actual), validLocale(valid), requestLocale(Locale::getRoot()) {
554543
}
555544

556545
U_NAMESPACE_END

icu4c/source/common/locbased.cpp

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,85 +11,36 @@
1111
**********************************************************************
1212
*/
1313
#include "locbased.h"
14-
#include "cstring.h"
15-
#include "charstr.h"
14+
#include "uresimp.h"
1615

1716
U_NAMESPACE_BEGIN
1817

19-
Locale LocaleBased::getLocale(const CharString* valid, const CharString* actual,
20-
ULocDataLocaleType type, UErrorCode& status) {
21-
const char* id = getLocaleID(valid, actual, type, status);
22-
return Locale(id != nullptr ? id : "");
23-
}
24-
25-
const char* LocaleBased::getLocaleID(const CharString* valid, const CharString* actual,
18+
const Locale& LocaleBased::getLocale(const Locale& valid, const Locale& actual,
2619
ULocDataLocaleType type, UErrorCode& status) {
2720
if (U_FAILURE(status)) {
28-
return nullptr;
21+
return Locale::getRoot();
2922
}
3023

3124
switch(type) {
3225
case ULOC_VALID_LOCALE:
33-
return valid == nullptr ? "" : valid->data();
26+
return valid;
3427
case ULOC_ACTUAL_LOCALE:
35-
return actual == nullptr ? "" : actual->data();
28+
return actual;
3629
default:
3730
status = U_ILLEGAL_ARGUMENT_ERROR;
38-
return nullptr;
31+
return Locale::getRoot();
3932
}
4033
}
4134

42-
void LocaleBased::setLocaleIDs(const CharString* validID, const CharString* actualID, UErrorCode& status) {
43-
setValidLocaleID(validID, status);
44-
setActualLocaleID(actualID,status);
45-
}
46-
void LocaleBased::setLocaleIDs(const char* validID, const char* actualID, UErrorCode& status) {
47-
setValidLocaleID(validID, status);
48-
setActualLocaleID(actualID,status);
49-
}
50-
51-
void LocaleBased::setLocaleID(const char* id, CharString*& dest, UErrorCode& status) {
52-
if (U_FAILURE(status)) { return; }
53-
if (id == nullptr || *id == 0) {
54-
delete dest;
55-
dest = nullptr;
56-
} else {
57-
if (dest == nullptr) {
58-
dest = new CharString(id, status);
59-
if (dest == nullptr) {
60-
status = U_MEMORY_ALLOCATION_ERROR;
61-
return;
62-
}
63-
} else {
64-
dest->copyFrom(id, status);
65-
}
66-
}
67-
}
35+
const char* LocaleBased::getLocaleID(const Locale& valid, const Locale& actual,
36+
ULocDataLocaleType type, UErrorCode& status) {
37+
const Locale& locale = getLocale(valid, actual, type, status);
6838

69-
void LocaleBased::setLocaleID(const CharString* id, CharString*& dest, UErrorCode& status) {
70-
if (U_FAILURE(status)) { return; }
71-
if (id == nullptr || id->isEmpty()) {
72-
delete dest;
73-
dest = nullptr;
74-
} else {
75-
if (dest == nullptr) {
76-
dest = new CharString(*id, status);
77-
if (dest == nullptr) {
78-
status = U_MEMORY_ALLOCATION_ERROR;
79-
return;
80-
}
81-
} else {
82-
dest->copyFrom(*id, status);
83-
}
39+
if (U_FAILURE(status)) {
40+
return nullptr;
8441
}
85-
}
8642

87-
bool LocaleBased::equalIDs(const CharString* left, const CharString* right) {
88-
// true if both are nullptr
89-
if (left == nullptr && right == nullptr) return true;
90-
// false if only one is nullptr
91-
if (left == nullptr || right == nullptr) return false;
92-
return *left == *right;
43+
return locale == Locale::getRoot() ? kRootLocaleName : locale.getName();
9344
}
9445

9546
U_NAMESPACE_END

icu4c/source/common/locbased.h

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,8 @@
1616
#include "unicode/locid.h"
1717
#include "unicode/uobject.h"
1818

19-
/**
20-
* Macro to declare a locale LocaleBased wrapper object for the given
21-
* object, which must have two members named `validLocale' and
22-
* `actualLocale' of which are pointers to the internal icu::CharString.
23-
*/
24-
#define U_LOCALE_BASED(varname, objname) \
25-
LocaleBased varname((objname).validLocale, (objname).actualLocale)
26-
2719
U_NAMESPACE_BEGIN
2820

29-
class CharString;
3021
/**
3122
* A utility class that unifies the implementation of getLocale() by
3223
* various ICU services. This class is likely to be removed in the
@@ -38,12 +29,6 @@ class U_COMMON_API LocaleBased : public UMemory {
3829

3930
public:
4031

41-
/**
42-
* Construct a LocaleBased wrapper around the two pointers. These
43-
* will be aliased for the lifetime of this object.
44-
*/
45-
inline LocaleBased(CharString*& validAlias, CharString*& actualAlias);
46-
4732
/**
4833
* Return locale meta-data for the service object wrapped by this
4934
* object. Either the valid or the actual locale may be
@@ -54,8 +39,8 @@ class U_COMMON_API LocaleBased : public UMemory {
5439
* @param status input-output error code
5540
* @return the indicated locale
5641
*/
57-
static Locale getLocale(
58-
const CharString* valid, const CharString* actual,
42+
static const Locale& getLocale(
43+
const Locale& valid, const Locale& actual,
5944
ULocDataLocaleType type, UErrorCode& status);
6045

6146
/**
@@ -69,51 +54,11 @@ class U_COMMON_API LocaleBased : public UMemory {
6954
* @return the indicated locale ID
7055
*/
7156
static const char* getLocaleID(
72-
const CharString* valid, const CharString* actual,
57+
const Locale& valid, const Locale& actual,
7358
ULocDataLocaleType type, UErrorCode& status);
7459

75-
/**
76-
* Set the locale meta-data for the service object wrapped by this
77-
* object. If either parameter is zero, it is ignored.
78-
* @param valid the ID of the valid locale
79-
* @param actual the ID of the actual locale
80-
*/
81-
void setLocaleIDs(const char* valid, const char* actual, UErrorCode& status);
82-
void setLocaleIDs(const CharString* valid, const CharString* actual, UErrorCode& status);
83-
84-
static void setLocaleID(const char* id, CharString*& dest, UErrorCode& status);
85-
static void setLocaleID(const CharString* id, CharString*& dest, UErrorCode& status);
86-
87-
static bool equalIDs(const CharString* left, const CharString* right);
88-
89-
private:
90-
91-
void setValidLocaleID(const CharString* id, UErrorCode& status);
92-
void setActualLocaleID(const CharString* id, UErrorCode& status);
93-
void setValidLocaleID(const char* id, UErrorCode& status);
94-
void setActualLocaleID(const char* id, UErrorCode& status);
95-
96-
CharString*& valid;
97-
CharString*& actual;
9860
};
9961

100-
inline LocaleBased::LocaleBased(CharString*& validAlias, CharString*& actualAlias) :
101-
valid(validAlias), actual(actualAlias) {
102-
}
103-
104-
inline void LocaleBased::setValidLocaleID(const CharString* id, UErrorCode& status) {
105-
setLocaleID(id, valid, status);
106-
}
107-
inline void LocaleBased::setActualLocaleID(const CharString* id, UErrorCode& status) {
108-
setLocaleID(id, actual, status);
109-
}
110-
inline void LocaleBased::setValidLocaleID(const char* id, UErrorCode& status) {
111-
setLocaleID(id, valid, status);
112-
}
113-
inline void LocaleBased::setActualLocaleID(const char* id, UErrorCode& status) {
114-
setLocaleID(id, actual, status);
115-
}
116-
11762
U_NAMESPACE_END
11863

11964
#endif

icu4c/source/common/unicode/brkiter.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ U_NAMESPACE_END
5858

5959
U_NAMESPACE_BEGIN
6060

61-
class CharString;
62-
6361
/**
6462
* The BreakIterator class implements methods for finding the location
6563
* of boundaries in text. BreakIterator is an abstract base class.
@@ -650,9 +648,9 @@ class U_COMMON_API_CLASS BreakIterator : public UObject {
650648
private:
651649

652650
/** @internal (private) */
653-
CharString* actualLocale = nullptr;
654-
CharString* validLocale = nullptr;
655-
CharString* requestLocale = nullptr;
651+
Locale actualLocale;
652+
Locale validLocale;
653+
Locale requestLocale;
656654
};
657655

658656
#ifndef U_HIDE_DEPRECATED_API

icu4c/source/i18n/calendar.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,9 @@ fAreAllFieldsSet(false),
702702
fAreFieldsVirtuallySet(false),
703703
fLenient(true),
704704
fRepeatedWallTime(UCAL_WALLTIME_LAST),
705-
fSkippedWallTime(UCAL_WALLTIME_LAST)
705+
fSkippedWallTime(UCAL_WALLTIME_LAST),
706+
validLocale(Locale::getRoot()),
707+
actualLocale(Locale::getRoot())
706708
{
707709
clear();
708710
if (U_FAILURE(success)) {
@@ -725,7 +727,9 @@ fAreAllFieldsSet(false),
725727
fAreFieldsVirtuallySet(false),
726728
fLenient(true),
727729
fRepeatedWallTime(UCAL_WALLTIME_LAST),
728-
fSkippedWallTime(UCAL_WALLTIME_LAST)
730+
fSkippedWallTime(UCAL_WALLTIME_LAST),
731+
validLocale(Locale::getRoot()),
732+
actualLocale(Locale::getRoot())
729733
{
730734
LocalPointer<TimeZone> zone(adoptZone, success);
731735
if (U_FAILURE(success)) {
@@ -755,7 +759,9 @@ fAreAllFieldsSet(false),
755759
fAreFieldsVirtuallySet(false),
756760
fLenient(true),
757761
fRepeatedWallTime(UCAL_WALLTIME_LAST),
758-
fSkippedWallTime(UCAL_WALLTIME_LAST)
762+
fSkippedWallTime(UCAL_WALLTIME_LAST),
763+
validLocale(Locale::getRoot()),
764+
actualLocale(Locale::getRoot())
759765
{
760766
if (U_FAILURE(success)) {
761767
return;
@@ -774,8 +780,6 @@ fSkippedWallTime(UCAL_WALLTIME_LAST)
774780
Calendar::~Calendar()
775781
{
776782
delete fZone;
777-
delete actualLocale;
778-
delete validLocale;
779783
}
780784

781785
// -------------------------------------
@@ -814,10 +818,8 @@ Calendar::operator=(const Calendar &right)
814818
fWeekendCease = right.fWeekendCease;
815819
fWeekendCeaseMillis = right.fWeekendCeaseMillis;
816820
fNextStamp = right.fNextStamp;
817-
UErrorCode status = U_ZERO_ERROR;
818-
U_LOCALE_BASED(locBased, *this);
819-
locBased.setLocaleIDs(right.validLocale, right.actualLocale, status);
820-
U_ASSERT(U_SUCCESS(status));
821+
validLocale = right.validLocale;
822+
actualLocale = right.actualLocale;
821823
}
822824

823825
return *this;
@@ -4135,9 +4137,8 @@ Calendar::setWeekData(const Locale& desiredLocale, const char *type, UErrorCode&
41354137
}
41364138

41374139
if (U_SUCCESS(status)) {
4138-
U_LOCALE_BASED(locBased,*this);
4139-
locBased.setLocaleIDs(ures_getLocaleByType(monthNames.getAlias(), ULOC_VALID_LOCALE, &status),
4140-
ures_getLocaleByType(monthNames.getAlias(), ULOC_ACTUAL_LOCALE, &status), status);
4140+
validLocale = Locale(ures_getLocaleByType(monthNames.getAlias(), ULOC_VALID_LOCALE, &status));
4141+
actualLocale = Locale(ures_getLocaleByType(monthNames.getAlias(), ULOC_ACTUAL_LOCALE, &status));
41414142
} else {
41424143
status = U_USING_FALLBACK_WARNING;
41434144
return;

0 commit comments

Comments
 (0)