-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathstring.h
More file actions
1953 lines (1778 loc) · 62.3 KB
/
string.h
File metadata and controls
1953 lines (1778 loc) · 62.3 KB
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
namespace fast_io
{
namespace containers
{
namespace details
{
struct
#if __has_cpp_attribute(__gnu__::__may_alias__)
[[__gnu__::__may_alias__]]
#endif
string_model
{
void *begin_ptr;
void *curr_ptr;
void *end_ptr;
};
template <typename T>
struct string_internal
{
T *begin_ptr;
T *curr_ptr;
T *end_ptr;
};
template <typename allocator_type, ::std::integral chtype>
inline constexpr ::fast_io::basic_allocation_least_result<chtype *> string_allocate_init(chtype const *first, ::std::size_t n) noexcept
{
using typed_allocator_type = typed_generic_allocator_adapter<allocator_type, chtype>;
// n is not possible to SIZE_MAX since that would overflow the memory which is not possible
::std::size_t const np1{static_cast<::std::size_t>(n + 1u)};
auto [ptr, allocn]{typed_allocator_type::allocate_at_least(np1)};
*::fast_io::freestanding::non_overlapped_copy_n(first, n, ptr) = 0;
return {ptr, static_cast<::std::size_t>(allocn - 1u)};
}
template <typename allocator_type, ::std::integral chtype>
inline constexpr void string_heap_dilate_uncheck(::fast_io::containers::details::string_internal<chtype> &imp, ::std::size_t rsize) noexcept
{
using untyped_allocator_type = generic_allocator_adapter<allocator_type>;
using typed_allocator_type = typed_generic_allocator_adapter<untyped_allocator_type, chtype>;
::std::size_t const bfsize{static_cast<::std::size_t>(imp.end_ptr - imp.begin_ptr)};
::std::size_t const strsize{static_cast<::std::size_t>(imp.curr_ptr - imp.begin_ptr)};
chtype *ptr;
auto beginptr{imp.begin_ptr};
bool const is_sso{!bfsize};
if (is_sso)
{
beginptr = nullptr;
}
if constexpr (typed_allocator_type::has_reallocate)
{
auto [newptr, newcap] = typed_allocator_type::reallocate_at_least(beginptr, rsize + 1u);
ptr = newptr;
rsize = newcap - 1u;
}
else
{
auto [newptr, newcap] = typed_allocator_type::reallocate_n_at_least(beginptr, bfsize, rsize + 1u);
ptr = newptr;
rsize = newcap - 1u;
}
imp = {ptr, ptr + strsize, ptr + rsize};
}
template <typename allocator_type, ::std::integral chtype>
inline constexpr void string_push_back_heap_grow_twice(::fast_io::containers::details::string_internal<chtype> &imp) noexcept
{
::std::size_t const bfsize{static_cast<::std::size_t>(imp.end_ptr - imp.begin_ptr)};
::std::size_t const bfsizep1{bfsize + 1u};
constexpr ::std::size_t mxsz{SIZE_MAX / sizeof(chtype) / 2u};
if (mxsz < bfsizep1) [[unlikely]]
{
::fast_io::fast_terminate();
}
::std::size_t const bfsizep1mul2{bfsizep1 << 1u};
return ::fast_io::containers::details::string_heap_dilate_uncheck<allocator_type>(imp, bfsizep1mul2);
}
} // namespace details
template <::std::integral chtype, typename alloctype>
class basic_string FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
{
public:
using allocator_type = alloctype;
using char_type = chtype;
using value_type = char_type;
using size_type = ::std::size_t;
using difference_type = ::std::ptrdiff_t;
using pointer = char_type *;
using const_pointer = char_type const *;
using reference = char_type &;
using const_reference = char_type const &;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = ::std::reverse_iterator<iterator>;
using const_reverse_iterator = ::std::reverse_iterator<const_iterator>;
using string_view_type = ::fast_io::containers::basic_string_view<char_type>;
using cstring_view_type = ::fast_io::containers::basic_cstring_view<char_type>;
::fast_io::containers::details::string_internal<char_type> imp;
private:
constexpr void reset_imp() noexcept
{
#if __cpp_constexpr_dynamic_alloc >= 201907L
if (__builtin_is_constant_evaluated())
{
using untyped_allocator_type = generic_allocator_adapter<allocator_type>;
using typed_allocator_type = typed_generic_allocator_adapter<untyped_allocator_type, chtype>;
auto [ptr, cap]{typed_allocator_type::allocate_at_least(2)};
*ptr = 0;
this->imp = {ptr, ptr, ptr + static_cast<size_type>(cap - 1u)};
}
else
#endif
{
char_type *const ncstr{const_cast<char_type *>(null_terminated_c_str_v<char_type>)};
this->imp = {ncstr, ncstr, ncstr};
}
}
public:
inline constexpr basic_string() noexcept
{
this->reset_imp();
}
inline explicit constexpr basic_string(size_type n) noexcept
{
if (!n)
{
this->reset_imp();
}
else
{
using untyped_allocator_type = generic_allocator_adapter<allocator_type>;
using typed_allocator_type = typed_generic_allocator_adapter<untyped_allocator_type, chtype>;
constexpr size_type mx{::std::numeric_limits<size_type>::max()};
if (n == mx)
{
::fast_io::fast_terminate();
}
auto [ptr, newcap]{typed_allocator_type::allocate_zero_at_least(n + 1u)};
this->imp = {ptr, ptr + n, ptr + static_cast<size_type>(newcap - 1u)};
}
}
inline explicit constexpr basic_string(size_type n, char_type ch) noexcept
{
if (!n)
{
this->reset_imp();
}
else
{
using untyped_allocator_type = generic_allocator_adapter<allocator_type>;
using typed_allocator_type = typed_generic_allocator_adapter<untyped_allocator_type, chtype>;
constexpr size_type mx{::std::numeric_limits<size_type>::max()};
if (n == mx)
{
::fast_io::fast_terminate();
}
auto [ptr, cap]{typed_allocator_type::allocate_at_least(n + 1u)};
this->imp = {ptr, ptr + n, ptr + static_cast<size_type>(cap - 1u)};
*::fast_io::freestanding::uninitialized_fill(ptr, ptr + n, ch) = 0;
}
}
private:
inline constexpr void construct_impl(char_type const *otherptr, size_type othern) noexcept
{
if (!othern)
{
this->reset_imp();
}
else
{
auto newres{::fast_io::containers::details::string_allocate_init<allocator_type>(otherptr, othern)};
auto ptrn{newres.ptr + othern};
this->imp = {newres.ptr, ptrn, newres.ptr + newres.count};
}
}
public:
inline explicit constexpr basic_string(char_type const *f, char_type const *e) noexcept
{
this->construct_impl(f, static_cast<size_type>(e - f));
}
inline explicit constexpr basic_string(string_view_type othervw) noexcept
{
this->construct_impl(othervw.data(), othervw.size());
}
inline constexpr const_pointer c_str() const noexcept
{
return imp.begin_ptr;
}
inline constexpr pointer data() noexcept
{
return imp.begin_ptr;
}
inline constexpr const_pointer data() const noexcept
{
return imp.begin_ptr;
}
#if __has_cpp_attribute(__gnu__::__pure__)
[[__gnu__::__pure__]]
#endif
[[nodiscard]]
inline constexpr bool is_empty() const noexcept
{
return imp.begin_ptr == imp.curr_ptr;
}
#if __has_cpp_attribute(__gnu__::__pure__)
[[__gnu__::__pure__]]
#endif
[[nodiscard]]
inline constexpr bool empty() const noexcept
{
return imp.begin_ptr == imp.curr_ptr;
}
#if __has_cpp_attribute(__gnu__::__pure__)
[[__gnu__::__pure__]]
#endif
[[nodiscard]]
inline constexpr size_type size() const noexcept
{
return static_cast<size_type>(imp.curr_ptr - imp.begin_ptr);
}
inline constexpr size_type size_bytes() const noexcept
{
return static_cast<size_type>(imp.curr_ptr - imp.begin_ptr) * sizeof(value_type);
}
inline constexpr size_type capacity() const noexcept
{
return static_cast<size_type>(imp.end_ptr - imp.begin_ptr);
}
inline constexpr size_type capacity_bytes() const noexcept
{
return static_cast<size_type>(imp.end_ptr - imp.begin_ptr) * sizeof(value_type);
}
static inline constexpr size_type max_size() noexcept
{
constexpr size_type n{SIZE_MAX / sizeof(value_type)};
return n;
}
static inline constexpr size_type max_size_bytes() noexcept
{
constexpr size_type n{SIZE_MAX / sizeof(value_type) * sizeof(value_type)};
return n;
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr reference back() noexcept
{
auto begin_ptr{imp.begin_ptr}, curr_ptr{imp.curr_ptr};
if (begin_ptr == curr_ptr) [[unlikely]]
{
::fast_io::fast_terminate();
}
return curr_ptr[-1];
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr const_reference back() const noexcept
{
auto begin_ptr{imp.begin_ptr}, curr_ptr{imp.curr_ptr};
if (begin_ptr == curr_ptr) [[unlikely]]
{
::fast_io::fast_terminate();
}
return curr_ptr[-1];
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr reference front() noexcept
{
auto begin_ptr{imp.begin_ptr}, curr_ptr{imp.curr_ptr};
if (begin_ptr == curr_ptr) [[unlikely]]
{
::fast_io::fast_terminate();
}
return *begin_ptr;
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr const_reference front() const noexcept
{
auto begin_ptr{imp.begin_ptr}, curr_ptr{imp.curr_ptr};
if (begin_ptr == curr_ptr) [[unlikely]]
{
::fast_io::fast_terminate();
}
return *begin_ptr;
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr reference back_unchecked() noexcept
{
return imp.curr_ptr[-1];
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr const_reference back_unchecked() const noexcept
{
return imp.curr_ptr[-1];
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr reference front_unchecked() noexcept
{
return *imp.begin_ptr;
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr const_reference front_unchecked() const noexcept
{
return *imp.begin_ptr;
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr const_reference operator[](size_type pos) const noexcept
{
auto begin_ptr{imp.begin_ptr}, curr_ptr{imp.curr_ptr};
if (static_cast<size_type>(curr_ptr - begin_ptr) <= pos) [[unlikely]]
{
::fast_io::fast_terminate();
}
return begin_ptr[pos];
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr reference operator[](size_type pos) noexcept
{
auto begin_ptr{imp.begin_ptr}, curr_ptr{imp.curr_ptr};
if (static_cast<size_type>(curr_ptr - begin_ptr) <= pos) [[unlikely]]
{
::fast_io::fast_terminate();
}
return begin_ptr[pos];
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr const_reference index_unchecked(size_type pos) const noexcept
{
return imp.begin_ptr[pos];
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
[[nodiscard]]
inline constexpr reference index_unchecked(size_type pos) noexcept
{
return imp.begin_ptr[pos];
}
private:
inline constexpr void moveconstructorcommon(basic_string &&other) noexcept
{
this->imp = other.imp;
other.reset_imp();
}
public:
inline constexpr basic_string(basic_string &&other) noexcept
{
this->moveconstructorcommon(::std::move(other));
}
inline constexpr basic_string &operator=(basic_string &&other) noexcept
{
if (__builtin_addressof(other) == this) [[unlikely]]
{
return *this;
}
this->destroy();
this->moveconstructorcommon(::std::move(other));
return *this;
}
inline constexpr basic_string(basic_string const &other) noexcept
{
auto otherbegin{other.imp.begin_ptr};
auto othercurr{other.imp.curr_ptr};
if (otherbegin == othercurr)
{
this->reset_imp();
return;
}
::std::size_t const stringlength{static_cast<::std::size_t>(othercurr - otherbegin)};
auto [ptr, allocn] = ::fast_io::containers::details::string_allocate_init<allocator_type>(otherbegin, stringlength);
this->imp = {ptr, ptr + stringlength, ptr + allocn};
}
private:
inline constexpr void assign_impl(char_type const *otherptr, ::std::size_t othern) noexcept
{
auto thisbegin{this->imp.begin_ptr};
auto thisend{this->imp.end_ptr};
::std::size_t const thiscap{static_cast<::std::size_t>(thisend - thisbegin)};
if (thiscap < othern)
{
this->destroy();
auto [ptr, allocn] = ::fast_io::containers::details::string_allocate_init<allocator_type>(otherptr, othern);
this->imp.end_ptr = thisend = ((this->imp.curr_ptr = this->imp.begin_ptr = thisbegin = ptr) + allocn);
}
if (thisbegin == thisend) [[unlikely]]
{
return;
}
*(this->imp.curr_ptr = ::fast_io::freestanding::overlapped_copy_n(otherptr, othern, thisbegin)) = 0;
}
public:
inline constexpr void assign(string_view_type myview) noexcept
{
this->assign_impl(myview.data(), myview.size());
}
inline constexpr void assign_characters(size_type n, char_type ch) noexcept
{
auto beginptr{this->imp.begin_ptr};
auto endptr{this->imp.end_ptr};
size_type const thiscap{static_cast<size_type>(endptr - beginptr)};
if (thiscap < n)
{
constexpr size_type mx{::std::numeric_limits<size_type>::max()};
if (n == mx)
{
::fast_io::fast_terminate();
}
this->destroy();
size_type const np1{static_cast<size_type>(n + 1u)};
using untyped_allocator_type = generic_allocator_adapter<allocator_type>;
using typed_allocator_type = typed_generic_allocator_adapter<untyped_allocator_type, chtype>;
auto [ptr, allocn]{typed_allocator_type::allocate_at_least(np1)};
this->imp.end_ptr = endptr = ((this->imp.curr_ptr = this->imp.begin_ptr = beginptr = ptr) + static_cast<size_type>(allocn - 1u));
}
if (beginptr == endptr) [[unlikely]]
{
return;
}
::fast_io::freestanding::uninitialized_fill_n(beginptr, n, ch);
*(this->imp.curr_ptr = beginptr + n) = 0;
}
inline constexpr void assign_characters(size_type n) noexcept
{
this->assign_characters(n, 0);
}
inline constexpr void assign_with_character(char_type ch) noexcept
{
this->assign_characters(1u, ch);
}
inline constexpr basic_string &operator=(basic_string const &other) noexcept
{
if (__builtin_addressof(other) == this) [[unlikely]]
{
return *this;
}
this->assign_impl(other.imp.begin_ptr, static_cast<::std::size_t>(other.imp.curr_ptr - other.imp.begin_ptr));
return *this;
}
inline constexpr basic_string &operator=(string_view_type const &other) noexcept
{
this->assign(other);
return *this;
}
private:
#if __has_cpp_attribute(__gnu__::__cold__)
[[__gnu__::__cold__]]
#endif
inline constexpr pointer insert_cold_impl(pointer insertpos, char_type const *otherptr, size_type othern) noexcept
{
using untyped_allocator_type = generic_allocator_adapter<allocator_type>;
using typed_allocator_type = typed_generic_allocator_adapter<untyped_allocator_type, chtype>;
constexpr size_type mx{::std::numeric_limits<size_type>::max()};
constexpr size_type mxdiv2{::std::numeric_limits<size_type>::max() / 2u};
constexpr size_type mxm1{static_cast<size_type>(mx - 1u)};
auto beginptr{this->imp.begin_ptr}, currptr{this->imp.curr_ptr}, endptr{this->imp.end_ptr};
bool const is_sso{this->imp.begin_ptr == this->imp.end_ptr};
size_type thissize{static_cast<size_type>(currptr - beginptr)};
size_type thiscap{static_cast<size_type>(endptr - beginptr)};
size_type newsize{thissize + othern};
size_type bigcap;
if (mxdiv2 <= thiscap)
{
bigcap = mxm1;
}
else
{
if (thiscap)
{
bigcap = static_cast<size_type>(thiscap << 1u);
}
else
{
bigcap = 1u;
}
}
size_type newcap{newsize};
if (newcap < bigcap)
{
newcap = bigcap;
}
if (newcap == mx)
{
::fast_io::fast_terminate();
}
size_type const newcapp1{static_cast<size_type>(newcap + 1u)};
// Allocate memory with the new capacity
auto [ptr, allocn]{typed_allocator_type::allocate_at_least(newcapp1)};
this->imp.begin_ptr = ptr;
this->imp.end_ptr = ptr + static_cast<size_type>(allocn - 1u);
// Perform the insertion
auto it{ptr};
it = ::fast_io::freestanding::non_overlapped_copy(beginptr, insertpos, it);
auto retit{it};
it = ::fast_io::freestanding::non_overlapped_copy_n(otherptr, othern, it);
it = ::fast_io::freestanding::non_overlapped_copy(insertpos, currptr, it);
*it = 0;
this->imp.curr_ptr = it;
// Deallocate the old memory if it was not small string optimization (SSO)
if (!is_sso)
{
typed_allocator_type::deallocate_n(beginptr, static_cast<size_type>(static_cast<size_type>(endptr - beginptr) + 1u));
}
return retit;
}
#if __has_cpp_attribute(__gnu__::__cold__)
[[__gnu__::__cold__]]
#endif
inline constexpr void append_cold_impl(char_type const *otherptr, size_type othern) noexcept
{
this->insert_cold_impl(this->imp.curr_ptr, otherptr, othern);
}
inline constexpr void append_impl(char_type const *otherptr, size_type othern) noexcept
{
if (!othern)
{
return;
}
auto beginptr{this->imp.begin_ptr}, currptr{this->imp.curr_ptr}, endptr{this->imp.end_ptr};
size_type thissize{static_cast<size_type>(currptr - beginptr)};
size_type thiscap{static_cast<size_type>(endptr - beginptr)};
size_type newsize{thissize + othern};
bool const needreallocate{thiscap < newsize};
if (needreallocate) [[unlikely]]
{
this->append_cold_impl(otherptr, othern);
return;
}
*(this->imp.curr_ptr = ::fast_io::freestanding::overlapped_copy_n(otherptr, othern, currptr)) = 0;
}
public:
inline constexpr void append(string_view_type vw) noexcept
{
this->append_impl(vw.data(), vw.size());
}
inline constexpr void append(basic_string const &other) noexcept
{
this->append_impl(other.data(), other.size());
}
inline constexpr void clear() noexcept
{
if (this->imp.begin_ptr == this->imp.end_ptr) [[unlikely]]
{
return;
}
*(this->imp.curr_ptr = this->imp.begin_ptr) = 0;
}
inline constexpr void clear_destroy() noexcept
{
this->destroy();
this->reset_imp();
}
inline constexpr void push_back_unchecked(char_type ch) noexcept
{
*this->imp.curr_ptr = ch;
*++this->imp.curr_ptr = 0;
}
private:
#if __has_cpp_attribute(__gnu__::__cold__)
[[__gnu__::__cold__]]
#endif
inline constexpr void grow_twice() noexcept
{
::fast_io::containers::details::string_push_back_heap_grow_twice<allocator_type>(this->imp);
}
public:
inline constexpr const_iterator cbegin() const noexcept
{
return this->imp.begin_ptr;
}
inline constexpr const_iterator cend() const noexcept
{
return this->imp.curr_ptr;
}
inline constexpr const_iterator begin() const noexcept
{
return this->imp.begin_ptr;
}
inline constexpr const_iterator end() const noexcept
{
return this->imp.curr_ptr;
}
inline constexpr iterator begin() noexcept
{
return this->imp.begin_ptr;
}
inline constexpr iterator end() noexcept
{
return this->imp.curr_ptr;
}
inline constexpr const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator(this->imp.curr_ptr);
}
inline constexpr const_iterator crend() const noexcept
{
return const_reverse_iterator(this->imp.begin_ptr);
}
inline constexpr const_iterator rbegin() const noexcept
{
return const_reverse_iterator(this->imp.curr_ptr);
}
inline constexpr const_iterator rend() const noexcept
{
return const_reverse_iterator(this->imp.begin_ptr);
}
inline constexpr reverse_iterator rbegin() noexcept
{
return reverse_iterator(this->imp.curr_ptr);
}
inline constexpr reverse_iterator rend() noexcept
{
return reverse_iterator(this->imp.begin_ptr);
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
inline constexpr void push_back(char_type ch) noexcept
{
if (this->imp.curr_ptr == this->imp.end_ptr) [[unlikely]]
{
this->grow_twice();
}
*this->imp.curr_ptr = ch;
*++this->imp.curr_ptr = 0;
}
#if __has_cpp_attribute(__gnu__::__always_inline__)
[[__gnu__::__always_inline__]]
#elif __has_cpp_attribute(msvc::forceinline)
[[msvc::forceinline]]
#endif
inline constexpr void pop_back() noexcept
{
if (this->imp.curr_ptr == this->imp.begin_ptr) [[unlikely]]
{
::fast_io::fast_terminate();
}
*--this->imp.curr_ptr = 0;
}
inline constexpr void pop_back_unchecked() noexcept
{
*--this->imp.curr_ptr = 0;
}
inline constexpr void reserve(size_type new_cap) noexcept
{
auto begin_ptr{this->imp.begin_ptr};
if (new_cap <= static_cast<size_type>(imp.end_ptr - begin_ptr))
{
return;
}
::fast_io::containers::details::string_heap_dilate_uncheck<allocator_type>(this->imp, new_cap);
}
inline constexpr void shrink_to_fit() noexcept
{
if (this->imp.curr_ptr == this->imp.end_ptr)
{
return;
}
::fast_io::containers::details::string_heap_dilate_uncheck<allocator_type>(this->imp, static_cast<size_type>(this->imp.curr_ptr - this->imp.begin_ptr));
}
private:
inline constexpr void destroy() noexcept
{
auto beginptr{this->imp.begin_ptr};
auto endptr{this->imp.end_ptr};
if (beginptr == endptr)
{
return;
}
using untyped_allocator_type = generic_allocator_adapter<allocator_type>;
using typed_allocator_type = typed_generic_allocator_adapter<untyped_allocator_type, chtype>;
typed_allocator_type::deallocate_n(beginptr, static_cast<size_type>(static_cast<size_type>(endptr - beginptr) + 1u));
}
public:
inline constexpr ~basic_string()
{
this->destroy();
}
private:
inline constexpr pointer insert_impl(pointer ptr, char_type const *otherptr, size_type othern) noexcept
{
auto beginptr{this->imp.begin_ptr}, currptr{this->imp.curr_ptr}, endptr{this->imp.end_ptr};
size_type thissize{static_cast<size_type>(currptr - beginptr)};
size_type thiscap{static_cast<size_type>(endptr - beginptr)};
size_type newsize{thissize + othern};
bool const needreallocate{thiscap < newsize};
if (needreallocate) [[unlikely]]
{
return this->insert_cold_impl(ptr, otherptr, othern);
}
if (!thiscap) [[unlikely]]
{
return beginptr;
}
auto newcurrptr{currptr + othern};
*newcurrptr = 0;
auto lastptr{::fast_io::freestanding::copy_backward(ptr, currptr, newcurrptr)};
auto retptr{::fast_io::freestanding::copy_backward(otherptr, otherptr + othern, lastptr)};
this->imp.curr_ptr = newcurrptr;
return retptr;
}
inline constexpr void insert_index_impl(size_type idx, char_type const *otherptr, size_type othern) noexcept
{
auto beginptr{this->imp.begin_ptr};
size_type sz{static_cast<size_type>(this->imp.curr_ptr - beginptr)};
if (sz < idx)
{
::fast_io::fast_terminate();
}
this->insert_impl(beginptr + idx, otherptr, othern);
}
public:
inline constexpr void insert_index(size_type idx, string_view_type vw) noexcept
{
return this->insert_index_impl(idx, vw.data(), vw.size());
}
inline constexpr iterator insert(const_iterator ptr, string_view_type vw) noexcept
{
if (__builtin_is_constant_evaluated())
{
return this->insert_impl(this->imp.begin_ptr + (ptr - this->imp.begin_ptr), vw.data(), vw.size());
}
else
{
return this->insert_impl(const_cast<pointer>(ptr), vw.data(), vw.size());
}
}
inline constexpr void insert_index(size_type idx, basic_string const &other) noexcept
{
return this->insert_index_impl(idx, other.data(), other.size());
}
private:
inline constexpr pointer erase_impl(pointer first, pointer last) noexcept
{
if (this->imp.begin_ptr != this->imp.end_ptr) [[likely]]
{
*(this->imp.curr_ptr = ::fast_io::freestanding::my_copy(last, this->imp.curr_ptr, first)) = 0;
}
return first;
}
inline constexpr pointer erase_impl(pointer first) noexcept
{
return this->erase_impl(first, first + 1);
}
public:
inline constexpr iterator erase(const_iterator first, const_iterator last) noexcept
{
if (__builtin_is_constant_evaluated())
{
auto beginptr{this->imp.begin_ptr};
return this->erase_impl(beginptr + (first - beginptr), beginptr + (last - beginptr));
}
else
{
return this->erase_impl(const_cast<pointer>(first), const_cast<pointer>(last));
}
}
inline constexpr void erase_index(size_type firstidx, size_type lastidx) noexcept
{
auto beginptr{this->imp.begin_ptr};
auto currptr{this->imp.curr_ptr};
size_type const sz{static_cast<size_type>(currptr - beginptr)};
if (lastidx < firstidx || sz < lastidx) [[unlikely]]
{
::fast_io::fast_terminate();
}
this->erase_impl(beginptr + firstidx, beginptr + lastidx);
}
inline constexpr iterator erase(const_iterator it) noexcept
{
if (__builtin_is_constant_evaluated())
{
auto beginptr{this->imp.begin_ptr};
return this->erase_impl(beginptr + (it - beginptr));
}
else
{
return this->erase_impl(const_cast<pointer>(it));
}
}
inline constexpr void erase_index(size_type idx) noexcept
{
auto beginptr{this->imp.begin_ptr};
auto currptr{this->imp.curr_ptr};
size_type const sz{static_cast<size_type>(currptr - beginptr)};
if (sz <= idx) [[unlikely]]
{
::fast_io::fast_terminate();
}
this->erase_impl(beginptr + idx);
}
inline constexpr void swap(basic_string &other) noexcept
{
::std::swap(other.imp, this->imp);
}
inline constexpr iterator insert(const_iterator ptr, basic_string const &other) noexcept
{
if (__builtin_is_constant_evaluated())
{
return this->insert_impl(this->imp.begin_ptr + (ptr - this->imp.begin_ptr), other.data(), other.size());
}
else
{
return this->insert_impl(const_cast<pointer>(ptr), other.data(), other.size());
}
}
private:
#if __has_cpp_attribute(__gnu__::__cold__)
[[__gnu__::__cold__]]
#endif
inline constexpr pointer replace_cold_impl(pointer first, pointer last, const_pointer otherptr, size_type othern) noexcept
{
using untyped_allocator_type = generic_allocator_adapter<allocator_type>;
using typed_allocator_type = typed_generic_allocator_adapter<untyped_allocator_type, chtype>;
constexpr size_type mx{::std::numeric_limits<size_type>::max()};
constexpr size_type mxdiv2{mx >> 1u};
constexpr size_type mxm1{static_cast<size_type>(mx - 1u)};
auto beginptr{this->imp.begin_ptr}, currptr{this->imp.curr_ptr}, endptr{this->imp.end_ptr};
bool const is_sso{beginptr == endptr};
size_type thissize{static_cast<size_type>(currptr - beginptr)};
size_type thiscap{static_cast<size_type>(endptr - beginptr)};
size_type const toremoven{static_cast<size_type>(last - first)};
size_type newsize{thissize + othern - toremoven};
size_type bigcap;
if (mxdiv2 <= thiscap)
{
bigcap = mxm1;
}
else
{
bigcap = static_cast<size_type>(thiscap << 1u);
}
size_type newcap{newsize};
if (newcap < bigcap)
{
newcap = bigcap;
}
if (newcap == mx)
{
::fast_io::fast_terminate();
}
size_type const newcapp1{static_cast<size_type>(newcap + 1u)};
auto [ptr, allocn]{typed_allocator_type::allocate_at_least(newcapp1)};
this->imp.begin_ptr = ptr;
this->imp.end_ptr = ptr + static_cast<size_type>(allocn - 1u);
auto it{ptr};
it = ::fast_io::freestanding::non_overlapped_copy(beginptr, first, it);
auto retit{it};
it = ::fast_io::freestanding::non_overlapped_copy_n(otherptr, othern, it);
it = ::fast_io::freestanding::non_overlapped_copy(last, currptr, it);
*it = 0;
this->imp.curr_ptr = it;
if (!is_sso)
{
typed_allocator_type::deallocate_n(beginptr, static_cast<size_type>(static_cast<size_type>(endptr - beginptr) + 1u));
}
return retit;
}
inline constexpr pointer replace_impl(pointer first, pointer last, const_pointer otherdata, size_type othersize) noexcept