14
14
#include " unicode/unistr.h"
15
15
#include " unicode/uobject.h"
16
16
17
- #include " charstr.h"
18
17
#include " cmemory.h"
19
18
#include " cstring.h"
19
+ #include " fixedstring.h"
20
20
#include " uassert.h"
21
21
#include " ucln_cmn.h"
22
22
#include " uhash.h"
@@ -53,7 +53,7 @@ struct TypeAlias : public icu::UMemory {
53
53
std::string_view from;
54
54
};
55
55
56
- static icu::MemoryPool<icu::CharString >* gKeyTypeStringPool = nullptr ;
56
+ static icu::MemoryPool<icu::FixedString >* gKeyTypeStringPool = nullptr ;
57
57
static icu::MemoryPool<LocExtKeyData>* gLocExtKeyDataEntries = nullptr ;
58
58
static icu::MemoryPool<LocExtType>* gLocExtTypeEntries = nullptr ;
59
59
static icu::MemoryPool<TypeAlias>* gTypeAliasEntries = nullptr ;
@@ -108,7 +108,7 @@ initFromResourceBundle(UErrorCode& sts) {
108
108
LocalUResourceBundlePointer bcpTypeAliasRes (ures_getByKey (keyTypeDataRes.getAlias (), " bcpTypeAlias" , nullptr , &tmpSts));
109
109
110
110
// initialize pools storing dynamically allocated objects
111
- gKeyTypeStringPool = new icu::MemoryPool<icu::CharString >;
111
+ gKeyTypeStringPool = new icu::MemoryPool<icu::FixedString >;
112
112
if (gKeyTypeStringPool == nullptr ) {
113
113
sts = U_MEMORY_ALLOCATION_ERROR;
114
114
return ;
@@ -146,15 +146,14 @@ initFromResourceBundle(UErrorCode& sts) {
146
146
// empty value indicates that BCP key is same with the legacy key.
147
147
const char * bcpKeyId = legacyKeyId;
148
148
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 )) {
151
152
sts = U_MEMORY_ALLOCATION_ERROR;
152
153
break ;
153
154
}
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);
158
157
bcpKeyId = bcpKeyIdBuf->data ();
159
158
}
160
159
@@ -220,18 +219,16 @@ initFromResourceBundle(UErrorCode& sts) {
220
219
// a timezone key uses a colon instead of a slash in the resource.
221
220
// e.g. America:Los_Angeles
222
221
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 ()) {
226
226
sts = U_MEMORY_ALLOCATION_ERROR;
227
227
break ;
228
228
}
229
- if (U_FAILURE (sts)) {
230
- break ;
231
- }
232
229
std::replace (
233
- legacyTypeIdBuf->data (),
234
- legacyTypeIdBuf->data () + legacyTypeIdBuf-> length (),
230
+ legacyTypeIdBuf->getAlias (),
231
+ legacyTypeIdBuf->getAlias () + legacyTypeIdView. length (),
235
232
' :' , ' /' );
236
233
legacyTypeId = legacyTypeIdBuf->data ();
237
234
}
@@ -245,15 +242,13 @@ initFromResourceBundle(UErrorCode& sts) {
245
242
// empty value indicates that BCP type is same with the legacy type.
246
243
const char * bcpTypeId = legacyTypeId;
247
244
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 )) {
250
248
sts = U_MEMORY_ALLOCATION_ERROR;
251
249
break ;
252
250
}
253
- bcpTypeIdBuf->appendInvariantChars (uBcpTypeId, sts);
254
- if (U_FAILURE (sts)) {
255
- break ;
256
- }
251
+ uBcpTypeId.extract (0 , length, bcpTypeIdBuf->getAlias (), length + 1 , US_INV);
257
252
bcpTypeId = bcpTypeIdBuf->data ();
258
253
}
259
254
@@ -302,20 +297,18 @@ initFromResourceBundle(UErrorCode& sts) {
302
297
if (isTZ) {
303
298
// replace colon with slash if necessary
304
299
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 ()) {
308
304
sts = U_MEMORY_ALLOCATION_ERROR;
309
305
break ;
310
306
}
311
- if (U_FAILURE (sts)) {
312
- break ;
313
- }
314
307
std::replace (
315
- fromBuf->data (),
316
- fromBuf->data () + fromBuf-> length (),
308
+ fromBuf->getAlias (),
309
+ fromBuf->getAlias () + fromView. length (),
317
310
' :' , ' /' );
318
- alias->from = fromBuf->toStringPiece () ;
311
+ alias->from = { fromBuf->data (), fromView. length ()} ;
319
312
}
320
313
}
321
314
uhash_put (typeDataMap, &alias->from , t, &sts);
0 commit comments