Skip to content

Commit dd6c002

Browse files
committed
ICU-23192 Replace use of CharString with FixedString in MemoryPool.
1 parent 2d2e98d commit dd6c002

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

icu4c/source/common/uloc_keytype.cpp

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#include "unicode/unistr.h"
1515
#include "unicode/uobject.h"
1616

17-
#include "charstr.h"
1817
#include "cmemory.h"
1918
#include "cstring.h"
19+
#include "fixedstring.h"
2020
#include "uassert.h"
2121
#include "ucln_cmn.h"
2222
#include "uhash.h"
@@ -53,7 +53,7 @@ struct TypeAlias : public icu::UMemory {
5353
std::string_view from;
5454
};
5555

56-
static icu::MemoryPool<icu::CharString>* gKeyTypeStringPool = nullptr;
56+
static icu::MemoryPool<icu::FixedString>* gKeyTypeStringPool = nullptr;
5757
static icu::MemoryPool<LocExtKeyData>* gLocExtKeyDataEntries = nullptr;
5858
static icu::MemoryPool<LocExtType>* gLocExtTypeEntries = nullptr;
5959
static icu::MemoryPool<TypeAlias>* gTypeAliasEntries = nullptr;
@@ -108,7 +108,7 @@ initFromResourceBundle(UErrorCode& sts) {
108108
LocalUResourceBundlePointer bcpTypeAliasRes(ures_getByKey(keyTypeDataRes.getAlias(), "bcpTypeAlias", nullptr, &tmpSts));
109109

110110
// initialize pools storing dynamically allocated objects
111-
gKeyTypeStringPool = new icu::MemoryPool<icu::CharString>;
111+
gKeyTypeStringPool = new icu::MemoryPool<icu::FixedString>;
112112
if (gKeyTypeStringPool == nullptr) {
113113
sts = U_MEMORY_ALLOCATION_ERROR;
114114
return;
@@ -146,15 +146,14 @@ initFromResourceBundle(UErrorCode& sts) {
146146
// empty value indicates that BCP key is same with the legacy key.
147147
const char* bcpKeyId = legacyKeyId;
148148
if (!uBcpKeyId.isEmpty()) {
149-
icu::CharString* bcpKeyIdBuf = gKeyTypeStringPool->create();
150-
if (bcpKeyIdBuf == nullptr) {
149+
int32_t length = uBcpKeyId.length();
150+
icu::FixedString* bcpKeyIdBuf = gKeyTypeStringPool->create();
151+
if (bcpKeyIdBuf == nullptr || !bcpKeyIdBuf->reserve(length + 1)) {
151152
sts = U_MEMORY_ALLOCATION_ERROR;
152153
break;
153154
}
154-
bcpKeyIdBuf->appendInvariantChars(uBcpKeyId, sts);
155-
if (U_FAILURE(sts)) {
156-
break;
157-
}
155+
156+
uBcpKeyId.extract(0, length, bcpKeyIdBuf->getAlias(), length + 1, US_INV);
158157
bcpKeyId = bcpKeyIdBuf->data();
159158
}
160159

@@ -220,18 +219,16 @@ initFromResourceBundle(UErrorCode& sts) {
220219
// a timezone key uses a colon instead of a slash in the resource.
221220
// e.g. America:Los_Angeles
222221
if (uprv_strchr(legacyTypeId, ':') != nullptr) {
223-
icu::CharString* legacyTypeIdBuf =
224-
gKeyTypeStringPool->create(legacyTypeId, sts);
225-
if (legacyTypeIdBuf == nullptr) {
222+
U_ASSERT(legacyTypeId != nullptr && *legacyTypeId != '\0');
223+
std::string_view legacyTypeIdView = legacyTypeId;
224+
icu::FixedString* legacyTypeIdBuf = gKeyTypeStringPool->create(legacyTypeIdView);
225+
if (legacyTypeIdBuf == nullptr || legacyTypeIdBuf->isEmpty()) {
226226
sts = U_MEMORY_ALLOCATION_ERROR;
227227
break;
228228
}
229-
if (U_FAILURE(sts)) {
230-
break;
231-
}
232229
std::replace(
233-
legacyTypeIdBuf->data(),
234-
legacyTypeIdBuf->data() + legacyTypeIdBuf->length(),
230+
legacyTypeIdBuf->getAlias(),
231+
legacyTypeIdBuf->getAlias() + legacyTypeIdView.length(),
235232
':', '/');
236233
legacyTypeId = legacyTypeIdBuf->data();
237234
}
@@ -245,15 +242,13 @@ initFromResourceBundle(UErrorCode& sts) {
245242
// empty value indicates that BCP type is same with the legacy type.
246243
const char* bcpTypeId = legacyTypeId;
247244
if (!uBcpTypeId.isEmpty()) {
248-
icu::CharString* bcpTypeIdBuf = gKeyTypeStringPool->create();
249-
if (bcpTypeIdBuf == nullptr) {
245+
int32_t length = uBcpTypeId.length();
246+
icu::FixedString* bcpTypeIdBuf = gKeyTypeStringPool->create();
247+
if (bcpTypeIdBuf == nullptr || !bcpTypeIdBuf->reserve(length + 1)) {
250248
sts = U_MEMORY_ALLOCATION_ERROR;
251249
break;
252250
}
253-
bcpTypeIdBuf->appendInvariantChars(uBcpTypeId, sts);
254-
if (U_FAILURE(sts)) {
255-
break;
256-
}
251+
uBcpTypeId.extract(0, length, bcpTypeIdBuf->getAlias(), length + 1, US_INV);
257252
bcpTypeId = bcpTypeIdBuf->data();
258253
}
259254

@@ -302,20 +297,18 @@ initFromResourceBundle(UErrorCode& sts) {
302297
if (isTZ) {
303298
// replace colon with slash if necessary
304299
if (uprv_strchr(from, ':') != nullptr) {
305-
icu::CharString* fromBuf =
306-
gKeyTypeStringPool->create(from, sts);
307-
if (fromBuf == nullptr) {
300+
U_ASSERT(from != nullptr && *from != '\0');
301+
std::string_view fromView = from;
302+
icu::FixedString* fromBuf = gKeyTypeStringPool->create(fromView);
303+
if (fromBuf == nullptr || fromBuf->isEmpty()) {
308304
sts = U_MEMORY_ALLOCATION_ERROR;
309305
break;
310306
}
311-
if (U_FAILURE(sts)) {
312-
break;
313-
}
314307
std::replace(
315-
fromBuf->data(),
316-
fromBuf->data() + fromBuf->length(),
308+
fromBuf->getAlias(),
309+
fromBuf->getAlias() + fromView.length(),
317310
':', '/');
318-
alias->from = fromBuf->toStringPiece();
311+
alias->from = {fromBuf->data(), fromView.length()};
319312
}
320313
}
321314
uhash_put(typeDataMap, &alias->from, t, &sts);

0 commit comments

Comments
 (0)