mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
basic_string.h
4746 lines (4367 loc) · 162 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
// Components for manipulating sequences of characters -*- C++ -*-
// Copyright (C) 1997-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file bits/basic_string.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{string}
*/
//
// ISO C++ 14882: 21 Strings library
//
#ifndef _BASIC_STRING_H
#define _BASIC_STRING_H 1
#pragma GCC system_header
#include <ext/alloc_traits.h>
#include <debug/debug.h>
#if __cplusplus >= 201103L
#include <initializer_list>
#endif
#if __cplusplus >= 201703L
# include <string_view>
#endif
#if __cplusplus > 202302L
# include <charconv>
#endif
#include <bits/version.h>
#if ! _GLIBCXX_USE_CXX11_ABI
# include "cow_string.h"
#else
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
_GLIBCXX_BEGIN_NAMESPACE_CXX11
/**
* @class basic_string basic_string.h <string>
* @brief Managing sequences of characters and character-like objects.
*
* @ingroup strings
* @ingroup sequences
* @headerfile string
* @since C++98
*
* @tparam _CharT Type of character
* @tparam _Traits Traits for character type, defaults to
* char_traits<_CharT>.
* @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
*
* Meets the requirements of a <a href="tables.html#65">container</a>, a
* <a href="tables.html#66">reversible container</a>, and a
* <a href="tables.html#67">sequence</a>. Of the
* <a href="tables.html#68">optional sequence requirements</a>, only
* @c push_back, @c at, and @c %array access are supported.
*/
template<typename _CharT, typename _Traits, typename _Alloc>
class basic_string
{
#if __cplusplus >= 202002L
static_assert(is_same_v<_CharT, typename _Traits::char_type>);
static_assert(is_same_v<_CharT, typename _Alloc::value_type>);
using _Char_alloc_type = _Alloc;
#else
typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
rebind<_CharT>::other _Char_alloc_type;
#endif
typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
// Types:
public:
typedef _Traits traits_type;
typedef typename _Traits::char_type value_type;
typedef _Char_alloc_type allocator_type;
typedef typename _Alloc_traits::size_type size_type;
typedef typename _Alloc_traits::difference_type difference_type;
typedef typename _Alloc_traits::reference reference;
typedef typename _Alloc_traits::const_reference const_reference;
typedef typename _Alloc_traits::pointer pointer;
typedef typename _Alloc_traits::const_pointer const_pointer;
typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
const_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
/// Value returned by various member functions when they fail.
static const size_type npos = static_cast<size_type>(-1);
protected:
// type used for positions in insert, erase etc.
#if __cplusplus < 201103L
typedef iterator __const_iterator;
#else
typedef const_iterator __const_iterator;
#endif
private:
static _GLIBCXX20_CONSTEXPR pointer
_S_allocate(_Char_alloc_type& __a, size_type __n)
{
pointer __p = _Alloc_traits::allocate(__a, __n);
#if __glibcxx_constexpr_string >= 201907L
// std::char_traits begins the lifetime of characters,
// but custom traits might not, so do it here.
if constexpr (!is_same_v<_Traits, char_traits<_CharT>>)
if (std::__is_constant_evaluated())
// Begin the lifetime of characters in allocated storage.
for (size_type __i = 0; __i < __n; ++__i)
std::construct_at(__builtin_addressof(__p[__i]));
#endif
return __p;
}
#if __cplusplus >= 201703L
// A helper type for avoiding boiler-plate.
typedef basic_string_view<_CharT, _Traits> __sv_type;
template<typename _Tp, typename _Res>
using _If_sv = enable_if_t<
__and_<is_convertible<const _Tp&, __sv_type>,
__not_<is_convertible<const _Tp*, const basic_string*>>,
__not_<is_convertible<const _Tp&, const _CharT*>>>::value,
_Res>;
// Allows an implicit conversion to __sv_type.
_GLIBCXX20_CONSTEXPR
static __sv_type
_S_to_string_view(__sv_type __svt) noexcept
{ return __svt; }
// Wraps a string_view by explicit conversion and thus
// allows to add an internal constructor that does not
// participate in overload resolution when a string_view
// is provided.
struct __sv_wrapper
{
_GLIBCXX20_CONSTEXPR explicit
__sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
__sv_type _M_sv;
};
/**
* @brief Only internally used: Construct string from a string view
* wrapper.
* @param __svw string view wrapper.
* @param __a Allocator to use.
*/
_GLIBCXX20_CONSTEXPR
explicit
basic_string(__sv_wrapper __svw, const _Alloc& __a)
: basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
#endif
// Use empty-base optimization: http://www.cantrip.org/emptyopt.html
struct _Alloc_hider : allocator_type // TODO check __is_final
{
#if __cplusplus < 201103L
_Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
: allocator_type(__a), _M_p(__dat) { }
#else
_GLIBCXX20_CONSTEXPR
_Alloc_hider(pointer __dat, const _Alloc& __a)
: allocator_type(__a), _M_p(__dat) { }
_GLIBCXX20_CONSTEXPR
_Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
: allocator_type(std::move(__a)), _M_p(__dat) { }
#endif
pointer _M_p; // The actual data.
};
_Alloc_hider _M_dataplus;
size_type _M_string_length;
enum { _S_local_capacity = 15 / sizeof(_CharT) };
union
{
_CharT _M_local_buf[_S_local_capacity + 1];
size_type _M_allocated_capacity;
};
_GLIBCXX20_CONSTEXPR
void
_M_data(pointer __p)
{ _M_dataplus._M_p = __p; }
_GLIBCXX20_CONSTEXPR
void
_M_length(size_type __length)
{ _M_string_length = __length; }
_GLIBCXX20_CONSTEXPR
pointer
_M_data() const
{ return _M_dataplus._M_p; }
_GLIBCXX20_CONSTEXPR
pointer
_M_local_data()
{
#if __cplusplus >= 201103L
return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
#else
return pointer(_M_local_buf);
#endif
}
_GLIBCXX20_CONSTEXPR
const_pointer
_M_local_data() const
{
#if __cplusplus >= 201103L
return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf);
#else
return const_pointer(_M_local_buf);
#endif
}
_GLIBCXX20_CONSTEXPR
void
_M_capacity(size_type __capacity)
{ _M_allocated_capacity = __capacity; }
_GLIBCXX20_CONSTEXPR
void
_M_set_length(size_type __n)
{
_M_length(__n);
traits_type::assign(_M_data()[__n], _CharT());
}
_GLIBCXX20_CONSTEXPR
bool
_M_is_local() const
{
if (_M_data() == _M_local_data())
{
if (_M_string_length > _S_local_capacity)
__builtin_unreachable();
return true;
}
return false;
}
// Create & Destroy
_GLIBCXX20_CONSTEXPR
pointer
_M_create(size_type&, size_type);
_GLIBCXX20_CONSTEXPR
void
_M_dispose()
{
if (!_M_is_local())
_M_destroy(_M_allocated_capacity);
}
_GLIBCXX20_CONSTEXPR
void
_M_destroy(size_type __size) throw()
{ _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
#if __cplusplus < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
// _M_construct_aux is used to implement the 21.3.1 para 15 which
// requires special behaviour if _InIterator is an integral type
template<typename _InIterator>
void
_M_construct_aux(_InIterator __beg, _InIterator __end,
std::__false_type)
{
typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
_M_construct(__beg, __end, _Tag());
}
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 438. Ambiguity in the "do the right thing" clause
template<typename _Integer>
void
_M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
{ _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
void
_M_construct_aux_2(size_type __req, _CharT __c)
{ _M_construct(__req, __c); }
#endif
// For Input Iterators, used in istreambuf_iterators, etc.
template<typename _InIterator>
_GLIBCXX20_CONSTEXPR
void
_M_construct(_InIterator __beg, _InIterator __end,
std::input_iterator_tag);
// For forward_iterators up to random_access_iterators, used for
// string::iterator, _CharT*, etc.
template<typename _FwdIterator>
_GLIBCXX20_CONSTEXPR
void
_M_construct(_FwdIterator __beg, _FwdIterator __end,
std::forward_iterator_tag);
_GLIBCXX20_CONSTEXPR
void
_M_construct(size_type __req, _CharT __c);
_GLIBCXX20_CONSTEXPR
allocator_type&
_M_get_allocator()
{ return _M_dataplus; }
_GLIBCXX20_CONSTEXPR
const allocator_type&
_M_get_allocator() const
{ return _M_dataplus; }
// Ensure that _M_local_buf is the active member of the union.
__attribute__((__always_inline__))
_GLIBCXX14_CONSTEXPR
void
_M_init_local_buf() _GLIBCXX_NOEXCEPT
{
#if __glibcxx_is_constant_evaluated
if (std::is_constant_evaluated())
for (size_type __i = 0; __i <= _S_local_capacity; ++__i)
_M_local_buf[__i] = _CharT();
#endif
}
__attribute__((__always_inline__))
_GLIBCXX14_CONSTEXPR
pointer
_M_use_local_data() _GLIBCXX_NOEXCEPT
{
#if __cpp_lib_is_constant_evaluated
_M_init_local_buf();
#endif
return _M_local_data();
}
private:
#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
// The explicit instantiations in misc-inst.cc require this due to
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
template<typename _Tp, bool _Requires =
!__are_same<_Tp, _CharT*>::__value
&& !__are_same<_Tp, const _CharT*>::__value
&& !__are_same<_Tp, iterator>::__value
&& !__are_same<_Tp, const_iterator>::__value>
struct __enable_if_not_native_iterator
{ typedef basic_string& __type; };
template<typename _Tp>
struct __enable_if_not_native_iterator<_Tp, false> { };
#endif
_GLIBCXX20_CONSTEXPR
size_type
_M_check(size_type __pos, const char* __s) const
{
if (__pos > this->size())
__throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
"this->size() (which is %zu)"),
__s, __pos, this->size());
return __pos;
}
_GLIBCXX20_CONSTEXPR
void
_M_check_length(size_type __n1, size_type __n2, const char* __s) const
{
if (this->max_size() - (this->size() - __n1) < __n2)
__throw_length_error(__N(__s));
}
// NB: _M_limit doesn't check for a bad __pos value.
_GLIBCXX20_CONSTEXPR
size_type
_M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
{
const bool __testoff = __off < this->size() - __pos;
return __testoff ? __off : this->size() - __pos;
}
// True if _Rep and source do not overlap.
bool
_M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
{
return (less<const _CharT*>()(__s, _M_data())
|| less<const _CharT*>()(_M_data() + this->size(), __s));
}
// When __n = 1 way faster than the general multichar
// traits_type::copy/move/assign.
_GLIBCXX20_CONSTEXPR
static void
_S_copy(_CharT* __d, const _CharT* __s, size_type __n)
{
if (__n == 1)
traits_type::assign(*__d, *__s);
else
traits_type::copy(__d, __s, __n);
}
_GLIBCXX20_CONSTEXPR
static void
_S_move(_CharT* __d, const _CharT* __s, size_type __n)
{
if (__n == 1)
traits_type::assign(*__d, *__s);
else
traits_type::move(__d, __s, __n);
}
_GLIBCXX20_CONSTEXPR
static void
_S_assign(_CharT* __d, size_type __n, _CharT __c)
{
if (__n == 1)
traits_type::assign(*__d, __c);
else
traits_type::assign(__d, __n, __c);
}
// _S_copy_chars is a separate template to permit specialization
// to optimize for the common case of pointers as iterators.
template<class _Iterator>
_GLIBCXX20_CONSTEXPR
static void
_S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
{
for (; __k1 != __k2; ++__k1, (void)++__p)
traits_type::assign(*__p, *__k1); // These types are off.
}
_GLIBCXX20_CONSTEXPR
static void
_S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
{ _S_copy_chars(__p, __k1.base(), __k2.base()); }
_GLIBCXX20_CONSTEXPR
static void
_S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
_GLIBCXX_NOEXCEPT
{ _S_copy_chars(__p, __k1.base(), __k2.base()); }
_GLIBCXX20_CONSTEXPR
static void
_S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
{ _S_copy(__p, __k1, __k2 - __k1); }
_GLIBCXX20_CONSTEXPR
static void
_S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
_GLIBCXX_NOEXCEPT
{ _S_copy(__p, __k1, __k2 - __k1); }
_GLIBCXX20_CONSTEXPR
static int
_S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
{
const difference_type __d = difference_type(__n1 - __n2);
if (__d > __gnu_cxx::__numeric_traits<int>::__max)
return __gnu_cxx::__numeric_traits<int>::__max;
else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
return __gnu_cxx::__numeric_traits<int>::__min;
else
return int(__d);
}
_GLIBCXX20_CONSTEXPR
void
_M_assign(const basic_string&);
_GLIBCXX20_CONSTEXPR
void
_M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
size_type __len2);
_GLIBCXX20_CONSTEXPR
void
_M_erase(size_type __pos, size_type __n);
public:
// Construct/copy/destroy:
// NB: We overload ctors in some cases instead of using default
// arguments, per 17.4.4.4 para. 2 item 2.
/**
* @brief Default constructor creates an empty string.
*/
_GLIBCXX20_CONSTEXPR
basic_string()
_GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
: _M_dataplus(_M_local_data())
{
_M_init_local_buf();
_M_set_length(0);
}
/**
* @brief Construct an empty string using allocator @a a.
*/
_GLIBCXX20_CONSTEXPR
explicit
basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
: _M_dataplus(_M_local_data(), __a)
{
_M_init_local_buf();
_M_set_length(0);
}
/**
* @brief Construct string with copy of value of @a __str.
* @param __str Source string.
*/
_GLIBCXX20_CONSTEXPR
basic_string(const basic_string& __str)
: _M_dataplus(_M_local_data(),
_Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
{
_M_construct(__str._M_data(), __str._M_data() + __str.length(),
std::forward_iterator_tag());
}
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2583. no way to supply an allocator for basic_string(str, pos)
/**
* @brief Construct string as copy of a substring.
* @param __str Source string.
* @param __pos Index of first character to copy from.
* @param __a Allocator to use.
*/
_GLIBCXX20_CONSTEXPR
basic_string(const basic_string& __str, size_type __pos,
const _Alloc& __a = _Alloc())
: _M_dataplus(_M_local_data(), __a)
{
const _CharT* __start = __str._M_data()
+ __str._M_check(__pos, "basic_string::basic_string");
_M_construct(__start, __start + __str._M_limit(__pos, npos),
std::forward_iterator_tag());
}
/**
* @brief Construct string as copy of a substring.
* @param __str Source string.
* @param __pos Index of first character to copy from.
* @param __n Number of characters to copy.
*/
_GLIBCXX20_CONSTEXPR
basic_string(const basic_string& __str, size_type __pos,
size_type __n)
: _M_dataplus(_M_local_data())
{
const _CharT* __start = __str._M_data()
+ __str._M_check(__pos, "basic_string::basic_string");
_M_construct(__start, __start + __str._M_limit(__pos, __n),
std::forward_iterator_tag());
}
/**
* @brief Construct string as copy of a substring.
* @param __str Source string.
* @param __pos Index of first character to copy from.
* @param __n Number of characters to copy.
* @param __a Allocator to use.
*/
_GLIBCXX20_CONSTEXPR
basic_string(const basic_string& __str, size_type __pos,
size_type __n, const _Alloc& __a)
: _M_dataplus(_M_local_data(), __a)
{
const _CharT* __start
= __str._M_data() + __str._M_check(__pos, "string::string");
_M_construct(__start, __start + __str._M_limit(__pos, __n),
std::forward_iterator_tag());
}
/**
* @brief Construct string initialized by a character %array.
* @param __s Source character %array.
* @param __n Number of characters to copy.
* @param __a Allocator to use (default is default allocator).
*
* NB: @a __s must have at least @a __n characters, '\\0'
* has no special meaning.
*/
_GLIBCXX20_CONSTEXPR
basic_string(const _CharT* __s, size_type __n,
const _Alloc& __a = _Alloc())
: _M_dataplus(_M_local_data(), __a)
{
// NB: Not required, but considered best practice.
if (__s == 0 && __n > 0)
std::__throw_logic_error(__N("basic_string: "
"construction from null is not valid"));
_M_construct(__s, __s + __n, std::forward_iterator_tag());
}
/**
* @brief Construct string as copy of a C string.
* @param __s Source C string.
* @param __a Allocator to use (default is default allocator).
*/
#if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3076. basic_string CTAD ambiguity
template<typename = _RequireAllocator<_Alloc>>
#endif
_GLIBCXX20_CONSTEXPR
basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
: _M_dataplus(_M_local_data(), __a)
{
// NB: Not required, but considered best practice.
if (__s == 0)
std::__throw_logic_error(__N("basic_string: "
"construction from null is not valid"));
const _CharT* __end = __s + traits_type::length(__s);
_M_construct(__s, __end, forward_iterator_tag());
}
/**
* @brief Construct string as multiple characters.
* @param __n Number of characters.
* @param __c Character to use.
* @param __a Allocator to use (default is default allocator).
*/
#if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3076. basic_string CTAD ambiguity
template<typename = _RequireAllocator<_Alloc>>
#endif
_GLIBCXX20_CONSTEXPR
basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
: _M_dataplus(_M_local_data(), __a)
{ _M_construct(__n, __c); }
#if __cplusplus >= 201103L
/**
* @brief Move construct string.
* @param __str Source string.
*
* The newly-created string contains the exact contents of @a __str.
* @a __str is a valid, but unspecified string.
*/
_GLIBCXX20_CONSTEXPR
basic_string(basic_string&& __str) noexcept
: _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
{
if (__str._M_is_local())
{
_M_init_local_buf();
traits_type::copy(_M_local_buf, __str._M_local_buf,
__str.length() + 1);
}
else
{
_M_data(__str._M_data());
_M_capacity(__str._M_allocated_capacity);
}
// Must use _M_length() here not _M_set_length() because
// basic_stringbuf relies on writing into unallocated capacity so
// we mess up the contents if we put a '\0' in the string.
_M_length(__str.length());
__str._M_data(__str._M_use_local_data());
__str._M_set_length(0);
}
/**
* @brief Construct string from an initializer %list.
* @param __l std::initializer_list of characters.
* @param __a Allocator to use (default is default allocator).
*/
_GLIBCXX20_CONSTEXPR
basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
: _M_dataplus(_M_local_data(), __a)
{ _M_construct(__l.begin(), __l.end(), std::forward_iterator_tag()); }
_GLIBCXX20_CONSTEXPR
basic_string(const basic_string& __str, const _Alloc& __a)
: _M_dataplus(_M_local_data(), __a)
{ _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); }
_GLIBCXX20_CONSTEXPR
basic_string(basic_string&& __str, const _Alloc& __a)
noexcept(_Alloc_traits::_S_always_equal())
: _M_dataplus(_M_local_data(), __a)
{
if (__str._M_is_local())
{
_M_init_local_buf();
traits_type::copy(_M_local_buf, __str._M_local_buf,
__str.length() + 1);
_M_length(__str.length());
__str._M_set_length(0);
}
else if (_Alloc_traits::_S_always_equal()
|| __str.get_allocator() == __a)
{
_M_data(__str._M_data());
_M_length(__str.length());
_M_capacity(__str._M_allocated_capacity);
__str._M_data(__str._M_use_local_data());
__str._M_set_length(0);
}
else
_M_construct(__str.begin(), __str.end(), std::forward_iterator_tag());
}
#endif // C++11
#if __cplusplus >= 202100L
basic_string(nullptr_t) = delete;
basic_string& operator=(nullptr_t) = delete;
#endif // C++23
/**
* @brief Construct string as copy of a range.
* @param __beg Start of range.
* @param __end End of range.
* @param __a Allocator to use (default is default allocator).
*/
#if __cplusplus >= 201103L
template<typename _InputIterator,
typename = std::_RequireInputIter<_InputIterator>>
#else
template<typename _InputIterator>
#endif
_GLIBCXX20_CONSTEXPR
basic_string(_InputIterator __beg, _InputIterator __end,
const _Alloc& __a = _Alloc())
: _M_dataplus(_M_local_data(), __a), _M_string_length(0)
{
#if __cplusplus >= 201103L
_M_construct(__beg, __end, std::__iterator_category(__beg));
#else
typedef typename std::__is_integer<_InputIterator>::__type _Integral;
_M_construct_aux(__beg, __end, _Integral());
#endif
}
#if __cplusplus >= 201703L
/**
* @brief Construct string from a substring of a string_view.
* @param __t Source object convertible to string view.
* @param __pos The index of the first character to copy from __t.
* @param __n The number of characters to copy from __t.
* @param __a Allocator to use.
*/
template<typename _Tp,
typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>>
_GLIBCXX20_CONSTEXPR
basic_string(const _Tp& __t, size_type __pos, size_type __n,
const _Alloc& __a = _Alloc())
: basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
/**
* @brief Construct string from a string_view.
* @param __t Source object convertible to string view.
* @param __a Allocator to use (default is default allocator).
*/
template<typename _Tp, typename = _If_sv<_Tp, void>>
_GLIBCXX20_CONSTEXPR
explicit
basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
: basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
#endif // C++17
/**
* @brief Destroy the string instance.
*/
_GLIBCXX20_CONSTEXPR
~basic_string()
{ _M_dispose(); }
/**
* @brief Assign the value of @a str to this string.
* @param __str Source string.
*/
_GLIBCXX20_CONSTEXPR
basic_string&
operator=(const basic_string& __str)
{
return this->assign(__str);
}
/**
* @brief Copy contents of @a s into this string.
* @param __s Source null-terminated string.
*/
_GLIBCXX20_CONSTEXPR
basic_string&
operator=(const _CharT* __s)
{ return this->assign(__s); }
/**
* @brief Set value to string of length 1.
* @param __c Source character.
*
* Assigning to a character makes this string length 1 and
* (*this)[0] == @a c.
*/
_GLIBCXX20_CONSTEXPR
basic_string&
operator=(_CharT __c)
{
this->assign(1, __c);
return *this;
}
#if __cplusplus >= 201103L
/**
* @brief Move assign the value of @a str to this string.
* @param __str Source string.
*
* The contents of @a str are moved into this string (without copying).
* @a str is a valid, but unspecified string.
*/
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2063. Contradictory requirements for string move assignment
_GLIBCXX20_CONSTEXPR
basic_string&
operator=(basic_string&& __str)
noexcept(_Alloc_traits::_S_nothrow_move())
{
const bool __equal_allocs = _Alloc_traits::_S_always_equal()
|| _M_get_allocator() == __str._M_get_allocator();
if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
&& !__equal_allocs)
{
// Destroy existing storage before replacing allocator.
_M_destroy(_M_allocated_capacity);
_M_data(_M_local_data());
_M_set_length(0);
}
// Replace allocator if POCMA is true.
std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
if (__str._M_is_local())
{
// We've always got room for a short string, just copy it
// (unless this is a self-move, because that would violate the
// char_traits::copy precondition that the ranges don't overlap).
if (__builtin_expect(std::__addressof(__str) != this, true))
{
if (__str.size())
this->_S_copy(_M_data(), __str._M_data(), __str.size());
_M_set_length(__str.size());
}
}
else if (_Alloc_traits::_S_propagate_on_move_assign() || __equal_allocs)
{
// Just move the allocated pointer, our allocator can free it.
pointer __data = nullptr;
size_type __capacity;
if (!_M_is_local())
{
if (__equal_allocs)
{
// __str can reuse our existing storage.
__data = _M_data();
__capacity = _M_allocated_capacity;
}
else // __str can't use it, so free it.
_M_destroy(_M_allocated_capacity);
}
_M_data(__str._M_data());
_M_length(__str.length());
_M_capacity(__str._M_allocated_capacity);
if (__data)
{
__str._M_data(__data);
__str._M_capacity(__capacity);
}
else
__str._M_data(__str._M_local_buf);
}
else // Need to do a deep copy
assign(__str);
__str.clear();
return *this;
}
/**
* @brief Set value to string constructed from initializer %list.
* @param __l std::initializer_list.
*/
_GLIBCXX20_CONSTEXPR
basic_string&
operator=(initializer_list<_CharT> __l)
{
this->assign(__l.begin(), __l.size());
return *this;
}
#endif // C++11
#if __cplusplus >= 201703L
/**
* @brief Set value to string constructed from a string_view.
* @param __svt An object convertible to string_view.
*/
template<typename _Tp>
_GLIBCXX20_CONSTEXPR
_If_sv<_Tp, basic_string&>
operator=(const _Tp& __svt)
{ return this->assign(__svt); }
/**
* @brief Convert to a string_view.
* @return A string_view.
*/
_GLIBCXX20_CONSTEXPR
operator __sv_type() const noexcept
{ return __sv_type(data(), size()); }
#endif // C++17
// Iterators:
/**
* Returns a read/write iterator that points to the first character in
* the %string.
*/
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
iterator
begin() _GLIBCXX_NOEXCEPT
{ return iterator(_M_data()); }
/**
* Returns a read-only (constant) iterator that points to the first
* character in the %string.
*/
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
const_iterator
begin() const _GLIBCXX_NOEXCEPT
{ return const_iterator(_M_data()); }
/**
* Returns a read/write iterator that points one past the last
* character in the %string.
*/
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
iterator
end() _GLIBCXX_NOEXCEPT
{ return iterator(_M_data() + this->size()); }
/**
* Returns a read-only (constant) iterator that points one past the
* last character in the %string.
*/
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
const_iterator
end() const _GLIBCXX_NOEXCEPT
{ return const_iterator(_M_data() + this->size()); }
/**
* Returns a read/write reverse iterator that points to the last
* character in the %string. Iteration is done in reverse element
* order.
*/
_GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
reverse_iterator
rbegin() _GLIBCXX_NOEXCEPT
{ return reverse_iterator(this->end()); }
/**