-
Notifications
You must be signed in to change notification settings - Fork 0
/
myString.cpp
1846 lines (1672 loc) · 50.2 KB
/
myString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cmath>
#include <csetjmp>
#include <cstdint>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <memory>
#include <new>
#include <ostream>
#include <stdexcept>
#include <utility>
#include <vector>
using std::cout;
class myStr
{
friend std::ostream &operator<<(std::ostream &osm, const myStr &str);
private:
static constexpr uint8_t sso_capacity = 17;
// Small string optimization 在栈区存储小串以提高性能
char sso_data_[sso_capacity + 1];
uint16_t size_;
uint16_t capacity_ = sso_capacity;
static constexpr uint16_t limits =
std::numeric_limits< decltype(capacity_) >::max();
bool useHeap_;
char *data_;
// for search sub-str
static std::unique_ptr< uint16_t[] > computLPS(const char *pattern)
{
const uint16_t pat_len = strlen(pattern);
std::unique_ptr< uint16_t[] > lps_array =
std::make_unique< uint16_t[] >(pat_len);
uint16_t lps_len = 0; // Length of the previous longest prefix suffix
uint16_t idx = 1;
while (idx < pat_len)
{
if (pattern[idx] == pattern[lps_len])
{
++lps_len;
lps_array[idx] = lps_len;
++idx;
} else
{
if (lps_len != 0)
{
lps_len = lps_array[lps_len - 1];
} else
{
lps_array[idx] = 0;
++idx;
}
}
}
return lps_array;
}
// for searching char
static bool is_in_pattern(const char *pattern, const char &ch)
{
uint16_t pattern_len = strlen(pattern);
for (uint16_t i = 0; i < pattern_len; ++i)
{
if (pattern[i] == ch)
{
return true;
}
}
return false;
};
// for sort
static void insertion_sort_asc(char *data, int left, int right)
{
for (int i = left + 1; i <= right; ++i)
{
char key = data[i];
int pre_idx = i - 1;
while (pre_idx >= left and data[pre_idx] > key)
{
data[pre_idx + 1] = data[pre_idx];
--pre_idx;
}
data[pre_idx + 1] = key;
}
}
static void insertion_sort_desc(char *data, int left, int right)
{
for (int i = left + 1; i <= right; ++i)
{
char key = data[i];
int pre_idx = i - 1;
while (pre_idx >= left and data[pre_idx] < key)
{
data[pre_idx + 1] = data[pre_idx];
--pre_idx;
}
data[pre_idx + 1] = key;
}
}
static int median_of_three_asc(char *data, int left, int right)
{
int mid = (left + right) >> 1;
if (data[left] > data[mid])
{
std::swap(data[left], data[mid]);
}
if (data[left] > data[right])
{
std::swap(data[left], data[right]);
}
if (data[mid] > data[right])
{
std::swap(data[mid], data[right]);
}
std::swap(data[mid], data[right - 1]);
return right - 1;
}
static int median_of_three_desc(char *data, int left, int right)
{
int mid = (left + right) >> 1;
if (data[left] < data[mid])
{
std::swap(data[left], data[mid]);
}
if (data[left] < data[right])
{
std::swap(data[left], data[right]);
}
if (data[mid] < data[right])
{
std::swap(data[mid], data[right]);
}
std::swap(data[mid], data[right - 1]);
return right - 1;
}
static uint16_t partition_asc(char *data, int left, int right)
{
int pivot_idx = median_of_three_asc(data, left, right);
char pivot = data[pivot_idx];
int left_boundary = left;
int right_boundary = right - 1;
while (true)
{
while (left_boundary < right_boundary and
data[++left_boundary] < pivot)
{
}
while (right_boundary > left_boundary and
data[--right_boundary] > pivot)
{
}
if (left_boundary < right_boundary)
{
std::swap(data[left_boundary], data[right_boundary]);
} else
{
break;
}
}
std::swap(data[left_boundary], data[right - 1]);
return left_boundary;
}
static uint16_t partition_desc(char *data, int left, int right)
{
int pivot_idx = median_of_three_desc(data, left, right);
char pivot = data[pivot_idx];
int left_boundary = left;
int right_boundary = right - 1;
while (true)
{
while (left_boundary < right_boundary and
data[++left_boundary] > pivot)
{
}
while (right_boundary > left_boundary and
data[--right_boundary] < pivot)
{
}
if (left_boundary < right_boundary)
{
std::swap(data[left_boundary], data[right_boundary]);
} else
{
break;
}
}
std::swap(data[left_boundary], data[right - 1]);
return left_boundary;
}
static void quick_sort_asc(char *data, int left, int right)
{
if (left + sso_capacity <= right)
{
int pivot_idx = partition_asc(data, left, right);
quick_sort_asc(data, left, pivot_idx - 1);
quick_sort_asc(data, pivot_idx + 1, right);
} else
{
insertion_sort_asc(data, left, right);
}
}
static void quick_sort_desc(char *data, int left, int right)
{
if (left + sso_capacity <= right)
{
int pivot_idx = partition_desc(data, left, right);
quick_sort_desc(data, left, pivot_idx - 1);
quick_sort_desc(data, pivot_idx + 1, right);
} else
{
insertion_sort_desc(data, left, right);
}
}
public:
class Iterator
{
private:
myStr &str;
uint16_t index;
public:
// Iterator
Iterator(myStr &str, uint16_t index) : str(str), index(index){};
char operator*() const
{
if (index >= str.size_)
{
throw std::out_of_range("Index out of range at deReference");
}
return str.useHeap_ ? str.data_[index] : str.sso_data_[index];
}
bool operator==(const Iterator &other) const
{
return index == other.index and &str == &other.str;
}
bool operator!=(const Iterator &other) const
{
return index != other.index or &str != &other.str;
}
Iterator &operator++()
{
++index;
return *this;
}
Iterator operator++(int)
{
Iterator temp = *this;
++index;
return temp;
}
Iterator &operator--()
{
--index;
return *this;
}
Iterator operator--(int)
{
Iterator temp = *this;
--index;
return temp;
}
Iterator operator+(uint16_t distance)
{
Iterator temp = *this;
temp.index += distance;
return temp;
}
Iterator &operator+=(uint16_t distance)
{
index += distance;
return *this;
}
Iterator operator-(uint16_t distance)
{
Iterator temp = *this;
temp.index -= distance;
return temp;
}
Iterator operator-=(uint16_t distance)
{
index -= distance;
return *this;
}
int distance(const Iterator &other) const
{
if (&str != &other.str)
{
throw std::invalid_argument(
"Iterators belong to different objects");
}
return static_cast< int >(index - other.index);
}
uint16_t get_position() const
{
return index;
}
bool is_sameString(const Iterator &other) const
{
return &str == &other.str;
}
};
// Member function
myStr() : useHeap_(false), size_(0), data_(nullptr), sso_data_(""){};
myStr(const char *str)
: size_(0), data_(nullptr), useHeap_(false), sso_data_("")
{
*this = str;
}
myStr(Iterator left, Iterator right)
: size_(0), data_(nullptr), useHeap_(false)
{
if (!left.is_sameString(right))
{
throw std::invalid_argument(
"Two Iterators belong to different myStr");
}
if (left.get_position() > right.get_position())
{
throw std::invalid_argument(
"Left Iterator must be before right Iterator");
}
size_ = right.get_position() - left.get_position();
if (size_ < sso_capacity)
{
useHeap_ = false;
for (uint16_t i = 0; i < size_; ++i)
{
sso_data_[i] = *left;
++left;
}
sso_data_[size_] = '\0';
} else
{
useHeap_ = true;
capacity_ = (size_ * 2 + 1 < limits) ? size_ * 2 + 1 : limits;
try
{
data_ = new char[capacity_];
for (uint16_t i = 0; i < size_; ++i)
{
data_[i] = *left;
++left;
}
data_[size_] = '\0';
}
catch (const std::bad_alloc &)
{
throw std::runtime_error(
"Memory allocation error at `range_constructor`");
}
}
}
myStr(const myStr &other)
: useHeap_(other.useHeap_), size_(other.size_),
capacity_(other.capacity_), data_(nullptr)
{
if (useHeap_)
{
try
{
data_ = new char[capacity_];
strcpy(data_, other.data_);
}
catch (std::bad_alloc &)
{
throw std::runtime_error("Memory allocation error");
}
} else
{
strcpy(sso_data_, other.sso_data_);
}
}
// operator= for const char*
myStr &operator=(const char *str)
{
uint32_t str_len = strlen(str);
if (str_len > limits)
{
throw std::out_of_range("String Exceeds limits");
}
// if str lt sso_capacity, it't not need allocate memory and it's need
// to free memory which had allocated.
if (str_len < sso_capacity)
{
if (useHeap_)
{
delete[] data_;
data_ = nullptr;
strcpy(sso_data_, str);
} else
{
strcpy(sso_data_, str);
}
}
// case need allocate memory
else
{
// case dont't need re-allocate memory
if (capacity_ > str_len)
{
strcpy(data_, str);
} else
{
if (useHeap_)
{
// re-allocate memory
delete[] data_;
capacity_ =
(str_len * 2 + 1) < limits ? str_len * 2 + 1 : limits;
try
{
data_ = new char[capacity_];
strcpy(data_, str);
}
catch (std::bad_alloc &)
{
throw std::runtime_error("Memory allocation error");
}
} else
{
// allocate memory
useHeap_ = true;
capacity_ =
(str_len * 2 + 1) < limits ? str_len * 2 + 1 : limits;
try
{
data_ = new char[capacity_];
strcpy(data_, str);
}
catch (std::bad_alloc &)
{
throw std::runtime_error("Memory allocation error");
}
}
}
}
size_ = str_len;
return *this;
}
// operator= for other myStr
myStr &operator=(const myStr &other)
{
// self-check for avoid memory leak
if (this == &other)
{
return *this;
}
if (useHeap_)
{
delete[] data_;
data_ = nullptr;
}
useHeap_ = other.useHeap_;
size_ = other.size_;
capacity_ = other.capacity_;
if (useHeap_)
{
try
{
data_ = new char[capacity_];
strcpy(data_, other.data_);
}
catch (std::bad_alloc &)
{
throw std::runtime_error("Memory allocation error");
}
} else
{
strcpy(sso_data_, other.sso_data_);
}
return *this;
}
myStr(myStr &&other) noexcept
: useHeap_(other.useHeap_), size_(other.size_),
capacity_(other.capacity_), data_(other.data_)
{
strcpy(sso_data_, other.sso_data_);
other.size_ = 0;
other.capacity_ = 0;
other.useHeap_ = false;
other.data_ = nullptr;
}
~myStr()
{
clear();
}
char &operator[](uint16_t index)
{
// If it's necessary for range checking
// if (index >= size_)
// {
// throw std::out_of_range("out_of_range");
// }
return useHeap_ ? data_[index] : sso_data_[index];
}
const char &operator[](uint16_t index) const
{
return useHeap_ ? data_[index] : sso_data_[index];
}
bool operator==(const myStr &other) const
{
if (size_ != other.size_)
{
return false;
}
for (uint16_t i = 0; i < size_; ++i)
{
char left_char = useHeap_ ? data_[i] : sso_data_[i];
char right_char =
other.useHeap_ ? other.data_[i] : other.sso_data_[i];
if (left_char != right_char)
{
return false;
}
}
return true;
}
bool operator!=(const myStr &other) const
{
if (size_ != other.size_)
{
return true;
}
for (uint16_t i = 0; i < size_; ++i)
{
char left_char = useHeap_ ? data_[i] : sso_data_[i];
char right_char =
other.useHeap_ ? other.data_[i] : other.sso_data_[i];
if (left_char != right_char)
{
return true;
}
}
return false;
}
myStr operator+(const char ch) const
{
myStr temp = *this;
uint32_t new_size = size_ + 1;
if (new_size > limits)
{
throw std::out_of_range("String Exceeds limit");
}
// sso_capacity still enough for appending string
if (new_size <= sso_capacity)
{
temp[size_] = ch;
temp[size_ + 1] = '\0';
}
// sso_capacity not enough
else if (useHeap_)
{
// data left capacity is enought for expanding string
if (capacity_ > new_size)
{
temp[size_] = ch;
temp[size_ + 1] = '\0';
}
// data left capacity isn't enough, so re-allocate memory for
// expanding string and free old data memory
else
{
temp.capacity_ =
(new_size * 2 + 1) < limits ? (new_size * 2 + 1) : limits;
char *new_data = new char[capacity_];
strcpy(new_data, data_);
delete[] temp.data_;
temp.data_ = new_data;
temp[size_] = ch;
temp[size_ + 1] = '\0';
}
}
// sso_capacity isn't enough, and data need use heap
else
{
temp.useHeap_ = true;
temp.capacity_ =
(new_size * 2 + 1) < limits ? (new_size * 2 + 1) : limits;
temp.data_ = new char[temp.capacity_];
strcpy(temp.data_, temp.sso_data_);
temp[size_] = ch;
temp[size_ + 1] = '\0';
}
temp.size_ = new_size;
return temp;
}
myStr operator+(const char *str) const
{
myStr temp = *this;
uint16_t expand_size = strlen(str);
uint32_t new_size = size_ + expand_size;
if (new_size > limits)
{
throw std::out_of_range("String Exceeds limit");
}
// sso_capacity still enough for appending string
if (new_size <= sso_capacity)
{
strcpy(&temp.sso_data_[temp.size_], str);
}
// sso_capacity not enough
else if (useHeap_)
{
uint16_t left_capacity = capacity_ - size_;
// data left capacity is enought for expanding string
if (left_capacity > expand_size)
{
strcpy(&temp.data_[size_], str);
}
// data left capacity isn't enough, so re-allocate memory for
// expanding string and free old data memory
else
{
temp.capacity_ =
(new_size * 2 + 1) < limits ? (new_size * 2 + 1) : limits;
char *new_data = new char[capacity_];
strcpy(new_data, data_);
strcpy(&new_data[size_], str);
delete[] temp.data_;
temp.data_ = new_data;
}
}
// sso_capacity isn't enough, and data need use heap
else
{
temp.useHeap_ = true;
temp.capacity_ =
(new_size * 2 + 1) < limits ? (new_size * 2 + 1) : limits;
temp.data_ = new char[temp.capacity_];
strcpy(temp.data_, temp.sso_data_);
strcpy(&temp.data_[temp.size_], str);
}
temp.size_ = new_size;
return temp;
}
myStr operator+(const myStr &other) const
{
return *this + (other.useHeap_ ? other.data_ : other.sso_data_);
}
myStr &operator+=(const char ch)
{
push_back(ch);
return *this;
}
myStr &operator+=(const char *str)
{
uint16_t expansion_size = strlen(str);
uint32_t new_size = size_ + expansion_size;
if (new_size > limits)
{
throw std::out_of_range("String Exceeds limit");
}
// sso_capacity still enough for appending string
if (new_size <= sso_capacity)
{
strcpy(&sso_data_[size_], str);
}
// sso_capacity not enough
else if (useHeap_)
{
uint16_t left_capacity = capacity_ - size_;
// data left capacity is enought for expanding string
if (left_capacity > expansion_size)
{
strcpy(&data_[size_], str);
}
// data left capacity isn't enough, so re-allocate memory for
// expanding string and free old data memory
else
{
capacity_ =
(new_size * 2 + 1) < limits ? (new_size * 2 + 1) : limits;
char *new_data = new char[capacity_];
strcpy(new_data, data_);
strcpy(&new_data[size_], str);
delete[] data_;
data_ = new_data;
}
}
// sso_capacity isn't enough, and data need use heap
else
{
useHeap_ = true;
capacity_ =
(new_size * 2 + 1) < limits ? (new_size * 2 + 1) : limits;
data_ = new char[capacity_];
strcpy(data_, sso_data_);
strcpy(&data_[size_], str);
}
size_ = new_size;
return *this;
}
myStr &operator+=(const myStr &other)
{
return *this += (other.useHeap_ ? other.data_ : other.sso_data_);
}
Iterator begin()
{
return Iterator(*this, 0);
}
Iterator end()
{
return Iterator(*this, size_);
}
Iterator rbegin()
{
return Iterator(*this, size_ - 1);
}
Iterator rend()
{
return Iterator(*this, -1);
}
uint16_t length() const
{
return size_;
}
uint16_t capacity() const
{
return capacity_;
}
uint16_t size() const
{
return size_;
}
const char *data()
{
return useHeap_ ? data_ : sso_data_;
}
// Reserve memory which eq new_capacity.
void reserve(const uint16_t new_capacity)
{
if (capacity_ < new_capacity)
{
char *new_data =
new char[new_capacity < limits ? new_capacity : limits];
if (useHeap_)
{
strcpy(new_data, data_);
delete[] data_; // 分析删除现有 data_
} else
{
strcpy(new_data, sso_data_);
}
data_ = new_data;
capacity_ = new_capacity;
useHeap_ = true;
}
}
// change size of myStr obj. If new_size > siez_, then the left chars filled
// by fill_char;
// If new_size < size_, then the str is truncated to new_size;
// @param[in] new_size
// @param[in] fill_char
void resize(const uint16_t new_size, const char fill_char = '\0')
{
// need to re-allocate memory
if (new_size > capacity_)
{
capacity_ = (new_size * 2 + 1) < limits ? new_size * 2 + 1 : limits;
char *new_data_ = new char[capacity_];
strcpy(new_data_, useHeap_ ? data_ : sso_data_);
if (useHeap_)
{
delete[] data_;
}
data_ = new_data_;
useHeap_ = true;
for (uint16_t i = size_; i < new_size; ++i)
{
data_[i] = fill_char;
}
size_ = new_size;
}
// new_size < capacity_ , so there is no need to re-allocate memory.
// Just fill new char or truncate str.
else
{
if (new_size > size_)
{
for (uint16_t i = size_; i < new_size; ++i)
{
useHeap_ ? data_[i] = fill_char : sso_data_[i] = fill_char;
}
} else
{
useHeap_ ? data_[new_size] = '\0' : sso_data_[new_size] = '\0';
}
}
size_ = new_size;
}
/**
* @brief push a char in the front of this string. O(n)
*
* @param[in] ch inserted char
*
* @return Reference of this string
*/
myStr &push_front(const char ch)
{
uint32_t new_size = size_ + 1;
if (new_size > limits)
{
throw std::out_of_range("String Exceeds limit at `push_front`");
}
// sso_capacity still enough for appending string
if (new_size <= sso_capacity)
{
memmove(&sso_data_[1], &sso_data_[0], size_);
sso_data_[0] = ch;
sso_data_[new_size] = '\0';
}
// sso_capacity not enough
else if (useHeap_)
{
// data left capacity is enought for expanding string
if (capacity_ > new_size)
{
memmove(&data_[1], &data_[0], size_);
data_[0] = ch;
data_[new_size] = '\0';
}
// data left capacity isn't enough, so re-allocate memory for
// expanding string and free old data memory
else
{
capacity_ =
(new_size * 2 + 1) < limits ? (new_size * 2 + 1) : limits;
char *new_data = new char[capacity_];
new_data[0] = ch;
strcpy(&new_data[1], data_);
new_data[new_size] = '\0';
delete[] data_;
data_ = new_data;
}
}
// sso_capacity isn't enough, and data need use heap
else
{
useHeap_ = true;
capacity_ =
(new_size * 2 + 1) < limits ? (new_size * 2 + 1) : limits;
data_ = new char[capacity_];
data_[0] = ch;
strcpy(&data_[1], sso_data_);
data_[new_size] = '\0';
}
size_ = new_size;
return *this;
}
/**
* @brief push a char end of this string
*
* @param[in] ch Inserted char
*
* @return Ref of this string
*/
myStr &push_back(const char ch)
{
uint32_t new_size = size_ + 1;
if (new_size > limits)
{
throw std::out_of_range("String Exceeds limit at `push_back`");
}
// sso_capacity still enough for appending string
if (new_size <= sso_capacity)
{
sso_data_[size_] = ch;
sso_data_[new_size] = '\0';
}
// sso_capacity not enough
else if (useHeap_)
{
// data left capacity is enought for expanding string
if (capacity_ > new_size)
{
data_[size_] = ch;
data_[new_size] = '\0';
}
// data left capacity isn't enough, so re-allocate memory for
// expanding string and free old data memory
else
{
capacity_ =
(new_size * 2 + 1) < limits ? (new_size * 2 + 1) : limits;
char *new_data = new char[capacity_];
strcpy(new_data, data_);
delete[] data_;
data_ = new_data;
data_[size_] = ch;
data_[new_size] = '\0';
}
}
// sso_capacity isn't enough, and data need use heap
else
{
useHeap_ = true;
capacity_ =
(new_size * 2 + 1) < limits ? (new_size * 2 + 1) : limits;
data_ = new char[capacity_];
strcpy(data_, sso_data_);