-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic_string.hpp
2153 lines (1778 loc) · 53.2 KB
/
basic_string.hpp
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
#pragma once
#include <memory>
#include <string_view>
#include <array>
#include <concepts>
#include <utility>
#include <cassert>
#include <compare>
#include <stdexcept>
#include <cstring>
#include <iterator>
#include <version>
#include <cwchar>
#if defined(__cpp_if_consteval) && (__cpp_if_consteval >= 202106L)
#define BIZWEN_CONSTEVAL consteval
#else
#define BIZWEN_CONSTEVAL (::std::is_constant_evaluated())
#endif
namespace bizwen
{
template <typename T>
struct is_character: ::std::bool_constant<::std::is_same_v<char, ::std::remove_cv_t<T>> || ::std::is_same_v<signed char, ::std::remove_cv_t<T>> || ::std::is_same_v<unsigned char, ::std::remove_cv_t<T>> || ::std::is_same_v<wchar_t, ::std::remove_cv_t<T>> || ::std::is_same_v<char8_t, ::std::remove_cv_t<T>> || ::std::is_same_v<char16_t, ::std::remove_cv_t<T>> || ::std::is_same_v<char32_t, ::std::remove_cv_t<T>>>
{
};
template <typename T>
constexpr bool is_character_v = is_character<T>::value;
template <typename T>
concept character = is_character_v<T>;
template <character CharT, class Traits = ::std::char_traits<CharT>, class Allocator = ::std::allocator<CharT>>
class alignas(alignof(int*)) basic_string
{
private:
/**
* @brief type of long string
*/
struct ls_type_
{
CharT* begin_{};
CharT* end_{};
CharT* last_{};
};
/**
* @brief short_string_max_ is the max length of short string
*/
static inline constexpr ::std::size_t short_string_max_{ sizeof(CharT*) * 4 / sizeof(CharT) - 2 };
/**
* @brief union storage long string and short string
*/
#pragma pack(1)
union storage_type_
{
::std::array<CharT, short_string_max_ + 1> ss_;
ls_type_ ls_;
};
/**
* https://github.com/microsoft/STL/issues/1364
*/
#ifdef _MSC_VER
[[msvc::no_unique_address]] Allocator allocator_{};
#else
[[no_unique_address]] Allocator allocator_{};
#endif
/**
* @brief storage of string
*/
storage_type_ stor_{};
/**
* @brief flag > 0: short string, length of string is size_flag
* @brief flag = 0: empty string
* @brief flag = MAX: long string, length of string is end - begin
*/
alignas(alignof(CharT)) unsigned char size_flag_{};
using atraits_t_ = ::std::allocator_traits<Allocator>;
static inline char exception_string_[] = "parameter is out of range, please check it.";
constexpr bool is_long_() const noexcept
{
return size_flag_ == decltype(size_flag_)(-1);
}
constexpr bool is_short_() const noexcept
{
return size_flag_ != decltype(size_flag_)(-1);
}
constexpr bool is_empty_() const noexcept
{
return !size_flag_;
}
constexpr ::std::size_t size_() const noexcept
{
if (is_long_())
{
auto&& ls = stor_.ls_;
return ls.end_ - ls.begin_;
}
else
{
return size_flag_;
}
}
public:
using traits_type = Traits;
using value_type = CharT;
using allocator_type = Allocator;
using size_type = typename ::std::allocator_traits<Allocator>::size_type;
using difference_type = typename ::std::allocator_traits<Allocator>::difference_type;
using reference = value_type&;
using const_reference = value_type const&;
using pointer = typename ::std::allocator_traits<Allocator>::pointer;
using const_pointer = typename ::std::allocator_traits<Allocator>::const_pointer;
static inline constexpr size_type npos = -1;
// ********************************* begin volume ******************************
constexpr bool empty() const noexcept
{
return is_empty_();
}
constexpr ::std::size_t size() const noexcept
{
return size_();
}
constexpr ::std::size_t length() const noexcept
{
return size_();
}
/**
* @return size_type{ -1 } / sizeof(CharT) / 2
*/
constexpr size_type max_size() const noexcept
{
return size_type{ -1 } / sizeof(CharT) / 2;
}
constexpr size_type capacity() const noexcept
{
if (is_long_())
{
auto&& ls = stor_.ls_;
return ls.last_ - ls.begin_ - 1;
}
else
{
return short_string_max_;
}
}
/**
* @brief shrink only occur on short string is enough to storage
*/
constexpr void shrink_to_fit() noexcept
{
if (size_() <= short_string_max_ && is_long_())
{
auto ls = stor_.ls_;
if BIZWEN_CONSTEVAL
{
stor_.ss_ = decltype(stor_.ss_){};
::std::copy(ls.begin_, ls.end_, stor_.ss_.data());
}
else
{
::std::memcpy(stor_.ss_.data(), ls.begin_, (ls.end_ - ls.begin_) * sizeof(CharT));
}
dealloc_(ls);
}
}
// ********************************* begin element access ******************************
private:
/**
* @brief internal use
* @return a pointer to the first element
*/
constexpr CharT const* begin_() const noexcept
{
if (is_long_())
return stor_.ls_.begin_;
else
return stor_.ss_.data();
}
/**
* @brief internal use
* @return a pointer to the first element
*/
constexpr CharT* begin_() noexcept
{
return const_cast<CharT*>(const_cast<basic_string const&>(*this).begin_());
}
/**
* @brief internal use
* @return a pointer to the next position of the last element
*/
constexpr CharT const* end_() const noexcept
{
if (is_long_())
return stor_.ls_.end_;
else
return stor_.ss_.data() + size_flag_;
}
/**
* @brief internal use
* @return a pointer to the next position of the last element
*/
constexpr CharT* end_() noexcept
{
return const_cast<CharT*>(const_cast<basic_string const&>(*this).end_());
}
public:
constexpr CharT const* data() const noexcept
{
return begin_();
}
constexpr CharT* data() noexcept
{
return begin_();
}
constexpr CharT const* c_str() const noexcept
{
return begin_();
}
constexpr const_reference at(size_type pos) const
{
if (pos >= size_())
throw ::std::out_of_range{ exception_string_ };
return *(begin_() + pos);
}
constexpr reference at(size_type pos)
{
return const_cast<CharT&>(const_cast<basic_string const&>(*this).at(pos));
}
constexpr const_reference operator[](size_type pos) const noexcept
{
assert(("pos >= size, please check the arg", pos < size_()));
return *(begin_() + pos);
}
constexpr reference operator[](size_type pos) noexcept
{
return const_cast<CharT&>(const_cast<basic_string const&>(*this)[pos]);
}
constexpr const CharT& front() const noexcept
{
assert(("string is empty", !is_empty_()));
return *begin_();
}
constexpr CharT& front()
{
return const_cast<CharT&>(const_cast<basic_string const&>(*this).front());
}
constexpr const CharT& back() const noexcept
{
assert(("string is empty", !is_empty_()));
return *(end_() - 1);
}
constexpr CharT& back()
{
return const_cast<CharT&>(const_cast<basic_string const&>(*this).back());
}
constexpr operator ::std::basic_string_view<CharT, Traits>() const noexcept
{
return ::std::basic_string_view<CharT, Traits>(begin_(), end_());
}
// ********************************* begin iterator type ******************************
private:
struct iterator_type_
{
using difference_type = ::std::ptrdiff_t;
using value_type = CharT;
using pointer = ::std::add_pointer_t<value_type>;
using reference = ::std::add_lvalue_reference_t<CharT>;
using iterator_category = ::std::random_access_iterator_tag;
using iterator_concept = ::std::contiguous_iterator_tag;
private:
CharT* current_{};
#ifndef NDEBUG
basic_string* target_{};
#endif
constexpr void check() const noexcept
{
#ifndef NDEBUG
if (current_ && current_ <= target_->end_() && current_ >= target_->begin_())
;
else
assert(("iterator is invalidated", false));
#endif
}
friend class basic_string;
public:
iterator_type_() noexcept = default;
iterator_type_(iterator_type_ const&) noexcept = default;
iterator_type_(iterator_type_&&) noexcept = default;
iterator_type_& operator=(iterator_type_ const&) & noexcept = default;
iterator_type_& operator=(iterator_type_&&) & noexcept = default;
private:
#ifndef NDEBUG
constexpr iterator_type_(CharT* current, basic_string* target)
: current_(current)
, target_(target)
{
}
#else
constexpr iterator_type_(CharT* current)
: current_(current)
{
}
#endif
public:
constexpr iterator_type_ operator+(difference_type n) const& noexcept
{
auto temp = *this;
temp.current_ += n;
temp.check();
return temp;
}
constexpr iterator_type_ operator-(difference_type n) const& noexcept
{
auto temp = *this;
temp.current_ -= n;
temp.check();
return temp;
}
constexpr friend iterator_type_ operator+(difference_type n, iterator_type_ const& rhs) noexcept
{
auto temp = rhs;
temp.current_ += n;
temp.check();
return temp;
}
constexpr friend iterator_type_ operator-(difference_type n, iterator_type_ const& rhs) noexcept
{
auto temp = rhs;
temp.current_ -= n;
temp.check();
return temp;
}
constexpr friend difference_type operator-(iterator_type_ const& lhs, iterator_type_ const& rhs) noexcept
{
assert(("iter belongs to different strings", lhs.target_ == rhs.target_));
return lhs.current_ - rhs.current_;
}
constexpr iterator_type_& operator+=(difference_type n) & noexcept
{
current_ += n;
check();
return *this;
}
constexpr iterator_type_& operator-=(difference_type n) & noexcept
{
current_ -= n;
check();
return *this;
}
constexpr iterator_type_& operator++() & noexcept
{
++current_;
check();
return *this;
}
constexpr iterator_type_& operator--() & noexcept
{
--current_;
check();
return *this;
}
constexpr iterator_type_ operator++(int) & noexcept
{
iterator_type_ temp{ *this };
++current_;
check();
return temp;
}
constexpr iterator_type_ operator--(int) & noexcept
{
iterator_type_ temp{ *this };
--current_;
check();
return temp;
}
constexpr CharT& operator[](difference_type n) const noexcept
{
#ifndef NDEBUG
iterator_type_ end = (*this) + n;
end.check();
#endif
return *(current_ + n);
}
constexpr CharT& operator*() const noexcept
{
return *current_;
}
constexpr CharT* operator->() const noexcept
{
return current_;
}
friend constexpr ::std::strong_ordering operator<=>(iterator_type_ const&, iterator_type_ const&) noexcept = default;
};
// ********************************* begin iterator function ******************************
public:
using iterator = iterator_type_;
using const_iterator = ::std::basic_const_iterator<iterator_type_>;
using reverse_iterator = ::std::reverse_iterator<iterator>;
using const_reverse_iterator = ::std::reverse_iterator<const_iterator>;
constexpr iterator begin() noexcept
{
#ifndef NDEBUG
return { begin_(), this };
#else
return { begin_() };
#endif
}
constexpr iterator end() noexcept
{
#ifndef NDEBUG
return iterator_type_{ end_(), this };
#else
return iterator_type_{ end_() };
#endif
}
constexpr const_iterator begin() const noexcept
{
#ifndef NDEBUG
return iterator_type_{ const_cast<CharT*>(begin_()), const_cast<basic_string*>(this) };
#else
return iterator_type_{ const_cast<CharT*>(begin_()) };
#endif
}
constexpr const_iterator end() const noexcept
{
#ifndef NDEBUG
return iterator_type_{ const_cast<CharT*>(end_()), const_cast<basic_string*>(this) };
#else
return iterator_type_{ const_cast<CharT*>(end_()) };
#endif
}
constexpr const_iterator cbegin() noexcept
{
return begin();
}
constexpr const_iterator cend() noexcept
{
return end();
}
// ********************************* begin memory management ******************************
private:
/**
* @brief allocates memory and automatically adds 1 to store trailing zero
* @brief strong exception safety guarantee
* @brief not responsible for reclaiming memory
* @brief never shrink
* @param n, expected number of characters
*/
constexpr void allocate_plus_one_(size_type n)
{
// strong exception safe grantee
if (n <= short_string_max_ && !is_long_())
{
size_flag_ = static_cast<signed char>(n);
return;
}
++n;
#if defined(__cpp_lib_allocate_at_least) && (__cpp_lib_allocate_at_least >= 202302L)
auto [ptr, count] = atraits_t_::allocate_at_least(allocator_, n);
stor_.ls_ = { ptr, nullptr, ptr + count };
#else
auto ptr = atraits_t_::allocate(allocator_, n);
stor_.ls_ = { ptr, nullptr, ptr + n };
#endif
size_flag_ = -1;
}
/**
* @brief dealloc the memory of long string
* @brief static member function
* @param ls, allocated long string
*/
constexpr void dealloc_(ls_type_& ls) noexcept
{
atraits_t_::deallocate(allocator_, ls.begin_, ls.last_ - ls.begin_);
}
/**
* @brief conditionally sets size correctly. only legal if n < capacity()
* @brief write 0 to the tail at the same time
* @brief never shrink
* @param n, expected string length
*/
constexpr void resize_(size_type n) noexcept
{
assert(("n > capacity()", n <= capacity()));
if (is_long_())
{
// if n = 0, keep storage avilable
auto&& ls = stor_.ls_;
ls.end_ = ls.begin_ + n;
// return advance
*ls.end_ = CharT{};
}
else
{
size_flag_ = static_cast<signed int>(n);
// gcc thinks it's out of range,
// so make gcc happy
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#endif
stor_.ss_[n] = CharT{};
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
}
}
/**
* @brief fill characters to *this
* @brief not doing anything else
* @param begin begin of characters
* @param end end of characters
*/
constexpr void fill_(CharT const* begin, CharT const* end) noexcept
{
assert(("cannot storage string in current allocated storage", static_cast<size_type>(end - begin) <= capacity()));
if BIZWEN_CONSTEVAL
{
::std::copy(begin, end, begin_());
}
else
{
::std::memmove(begin_(), begin, (end - begin) * sizeof(CharT));
}
}
/**
* @brief caculate the length of c style string
* @param begin begin of characters
* @return length
*/
constexpr static size_type c_style_string_length_(CharT const* begin) noexcept
{
#if defined(__cpp_if_consteval) && (__cpp_if_consteval >= 202106L)
if !consteval
#else
if (!::std::is_constant_evaluated())
#endif
{
if constexpr (::std::is_same_v<char, CharT>)
{
return ::std::strlen(begin);
}
else if constexpr (::std::is_same_v<wchar_t, CharT>)
{
return ::std::wcslen(begin);
}
}
auto end = begin;
for (; *end != CharT{}; ++end)
;
return end - begin;
}
/**
* @brief assign characters to *this
* @brief this function can be called with any legal state
* @brief strong exception safety guarantee
* @brief this function provide for assign and operator=
* @param begin begin of characters
* @param end end of characters
*/
constexpr void assign_(CharT const* first, CharT const* last)
{
auto size = last - first;
if (capacity() < static_cast<size_type>(size))
{
if (is_long_())
{
auto ls = stor_.ls_;
allocate_plus_one_(size);
dealloc_(ls);
}
else
{
allocate_plus_one_(size);
}
}
fill_(first, last);
resize_(size);
}
/**
* @brief insert characters to *this
* @brief this function can be called with any legal state
* @brief strong exception safety guarantee
*/
constexpr void insert_(size_type index, CharT const* first, CharT const* last)
{
auto size = size_();
if (index > size)
throw ::std::out_of_range{ exception_string_ };
auto length = last - first;
auto new_size = size + length;
auto begin = begin_();
auto end = begin + size;
auto start = begin + index;
// if start is not in range and capacity > length + size
if ((start < first || start > last) && capacity() > new_size)
{
if BIZWEN_CONSTEVAL
{
::std::copy_backward(start, end, end + length);
}
else
{
::std::memmove(end, start, length * sizeof(CharT));
}
// re-cacl first and last, because range is in *this
if (first >= start && last <= end)
{
first += index;
last += index;
}
if BIZWEN_CONSTEVAL
{
::std::copy(first, last, start);
}
else
{
::std::memmove(start, first, length * sizeof(CharT));
}
}
else
{
basic_string temp{};
temp.allocate_plus_one_(new_size);
auto temp_begin = temp.begin_();
auto temp_start = temp_begin + index;
auto temp_end = temp_begin + size;
if BIZWEN_CONSTEVAL
{
::std::copy(begin, start, temp_begin);
::std::copy(first, last, temp_start);
::std::copy(start, end, temp_start + length);
}
else
{
::std::memcpy(temp_begin, begin, index * sizeof(CharT));
::std::memcpy(temp_start, first, length * sizeof(CharT));
::std::memcpy(temp_start + length, start, (end - start) * sizeof(CharT));
}
temp.swap(*this);
}
resize_(new_size);
}
constexpr void replace_(size_type pos, size_type count, CharT const* first2, CharT const* last2)
{
auto size = size_();
if (pos > size)
throw ::std::out_of_range{ exception_string_ };
auto begin = begin_();
auto first1 = begin + pos;
auto last1 = begin + ::std::min(pos + count, size);
auto length1 = last1 - first1;
auto length2 = last2 - first2;
auto new_size = size + length1 - length2;
auto end = begin + size;
if (!(last1 < first2 || last2 < first1) && new_size <= capacity())
{
auto diff = length1 - length2;
if BIZWEN_CONSTEVAL
{
if (diff > 0)
::std::copy(last1, end, last1 - diff);
else if (diff < 0)
::std::copy_backward(last1, end, end - diff);
}
else
{
if (diff > 0)
::std::memmove(last1 + diff, last1, diff * sizeof(CharT));
else if (diff < 0)
::std::memmove(last1 - diff, last1, -diff * sizeof(CharT));
}
if (first2 >= last1 && last2 <= end)
{
first2 += diff;
last2 += diff;
}
if BIZWEN_CONSTEVAL
{
::std::copy(first2, last2, first1);
}
else
{
::std::memmove(first1, first2, length2 * sizeof(CharT));
}
}
else
{
basic_string temp{};
temp.allocate_plus_one_(new_size);
auto temp_begin = temp.begin_();
auto temp_start = temp.begin_() + (first1 - begin);
if BIZWEN_CONSTEVAL
{
::std::copy(begin, first1, temp_begin);
::std::copy(first2, last2, temp_start);
::std::copy(last1, end, temp_start + length2);
}
else
{
::std::memcpy(begin, temp_begin, first1 - begin * sizeof(CharT));
::std::memcpy(temp_start, first2, length2 * sizeof(CharT));
::std::memcpy(temp_start + length2, last1, (end - last1) * sizeof(CharT));
}
temp.swap(*this);
}
resize_(new_size);
}
public:
constexpr ~basic_string()
{
if (is_long_())
dealloc_(stor_.ls_);
}
/**
* @brief reserve memory
* @brief strong exception safety guarantee
* @brief never shrink
* @param new_cap new capacity
*/
constexpr void reserve(size_type new_cap)
{
if (capacity() >= new_cap)
return;
if (is_long_())
{
auto ls = stor_.ls_;
allocate_plus_one_(new_cap);
fill_(ls.begin_, ls.end_);
// even though I'm only subtracting two pointers without r/w
// gcc still thinks it's use after free, so make gcc happy
auto size = ls.end_ - ls.begin_;
dealloc_(ls);
// resize_(ls.end_ - ls.begin_);
resize_(size);
}
else
{
auto ss = stor_.ss_;
auto size = size_flag_;
auto data = ss.data();
allocate_plus_one_(new_cap);
fill_(data, data + size);
resize_(size);
}
}
/**
* @brief resize string length
* @brief strong exception safety guarantee
* @brief never shrink
* @param count new size
* @param ch character to fill
*/
constexpr void resize(size_type count, CharT ch)
{
auto size = size_();
if (count > size)
{
reserve(count);
auto begin = begin_();
::std::fill(begin + size, begin + count, ch);
}
resize_(count);
}
/**
* @brief resize string length
* @brief strong exception safety guarantee
* @brief never shrink
* @param count new size
*/
constexpr void resize(size_type count)
{
resize(count, CharT{});
}
/**
* @brief equal to resize(0)
* @brief never shrink
*/
constexpr void clear() noexcept
{
resize_(0);
}
/**
* @brief use size * 1.5 for growth
* @param ch character to fill
*/
constexpr void push_back(CharT ch)
{
auto size = size_();
if (capacity() == size)
reserve(size * 2 - size / 2);
*end_() = ch;
resize_(size + 1);
}
// ********************************* begin swap ******************************
private:
constexpr void swap_without_ator(basic_string& other) noexcept
{
auto&& self = *this;
::std::ranges::swap(self.stor_, other.stor_);
::std::ranges::swap(self.size_flag_, other.size_flag_);
}
public:
constexpr void swap(basic_string& other) noexcept
{
if constexpr (::std::allocator_traits<Allocator>::propagate_on_container_swap::value)
::std::ranges::swap(other.allocator_, other.allocator_);
else
assert(other.allocator_ == allocator_);
other.swap_without_ator(*this);
}
friend void swap(basic_string& self, basic_string& other) noexcept
{
self.swap(other);
}
// ********************************* begin constructor ******************************
public:
constexpr basic_string() noexcept = default;
constexpr basic_string(allocator_type const& a) noexcept
: allocator_(a)
{
}
constexpr basic_string(size_type n, CharT ch, allocator_type const& a = allocator_type())
: allocator_(a)
{
allocate_plus_one_(n);
auto begin = begin_();
auto end = begin + n;
::std::fill(begin, end, ch);
resize_(n);
}
constexpr basic_string(const basic_string& other, size_type pos, size_type count, allocator_type const& a = allocator_type())
: allocator_(a)
{
auto other_size = other.size_();
if (pos > other_size)
throw ::std::out_of_range{ exception_string_ };
count = ::std::min(other_size - pos, count);
allocate_plus_one_(count);
auto start = other.begin_() + pos;
fill_(start, start + count);
resize_(count);
}
constexpr basic_string(const basic_string& other, size_type pos, allocator_type const& a = allocator_type())
: basic_string(other, pos, other.size_() - pos, a)
{
}
constexpr basic_string(const CharT* s, size_type count, allocator_type const& a = allocator_type())
: allocator_(a)
{
allocate_plus_one_(count);
fill_(s, s + count);
resize_(count);