ICU-21466 Remove size limit of intvector

See #1537
This commit is contained in:
Frank Tang 2021-01-23 02:17:30 +00:00 committed by Frank Yung-Fong Tang
parent 56b4630ada
commit b82dc88148
2 changed files with 16 additions and 3 deletions

View file

@ -340,7 +340,8 @@ IntResource::~IntResource() {}
IntVectorResource::IntVectorResource(SRBRoot *bundle, const char *tag,
const UString* comment, UErrorCode &errorCode)
: SResource(bundle, tag, URES_INT_VECTOR, comment, errorCode),
fCount(0), fArray(new uint32_t[RESLIST_MAX_INT_VECTOR]) {
fCount(0), fSize(RESLIST_INT_VECTOR_INIT_SIZE),
fArray(new uint32_t[fSize]) {
if (fArray == NULL) {
errorCode = U_MEMORY_ALLOCATION_ERROR;
return;
@ -352,6 +353,17 @@ IntVectorResource::~IntVectorResource() {
}
void IntVectorResource::add(int32_t value, UErrorCode &errorCode) {
if (fCount == fSize) {
uint32_t* tmp = new uint32_t[2 * fSize];
if (tmp == nullptr) {
errorCode = U_MEMORY_ALLOCATION_ERROR;
return;
}
uprv_memcpy(tmp, fArray, fSize * sizeof(uint32_t));
delete[] fArray;
fArray = tmp;
fSize *= 2;
}
if (U_SUCCESS(errorCode)) {
fArray[fCount++] = value;
}

View file

@ -21,7 +21,7 @@
#define RESLIST_H
#define KEY_SPACE_SIZE 65536
#define RESLIST_MAX_INT_VECTOR 2048
#define RESLIST_INT_VECTOR_INIT_SIZE 2048
#include <functional>
@ -405,7 +405,8 @@ public:
virtual void handleWrite(UNewDataMemory *mem, uint32_t *byteOffset);
// TODO: UVector32
uint32_t fCount;
size_t fCount;
size_t fSize;
uint32_t *fArray;
};