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