Skip to content

Commit c3d0d8b

Browse files
authored
[EGD-4203] UTF8 smart pointers refactor (#938)
1 parent 9684d5e commit c3d0d8b

File tree

2 files changed

+67
-97
lines changed

2 files changed

+67
-97
lines changed

module-utils/utf8/UTF8.cpp

Lines changed: 62 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -113,26 +113,22 @@ void U8char::set(char *val, unsigned int size)
113113
}
114114

115115
UTF8::UTF8()
116-
: data{new char[stringExpansion]}, sizeAllocated{stringExpansion}, sizeUsed{1}, strLength{0}, lastIndex{0},
117-
lastIndexData{data}
116+
: data{std::make_unique<char[]>(stringExpansion)},
117+
sizeAllocated{stringExpansion}, sizeUsed{1}, strLength{0}, lastIndex{0}, lastIndexData{data.get()}
118118
{
119-
if (data != nullptr) {
120-
memset(data, 0, sizeAllocated);
121-
}
122119
}
123120

124121
UTF8::UTF8(const char *str)
125122
{
126123
// bufferSize increased by 1 to ensure ending 0 in new string
127124
sizeUsed = strlen(str) + 1;
128125
sizeAllocated = getDataBufferSize(sizeUsed);
129-
data = new char[sizeAllocated];
126+
data = std::make_unique<char[]>(sizeAllocated);
130127
if (data != nullptr) {
131-
memset(data, 0, sizeAllocated);
132-
memcpy(data, str, sizeUsed);
133-
lastIndexData = data;
128+
memcpy(data.get(), str, sizeUsed);
129+
lastIndexData = data.get();
134130
}
135-
strLength = getCharactersCount(data);
131+
strLength = getCharactersCount(data.get());
136132
lastIndex = 0;
137133
}
138134

@@ -141,13 +137,12 @@ UTF8::UTF8(const std::string &str)
141137
// bufferSize increased by 1 to ensure ending 0 in new string
142138
sizeUsed = str.length() + 1;
143139
sizeAllocated = getDataBufferSize(sizeUsed);
144-
data = new char[sizeAllocated];
140+
data = std::make_unique<char[]>(sizeAllocated);
145141
if (data != nullptr) {
146-
memset(data, 0, sizeAllocated);
147-
memcpy(data, str.c_str(), sizeUsed);
148-
lastIndexData = data;
142+
memcpy(data.get(), str.c_str(), sizeUsed);
143+
lastIndexData = data.get();
149144
}
150-
strLength = getCharactersCount(data);
145+
strLength = getCharactersCount(data.get());
151146
lastIndex = 0;
152147
}
153148

@@ -159,67 +154,58 @@ UTF8::UTF8(const UTF8 &utf)
159154

160155
// if there is any data used in the string allocate memory and copy usedSize bytes
161156
if (strLength != 0) {
162-
data = new char[sizeAllocated];
157+
data = std::make_unique<char[]>(sizeAllocated);
163158
if (data == nullptr) {
164159
// LOG_FATAL("No memory for copy constructor.");
165160
sizeAllocated = 0;
166161
sizeUsed = 0;
167162
return;
168163
}
169-
memcpy(data, utf.data, sizeAllocated);
164+
memcpy(data.get(), utf.data.get(), sizeAllocated);
170165
}
171166
else {
172167
sizeAllocated = stringExpansion;
173-
data = new char[sizeAllocated];
174-
memset(data, 0, sizeAllocated);
168+
data = std::make_unique<char[]>(sizeAllocated);
175169
sizeUsed = 1;
176170
}
177171
lastIndex = 0;
178-
lastIndexData = data;
172+
lastIndexData = data.get();
179173
}
180174

181175
UTF8::UTF8(UTF8 &&utf)
182-
: data{utf.data}, sizeAllocated{utf.sizeAllocated}, sizeUsed{utf.sizeUsed}, strLength{utf.strLength}, lastIndex{0},
183-
lastIndexData{data}
176+
: data{std::move(utf.data)}, sizeAllocated{utf.sizeAllocated}, sizeUsed{utf.sizeUsed}, strLength{utf.strLength},
177+
lastIndex{0}, lastIndexData{data.get()}
184178
{
185-
utf.data = nullptr;
186179
}
187180

188181
UTF8::UTF8(const char *data, const uint32_t allocated, const uint32_t used, const uint32_t len)
189182
: sizeAllocated{allocated}, sizeUsed{used}, strLength{len}, lastIndex{0}
190183
{
191-
this->data = new char[allocated];
184+
this->data = std::make_unique<char[]>(allocated);
192185
if (this->data == nullptr) {
193186
sizeAllocated = 0;
194187
sizeUsed = 0;
195188
strLength = 0;
196189
return;
197190
}
198-
memcpy(this->data, data, allocated);
199-
lastIndexData = this->data;
200-
}
201-
202-
UTF8::~UTF8()
203-
{
204-
delete[] data;
191+
memcpy(this->data.get(), data, allocated);
192+
lastIndexData = this->data.get();
205193
}
206194

207195
bool UTF8::expand(uint32_t size)
208196
{
209197

210198
uint32_t newSizeAllocated = getDataBufferSize(sizeAllocated + size);
211-
auto *newData = new char[newSizeAllocated];
199+
auto newData = std::make_unique<char[]>(newSizeAllocated);
212200

213201
if (newData != nullptr) {
214202

215-
memcpy(newData, data, sizeUsed);
216-
memset(newData + sizeUsed, 0, newSizeAllocated - sizeUsed);
203+
memcpy(newData.get(), data.get(), sizeUsed);
217204

218-
delete[] data;
219-
data = newData;
205+
data = std::move(newData);
220206
sizeAllocated = newSizeAllocated;
221207
lastIndex = 0;
222-
lastIndexData = data;
208+
lastIndexData = data.get();
223209
return true;
224210
}
225211
return false;
@@ -264,13 +250,11 @@ UTF8 &UTF8::operator=(const UTF8 &utf)
264250
sizeUsed = utf.sizeUsed;
265251
strLength = utf.strLength;
266252

267-
delete[] data;
268-
269-
data = new char[sizeAllocated];
270-
memcpy(data, utf.data, sizeAllocated);
253+
data = std::make_unique<char[]>(sizeAllocated);
254+
memcpy(data.get(), utf.data.get(), sizeAllocated);
271255

272256
lastIndex = 0;
273-
lastIndexData = data;
257+
lastIndexData = data.get();
274258

275259
return *this;
276260
}
@@ -279,9 +263,7 @@ UTF8 &UTF8::operator=(UTF8 &&utf) noexcept
279263
{
280264
// prevent moving if object is moved to itself
281265
if (this != &utf) {
282-
delete[] data;
283-
data = utf.data;
284-
utf.data = nullptr;
266+
data = std::move(utf.data);
285267
sizeAllocated = utf.sizeAllocated;
286268
sizeUsed = utf.sizeUsed;
287269
strLength = utf.strLength;
@@ -304,7 +286,7 @@ uint32_t UTF8::operator[](const uint32_t &idx) const
304286
charCnt = lastIndex;
305287
}
306288
else {
307-
dataPtr = data;
289+
dataPtr = data.get();
308290
charCnt = 0;
309291
}
310292
assert(dataPtr);
@@ -322,7 +304,7 @@ uint32_t UTF8::operator[](const uint32_t &idx) const
322304

323305
U8char UTF8::getChar(unsigned int pos)
324306
{
325-
auto ptr = data;
307+
auto ptr = data.get();
326308
long int to = pos;
327309

328310
U8char u;
@@ -348,20 +330,19 @@ UTF8 &UTF8::operator+=(const UTF8 &utf)
348330
}
349331

350332
uint32_t newSizeAllocated = getDataBufferSize(sizeUsed + utf.sizeUsed);
351-
auto *newData = new char[newSizeAllocated];
333+
auto newData = std::make_unique<char[]>(newSizeAllocated);
352334
if (newData != nullptr) {
353335

354-
memcpy(newData, data, sizeUsed);
336+
memcpy(newData.get(), data.get(), sizeUsed);
355337
//-1 comes from the fact that null terminator is counted as a used byte in string's buffer.
356-
memcpy(newData + sizeUsed - 1, utf.data, utf.sizeUsed);
357-
delete[] data;
358-
data = newData;
338+
memcpy(newData.get() + sizeUsed - 1, utf.data.get(), utf.sizeUsed);
339+
data = std::move(newData);
359340
sizeAllocated = newSizeAllocated;
360341
strLength += utf.strLength;
361342
//-1 is to ignore double null terminator as it is counted in sizeUsed
362343
sizeUsed += utf.sizeUsed - 1;
363344
lastIndex = 0;
364-
lastIndexData = data;
345+
lastIndexData = data.get();
365346
}
366347
return *this;
367348
}
@@ -371,25 +352,24 @@ bool UTF8::operator==(const UTF8 &utf) const
371352
uint32_t len = strLength - utf.strLength;
372353
uint32_t used = sizeUsed - utf.sizeUsed;
373354
if ((len | used) == 0) {
374-
return memcmp(data, utf.data, sizeUsed) == 0;
355+
return memcmp(data.get(), utf.data.get(), sizeUsed) == 0;
375356
}
376357
return false;
377358
}
378359

379360
const char *UTF8::c_str() const
380361
{
381-
return data;
362+
return data.get();
382363
}
383364

384365
void UTF8::clear()
385366
{
386-
delete[] data;
387-
data = new char[stringExpansion];
367+
data = std::make_unique<char[]>(stringExpansion);
388368
sizeAllocated = stringExpansion;
389369
sizeUsed = 1;
390370
strLength = 0;
391371
lastIndex = 0;
392-
lastIndexData = data;
372+
lastIndexData = data.get();
393373
}
394374

395375
UTF8 UTF8::substr(const uint32_t begin, const uint32_t length) const
@@ -399,7 +379,7 @@ UTF8 UTF8::substr(const uint32_t begin, const uint32_t length) const
399379
return UTF8();
400380
}
401381

402-
char *beginPtr = this->data;
382+
char *beginPtr = this->data.get();
403383
char *endPtr = nullptr;
404384

405385
uint32_t bufferSize = 0;
@@ -419,12 +399,10 @@ UTF8 UTF8::substr(const uint32_t begin, const uint32_t length) const
419399
}
420400
// copy data to buffer
421401
// bufferSize increased by 1 to ensure ending 0 in new string
422-
auto *buffer = new char[bufferSize + 1];
423-
memset(buffer, 0, bufferSize + 1);
424-
memcpy(buffer, beginPtr, bufferSize);
402+
auto buffer = std::make_unique<char[]>(bufferSize + 1);
403+
memcpy(buffer.get(), beginPtr, bufferSize);
425404

426-
UTF8 retString = UTF8(buffer);
427-
delete[] buffer;
405+
UTF8 retString = UTF8(buffer.get());
428406

429407
return retString;
430408
}
@@ -446,7 +424,7 @@ uint32_t UTF8::find(const char *s, uint32_t pos)
446424
}
447425

448426
uint32_t position = 0;
449-
auto *dataPtr = this->data;
427+
auto *dataPtr = this->data.get();
450428

451429
for (position = 0; position < pos; position++) {
452430
dataPtr += charLength(dataPtr);
@@ -483,7 +461,7 @@ uint32_t UTF8::findLast(const char *s, uint32_t pos)
483461
}
484462

485463
uint32_t position = 0;
486-
auto *dataPtr = this->data;
464+
auto *dataPtr = this->data.get();
487465
uint32_t lastFoundPosition = npos;
488466

489467
// calculate position of last string to compare
@@ -505,7 +483,7 @@ UTF8 UTF8::split(const uint32_t &idx)
505483
return UTF8();
506484
}
507485

508-
auto *dataPtr = this->data;
486+
auto *dataPtr = this->data.get();
509487

510488
// move data pointer to split index
511489
for (uint32_t i = 0; i < idx; i++) {
@@ -516,26 +494,22 @@ UTF8 UTF8::split(const uint32_t &idx)
516494

517495
// re-create source string
518496
// create temp copy of string
519-
uint32_t tempStringSize = dataPtr - this->data;
497+
uint32_t tempStringSize = dataPtr - this->data.get();
520498
uint32_t tempStringBufferSize = getDataBufferSize(tempStringSize);
521-
auto *tempString = new char[tempStringBufferSize];
499+
auto tempString = std::make_unique<char[]>(tempStringBufferSize);
522500

523-
memset(tempString, 0, tempStringBufferSize);
524-
memcpy(tempString, this->data, tempStringSize);
501+
memcpy(tempString.get(), this->data.get(), tempStringSize);
525502

526503
// add 1 to ensure string terminating zero
527504
this->sizeUsed = tempStringSize + 1;
528505
this->sizeAllocated = tempStringBufferSize;
529506
this->strLength = idx;
530507

531508
// clear used memory
532-
memset(this->data, 0, this->sizeAllocated);
533-
delete[] this->data;
534-
535-
this->data = tempString;
509+
this->data = std::move(tempString);
536510

537511
this->lastIndex = 0;
538-
this->lastIndexData = this->data;
512+
this->lastIndexData = this->data.get();
539513

540514
return retString;
541515
}
@@ -563,7 +537,7 @@ bool UTF8::removeChar(const uint32_t &pos, const uint32_t &count)
563537
}
564538

565539
// get pointer to begin of string to remove
566-
auto *beginPtr = this->data;
540+
auto *beginPtr = this->data.get();
567541
for (uint32_t i = 0; i < pos; i++) {
568542
beginPtr += charLength(beginPtr);
569543
}
@@ -578,27 +552,22 @@ bool UTF8::removeChar(const uint32_t &pos, const uint32_t &count)
578552
uint32_t newStringSize = this->sizeUsed - bytesToRemove;
579553

580554
uint32_t tempStringBufferSize = getDataBufferSize(newStringSize);
581-
auto *tempString = new char[tempStringBufferSize];
555+
auto tempString = std::make_unique<char[]>(tempStringBufferSize);
582556
if (tempString == nullptr) {
583557
return false;
584558
}
585559

586560
// create new data buffer
587-
uint32_t copyOffset = beginPtr - this->data;
588-
memset(tempString, 0, tempStringBufferSize);
589-
memcpy(tempString, this->data, beginPtr - this->data);
590-
memcpy(tempString + copyOffset, endPtr, this->sizeUsed - bytesToRemove - copyOffset);
561+
uint32_t copyOffset = beginPtr - this->data.get();
562+
memcpy(tempString.get(), this->data.get(), beginPtr - this->data.get());
563+
memcpy(tempString.get() + copyOffset, endPtr, this->sizeUsed - bytesToRemove - copyOffset);
591564

592565
this->sizeAllocated = tempStringBufferSize;
593566
this->strLength -= count;
594567
this->sizeUsed -= bytesToRemove;
595568

596-
// delete old data buffer
597-
memset(this->data, 0, this->sizeAllocated);
598-
delete[] this->data;
599-
600569
// assign new data buffer
601-
this->data = tempString;
570+
this->data = std::move(tempString);
602571

603572
return true;
604573
}
@@ -686,17 +655,17 @@ bool UTF8::insert(const char *ch, const uint32_t &index)
686655
}
687656

688657
// find pointer where new character should be copied
689-
auto *pos = data;
658+
auto *pos = data.get();
690659
for (unsigned int i = 0; i < insertIndex; i++) {
691660
pos += charLength(pos);
692661
}
693662

694-
if ((pos - data) >= static_cast<int64_t>(sizeUsed)) {
663+
if ((pos - data.get()) >= static_cast<int64_t>(sizeUsed)) {
695664
debug_utf("decode/encode error %d - ( %d ) < 0 && allocated: %d\n", sizeUsed, pos - data, sizeAllocated);
696665
return false;
697666
}
698667
if (insertIndex != length()) {
699-
memmove(pos + ch_len, pos, sizeUsed - (pos - data)); // move data when insert is in text, not at the end
668+
memmove(pos + ch_len, pos, sizeUsed - (pos - data.get())); // move data when insert is in text, not at the end
700669
}
701670
memcpy(pos, ch, ch_len); // copy UTF8 char value
702671

@@ -734,14 +703,14 @@ bool UTF8::insertString(const UTF8 &str, const uint32_t &index)
734703
uint32_t totalSize = sizeUsed + str.sizeUsed - 1; //-1 because there are 2 end terminators
735704
expand(getDataBufferSize(totalSize));
736705

737-
auto *beginPtr = this->data;
706+
auto *beginPtr = this->data.get();
738707
for (uint32_t i = 0; i < insertIndex; i++) {
739708
beginPtr += charLength(beginPtr);
740709
}
741710

742711
//-1 to ignore end terminator from str
743-
memmove(beginPtr + str.sizeUsed - 1, beginPtr, sizeUsed - (beginPtr - data));
744-
memcpy(beginPtr, str.data, str.sizeUsed - 1);
712+
memmove(beginPtr + str.sizeUsed - 1, beginPtr, sizeUsed - (beginPtr - data.get()));
713+
memcpy(beginPtr, str.data.get(), str.sizeUsed - 1);
745714

746715
return false;
747716
}

0 commit comments

Comments
 (0)