@@ -113,26 +113,22 @@ void U8char::set(char *val, unsigned int size)
113
113
}
114
114
115
115
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 () }
118
118
{
119
- if (data != nullptr ) {
120
- memset (data, 0 , sizeAllocated);
121
- }
122
119
}
123
120
124
121
UTF8::UTF8 (const char *str)
125
122
{
126
123
// bufferSize increased by 1 to ensure ending 0 in new string
127
124
sizeUsed = strlen (str) + 1 ;
128
125
sizeAllocated = getDataBufferSize (sizeUsed);
129
- data = new char [sizeAllocated] ;
126
+ data = std::make_unique< char []>(sizeAllocated) ;
130
127
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 ();
134
130
}
135
- strLength = getCharactersCount (data);
131
+ strLength = getCharactersCount (data. get () );
136
132
lastIndex = 0 ;
137
133
}
138
134
@@ -141,13 +137,12 @@ UTF8::UTF8(const std::string &str)
141
137
// bufferSize increased by 1 to ensure ending 0 in new string
142
138
sizeUsed = str.length () + 1 ;
143
139
sizeAllocated = getDataBufferSize (sizeUsed);
144
- data = new char [sizeAllocated] ;
140
+ data = std::make_unique< char []>(sizeAllocated) ;
145
141
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 ();
149
144
}
150
- strLength = getCharactersCount (data);
145
+ strLength = getCharactersCount (data. get () );
151
146
lastIndex = 0 ;
152
147
}
153
148
@@ -159,67 +154,58 @@ UTF8::UTF8(const UTF8 &utf)
159
154
160
155
// if there is any data used in the string allocate memory and copy usedSize bytes
161
156
if (strLength != 0 ) {
162
- data = new char [sizeAllocated] ;
157
+ data = std::make_unique< char []>(sizeAllocated) ;
163
158
if (data == nullptr ) {
164
159
// LOG_FATAL("No memory for copy constructor.");
165
160
sizeAllocated = 0 ;
166
161
sizeUsed = 0 ;
167
162
return ;
168
163
}
169
- memcpy (data, utf.data , sizeAllocated);
164
+ memcpy (data. get () , utf.data . get () , sizeAllocated);
170
165
}
171
166
else {
172
167
sizeAllocated = stringExpansion;
173
- data = new char [sizeAllocated];
174
- memset (data, 0 , sizeAllocated);
168
+ data = std::make_unique<char []>(sizeAllocated);
175
169
sizeUsed = 1 ;
176
170
}
177
171
lastIndex = 0 ;
178
- lastIndexData = data;
172
+ lastIndexData = data. get () ;
179
173
}
180
174
181
175
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 () }
184
178
{
185
- utf.data = nullptr ;
186
179
}
187
180
188
181
UTF8::UTF8 (const char *data, const uint32_t allocated, const uint32_t used, const uint32_t len)
189
182
: sizeAllocated{allocated}, sizeUsed{used}, strLength{len}, lastIndex{0 }
190
183
{
191
- this ->data = new char [allocated] ;
184
+ this ->data = std::make_unique< char []>(allocated) ;
192
185
if (this ->data == nullptr ) {
193
186
sizeAllocated = 0 ;
194
187
sizeUsed = 0 ;
195
188
strLength = 0 ;
196
189
return ;
197
190
}
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 ();
205
193
}
206
194
207
195
bool UTF8::expand (uint32_t size)
208
196
{
209
197
210
198
uint32_t newSizeAllocated = getDataBufferSize (sizeAllocated + size);
211
- auto * newData = new char [newSizeAllocated] ;
199
+ auto newData = std::make_unique< char []>(newSizeAllocated) ;
212
200
213
201
if (newData != nullptr ) {
214
202
215
- memcpy (newData, data, sizeUsed);
216
- memset (newData + sizeUsed, 0 , newSizeAllocated - sizeUsed);
203
+ memcpy (newData.get (), data.get (), sizeUsed);
217
204
218
- delete[] data;
219
- data = newData;
205
+ data = std::move (newData);
220
206
sizeAllocated = newSizeAllocated;
221
207
lastIndex = 0 ;
222
- lastIndexData = data;
208
+ lastIndexData = data. get () ;
223
209
return true ;
224
210
}
225
211
return false ;
@@ -264,13 +250,11 @@ UTF8 &UTF8::operator=(const UTF8 &utf)
264
250
sizeUsed = utf.sizeUsed ;
265
251
strLength = utf.strLength ;
266
252
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);
271
255
272
256
lastIndex = 0 ;
273
- lastIndexData = data;
257
+ lastIndexData = data. get () ;
274
258
275
259
return *this ;
276
260
}
@@ -279,9 +263,7 @@ UTF8 &UTF8::operator=(UTF8 &&utf) noexcept
279
263
{
280
264
// prevent moving if object is moved to itself
281
265
if (this != &utf) {
282
- delete[] data;
283
- data = utf.data ;
284
- utf.data = nullptr ;
266
+ data = std::move (utf.data );
285
267
sizeAllocated = utf.sizeAllocated ;
286
268
sizeUsed = utf.sizeUsed ;
287
269
strLength = utf.strLength ;
@@ -304,7 +286,7 @@ uint32_t UTF8::operator[](const uint32_t &idx) const
304
286
charCnt = lastIndex;
305
287
}
306
288
else {
307
- dataPtr = data;
289
+ dataPtr = data. get () ;
308
290
charCnt = 0 ;
309
291
}
310
292
assert (dataPtr);
@@ -322,7 +304,7 @@ uint32_t UTF8::operator[](const uint32_t &idx) const
322
304
323
305
U8char UTF8::getChar (unsigned int pos)
324
306
{
325
- auto ptr = data;
307
+ auto ptr = data. get () ;
326
308
long int to = pos;
327
309
328
310
U8char u;
@@ -348,20 +330,19 @@ UTF8 &UTF8::operator+=(const UTF8 &utf)
348
330
}
349
331
350
332
uint32_t newSizeAllocated = getDataBufferSize (sizeUsed + utf.sizeUsed );
351
- auto * newData = new char [newSizeAllocated] ;
333
+ auto newData = std::make_unique< char []>(newSizeAllocated) ;
352
334
if (newData != nullptr ) {
353
335
354
- memcpy (newData, data, sizeUsed);
336
+ memcpy (newData. get () , data. get () , sizeUsed);
355
337
// -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);
359
340
sizeAllocated = newSizeAllocated;
360
341
strLength += utf.strLength ;
361
342
// -1 is to ignore double null terminator as it is counted in sizeUsed
362
343
sizeUsed += utf.sizeUsed - 1 ;
363
344
lastIndex = 0 ;
364
- lastIndexData = data;
345
+ lastIndexData = data. get () ;
365
346
}
366
347
return *this ;
367
348
}
@@ -371,25 +352,24 @@ bool UTF8::operator==(const UTF8 &utf) const
371
352
uint32_t len = strLength - utf.strLength ;
372
353
uint32_t used = sizeUsed - utf.sizeUsed ;
373
354
if ((len | used) == 0 ) {
374
- return memcmp (data, utf.data , sizeUsed) == 0 ;
355
+ return memcmp (data. get () , utf.data . get () , sizeUsed) == 0 ;
375
356
}
376
357
return false ;
377
358
}
378
359
379
360
const char *UTF8::c_str () const
380
361
{
381
- return data;
362
+ return data. get () ;
382
363
}
383
364
384
365
void UTF8::clear ()
385
366
{
386
- delete[] data;
387
- data = new char [stringExpansion];
367
+ data = std::make_unique<char []>(stringExpansion);
388
368
sizeAllocated = stringExpansion;
389
369
sizeUsed = 1 ;
390
370
strLength = 0 ;
391
371
lastIndex = 0 ;
392
- lastIndexData = data;
372
+ lastIndexData = data. get () ;
393
373
}
394
374
395
375
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
399
379
return UTF8 ();
400
380
}
401
381
402
- char *beginPtr = this ->data ;
382
+ char *beginPtr = this ->data . get () ;
403
383
char *endPtr = nullptr ;
404
384
405
385
uint32_t bufferSize = 0 ;
@@ -419,12 +399,10 @@ UTF8 UTF8::substr(const uint32_t begin, const uint32_t length) const
419
399
}
420
400
// copy data to buffer
421
401
// 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);
425
404
426
- UTF8 retString = UTF8 (buffer);
427
- delete[] buffer;
405
+ UTF8 retString = UTF8 (buffer.get ());
428
406
429
407
return retString;
430
408
}
@@ -446,7 +424,7 @@ uint32_t UTF8::find(const char *s, uint32_t pos)
446
424
}
447
425
448
426
uint32_t position = 0 ;
449
- auto *dataPtr = this ->data ;
427
+ auto *dataPtr = this ->data . get () ;
450
428
451
429
for (position = 0 ; position < pos; position++) {
452
430
dataPtr += charLength (dataPtr);
@@ -483,7 +461,7 @@ uint32_t UTF8::findLast(const char *s, uint32_t pos)
483
461
}
484
462
485
463
uint32_t position = 0 ;
486
- auto *dataPtr = this ->data ;
464
+ auto *dataPtr = this ->data . get () ;
487
465
uint32_t lastFoundPosition = npos;
488
466
489
467
// calculate position of last string to compare
@@ -505,7 +483,7 @@ UTF8 UTF8::split(const uint32_t &idx)
505
483
return UTF8 ();
506
484
}
507
485
508
- auto *dataPtr = this ->data ;
486
+ auto *dataPtr = this ->data . get () ;
509
487
510
488
// move data pointer to split index
511
489
for (uint32_t i = 0 ; i < idx; i++) {
@@ -516,26 +494,22 @@ UTF8 UTF8::split(const uint32_t &idx)
516
494
517
495
// re-create source string
518
496
// create temp copy of string
519
- uint32_t tempStringSize = dataPtr - this ->data ;
497
+ uint32_t tempStringSize = dataPtr - this ->data . get () ;
520
498
uint32_t tempStringBufferSize = getDataBufferSize (tempStringSize);
521
- auto * tempString = new char [tempStringBufferSize] ;
499
+ auto tempString = std::make_unique< char []>(tempStringBufferSize) ;
522
500
523
- memset (tempString, 0 , tempStringBufferSize);
524
- memcpy (tempString, this ->data , tempStringSize);
501
+ memcpy (tempString.get (), this ->data .get (), tempStringSize);
525
502
526
503
// add 1 to ensure string terminating zero
527
504
this ->sizeUsed = tempStringSize + 1 ;
528
505
this ->sizeAllocated = tempStringBufferSize;
529
506
this ->strLength = idx;
530
507
531
508
// 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);
536
510
537
511
this ->lastIndex = 0 ;
538
- this ->lastIndexData = this ->data ;
512
+ this ->lastIndexData = this ->data . get () ;
539
513
540
514
return retString;
541
515
}
@@ -563,7 +537,7 @@ bool UTF8::removeChar(const uint32_t &pos, const uint32_t &count)
563
537
}
564
538
565
539
// get pointer to begin of string to remove
566
- auto *beginPtr = this ->data ;
540
+ auto *beginPtr = this ->data . get () ;
567
541
for (uint32_t i = 0 ; i < pos; i++) {
568
542
beginPtr += charLength (beginPtr);
569
543
}
@@ -578,27 +552,22 @@ bool UTF8::removeChar(const uint32_t &pos, const uint32_t &count)
578
552
uint32_t newStringSize = this ->sizeUsed - bytesToRemove;
579
553
580
554
uint32_t tempStringBufferSize = getDataBufferSize (newStringSize);
581
- auto * tempString = new char [tempStringBufferSize] ;
555
+ auto tempString = std::make_unique< char []>(tempStringBufferSize) ;
582
556
if (tempString == nullptr ) {
583
557
return false ;
584
558
}
585
559
586
560
// 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);
591
564
592
565
this ->sizeAllocated = tempStringBufferSize;
593
566
this ->strLength -= count;
594
567
this ->sizeUsed -= bytesToRemove;
595
568
596
- // delete old data buffer
597
- memset (this ->data , 0 , this ->sizeAllocated );
598
- delete[] this ->data ;
599
-
600
569
// assign new data buffer
601
- this ->data = tempString;
570
+ this ->data = std::move ( tempString) ;
602
571
603
572
return true ;
604
573
}
@@ -686,17 +655,17 @@ bool UTF8::insert(const char *ch, const uint32_t &index)
686
655
}
687
656
688
657
// find pointer where new character should be copied
689
- auto *pos = data;
658
+ auto *pos = data. get () ;
690
659
for (unsigned int i = 0 ; i < insertIndex; i++) {
691
660
pos += charLength (pos);
692
661
}
693
662
694
- if ((pos - data) >= static_cast <int64_t >(sizeUsed)) {
663
+ if ((pos - data. get () ) >= static_cast <int64_t >(sizeUsed)) {
695
664
debug_utf (" decode/encode error %d - ( %d ) < 0 && allocated: %d\n " , sizeUsed, pos - data, sizeAllocated);
696
665
return false ;
697
666
}
698
667
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
700
669
}
701
670
memcpy (pos, ch, ch_len); // copy UTF8 char value
702
671
@@ -734,14 +703,14 @@ bool UTF8::insertString(const UTF8 &str, const uint32_t &index)
734
703
uint32_t totalSize = sizeUsed + str.sizeUsed - 1 ; // -1 because there are 2 end terminators
735
704
expand (getDataBufferSize (totalSize));
736
705
737
- auto *beginPtr = this ->data ;
706
+ auto *beginPtr = this ->data . get () ;
738
707
for (uint32_t i = 0 ; i < insertIndex; i++) {
739
708
beginPtr += charLength (beginPtr);
740
709
}
741
710
742
711
// -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 );
745
714
746
715
return false ;
747
716
}
0 commit comments