]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/basic_string.h
Use the counted_by attribute of pointers in array bound checker.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / basic_string.h
CommitLineData
725dc051
BK
1// Components for manipulating sequences of characters -*- C++ -*-
2
6441eb6d 3// Copyright (C) 1997-2025 Free Software Foundation, Inc.
725dc051
BK
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
725dc051
BK
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
725dc051 24
f910786b 25/** @file bits/basic_string.h
729e3d3f 26 * This is an internal header file, included by other library headers.
f910786b 27 * Do not attempt to use it directly. @headername{string}
729e3d3f
PE
28 */
29
143c27b0
BK
30//
31// ISO C++ 14882: 21 Strings library
32//
33
3d7c150e
BK
34#ifndef _BASIC_STRING_H
35#define _BASIC_STRING_H 1
725dc051 36
63a598de 37#ifdef _GLIBCXX_SYSHDR
b0a85b86 38#pragma GCC system_header
63a598de 39#endif
b0a85b86 40
34a2b755 41#include <ext/alloc_traits.h>
285b36d6 42#include <debug/debug.h>
e25d2617 43
734f5023 44#if __cplusplus >= 201103L
988499f4 45#include <initializer_list>
a7d5d7e2 46#endif
725dc051 47
affdef57
JW
48#include <bits/version.h>
49
50#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
51# include <string_view>
52#endif
53
ae54d8cb 54#if __glibcxx_containers_ranges // C++ >= 23
882d3b31
JW
55# include <bits/ranges_algobase.h> // ranges::copy
56# include <bits/ranges_util.h> // ranges::subrange
57#endif
58
aeed687f
JW
59#if __cplusplus > 202302L
60# include <charconv>
61#endif
62
083b7f28 63
7b527614
JW
64#if ! _GLIBCXX_USE_CXX11_ABI
65# include "cow_string.h"
66#else
083b7f28 67
12ffa228
BK
68namespace std _GLIBCXX_VISIBILITY(default)
69{
70_GLIBCXX_BEGIN_NAMESPACE_VERSION
7b527614 71_GLIBCXX_BEGIN_NAMESPACE_CXX11
3cbc7af0 72
34a2b755
JW
73 /**
74 * @class basic_string basic_string.h <string>
75 * @brief Managing sequences of characters and character-like objects.
76 *
77 * @ingroup strings
78 * @ingroup sequences
865869dc
JW
79 * @headerfile string
80 * @since C++98
34a2b755
JW
81 *
82 * @tparam _CharT Type of character
83 * @tparam _Traits Traits for character type, defaults to
84 * char_traits<_CharT>.
85 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
86 *
87 * Meets the requirements of a <a href="tables.html#65">container</a>, a
88 * <a href="tables.html#66">reversible container</a>, and a
89 * <a href="tables.html#67">sequence</a>. Of the
90 * <a href="tables.html#68">optional sequence requirements</a>, only
91 * @c push_back, @c at, and @c %array access are supported.
92 */
93 template<typename _CharT, typename _Traits, typename _Alloc>
94 class basic_string
95 {
c62e9454 96#if __cplusplus >= 202002L
00ee8818
GA
97 static_assert(is_trivially_copyable_v<_CharT>
98 && is_trivially_default_constructible_v<_CharT>
99 && is_standard_layout_v<_CharT>);
c62e9454
JW
100 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
101 static_assert(is_same_v<_CharT, typename _Alloc::value_type>);
102 using _Char_alloc_type = _Alloc;
103#else
34a2b755
JW
104 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
105 rebind<_CharT>::other _Char_alloc_type;
c62e9454 106#endif
2d76292b 107
34a2b755
JW
108 typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
109
110 // Types:
111 public:
112 typedef _Traits traits_type;
113 typedef typename _Traits::char_type value_type;
114 typedef _Char_alloc_type allocator_type;
115 typedef typename _Alloc_traits::size_type size_type;
116 typedef typename _Alloc_traits::difference_type difference_type;
117 typedef typename _Alloc_traits::reference reference;
118 typedef typename _Alloc_traits::const_reference const_reference;
119 typedef typename _Alloc_traits::pointer pointer;
120 typedef typename _Alloc_traits::const_pointer const_pointer;
121 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
122 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
123 const_iterator;
124 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
125 typedef std::reverse_iterator<iterator> reverse_iterator;
126
127 /// Value returned by various member functions when they fail.
128 static const size_type npos = static_cast<size_type>(-1);
129
3eb1eda1 130 protected:
34a2b755
JW
131 // type used for positions in insert, erase etc.
132#if __cplusplus < 201103L
133 typedef iterator __const_iterator;
134#else
135 typedef const_iterator __const_iterator;
136#endif
137
3eb1eda1 138 private:
c62e9454
JW
139 static _GLIBCXX20_CONSTEXPR pointer
140 _S_allocate(_Char_alloc_type& __a, size_type __n)
141 {
142 pointer __p = _Alloc_traits::allocate(__a, __n);
7ffa63df 143#if __glibcxx_constexpr_string >= 201907L
c62e9454
JW
144 // std::char_traits begins the lifetime of characters,
145 // but custom traits might not, so do it here.
146 if constexpr (!is_same_v<_Traits, char_traits<_CharT>>)
147 if (std::__is_constant_evaluated())
148 // Begin the lifetime of characters in allocated storage.
149 for (size_type __i = 0; __i < __n; ++__i)
150 std::construct_at(__builtin_addressof(__p[__i]));
151#endif
152 return __p;
153 }
154
affdef57 155#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
156 // A helper type for avoiding boiler-plate.
157 typedef basic_string_view<_CharT, _Traits> __sv_type;
92daf2de
JW
158
159 template<typename _Tp, typename _Res>
160 using _If_sv = enable_if_t<
161 __and_<is_convertible<const _Tp&, __sv_type>,
4cf5930f 162 __not_<is_convertible<const _Tp*, const basic_string*>>,
92daf2de
JW
163 __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
164 _Res>;
df66af3b
DK
165
166 // Allows an implicit conversion to __sv_type.
b96e2ff9 167 _GLIBCXX20_CONSTEXPR
df66af3b
DK
168 static __sv_type
169 _S_to_string_view(__sv_type __svt) noexcept
170 { return __svt; }
171
172 // Wraps a string_view by explicit conversion and thus
173 // allows to add an internal constructor that does not
174 // participate in overload resolution when a string_view
175 // is provided.
176 struct __sv_wrapper
177 {
b96e2ff9
ML
178 _GLIBCXX20_CONSTEXPR explicit
179 __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
180
df66af3b
DK
181 __sv_type _M_sv;
182 };
1a289fa3
JW
183
184 /**
185 * @brief Only internally used: Construct string from a string view
186 * wrapper.
187 * @param __svw string view wrapper.
188 * @param __a Allocator to use.
189 */
b96e2ff9 190 _GLIBCXX20_CONSTEXPR
1a289fa3
JW
191 explicit
192 basic_string(__sv_wrapper __svw, const _Alloc& __a)
193 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
ca8f2cb1
VV
194#endif
195
34a2b755
JW
196 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
197 struct _Alloc_hider : allocator_type // TODO check __is_final
198 {
8cab3d18 199#if __cplusplus < 201103L
34a2b755
JW
200 _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
201 : allocator_type(__a), _M_p(__dat) { }
8cab3d18 202#else
b96e2ff9 203 _GLIBCXX20_CONSTEXPR
8cab3d18
JW
204 _Alloc_hider(pointer __dat, const _Alloc& __a)
205 : allocator_type(__a), _M_p(__dat) { }
206
b96e2ff9 207 _GLIBCXX20_CONSTEXPR
8cab3d18
JW
208 _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
209 : allocator_type(std::move(__a)), _M_p(__dat) { }
210#endif
34a2b755
JW
211
212 pointer _M_p; // The actual data.
213 };
214
215 _Alloc_hider _M_dataplus;
216 size_type _M_string_length;
217
59aa28e8 218 enum { _S_local_capacity = 15 / sizeof(_CharT) };
34a2b755
JW
219
220 union
221 {
222 _CharT _M_local_buf[_S_local_capacity + 1];
223 size_type _M_allocated_capacity;
224 };
225
b96e2ff9 226 _GLIBCXX20_CONSTEXPR
34a2b755
JW
227 void
228 _M_data(pointer __p)
229 { _M_dataplus._M_p = __p; }
230
b96e2ff9 231 _GLIBCXX20_CONSTEXPR
34a2b755
JW
232 void
233 _M_length(size_type __length)
234 { _M_string_length = __length; }
235
b96e2ff9 236 _GLIBCXX20_CONSTEXPR
34a2b755
JW
237 pointer
238 _M_data() const
239 { return _M_dataplus._M_p; }
240
b96e2ff9 241 _GLIBCXX20_CONSTEXPR
34a2b755
JW
242 pointer
243 _M_local_data()
244 {
245#if __cplusplus >= 201103L
246 return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
247#else
248 return pointer(_M_local_buf);
249#endif
250 }
251
b96e2ff9 252 _GLIBCXX20_CONSTEXPR
34a2b755
JW
253 const_pointer
254 _M_local_data() const
255 {
256#if __cplusplus >= 201103L
257 return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf);
258#else
259 return const_pointer(_M_local_buf);
260#endif
261 }
262
b96e2ff9 263 _GLIBCXX20_CONSTEXPR
34a2b755
JW
264 void
265 _M_capacity(size_type __capacity)
266 { _M_allocated_capacity = __capacity; }
267
b96e2ff9 268 _GLIBCXX20_CONSTEXPR
34a2b755
JW
269 void
270 _M_set_length(size_type __n)
271 {
272 _M_length(__n);
273 traits_type::assign(_M_data()[__n], _CharT());
274 }
275
b96e2ff9 276 _GLIBCXX20_CONSTEXPR
34a2b755
JW
277 bool
278 _M_is_local() const
bf78b438
JW
279 {
280 if (_M_data() == _M_local_data())
281 {
282 if (_M_string_length > _S_local_capacity)
283 __builtin_unreachable();
284 return true;
285 }
286 return false;
287 }
34a2b755
JW
288
289 // Create & Destroy
b96e2ff9 290 _GLIBCXX20_CONSTEXPR
34a2b755
JW
291 pointer
292 _M_create(size_type&, size_type);
293
b96e2ff9 294 _GLIBCXX20_CONSTEXPR
34a2b755
JW
295 void
296 _M_dispose()
297 {
298 if (!_M_is_local())
299 _M_destroy(_M_allocated_capacity);
300 }
301
b96e2ff9 302 _GLIBCXX20_CONSTEXPR
34a2b755
JW
303 void
304 _M_destroy(size_type __size) throw()
305 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
306
5a9572e4 307#if __cplusplus < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
34a2b755
JW
308 // _M_construct_aux is used to implement the 21.3.1 para 15 which
309 // requires special behaviour if _InIterator is an integral type
310 template<typename _InIterator>
311 void
312 _M_construct_aux(_InIterator __beg, _InIterator __end,
313 std::__false_type)
314 {
315 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
316 _M_construct(__beg, __end, _Tag());
317 }
318
319 // _GLIBCXX_RESOLVE_LIB_DEFECTS
320 // 438. Ambiguity in the "do the right thing" clause
321 template<typename _Integer>
322 void
323 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
324 { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
325
326 void
327 _M_construct_aux_2(size_type __req, _CharT __c)
328 { _M_construct(__req, __c); }
5a9572e4 329#endif
34a2b755
JW
330
331 // For Input Iterators, used in istreambuf_iterators, etc.
332 template<typename _InIterator>
b96e2ff9 333 _GLIBCXX20_CONSTEXPR
34a2b755
JW
334 void
335 _M_construct(_InIterator __beg, _InIterator __end,
336 std::input_iterator_tag);
337
338 // For forward_iterators up to random_access_iterators, used for
339 // string::iterator, _CharT*, etc.
340 template<typename _FwdIterator>
b96e2ff9 341 _GLIBCXX20_CONSTEXPR
34a2b755
JW
342 void
343 _M_construct(_FwdIterator __beg, _FwdIterator __end,
344 std::forward_iterator_tag);
345
b96e2ff9 346 _GLIBCXX20_CONSTEXPR
34a2b755
JW
347 void
348 _M_construct(size_type __req, _CharT __c);
349
9c5505a3
JH
350 // Construct using block of memory of known size.
351 // If _Terminated is true assume that source is already 0 terminated.
352 template<bool _Terminated>
353 _GLIBCXX20_CONSTEXPR
354 void
355 _M_construct(const _CharT *__c, size_type __n);
356
b96e2ff9 357 _GLIBCXX20_CONSTEXPR
34a2b755
JW
358 allocator_type&
359 _M_get_allocator()
360 { return _M_dataplus; }
361
b96e2ff9 362 _GLIBCXX20_CONSTEXPR
34a2b755
JW
363 const allocator_type&
364 _M_get_allocator() const
365 { return _M_dataplus; }
366
6afa1083
JW
367 // Ensure that _M_local_buf is the active member of the union.
368 __attribute__((__always_inline__))
369 _GLIBCXX14_CONSTEXPR
405a4140
JW
370 void
371 _M_init_local_buf() _GLIBCXX_NOEXCEPT
6afa1083 372 {
7ffa63df 373#if __glibcxx_is_constant_evaluated
74d14778 374 if (std::is_constant_evaluated())
52672be7
NS
375 for (size_type __i = 0; __i <= _S_local_capacity; ++__i)
376 _M_local_buf[__i] = _CharT();
405a4140
JW
377#endif
378 }
379
380 __attribute__((__always_inline__))
381 _GLIBCXX14_CONSTEXPR
382 pointer
383 _M_use_local_data() _GLIBCXX_NOEXCEPT
384 {
385#if __cpp_lib_is_constant_evaluated
386 _M_init_local_buf();
6afa1083
JW
387#endif
388 return _M_local_data();
389 }
390
34a2b755
JW
391 private:
392
393#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
394 // The explicit instantiations in misc-inst.cc require this due to
395 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
396 template<typename _Tp, bool _Requires =
397 !__are_same<_Tp, _CharT*>::__value
398 && !__are_same<_Tp, const _CharT*>::__value
399 && !__are_same<_Tp, iterator>::__value
400 && !__are_same<_Tp, const_iterator>::__value>
401 struct __enable_if_not_native_iterator
402 { typedef basic_string& __type; };
403 template<typename _Tp>
404 struct __enable_if_not_native_iterator<_Tp, false> { };
405#endif
406
b96e2ff9 407 _GLIBCXX20_CONSTEXPR
34a2b755
JW
408 size_type
409 _M_check(size_type __pos, const char* __s) const
410 {
411 if (__pos > this->size())
412 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
413 "this->size() (which is %zu)"),
414 __s, __pos, this->size());
415 return __pos;
416 }
417
b96e2ff9 418 _GLIBCXX20_CONSTEXPR
34a2b755
JW
419 void
420 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
421 {
422 if (this->max_size() - (this->size() - __n1) < __n2)
423 __throw_length_error(__N(__s));
424 }
425
426
427 // NB: _M_limit doesn't check for a bad __pos value.
b96e2ff9 428 _GLIBCXX20_CONSTEXPR
34a2b755
JW
429 size_type
430 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
431 {
432 const bool __testoff = __off < this->size() - __pos;
433 return __testoff ? __off : this->size() - __pos;
434 }
435
436 // True if _Rep and source do not overlap.
437 bool
438 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
439 {
2d60da10
JW
440 return (less<const _CharT*>()(__s, _M_data())
441 || less<const _CharT*>()(_M_data() + this->size(), __s));
34a2b755
JW
442 }
443
444 // When __n = 1 way faster than the general multichar
445 // traits_type::copy/move/assign.
b96e2ff9 446 _GLIBCXX20_CONSTEXPR
34a2b755
JW
447 static void
448 _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
449 {
450 if (__n == 1)
451 traits_type::assign(*__d, *__s);
452 else
453 traits_type::copy(__d, __s, __n);
454 }
455
b96e2ff9 456 _GLIBCXX20_CONSTEXPR
34a2b755
JW
457 static void
458 _S_move(_CharT* __d, const _CharT* __s, size_type __n)
459 {
460 if (__n == 1)
461 traits_type::assign(*__d, *__s);
462 else
463 traits_type::move(__d, __s, __n);
464 }
465
b96e2ff9 466 _GLIBCXX20_CONSTEXPR
34a2b755
JW
467 static void
468 _S_assign(_CharT* __d, size_type __n, _CharT __c)
469 {
470 if (__n == 1)
471 traits_type::assign(*__d, __c);
472 else
473 traits_type::assign(__d, __n, __c);
474 }
475
648d5c26
JW
476#pragma GCC diagnostic push
477#pragma GCC diagnostic ignored "-Wc++17-extensions"
34a2b755
JW
478 // _S_copy_chars is a separate template to permit specialization
479 // to optimize for the common case of pointers as iterators.
480 template<class _Iterator>
b96e2ff9 481 _GLIBCXX20_CONSTEXPR
34a2b755
JW
482 static void
483 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
34a2b755 484 {
648d5c26
JW
485#if __cplusplus >= 201103L
486 using _IterBase = decltype(std::__niter_base(__k1));
487 if constexpr (__or_<is_same<_IterBase, _CharT*>,
488 is_same<_IterBase, const _CharT*>>::value)
489 _S_copy(__p, std::__niter_base(__k1), __k2 - __k1);
490#if __cpp_lib_concepts
05d3aebe
JW
491 else if constexpr (requires {
492 requires contiguous_iterator<_Iterator>;
493 { std::to_address(__k1) }
494 -> convertible_to<const _CharT*>;
495 })
648d5c26
JW
496 {
497 const auto __d = __k2 - __k1;
498 (void) (__k1 + __d); // See P3349R1
499 _S_copy(__p, std::to_address(__k1), static_cast<size_type>(__d));
500 }
501#endif
502 else
503#endif
f970a17d 504 for (; __k1 != __k2; ++__k1, (void)++__p)
05d3aebe 505 traits_type::assign(*__p, static_cast<_CharT>(*__k1));
34a2b755 506 }
648d5c26 507#pragma GCC diagnostic pop
34a2b755 508
648d5c26 509#if __cplusplus < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
34a2b755 510 static void
648d5c26 511 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
34a2b755
JW
512 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
513
514 static void
515 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
34a2b755
JW
516 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
517
518 static void
648d5c26 519 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
34a2b755
JW
520 { _S_copy(__p, __k1, __k2 - __k1); }
521
522 static void
523 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
34a2b755 524 { _S_copy(__p, __k1, __k2 - __k1); }
648d5c26 525#endif
34a2b755 526
ae54d8cb 527#if __glibcxx_containers_ranges // C++ >= 23
882d3b31
JW
528 // pre: __n == ranges::distance(__rg). __p+[0,__n) is a valid range.
529 template<typename _Rg>
530 static constexpr void
531 _S_copy_range(pointer __p, _Rg&& __rg, size_type __n)
532 {
05d3aebe
JW
533 if constexpr (requires {
534 requires ranges::contiguous_range<_Rg>;
535 { ranges::data(std::forward<_Rg>(__rg)) }
536 -> convertible_to<const _CharT*>;
537 })
882d3b31
JW
538 _S_copy(__p, ranges::data(std::forward<_Rg>(__rg)), __n);
539 else
05d3aebe
JW
540 {
541 auto __first = ranges::begin(__rg);
542 const auto __last = ranges::end(__rg);
543 for (; __first != __last; ++__first)
544 traits_type::assign(*__p++, static_cast<_CharT>(*__first));
545 }
882d3b31
JW
546 }
547#endif
548
b96e2ff9 549 _GLIBCXX20_CONSTEXPR
34a2b755
JW
550 static int
551 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
552 {
553 const difference_type __d = difference_type(__n1 - __n2);
554
555 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
556 return __gnu_cxx::__numeric_traits<int>::__max;
557 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
558 return __gnu_cxx::__numeric_traits<int>::__min;
559 else
560 return int(__d);
561 }
562
b96e2ff9 563 _GLIBCXX20_CONSTEXPR
34a2b755 564 void
a7d47f35 565 _M_assign(const basic_string&);
34a2b755 566
b96e2ff9 567 _GLIBCXX20_CONSTEXPR
34a2b755
JW
568 void
569 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
570 size_type __len2);
571
b96e2ff9 572 _GLIBCXX20_CONSTEXPR
34a2b755
JW
573 void
574 _M_erase(size_type __pos, size_type __n);
575
576 public:
577 // Construct/copy/destroy:
578 // NB: We overload ctors in some cases instead of using default
579 // arguments, per 17.4.4.4 para. 2 item 2.
580
581 /**
582 * @brief Default constructor creates an empty string.
583 */
b96e2ff9 584 _GLIBCXX20_CONSTEXPR
bcb896ab 585 basic_string()
5caff414 586 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
8b8e5ed6
JW
587#if __cpp_concepts && __glibcxx_type_trait_variable_templates
588 requires is_default_constructible_v<_Alloc>
589#endif
34a2b755 590 : _M_dataplus(_M_local_data())
2d76292b 591 {
405a4140 592 _M_init_local_buf();
2d76292b
JW
593 _M_set_length(0);
594 }
34a2b755
JW
595
596 /**
597 * @brief Construct an empty string using allocator @a a.
598 */
b96e2ff9 599 _GLIBCXX20_CONSTEXPR
34a2b755 600 explicit
5caff414 601 basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
34a2b755 602 : _M_dataplus(_M_local_data(), __a)
2d76292b 603 {
405a4140 604 _M_init_local_buf();
2d76292b
JW
605 _M_set_length(0);
606 }
34a2b755
JW
607
608 /**
609 * @brief Construct string with copy of value of @a __str.
610 * @param __str Source string.
611 */
b96e2ff9 612 _GLIBCXX20_CONSTEXPR
34a2b755 613 basic_string(const basic_string& __str)
5caff414
JW
614 : _M_dataplus(_M_local_data(),
615 _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
5a9572e4 616 {
9c5505a3 617 _M_construct<true>(__str._M_data(), __str.length());
5a9572e4 618 }
34a2b755 619
86bbf15b
JW
620 // _GLIBCXX_RESOLVE_LIB_DEFECTS
621 // 2583. no way to supply an allocator for basic_string(str, pos)
34a2b755
JW
622 /**
623 * @brief Construct string as copy of a substring.
624 * @param __str Source string.
625 * @param __pos Index of first character to copy from.
86bbf15b 626 * @param __a Allocator to use.
34a2b755 627 */
b96e2ff9 628 _GLIBCXX20_CONSTEXPR
34a2b755 629 basic_string(const basic_string& __str, size_type __pos,
86bbf15b
JW
630 const _Alloc& __a = _Alloc())
631 : _M_dataplus(_M_local_data(), __a)
632 {
633 const _CharT* __start = __str._M_data()
634 + __str._M_check(__pos, "basic_string::basic_string");
5a9572e4
JW
635 _M_construct(__start, __start + __str._M_limit(__pos, npos),
636 std::forward_iterator_tag());
86bbf15b
JW
637 }
638
639 /**
640 * @brief Construct string as copy of a substring.
641 * @param __str Source string.
642 * @param __pos Index of first character to copy from.
643 * @param __n Number of characters to copy.
644 */
b96e2ff9 645 _GLIBCXX20_CONSTEXPR
86bbf15b
JW
646 basic_string(const basic_string& __str, size_type __pos,
647 size_type __n)
34a2b755
JW
648 : _M_dataplus(_M_local_data())
649 {
2d60da10 650 const _CharT* __start = __str._M_data()
34a2b755 651 + __str._M_check(__pos, "basic_string::basic_string");
5a9572e4
JW
652 _M_construct(__start, __start + __str._M_limit(__pos, __n),
653 std::forward_iterator_tag());
34a2b755
JW
654 }
655
656 /**
657 * @brief Construct string as copy of a substring.
658 * @param __str Source string.
659 * @param __pos Index of first character to copy from.
86bbf15b 660 * @param __n Number of characters to copy.
34a2b755
JW
661 * @param __a Allocator to use.
662 */
b96e2ff9 663 _GLIBCXX20_CONSTEXPR
34a2b755
JW
664 basic_string(const basic_string& __str, size_type __pos,
665 size_type __n, const _Alloc& __a)
666 : _M_dataplus(_M_local_data(), __a)
667 {
2d60da10
JW
668 const _CharT* __start
669 = __str._M_data() + __str._M_check(__pos, "string::string");
5a9572e4
JW
670 _M_construct(__start, __start + __str._M_limit(__pos, __n),
671 std::forward_iterator_tag());
34a2b755
JW
672 }
673
674 /**
675 * @brief Construct string initialized by a character %array.
676 * @param __s Source character %array.
677 * @param __n Number of characters to copy.
678 * @param __a Allocator to use (default is default allocator).
679 *
680 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
681 * has no special meaning.
682 */
b96e2ff9 683 _GLIBCXX20_CONSTEXPR
34a2b755
JW
684 basic_string(const _CharT* __s, size_type __n,
685 const _Alloc& __a = _Alloc())
686 : _M_dataplus(_M_local_data(), __a)
5a9572e4
JW
687 {
688 // NB: Not required, but considered best practice.
689 if (__s == 0 && __n > 0)
690 std::__throw_logic_error(__N("basic_string: "
691 "construction from null is not valid"));
692 _M_construct(__s, __s + __n, std::forward_iterator_tag());
693 }
34a2b755
JW
694
695 /**
696 * @brief Construct string as copy of a C string.
697 * @param __s Source C string.
698 * @param __a Allocator to use (default is default allocator).
699 */
5d84e6c5
JW
700#if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
701 // _GLIBCXX_RESOLVE_LIB_DEFECTS
702 // 3076. basic_string CTAD ambiguity
703 template<typename = _RequireAllocator<_Alloc>>
704#endif
b96e2ff9 705 _GLIBCXX20_CONSTEXPR
34a2b755
JW
706 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
707 : _M_dataplus(_M_local_data(), __a)
789c57bc 708 {
5a9572e4
JW
709 // NB: Not required, but considered best practice.
710 if (__s == 0)
711 std::__throw_logic_error(__N("basic_string: "
712 "construction from null is not valid"));
713 const _CharT* __end = __s + traits_type::length(__s);
714 _M_construct(__s, __end, forward_iterator_tag());
789c57bc 715 }
34a2b755
JW
716
717 /**
718 * @brief Construct string as multiple characters.
719 * @param __n Number of characters.
720 * @param __c Character to use.
721 * @param __a Allocator to use (default is default allocator).
722 */
5d84e6c5
JW
723#if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
724 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725 // 3076. basic_string CTAD ambiguity
726 template<typename = _RequireAllocator<_Alloc>>
727#endif
b96e2ff9 728 _GLIBCXX20_CONSTEXPR
34a2b755
JW
729 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
730 : _M_dataplus(_M_local_data(), __a)
731 { _M_construct(__n, __c); }
732
733#if __cplusplus >= 201103L
734 /**
735 * @brief Move construct string.
736 * @param __str Source string.
737 *
738 * The newly-created string contains the exact contents of @a __str.
739 * @a __str is a valid, but unspecified string.
252c9967 740 */
b96e2ff9 741 _GLIBCXX20_CONSTEXPR
34a2b755
JW
742 basic_string(basic_string&& __str) noexcept
743 : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
744 {
745 if (__str._M_is_local())
746 {
405a4140 747 _M_init_local_buf();
faa7d78e 748 traits_type::copy(_M_local_buf, __str._M_local_buf,
b96e2ff9 749 __str.length() + 1);
34a2b755
JW
750 }
751 else
752 {
753 _M_data(__str._M_data());
754 _M_capacity(__str._M_allocated_capacity);
755 }
756
757 // Must use _M_length() here not _M_set_length() because
758 // basic_stringbuf relies on writing into unallocated capacity so
759 // we mess up the contents if we put a '\0' in the string.
760 _M_length(__str.length());
28adad7a 761 __str._M_data(__str._M_use_local_data());
34a2b755
JW
762 __str._M_set_length(0);
763 }
764
ae54d8cb 765#if __glibcxx_containers_ranges // C++ >= 23
882d3b31
JW
766 /**
767 * @brief Construct a string from a range.
768 * @since C++23
769 */
770 template<__detail::__container_compatible_range<_CharT> _Rg>
771 constexpr
772 basic_string(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
773 : basic_string(__a)
774 {
775 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
776 {
777 const auto __n = static_cast<size_type>(ranges::distance(__rg));
778 reserve(__n);
779 _S_copy_range(_M_data(), std::forward<_Rg>(__rg), __n);
780 _M_set_length(__n);
781 }
782 else
783 {
784 auto __first = ranges::begin(__rg);
785 const auto __last = ranges::end(__rg);
786 for (; __first != __last; ++__first)
787 push_back(*__first);
788 }
789 }
790#endif
791
34a2b755
JW
792 /**
793 * @brief Construct string from an initializer %list.
794 * @param __l std::initializer_list of characters.
795 * @param __a Allocator to use (default is default allocator).
796 */
b96e2ff9 797 _GLIBCXX20_CONSTEXPR
34a2b755
JW
798 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
799 : _M_dataplus(_M_local_data(), __a)
5a9572e4 800 { _M_construct(__l.begin(), __l.end(), std::forward_iterator_tag()); }
34a2b755 801
b96e2ff9 802 _GLIBCXX20_CONSTEXPR
34a2b755
JW
803 basic_string(const basic_string& __str, const _Alloc& __a)
804 : _M_dataplus(_M_local_data(), __a)
5a9572e4 805 { _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); }
34a2b755 806
b96e2ff9 807 _GLIBCXX20_CONSTEXPR
34a2b755 808 basic_string(basic_string&& __str, const _Alloc& __a)
5caff414 809 noexcept(_Alloc_traits::_S_always_equal())
34a2b755
JW
810 : _M_dataplus(_M_local_data(), __a)
811 {
5caff414
JW
812 if (__str._M_is_local())
813 {
405a4140 814 _M_init_local_buf();
5caff414 815 traits_type::copy(_M_local_buf, __str._M_local_buf,
b96e2ff9 816 __str.length() + 1);
5caff414
JW
817 _M_length(__str.length());
818 __str._M_set_length(0);
819 }
820 else if (_Alloc_traits::_S_always_equal()
821 || __str.get_allocator() == __a)
822 {
823 _M_data(__str._M_data());
824 _M_length(__str.length());
825 _M_capacity(__str._M_allocated_capacity);
28adad7a 826 __str._M_data(__str._M_use_local_data());
5caff414
JW
827 __str._M_set_length(0);
828 }
34a2b755 829 else
5a9572e4 830 _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag());
34a2b755 831 }
fe3ed885 832#endif // C++11
34a2b755 833
fe3ed885 834#if __cplusplus >= 202100L
cf876562
JW
835 basic_string(nullptr_t) = delete;
836 basic_string& operator=(nullptr_t) = delete;
fe3ed885 837#endif // C++23
34a2b755
JW
838
839 /**
840 * @brief Construct string as copy of a range.
841 * @param __beg Start of range.
842 * @param __end End of range.
843 * @param __a Allocator to use (default is default allocator).
844 */
845#if __cplusplus >= 201103L
846 template<typename _InputIterator,
847 typename = std::_RequireInputIter<_InputIterator>>
848#else
849 template<typename _InputIterator>
850#endif
b96e2ff9 851 _GLIBCXX20_CONSTEXPR
34a2b755
JW
852 basic_string(_InputIterator __beg, _InputIterator __end,
853 const _Alloc& __a = _Alloc())
cbf6c7a1 854 : _M_dataplus(_M_local_data(), __a), _M_string_length(0)
5a9572e4
JW
855 {
856#if __cplusplus >= 201103L
857 _M_construct(__beg, __end, std::__iterator_category(__beg));
858#else
859 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
860 _M_construct_aux(__beg, __end, _Integral());
861#endif
862 }
34a2b755 863
affdef57 864#ifdef __glibcxx_string_view // >= C++17
bf56b0b8
JW
865 /**
866 * @brief Construct string from a substring of a string_view.
df66af3b 867 * @param __t Source object convertible to string view.
bf56b0b8
JW
868 * @param __pos The index of the first character to copy from __t.
869 * @param __n The number of characters to copy from __t.
870 * @param __a Allocator to use.
871 */
6aa0859a
JW
872 template<typename _Tp,
873 typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>>
b96e2ff9 874 _GLIBCXX20_CONSTEXPR
bf56b0b8
JW
875 basic_string(const _Tp& __t, size_type __pos, size_type __n,
876 const _Alloc& __a = _Alloc())
df66af3b 877 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
bf56b0b8 878
ca8f2cb1
VV
879 /**
880 * @brief Construct string from a string_view.
df66af3b 881 * @param __t Source object convertible to string view.
ca8f2cb1
VV
882 * @param __a Allocator to use (default is default allocator).
883 */
df66af3b 884 template<typename _Tp, typename = _If_sv<_Tp, void>>
b96e2ff9 885 _GLIBCXX20_CONSTEXPR
df66af3b
DK
886 explicit
887 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
888 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
ca8f2cb1
VV
889#endif // C++17
890
34a2b755
JW
891 /**
892 * @brief Destroy the string instance.
893 */
b96e2ff9 894 _GLIBCXX20_CONSTEXPR
34a2b755
JW
895 ~basic_string()
896 { _M_dispose(); }
897
898 /**
899 * @brief Assign the value of @a str to this string.
900 * @param __str Source string.
901 */
b96e2ff9 902 _GLIBCXX20_CONSTEXPR
34a2b755
JW
903 basic_string&
904 operator=(const basic_string& __str)
5caff414 905 {
11d10beb 906 return this->assign(__str);
5caff414 907 }
34a2b755
JW
908
909 /**
910 * @brief Copy contents of @a s into this string.
911 * @param __s Source null-terminated string.
912 */
b96e2ff9 913 _GLIBCXX20_CONSTEXPR
34a2b755
JW
914 basic_string&
915 operator=(const _CharT* __s)
916 { return this->assign(__s); }
917
918 /**
919 * @brief Set value to string of length 1.
920 * @param __c Source character.
921 *
922 * Assigning to a character makes this string length 1 and
923 * (*this)[0] == @a c.
924 */
b96e2ff9 925 _GLIBCXX20_CONSTEXPR
34a2b755
JW
926 basic_string&
927 operator=(_CharT __c)
928 {
929 this->assign(1, __c);
930 return *this;
931 }
932
933#if __cplusplus >= 201103L
934 /**
935 * @brief Move assign the value of @a str to this string.
936 * @param __str Source string.
937 *
938 * The contents of @a str are moved into this string (without copying).
939 * @a str is a valid, but unspecified string.
252c9967 940 */
34a2b755
JW
941 // _GLIBCXX_RESOLVE_LIB_DEFECTS
942 // 2063. Contradictory requirements for string move assignment
b96e2ff9 943 _GLIBCXX20_CONSTEXPR
34a2b755
JW
944 basic_string&
945 operator=(basic_string&& __str)
5caff414 946 noexcept(_Alloc_traits::_S_nothrow_move())
34a2b755 947 {
540a22d2
FD
948 const bool __equal_allocs = _Alloc_traits::_S_always_equal()
949 || _M_get_allocator() == __str._M_get_allocator();
5caff414 950 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
540a22d2 951 && !__equal_allocs)
5caff414
JW
952 {
953 // Destroy existing storage before replacing allocator.
954 _M_destroy(_M_allocated_capacity);
955 _M_data(_M_local_data());
956 _M_set_length(0);
957 }
958 // Replace allocator if POCMA is true.
959 std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
960
71e09389
JW
961 if (__str._M_is_local())
962 {
c2fb0a1a
JW
963 // We've always got room for a short string, just copy it
964 // (unless this is a self-move, because that would violate the
965 // char_traits::copy precondition that the ranges don't overlap).
966 if (__builtin_expect(std::__addressof(__str) != this, true))
967 {
968 if (__str.size())
969 this->_S_copy(_M_data(), __str._M_data(), __str.size());
970 _M_set_length(__str.size());
971 }
71e09389 972 }
540a22d2 973 else if (_Alloc_traits::_S_propagate_on_move_assign() || __equal_allocs)
5caff414 974 {
71e09389 975 // Just move the allocated pointer, our allocator can free it.
5caff414
JW
976 pointer __data = nullptr;
977 size_type __capacity;
978 if (!_M_is_local())
979 {
540a22d2 980 if (__equal_allocs)
5caff414 981 {
71e09389 982 // __str can reuse our existing storage.
5caff414
JW
983 __data = _M_data();
984 __capacity = _M_allocated_capacity;
985 }
71e09389 986 else // __str can't use it, so free it.
5caff414
JW
987 _M_destroy(_M_allocated_capacity);
988 }
989
990 _M_data(__str._M_data());
991 _M_length(__str.length());
992 _M_capacity(__str._M_allocated_capacity);
993 if (__data)
994 {
995 __str._M_data(__data);
996 __str._M_capacity(__capacity);
997 }
998 else
065dddc6 999 __str._M_data(__str._M_use_local_data());
5caff414 1000 }
71e09389 1001 else // Need to do a deep copy
c07cf418 1002 _M_assign(__str);
5caff414 1003 __str.clear();
34a2b755
JW
1004 return *this;
1005 }
1006
1007 /**
1008 * @brief Set value to string constructed from initializer %list.
1009 * @param __l std::initializer_list.
1010 */
b96e2ff9 1011 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1012 basic_string&
1013 operator=(initializer_list<_CharT> __l)
1014 {
1015 this->assign(__l.begin(), __l.size());
1016 return *this;
1017 }
1018#endif // C++11
1019
affdef57 1020#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
1021 /**
1022 * @brief Set value to string constructed from a string_view.
df66af3b 1023 * @param __svt An object convertible to string_view.
ca8f2cb1 1024 */
df66af3b 1025 template<typename _Tp>
b96e2ff9 1026 _GLIBCXX20_CONSTEXPR
df66af3b
DK
1027 _If_sv<_Tp, basic_string&>
1028 operator=(const _Tp& __svt)
1029 { return this->assign(__svt); }
ca8f2cb1
VV
1030
1031 /**
1032 * @brief Convert to a string_view.
1033 * @return A string_view.
1034 */
b96e2ff9 1035 _GLIBCXX20_CONSTEXPR
ca8f2cb1 1036 operator __sv_type() const noexcept
df66af3b 1037 { return __sv_type(data(), size()); }
ca8f2cb1
VV
1038#endif // C++17
1039
34a2b755
JW
1040 // Iterators:
1041 /**
1042 * Returns a read/write iterator that points to the first character in
1043 * the %string.
1044 */
b911ca42 1045 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1046 iterator
1047 begin() _GLIBCXX_NOEXCEPT
1048 { return iterator(_M_data()); }
1049
1050 /**
1051 * Returns a read-only (constant) iterator that points to the first
1052 * character in the %string.
1053 */
b911ca42 1054 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1055 const_iterator
1056 begin() const _GLIBCXX_NOEXCEPT
1057 { return const_iterator(_M_data()); }
1058
1059 /**
1060 * Returns a read/write iterator that points one past the last
1061 * character in the %string.
1062 */
b911ca42 1063 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1064 iterator
1065 end() _GLIBCXX_NOEXCEPT
1066 { return iterator(_M_data() + this->size()); }
1067
1068 /**
1069 * Returns a read-only (constant) iterator that points one past the
1070 * last character in the %string.
1071 */
b911ca42 1072 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1073 const_iterator
1074 end() const _GLIBCXX_NOEXCEPT
1075 { return const_iterator(_M_data() + this->size()); }
1076
1077 /**
1078 * Returns a read/write reverse iterator that points to the last
1079 * character in the %string. Iteration is done in reverse element
1080 * order.
1081 */
b911ca42 1082 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1083 reverse_iterator
1084 rbegin() _GLIBCXX_NOEXCEPT
1085 { return reverse_iterator(this->end()); }
1086
1087 /**
1088 * Returns a read-only (constant) reverse iterator that points
1089 * to the last character in the %string. Iteration is done in
1090 * reverse element order.
1091 */
b911ca42 1092 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1093 const_reverse_iterator
1094 rbegin() const _GLIBCXX_NOEXCEPT
1095 { return const_reverse_iterator(this->end()); }
1096
1097 /**
1098 * Returns a read/write reverse iterator that points to one before the
1099 * first character in the %string. Iteration is done in reverse
1100 * element order.
1101 */
b911ca42 1102 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1103 reverse_iterator
1104 rend() _GLIBCXX_NOEXCEPT
1105 { return reverse_iterator(this->begin()); }
1106
1107 /**
1108 * Returns a read-only (constant) reverse iterator that points
1109 * to one before the first character in the %string. Iteration
1110 * is done in reverse element order.
1111 */
b911ca42 1112 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1113 const_reverse_iterator
1114 rend() const _GLIBCXX_NOEXCEPT
1115 { return const_reverse_iterator(this->begin()); }
1116
1117#if __cplusplus >= 201103L
1118 /**
1119 * Returns a read-only (constant) iterator that points to the first
1120 * character in the %string.
1121 */
b911ca42 1122 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1123 const_iterator
1124 cbegin() const noexcept
1125 { return const_iterator(this->_M_data()); }
1126
1127 /**
1128 * Returns a read-only (constant) iterator that points one past the
1129 * last character in the %string.
1130 */
b911ca42 1131 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1132 const_iterator
1133 cend() const noexcept
1134 { return const_iterator(this->_M_data() + this->size()); }
1135
1136 /**
1137 * Returns a read-only (constant) reverse iterator that points
1138 * to the last character in the %string. Iteration is done in
1139 * reverse element order.
1140 */
b911ca42 1141 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1142 const_reverse_iterator
1143 crbegin() const noexcept
1144 { return const_reverse_iterator(this->end()); }
1145
1146 /**
1147 * Returns a read-only (constant) reverse iterator that points
1148 * to one before the first character in the %string. Iteration
1149 * is done in reverse element order.
1150 */
b911ca42 1151 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1152 const_reverse_iterator
1153 crend() const noexcept
1154 { return const_reverse_iterator(this->begin()); }
1155#endif
1156
1157 public:
1158 // Capacity:
1159 /// Returns the number of characters in the string, not including any
1160 /// null-termination.
b911ca42 1161 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1162 size_type
1163 size() const _GLIBCXX_NOEXCEPT
1046c32d
JH
1164 {
1165 size_type __sz = _M_string_length;
1166 if (__sz > max_size ())
0ec53064 1167 __builtin_unreachable();
1046c32d
JH
1168 return __sz;
1169 }
34a2b755
JW
1170
1171 /// Returns the number of characters in the string, not including any
1172 /// null-termination.
b911ca42 1173 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1174 size_type
1175 length() const _GLIBCXX_NOEXCEPT
1046c32d 1176 { return size(); }
34a2b755
JW
1177
1178 /// Returns the size() of the largest possible %string.
b911ca42 1179 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1180 size_type
1181 max_size() const _GLIBCXX_NOEXCEPT
1046c32d
JH
1182 {
1183 const size_t __diffmax
1184 = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_CharT);
1185 const size_t __allocmax = _Alloc_traits::max_size(_M_get_allocator());
1186 return (std::min)(__diffmax, __allocmax) - 1;
1187 }
34a2b755
JW
1188
1189 /**
1190 * @brief Resizes the %string to the specified number of characters.
1191 * @param __n Number of characters the %string should contain.
1192 * @param __c Character to fill any new elements.
1193 *
1194 * This function will %resize the %string to the specified
1195 * number of characters. If the number is smaller than the
1196 * %string's current size the %string is truncated, otherwise
1197 * the %string is extended and new elements are %set to @a __c.
1198 */
b96e2ff9 1199 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1200 void
1201 resize(size_type __n, _CharT __c);
1202
1203 /**
1204 * @brief Resizes the %string to the specified number of characters.
1205 * @param __n Number of characters the %string should contain.
1206 *
1207 * This function will resize the %string to the specified length. If
1208 * the new size is smaller than the %string's current size the %string
1209 * is truncated, otherwise the %string is extended and new characters
1210 * are default-constructed. For basic types such as char, this means
1211 * setting them to 0.
1212 */
b96e2ff9 1213 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1214 void
1215 resize(size_type __n)
1216 { this->resize(__n, _CharT()); }
1217
1218#if __cplusplus >= 201103L
140cf935
AL
1219#pragma GCC diagnostic push
1220#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
34a2b755 1221 /// A non-binding request to reduce capacity() to size().
b96e2ff9 1222 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1223 void
1224 shrink_to_fit() noexcept
140cf935
AL
1225 { reserve(); }
1226#pragma GCC diagnostic pop
34a2b755
JW
1227#endif
1228
7ffa63df 1229#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
dfa85bee
JW
1230 /** Resize the string and call a function to fill it.
1231 *
1232 * @param __n The maximum size requested.
1233 * @param __op A callable object that writes characters to the string.
1234 *
1235 * This is a low-level function that is easy to misuse, be careful.
1236 *
1237 * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n`
1238 * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`,
1239 * and finally set the string length to `n2` (adding a null terminator
1240 * at the end). The function object `op` is allowed to write to the
1241 * extra capacity added by the initial reserve operation, which is not
1242 * allowed if you just call `str.reserve(n)` yourself.
1243 *
1244 * This can be used to efficiently fill a `string` buffer without the
1245 * overhead of zero-initializing characters that will be overwritten
1246 * anyway.
1247 *
1248 * The callable `op` must not access the string directly (only through
1249 * the pointer passed as its first argument), must not write more than
1250 * `n` characters to the string, must return a value no greater than `n`,
1251 * and must ensure that all characters up to the returned length are
1252 * valid after it returns (i.e. there must be no uninitialized values
1253 * left in the string after the call, because accessing them would
1254 * have undefined behaviour). If `op` exits by throwing an exception
1255 * the behaviour is undefined.
1256 *
1257 * @since C++23
1258 */
929abc7f
JW
1259 template<typename _Operation>
1260 constexpr void
1261 resize_and_overwrite(size_type __n, _Operation __op);
95c2b0cc
JW
1262#endif
1263
1264#if __cplusplus >= 201103L
1265 /// Non-standard version of resize_and_overwrite for C++11 and above.
1266 template<typename _Operation>
1267 _GLIBCXX20_CONSTEXPR void
1268 __resize_and_overwrite(size_type __n, _Operation __op);
929abc7f
JW
1269#endif
1270
34a2b755
JW
1271 /**
1272 * Returns the total number of characters that the %string can hold
1273 * before needing to allocate more memory.
1274 */
b911ca42 1275 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1276 size_type
1277 capacity() const _GLIBCXX_NOEXCEPT
1278 {
1046c32d
JH
1279 size_t __sz = _M_is_local() ? size_type(_S_local_capacity)
1280 : _M_allocated_capacity;
1281 if (__sz < _S_local_capacity || __sz > max_size ())
0ec53064 1282 __builtin_unreachable();
1046c32d 1283 return __sz;
34a2b755
JW
1284 }
1285
1286 /**
1287 * @brief Attempt to preallocate enough memory for specified number of
1288 * characters.
1289 * @param __res_arg Number of characters required.
1290 * @throw std::length_error If @a __res_arg exceeds @c max_size().
1291 *
1292 * This function attempts to reserve enough memory for the
1293 * %string to hold the specified number of characters. If the
1294 * number requested is more than max_size(), length_error is
1295 * thrown.
1296 *
1297 * The advantage of this function is that if optimal code is a
1298 * necessity and the user can determine the string length that will be
1299 * required, the user can reserve the memory in %advance, and thus
1300 * prevent a possible reallocation of memory and copying of %string
1301 * data.
1302 */
b96e2ff9 1303 _GLIBCXX20_CONSTEXPR
34a2b755 1304 void
140cf935
AL
1305 reserve(size_type __res_arg);
1306
1307 /**
1308 * Equivalent to shrink_to_fit().
1309 */
1310#if __cplusplus > 201703L
1311 [[deprecated("use shrink_to_fit() instead")]]
1312#endif
b96e2ff9 1313 _GLIBCXX20_CONSTEXPR
140cf935
AL
1314 void
1315 reserve();
34a2b755
JW
1316
1317 /**
1318 * Erases the string, making it empty.
1319 */
b96e2ff9 1320 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1321 void
1322 clear() _GLIBCXX_NOEXCEPT
1323 { _M_set_length(0); }
1324
1325 /**
45ab93d9 1326 * Returns true if the %string is empty. Equivalent to
34a2b755
JW
1327 * <code>*this == ""</code>.
1328 */
b96e2ff9
ML
1329 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1330 bool
34a2b755 1331 empty() const _GLIBCXX_NOEXCEPT
1046c32d 1332 { return _M_string_length == 0; }
34a2b755
JW
1333
1334 // Element access:
1335 /**
1336 * @brief Subscript access to the data contained in the %string.
1337 * @param __pos The index of the character to access.
1338 * @return Read-only (constant) reference to the character.
1339 *
1340 * This operator allows for easy, array-style, data access.
1341 * Note that data access with this operator is unchecked and
1342 * out_of_range lookups are not defined. (For checked lookups
1343 * see at().)
1344 */
b911ca42 1345 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1346 const_reference
1347 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
1348 {
2f1e8e7c 1349 __glibcxx_assert(__pos <= size());
34a2b755
JW
1350 return _M_data()[__pos];
1351 }
1352
1353 /**
1354 * @brief Subscript access to the data contained in the %string.
1355 * @param __pos The index of the character to access.
1356 * @return Read/write reference to the character.
1357 *
1358 * This operator allows for easy, array-style, data access.
1359 * Note that data access with this operator is unchecked and
1360 * out_of_range lookups are not defined. (For checked lookups
1361 * see at().)
1362 */
b911ca42 1363 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1364 reference
1365 operator[](size_type __pos)
1366 {
1367 // Allow pos == size() both in C++98 mode, as v3 extension,
1368 // and in C++11 mode.
2f1e8e7c 1369 __glibcxx_assert(__pos <= size());
34a2b755
JW
1370 // In pedantic mode be strict in C++98 mode.
1371 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1372 return _M_data()[__pos];
1373 }
1374
1375 /**
1376 * @brief Provides access to the data contained in the %string.
1377 * @param __n The index of the character to access.
1378 * @return Read-only (const) reference to the character.
1379 * @throw std::out_of_range If @a n is an invalid index.
1380 *
1381 * This function provides for safer data access. The parameter is
1382 * first checked that it is in the range of the string. The function
1383 * throws out_of_range if the check fails.
1384 */
b911ca42 1385 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1386 const_reference
1387 at(size_type __n) const
1388 {
1389 if (__n >= this->size())
1390 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1391 "(which is %zu) >= this->size() "
1392 "(which is %zu)"),
1393 __n, this->size());
1394 return _M_data()[__n];
1395 }
1396
1397 /**
1398 * @brief Provides access to the data contained in the %string.
1399 * @param __n The index of the character to access.
1400 * @return Read/write reference to the character.
1401 * @throw std::out_of_range If @a n is an invalid index.
1402 *
1403 * This function provides for safer data access. The parameter is
1404 * first checked that it is in the range of the string. The function
1405 * throws out_of_range if the check fails.
1406 */
b911ca42 1407 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1408 reference
1409 at(size_type __n)
1410 {
1411 if (__n >= size())
1412 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1413 "(which is %zu) >= this->size() "
1414 "(which is %zu)"),
1415 __n, this->size());
1416 return _M_data()[__n];
1417 }
1418
1419#if __cplusplus >= 201103L
1420 /**
1421 * Returns a read/write reference to the data at the first
1422 * element of the %string.
1423 */
b911ca42 1424 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1425 reference
1426 front() noexcept
e25d2617 1427 {
2f1e8e7c 1428 __glibcxx_assert(!empty());
e25d2617
FD
1429 return operator[](0);
1430 }
34a2b755
JW
1431
1432 /**
1433 * Returns a read-only (constant) reference to the data at the first
1434 * element of the %string.
1435 */
b911ca42 1436 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1437 const_reference
1438 front() const noexcept
e25d2617 1439 {
2f1e8e7c 1440 __glibcxx_assert(!empty());
e25d2617
FD
1441 return operator[](0);
1442 }
34a2b755
JW
1443
1444 /**
1445 * Returns a read/write reference to the data at the last
1446 * element of the %string.
1447 */
b911ca42 1448 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1449 reference
1450 back() noexcept
e25d2617 1451 {
2f1e8e7c 1452 __glibcxx_assert(!empty());
e25d2617
FD
1453 return operator[](this->size() - 1);
1454 }
34a2b755
JW
1455
1456 /**
1457 * Returns a read-only (constant) reference to the data at the
1458 * last element of the %string.
1459 */
b911ca42 1460 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
1461 const_reference
1462 back() const noexcept
e25d2617 1463 {
2f1e8e7c 1464 __glibcxx_assert(!empty());
e25d2617
FD
1465 return operator[](this->size() - 1);
1466 }
34a2b755
JW
1467#endif
1468
1469 // Modifiers:
1470 /**
1471 * @brief Append a string to this string.
1472 * @param __str The string to append.
1473 * @return Reference to this string.
1474 */
b96e2ff9 1475 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1476 basic_string&
1477 operator+=(const basic_string& __str)
1478 { return this->append(__str); }
1479
1480 /**
1481 * @brief Append a C string.
1482 * @param __s The C string to append.
1483 * @return Reference to this string.
1484 */
b96e2ff9 1485 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1486 basic_string&
1487 operator+=(const _CharT* __s)
1488 { return this->append(__s); }
1489
1490 /**
1491 * @brief Append a character.
1492 * @param __c The character to append.
1493 * @return Reference to this string.
1494 */
b96e2ff9 1495 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1496 basic_string&
1497 operator+=(_CharT __c)
1498 {
1499 this->push_back(__c);
1500 return *this;
1501 }
1502
1503#if __cplusplus >= 201103L
1504 /**
1505 * @brief Append an initializer_list of characters.
1506 * @param __l The initializer_list of characters to be appended.
1507 * @return Reference to this string.
1508 */
b96e2ff9 1509 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1510 basic_string&
1511 operator+=(initializer_list<_CharT> __l)
1512 { return this->append(__l.begin(), __l.size()); }
1513#endif // C++11
1514
affdef57 1515#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
1516 /**
1517 * @brief Append a string_view.
df66af3b 1518 * @param __svt An object convertible to string_view to be appended.
ca8f2cb1
VV
1519 * @return Reference to this string.
1520 */
df66af3b 1521 template<typename _Tp>
b96e2ff9 1522 _GLIBCXX20_CONSTEXPR
df66af3b
DK
1523 _If_sv<_Tp, basic_string&>
1524 operator+=(const _Tp& __svt)
1525 { return this->append(__svt); }
ca8f2cb1
VV
1526#endif // C++17
1527
34a2b755
JW
1528 /**
1529 * @brief Append a string to this string.
1530 * @param __str The string to append.
1531 * @return Reference to this string.
1532 */
b96e2ff9 1533 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1534 basic_string&
1535 append(const basic_string& __str)
2e806704 1536 { return this->append(__str._M_data(), __str.size()); }
34a2b755
JW
1537
1538 /**
1539 * @brief Append a substring.
1540 * @param __str The string to append.
1541 * @param __pos Index of the first character of str to append.
1542 * @param __n The number of characters to append.
1543 * @return Reference to this string.
1544 * @throw std::out_of_range if @a __pos is not a valid index.
1545 *
1546 * This function appends @a __n characters from @a __str
1547 * starting at @a __pos to this string. If @a __n is is larger
1548 * than the number of available characters in @a __str, the
1549 * remainder of @a __str is appended.
1550 */
b96e2ff9 1551 _GLIBCXX20_CONSTEXPR
34a2b755 1552 basic_string&
852ee53c 1553 append(const basic_string& __str, size_type __pos, size_type __n = npos)
2e806704
JM
1554 { return this->append(__str._M_data()
1555 + __str._M_check(__pos, "basic_string::append"),
1556 __str._M_limit(__pos, __n)); }
34a2b755
JW
1557
1558 /**
1559 * @brief Append a C substring.
1560 * @param __s The C string to append.
1561 * @param __n The number of characters to append.
1562 * @return Reference to this string.
1563 */
b96e2ff9 1564 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1565 basic_string&
1566 append(const _CharT* __s, size_type __n)
1567 {
1568 __glibcxx_requires_string_len(__s, __n);
1569 _M_check_length(size_type(0), __n, "basic_string::append");
1570 return _M_append(__s, __n);
1571 }
1572
1573 /**
1574 * @brief Append a C string.
1575 * @param __s The C string to append.
1576 * @return Reference to this string.
1577 */
b96e2ff9 1578 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1579 basic_string&
1580 append(const _CharT* __s)
1581 {
1582 __glibcxx_requires_string(__s);
1583 const size_type __n = traits_type::length(__s);
1584 _M_check_length(size_type(0), __n, "basic_string::append");
1585 return _M_append(__s, __n);
1586 }
1587
1588 /**
1589 * @brief Append multiple characters.
1590 * @param __n The number of characters to append.
1591 * @param __c The character to use.
1592 * @return Reference to this string.
1593 *
1594 * Appends __n copies of __c to this string.
1595 */
b96e2ff9 1596 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1597 basic_string&
1598 append(size_type __n, _CharT __c)
1599 { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
1600
ae54d8cb 1601#if __glibcxx_containers_ranges // C++ >= 23
882d3b31
JW
1602 /**
1603 * @brief Append a range to the string.
1604 * @param __rg A range of values that are convertible to `value_type`.
1605 * @since C++23
1606 *
1607 * The range `__rg` is allowed to overlap with `*this`.
1608 */
1609 template<__detail::__container_compatible_range<_CharT> _Rg>
1610 constexpr basic_string&
1611 append_range(_Rg&& __rg)
1612 {
1613 // N.B. __rg may overlap with *this, so we must copy from __rg before
1614 // existing elements or iterators referring to *this are invalidated.
1615 // e.g. in s.append_range(views::concat(s, str)), rg overlaps s.
1616 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
1617 {
1618 const auto __len = size_type(ranges::distance(__rg));
1619
1620 // Don't care if this addition wraps around, we check it below:
1621 const size_type __newlen = size() + __len;
1622
1623 if ((capacity() - size()) >= __len)
1624 _S_copy_range(_M_data() + size(), std::forward<_Rg>(__rg),
1625 __len);
1626 else
1627 {
1628 _M_check_length(0, __len, "basic_string::append_range");
1629 basic_string __s(_M_get_allocator());
1630 __s.reserve(__newlen);
1631 _S_copy_range(__s._M_data() + size(), std::forward<_Rg>(__rg),
1632 __len);
1633 _S_copy(__s._M_data(), _M_data(), size());
1634 if (!_M_is_local())
1635 _M_destroy(_M_allocated_capacity);
1636 _M_data(__s._M_data());
1637 _M_capacity(__s._M_allocated_capacity);
1638 __s._M_data(__s._M_local_data());
1639 __s._M_length(0);
1640 }
1641 _M_set_length(__newlen); // adds null-terminator
1642 }
1643 else
1644 {
1645 basic_string __s(from_range, std::forward<_Rg>(__rg),
1646 _M_get_allocator());
1647 append(__s);
1648 }
1649 return *this;
1650 }
1651#endif
1652
34a2b755
JW
1653#if __cplusplus >= 201103L
1654 /**
1655 * @brief Append an initializer_list of characters.
1656 * @param __l The initializer_list of characters to append.
1657 * @return Reference to this string.
1658 */
b96e2ff9 1659 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1660 basic_string&
1661 append(initializer_list<_CharT> __l)
1662 { return this->append(__l.begin(), __l.size()); }
1663#endif // C++11
1664
1665 /**
1666 * @brief Append a range of characters.
1667 * @param __first Iterator referencing the first character to append.
1668 * @param __last Iterator marking the end of the range.
1669 * @return Reference to this string.
1670 *
1671 * Appends characters in the range [__first,__last) to this string.
1672 */
1673#if __cplusplus >= 201103L
1674 template<class _InputIterator,
1675 typename = std::_RequireInputIter<_InputIterator>>
b96e2ff9 1676 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1677#else
1678 template<class _InputIterator>
1679#endif
1680 basic_string&
1681 append(_InputIterator __first, _InputIterator __last)
1682 { return this->replace(end(), end(), __first, __last); }
1683
affdef57 1684#ifdef __glibcxx_string_view
ca8f2cb1
VV
1685 /**
1686 * @brief Append a string_view.
df66af3b 1687 * @param __svt An object convertible to string_view to be appended.
ca8f2cb1
VV
1688 * @return Reference to this string.
1689 */
df66af3b 1690 template<typename _Tp>
b96e2ff9 1691 _GLIBCXX20_CONSTEXPR
df66af3b
DK
1692 _If_sv<_Tp, basic_string&>
1693 append(const _Tp& __svt)
1694 {
1695 __sv_type __sv = __svt;
1696 return this->append(__sv.data(), __sv.size());
1697 }
ca8f2cb1
VV
1698
1699 /**
1700 * @brief Append a range of characters from a string_view.
df66af3b 1701 * @param __svt An object convertible to string_view to be appended from.
ca8f2cb1
VV
1702 * @param __pos The position in the string_view to append from.
1703 * @param __n The number of characters to append from the string_view.
1704 * @return Reference to this string.
1705 */
df66af3b 1706 template<typename _Tp>
b96e2ff9 1707 _GLIBCXX20_CONSTEXPR
df66af3b 1708 _If_sv<_Tp, basic_string&>
657213f7
JW
1709 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1710 {
1711 __sv_type __sv = __svt;
1712 return _M_append(__sv.data()
fb8b3e29
JW
1713 + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
1714 std::__sv_limit(__sv.size(), __pos, __n));
657213f7 1715 }
ca8f2cb1
VV
1716#endif // C++17
1717
34a2b755
JW
1718 /**
1719 * @brief Append a single character.
1720 * @param __c Character to append.
1721 */
b96e2ff9 1722 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1723 void
1724 push_back(_CharT __c)
1725 {
1726 const size_type __size = this->size();
1727 if (__size + 1 > this->capacity())
1728 this->_M_mutate(__size, size_type(0), 0, size_type(1));
1729 traits_type::assign(this->_M_data()[__size], __c);
1730 this->_M_set_length(__size + 1);
1731 }
1732
1733 /**
1734 * @brief Set value to contents of another string.
1735 * @param __str Source string to use.
1736 * @return Reference to this string.
1737 */
b96e2ff9 1738 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1739 basic_string&
1740 assign(const basic_string& __str)
1741 {
db33efde
NDR
1742#if __cplusplus >= 201103L
1743 if (_Alloc_traits::_S_propagate_on_copy_assign())
1744 {
1745 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
1746 && _M_get_allocator() != __str._M_get_allocator())
1747 {
1748 // Propagating allocator cannot free existing storage so must
1749 // deallocate it before replacing current allocator.
1750 if (__str.size() <= _S_local_capacity)
1751 {
1752 _M_destroy(_M_allocated_capacity);
6afa1083 1753 _M_data(_M_use_local_data());
db33efde
NDR
1754 _M_set_length(0);
1755 }
1756 else
1757 {
1758 const auto __len = __str.size();
1759 auto __alloc = __str._M_get_allocator();
1760 // If this allocation throws there are no effects:
c62e9454 1761 auto __ptr = _S_allocate(__alloc, __len + 1);
db33efde
NDR
1762 _M_destroy(_M_allocated_capacity);
1763 _M_data(__ptr);
1764 _M_capacity(__len);
1765 _M_set_length(__len);
1766 }
1767 }
1768 std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
1769 }
1770#endif
34a2b755
JW
1771 this->_M_assign(__str);
1772 return *this;
1773 }
1774
1775#if __cplusplus >= 201103L
1776 /**
1777 * @brief Set value to contents of another string.
1778 * @param __str Source string to use.
1779 * @return Reference to this string.
1780 *
1781 * This function sets this string to the exact contents of @a __str.
1782 * @a __str is a valid, but unspecified string.
1783 */
b96e2ff9 1784 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1785 basic_string&
1786 assign(basic_string&& __str)
5caff414 1787 noexcept(_Alloc_traits::_S_nothrow_move())
34a2b755
JW
1788 {
1789 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1790 // 2063. Contradictory requirements for string move assignment
1791 return *this = std::move(__str);
1792 }
1793#endif // C++11
1794
1795 /**
1796 * @brief Set value to a substring of a string.
1797 * @param __str The string to use.
1798 * @param __pos Index of the first character of str.
1799 * @param __n Number of characters to use.
1800 * @return Reference to this string.
1801 * @throw std::out_of_range if @a pos is not a valid index.
1802 *
1803 * This function sets this string to the substring of @a __str
1804 * consisting of @a __n characters at @a __pos. If @a __n is
1805 * is larger than the number of available characters in @a
1806 * __str, the remainder of @a __str is used.
1807 */
b96e2ff9 1808 _GLIBCXX20_CONSTEXPR
34a2b755 1809 basic_string&
852ee53c 1810 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
2d60da10 1811 { return _M_replace(size_type(0), this->size(), __str._M_data()
34a2b755
JW
1812 + __str._M_check(__pos, "basic_string::assign"),
1813 __str._M_limit(__pos, __n)); }
1814
1815 /**
1816 * @brief Set value to a C substring.
1817 * @param __s The C string to use.
1818 * @param __n Number of characters to use.
1819 * @return Reference to this string.
1820 *
1821 * This function sets the value of this string to the first @a __n
1822 * characters of @a __s. If @a __n is is larger than the number of
1823 * available characters in @a __s, the remainder of @a __s is used.
1824 */
b96e2ff9 1825 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1826 basic_string&
1827 assign(const _CharT* __s, size_type __n)
1828 {
1829 __glibcxx_requires_string_len(__s, __n);
1830 return _M_replace(size_type(0), this->size(), __s, __n);
1831 }
1832
1833 /**
1834 * @brief Set value to contents of a C string.
1835 * @param __s The C string to use.
1836 * @return Reference to this string.
1837 *
1838 * This function sets the value of this string to the value of @a __s.
1839 * The data is copied, so there is no dependence on @a __s once the
1840 * function returns.
1841 */
b96e2ff9 1842 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1843 basic_string&
1844 assign(const _CharT* __s)
1845 {
1846 __glibcxx_requires_string(__s);
1847 return _M_replace(size_type(0), this->size(), __s,
1848 traits_type::length(__s));
1849 }
1850
1851 /**
1852 * @brief Set value to multiple characters.
1853 * @param __n Length of the resulting string.
1854 * @param __c The character to use.
1855 * @return Reference to this string.
1856 *
1857 * This function sets the value of this string to @a __n copies of
1858 * character @a __c.
1859 */
b96e2ff9 1860 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1861 basic_string&
1862 assign(size_type __n, _CharT __c)
1863 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1864
1865 /**
1866 * @brief Set value to a range of characters.
1867 * @param __first Iterator referencing the first character to append.
1868 * @param __last Iterator marking the end of the range.
1869 * @return Reference to this string.
1870 *
1871 * Sets value of string to characters in the range [__first,__last).
1872 */
1873#if __cplusplus >= 201103L
cc3d7baf
JW
1874#pragma GCC diagnostic push
1875#pragma GCC diagnostic ignored "-Wc++17-extensions"
34a2b755
JW
1876 template<class _InputIterator,
1877 typename = std::_RequireInputIter<_InputIterator>>
b96e2ff9 1878 _GLIBCXX20_CONSTEXPR
cc3d7baf
JW
1879 basic_string&
1880 assign(_InputIterator __first, _InputIterator __last)
1881 {
85e5b80e
JW
1882 using _IterTraits = iterator_traits<_InputIterator>;
1883 if constexpr (is_pointer<decltype(std::__niter_base(__first))>::value
1884 && is_same<typename _IterTraits::value_type,
1885 _CharT>::value)
1886 {
1887 __glibcxx_requires_valid_range(__first, __last);
1888 return _M_replace(size_type(0), size(),
1889 std::__niter_base(__first), __last - __first);
1890 }
cc3d7baf 1891#if __cplusplus >= 202002L
85e5b80e
JW
1892 else if constexpr (contiguous_iterator<_InputIterator>
1893 && is_same_v<iter_value_t<_InputIterator>,
1894 _CharT>)
cc3d7baf
JW
1895 {
1896 __glibcxx_requires_valid_range(__first, __last);
1897 return _M_replace(size_type(0), size(),
de2dc623 1898 std::to_address(__first), __last - __first);
cc3d7baf 1899 }
85e5b80e 1900#endif
cc3d7baf
JW
1901 else
1902 return *this = basic_string(__first, __last, get_allocator());
1903 }
1904#pragma GCC diagnostic pop
34a2b755
JW
1905#else
1906 template<class _InputIterator>
cc3d7baf
JW
1907 basic_string&
1908 assign(_InputIterator __first, _InputIterator __last)
1909 { return this->replace(begin(), end(), __first, __last); }
34a2b755 1910#endif
34a2b755 1911
ae54d8cb 1912#if __glibcxx_containers_ranges // C++ >= 23
882d3b31
JW
1913 /**
1914 * @brief Assign a range to the string.
1915 * @param __rg A range of values that are convertible to `value_type`.
1916 * @since C++23
1917 *
1918 * The range `__rg` is allowed to overlap with `*this`.
1919 */
1920 template<__detail::__container_compatible_range<_CharT> _Rg>
1921 constexpr basic_string&
1922 assign_range(_Rg&& __rg)
1923 {
1924 basic_string __s(from_range, std::forward<_Rg>(__rg),
1925 _M_get_allocator());
1926 assign(std::move(__s));
1927 return *this;
1928 }
1929#endif
1930
34a2b755
JW
1931#if __cplusplus >= 201103L
1932 /**
1933 * @brief Set value to an initializer_list of characters.
1934 * @param __l The initializer_list of characters to assign.
1935 * @return Reference to this string.
1936 */
b96e2ff9 1937 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1938 basic_string&
1939 assign(initializer_list<_CharT> __l)
cc3d7baf
JW
1940 {
1941 // The initializer_list array cannot alias the characters in *this
1942 // so we don't need to use replace to that case.
1943 const size_type __n = __l.size();
1944 if (__n > capacity())
1945 *this = basic_string(__l.begin(), __l.end(), get_allocator());
1946 else
1947 {
1948 if (__n)
1949 _S_copy(_M_data(), __l.begin(), __n);
1950 _M_set_length(__n);
1951 }
1952 return *this;
1953 }
34a2b755
JW
1954#endif // C++11
1955
affdef57 1956#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
1957 /**
1958 * @brief Set value from a string_view.
df66af3b 1959 * @param __svt The source object convertible to string_view.
ca8f2cb1
VV
1960 * @return Reference to this string.
1961 */
df66af3b 1962 template<typename _Tp>
b96e2ff9 1963 _GLIBCXX20_CONSTEXPR
df66af3b
DK
1964 _If_sv<_Tp, basic_string&>
1965 assign(const _Tp& __svt)
1966 {
1967 __sv_type __sv = __svt;
1968 return this->assign(__sv.data(), __sv.size());
1969 }
ca8f2cb1
VV
1970
1971 /**
1972 * @brief Set value from a range of characters in a string_view.
df66af3b 1973 * @param __svt The source object convertible to string_view.
ca8f2cb1
VV
1974 * @param __pos The position in the string_view to assign from.
1975 * @param __n The number of characters to assign.
1976 * @return Reference to this string.
1977 */
df66af3b 1978 template<typename _Tp>
b96e2ff9 1979 _GLIBCXX20_CONSTEXPR
657213f7
JW
1980 _If_sv<_Tp, basic_string&>
1981 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
1982 {
1983 __sv_type __sv = __svt;
fb8b3e29
JW
1984 return _M_replace(size_type(0), this->size(),
1985 __sv.data()
1986 + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
1987 std::__sv_limit(__sv.size(), __pos, __n));
657213f7 1988 }
ca8f2cb1
VV
1989#endif // C++17
1990
34a2b755
JW
1991#if __cplusplus >= 201103L
1992 /**
1993 * @brief Insert multiple characters.
1994 * @param __p Const_iterator referencing location in string to
1995 * insert at.
1996 * @param __n Number of characters to insert
1997 * @param __c The character to insert.
1998 * @return Iterator referencing the first inserted char.
1999 * @throw std::length_error If new length exceeds @c max_size().
2000 *
2001 * Inserts @a __n copies of character @a __c starting at the
2002 * position referenced by iterator @a __p. If adding
2003 * characters causes the length to exceed max_size(),
2004 * length_error is thrown. The value of the string doesn't
2005 * change if an error is thrown.
2006 */
b96e2ff9 2007 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2008 iterator
2009 insert(const_iterator __p, size_type __n, _CharT __c)
2010 {
2011 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2012 const size_type __pos = __p - begin();
2013 this->replace(__p, __p, __n, __c);
2014 return iterator(this->_M_data() + __pos);
2015 }
2016#else
2017 /**
2018 * @brief Insert multiple characters.
2019 * @param __p Iterator referencing location in string to insert at.
2020 * @param __n Number of characters to insert
2021 * @param __c The character to insert.
2022 * @throw std::length_error If new length exceeds @c max_size().
2023 *
2024 * Inserts @a __n copies of character @a __c starting at the
2025 * position referenced by iterator @a __p. If adding
2026 * characters causes the length to exceed max_size(),
2027 * length_error is thrown. The value of the string doesn't
2028 * change if an error is thrown.
2029 */
2030 void
2031 insert(iterator __p, size_type __n, _CharT __c)
2032 { this->replace(__p, __p, __n, __c); }
2033#endif
2034
2035#if __cplusplus >= 201103L
2036 /**
2037 * @brief Insert a range of characters.
2038 * @param __p Const_iterator referencing location in string to
2039 * insert at.
2040 * @param __beg Start of range.
2041 * @param __end End of range.
2042 * @return Iterator referencing the first inserted char.
2043 * @throw std::length_error If new length exceeds @c max_size().
2044 *
2045 * Inserts characters in range [beg,end). If adding characters
2046 * causes the length to exceed max_size(), length_error is
2047 * thrown. The value of the string doesn't change if an error
2048 * is thrown.
2049 */
2050 template<class _InputIterator,
2051 typename = std::_RequireInputIter<_InputIterator>>
b96e2ff9 2052 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2053 iterator
2054 insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
2055 {
2056 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2057 const size_type __pos = __p - begin();
2058 this->replace(__p, __p, __beg, __end);
2059 return iterator(this->_M_data() + __pos);
2060 }
2061#else
2062 /**
2063 * @brief Insert a range of characters.
2064 * @param __p Iterator referencing location in string to insert at.
2065 * @param __beg Start of range.
2066 * @param __end End of range.
2067 * @throw std::length_error If new length exceeds @c max_size().
2068 *
2069 * Inserts characters in range [__beg,__end). If adding
2070 * characters causes the length to exceed max_size(),
2071 * length_error is thrown. The value of the string doesn't
2072 * change if an error is thrown.
2073 */
2074 template<class _InputIterator>
2075 void
2076 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
2077 { this->replace(__p, __p, __beg, __end); }
2078#endif
2079
ae54d8cb 2080#if __glibcxx_containers_ranges // C++ >= 23
882d3b31
JW
2081 /**
2082 * @brief Insert a range into the string.
2083 * @param __rg A range of values that are convertible to `value_type`.
2084 * @since C++23
2085 *
2086 * The range `__rg` is allowed to overlap with `*this`.
2087 */
2088 template<__detail::__container_compatible_range<_CharT> _Rg>
2089 constexpr iterator
2090 insert_range(const_iterator __p, _Rg&& __rg)
2091 {
2092 auto __pos = __p - cbegin();
2093
2094 if constexpr (ranges::forward_range<_Rg>)
2095 if (ranges::empty(__rg))
2096 return begin() + __pos;
2097
2098
2099 if (__p == cend())
2100 append_range(std::forward<_Rg>(__rg));
2101 else
2102 {
2103 basic_string __s(from_range, std::forward<_Rg>(__rg),
2104 _M_get_allocator());
2105 insert(__pos, __s);
2106 }
2107 return begin() + __pos;
2108 }
2109#endif
2110
34a2b755
JW
2111#if __cplusplus >= 201103L
2112 /**
2113 * @brief Insert an initializer_list of characters.
2114 * @param __p Iterator referencing location in string to insert at.
2115 * @param __l The initializer_list of characters to insert.
2116 * @throw std::length_error If new length exceeds @c max_size().
2117 */
b96e2ff9 2118 _GLIBCXX20_CONSTEXPR
cda121ac
JW
2119 iterator
2120 insert(const_iterator __p, initializer_list<_CharT> __l)
2121 { return this->insert(__p, __l.begin(), __l.end()); }
2122
2123#ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
2124 // See PR libstdc++/83328
34a2b755
JW
2125 void
2126 insert(iterator __p, initializer_list<_CharT> __l)
2127 {
2128 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2129 this->insert(__p - begin(), __l.begin(), __l.size());
2130 }
cda121ac 2131#endif
34a2b755
JW
2132#endif // C++11
2133
2134 /**
2135 * @brief Insert value of a string.
8c6a71e4 2136 * @param __pos1 Position in string to insert at.
34a2b755
JW
2137 * @param __str The string to insert.
2138 * @return Reference to this string.
2139 * @throw std::length_error If new length exceeds @c max_size().
2140 *
2141 * Inserts value of @a __str starting at @a __pos1. If adding
2142 * characters causes the length to exceed max_size(),
2143 * length_error is thrown. The value of the string doesn't
2144 * change if an error is thrown.
2145 */
b96e2ff9 2146 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2147 basic_string&
2148 insert(size_type __pos1, const basic_string& __str)
2149 { return this->replace(__pos1, size_type(0),
2d60da10 2150 __str._M_data(), __str.size()); }
34a2b755
JW
2151
2152 /**
2153 * @brief Insert a substring.
8c6a71e4
JW
2154 * @param __pos1 Position in string to insert at.
2155 * @param __str The string to insert.
34a2b755
JW
2156 * @param __pos2 Start of characters in str to insert.
2157 * @param __n Number of characters to insert.
2158 * @return Reference to this string.
2159 * @throw std::length_error If new length exceeds @c max_size().
2160 * @throw std::out_of_range If @a pos1 > size() or
2161 * @a __pos2 > @a str.size().
2162 *
2163 * Starting at @a pos1, insert @a __n character of @a __str
2164 * beginning with @a __pos2. If adding characters causes the
2165 * length to exceed max_size(), length_error is thrown. If @a
2166 * __pos1 is beyond the end of this string or @a __pos2 is
2167 * beyond the end of @a __str, out_of_range is thrown. The
2168 * value of the string doesn't change if an error is thrown.
2169 */
b96e2ff9 2170 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2171 basic_string&
2172 insert(size_type __pos1, const basic_string& __str,
852ee53c 2173 size_type __pos2, size_type __n = npos)
2d60da10 2174 { return this->replace(__pos1, size_type(0), __str._M_data()
34a2b755
JW
2175 + __str._M_check(__pos2, "basic_string::insert"),
2176 __str._M_limit(__pos2, __n)); }
2177
2178 /**
2179 * @brief Insert a C substring.
8c6a71e4 2180 * @param __pos Position in string to insert at.
34a2b755
JW
2181 * @param __s The C string to insert.
2182 * @param __n The number of characters to insert.
2183 * @return Reference to this string.
2184 * @throw std::length_error If new length exceeds @c max_size().
2185 * @throw std::out_of_range If @a __pos is beyond the end of this
2186 * string.
2187 *
2188 * Inserts the first @a __n characters of @a __s starting at @a
2189 * __pos. If adding characters causes the length to exceed
2190 * max_size(), length_error is thrown. If @a __pos is beyond
2191 * end(), out_of_range is thrown. The value of the string
2192 * doesn't change if an error is thrown.
2193 */
b96e2ff9 2194 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2195 basic_string&
2196 insert(size_type __pos, const _CharT* __s, size_type __n)
2197 { return this->replace(__pos, size_type(0), __s, __n); }
2198
2199 /**
2200 * @brief Insert a C string.
8c6a71e4 2201 * @param __pos Position in string to insert at.
34a2b755
JW
2202 * @param __s The C string to insert.
2203 * @return Reference to this string.
2204 * @throw std::length_error If new length exceeds @c max_size().
2205 * @throw std::out_of_range If @a pos is beyond the end of this
2206 * string.
2207 *
2208 * Inserts the first @a n characters of @a __s starting at @a __pos. If
2209 * adding characters causes the length to exceed max_size(),
2210 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
2211 * thrown. The value of the string doesn't change if an error is
2212 * thrown.
2213 */
b96e2ff9 2214 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2215 basic_string&
2216 insert(size_type __pos, const _CharT* __s)
2217 {
2218 __glibcxx_requires_string(__s);
2219 return this->replace(__pos, size_type(0), __s,
2220 traits_type::length(__s));
2221 }
2222
2223 /**
2224 * @brief Insert multiple characters.
2225 * @param __pos Index in string to insert at.
2226 * @param __n Number of characters to insert
2227 * @param __c The character to insert.
2228 * @return Reference to this string.
2229 * @throw std::length_error If new length exceeds @c max_size().
2230 * @throw std::out_of_range If @a __pos is beyond the end of this
2231 * string.
2232 *
2233 * Inserts @a __n copies of character @a __c starting at index
2234 * @a __pos. If adding characters causes the length to exceed
2235 * max_size(), length_error is thrown. If @a __pos > length(),
2236 * out_of_range is thrown. The value of the string doesn't
2237 * change if an error is thrown.
2238 */
b96e2ff9 2239 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2240 basic_string&
2241 insert(size_type __pos, size_type __n, _CharT __c)
2242 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
2243 size_type(0), __n, __c); }
2244
2245 /**
2246 * @brief Insert one character.
2247 * @param __p Iterator referencing position in string to insert at.
2248 * @param __c The character to insert.
2249 * @return Iterator referencing newly inserted char.
2250 * @throw std::length_error If new length exceeds @c max_size().
2251 *
2252 * Inserts character @a __c at position referenced by @a __p.
2253 * If adding character causes the length to exceed max_size(),
2254 * length_error is thrown. If @a __p is beyond end of string,
2255 * out_of_range is thrown. The value of the string doesn't
2256 * change if an error is thrown.
2257 */
b96e2ff9 2258 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2259 iterator
2260 insert(__const_iterator __p, _CharT __c)
2261 {
2262 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2263 const size_type __pos = __p - begin();
2264 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
2265 return iterator(_M_data() + __pos);
2266 }
2267
affdef57 2268#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
2269 /**
2270 * @brief Insert a string_view.
8c6a71e4 2271 * @param __pos Position in string to insert at.
df66af3b 2272 * @param __svt The object convertible to string_view to insert.
ca8f2cb1
VV
2273 * @return Reference to this string.
2274 */
df66af3b 2275 template<typename _Tp>
b96e2ff9 2276 _GLIBCXX20_CONSTEXPR
df66af3b
DK
2277 _If_sv<_Tp, basic_string&>
2278 insert(size_type __pos, const _Tp& __svt)
2279 {
2280 __sv_type __sv = __svt;
2281 return this->insert(__pos, __sv.data(), __sv.size());
2282 }
ca8f2cb1
VV
2283
2284 /**
2285 * @brief Insert a string_view.
8c6a71e4
JW
2286 * @param __pos1 Position in string to insert at.
2287 * @param __svt The object convertible to string_view to insert from.
2288 * @param __pos2 Start of characters in str to insert.
ca8f2cb1
VV
2289 * @param __n The number of characters to insert.
2290 * @return Reference to this string.
2291 */
df66af3b 2292 template<typename _Tp>
b96e2ff9 2293 _GLIBCXX20_CONSTEXPR
657213f7
JW
2294 _If_sv<_Tp, basic_string&>
2295 insert(size_type __pos1, const _Tp& __svt,
2296 size_type __pos2, size_type __n = npos)
2297 {
2298 __sv_type __sv = __svt;
fb8b3e29
JW
2299 return this->replace(__pos1, size_type(0),
2300 __sv.data()
2301 + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
2302 std::__sv_limit(__sv.size(), __pos2, __n));
657213f7 2303 }
ca8f2cb1
VV
2304#endif // C++17
2305
34a2b755
JW
2306 /**
2307 * @brief Remove characters.
2308 * @param __pos Index of first character to remove (default 0).
2309 * @param __n Number of characters to remove (default remainder).
2310 * @return Reference to this string.
2311 * @throw std::out_of_range If @a pos is beyond the end of this
2312 * string.
2313 *
2314 * Removes @a __n characters from this string starting at @a
2315 * __pos. The length of the string is reduced by @a __n. If
2316 * there are < @a __n characters to remove, the remainder of
2317 * the string is truncated. If @a __p is beyond end of string,
2318 * out_of_range is thrown. The value of the string doesn't
2319 * change if an error is thrown.
2320 */
b96e2ff9 2321 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2322 basic_string&
2323 erase(size_type __pos = 0, size_type __n = npos)
2324 {
a922c5ff
JW
2325 _M_check(__pos, "basic_string::erase");
2326 if (__n == npos)
2327 this->_M_set_length(__pos);
2328 else if (__n != 0)
2329 this->_M_erase(__pos, _M_limit(__pos, __n));
34a2b755
JW
2330 return *this;
2331 }
2332
2333 /**
2334 * @brief Remove one character.
2335 * @param __position Iterator referencing the character to remove.
2336 * @return iterator referencing same location after removal.
2337 *
2338 * Removes the character at @a __position from this string. The value
2339 * of the string doesn't change if an error is thrown.
2340 */
b96e2ff9 2341 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2342 iterator
2343 erase(__const_iterator __position)
2344 {
2345 _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
2346 && __position < end());
2347 const size_type __pos = __position - begin();
2348 this->_M_erase(__pos, size_type(1));
2349 return iterator(_M_data() + __pos);
2350 }
2351
2352 /**
2353 * @brief Remove a range of characters.
2354 * @param __first Iterator referencing the first character to remove.
2355 * @param __last Iterator referencing the end of the range.
2356 * @return Iterator referencing location of first after removal.
2357 *
2358 * Removes the characters in the range [first,last) from this string.
2359 * The value of the string doesn't change if an error is thrown.
2360 */
b96e2ff9 2361 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2362 iterator
2363 erase(__const_iterator __first, __const_iterator __last)
2364 {
2365 _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
2366 && __last <= end());
2367 const size_type __pos = __first - begin();
a922c5ff
JW
2368 if (__last == end())
2369 this->_M_set_length(__pos);
2370 else
2371 this->_M_erase(__pos, __last - __first);
34a2b755
JW
2372 return iterator(this->_M_data() + __pos);
2373 }
2374
2375#if __cplusplus >= 201103L
2376 /**
2377 * @brief Remove the last character.
2378 *
2379 * The string must be non-empty.
2380 */
b96e2ff9 2381 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2382 void
2383 pop_back() noexcept
e25d2617 2384 {
2f1e8e7c 2385 __glibcxx_assert(!empty());
e25d2617
FD
2386 _M_erase(size() - 1, 1);
2387 }
34a2b755
JW
2388#endif // C++11
2389
2390 /**
2391 * @brief Replace characters with value from another string.
2392 * @param __pos Index of first character to replace.
2393 * @param __n Number of characters to be replaced.
2394 * @param __str String to insert.
2395 * @return Reference to this string.
2396 * @throw std::out_of_range If @a pos is beyond the end of this
2397 * string.
2398 * @throw std::length_error If new length exceeds @c max_size().
2399 *
2400 * Removes the characters in the range [__pos,__pos+__n) from
2401 * this string. In place, the value of @a __str is inserted.
2402 * If @a __pos is beyond end of string, out_of_range is thrown.
2403 * If the length of the result exceeds max_size(), length_error
2404 * is thrown. The value of the string doesn't change if an
2405 * error is thrown.
2406 */
b96e2ff9 2407 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2408 basic_string&
2409 replace(size_type __pos, size_type __n, const basic_string& __str)
2d60da10 2410 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
34a2b755
JW
2411
2412 /**
2413 * @brief Replace characters with value from another string.
2414 * @param __pos1 Index of first character to replace.
2415 * @param __n1 Number of characters to be replaced.
2416 * @param __str String to insert.
2417 * @param __pos2 Index of first character of str to use.
2418 * @param __n2 Number of characters from str to use.
2419 * @return Reference to this string.
2420 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
2421 * __str.size().
2422 * @throw std::length_error If new length exceeds @c max_size().
2423 *
2424 * Removes the characters in the range [__pos1,__pos1 + n) from this
2425 * string. In place, the value of @a __str is inserted. If @a __pos is
2426 * beyond end of string, out_of_range is thrown. If the length of the
2427 * result exceeds max_size(), length_error is thrown. The value of the
2428 * string doesn't change if an error is thrown.
2429 */
b96e2ff9 2430 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2431 basic_string&
2432 replace(size_type __pos1, size_type __n1, const basic_string& __str,
852ee53c 2433 size_type __pos2, size_type __n2 = npos)
2d60da10 2434 { return this->replace(__pos1, __n1, __str._M_data()
34a2b755
JW
2435 + __str._M_check(__pos2, "basic_string::replace"),
2436 __str._M_limit(__pos2, __n2)); }
2437
2438 /**
2439 * @brief Replace characters with value of a C substring.
2440 * @param __pos Index of first character to replace.
2441 * @param __n1 Number of characters to be replaced.
2442 * @param __s C string to insert.
2443 * @param __n2 Number of characters from @a s to use.
2444 * @return Reference to this string.
2445 * @throw std::out_of_range If @a pos1 > size().
2446 * @throw std::length_error If new length exceeds @c max_size().
2447 *
2448 * Removes the characters in the range [__pos,__pos + __n1)
2449 * from this string. In place, the first @a __n2 characters of
2450 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
2451 * @a __pos is beyond end of string, out_of_range is thrown. If
2452 * the length of result exceeds max_size(), length_error is
2453 * thrown. The value of the string doesn't change if an error
2454 * is thrown.
2455 */
b96e2ff9 2456 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2457 basic_string&
2458 replace(size_type __pos, size_type __n1, const _CharT* __s,
2459 size_type __n2)
2460 {
2461 __glibcxx_requires_string_len(__s, __n2);
2462 return _M_replace(_M_check(__pos, "basic_string::replace"),
2463 _M_limit(__pos, __n1), __s, __n2);
2464 }
2465
2466 /**
2467 * @brief Replace characters with value of a C string.
2468 * @param __pos Index of first character to replace.
2469 * @param __n1 Number of characters to be replaced.
2470 * @param __s C string to insert.
2471 * @return Reference to this string.
2472 * @throw std::out_of_range If @a pos > size().
2473 * @throw std::length_error If new length exceeds @c max_size().
2474 *
2475 * Removes the characters in the range [__pos,__pos + __n1)
2476 * from this string. In place, the characters of @a __s are
2477 * inserted. If @a __pos is beyond end of string, out_of_range
2478 * is thrown. If the length of result exceeds max_size(),
2479 * length_error is thrown. The value of the string doesn't
2480 * change if an error is thrown.
2481 */
b96e2ff9 2482 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2483 basic_string&
2484 replace(size_type __pos, size_type __n1, const _CharT* __s)
2485 {
2486 __glibcxx_requires_string(__s);
2487 return this->replace(__pos, __n1, __s, traits_type::length(__s));
2488 }
2489
2490 /**
2491 * @brief Replace characters with multiple characters.
2492 * @param __pos Index of first character to replace.
2493 * @param __n1 Number of characters to be replaced.
2494 * @param __n2 Number of characters to insert.
2495 * @param __c Character to insert.
2496 * @return Reference to this string.
2497 * @throw std::out_of_range If @a __pos > size().
2498 * @throw std::length_error If new length exceeds @c max_size().
2499 *
2500 * Removes the characters in the range [pos,pos + n1) from this
2501 * string. In place, @a __n2 copies of @a __c are inserted.
2502 * If @a __pos is beyond end of string, out_of_range is thrown.
2503 * If the length of result exceeds max_size(), length_error is
2504 * thrown. The value of the string doesn't change if an error
2505 * is thrown.
2506 */
b96e2ff9 2507 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2508 basic_string&
2509 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
2510 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
2511 _M_limit(__pos, __n1), __n2, __c); }
2512
2513 /**
2514 * @brief Replace range of characters with string.
2515 * @param __i1 Iterator referencing start of range to replace.
2516 * @param __i2 Iterator referencing end of range to replace.
2517 * @param __str String value to insert.
2518 * @return Reference to this string.
2519 * @throw std::length_error If new length exceeds @c max_size().
2520 *
2521 * Removes the characters in the range [__i1,__i2). In place,
2522 * the value of @a __str is inserted. If the length of result
2523 * exceeds max_size(), length_error is thrown. The value of
2524 * the string doesn't change if an error is thrown.
2525 */
b96e2ff9 2526 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2527 basic_string&
2528 replace(__const_iterator __i1, __const_iterator __i2,
2529 const basic_string& __str)
2d60da10 2530 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
34a2b755
JW
2531
2532 /**
2533 * @brief Replace range of characters with C substring.
2534 * @param __i1 Iterator referencing start of range to replace.
2535 * @param __i2 Iterator referencing end of range to replace.
2536 * @param __s C string value to insert.
2537 * @param __n Number of characters from s to insert.
2538 * @return Reference to this string.
2539 * @throw std::length_error If new length exceeds @c max_size().
2540 *
2541 * Removes the characters in the range [__i1,__i2). In place,
2542 * the first @a __n characters of @a __s are inserted. If the
2543 * length of result exceeds max_size(), length_error is thrown.
2544 * The value of the string doesn't change if an error is
2545 * thrown.
2546 */
b96e2ff9 2547 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2548 basic_string&
2549 replace(__const_iterator __i1, __const_iterator __i2,
2550 const _CharT* __s, size_type __n)
2551 {
2552 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2553 && __i2 <= end());
2554 return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
2555 }
2556
2557 /**
2558 * @brief Replace range of characters with C string.
2559 * @param __i1 Iterator referencing start of range to replace.
2560 * @param __i2 Iterator referencing end of range to replace.
2561 * @param __s C string value to insert.
2562 * @return Reference to this string.
2563 * @throw std::length_error If new length exceeds @c max_size().
2564 *
2565 * Removes the characters in the range [__i1,__i2). In place,
2566 * the characters of @a __s are inserted. If the length of
2567 * result exceeds max_size(), length_error is thrown. The
2568 * value of the string doesn't change if an error is thrown.
2569 */
b96e2ff9 2570 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2571 basic_string&
2572 replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
2573 {
2574 __glibcxx_requires_string(__s);
2575 return this->replace(__i1, __i2, __s, traits_type::length(__s));
2576 }
2577
2578 /**
2579 * @brief Replace range of characters with multiple characters
2580 * @param __i1 Iterator referencing start of range to replace.
2581 * @param __i2 Iterator referencing end of range to replace.
2582 * @param __n Number of characters to insert.
2583 * @param __c Character to insert.
2584 * @return Reference to this string.
2585 * @throw std::length_error If new length exceeds @c max_size().
2586 *
2587 * Removes the characters in the range [__i1,__i2). In place,
2588 * @a __n copies of @a __c are inserted. If the length of
2589 * result exceeds max_size(), length_error is thrown. The
2590 * value of the string doesn't change if an error is thrown.
2591 */
b96e2ff9 2592 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2593 basic_string&
2594 replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
2595 _CharT __c)
2596 {
2597 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2598 && __i2 <= end());
2599 return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
2600 }
2601
2602 /**
2603 * @brief Replace range of characters with range.
2604 * @param __i1 Iterator referencing start of range to replace.
2605 * @param __i2 Iterator referencing end of range to replace.
2606 * @param __k1 Iterator referencing start of range to insert.
2607 * @param __k2 Iterator referencing end of range to insert.
2608 * @return Reference to this string.
2609 * @throw std::length_error If new length exceeds @c max_size().
2610 *
2611 * Removes the characters in the range [__i1,__i2). In place,
2612 * characters in the range [__k1,__k2) are inserted. If the
2613 * length of result exceeds max_size(), length_error is thrown.
2614 * The value of the string doesn't change if an error is
2615 * thrown.
2616 */
2617#if __cplusplus >= 201103L
2618 template<class _InputIterator,
2619 typename = std::_RequireInputIter<_InputIterator>>
b96e2ff9 2620 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2621 basic_string&
2622 replace(const_iterator __i1, const_iterator __i2,
2623 _InputIterator __k1, _InputIterator __k2)
2624 {
2625 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2626 && __i2 <= end());
2627 __glibcxx_requires_valid_range(__k1, __k2);
2628 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
2629 std::__false_type());
2630 }
2631#else
2632 template<class _InputIterator>
2633#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2634 typename __enable_if_not_native_iterator<_InputIterator>::__type
2635#else
2636 basic_string&
2637#endif
2638 replace(iterator __i1, iterator __i2,
2639 _InputIterator __k1, _InputIterator __k2)
2640 {
2641 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2642 && __i2 <= end());
2643 __glibcxx_requires_valid_range(__k1, __k2);
2644 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2645 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2646 }
2647#endif
2648
2649 // Specializations for the common case of pointer and iterator:
2650 // useful to avoid the overhead of temporary buffering in _M_replace.
b96e2ff9 2651 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2652 basic_string&
2653 replace(__const_iterator __i1, __const_iterator __i2,
2654 _CharT* __k1, _CharT* __k2)
2655 {
2656 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2657 && __i2 <= end());
2658 __glibcxx_requires_valid_range(__k1, __k2);
2659 return this->replace(__i1 - begin(), __i2 - __i1,
2660 __k1, __k2 - __k1);
2661 }
2662
b96e2ff9 2663 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2664 basic_string&
2665 replace(__const_iterator __i1, __const_iterator __i2,
2666 const _CharT* __k1, const _CharT* __k2)
2667 {
2668 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2669 && __i2 <= end());
2670 __glibcxx_requires_valid_range(__k1, __k2);
2671 return this->replace(__i1 - begin(), __i2 - __i1,
2672 __k1, __k2 - __k1);
2673 }
2674
b96e2ff9 2675 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2676 basic_string&
2677 replace(__const_iterator __i1, __const_iterator __i2,
2678 iterator __k1, iterator __k2)
2679 {
2680 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2681 && __i2 <= end());
2682 __glibcxx_requires_valid_range(__k1, __k2);
2683 return this->replace(__i1 - begin(), __i2 - __i1,
2684 __k1.base(), __k2 - __k1);
2685 }
2686
b96e2ff9 2687 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2688 basic_string&
2689 replace(__const_iterator __i1, __const_iterator __i2,
2690 const_iterator __k1, const_iterator __k2)
2691 {
2692 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2693 && __i2 <= end());
2694 __glibcxx_requires_valid_range(__k1, __k2);
2695 return this->replace(__i1 - begin(), __i2 - __i1,
2696 __k1.base(), __k2 - __k1);
2697 }
2698
ae54d8cb 2699#if __glibcxx_containers_ranges // C++ >= 23
882d3b31
JW
2700 /**
2701 * @brief Replace part of the string with a range.
2702 * @param __rg A range of values that are convertible to `value_type`.
2703 * @since C++23
2704 *
2705 * The range `__rg` is allowed to overlap with `*this`.
2706 */
2707 template<__detail::__container_compatible_range<_CharT> _Rg>
2708 constexpr basic_string&
2709 replace_with_range(const_iterator __i1, const_iterator __i2, _Rg&& __rg)
2710 {
2711 if (__i1 == cend())
2712 append_range(std::forward<_Rg>(__rg));
2713 else
2714 {
2715 basic_string __s(from_range, std::forward<_Rg>(__rg),
2716 _M_get_allocator());
2717 replace(__i1, __i2, __s);
2718 }
2719 return *this;
2720 }
2721#endif
2722
34a2b755
JW
2723#if __cplusplus >= 201103L
2724 /**
2725 * @brief Replace range of characters with initializer_list.
2726 * @param __i1 Iterator referencing start of range to replace.
2727 * @param __i2 Iterator referencing end of range to replace.
2728 * @param __l The initializer_list of characters to insert.
2729 * @return Reference to this string.
2730 * @throw std::length_error If new length exceeds @c max_size().
2731 *
2732 * Removes the characters in the range [__i1,__i2). In place,
2733 * characters in the range [__k1,__k2) are inserted. If the
2734 * length of result exceeds max_size(), length_error is thrown.
2735 * The value of the string doesn't change if an error is
2736 * thrown.
2737 */
b96e2ff9 2738 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2739 basic_string& replace(const_iterator __i1, const_iterator __i2,
2740 initializer_list<_CharT> __l)
17f8dc93 2741 { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
34a2b755
JW
2742#endif // C++11
2743
affdef57 2744#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
2745 /**
2746 * @brief Replace range of characters with string_view.
2747 * @param __pos The position to replace at.
2748 * @param __n The number of characters to replace.
df66af3b 2749 * @param __svt The object convertible to string_view to insert.
ca8f2cb1
VV
2750 * @return Reference to this string.
2751 */
df66af3b 2752 template<typename _Tp>
b96e2ff9 2753 _GLIBCXX20_CONSTEXPR
df66af3b
DK
2754 _If_sv<_Tp, basic_string&>
2755 replace(size_type __pos, size_type __n, const _Tp& __svt)
2756 {
2757 __sv_type __sv = __svt;
2758 return this->replace(__pos, __n, __sv.data(), __sv.size());
2759 }
ca8f2cb1
VV
2760
2761 /**
2762 * @brief Replace range of characters with string_view.
2763 * @param __pos1 The position to replace at.
2764 * @param __n1 The number of characters to replace.
df66af3b 2765 * @param __svt The object convertible to string_view to insert from.
ca8f2cb1
VV
2766 * @param __pos2 The position in the string_view to insert from.
2767 * @param __n2 The number of characters to insert.
2768 * @return Reference to this string.
2769 */
df66af3b 2770 template<typename _Tp>
b96e2ff9 2771 _GLIBCXX20_CONSTEXPR
657213f7
JW
2772 _If_sv<_Tp, basic_string&>
2773 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2774 size_type __pos2, size_type __n2 = npos)
2775 {
2776 __sv_type __sv = __svt;
fb8b3e29
JW
2777 return this->replace(__pos1, __n1,
2778 __sv.data()
2779 + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
2780 std::__sv_limit(__sv.size(), __pos2, __n2));
657213f7 2781 }
ca8f2cb1
VV
2782
2783 /**
2784 * @brief Replace range of characters with string_view.
2785 * @param __i1 An iterator referencing the start position
2786 to replace at.
2787 * @param __i2 An iterator referencing the end position
2788 for the replace.
df66af3b 2789 * @param __svt The object convertible to string_view to insert from.
ca8f2cb1
VV
2790 * @return Reference to this string.
2791 */
df66af3b 2792 template<typename _Tp>
b96e2ff9 2793 _GLIBCXX20_CONSTEXPR
df66af3b
DK
2794 _If_sv<_Tp, basic_string&>
2795 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2796 {
2797 __sv_type __sv = __svt;
2798 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2799 }
ca8f2cb1
VV
2800#endif // C++17
2801
34a2b755
JW
2802 private:
2803 template<class _Integer>
b96e2ff9 2804 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2805 basic_string&
2806 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2807 _Integer __n, _Integer __val, __true_type)
2808 { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
2809
2810 template<class _InputIterator>
b96e2ff9 2811 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2812 basic_string&
2813 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2814 _InputIterator __k1, _InputIterator __k2,
2815 __false_type);
2816
b96e2ff9 2817 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2818 basic_string&
2819 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2820 _CharT __c);
2821
723ef5a9
JJ
2822 __attribute__((__noinline__, __noclone__, __cold__)) void
2823 _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s,
2824 const size_type __len2, const size_type __how_much);
2825
b96e2ff9 2826 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2827 basic_string&
2828 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
2829 const size_type __len2);
2830
b96e2ff9 2831 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2832 basic_string&
2833 _M_append(const _CharT* __s, size_type __n);
2834
2835 public:
2836
2837 /**
2838 * @brief Copy substring into C string.
2839 * @param __s C string to copy value into.
2840 * @param __n Number of characters to copy.
2841 * @param __pos Index of first character to copy.
2842 * @return Number of characters actually copied
2843 * @throw std::out_of_range If __pos > size().
2844 *
2845 * Copies up to @a __n characters starting at @a __pos into the
2846 * C string @a __s. If @a __pos is %greater than size(),
2847 * out_of_range is thrown.
2848 */
b96e2ff9 2849 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2850 size_type
2851 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2852
2853 /**
2854 * @brief Swap contents with another string.
2855 * @param __s String to swap with.
2856 *
2857 * Exchanges the contents of this string with that of @a __s in constant
2858 * time.
2859 */
b96e2ff9 2860 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2861 void
2862 swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
2863
2864 // String operations:
2865 /**
2866 * @brief Return const pointer to null-terminated contents.
2867 *
2868 * This is a handle to internal data. Do not modify or dire things may
2869 * happen.
2870 */
b911ca42 2871 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
2872 const _CharT*
2873 c_str() const _GLIBCXX_NOEXCEPT
2d60da10 2874 { return _M_data(); }
34a2b755
JW
2875
2876 /**
2877 * @brief Return const pointer to contents.
2878 *
92d58dee
JW
2879 * This is a pointer to internal data. It is undefined to modify
2880 * the contents through the returned pointer. To get a pointer that
2881 * allows modifying the contents use @c &str[0] instead,
2882 * (or in C++17 the non-const @c str.data() overload).
34a2b755 2883 */
b911ca42 2884 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
2885 const _CharT*
2886 data() const _GLIBCXX_NOEXCEPT
2d60da10 2887 { return _M_data(); }
34a2b755 2888
1a289fa3 2889#if __cplusplus >= 201703L
92d58dee
JW
2890 /**
2891 * @brief Return non-const pointer to contents.
2892 *
2893 * This is a pointer to the character sequence held by the string.
2894 * Modifying the characters in the sequence is allowed.
2895 */
b911ca42 2896 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
92d58dee
JW
2897 _CharT*
2898 data() noexcept
2899 { return _M_data(); }
2900#endif
2901
34a2b755
JW
2902 /**
2903 * @brief Return copy of allocator used to construct this string.
2904 */
b911ca42 2905 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
2906 allocator_type
2907 get_allocator() const _GLIBCXX_NOEXCEPT
2908 { return _M_get_allocator(); }
2909
2910 /**
2911 * @brief Find position of a C substring.
2912 * @param __s C string to locate.
2913 * @param __pos Index of character to search from.
2914 * @param __n Number of characters from @a s to search for.
2915 * @return Index of start of first occurrence.
2916 *
2917 * Starting from @a __pos, searches forward for the first @a
2918 * __n characters in @a __s within this string. If found,
2919 * returns the index where it begins. If not found, returns
2920 * npos.
2921 */
b911ca42 2922 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755 2923 size_type
39a03251
JW
2924 find(const _CharT* __s, size_type __pos, size_type __n) const
2925 _GLIBCXX_NOEXCEPT;
34a2b755
JW
2926
2927 /**
2928 * @brief Find position of a string.
2929 * @param __str String to locate.
2930 * @param __pos Index of character to search from (default 0).
2931 * @return Index of start of first occurrence.
2932 *
2933 * Starting from @a __pos, searches forward for value of @a __str within
2934 * this string. If found, returns the index where it begins. If not
2935 * found, returns npos.
2936 */
b911ca42 2937 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
2938 size_type
2939 find(const basic_string& __str, size_type __pos = 0) const
39a03251 2940 _GLIBCXX_NOEXCEPT
34a2b755
JW
2941 { return this->find(__str.data(), __pos, __str.size()); }
2942
affdef57 2943#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
2944 /**
2945 * @brief Find position of a string_view.
df66af3b 2946 * @param __svt The object convertible to string_view to locate.
ca8f2cb1
VV
2947 * @param __pos Index of character to search from (default 0).
2948 * @return Index of start of first occurrence.
2949 */
df66af3b 2950 template<typename _Tp>
b911ca42 2951 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
df66af3b
DK
2952 _If_sv<_Tp, size_type>
2953 find(const _Tp& __svt, size_type __pos = 0) const
2954 noexcept(is_same<_Tp, __sv_type>::value)
2955 {
2956 __sv_type __sv = __svt;
2957 return this->find(__sv.data(), __pos, __sv.size());
2958 }
ca8f2cb1
VV
2959#endif // C++17
2960
34a2b755
JW
2961 /**
2962 * @brief Find position of a C string.
2963 * @param __s C string to locate.
2964 * @param __pos Index of character to search from (default 0).
2965 * @return Index of start of first occurrence.
2966 *
2967 * Starting from @a __pos, searches forward for the value of @a
2968 * __s within this string. If found, returns the index where
2969 * it begins. If not found, returns npos.
2970 */
b911ca42 2971 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755 2972 size_type
39a03251 2973 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
34a2b755
JW
2974 {
2975 __glibcxx_requires_string(__s);
2976 return this->find(__s, __pos, traits_type::length(__s));
2977 }
2978
2979 /**
2980 * @brief Find position of a character.
2981 * @param __c Character to locate.
2982 * @param __pos Index of character to search from (default 0).
2983 * @return Index of first occurrence.
2984 *
2985 * Starting from @a __pos, searches forward for @a __c within
2986 * this string. If found, returns the index where it was
2987 * found. If not found, returns npos.
2988 */
b911ca42 2989 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
2990 size_type
2991 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
2992
2993 /**
2994 * @brief Find last position of a string.
2995 * @param __str String to locate.
2996 * @param __pos Index of character to search back from (default end).
2997 * @return Index of start of last occurrence.
2998 *
2999 * Starting from @a __pos, searches backward for value of @a
3000 * __str within this string. If found, returns the index where
3001 * it begins. If not found, returns npos.
3002 */
b911ca42 3003 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3004 size_type
3005 rfind(const basic_string& __str, size_type __pos = npos) const
39a03251 3006 _GLIBCXX_NOEXCEPT
34a2b755
JW
3007 { return this->rfind(__str.data(), __pos, __str.size()); }
3008
affdef57 3009#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
3010 /**
3011 * @brief Find last position of a string_view.
df66af3b 3012 * @param __svt The object convertible to string_view to locate.
ca8f2cb1
VV
3013 * @param __pos Index of character to search back from (default end).
3014 * @return Index of start of last occurrence.
3015 */
df66af3b 3016 template<typename _Tp>
b911ca42 3017 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
df66af3b
DK
3018 _If_sv<_Tp, size_type>
3019 rfind(const _Tp& __svt, size_type __pos = npos) const
3020 noexcept(is_same<_Tp, __sv_type>::value)
3021 {
3022 __sv_type __sv = __svt;
3023 return this->rfind(__sv.data(), __pos, __sv.size());
3024 }
ca8f2cb1
VV
3025#endif // C++17
3026
34a2b755
JW
3027 /**
3028 * @brief Find last position of a C substring.
3029 * @param __s C string to locate.
3030 * @param __pos Index of character to search back from.
3031 * @param __n Number of characters from s to search for.
3032 * @return Index of start of last occurrence.
3033 *
3034 * Starting from @a __pos, searches backward for the first @a
3035 * __n characters in @a __s within this string. If found,
3036 * returns the index where it begins. If not found, returns
3037 * npos.
3038 */
b911ca42 3039 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755 3040 size_type
39a03251
JW
3041 rfind(const _CharT* __s, size_type __pos, size_type __n) const
3042 _GLIBCXX_NOEXCEPT;
34a2b755
JW
3043
3044 /**
3045 * @brief Find last position of a C string.
3046 * @param __s C string to locate.
3047 * @param __pos Index of character to start search at (default end).
3048 * @return Index of start of last occurrence.
3049 *
3050 * Starting from @a __pos, searches backward for the value of
3051 * @a __s within this string. If found, returns the index
3052 * where it begins. If not found, returns npos.
3053 */
b911ca42 3054 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3055 size_type
3056 rfind(const _CharT* __s, size_type __pos = npos) const
3057 {
3058 __glibcxx_requires_string(__s);
3059 return this->rfind(__s, __pos, traits_type::length(__s));
3060 }
3061
3062 /**
3063 * @brief Find last position of a character.
3064 * @param __c Character to locate.
3065 * @param __pos Index of character to search back from (default end).
3066 * @return Index of last occurrence.
3067 *
3068 * Starting from @a __pos, searches backward for @a __c within
3069 * this string. If found, returns the index where it was
3070 * found. If not found, returns npos.
3071 */
b911ca42 3072 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3073 size_type
3074 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
3075
3076 /**
3077 * @brief Find position of a character of string.
3078 * @param __str String containing characters to locate.
3079 * @param __pos Index of character to search from (default 0).
3080 * @return Index of first occurrence.
3081 *
3082 * Starting from @a __pos, searches forward for one of the
3083 * characters of @a __str within this string. If found,
3084 * returns the index where it was found. If not found, returns
3085 * npos.
3086 */
b911ca42 3087 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3088 size_type
3089 find_first_of(const basic_string& __str, size_type __pos = 0) const
39a03251 3090 _GLIBCXX_NOEXCEPT
34a2b755
JW
3091 { return this->find_first_of(__str.data(), __pos, __str.size()); }
3092
affdef57 3093#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
3094 /**
3095 * @brief Find position of a character of a string_view.
df66af3b
DK
3096 * @param __svt An object convertible to string_view containing
3097 * characters to locate.
ca8f2cb1
VV
3098 * @param __pos Index of character to search from (default 0).
3099 * @return Index of first occurrence.
3100 */
df66af3b 3101 template<typename _Tp>
b911ca42 3102 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
df66af3b
DK
3103 _If_sv<_Tp, size_type>
3104 find_first_of(const _Tp& __svt, size_type __pos = 0) const
3105 noexcept(is_same<_Tp, __sv_type>::value)
3106 {
3107 __sv_type __sv = __svt;
3108 return this->find_first_of(__sv.data(), __pos, __sv.size());
3109 }
ca8f2cb1
VV
3110#endif // C++17
3111
34a2b755
JW
3112 /**
3113 * @brief Find position of a character of C substring.
3114 * @param __s String containing characters to locate.
3115 * @param __pos Index of character to search from.
3116 * @param __n Number of characters from s to search for.
3117 * @return Index of first occurrence.
3118 *
3119 * Starting from @a __pos, searches forward for one of the
3120 * first @a __n characters of @a __s within this string. If
3121 * found, returns the index where it was found. If not found,
3122 * returns npos.
3123 */
b911ca42 3124 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755 3125 size_type
39a03251
JW
3126 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
3127 _GLIBCXX_NOEXCEPT;
34a2b755
JW
3128
3129 /**
3130 * @brief Find position of a character of C string.
3131 * @param __s String containing characters to locate.
3132 * @param __pos Index of character to search from (default 0).
3133 * @return Index of first occurrence.
3134 *
3135 * Starting from @a __pos, searches forward for one of the
3136 * characters of @a __s within this string. If found, returns
3137 * the index where it was found. If not found, returns npos.
3138 */
b911ca42 3139 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3140 size_type
3141 find_first_of(const _CharT* __s, size_type __pos = 0) const
39a03251 3142 _GLIBCXX_NOEXCEPT
34a2b755
JW
3143 {
3144 __glibcxx_requires_string(__s);
3145 return this->find_first_of(__s, __pos, traits_type::length(__s));
3146 }
3147
3148 /**
3149 * @brief Find position of a character.
3150 * @param __c Character to locate.
3151 * @param __pos Index of character to search from (default 0).
3152 * @return Index of first occurrence.
3153 *
3154 * Starting from @a __pos, searches forward for the character
3155 * @a __c within this string. If found, returns the index
3156 * where it was found. If not found, returns npos.
3157 *
3158 * Note: equivalent to find(__c, __pos).
3159 */
b911ca42 3160 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3161 size_type
3162 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
3163 { return this->find(__c, __pos); }
3164
3165 /**
3166 * @brief Find last position of a character of string.
3167 * @param __str String containing characters to locate.
3168 * @param __pos Index of character to search back from (default end).
3169 * @return Index of last occurrence.
3170 *
3171 * Starting from @a __pos, searches backward for one of the
3172 * characters of @a __str within this string. If found,
3173 * returns the index where it was found. If not found, returns
3174 * npos.
3175 */
b911ca42 3176 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3177 size_type
3178 find_last_of(const basic_string& __str, size_type __pos = npos) const
39a03251 3179 _GLIBCXX_NOEXCEPT
34a2b755
JW
3180 { return this->find_last_of(__str.data(), __pos, __str.size()); }
3181
affdef57 3182#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
3183 /**
3184 * @brief Find last position of a character of string.
df66af3b
DK
3185 * @param __svt An object convertible to string_view containing
3186 * characters to locate.
ca8f2cb1
VV
3187 * @param __pos Index of character to search back from (default end).
3188 * @return Index of last occurrence.
3189 */
df66af3b 3190 template<typename _Tp>
b911ca42 3191 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
df66af3b
DK
3192 _If_sv<_Tp, size_type>
3193 find_last_of(const _Tp& __svt, size_type __pos = npos) const
3194 noexcept(is_same<_Tp, __sv_type>::value)
3195 {
3196 __sv_type __sv = __svt;
3197 return this->find_last_of(__sv.data(), __pos, __sv.size());
3198 }
ca8f2cb1
VV
3199#endif // C++17
3200
34a2b755
JW
3201 /**
3202 * @brief Find last position of a character of C substring.
3203 * @param __s C string containing characters to locate.
3204 * @param __pos Index of character to search back from.
3205 * @param __n Number of characters from s to search for.
3206 * @return Index of last occurrence.
3207 *
3208 * Starting from @a __pos, searches backward for one of the
3209 * first @a __n characters of @a __s within this string. If
3210 * found, returns the index where it was found. If not found,
3211 * returns npos.
3212 */
b911ca42 3213 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755 3214 size_type
39a03251
JW
3215 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
3216 _GLIBCXX_NOEXCEPT;
34a2b755
JW
3217
3218 /**
3219 * @brief Find last position of a character of C string.
3220 * @param __s C string containing characters to locate.
3221 * @param __pos Index of character to search back from (default end).
3222 * @return Index of last occurrence.
3223 *
3224 * Starting from @a __pos, searches backward for one of the
3225 * characters of @a __s within this string. If found, returns
3226 * the index where it was found. If not found, returns npos.
3227 */
b911ca42 3228 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3229 size_type
3230 find_last_of(const _CharT* __s, size_type __pos = npos) const
39a03251 3231 _GLIBCXX_NOEXCEPT
34a2b755
JW
3232 {
3233 __glibcxx_requires_string(__s);
3234 return this->find_last_of(__s, __pos, traits_type::length(__s));
3235 }
3236
3237 /**
3238 * @brief Find last position of a character.
3239 * @param __c Character to locate.
3240 * @param __pos Index of character to search back from (default end).
3241 * @return Index of last occurrence.
3242 *
3243 * Starting from @a __pos, searches backward for @a __c within
3244 * this string. If found, returns the index where it was
3245 * found. If not found, returns npos.
3246 *
3247 * Note: equivalent to rfind(__c, __pos).
3248 */
b911ca42 3249 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3250 size_type
3251 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
3252 { return this->rfind(__c, __pos); }
3253
3254 /**
3255 * @brief Find position of a character not in string.
3256 * @param __str String containing characters to avoid.
3257 * @param __pos Index of character to search from (default 0).
3258 * @return Index of first occurrence.
3259 *
3260 * Starting from @a __pos, searches forward for a character not contained
3261 * in @a __str within this string. If found, returns the index where it
3262 * was found. If not found, returns npos.
3263 */
b911ca42 3264 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3265 size_type
3266 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
39a03251 3267 _GLIBCXX_NOEXCEPT
34a2b755
JW
3268 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
3269
affdef57 3270#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
3271 /**
3272 * @brief Find position of a character not in a string_view.
df66af3b
DK
3273 * @param __svt A object convertible to string_view containing
3274 * characters to avoid.
ca8f2cb1
VV
3275 * @param __pos Index of character to search from (default 0).
3276 * @return Index of first occurrence.
3277 */
df66af3b 3278 template<typename _Tp>
b911ca42 3279 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
df66af3b
DK
3280 _If_sv<_Tp, size_type>
3281 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
3282 noexcept(is_same<_Tp, __sv_type>::value)
3283 {
3284 __sv_type __sv = __svt;
3285 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
3286 }
ca8f2cb1
VV
3287#endif // C++17
3288
34a2b755
JW
3289 /**
3290 * @brief Find position of a character not in C substring.
3291 * @param __s C string containing characters to avoid.
3292 * @param __pos Index of character to search from.
3293 * @param __n Number of characters from __s to consider.
3294 * @return Index of first occurrence.
3295 *
3296 * Starting from @a __pos, searches forward for a character not
3297 * contained in the first @a __n characters of @a __s within
3298 * this string. If found, returns the index where it was
3299 * found. If not found, returns npos.
3300 */
b911ca42 3301 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3302 size_type
3303 find_first_not_of(const _CharT* __s, size_type __pos,
39a03251 3304 size_type __n) const _GLIBCXX_NOEXCEPT;
34a2b755
JW
3305
3306 /**
3307 * @brief Find position of a character not in C string.
3308 * @param __s C string containing characters to avoid.
3309 * @param __pos Index of character to search from (default 0).
3310 * @return Index of first occurrence.
3311 *
3312 * Starting from @a __pos, searches forward for a character not
3313 * contained in @a __s within this string. If found, returns
3314 * the index where it was found. If not found, returns npos.
3315 */
b911ca42 3316 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3317 size_type
3318 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
39a03251 3319 _GLIBCXX_NOEXCEPT
34a2b755
JW
3320 {
3321 __glibcxx_requires_string(__s);
3322 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
3323 }
3324
3325 /**
3326 * @brief Find position of a different character.
3327 * @param __c Character to avoid.
3328 * @param __pos Index of character to search from (default 0).
3329 * @return Index of first occurrence.
3330 *
3331 * Starting from @a __pos, searches forward for a character
3332 * other than @a __c within this string. If found, returns the
3333 * index where it was found. If not found, returns npos.
3334 */
b911ca42 3335 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3336 size_type
3337 find_first_not_of(_CharT __c, size_type __pos = 0) const
39a03251 3338 _GLIBCXX_NOEXCEPT;
34a2b755
JW
3339
3340 /**
3341 * @brief Find last position of a character not in string.
3342 * @param __str String containing characters to avoid.
3343 * @param __pos Index of character to search back from (default end).
3344 * @return Index of last occurrence.
3345 *
3346 * Starting from @a __pos, searches backward for a character
3347 * not contained in @a __str within this string. If found,
3348 * returns the index where it was found. If not found, returns
3349 * npos.
3350 */
b911ca42 3351 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3352 size_type
3353 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
39a03251 3354 _GLIBCXX_NOEXCEPT
34a2b755
JW
3355 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
3356
affdef57 3357#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
3358 /**
3359 * @brief Find last position of a character not in a string_view.
df66af3b
DK
3360 * @param __svt An object convertible to string_view containing
3361 * characters to avoid.
ca8f2cb1
VV
3362 * @param __pos Index of character to search back from (default end).
3363 * @return Index of last occurrence.
3364 */
df66af3b 3365 template<typename _Tp>
b911ca42 3366 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
df66af3b
DK
3367 _If_sv<_Tp, size_type>
3368 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
3369 noexcept(is_same<_Tp, __sv_type>::value)
3370 {
3371 __sv_type __sv = __svt;
3372 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
3373 }
ca8f2cb1
VV
3374#endif // C++17
3375
34a2b755
JW
3376 /**
3377 * @brief Find last position of a character not in C substring.
3378 * @param __s C string containing characters to avoid.
3379 * @param __pos Index of character to search back from.
3380 * @param __n Number of characters from s to consider.
3381 * @return Index of last occurrence.
3382 *
3383 * Starting from @a __pos, searches backward for a character not
3384 * contained in the first @a __n characters of @a __s within this string.
3385 * If found, returns the index where it was found. If not found,
3386 * returns npos.
3387 */
b911ca42 3388 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3389 size_type
3390 find_last_not_of(const _CharT* __s, size_type __pos,
39a03251 3391 size_type __n) const _GLIBCXX_NOEXCEPT;
34a2b755
JW
3392 /**
3393 * @brief Find last position of a character not in C string.
3394 * @param __s C string containing characters to avoid.
3395 * @param __pos Index of character to search back from (default end).
3396 * @return Index of last occurrence.
3397 *
3398 * Starting from @a __pos, searches backward for a character
3399 * not contained in @a __s within this string. If found,
3400 * returns the index where it was found. If not found, returns
3401 * npos.
3402 */
b911ca42 3403 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3404 size_type
3405 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
39a03251 3406 _GLIBCXX_NOEXCEPT
34a2b755
JW
3407 {
3408 __glibcxx_requires_string(__s);
3409 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
3410 }
3411
3412 /**
3413 * @brief Find last position of a different character.
3414 * @param __c Character to avoid.
3415 * @param __pos Index of character to search back from (default end).
3416 * @return Index of last occurrence.
3417 *
3418 * Starting from @a __pos, searches backward for a character other than
3419 * @a __c within this string. If found, returns the index where it was
3420 * found. If not found, returns npos.
3421 */
b911ca42 3422 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3423 size_type
3424 find_last_not_of(_CharT __c, size_type __pos = npos) const
39a03251 3425 _GLIBCXX_NOEXCEPT;
34a2b755
JW
3426
3427 /**
3428 * @brief Get a substring.
3429 * @param __pos Index of first character (default 0).
3430 * @param __n Number of characters in substring (default remainder).
3431 * @return The new string.
3432 * @throw std::out_of_range If __pos > size().
3433 *
3434 * Construct and return a new string using the @a __n
3435 * characters starting at @a __pos. If the string is too
3436 * short, use the remainder of the characters. If @a __pos is
3437 * beyond the end of the string, out_of_range is thrown.
3438 */
b911ca42 3439 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3440 basic_string
3441 substr(size_type __pos = 0, size_type __n = npos) const
3442 { return basic_string(*this,
3443 _M_check(__pos, "basic_string::substr"), __n); }
3444
3445 /**
3446 * @brief Compare to a string.
3447 * @param __str String to compare against.
3448 * @return Integer < 0, 0, or > 0.
3449 *
3450 * Returns an integer < 0 if this string is ordered before @a
3451 * __str, 0 if their values are equivalent, or > 0 if this
3452 * string is ordered after @a __str. Determines the effective
3453 * length rlen of the strings to compare as the smallest of
3454 * size() and str.size(). The function then compares the two
3455 * strings by calling traits::compare(data(), str.data(),rlen).
3456 * If the result of the comparison is nonzero returns it,
3457 * otherwise the shorter one is ordered first.
3458 */
b911ca42 3459 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3460 int
3461 compare(const basic_string& __str) const
3462 {
3463 const size_type __size = this->size();
3464 const size_type __osize = __str.size();
3465 const size_type __len = std::min(__size, __osize);
3466
2d60da10 3467 int __r = traits_type::compare(_M_data(), __str.data(), __len);
34a2b755
JW
3468 if (!__r)
3469 __r = _S_compare(__size, __osize);
3470 return __r;
3471 }
3472
affdef57 3473#ifdef __glibcxx_string_view // >= C++17
ca8f2cb1
VV
3474 /**
3475 * @brief Compare to a string_view.
df66af3b 3476 * @param __svt An object convertible to string_view to compare against.
ca8f2cb1
VV
3477 * @return Integer < 0, 0, or > 0.
3478 */
df66af3b 3479 template<typename _Tp>
b911ca42 3480 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
df66af3b
DK
3481 _If_sv<_Tp, int>
3482 compare(const _Tp& __svt) const
3483 noexcept(is_same<_Tp, __sv_type>::value)
3484 {
3485 __sv_type __sv = __svt;
3486 const size_type __size = this->size();
3487 const size_type __osize = __sv.size();
3488 const size_type __len = std::min(__size, __osize);
3489
3490 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
3491 if (!__r)
3492 __r = _S_compare(__size, __osize);
3493 return __r;
3494 }
ca8f2cb1
VV
3495
3496 /**
3497 * @brief Compare to a string_view.
3498 * @param __pos A position in the string to start comparing from.
3499 * @param __n The number of characters to compare.
df66af3b
DK
3500 * @param __svt An object convertible to string_view to compare
3501 * against.
ca8f2cb1
VV
3502 * @return Integer < 0, 0, or > 0.
3503 */
df66af3b 3504 template<typename _Tp>
b911ca42 3505 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
df66af3b
DK
3506 _If_sv<_Tp, int>
3507 compare(size_type __pos, size_type __n, const _Tp& __svt) const
3508 noexcept(is_same<_Tp, __sv_type>::value)
3509 {
3510 __sv_type __sv = __svt;
3511 return __sv_type(*this).substr(__pos, __n).compare(__sv);
3512 }
ca8f2cb1
VV
3513
3514 /**
3515 * @brief Compare to a string_view.
3516 * @param __pos1 A position in the string to start comparing from.
3517 * @param __n1 The number of characters to compare.
df66af3b
DK
3518 * @param __svt An object convertible to string_view to compare
3519 * against.
ca8f2cb1
VV
3520 * @param __pos2 A position in the string_view to start comparing from.
3521 * @param __n2 The number of characters to compare.
3522 * @return Integer < 0, 0, or > 0.
3523 */
df66af3b 3524 template<typename _Tp>
b911ca42 3525 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
657213f7
JW
3526 _If_sv<_Tp, int>
3527 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
3528 size_type __pos2, size_type __n2 = npos) const
df66af3b 3529 noexcept(is_same<_Tp, __sv_type>::value)
657213f7
JW
3530 {
3531 __sv_type __sv = __svt;
3532 return __sv_type(*this)
3533 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3534 }
ca8f2cb1
VV
3535#endif // C++17
3536
34a2b755
JW
3537 /**
3538 * @brief Compare substring to a string.
3539 * @param __pos Index of first character of substring.
3540 * @param __n Number of characters in substring.
3541 * @param __str String to compare against.
3542 * @return Integer < 0, 0, or > 0.
3543 *
3544 * Form the substring of this string from the @a __n characters
3545 * starting at @a __pos. Returns an integer < 0 if the
3546 * substring is ordered before @a __str, 0 if their values are
3547 * equivalent, or > 0 if the substring is ordered after @a
3548 * __str. Determines the effective length rlen of the strings
3549 * to compare as the smallest of the length of the substring
3550 * and @a __str.size(). The function then compares the two
3551 * strings by calling
3552 * traits::compare(substring.data(),str.data(),rlen). If the
3553 * result of the comparison is nonzero returns it, otherwise
3554 * the shorter one is ordered first.
3555 */
b911ca42 3556 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755 3557 int
1b65779f
JW
3558 compare(size_type __pos, size_type __n, const basic_string& __str) const
3559 {
3560 _M_check(__pos, "basic_string::compare");
3561 __n = _M_limit(__pos, __n);
3562 const size_type __osize = __str.size();
3563 const size_type __len = std::min(__n, __osize);
3564 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
3565 if (!__r)
3566 __r = _S_compare(__n, __osize);
3567 return __r;
3568 }
34a2b755
JW
3569
3570 /**
3571 * @brief Compare substring to a substring.
3572 * @param __pos1 Index of first character of substring.
3573 * @param __n1 Number of characters in substring.
3574 * @param __str String to compare against.
3575 * @param __pos2 Index of first character of substring of str.
3576 * @param __n2 Number of characters in substring of str.
3577 * @return Integer < 0, 0, or > 0.
3578 *
3579 * Form the substring of this string from the @a __n1
3580 * characters starting at @a __pos1. Form the substring of @a
3581 * __str from the @a __n2 characters starting at @a __pos2.
3582 * Returns an integer < 0 if this substring is ordered before
3583 * the substring of @a __str, 0 if their values are equivalent,
3584 * or > 0 if this substring is ordered after the substring of
3585 * @a __str. Determines the effective length rlen of the
3586 * strings to compare as the smallest of the lengths of the
3587 * substrings. The function then compares the two strings by
3588 * calling
3589 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
3590 * If the result of the comparison is nonzero returns it,
3591 * otherwise the shorter one is ordered first.
3592 */
b911ca42 3593 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3594 int
3595 compare(size_type __pos1, size_type __n1, const basic_string& __str,
1b65779f
JW
3596 size_type __pos2, size_type __n2 = npos) const
3597 {
3598 _M_check(__pos1, "basic_string::compare");
3599 __str._M_check(__pos2, "basic_string::compare");
3600 __n1 = _M_limit(__pos1, __n1);
3601 __n2 = __str._M_limit(__pos2, __n2);
3602 const size_type __len = std::min(__n1, __n2);
3603 int __r = traits_type::compare(_M_data() + __pos1,
3604 __str.data() + __pos2, __len);
3605 if (!__r)
3606 __r = _S_compare(__n1, __n2);
3607 return __r;
3608 }
34a2b755
JW
3609
3610 /**
3611 * @brief Compare to a C string.
3612 * @param __s C string to compare against.
3613 * @return Integer < 0, 0, or > 0.
3614 *
3615 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
3616 * their values are equivalent, or > 0 if this string is ordered after
3617 * @a __s. Determines the effective length rlen of the strings to
3618 * compare as the smallest of size() and the length of a string
3619 * constructed from @a __s. The function then compares the two strings
3620 * by calling traits::compare(data(),s,rlen). If the result of the
3621 * comparison is nonzero returns it, otherwise the shorter one is
3622 * ordered first.
3623 */
b911ca42 3624 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755 3625 int
1b65779f
JW
3626 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3627 {
3628 __glibcxx_requires_string(__s);
3629 const size_type __size = this->size();
3630 const size_type __osize = traits_type::length(__s);
3631 const size_type __len = std::min(__size, __osize);
3632 int __r = traits_type::compare(_M_data(), __s, __len);
3633 if (!__r)
3634 __r = _S_compare(__size, __osize);
3635 return __r;
3636 }
34a2b755
JW
3637
3638 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3639 // 5 String::compare specification questionable
3640 /**
3641 * @brief Compare substring to a C string.
3642 * @param __pos Index of first character of substring.
3643 * @param __n1 Number of characters in substring.
3644 * @param __s C string to compare against.
3645 * @return Integer < 0, 0, or > 0.
3646 *
3647 * Form the substring of this string from the @a __n1
3648 * characters starting at @a pos. Returns an integer < 0 if
3649 * the substring is ordered before @a __s, 0 if their values
3650 * are equivalent, or > 0 if the substring is ordered after @a
3651 * __s. Determines the effective length rlen of the strings to
3652 * compare as the smallest of the length of the substring and
3653 * the length of a string constructed from @a __s. The
3654 * function then compares the two string by calling
3655 * traits::compare(substring.data(),__s,rlen). If the result of
3656 * the comparison is nonzero returns it, otherwise the shorter
3657 * one is ordered first.
3658 */
b911ca42 3659 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755 3660 int
1b65779f
JW
3661 compare(size_type __pos, size_type __n1, const _CharT* __s) const
3662 {
3663 __glibcxx_requires_string(__s);
3664 _M_check(__pos, "basic_string::compare");
3665 __n1 = _M_limit(__pos, __n1);
3666 const size_type __osize = traits_type::length(__s);
3667 const size_type __len = std::min(__n1, __osize);
3668 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3669 if (!__r)
3670 __r = _S_compare(__n1, __osize);
3671 return __r;
3672 }
34a2b755
JW
3673
3674 /**
3675 * @brief Compare substring against a character %array.
3676 * @param __pos Index of first character of substring.
3677 * @param __n1 Number of characters in substring.
3678 * @param __s character %array to compare against.
3679 * @param __n2 Number of characters of s.
3680 * @return Integer < 0, 0, or > 0.
3681 *
3682 * Form the substring of this string from the @a __n1
3683 * characters starting at @a __pos. Form a string from the
3684 * first @a __n2 characters of @a __s. Returns an integer < 0
3685 * if this substring is ordered before the string from @a __s,
3686 * 0 if their values are equivalent, or > 0 if this substring
3687 * is ordered after the string from @a __s. Determines the
3688 * effective length rlen of the strings to compare as the
3689 * smallest of the length of the substring and @a __n2. The
3690 * function then compares the two strings by calling
3691 * traits::compare(substring.data(),s,rlen). If the result of
3692 * the comparison is nonzero returns it, otherwise the shorter
3693 * one is ordered first.
3694 *
3695 * NB: s must have at least n2 characters, &apos;\\0&apos; has
3696 * no special meaning.
3697 */
b911ca42 3698 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
34a2b755
JW
3699 int
3700 compare(size_type __pos, size_type __n1, const _CharT* __s,
1b65779f
JW
3701 size_type __n2) const
3702 {
3703 __glibcxx_requires_string_len(__s, __n2);
3704 _M_check(__pos, "basic_string::compare");
3705 __n1 = _M_limit(__pos, __n1);
3706 const size_type __len = std::min(__n1, __n2);
3707 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3708 if (!__r)
3709 __r = _S_compare(__n1, __n2);
3710 return __r;
3711 }
93ef155b 3712
b96e2ff9 3713#if __cplusplus >= 202002L
b911ca42 3714 [[nodiscard]]
b96e2ff9 3715 constexpr bool
5bd624fb
ESR
3716 starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3717 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3718
b911ca42 3719 [[nodiscard]]
b96e2ff9 3720 constexpr bool
5bd624fb
ESR
3721 starts_with(_CharT __x) const noexcept
3722 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3723
b911ca42 3724 [[nodiscard, __gnu__::__nonnull__]]
b96e2ff9 3725 constexpr bool
5bd624fb
ESR
3726 starts_with(const _CharT* __x) const noexcept
3727 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3728
b911ca42 3729 [[nodiscard]]
b96e2ff9 3730 constexpr bool
5bd624fb
ESR
3731 ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3732 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3733
b911ca42 3734 [[nodiscard]]
b96e2ff9 3735 constexpr bool
5bd624fb
ESR
3736 ends_with(_CharT __x) const noexcept
3737 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3738
b911ca42 3739 [[nodiscard, __gnu__::__nonnull__]]
b96e2ff9 3740 constexpr bool
5bd624fb
ESR
3741 ends_with(const _CharT* __x) const noexcept
3742 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3743#endif // C++20
3744
f004d6d9 3745#if __cplusplus > 202002L
b911ca42 3746 [[nodiscard]]
b96e2ff9 3747 constexpr bool
f004d6d9
PF
3748 contains(basic_string_view<_CharT, _Traits> __x) const noexcept
3749 { return __sv_type(this->data(), this->size()).contains(__x); }
3750
b911ca42 3751 [[nodiscard]]
b96e2ff9 3752 constexpr bool
f004d6d9
PF
3753 contains(_CharT __x) const noexcept
3754 { return __sv_type(this->data(), this->size()).contains(__x); }
3755
b911ca42 3756 [[nodiscard, __gnu__::__nonnull__]]
b96e2ff9 3757 constexpr bool
f004d6d9
PF
3758 contains(const _CharT* __x) const noexcept
3759 { return __sv_type(this->data(), this->size()).contains(__x); }
3760#endif // C++23
3761
93ef155b
JW
3762 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3763 template<typename, typename, typename> friend class basic_stringbuf;
3764 };
34a2b755 3765_GLIBCXX_END_NAMESPACE_CXX11
7b527614
JW
3766_GLIBCXX_END_NAMESPACE_VERSION
3767} // namespace std
3768#endif // _GLIBCXX_USE_CXX11_ABI
ec61e852 3769
7b527614
JW
3770namespace std _GLIBCXX_VISIBILITY(default)
3771{
3772_GLIBCXX_BEGIN_NAMESPACE_VERSION
725dc051 3773
6d82c562
JW
3774#if __cpp_deduction_guides >= 201606
3775_GLIBCXX_BEGIN_NAMESPACE_CXX11
3776 template<typename _InputIterator, typename _CharT
3777 = typename iterator_traits<_InputIterator>::value_type,
3778 typename _Allocator = allocator<_CharT>,
3779 typename = _RequireInputIter<_InputIterator>,
3780 typename = _RequireAllocator<_Allocator>>
3781 basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
3782 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
53e926c8
JW
3783
3784 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3785 // 3075. basic_string needs deduction guides from basic_string_view
3786 template<typename _CharT, typename _Traits,
3787 typename _Allocator = allocator<_CharT>,
3788 typename = _RequireAllocator<_Allocator>>
3789 basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
3790 -> basic_string<_CharT, _Traits, _Allocator>;
3791
3792 template<typename _CharT, typename _Traits,
3793 typename _Allocator = allocator<_CharT>,
3794 typename = _RequireAllocator<_Allocator>>
3795 basic_string(basic_string_view<_CharT, _Traits>,
3796 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3797 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3798 const _Allocator& = _Allocator())
3799 -> basic_string<_CharT, _Traits, _Allocator>;
882d3b31 3800
ae54d8cb 3801#if __glibcxx_containers_ranges // C++ >= 23
882d3b31
JW
3802 template<ranges::input_range _Rg,
3803 typename _Allocator = allocator<ranges::range_value_t<_Rg>>>
3804 basic_string(from_range_t, _Rg&&, _Allocator = _Allocator())
3805 -> basic_string<ranges::range_value_t<_Rg>,
3806 char_traits<ranges::range_value_t<_Rg>>,
3807 _Allocator>;
3808#endif
6d82c562
JW
3809_GLIBCXX_END_NAMESPACE_CXX11
3810#endif
3811
c93baa93
WH
3812 template<typename _Str>
3813 _GLIBCXX20_CONSTEXPR
3814 inline _Str
3815 __str_concat(typename _Str::value_type const* __lhs,
3816 typename _Str::size_type __lhs_len,
3817 typename _Str::value_type const* __rhs,
3818 typename _Str::size_type __rhs_len,
3819 typename _Str::allocator_type const& __a)
3820 {
3821 typedef typename _Str::allocator_type allocator_type;
3822 typedef __gnu_cxx::__alloc_traits<allocator_type> _Alloc_traits;
3823 _Str __str(_Alloc_traits::_S_select_on_copy(__a));
3824 __str.reserve(__lhs_len + __rhs_len);
3825 __str.append(__lhs, __lhs_len);
3826 __str.append(__rhs, __rhs_len);
3827 return __str;
3828 }
3829
725dc051 3830 // operator+
284f19bf
JQ
3831 /**
3832 * @brief Concatenate two strings.
93c66bc6
BK
3833 * @param __lhs First string.
3834 * @param __rhs Last string.
3835 * @return New string with value of @a __lhs followed by @a __rhs.
ed6814f7 3836 */
41b8e86c 3837 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 3838 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
c93baa93 3839 inline basic_string<_CharT, _Traits, _Alloc>
725dc051
BK
3840 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3841 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
3842 {
c93baa93
WH
3843 typedef basic_string<_CharT, _Traits, _Alloc> _Str;
3844 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3845 __rhs.c_str(), __rhs.size(),
3846 __lhs.get_allocator());
725dc051
BK
3847 }
3848
284f19bf
JQ
3849 /**
3850 * @brief Concatenate C string and string.
93c66bc6
BK
3851 * @param __lhs First string.
3852 * @param __rhs Last string.
3853 * @return New string with value of @a __lhs followed by @a __rhs.
ed6814f7 3854 */
725dc051 3855 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 3856 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
c93baa93 3857 inline basic_string<_CharT,_Traits,_Alloc>
725dc051 3858 operator+(const _CharT* __lhs,
c93baa93
WH
3859 const basic_string<_CharT,_Traits,_Alloc>& __rhs)
3860 {
3861 __glibcxx_requires_string(__lhs);
3862 typedef basic_string<_CharT, _Traits, _Alloc> _Str;
3863 return std::__str_concat<_Str>(__lhs, _Traits::length(__lhs),
3864 __rhs.c_str(), __rhs.size(),
3865 __rhs.get_allocator());
3866 }
725dc051 3867
284f19bf
JQ
3868 /**
3869 * @brief Concatenate character and string.
93c66bc6
BK
3870 * @param __lhs First string.
3871 * @param __rhs Last string.
3872 * @return New string with @a __lhs followed by @a __rhs.
ed6814f7 3873 */
725dc051 3874 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 3875 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
c93baa93
WH
3876 inline basic_string<_CharT,_Traits,_Alloc>
3877 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs)
3878 {
3879 typedef basic_string<_CharT, _Traits, _Alloc> _Str;
3880 return std::__str_concat<_Str>(__builtin_addressof(__lhs), 1,
3881 __rhs.c_str(), __rhs.size(),
3882 __rhs.get_allocator());
3883 }
725dc051 3884
284f19bf
JQ
3885 /**
3886 * @brief Concatenate string and C string.
93c66bc6
BK
3887 * @param __lhs First string.
3888 * @param __rhs Last string.
3889 * @return New string with @a __lhs followed by @a __rhs.
ed6814f7 3890 */
725dc051 3891 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 3892 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
e5428086 3893 inline basic_string<_CharT, _Traits, _Alloc>
725dc051 3894 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
e5428086
JW
3895 const _CharT* __rhs)
3896 {
c93baa93
WH
3897 __glibcxx_requires_string(__rhs);
3898 typedef basic_string<_CharT, _Traits, _Alloc> _Str;
3899 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3900 __rhs, _Traits::length(__rhs),
3901 __lhs.get_allocator());
e5428086 3902 }
284f19bf
JQ
3903 /**
3904 * @brief Concatenate string and character.
93c66bc6
BK
3905 * @param __lhs First string.
3906 * @param __rhs Last string.
3907 * @return New string with @a __lhs followed by @a __rhs.
ed6814f7 3908 */
725dc051 3909 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 3910 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
725dc051
BK
3911 inline basic_string<_CharT, _Traits, _Alloc>
3912 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
3913 {
c93baa93
WH
3914 typedef basic_string<_CharT, _Traits, _Alloc> _Str;
3915 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3916 __builtin_addressof(__rhs), 1,
3917 __lhs.get_allocator());
725dc051
BK
3918 }
3919
734f5023 3920#if __cplusplus >= 201103L
ce99f498 3921 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 3922 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
ce99f498
PC
3923 inline basic_string<_CharT, _Traits, _Alloc>
3924 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3925 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
3926 { return std::move(__lhs.append(__rhs)); }
3927
3928 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3929 _GLIBCXX20_CONSTEXPR
ce99f498
PC
3930 inline basic_string<_CharT, _Traits, _Alloc>
3931 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3932 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
3933 { return std::move(__rhs.insert(0, __lhs)); }
3934
3935 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 3936 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
ce99f498
PC
3937 inline basic_string<_CharT, _Traits, _Alloc>
3938 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3939 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
37a68925 3940 {
02537455
JW
3941#pragma GCC diagnostic push
3942#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
3943 // Return value must use __lhs.get_allocator(), but if __rhs has equal
3944 // allocator then we can choose which parameter to modify in-place.
f4e678ef 3945 bool __use_rhs = false;
02537455 3946 if constexpr (allocator_traits<_Alloc>::is_always_equal::value)
f4e678ef
NDR
3947 __use_rhs = true;
3948 else if (__lhs.get_allocator() == __rhs.get_allocator())
3949 __use_rhs = true;
3950 if (__use_rhs)
f4e678ef
NDR
3951 {
3952 const auto __size = __lhs.size() + __rhs.size();
3953 if (__size > __lhs.capacity() && __size <= __rhs.capacity())
3954 return std::move(__rhs.insert(0, __lhs));
3955 }
3956 return std::move(__lhs.append(__rhs));
02537455 3957#pragma GCC diagnostic pop
37a68925 3958 }
ce99f498
PC
3959
3960 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 3961 _GLIBCXX_NODISCARD _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
ce99f498
PC
3962 inline basic_string<_CharT, _Traits, _Alloc>
3963 operator+(const _CharT* __lhs,
3964 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
3965 { return std::move(__rhs.insert(0, __lhs)); }
3966
3967 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 3968 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
ce99f498 3969 inline basic_string<_CharT, _Traits, _Alloc>
37a68925
PC
3970 operator+(_CharT __lhs,
3971 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
ce99f498
PC
3972 { return std::move(__rhs.insert(0, 1, __lhs)); }
3973
3974 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 3975 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
ce99f498
PC
3976 inline basic_string<_CharT, _Traits, _Alloc>
3977 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3978 const _CharT* __rhs)
3979 { return std::move(__lhs.append(__rhs)); }
3980
3981 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 3982 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
ce99f498
PC
3983 inline basic_string<_CharT, _Traits, _Alloc>
3984 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3985 _CharT __rhs)
3986 { return std::move(__lhs.append(1, __rhs)); }
3987#endif
3988
813afa28
GA
3989#if __glibcxx_string_view >= 202403L
3990 // const string & + string_view
3991 template<typename _CharT, typename _Traits, typename _Alloc>
3992 [[nodiscard]]
3993 constexpr basic_string<_CharT, _Traits, _Alloc>
3994 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3995 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs)
3996 {
3997 using _Str = basic_string<_CharT, _Traits, _Alloc>;
3998 return std::__str_concat<_Str>(__lhs.data(), __lhs.size(),
3999 __rhs.data(), __rhs.size(),
4000 __lhs.get_allocator());
4001 }
4002
4003 // string && + string_view
4004 template<typename _CharT, typename _Traits, typename _Alloc>
4005 [[nodiscard]]
4006 constexpr basic_string<_CharT, _Traits, _Alloc>
4007 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
4008 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs)
4009 {
4010 return std::move(__lhs.append(__rhs));
4011 }
4012
4013 // string_view + const string &
4014 template<typename _CharT, typename _Traits, typename _Alloc>
4015 [[nodiscard]]
4016 constexpr basic_string<_CharT, _Traits, _Alloc>
4017 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
4018 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4019 {
4020 using _Str = basic_string<_CharT, _Traits, _Alloc>;
4021 return std::__str_concat<_Str>(__lhs.data(), __lhs.size(),
4022 __rhs.data(), __rhs.size(),
4023 __rhs.get_allocator());
4024 }
4025
4026 // string_view + string &&
4027 template<typename _CharT, typename _Traits, typename _Alloc>
4028 [[nodiscard]]
4029 constexpr basic_string<_CharT, _Traits, _Alloc>
4030 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
4031 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
4032 {
4033 return std::move(__rhs.insert(0, __lhs));
4034 }
4035#endif
4036
725dc051 4037 // operator ==
284f19bf
JQ
4038 /**
4039 * @brief Test equivalence of two strings.
93c66bc6
BK
4040 * @param __lhs First string.
4041 * @param __rhs Second string.
4042 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
ed6814f7 4043 */
725dc051 4044 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4045 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
725dc051
BK
4046 inline bool
4047 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4048 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 4049 _GLIBCXX_NOEXCEPT
6abe3415
JW
4050 {
4051 return __lhs.size() == __rhs.size()
4052 && !_Traits::compare(__lhs.data(), __rhs.data(), __lhs.size());
4053 }
bd12160a 4054
284f19bf
JQ
4055 /**
4056 * @brief Test equivalence of string and C string.
93c66bc6
BK
4057 * @param __lhs String.
4058 * @param __rhs C string.
4059 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
ed6814f7 4060 */
725dc051 4061 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4062 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
725dc051
BK
4063 inline bool
4064 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4065 const _CharT* __rhs)
6abe3415
JW
4066 {
4067 return __lhs.size() == _Traits::length(__rhs)
4068 && !_Traits::compare(__lhs.data(), __rhs, __lhs.size());
4069 }
725dc051 4070
875d6cb3
JW
4071#if __cpp_lib_three_way_comparison
4072 /**
4073 * @brief Three-way comparison of a string and a C string.
4074 * @param __lhs A string.
4075 * @param __rhs A null-terminated string.
4076 * @return A value indicating whether `__lhs` is less than, equal to,
4077 * greater than, or incomparable with `__rhs`.
4078 */
4079 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4080 [[nodiscard]]
73e4d9f1 4081 constexpr auto
875d6cb3
JW
4082 operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4083 const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept
4084 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
4085 { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
4086
4087 /**
4088 * @brief Three-way comparison of a string and a C string.
4089 * @param __lhs A string.
4090 * @param __rhs A null-terminated string.
4091 * @return A value indicating whether `__lhs` is less than, equal to,
4092 * greater than, or incomparable with `__rhs`.
4093 */
4094 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4095 [[nodiscard]]
73e4d9f1 4096 constexpr auto
875d6cb3
JW
4097 operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4098 const _CharT* __rhs) noexcept
4099 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
4100 { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
4101#else
4102 /**
4103 * @brief Test equivalence of C string and string.
4104 * @param __lhs C string.
4105 * @param __rhs String.
4106 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
4107 */
4108 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4109 _GLIBCXX_NODISCARD
875d6cb3
JW
4110 inline bool
4111 operator==(const _CharT* __lhs,
4112 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6abe3415 4113 { return __rhs == __lhs; }
875d6cb3 4114
725dc051 4115 // operator !=
284f19bf
JQ
4116 /**
4117 * @brief Test difference of two strings.
93c66bc6
BK
4118 * @param __lhs First string.
4119 * @param __rhs Second string.
4120 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
ed6814f7 4121 */
725dc051 4122 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4123 _GLIBCXX_NODISCARD
725dc051
BK
4124 inline bool
4125 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4126 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 4127 _GLIBCXX_NOEXCEPT
bd12160a 4128 { return !(__lhs == __rhs); }
725dc051 4129
284f19bf
JQ
4130 /**
4131 * @brief Test difference of C string and string.
93c66bc6
BK
4132 * @param __lhs C string.
4133 * @param __rhs String.
4134 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
ed6814f7 4135 */
725dc051 4136 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4137 _GLIBCXX_NODISCARD
725dc051
BK
4138 inline bool
4139 operator!=(const _CharT* __lhs,
4140 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6abe3415 4141 { return !(__rhs == __lhs); }
725dc051 4142
284f19bf
JQ
4143 /**
4144 * @brief Test difference of string and C string.
93c66bc6
BK
4145 * @param __lhs String.
4146 * @param __rhs C string.
4147 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
ed6814f7 4148 */
725dc051 4149 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4150 _GLIBCXX_NODISCARD
725dc051
BK
4151 inline bool
4152 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4153 const _CharT* __rhs)
bd12160a 4154 { return !(__lhs == __rhs); }
725dc051
BK
4155
4156 // operator <
284f19bf
JQ
4157 /**
4158 * @brief Test if string precedes string.
93c66bc6
BK
4159 * @param __lhs First string.
4160 * @param __rhs Second string.
4161 * @return True if @a __lhs precedes @a __rhs. False otherwise.
ed6814f7 4162 */
725dc051 4163 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4164 _GLIBCXX_NODISCARD
725dc051
BK
4165 inline bool
4166 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4167 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 4168 _GLIBCXX_NOEXCEPT
725dc051
BK
4169 { return __lhs.compare(__rhs) < 0; }
4170
284f19bf
JQ
4171 /**
4172 * @brief Test if string precedes C string.
93c66bc6
BK
4173 * @param __lhs String.
4174 * @param __rhs C string.
4175 * @return True if @a __lhs precedes @a __rhs. False otherwise.
ed6814f7 4176 */
725dc051 4177 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4178 _GLIBCXX_NODISCARD
725dc051
BK
4179 inline bool
4180 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4181 const _CharT* __rhs)
4182 { return __lhs.compare(__rhs) < 0; }
4183
284f19bf
JQ
4184 /**
4185 * @brief Test if C string precedes string.
93c66bc6
BK
4186 * @param __lhs C string.
4187 * @param __rhs String.
4188 * @return True if @a __lhs precedes @a __rhs. False otherwise.
ed6814f7 4189 */
725dc051 4190 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4191 _GLIBCXX_NODISCARD
725dc051
BK
4192 inline bool
4193 operator<(const _CharT* __lhs,
4194 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4195 { return __rhs.compare(__lhs) > 0; }
4196
4197 // operator >
284f19bf
JQ
4198 /**
4199 * @brief Test if string follows string.
93c66bc6
BK
4200 * @param __lhs First string.
4201 * @param __rhs Second string.
4202 * @return True if @a __lhs follows @a __rhs. False otherwise.
ed6814f7 4203 */
725dc051 4204 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4205 _GLIBCXX_NODISCARD
725dc051
BK
4206 inline bool
4207 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4208 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 4209 _GLIBCXX_NOEXCEPT
725dc051
BK
4210 { return __lhs.compare(__rhs) > 0; }
4211
284f19bf
JQ
4212 /**
4213 * @brief Test if string follows C string.
93c66bc6
BK
4214 * @param __lhs String.
4215 * @param __rhs C string.
4216 * @return True if @a __lhs follows @a __rhs. False otherwise.
ed6814f7 4217 */
725dc051 4218 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4219 _GLIBCXX_NODISCARD
725dc051
BK
4220 inline bool
4221 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4222 const _CharT* __rhs)
4223 { return __lhs.compare(__rhs) > 0; }
4224
284f19bf
JQ
4225 /**
4226 * @brief Test if C string follows string.
93c66bc6
BK
4227 * @param __lhs C string.
4228 * @param __rhs String.
4229 * @return True if @a __lhs follows @a __rhs. False otherwise.
ed6814f7 4230 */
725dc051 4231 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4232 _GLIBCXX_NODISCARD
725dc051
BK
4233 inline bool
4234 operator>(const _CharT* __lhs,
4235 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4236 { return __rhs.compare(__lhs) < 0; }
4237
4238 // operator <=
284f19bf
JQ
4239 /**
4240 * @brief Test if string doesn't follow string.
93c66bc6
BK
4241 * @param __lhs First string.
4242 * @param __rhs Second string.
4243 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
ed6814f7 4244 */
725dc051 4245 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4246 _GLIBCXX_NODISCARD
725dc051
BK
4247 inline bool
4248 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4249 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 4250 _GLIBCXX_NOEXCEPT
725dc051
BK
4251 { return __lhs.compare(__rhs) <= 0; }
4252
284f19bf
JQ
4253 /**
4254 * @brief Test if string doesn't follow C string.
93c66bc6
BK
4255 * @param __lhs String.
4256 * @param __rhs C string.
4257 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
ed6814f7 4258 */
725dc051 4259 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4260 _GLIBCXX_NODISCARD
725dc051
BK
4261 inline bool
4262 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4263 const _CharT* __rhs)
4264 { return __lhs.compare(__rhs) <= 0; }
4265
284f19bf
JQ
4266 /**
4267 * @brief Test if C string doesn't follow string.
93c66bc6
BK
4268 * @param __lhs C string.
4269 * @param __rhs String.
4270 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
ed6814f7 4271 */
725dc051 4272 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4273 _GLIBCXX_NODISCARD
725dc051
BK
4274 inline bool
4275 operator<=(const _CharT* __lhs,
4276 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
e9fb72e8 4277 { return __rhs.compare(__lhs) >= 0; }
725dc051
BK
4278
4279 // operator >=
284f19bf
JQ
4280 /**
4281 * @brief Test if string doesn't precede string.
93c66bc6
BK
4282 * @param __lhs First string.
4283 * @param __rhs Second string.
4284 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
ed6814f7 4285 */
725dc051 4286 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4287 _GLIBCXX_NODISCARD
725dc051
BK
4288 inline bool
4289 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4290 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 4291 _GLIBCXX_NOEXCEPT
725dc051
BK
4292 { return __lhs.compare(__rhs) >= 0; }
4293
284f19bf
JQ
4294 /**
4295 * @brief Test if string doesn't precede C string.
93c66bc6
BK
4296 * @param __lhs String.
4297 * @param __rhs C string.
4298 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
ed6814f7 4299 */
725dc051 4300 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4301 _GLIBCXX_NODISCARD
725dc051
BK
4302 inline bool
4303 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4304 const _CharT* __rhs)
4305 { return __lhs.compare(__rhs) >= 0; }
4306
284f19bf
JQ
4307 /**
4308 * @brief Test if C string doesn't precede string.
93c66bc6
BK
4309 * @param __lhs C string.
4310 * @param __rhs String.
4311 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
ed6814f7 4312 */
725dc051 4313 template<typename _CharT, typename _Traits, typename _Alloc>
b911ca42 4314 _GLIBCXX_NODISCARD
725dc051
BK
4315 inline bool
4316 operator>=(const _CharT* __lhs,
4317 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4318 { return __rhs.compare(__lhs) <= 0; }
875d6cb3 4319#endif // three-way comparison
725dc051 4320
284f19bf
JQ
4321 /**
4322 * @brief Swap contents of two strings.
93c66bc6
BK
4323 * @param __lhs First string.
4324 * @param __rhs Second string.
284f19bf 4325 *
93c66bc6 4326 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
284f19bf 4327 */
725dc051 4328 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 4329 _GLIBCXX20_CONSTEXPR
725dc051
BK
4330 inline void
4331 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
4332 basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 4333 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
725dc051
BK
4334 { __lhs.swap(__rhs); }
4335
34a2b755 4336
284f19bf
JQ
4337 /**
4338 * @brief Read stream into a string.
93c66bc6
BK
4339 * @param __is Input stream.
4340 * @param __str Buffer to store into.
284f19bf
JQ
4341 * @return Reference to the input stream.
4342 *
93c66bc6
BK
4343 * Stores characters from @a __is into @a __str until whitespace is
4344 * found, the end of the stream is encountered, or str.max_size()
4345 * is reached. If is.width() is non-zero, that is the limit on the
4346 * number of characters stored into @a __str. Any previous
4347 * contents of @a __str are erased.
284f19bf 4348 */
725dc051
BK
4349 template<typename _CharT, typename _Traits, typename _Alloc>
4350 basic_istream<_CharT, _Traits>&
4351 operator>>(basic_istream<_CharT, _Traits>& __is,
4352 basic_string<_CharT, _Traits, _Alloc>& __str);
4353
ceed88b1
PC
4354 template<>
4355 basic_istream<char>&
4356 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
4357
284f19bf
JQ
4358 /**
4359 * @brief Write string to a stream.
93c66bc6
BK
4360 * @param __os Output stream.
4361 * @param __str String to write out.
284f19bf
JQ
4362 * @return Reference to the output stream.
4363 *
93c66bc6 4364 * Output characters of @a __str into os following the same rules as for
284f19bf
JQ
4365 * writing a C string.
4366 */
725dc051 4367 template<typename _CharT, typename _Traits, typename _Alloc>
70c99f6c 4368 inline basic_ostream<_CharT, _Traits>&
725dc051 4369 operator<<(basic_ostream<_CharT, _Traits>& __os,
ec2061a9
PC
4370 const basic_string<_CharT, _Traits, _Alloc>& __str)
4371 {
4372 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4373 // 586. string inserter not a formatted function
11202768 4374 return __ostream_insert(__os, __str.data(), __str.size());
ec2061a9 4375 }
725dc051 4376
284f19bf
JQ
4377 /**
4378 * @brief Read a line from stream into a string.
93c66bc6
BK
4379 * @param __is Input stream.
4380 * @param __str Buffer to store into.
4381 * @param __delim Character marking end of line.
284f19bf
JQ
4382 * @return Reference to the input stream.
4383 *
93c66bc6
BK
4384 * Stores characters from @a __is into @a __str until @a __delim is
4385 * found, the end of the stream is encountered, or str.max_size()
3b2453a9
JW
4386 * is reached. Any previous contents of @a __str are erased. If
4387 * @a __delim is encountered, it is extracted but not stored into
4388 * @a __str.
284f19bf 4389 */
725dc051 4390 template<typename _CharT, typename _Traits, typename _Alloc>
e9fb72e8 4391 basic_istream<_CharT, _Traits>&
725dc051
BK
4392 getline(basic_istream<_CharT, _Traits>& __is,
4393 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
4394
284f19bf
JQ
4395 /**
4396 * @brief Read a line from stream into a string.
93c66bc6
BK
4397 * @param __is Input stream.
4398 * @param __str Buffer to store into.
284f19bf
JQ
4399 * @return Reference to the input stream.
4400 *
93c66bc6 4401 * Stores characters from is into @a __str until &apos;\n&apos; is
2a60a9f6 4402 * found, the end of the stream is encountered, or str.max_size()
3b2453a9
JW
4403 * is reached. Any previous contents of @a __str are erased. If
4404 * end of line is encountered, it is extracted but not stored into
4405 * @a __str.
284f19bf 4406 */
725dc051 4407 template<typename _CharT, typename _Traits, typename _Alloc>
e9fb72e8 4408 inline basic_istream<_CharT, _Traits>&
725dc051 4409 getline(basic_istream<_CharT, _Traits>& __is,
70c99f6c 4410 basic_string<_CharT, _Traits, _Alloc>& __str)
19173661
JW
4411 { return std::getline(__is, __str, __is.widen('\n')); }
4412
4413#if __cplusplus >= 201103L
4414 /// Read a line from an rvalue stream into a string.
4415 template<typename _CharT, typename _Traits, typename _Alloc>
caff45a6 4416 inline basic_istream<_CharT, _Traits>&
19173661
JW
4417 getline(basic_istream<_CharT, _Traits>&& __is,
4418 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
4419 { return std::getline(__is, __str, __delim); }
4420
4421 /// Read a line from an rvalue stream into a string.
4422 template<typename _CharT, typename _Traits, typename _Alloc>
4423 inline basic_istream<_CharT, _Traits>&
4424 getline(basic_istream<_CharT, _Traits>&& __is,
4425 basic_string<_CharT, _Traits, _Alloc>& __str)
4426 { return std::getline(__is, __str); }
4427#endif
70c99f6c 4428
e9fb72e8
PC
4429 template<>
4430 basic_istream<char>&
4431 getline(basic_istream<char>& __in, basic_string<char>& __str,
4432 char __delim);
4433
4434#ifdef _GLIBCXX_USE_WCHAR_T
4435 template<>
4436 basic_istream<wchar_t>&
4437 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
4438 wchar_t __delim);
45ab93d9 4439#endif
3cbc7af0 4440
12ffa228
BK
4441_GLIBCXX_END_NAMESPACE_VERSION
4442} // namespace
7364f286 4443
23c64853 4444#if __cplusplus >= 201103L
7364f286 4445
a5a6b586 4446#include <ext/string_conversions.h>
cd0b94e6 4447#include <bits/charconv.h>
7364f286 4448
12ffa228
BK
4449namespace std _GLIBCXX_VISIBILITY(default)
4450{
4451_GLIBCXX_BEGIN_NAMESPACE_VERSION
34a2b755 4452_GLIBCXX_BEGIN_NAMESPACE_CXX11
7364f286 4453
a5a6b586
PC
4454 // 21.4 Numeric Conversions [string.conversions].
4455 inline int
4456 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
4457 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
4458 __idx, __base); }
4459
4460 inline long
4461 stol(const string& __str, size_t* __idx = 0, int __base = 10)
4462 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
4463 __idx, __base); }
4464
4465 inline unsigned long
4466 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
4467 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
4468 __idx, __base); }
4469
a1d12752 4470#if _GLIBCXX_USE_C99_STDLIB
a5a6b586
PC
4471 inline long long
4472 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
4473 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
4474 __idx, __base); }
4475
4476 inline unsigned long long
4477 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
4478 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
4479 __idx, __base); }
a1d12752
JW
4480#elif __LONG_WIDTH__ == __LONG_LONG_WIDTH__
4481 inline long long
4482 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
4483 { return std::stol(__str, __idx, __base); }
a5a6b586 4484
a1d12752
JW
4485 inline unsigned long long
4486 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
4487 { return std::stoul(__str, __idx, __base); }
4488#endif
a5a6b586
PC
4489
4490 inline double
4491 stod(const string& __str, size_t* __idx = 0)
4492 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
4493
31c3b67d 4494#if _GLIBCXX_HAVE_STRTOF
a1d12752
JW
4495 // NB: strtof vs strtod.
4496 inline float
4497 stof(const string& __str, size_t* __idx = 0)
4498 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
f0b0c211
JW
4499#else
4500 inline float
4501 stof(const string& __str, size_t* __idx = 0)
4502 {
4503 double __d = std::stod(__str, __idx);
31c3b67d 4504 if (__builtin_isfinite(__d) && __d != 0.0)
f0b0c211
JW
4505 {
4506 double __abs_d = __builtin_fabs(__d);
4507 if (__abs_d < __FLT_MIN__ || __abs_d > __FLT_MAX__)
4508 {
4509 errno = ERANGE;
4510 std::__throw_out_of_range("stof");
4511 }
4512 }
4513 return __d;
4514 }
58303d42 4515#endif
a1d12752 4516
31c3b67d 4517#if _GLIBCXX_HAVE_STRTOLD && ! _GLIBCXX_HAVE_BROKEN_STRTOLD
a5a6b586
PC
4518 inline long double
4519 stold(const string& __str, size_t* __idx = 0)
4520 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
a1d12752
JW
4521#elif __DBL_MANT_DIG__ == __LDBL_MANT_DIG__
4522 inline long double
4523 stold(const string& __str, size_t* __idx = 0)
4524 { return std::stod(__str, __idx); }
58303d42 4525#endif
a5a6b586 4526
aeed687f 4527 // _GLIBCXX_RESOLVE_LIB_DEFECTS
cd0b94e6 4528 // DR 1261. Insufficent overloads for to_string / to_wstring
a4ecd144 4529
b911ca42 4530 _GLIBCXX_NODISCARD
a4ecd144
PC
4531 inline string
4532 to_string(int __val)
9d813ddd
JW
4533#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4534 noexcept // any 32-bit value fits in the SSO buffer
4535#endif
cd0b94e6
JW
4536 {
4537 const bool __neg = __val < 0;
4538 const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
4539 const auto __len = __detail::__to_chars_len(__uval);
51ec07b1
JW
4540 string __str;
4541 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4542 __p[0] = '-';
4543 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4544 return __n;
4545 });
cd0b94e6
JW
4546 return __str;
4547 }
a4ecd144 4548
b911ca42 4549 _GLIBCXX_NODISCARD
a4ecd144
PC
4550 inline string
4551 to_string(unsigned __val)
9d813ddd
JW
4552#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4553 noexcept // any 32-bit value fits in the SSO buffer
4554#endif
cd0b94e6 4555 {
51ec07b1
JW
4556 const auto __len = __detail::__to_chars_len(__val);
4557 string __str;
4558 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4559 __detail::__to_chars_10_impl(__p, __n, __val);
4560 return __n;
4561 });
cd0b94e6
JW
4562 return __str;
4563 }
a4ecd144 4564
b911ca42 4565 _GLIBCXX_NODISCARD
a4ecd144
PC
4566 inline string
4567 to_string(long __val)
9d813ddd
JW
4568#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4569 noexcept // any 32-bit value fits in the SSO buffer
4570#endif
cd0b94e6
JW
4571 {
4572 const bool __neg = __val < 0;
4573 const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
4574 const auto __len = __detail::__to_chars_len(__uval);
51ec07b1
JW
4575 string __str;
4576 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4577 __p[0] = '-';
4578 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4579 return __n;
4580 });
cd0b94e6
JW
4581 return __str;
4582 }
a4ecd144 4583
b911ca42 4584 _GLIBCXX_NODISCARD
a4ecd144
PC
4585 inline string
4586 to_string(unsigned long __val)
9d813ddd
JW
4587#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4588 noexcept // any 32-bit value fits in the SSO buffer
4589#endif
cd0b94e6 4590 {
51ec07b1
JW
4591 const auto __len = __detail::__to_chars_len(__val);
4592 string __str;
4593 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4594 __detail::__to_chars_10_impl(__p, __n, __val);
4595 return __n;
4596 });
cd0b94e6
JW
4597 return __str;
4598 }
a4ecd144 4599
b911ca42 4600 _GLIBCXX_NODISCARD
a5a6b586
PC
4601 inline string
4602 to_string(long long __val)
cd0b94e6
JW
4603 {
4604 const bool __neg = __val < 0;
4605 const unsigned long long __uval
4606 = __neg ? (unsigned long long)~__val + 1ull : __val;
4607 const auto __len = __detail::__to_chars_len(__uval);
51ec07b1
JW
4608 string __str;
4609 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4610 __p[0] = '-';
4611 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4612 return __n;
4613 });
cd0b94e6
JW
4614 return __str;
4615 }
a5a6b586 4616
b911ca42 4617 _GLIBCXX_NODISCARD
a5a6b586
PC
4618 inline string
4619 to_string(unsigned long long __val)
cd0b94e6 4620 {
51ec07b1
JW
4621 const auto __len = __detail::__to_chars_len(__val);
4622 string __str;
4623 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4624 __detail::__to_chars_10_impl(__p, __n, __val);
4625 return __n;
4626 });
cd0b94e6
JW
4627 return __str;
4628 }
4629
7ffa63df 4630#if __glibcxx_to_string >= 202306L // C++ >= 26
aeed687f
JW
4631
4632 [[nodiscard]]
4633 inline string
4634 to_string(float __val)
4635 {
4636 string __str;
4637 size_t __len = 15;
4638 do {
4639 __str.resize_and_overwrite(__len,
4640 [__val, &__len] (char* __p, size_t __n) {
4641 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4642 if (__err == errc{}) [[likely]]
4643 return __end - __p;
4644 __len *= 2;
4645 return __p - __p;;
4646 });
4647 } while (__str.empty());
4648 return __str;
4649 }
4650
4651 [[nodiscard]]
4652 inline string
4653 to_string(double __val)
4654 {
4655 string __str;
4656 size_t __len = 15;
4657 do {
4658 __str.resize_and_overwrite(__len,
4659 [__val, &__len] (char* __p, size_t __n) {
4660 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4661 if (__err == errc{}) [[likely]]
4662 return __end - __p;
4663 __len *= 2;
4664 return __p - __p;;
4665 });
4666 } while (__str.empty());
4667 return __str;
4668 }
4669
4670 [[nodiscard]]
4671 inline string
4672 to_string(long double __val)
4673 {
4674 string __str;
4675 size_t __len = 15;
4676 do {
4677 __str.resize_and_overwrite(__len,
4678 [__val, &__len] (char* __p, size_t __n) {
4679 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4680 if (__err == errc{}) [[likely]]
4681 return __end - __p;
4682 __len *= 2;
4683 return __p - __p;;
4684 });
4685 } while (__str.empty());
4686 return __str;
4687 }
4688#elif _GLIBCXX_USE_C99_STDIO
d797202c
IS
4689#pragma GCC diagnostic push
4690#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
cd0b94e6 4691 // NB: (v)snprintf vs sprintf.
a5a6b586 4692
b911ca42 4693 _GLIBCXX_NODISCARD
a4ecd144
PC
4694 inline string
4695 to_string(float __val)
4696 {
45ab93d9 4697 const int __n =
a4ecd144
PC
4698 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
4699 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4700 "%f", __val);
4701 }
4702
b911ca42 4703 _GLIBCXX_NODISCARD
a4ecd144
PC
4704 inline string
4705 to_string(double __val)
4706 {
45ab93d9 4707 const int __n =
a4ecd144
PC
4708 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
4709 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4710 "%f", __val);
4711 }
4712
b911ca42 4713 _GLIBCXX_NODISCARD
a5a6b586
PC
4714 inline string
4715 to_string(long double __val)
4716 {
45ab93d9 4717 const int __n =
a5a6b586
PC
4718 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
4719 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4720 "%Lf", __val);
4721 }
d797202c 4722#pragma GCC diagnostic pop
23c64853 4723#endif // _GLIBCXX_USE_C99_STDIO
7364f286 4724
356510ac 4725#if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
45ab93d9 4726 inline int
a5a6b586
PC
4727 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
4728 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
4729 __idx, __base); }
4730
45ab93d9 4731 inline long
a5a6b586
PC
4732 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
4733 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
4734 __idx, __base); }
4735
4736 inline unsigned long
4737 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
4738 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
4739 __idx, __base); }
4740
4741 inline long long
4742 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
4743 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
4744 __idx, __base); }
4745
4746 inline unsigned long long
4747 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
4748 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
4749 __idx, __base); }
4750
4751 // NB: wcstof vs wcstod.
4752 inline float
4753 stof(const wstring& __str, size_t* __idx = 0)
4754 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
4755
4756 inline double
4757 stod(const wstring& __str, size_t* __idx = 0)
4758 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
4759
4760 inline long double
4761 stold(const wstring& __str, size_t* __idx = 0)
4762 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
51ec07b1
JW
4763#endif
4764
4765#ifdef _GLIBCXX_USE_WCHAR_T
4766#pragma GCC diagnostic push
4767#pragma GCC diagnostic ignored "-Wc++17-extensions"
4768 _GLIBCXX20_CONSTEXPR
4769 inline void
4770 __to_wstring_numeric(const char* __s, int __len, wchar_t* __wout)
4771 {
4772 // This condition is true if exec-charset and wide-exec-charset share the
4773 // same values for the ASCII subset or the EBCDIC invariant character set.
4774 if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-'
4775 && wchar_t('.') == L'.' && wchar_t('e') == L'e')
4776 {
4777 for (int __i = 0; __i < __len; ++__i)
4778 __wout[__i] = (wchar_t) __s[__i];
4779 }
4780 else
4781 {
4782 wchar_t __wc[256];
4783 for (int __i = '0'; __i <= '9'; ++__i)
4784 __wc[__i] = L'0' + __i;
4785 __wc['.'] = L'.';
4786 __wc['+'] = L'+';
4787 __wc['-'] = L'-';
4788 __wc['a'] = L'a';
4789 __wc['b'] = L'b';
4790 __wc['c'] = L'c';
4791 __wc['d'] = L'd';
4792 __wc['e'] = L'e';
4793 __wc['f'] = L'f';
e1937cf3 4794 __wc['i'] = L'i'; // for "inf"
51ec07b1
JW
4795 __wc['n'] = L'n'; // for "nan" and "inf"
4796 __wc['p'] = L'p'; // for hexfloats "0x1p1"
4797 __wc['x'] = L'x';
4798 __wc['A'] = L'A';
4799 __wc['B'] = L'B';
4800 __wc['C'] = L'C';
4801 __wc['D'] = L'D';
4802 __wc['E'] = L'E';
4803 __wc['F'] = L'F';
e1937cf3 4804 __wc['I'] = L'I';
51ec07b1
JW
4805 __wc['N'] = L'N';
4806 __wc['P'] = L'P';
4807 __wc['X'] = L'X';
4808
4809 for (int __i = 0; __i < __len; ++__i)
4810 __wout[__i] = __wc[(int)__s[__i]];
4811 }
4812 }
4813
7ffa63df 4814#if __glibcxx_constexpr_string >= 201907L
51ec07b1
JW
4815 constexpr
4816#endif
4817 inline wstring
affdef57 4818#ifdef __glibcxx_string_view // >= C++17
51ec07b1
JW
4819 __to_wstring_numeric(string_view __s)
4820#else
4821 __to_wstring_numeric(const string& __s)
4822#endif
4823 {
4824 if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-'
4825 && wchar_t('.') == L'.' && wchar_t('e') == L'e')
4826 return wstring(__s.data(), __s.data() + __s.size());
4827 else
4828 {
4829 wstring __ws;
4830 auto __f = __s.data();
4831 __ws.__resize_and_overwrite(__s.size(),
4832 [__f] (wchar_t* __to, int __n) {
4833 std::__to_wstring_numeric(__f, __n, __to);
4834 return __n;
4835 });
4836 return __ws;
4837 }
4838 }
4839#pragma GCC diagnostic pop
a5a6b586 4840
b911ca42 4841 _GLIBCXX_NODISCARD
a4ecd144
PC
4842 inline wstring
4843 to_wstring(int __val)
51ec07b1 4844 { return std::__to_wstring_numeric(std::to_string(__val)); }
a4ecd144 4845
b911ca42 4846 _GLIBCXX_NODISCARD
a4ecd144
PC
4847 inline wstring
4848 to_wstring(unsigned __val)
51ec07b1 4849 { return std::__to_wstring_numeric(std::to_string(__val)); }
a4ecd144 4850
b911ca42 4851 _GLIBCXX_NODISCARD
a4ecd144
PC
4852 inline wstring
4853 to_wstring(long __val)
51ec07b1 4854 { return std::__to_wstring_numeric(std::to_string(__val)); }
a4ecd144 4855
b911ca42 4856 _GLIBCXX_NODISCARD
a4ecd144
PC
4857 inline wstring
4858 to_wstring(unsigned long __val)
51ec07b1 4859 { return std::__to_wstring_numeric(std::to_string(__val)); }
a4ecd144 4860
b911ca42 4861 _GLIBCXX_NODISCARD
a5a6b586
PC
4862 inline wstring
4863 to_wstring(long long __val)
51ec07b1 4864 { return std::__to_wstring_numeric(std::to_string(__val)); }
a5a6b586 4865
b911ca42 4866 _GLIBCXX_NODISCARD
a5a6b586
PC
4867 inline wstring
4868 to_wstring(unsigned long long __val)
51ec07b1 4869 { return std::__to_wstring_numeric(std::to_string(__val)); }
a5a6b586 4870
7ffa63df 4871#if __glibcxx_to_string || _GLIBCXX_USE_C99_STDIO
b911ca42 4872 _GLIBCXX_NODISCARD
a4ecd144
PC
4873 inline wstring
4874 to_wstring(float __val)
51ec07b1 4875 { return std::__to_wstring_numeric(std::to_string(__val)); }
a4ecd144 4876
b911ca42 4877 _GLIBCXX_NODISCARD
a4ecd144
PC
4878 inline wstring
4879 to_wstring(double __val)
51ec07b1 4880 { return std::__to_wstring_numeric(std::to_string(__val)); }
a4ecd144 4881
b911ca42 4882 _GLIBCXX_NODISCARD
a5a6b586
PC
4883 inline wstring
4884 to_wstring(long double __val)
51ec07b1
JW
4885 { return std::__to_wstring_numeric(std::to_string(__val)); }
4886#endif
4887#endif // _GLIBCXX_USE_WCHAR_T
7364f286 4888
34a2b755 4889_GLIBCXX_END_NAMESPACE_CXX11
12ffa228
BK
4890_GLIBCXX_END_NAMESPACE_VERSION
4891} // namespace
725dc051 4892
23c64853 4893#endif /* C++11 */
15d81a3c 4894
734f5023 4895#if __cplusplus >= 201103L
15d81a3c
PC
4896
4897#include <bits/functional_hash.h>
4898
12ffa228
BK
4899namespace std _GLIBCXX_VISIBILITY(default)
4900{
4901_GLIBCXX_BEGIN_NAMESPACE_VERSION
15d81a3c 4902
b370ed0b
JW
4903 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4904 // 3705. Hashability shouldn't depend on basic_string's allocator
15d81a3c 4905
b370ed0b
JW
4906 template<typename _CharT, typename _Alloc,
4907 typename _StrT = basic_string<_CharT, char_traits<_CharT>, _Alloc>>
4908 struct __str_hash_base
4909 : public __hash_base<size_t, _StrT>
15d81a3c 4910 {
b911ca42 4911 [[__nodiscard__]]
15d81a3c 4912 size_t
b370ed0b
JW
4913 operator()(const _StrT& __s) const noexcept
4914 { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(_CharT)); }
15d81a3c
PC
4915 };
4916
b370ed0b
JW
4917#ifndef _GLIBCXX_COMPATIBILITY_CXX0X
4918 /// std::hash specialization for string.
4919 template<typename _Alloc>
4920 struct hash<basic_string<char, char_traits<char>, _Alloc>>
4921 : public __str_hash_base<char, _Alloc>
4df047dd
FD
4922 { };
4923
15d81a3c 4924 /// std::hash specialization for wstring.
b370ed0b
JW
4925 template<typename _Alloc>
4926 struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Alloc>>
4927 : public __str_hash_base<wchar_t, _Alloc>
4928 { };
4df047dd 4929
b370ed0b
JW
4930 template<typename _Alloc>
4931 struct __is_fast_hash<hash<basic_string<wchar_t, char_traits<wchar_t>,
4932 _Alloc>>>
4933 : std::false_type
4df047dd 4934 { };
15d81a3c
PC
4935#endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
4936
c124af93
TH
4937#ifdef _GLIBCXX_USE_CHAR8_T
4938 /// std::hash specialization for u8string.
b370ed0b
JW
4939 template<typename _Alloc>
4940 struct hash<basic_string<char8_t, char_traits<char8_t>, _Alloc>>
4941 : public __str_hash_base<char8_t, _Alloc>
c124af93
TH
4942 { };
4943#endif
4944
15d81a3c 4945 /// std::hash specialization for u16string.
b370ed0b
JW
4946 template<typename _Alloc>
4947 struct hash<basic_string<char16_t, char_traits<char16_t>, _Alloc>>
4948 : public __str_hash_base<char16_t, _Alloc>
4df047dd
FD
4949 { };
4950
15d81a3c 4951 /// std::hash specialization for u32string.
b370ed0b
JW
4952 template<typename _Alloc>
4953 struct hash<basic_string<char32_t, char_traits<char32_t>, _Alloc>>
4954 : public __str_hash_base<char32_t, _Alloc>
4955 { };
4df047dd 4956
b370ed0b
JW
4957#if ! _GLIBCXX_INLINE_VERSION
4958 // PR libstdc++/105907 - __is_fast_hash affects unordered container ABI.
4959 template<> struct __is_fast_hash<hash<string>> : std::false_type { };
4960 template<> struct __is_fast_hash<hash<wstring>> : std::false_type { };
4961 template<> struct __is_fast_hash<hash<u16string>> : std::false_type { };
4962 template<> struct __is_fast_hash<hash<u32string>> : std::false_type { };
4963#ifdef _GLIBCXX_USE_CHAR8_T
4964 template<> struct __is_fast_hash<hash<u8string>> : std::false_type { };
4965#endif
4966#else
4967 // For versioned namespace, assume every std::hash<basic_string<>> is slow.
4968 template<typename _CharT, typename _Traits, typename _Alloc>
4969 struct __is_fast_hash<hash<basic_string<_CharT, _Traits, _Alloc>>>
4970 : std::false_type
4df047dd 4971 { };
b370ed0b 4972#endif
15d81a3c 4973
7ffa63df 4974#ifdef __glibcxx_string_udls // C++ >= 14
0372af98
ESR
4975 inline namespace literals
4976 {
4977 inline namespace string_literals
4978 {
f03858e5
JW
4979#pragma GCC diagnostic push
4980#pragma GCC diagnostic ignored "-Wliteral-suffix"
b96e2ff9 4981
7ffa63df 4982#if __glibcxx_constexpr_string >= 201907L
b96e2ff9
ML
4983# define _GLIBCXX_STRING_CONSTEXPR constexpr
4984#else
4985# define _GLIBCXX_STRING_CONSTEXPR
4986#endif
4987
4988 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
88c4d6b7 4989 inline basic_string<char>
e9a64492 4990 operator""s(const char* __str, size_t __len)
88c4d6b7 4991 { return basic_string<char>{__str, __len}; }
1c9f675f 4992
b96e2ff9 4993 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
88c4d6b7 4994 inline basic_string<wchar_t>
e9a64492 4995 operator""s(const wchar_t* __str, size_t __len)
88c4d6b7 4996 { return basic_string<wchar_t>{__str, __len}; }
1c9f675f 4997
c124af93 4998#ifdef _GLIBCXX_USE_CHAR8_T
b96e2ff9 4999 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
c124af93
TH
5000 inline basic_string<char8_t>
5001 operator""s(const char8_t* __str, size_t __len)
5002 { return basic_string<char8_t>{__str, __len}; }
5003#endif
5004
b96e2ff9 5005 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
88c4d6b7 5006 inline basic_string<char16_t>
e9a64492 5007 operator""s(const char16_t* __str, size_t __len)
88c4d6b7 5008 { return basic_string<char16_t>{__str, __len}; }
1c9f675f 5009
b96e2ff9 5010 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
88c4d6b7 5011 inline basic_string<char32_t>
e9a64492 5012 operator""s(const char32_t* __str, size_t __len)
88c4d6b7 5013 { return basic_string<char32_t>{__str, __len}; }
1c9f675f 5014
b96e2ff9 5015#undef _GLIBCXX_STRING_CONSTEXPR
f03858e5 5016#pragma GCC diagnostic pop
88c4d6b7
ESR
5017 } // inline namespace string_literals
5018 } // inline namespace literals
7ffa63df 5019#endif // __glibcxx_string_udls
88c4d6b7 5020
affdef57 5021#ifdef __glibcxx_variant // >= C++17
10f26de9
JW
5022 namespace __detail::__variant
5023 {
5024 template<typename> struct _Never_valueless_alt; // see <variant>
5025
5026 // Provide the strong exception-safety guarantee when emplacing a
47a468bd 5027 // basic_string into a variant, but only if moving the string cannot throw.
10f26de9
JW
5028 template<typename _Tp, typename _Traits, typename _Alloc>
5029 struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
47a468bd
JW
5030 : __and_<
5031 is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
5032 is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
5033 >::type
10f26de9
JW
5034 { };
5035 } // namespace __detail::__variant
5036#endif // C++17
1c9f675f 5037
4a15d842 5038_GLIBCXX_END_NAMESPACE_VERSION
1c9f675f
ESR
5039} // namespace std
5040
734f5023 5041#endif // C++11
a5a6b586 5042
3d7c150e 5043#endif /* _BASIC_STRING_H */