-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_string.h
2075 lines (1873 loc) · 56.8 KB
/
basic_string.h
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
#ifndef MYTINYSTL_BASIC_STRING_H_
#define MYTINYSTL_BASIC_STRING_H_
// 这个头文件包含一个模板类 basic_string
// 用于表示字符串类型
#include <iostream>
#include "iterator.h"
#include "memory.h"
#include "functional.h"
#include "exceptdef.h"
namespace mystl
{
// char_traits
template <class CharType>
struct char_traits
{
typedef CharType char_type;
static size_t length(const char_type* str)
{
size_t len = 0;
for (; *str != char_type(0); ++str)
++len;
return len;
}
static int compare(const char_type* s1, const char_type* s2, size_t n)
{
for (; n != 0; --n, ++s1, ++s2)
{
if (*s1 < *s2)
return -1;
if (*s2 < *s1)
return 1;
}
return 0;
}
static char_type* copy(char_type* dst, const char_type* src, size_t n)
{
MYSTL_DEBUG(src + n <= dst || dst + n <= src);
char_type* r = dst;
for (; n != 0; --n, ++dst, ++src)
*dst = *src;
return r;
}
static char_type* move(char_type* dst, const char_type* src, size_t n)
{
char_type* r = dst;
if (dst < src)
{
for (; n != 0; --n, ++dst, ++src)
*dst = *src;
}
else if (src < dst)
{
dst += n;
src += n;
for (; n != 0; --n)
*--dst = *--src;
}
return r;
}
static char_type* fill(char_type* dst, char_type ch, size_t count)
{
char_type* r = dst;
for (; count > 0; --count, ++dst)
*dst = ch;
return r;
}
};
// Partialized. char_traits<char>
template <>
struct char_traits<char>
{
typedef char char_type;
static size_t length(const char_type* str) noexcept
{ return std::strlen(str); }
static int compare(const char_type* s1, const char_type* s2, size_t n) noexcept
{ return std::memcmp(s1, s2, n); }
static char_type* copy(char_type* dst, const char_type* src, size_t n) noexcept
{
MYSTL_DEBUG(src + n <= dst || dst + n <= src);
return static_cast<char_type*>(std::memcpy(dst, src, n));
}
static char_type* move(char_type* dst, const char_type* src, size_t n) noexcept
{
return static_cast<char_type*>(std::memmove(dst, src, n));
}
static char_type* fill(char_type* dst, char_type ch, size_t count) noexcept
{
return static_cast<char_type*>(std::memset(dst, ch, count));
}
};
// Partialized. char_traits<wchar_t>
template <>
struct char_traits<wchar_t>
{
typedef wchar_t char_type;
static size_t length(const char_type* str) noexcept
{
return std::wcslen(str);
}
static int compare(const char_type* s1, const char_type* s2, size_t n) noexcept
{
return std::wmemcmp(s1, s2, n);
}
static char_type* copy(char_type* dst, const char_type* src, size_t n) noexcept
{
MYSTL_DEBUG(src + n <= dst || dst + n <= src);
return static_cast<char_type*>(std::wmemcpy(dst, src, n));
}
static char_type* move(char_type* dst, const char_type* src, size_t n) noexcept
{
return static_cast<char_type*>(std::wmemmove(dst, src, n));
}
static char_type* fill(char_type* dst, char_type ch, size_t count) noexcept
{
return static_cast<char_type*>(std::wmemset(dst, ch, count));
}
};
// Partialized. char_traits<char16_t>
template <>
struct char_traits<char16_t>
{
typedef char16_t char_type;
static size_t length(const char_type* str) noexcept
{
size_t len = 0;
for (; *str != char_type(0); ++str)
++len;
return len;
}
static int compare(const char_type* s1, const char_type* s2, size_t n) noexcept
{
for (; n != 0; --n, ++s1, ++s2)
{
if (*s1 < *s2)
return -1;
if (*s2 < *s1)
return 1;
}
return 0;
}
static char_type* copy(char_type* dst, const char_type* src, size_t n) noexcept
{
MYSTL_DEBUG(src + n <= dst || dst + n <= src);
char_type* r = dst;
for (; n != 0; --n, ++dst, ++src)
*dst = *src;
return r;
}
static char_type* move(char_type* dst, const char_type* src, size_t n) noexcept
{
char_type* r = dst;
if (dst < src)
{
for (; n != 0; --n, ++dst, ++src)
*dst = *src;
}
else if (src < dst)
{
dst += n;
src += n;
for (; n != 0; --n)
*--dst = *--src;
}
return r;
}
static char_type* fill(char_type* dst, char_type ch, size_t count) noexcept
{
char_type* r = dst;
for (; count > 0; --count, ++dst)
*dst = ch;
return r;
}
};
// Partialized. char_traits<char32_t>
template <>
struct char_traits<char32_t>
{
typedef char32_t char_type;
static size_t length(const char_type* str) noexcept
{
size_t len = 0;
for (; *str != char_type(0); ++str)
++len;
return len;
}
static int compare(const char_type* s1, const char_type* s2, size_t n) noexcept
{
for (; n != 0; --n, ++s1, ++s2)
{
if (*s1 < *s2)
return -1;
if (*s2 < *s1)
return 1;
}
return 0;
}
static char_type* copy(char_type* dst, const char_type* src, size_t n) noexcept
{
MYSTL_DEBUG(src + n <= dst || dst + n <= src);
char_type* r = dst;
for (; n != 0; --n, ++dst, ++src)
*dst = *src;
return r;
}
static char_type* move(char_type* dst, const char_type* src, size_t n) noexcept
{
char_type* r = dst;
if (dst < src)
{
for (; n != 0; --n, ++dst, ++src)
*dst = *src;
}
else if (src < dst)
{
dst += n;
src += n;
for (; n != 0; --n)
*--dst = *--src;
}
return r;
}
static char_type* fill(char_type* dst, char_type ch, size_t count) noexcept
{
char_type* r = dst;
for (; count > 0; --count, ++dst)
*dst = ch;
return r;
}
};
// 初始化 basic_string 尝试分配的最小 buffer 大小,可能被忽略
#define STRING_INIT_SIZE 32
// 模板类 basic_string
// 参数一代表字符类型,参数二代表萃取字符类型的方式,缺省使用 mystl::char_traits
template <class CharType, class CharTraits = mystl::char_traits<CharType>>
class basic_string
{
public:
typedef CharTraits traits_type;
typedef CharTraits char_traits;
typedef mystl::allocator<CharType> allocator_type;
typedef mystl::allocator<CharType> data_allocator;
typedef typename allocator_type::value_type value_type;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef mystl::reverse_iterator<iterator> reverse_iterator;
typedef mystl::reverse_iterator<const_iterator> const_reverse_iterator;
allocator_type get_allocator() { return allocator_type(); }
static_assert(std::is_pod<CharType>::value, "Character type of basic_string must be a POD");
static_assert(std::is_same<CharType, typename traits_type::char_type>::value,
"CharType must be same as traits_type::char_type");
public:
// 末尾位置的值,例:
// if (str.find('a') != string::npos) { /* do something */ }
static constexpr size_type npos = static_cast<size_type>(-1);
private:
iterator buffer_; // 储存字符串的起始位置
size_type size_; // 大小
size_type cap_; // 容量
public:
// 构造、复制、移动、析构函数
basic_string() noexcept
{ try_init(); }
basic_string(size_type n, value_type ch)
:buffer_(nullptr), size_(0), cap_(0)
{
fill_init(n, ch);
}
basic_string(const basic_string& other, size_type pos)
:buffer_(nullptr), size_(0), cap_(0)
{
init_from(other.buffer_, pos, other.size_ - pos);
}
basic_string(const basic_string& other, size_type pos, size_type count)
:buffer_(nullptr), size_(0), cap_(0)
{
init_from(other.buffer_, pos, count);
}
basic_string(const_pointer str)
:buffer_(nullptr), size_(0), cap_(0)
{
init_from(str, 0, char_traits::length(str));
}
basic_string(const_pointer str, size_type count)
:buffer_(nullptr), size_(0), cap_(0)
{
init_from(str, 0, count);
}
template <class Iter, typename std::enable_if<
mystl::is_input_iterator<Iter>::value, int>::type = 0>
basic_string(Iter first, Iter last)
{ copy_init(first, last, iterator_category(first)); }
basic_string(const basic_string& rhs)
:buffer_(nullptr), size_(0), cap_(0)
{
init_from(rhs.buffer_, 0, rhs.size_);
}
basic_string(basic_string&& rhs) noexcept
:buffer_(rhs.buffer_), size_(rhs.size_), cap_(rhs.cap_)
{
rhs.buffer_ = nullptr;
rhs.size_ = 0;
rhs.cap_ = 0;
}
basic_string& operator=(const basic_string& rhs);
basic_string& operator=(basic_string&& rhs) noexcept;
basic_string& operator=(const_pointer str);
basic_string& operator=(value_type ch);
~basic_string() { destroy_buffer(); }
public:
// 迭代器相关操作
iterator begin() noexcept
{ return buffer_; }
const_iterator begin() const noexcept
{ return buffer_; }
iterator end() noexcept
{ return buffer_ + size_; }
const_iterator end() const noexcept
{ return buffer_ + size_; }
reverse_iterator rbegin() noexcept
{ return reverse_iterator(end()); }
const_reverse_iterator rbegin() const noexcept
{ return const_reverse_iterator(end()); }
reverse_iterator rend() noexcept
{ return reverse_iterator(begin()); }
const_reverse_iterator rend() const noexcept
{ return const_reverse_iterator(begin()); }
const_iterator cbegin() const noexcept
{ return begin(); }
const_iterator cend() const noexcept
{ return end(); }
const_reverse_iterator crbegin() const noexcept
{ return rbegin(); }
const_reverse_iterator crend() const noexcept
{ return rend(); }
// 容量相关操作
bool empty() const noexcept
{ return size_ == 0; }
size_type size() const noexcept
{ return size_; }
size_type length() const noexcept
{ return size_; }
size_type capacity() const noexcept
{ return cap_; }
size_type max_size() const noexcept
{ return static_cast<size_type>(-1); }
void reserve(size_type n);
void shrink_to_fit();
// 访问元素相关操作
reference operator[](size_type n)
{
MYSTL_DEBUG(n <= size_);
if (n == size_)
*(buffer_ + n) = value_type();
return *(buffer_ + n);
}
const_reference operator[](size_type n) const
{
MYSTL_DEBUG(n <= size_);
if (n == size_)
*(buffer_ + n) = value_type();
return *(buffer_ + n);
}
reference at(size_type n)
{
THROW_OUT_OF_RANGE_IF(n >= size_, "basic_string<Char, Traits>::at()"
"subscript out of range");
return (*this)[n];
}
const_reference at(size_type n) const
{
THROW_OUT_OF_RANGE_IF(n >= size_, "basic_string<Char, Traits>::at()"
"subscript out of range");
return (*this)[n];
}
reference front()
{
MYSTL_DEBUG(!empty());
return *begin();
}
const_reference front() const
{
MYSTL_DEBUG(!empty());
return *begin();
}
reference back()
{
MYSTL_DEBUG(!empty());
return *(end() - 1);
}
const_reference back() const
{
MYSTL_DEBUG(!empty());
return *(end() - 1);
}
const_pointer data() const noexcept
{ return to_raw_pointer(); }
const_pointer c_str() const noexcept
{ return to_raw_pointer(); }
// 添加删除相关操作
// insert
iterator insert(const_iterator pos, value_type ch);
iterator insert(const_iterator pos, size_type count, value_type ch);
template <class Iter>
iterator insert(const_iterator pos, Iter first, Iter last);
// push_back / pop_back
void push_back(value_type ch)
{ append(1, ch); }
void pop_back()
{
MYSTL_DEBUG(!empty());
--size_;
}
// append
basic_string& append(size_type count, value_type ch);
basic_string& append(const basic_string& str)
{ return append(str, 0, str.size_); }
basic_string& append(const basic_string& str, size_type pos)
{ return append(str, pos, str.size_ - pos); }
basic_string& append(const basic_string& str, size_type pos, size_type count);
basic_string& append(const_pointer s)
{ return append(s, char_traits::length(s)); }
basic_string& append(const_pointer s, size_type count);
template <class Iter, typename std::enable_if<
mystl::is_input_iterator<Iter>::value, int>::type = 0>
basic_string& append(Iter first, Iter last)
{ return append_range(first, last); }
// erase /clear
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);
// resize
void resize(size_type count)
{ resize(count, value_type()); }
void resize(size_type count, value_type ch);
void clear() noexcept
{ size_ = 0; }
// basic_string 相关操作
// compare
int compare(const basic_string& other) const;
int compare(size_type pos1, size_type count1, const basic_string& other) const;
int compare(size_type pos1, size_type count1, const basic_string& other,
size_type pos2, size_type count2 = npos) const;
int compare(const_pointer s) const;
int compare(size_type pos1, size_type count1, const_pointer s) const;
int compare(size_type pos1, size_type count1, const_pointer s, size_type count2) const;
// substr
basic_string substr(size_type index, size_type count = npos)
{
count = mystl::min(count, size_ - index);
return basic_string(buffer_ + index, buffer_ + index + count);
}
// replace
basic_string& replace(size_type pos, size_type count, const basic_string& str)
{
THROW_OUT_OF_RANGE_IF(pos > size_, "basic_string<Char, Traits>::replace's pos out of range");
return replace_cstr(buffer_ + pos, count, str.buffer_, str.size_);
}
basic_string& replace(const_iterator first, const_iterator last, const basic_string& str)
{
MYSTL_DEBUG(begin() <= first && last <= end() && first <= last);
return replace_cstr(first, static_cast<size_type>(last - first), str.buffer_, str.size_);
}
basic_string& replace(size_type pos, size_type count, const_pointer str)
{
THROW_OUT_OF_RANGE_IF(pos > size_, "basic_string<Char, Traits>::replace's pos out of range");
return replace_cstr(buffer_ + pos, count, str, char_traits::length(str));
}
basic_string& replace(const_iterator first, const_iterator last, const_pointer str)
{
MYSTL_DEBUG(begin() <= first && last <= end() && first <= last);
return replace_cstr(first, static_cast<size_type>(last - first), str, char_traits::length(str));
}
basic_string& replace(size_type pos, size_type count, const_pointer str, size_type count2)
{
THROW_OUT_OF_RANGE_IF(pos > size_, "basic_string<Char, Traits>::replace's pos out of range");
return replace_cstr(buffer_ + pos, count, str, count2);
}
basic_string& replace(const_iterator first, const_iterator last, const_pointer str, size_type count)
{
MYSTL_DEBUG(begin() <= first && last <= end() && first <= last);
return replace_cstr(first, static_cast<size_type>(last - first), str, count);
}
basic_string& replace(size_type pos, size_type count, size_type count2, value_type ch)
{
THROW_OUT_OF_RANGE_IF(pos > size_, "basic_string<Char, Traits>::replace's pos out of range");
return replace_fill(buffer_ + pos, count, count2, ch);
}
basic_string& replace(const_iterator first, const_iterator last, size_type count, value_type ch)
{
MYSTL_DEBUG(begin() <= first && last <= end() && first <= last);
return replace_fill(first, static_cast<size_type>(last - first), count, ch);
}
basic_string& replace(size_type pos1, size_type count1, const basic_string& str,
size_type pos2, size_type count2 = npos)
{
THROW_OUT_OF_RANGE_IF(pos1 > size_ || pos2 > str.size_,
"basic_string<Char, Traits>::replace's pos out of range");
return replace_cstr(buffer_ + pos1, count1, str.buffer_ + pos2, count2);
}
template <class Iter, typename std::enable_if<
mystl::is_input_iterator<Iter>::value, int>::type = 0>
basic_string& replace(const_iterator first, const_iterator last, Iter first2, Iter last2)
{
MYSTL_DEBUG(begin() <= first && last <= end() && first <= last);
return replace_copy(first, last, first2, last2);
}
// reverse
void reverse() noexcept;
// swap
void swap(basic_string& rhs) noexcept;
// 查找相关操作
// find
size_type find(value_type ch, size_type pos = 0) const noexcept;
size_type find(const_pointer str, size_type pos = 0) const noexcept;
size_type find(const_pointer str, size_type pos, size_type count) const noexcept;
size_type find(const basic_string& str, size_type pos = 0) const noexcept;
// rfind
size_type rfind(value_type ch, size_type pos = npos) const noexcept;
size_type rfind(const_pointer str, size_type pos = npos) const noexcept;
size_type rfind(const_pointer str, size_type pos, size_type count) const noexcept;
size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
// find_first_of
size_type find_first_of(value_type ch, size_type pos = 0) const noexcept;
size_type find_first_of(const_pointer s, size_type pos = 0) const noexcept;
size_type find_first_of(const_pointer s, size_type pos, size_type count) const noexcept;
size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
// find_first_not_of
size_type find_first_not_of(value_type ch, size_type pos = 0) const noexcept;
size_type find_first_not_of(const_pointer s, size_type pos = 0) const noexcept;
size_type find_first_not_of(const_pointer s, size_type pos, size_type count) const noexcept;
size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
// find_last_of
size_type find_last_of(value_type ch, size_type pos = 0) const noexcept;
size_type find_last_of(const_pointer s, size_type pos = 0) const noexcept;
size_type find_last_of(const_pointer s, size_type pos, size_type count) const noexcept;
size_type find_last_of(const basic_string& str, size_type pos = 0) const noexcept;
// find_last_not_of
size_type find_last_not_of(value_type ch, size_type pos = 0) const noexcept;
size_type find_last_not_of(const_pointer s, size_type pos = 0) const noexcept;
size_type find_last_not_of(const_pointer s, size_type pos, size_type count) const noexcept;
size_type find_last_not_of(const basic_string& str, size_type pos = 0) const noexcept;
// count
size_type count(value_type ch, size_type pos = 0) const noexcept;
public:
// 重载 operator+=
basic_string& operator+=(const basic_string& str)
{ return append(str); }
basic_string& operator+=(value_type ch)
{ return append(1, ch); }
basic_string& operator+=(const_pointer str)
{ return append(str, str + char_traits::length(str)); }
// 重载 operator >> / operatror <<
friend std::istream& operator >> (std::istream& is, basic_string& str)
{
value_type* buf = new value_type[4096];
is >> buf;
basic_string tmp(buf);
str = std::move(tmp);
delete[]buf;
return is;
}
friend std::ostream& operator << (std::ostream& os, const basic_string& str)
{
for (size_type i = 0; i < str.size_; ++i)
os << *(str.buffer_ + i);
return os;
}
private:
// helper functions
// init / destroy
void try_init() noexcept;
void fill_init(size_type n, value_type ch);
template <class Iter>
void copy_init(Iter first, Iter last, mystl::input_iterator_tag);
template <class Iter>
void copy_init(Iter first, Iter last, mystl::forward_iterator_tag);
void init_from(const_pointer src, size_type pos, size_type n);
void destroy_buffer();
// get raw pointer
const_pointer to_raw_pointer() const;
// shrink_to_fit
void reinsert(size_type size);
// append
template <class Iter>
basic_string& append_range(Iter first, Iter last);
// compare
int compare_cstr(const_pointer s1, size_type n1, const_pointer s2, size_type n2) const;
// replace
basic_string& replace_cstr(const_iterator first, size_type count1, const_pointer str, size_type count2);
basic_string& replace_fill(const_iterator first, size_type count1, size_type count2, value_type ch);
template <class Iter>
basic_string& replace_copy(const_iterator first, const_iterator last, Iter first2, Iter last2);
// reallocate
void reallocate(size_type need);
iterator reallocate_and_fill(iterator pos, size_type n, value_type ch);
iterator reallocate_and_copy(iterator pos, const_iterator first, const_iterator last);
};
/*****************************************************************************************/
// 复制赋值操作符
template <class CharType, class CharTraits>
basic_string<CharType, CharTraits>&
basic_string<CharType, CharTraits>::
operator=(const basic_string& rhs)
{
if (this != &rhs)
{
basic_string tmp(rhs);
swap(tmp);
}
return *this;
}
// 移动赋值操作符
template <class CharType, class CharTraits>
basic_string<CharType, CharTraits>&
basic_string<CharType, CharTraits>::
operator=(basic_string&& rhs) noexcept
{
destroy_buffer();
buffer_ = rhs.buffer_;
size_ = rhs.size_;
cap_ = rhs.cap_;
rhs.buffer_ = nullptr;
rhs.size_ = 0;
rhs.cap_ = 0;
return *this;
}
// 用一个字符串赋值
template <class CharType, class CharTraits>
basic_string<CharType, CharTraits>&
basic_string<CharType, CharTraits>::
operator=(const_pointer str)
{
const size_type len = char_traits::length(str);
if (cap_ < len)
{
auto new_buffer = data_allocator::allocate(len + 1);
data_allocator::deallocate(buffer_);
buffer_ = new_buffer;
cap_ = len + 1;
}
char_traits::copy(buffer_, str, len);
size_ = len;
return *this;
}
// 用一个字符赋值
template <class CharType, class CharTraits>
basic_string<CharType, CharTraits>&
basic_string<CharType, CharTraits>::
operator=(value_type ch)
{
if (cap_ < 1)
{
auto new_buffer = data_allocator::allocate(2);
data_allocator::deallocate(buffer_);
buffer_ = new_buffer;
cap_ = 2;
}
*buffer_ = ch;
size_ = 1;
return *this;
}
// 预留储存空间
template <class CharType, class CharTraits>
void basic_string<CharType, CharTraits>::
reserve(size_type n)
{
if (cap_ < n)
{
THROW_LENGTH_ERROR_IF(n > max_size(), "n can not larger than max_size()"
"in basic_string<Char,Traits>::reserve(n)");
auto new_buffer = data_allocator::allocate(n);
char_traits::move(new_buffer, buffer_, size_);
buffer_ = new_buffer;
cap_ = n;
}
}
// 减少不用的空间
template <class CharType, class CharTraits>
void basic_string<CharType, CharTraits>::
shrink_to_fit()
{
if (size_ != cap_)
{
reinsert(size_);
}
}
// 在 pos 处插入一个元素
template <class CharType, class CharTraits>
typename basic_string<CharType, CharTraits>::iterator
basic_string<CharType, CharTraits>::
insert(const_iterator pos, value_type ch)
{
iterator r = const_cast<iterator>(pos);
if (size_ == cap_)
{
return reallocate_and_fill(r, 1, ch);
}
char_traits::move(r + 1, r, end() - r);
++size_;
*r = ch;
return r;
}
// 在 pos 处插入 n 个元素
template <class CharType, class CharTraits>
typename basic_string<CharType, CharTraits>::iterator
basic_string<CharType, CharTraits>::
insert(const_iterator pos, size_type count, value_type ch)
{
iterator r = const_cast<iterator>(pos);
if (count == 0)
return r;
if (cap_ - size_ < count)
{
return reallocate_and_fill(r, count, ch);
}
if (pos == end())
{
char_traits::fill(end(), ch, count);
size_ += count;
return r;
}
char_traits::move(r + count, r, count);
char_traits::fill(r, ch, count);
size_ += count;
return r;
}
// 在 pos 处插入 [first, last) 内的元素
template <class CharType, class CharTraits>
template <class Iter>
typename basic_string<CharType, CharTraits>::iterator
basic_string<CharType, CharTraits>::
insert(const_iterator pos, Iter first, Iter last)
{
iterator r = const_cast<iterator>(pos);
const size_type len = mystl::distance(first, last);
if (len == 0)
return r;
if (cap_ - size_ < len)
{
return reallocate_and_copy(r, first, last);
}
if (pos == end())
{
mystl::uninitialized_copy(first, last, end());
size_ += len;
return r;
}
char_traits::move(r + len, r, len);
mystl::uninitialized_copy(first, last, r);
size_ += len;
return r;
}
// 在末尾添加 count 个 ch
template <class CharType, class CharTraits>
basic_string<CharType, CharTraits>&
basic_string<CharType, CharTraits>::
append(size_type count, value_type ch)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - count,
"basic_string<Char, Tratis>'s size too big");
if (cap_ - size_ < count)
{
reallocate(count);
}
char_traits::fill(buffer_ + size_, ch, count);
size_ += count;
return *this;
}
// 在末尾添加 [str[pos] str[pos+count]) 一段
template <class CharType, class CharTraits>
basic_string<CharType, CharTraits>&
basic_string<CharType, CharTraits>::
append(const basic_string& str, size_type pos, size_type count)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - count,
"basic_string<Char, Tratis>'s size too big");
if (count == 0)
return *this;
if (cap_ - size_ < count)
{
reallocate(count);
}
char_traits::copy(buffer_ + size_, str.buffer_ + pos, count);
size_ += count;
return *this;
}
// 在末尾添加 [s, s+count) 一段
template <class CharType, class CharTraits>
basic_string<CharType, CharTraits>&
basic_string<CharType, CharTraits>::
append(const_pointer s, size_type count)
{
THROW_LENGTH_ERROR_IF(size_ > max_size() - count,
"basic_string<Char, Tratis>'s size too big");
if (cap_ - size_ < count)
{
reallocate(count);
}
char_traits::copy(buffer_ + size_, s, count);
size_ += count;
return *this;
}
// 删除 pos 处的元素
template <class CharType, class CharTraits>
typename basic_string<CharType, CharTraits>::iterator
basic_string<CharType, CharTraits>::
erase(const_iterator pos)
{
MYSTL_DEBUG(pos != end());
iterator r = const_cast<iterator>(pos);
char_traits::move(r, pos + 1, end() - pos - 1);
--size_;
return r;
}
// 删除 [first, last) 的元素
template <class CharType, class CharTraits>
typename basic_string<CharType, CharTraits>::iterator
basic_string<CharType, CharTraits>::
erase(const_iterator first, const_iterator last)
{
if (first == begin() && last == end())
{
clear();
return end();
}
const size_type n = end() - last;
iterator r = const_cast<iterator>(first);
char_traits::move(r, last, n);
size_ -= (last - first);
return r;
}
// 重置容器大小
template <class CharType, class CharTraits>
void basic_string<CharType, CharTraits>::
resize(size_type count, value_type ch)
{
if (count < size_)
{
erase(buffer_ + count, buffer_ + size_);
}
else
{
append(count - size_, ch);
}
}
// 比较两个 basic_string,小于返回 -1,大于返回 1,等于返回 0
template <class CharType, class CharTraits>
int basic_string<CharType, CharTraits>::
compare(const basic_string& other) const
{
return compare_cstr(buffer_, size_, other.buffer_, other.size_);
}
// 从 pos1 下标开始的 count1 个字符跟另一个 basic_string 比较
template <class CharType, class CharTraits>
int basic_string<CharType, CharTraits>::
compare(size_type pos1, size_type count1, const basic_string& other) const
{
auto n1 = mystl::min(count1, size_ - pos1);
return compare_cstr(buffer_ + pos1, n1, other.buffer_, other.size_);
}
// 从 pos1 下标开始的 count1 个字符跟另一个 basic_string 下标 pos2 开始的 count2 个字符比较
template <class CharType, class CharTraits>
int basic_string<CharType, CharTraits>::