libstdc++
cow_string.h
Go to the documentation of this file.
1 // Definition of gcc4-compatible Copy-on-Write basic_string -*- C++ -*-
2 
3 // Copyright (C) 1997-2025 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/cow_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  * Defines the reference-counted COW string implementation.
30  */
31 
32 #ifndef _COW_STRING_H
33 #define _COW_STRING_H 1
34 
35 #if ! _GLIBCXX_USE_CXX11_ABI
36 
37 #include <ext/atomicity.h> // _Atomic_word, __is_single_threaded
38 
39 namespace std _GLIBCXX_VISIBILITY(default)
40 {
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 
43  /**
44  * @class basic_string basic_string.h <string>
45  * @brief Managing sequences of characters and character-like objects.
46  *
47  * @ingroup strings
48  * @ingroup sequences
49  * @headerfile string
50  * @since C++98
51  *
52  * @tparam _CharT Type of character
53  * @tparam _Traits Traits for character type, defaults to
54  * char_traits<_CharT>.
55  * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
56  *
57  * Meets the requirements of a <a href="tables.html#65">container</a>, a
58  * <a href="tables.html#66">reversible container</a>, and a
59  * <a href="tables.html#67">sequence</a>. Of the
60  * <a href="tables.html#68">optional sequence requirements</a>, only
61  * @c push_back, @c at, and @c %array access are supported.
62  *
63  * @doctodo
64  *
65  *
66  * Documentation? What's that?
67  * Nathan Myers <ncm@cantrip.org>.
68  *
69  * A string looks like this:
70  *
71  * @code
72  * [_Rep]
73  * _M_length
74  * [basic_string<char_type>] _M_capacity
75  * _M_dataplus _M_refcount
76  * _M_p ----------------> unnamed array of char_type
77  * @endcode
78  *
79  * Where the _M_p points to the first character in the string, and
80  * you cast it to a pointer-to-_Rep and subtract 1 to get a
81  * pointer to the header.
82  *
83  * This approach has the enormous advantage that a string object
84  * requires only one allocation. All the ugliness is confined
85  * within a single %pair of inline functions, which each compile to
86  * a single @a add instruction: _Rep::_M_data(), and
87  * string::_M_rep(); and the allocation function which gets a
88  * block of raw bytes and with room enough and constructs a _Rep
89  * object at the front.
90  *
91  * The reason you want _M_data pointing to the character %array and
92  * not the _Rep is so that the debugger can see the string
93  * contents. (Probably we should add a non-inline member to get
94  * the _Rep for the debugger to use, so users can check the actual
95  * string length.)
96  *
97  * Note that the _Rep object is a POD so that you can have a
98  * static <em>empty string</em> _Rep object already @a constructed before
99  * static constructors have run. The reference-count encoding is
100  * chosen so that a 0 indicates one reference, so you never try to
101  * destroy the empty-string _Rep object.
102  *
103  * All but the last paragraph is considered pretty conventional
104  * for a Copy-On-Write C++ string implementation.
105  */
106  // 21.3 Template class basic_string
107  template<typename _CharT, typename _Traits, typename _Alloc>
109  {
111  rebind<_CharT>::other _CharT_alloc_type;
113 
114  // Types:
115  public:
116  typedef _Traits traits_type;
117  typedef typename _Traits::char_type value_type;
118  typedef _Alloc allocator_type;
119  typedef typename _CharT_alloc_traits::size_type size_type;
120  typedef typename _CharT_alloc_traits::difference_type difference_type;
121 #if __cplusplus < 201103L
122  typedef typename _CharT_alloc_type::reference reference;
123  typedef typename _CharT_alloc_type::const_reference const_reference;
124 #else
125  typedef value_type& reference;
126  typedef const value_type& const_reference;
127 #endif
128  typedef typename _CharT_alloc_traits::pointer pointer;
129  typedef typename _CharT_alloc_traits::const_pointer const_pointer;
130  typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
131  typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
132  const_iterator;
135 
136  protected:
137  // type used for positions in insert, erase etc.
138  typedef iterator __const_iterator;
139 
140  private:
141  // _Rep: string representation
142  // Invariants:
143  // 1. String really contains _M_length + 1 characters: due to 21.3.4
144  // must be kept null-terminated.
145  // 2. _M_capacity >= _M_length
146  // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
147  // 3. _M_refcount has three states:
148  // -1: leaked, one reference, no ref-copies allowed, non-const.
149  // 0: one reference, non-const.
150  // n>0: n + 1 references, operations require a lock, const.
151  // 4. All fields==0 is an empty string, given the extra storage
152  // beyond-the-end for a null terminator; thus, the shared
153  // empty string representation needs no constructor.
154 
155  struct _Rep_base
156  {
157  size_type _M_length;
158  size_type _M_capacity;
159  _Atomic_word _M_refcount;
160  };
161 
162  struct _Rep : _Rep_base
163  {
164  // Types:
166  rebind<char>::other _Raw_bytes_alloc;
167 
168  // (Public) Data members:
169 
170  // The maximum number of individual char_type elements of an
171  // individual string is determined by _S_max_size. This is the
172  // value that will be returned by max_size(). (Whereas npos
173  // is the maximum number of bytes the allocator can allocate.)
174  // If one was to divvy up the theoretical largest size string,
175  // with a terminating character and m _CharT elements, it'd
176  // look like this:
177  // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
178  // Solving for m:
179  // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
180  // In addition, this implementation quarters this amount.
181  static const size_type _S_max_size;
182  static const _CharT _S_terminal;
183 
184  // The following storage is init'd to 0 by the linker, resulting
185  // (carefully) in an empty string with one reference.
186  static size_type _S_empty_rep_storage[];
187 
188  static _Rep&
189  _S_empty_rep() _GLIBCXX_NOEXCEPT
190  {
191  // NB: Mild hack to avoid strict-aliasing warnings. Note that
192  // _S_empty_rep_storage is never modified and the punning should
193  // be reasonably safe in this case.
194  void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
195  return *reinterpret_cast<_Rep*>(__p);
196  }
197 
198  bool
199  _M_is_leaked() const _GLIBCXX_NOEXCEPT
200  {
201 #if defined(__GTHREADS)
202  // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
203  // so we need to use an atomic load. However, _M_is_leaked
204  // predicate does not change concurrently (i.e. the string is either
205  // leaked or not), so a relaxed load is enough.
206  return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
207 #else
208  return this->_M_refcount < 0;
209 #endif
210  }
211 
212  bool
213  _M_is_shared() const _GLIBCXX_NOEXCEPT
214  {
215 #if defined(__GTHREADS)
216  // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
217  // so we need to use an atomic load. Another thread can drop last
218  // but one reference concurrently with this check, so we need this
219  // load to be acquire to synchronize with release fetch_and_add in
220  // _M_dispose.
221  if (!__gnu_cxx::__is_single_threaded())
222  return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
223 #endif
224  return this->_M_refcount > 0;
225  }
226 
227  void
228  _M_set_leaked() _GLIBCXX_NOEXCEPT
229  { this->_M_refcount = -1; }
230 
231  void
232  _M_set_sharable() _GLIBCXX_NOEXCEPT
233  { this->_M_refcount = 0; }
234 
235  void
236  _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
237  {
238 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
239  if (__builtin_expect(this != &_S_empty_rep(), false))
240 #endif
241  {
242  this->_M_set_sharable(); // One reference.
243  this->_M_length = __n;
244  traits_type::assign(this->_M_refdata()[__n], _S_terminal);
245  // grrr. (per 21.3.4)
246  // You cannot leave those LWG people alone for a second.
247  }
248  }
249 
250  _CharT*
251  _M_refdata() throw()
252  { return reinterpret_cast<_CharT*>(this + 1); }
253 
254  _CharT*
255  _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
256  {
257  return (!_M_is_leaked() && __alloc1 == __alloc2)
258  ? _M_refcopy() : _M_clone(__alloc1);
259  }
260 
261  // Create & Destroy
262  static _Rep*
263  _S_create(size_type, size_type, const _Alloc&);
264 
265  void
266  _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
267  {
268 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
269  if (__builtin_expect(this != &_S_empty_rep(), false))
270 #endif
271  {
272  // Be race-detector-friendly. For more info see bits/c++config.
273  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
274  // Decrement of _M_refcount is acq_rel, because:
275  // - all but last decrements need to release to synchronize with
276  // the last decrement that will delete the object.
277  // - the last decrement needs to acquire to synchronize with
278  // all the previous decrements.
279  // - last but one decrement needs to release to synchronize with
280  // the acquire load in _M_is_shared that will conclude that
281  // the object is not shared anymore.
282  if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
283  -1) <= 0)
284  {
285  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
286  _M_destroy(__a);
287  }
288  }
289  } // XXX MT
290 
291  void
292  _M_destroy(const _Alloc&) throw();
293 
294  _CharT*
295  _M_refcopy() throw()
296  {
297 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
298  if (__builtin_expect(this != &_S_empty_rep(), false))
299 #endif
300  __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
301  return _M_refdata();
302  } // XXX MT
303 
304  _CharT*
305  _M_clone(const _Alloc&, size_type __res = 0);
306  };
307 
308  // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
309  struct _Alloc_hider : _Alloc
310  {
311  _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
312  : _Alloc(__a), _M_p(__dat) { }
313 
314  _CharT* _M_p; // The actual data.
315  };
316 
317  public:
318  // Data Members (public):
319  // NB: This is an unsigned type, and thus represents the maximum
320  // size that the allocator can hold.
321  /// Value returned by various member functions when they fail.
322  static const size_type npos = static_cast<size_type>(-1);
323 
324  private:
325  // Data Members (private):
326  mutable _Alloc_hider _M_dataplus;
327 
328  _CharT*
329  _M_data() const _GLIBCXX_NOEXCEPT
330  { return _M_dataplus._M_p; }
331 
332  _CharT*
333  _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
334  { return (_M_dataplus._M_p = __p); }
335 
336  _Rep*
337  _M_rep() const _GLIBCXX_NOEXCEPT
338  { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
339 
340  // For the internal use we have functions similar to `begin'/`end'
341  // but they do not call _M_leak.
342  iterator
343  _M_ibegin() const _GLIBCXX_NOEXCEPT
344  { return iterator(_M_data()); }
345 
346  iterator
347  _M_iend() const _GLIBCXX_NOEXCEPT
348  { return iterator(_M_data() + this->size()); }
349 
350  void
351  _M_leak() // for use in begin() & non-const op[]
352  {
353  if (!_M_rep()->_M_is_leaked())
354  _M_leak_hard();
355  }
356 
357  size_type
358  _M_check(size_type __pos, const char* __s) const
359  {
360  if (__pos > this->size())
361  __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
362  "this->size() (which is %zu)"),
363  __s, __pos, this->size());
364  return __pos;
365  }
366 
367  void
368  _M_check_length(size_type __n1, size_type __n2, const char* __s) const
369  {
370  if (this->max_size() - (this->size() - __n1) < __n2)
371  __throw_length_error(__N(__s));
372  }
373 
374  // NB: _M_limit doesn't check for a bad __pos value.
375  size_type
376  _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
377  {
378  const bool __testoff = __off < this->size() - __pos;
379  return __testoff ? __off : this->size() - __pos;
380  }
381 
382  // True if _Rep and source do not overlap.
383  bool
384  _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
385  {
386  return (less<const _CharT*>()(__s, _M_data())
387  || less<const _CharT*>()(_M_data() + this->size(), __s));
388  }
389 
390  // When __n = 1 way faster than the general multichar
391  // traits_type::copy/move/assign.
392  static void
393  _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
394  {
395  if (__n == 1)
396  traits_type::assign(*__d, *__s);
397  else
398  traits_type::copy(__d, __s, __n);
399  }
400 
401  static void
402  _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
403  {
404  if (__n == 1)
405  traits_type::assign(*__d, *__s);
406  else
407  traits_type::move(__d, __s, __n);
408  }
409 
410  static void
411  _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
412  {
413  if (__n == 1)
414  traits_type::assign(*__d, __c);
415  else
416  traits_type::assign(__d, __n, __c);
417  }
418 
419  // _S_copy_chars is a separate template to permit specialization
420  // to optimize for the common case of pointers as iterators.
421  template<class _Iterator>
422  static void
423  _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
424  {
425  for (; __k1 != __k2; ++__k1, (void)++__p)
426  traits_type::assign(*__p, static_cast<_CharT>(*__k1));
427  }
428 
429  static void
430  _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
431  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
432 
433  static void
434  _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
435  _GLIBCXX_NOEXCEPT
436  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
437 
438  static void
439  _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
440  { _M_copy(__p, __k1, __k2 - __k1); }
441 
442  static void
443  _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
444  _GLIBCXX_NOEXCEPT
445  { _M_copy(__p, __k1, __k2 - __k1); }
446 
447  static int
448  _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
449  {
450  const difference_type __d = difference_type(__n1 - __n2);
451 
452  if (__d > __gnu_cxx::__numeric_traits<int>::__max)
453  return __gnu_cxx::__numeric_traits<int>::__max;
454  else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
455  return __gnu_cxx::__numeric_traits<int>::__min;
456  else
457  return int(__d);
458  }
459 
460  void
461  _M_mutate(size_type __pos, size_type __len1, size_type __len2);
462 
463  void
464  _M_leak_hard();
465 
466  static _Rep&
467  _S_empty_rep() _GLIBCXX_NOEXCEPT
468  { return _Rep::_S_empty_rep(); }
469 
470 #ifdef __glibcxx_string_view // >= C++17
471  // A helper type for avoiding boiler-plate.
472  typedef basic_string_view<_CharT, _Traits> __sv_type;
473 
474  template<typename _Tp, typename _Res>
475  using _If_sv = enable_if_t<
476  __and_<is_convertible<const _Tp&, __sv_type>,
477  __not_<is_convertible<const _Tp*, const basic_string*>>,
478  __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
479  _Res>;
480 
481  // Allows an implicit conversion to __sv_type.
482  static __sv_type
483  _S_to_string_view(__sv_type __svt) noexcept
484  { return __svt; }
485 
486  // Wraps a string_view by explicit conversion and thus
487  // allows to add an internal constructor that does not
488  // participate in overload resolution when a string_view
489  // is provided.
490  struct __sv_wrapper
491  {
492  explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
493  __sv_type _M_sv;
494  };
495 
496  /**
497  * @brief Only internally used: Construct string from a string view
498  * wrapper.
499  * @param __svw string view wrapper.
500  * @param __a Allocator to use.
501  */
502  explicit
503  basic_string(__sv_wrapper __svw, const _Alloc& __a)
504  : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
505 #endif
506 
507  public:
508  // Construct/copy/destroy:
509  // NB: We overload ctors in some cases instead of using default
510  // arguments, per 17.4.4.4 para. 2 item 2.
511 
512  /**
513  * @brief Default constructor creates an empty string.
514  */
516 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
517  _GLIBCXX_NOEXCEPT
518 #endif
519 #if __cpp_concepts && __glibcxx_type_trait_variable_templates
520  requires is_default_constructible_v<_Alloc>
521 #endif
522 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
523  : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
524 #else
525  : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
526 #endif
527  { }
528 
529  /**
530  * @brief Construct an empty string using allocator @a a.
531  */
532  explicit
533  basic_string(const _Alloc& __a)
534  : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
535  { }
536 
537  // NB: per LWG issue 42, semantics different from IS:
538  /**
539  * @brief Construct string with copy of value of @a str.
540  * @param __str Source string.
541  */
543  : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
544  __str.get_allocator()),
545  __str.get_allocator())
546  { }
547 
548  // _GLIBCXX_RESOLVE_LIB_DEFECTS
549  // 2583. no way to supply an allocator for basic_string(str, pos)
550  /**
551  * @brief Construct string as copy of a substring.
552  * @param __str Source string.
553  * @param __pos Index of first character to copy from.
554  * @param __a Allocator to use.
555  */
556  basic_string(const basic_string& __str, size_type __pos,
557  const _Alloc& __a = _Alloc());
558 
559  /**
560  * @brief Construct string as copy of a substring.
561  * @param __str Source string.
562  * @param __pos Index of first character to copy from.
563  * @param __n Number of characters to copy.
564  */
565  basic_string(const basic_string& __str, size_type __pos,
566  size_type __n);
567  /**
568  * @brief Construct string as copy of a substring.
569  * @param __str Source string.
570  * @param __pos Index of first character to copy from.
571  * @param __n Number of characters to copy.
572  * @param __a Allocator to use.
573  */
574  basic_string(const basic_string& __str, size_type __pos,
575  size_type __n, const _Alloc& __a);
576 
577  /**
578  * @brief Construct string initialized by a character %array.
579  * @param __s Source character %array.
580  * @param __n Number of characters to copy.
581  * @param __a Allocator to use (default is default allocator).
582  *
583  * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
584  * has no special meaning.
585  */
586  basic_string(const _CharT* __s, size_type __n,
587  const _Alloc& __a = _Alloc())
588  : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
589  { }
590 
591  /**
592  * @brief Construct string as copy of a C string.
593  * @param __s Source C string.
594  * @param __a Allocator to use (default is default allocator).
595  */
596 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
597  // _GLIBCXX_RESOLVE_LIB_DEFECTS
598  // 3076. basic_string CTAD ambiguity
599  template<typename = _RequireAllocator<_Alloc>>
600 #endif
601  basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
602  : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
603  __s + npos, __a), __a)
604  { }
605 
606  /**
607  * @brief Construct string as multiple characters.
608  * @param __n Number of characters.
609  * @param __c Character to use.
610  * @param __a Allocator to use (default is default allocator).
611  */
612  basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
613  : _M_dataplus(_S_construct(__n, __c, __a), __a)
614  { }
615 
616 #if __cplusplus >= 201103L
617  /**
618  * @brief Move construct string.
619  * @param __str Source string.
620  *
621  * The newly-created string contains the exact contents of @a __str.
622  * @a __str is a valid, but unspecified string.
623  */
624  basic_string(basic_string&& __str) noexcept
625  : _M_dataplus(std::move(__str._M_dataplus))
626  {
627 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
628  // Make __str use the shared empty string rep.
629  __str._M_data(_S_empty_rep()._M_refdata());
630 #else
631  // Rather than allocate an empty string for the rvalue string,
632  // just share ownership with it by incrementing the reference count.
633  // If the rvalue string was the unique owner then there are exactly
634  // two owners now.
635  if (_M_rep()->_M_is_shared())
636  __gnu_cxx::__atomic_add_dispatch(&_M_rep()->_M_refcount, 1);
637  else
638  _M_rep()->_M_refcount = 1;
639 #endif
640  }
641 
642 #if __glibcxx_containers_ranges // C++ >= 23
643  /**
644  * @brief Construct a string from a range.
645  * @since C++23
646  */
647  template<__detail::__container_compatible_range<_CharT> _Rg>
648  basic_string(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
649  : basic_string(__a)
650  {
651  if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
652  {
653  const auto __n = static_cast<size_type>(ranges::distance(__rg));
654  if (__n == 0)
655  return;
656 
657  reserve(__n);
658  pointer __p = _M_data();
659  if constexpr (requires {
660  requires ranges::contiguous_range<_Rg>;
661  { ranges::data(std::forward<_Rg>(__rg)) }
662  -> convertible_to<const _CharT*>;
663  })
664  _M_copy(__p, ranges::data(std::forward<_Rg>(__rg)), __n);
665  else
666  {
667  auto __first = ranges::begin(__rg);
668  const auto __last = ranges::end(__rg);
669  for (; __first != __last; ++__first)
670  traits_type::assign(*__p++, static_cast<_CharT>(*__first));
671  }
672  _M_rep()->_M_set_length_and_sharable(__n);
673  }
674  else
675  {
676  auto __first = ranges::begin(__rg);
677  const auto __last = ranges::end(__rg);
678  for (; __first != __last; ++__first)
679  push_back(*__first);
680  }
681  }
682 #endif
683 
684  /**
685  * @brief Construct string from an initializer %list.
686  * @param __l std::initializer_list of characters.
687  * @param __a Allocator to use (default is default allocator).
688  */
689  basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
690  : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a)
691  { }
692 
693  basic_string(const basic_string& __str, const _Alloc& __a)
694  : _M_dataplus(__str._M_rep()->_M_grab(__a, __str.get_allocator()), __a)
695  { }
696 
697  basic_string(basic_string&& __str, const _Alloc& __a)
698  : _M_dataplus(__str._M_data(), __a)
699  {
700  if (__a == __str.get_allocator())
701  {
702 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
703  __str._M_data(_S_empty_rep()._M_refdata());
704 #else
705  __str._M_data(_S_construct(size_type(), _CharT(), __a));
706 #endif
707  }
708  else
709  _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
710  }
711 #endif // C++11
712 
713 #if __cplusplus >= 202100L
714  basic_string(nullptr_t) = delete;
715  basic_string& operator=(nullptr_t) = delete;
716 #endif // C++23
717 
718  /**
719  * @brief Construct string as copy of a range.
720  * @param __beg Start of range.
721  * @param __end End of range.
722  * @param __a Allocator to use (default is default allocator).
723  */
724  template<class _InputIterator>
725  basic_string(_InputIterator __beg, _InputIterator __end,
726  const _Alloc& __a = _Alloc())
727  : _M_dataplus(_S_construct(__beg, __end, __a), __a)
728  { }
729 
730 #ifdef __glibcxx_string_view // >= C++17
731  /**
732  * @brief Construct string from a substring of a string_view.
733  * @param __t Source object convertible to string view.
734  * @param __pos The index of the first character to copy from __t.
735  * @param __n The number of characters to copy from __t.
736  * @param __a Allocator to use.
737  */
738  template<typename _Tp,
740  basic_string(const _Tp& __t, size_type __pos, size_type __n,
741  const _Alloc& __a = _Alloc())
742  : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
743 
744  /**
745  * @brief Construct string from a string_view.
746  * @param __t Source object convertible to string view.
747  * @param __a Allocator to use (default is default allocator).
748  */
749  template<typename _Tp, typename = _If_sv<_Tp, void>>
750  explicit
751  basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
752  : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
753 #endif // C++17
754 
755  /**
756  * @brief Destroy the string instance.
757  */
758  ~basic_string() _GLIBCXX_NOEXCEPT
759  { _M_rep()->_M_dispose(this->get_allocator()); }
760 
761  /**
762  * @brief Assign the value of @a str to this string.
763  * @param __str Source string.
764  */
765  basic_string&
766  operator=(const basic_string& __str)
767  { return this->assign(__str); }
768 
769  /**
770  * @brief Copy contents of @a s into this string.
771  * @param __s Source null-terminated string.
772  */
773  basic_string&
774  operator=(const _CharT* __s)
775  { return this->assign(__s); }
776 
777  /**
778  * @brief Set value to string of length 1.
779  * @param __c Source character.
780  *
781  * Assigning to a character makes this string length 1 and
782  * (*this)[0] == @a c.
783  */
784  basic_string&
785  operator=(_CharT __c)
786  {
787  this->assign(1, __c);
788  return *this;
789  }
790 
791 #if __cplusplus >= 201103L
792  /**
793  * @brief Move assign the value of @a str to this string.
794  * @param __str Source string.
795  *
796  * The contents of @a str are moved into this string (without copying).
797  * @a str is a valid, but unspecified string.
798  */
799  basic_string&
802  {
803  // NB: DR 1204.
804  this->swap(__str);
805  return *this;
806  }
807 
808  /**
809  * @brief Set value to string constructed from initializer %list.
810  * @param __l std::initializer_list.
811  */
812  basic_string&
814  {
815  this->assign(__l.begin(), __l.size());
816  return *this;
817  }
818 #endif // C++11
819 
820 #ifdef __glibcxx_string_view // >= C++17
821  /**
822  * @brief Set value to string constructed from a string_view.
823  * @param __svt An object convertible to string_view.
824  */
825  template<typename _Tp>
826  _If_sv<_Tp, basic_string&>
827  operator=(const _Tp& __svt)
828  { return this->assign(__svt); }
829 
830  /**
831  * @brief Convert to a string_view.
832  * @return A string_view.
833  */
834  operator __sv_type() const noexcept
835  { return __sv_type(data(), size()); }
836 #endif // C++17
837 
838  // Iterators:
839  /**
840  * Returns a read/write iterator that points to the first character in
841  * the %string. Unshares the string.
842  */
843  iterator
844  begin() // FIXME C++11: should be noexcept.
845  {
846  _M_leak();
847  return iterator(_M_data());
848  }
849 
850  /**
851  * Returns a read-only (constant) iterator that points to the first
852  * character in the %string.
853  */
854  const_iterator
855  begin() const _GLIBCXX_NOEXCEPT
856  { return const_iterator(_M_data()); }
857 
858  /**
859  * Returns a read/write iterator that points one past the last
860  * character in the %string. Unshares the string.
861  */
862  iterator
863  end() // FIXME C++11: should be noexcept.
864  {
865  _M_leak();
866  return iterator(_M_data() + this->size());
867  }
868 
869  /**
870  * Returns a read-only (constant) iterator that points one past the
871  * last character in the %string.
872  */
873  const_iterator
874  end() const _GLIBCXX_NOEXCEPT
875  { return const_iterator(_M_data() + this->size()); }
876 
877  /**
878  * Returns a read/write reverse iterator that points to the last
879  * character in the %string. Iteration is done in reverse element
880  * order. Unshares the string.
881  */
882  reverse_iterator
883  rbegin() // FIXME C++11: should be noexcept.
884  { return reverse_iterator(this->end()); }
885 
886  /**
887  * Returns a read-only (constant) reverse iterator that points
888  * to the last character in the %string. Iteration is done in
889  * reverse element order.
890  */
891  const_reverse_iterator
892  rbegin() const _GLIBCXX_NOEXCEPT
893  { return const_reverse_iterator(this->end()); }
894 
895  /**
896  * Returns a read/write reverse iterator that points to one before the
897  * first character in the %string. Iteration is done in reverse
898  * element order. Unshares the string.
899  */
900  reverse_iterator
901  rend() // FIXME C++11: should be noexcept.
902  { return reverse_iterator(this->begin()); }
903 
904  /**
905  * Returns a read-only (constant) reverse iterator that points
906  * to one before the first character in the %string. Iteration
907  * is done in reverse element order.
908  */
909  const_reverse_iterator
910  rend() const _GLIBCXX_NOEXCEPT
911  { return const_reverse_iterator(this->begin()); }
912 
913 #if __cplusplus >= 201103L
914  /**
915  * Returns a read-only (constant) iterator that points to the first
916  * character in the %string.
917  */
918  const_iterator
919  cbegin() const noexcept
920  { return const_iterator(this->_M_data()); }
921 
922  /**
923  * Returns a read-only (constant) iterator that points one past the
924  * last character in the %string.
925  */
926  const_iterator
927  cend() const noexcept
928  { return const_iterator(this->_M_data() + this->size()); }
929 
930  /**
931  * Returns a read-only (constant) reverse iterator that points
932  * to the last character in the %string. Iteration is done in
933  * reverse element order.
934  */
935  const_reverse_iterator
936  crbegin() const noexcept
937  { return const_reverse_iterator(this->end()); }
938 
939  /**
940  * Returns a read-only (constant) reverse iterator that points
941  * to one before the first character in the %string. Iteration
942  * is done in reverse element order.
943  */
944  const_reverse_iterator
945  crend() const noexcept
946  { return const_reverse_iterator(this->begin()); }
947 #endif
948 
949  public:
950  // Capacity:
951 
952  /// Returns the number of characters in the string, not including any
953  /// null-termination.
954  size_type
955  size() const _GLIBCXX_NOEXCEPT
956  {
957 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 && __OPTIMIZE__
958  if (_S_empty_rep()._M_length != 0)
959  __builtin_unreachable();
960 #endif
961  return _M_rep()->_M_length;
962  }
963 
964  /// Returns the number of characters in the string, not including any
965  /// null-termination.
966  size_type
967  length() const _GLIBCXX_NOEXCEPT
968  { return size(); }
969 
970  /// Returns the size() of the largest possible %string.
971  size_type
972  max_size() const _GLIBCXX_NOEXCEPT
973  { return _Rep::_S_max_size; }
974 
975  /**
976  * @brief Resizes the %string to the specified number of characters.
977  * @param __n Number of characters the %string should contain.
978  * @param __c Character to fill any new elements.
979  *
980  * This function will %resize the %string to the specified
981  * number of characters. If the number is smaller than the
982  * %string's current size the %string is truncated, otherwise
983  * the %string is extended and new elements are %set to @a __c.
984  */
985  void
986  resize(size_type __n, _CharT __c);
987 
988  /**
989  * @brief Resizes the %string to the specified number of characters.
990  * @param __n Number of characters the %string should contain.
991  *
992  * This function will resize the %string to the specified length. If
993  * the new size is smaller than the %string's current size the %string
994  * is truncated, otherwise the %string is extended and new characters
995  * are default-constructed. For basic types such as char, this means
996  * setting them to 0.
997  */
998  void
999  resize(size_type __n)
1000  { this->resize(__n, _CharT()); }
1001 
1002 #if __cplusplus >= 201103L
1003 #pragma GCC diagnostic push
1004 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1005  /// A non-binding request to reduce capacity() to size().
1006  void
1007  shrink_to_fit() noexcept
1008  { reserve(); }
1009 #pragma GCC diagnostic pop
1010 #endif
1011 
1012 #ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
1013  /** Resize the string and call a function to fill it.
1014  *
1015  * @param __n The maximum size requested.
1016  * @param __op A callable object that writes characters to the string.
1017  *
1018  * This is a low-level function that is easy to misuse, be careful.
1019  *
1020  * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n`
1021  * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`,
1022  * and finally set the string length to `n2` (adding a null terminator
1023  * at the end). The function object `op` is allowed to write to the
1024  * extra capacity added by the initial reserve operation, which is not
1025  * allowed if you just call `str.reserve(n)` yourself.
1026  *
1027  * This can be used to efficiently fill a `string` buffer without the
1028  * overhead of zero-initializing characters that will be overwritten
1029  * anyway.
1030  *
1031  * The callable `op` must not access the string directly (only through
1032  * the pointer passed as its first argument), must not write more than
1033  * `n` characters to the string, must return a value no greater than `n`,
1034  * and must ensure that all characters up to the returned length are
1035  * valid after it returns (i.e. there must be no uninitialized values
1036  * left in the string after the call, because accessing them would
1037  * have undefined behaviour). If `op` exits by throwing an exception
1038  * the behaviour is undefined.
1039  *
1040  * @since C++23
1041  */
1042  template<typename _Operation>
1043  void
1044  resize_and_overwrite(size_type __n, _Operation __op);
1045 #endif // __glibcxx_string_resize_and_overwrite
1046 
1047 #if __cplusplus >= 201103L
1048  /// Non-standard version of resize_and_overwrite for C++11 and above.
1049  template<typename _Operation>
1050  void
1051  __resize_and_overwrite(size_type __n, _Operation __op);
1052 #endif
1053 
1054  /**
1055  * Returns the total number of characters that the %string can hold
1056  * before needing to allocate more memory.
1057  */
1058  size_type
1059  capacity() const _GLIBCXX_NOEXCEPT
1060  { return _M_rep()->_M_capacity; }
1061 
1062  /**
1063  * @brief Attempt to preallocate enough memory for specified number of
1064  * characters.
1065  * @param __res_arg Number of characters required.
1066  * @throw std::length_error If @a __res_arg exceeds @c max_size().
1067  *
1068  * This function attempts to reserve enough memory for the
1069  * %string to hold the specified number of characters. If the
1070  * number requested is more than max_size(), length_error is
1071  * thrown.
1072  *
1073  * The advantage of this function is that if optimal code is a
1074  * necessity and the user can determine the string length that will be
1075  * required, the user can reserve the memory in %advance, and thus
1076  * prevent a possible reallocation of memory and copying of %string
1077  * data.
1078  */
1079  void
1080  reserve(size_type __res_arg);
1081 
1082  /// Equivalent to shrink_to_fit().
1083 #if __cplusplus > 201703L
1084  [[deprecated("use shrink_to_fit() instead")]]
1085 #endif
1086  void
1088 
1089  /**
1090  * Erases the string, making it empty.
1091  */
1092 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
1093  void
1094  clear() _GLIBCXX_NOEXCEPT
1095  {
1096  if (_M_rep()->_M_is_shared())
1097  {
1098  _M_rep()->_M_dispose(this->get_allocator());
1099  _M_data(_S_empty_rep()._M_refdata());
1100  }
1101  else
1102  _M_rep()->_M_set_length_and_sharable(0);
1103  }
1104 #else
1105  // PR 56166: this should not throw.
1106  void
1107  clear()
1108  { _M_mutate(0, this->size(), 0); }
1109 #endif
1110 
1111  /**
1112  * Returns true if the %string is empty. Equivalent to
1113  * <code>*this == ""</code>.
1114  */
1115  _GLIBCXX_NODISCARD bool
1116  empty() const _GLIBCXX_NOEXCEPT
1117  { return this->size() == 0; }
1118 
1119  // Element access:
1120  /**
1121  * @brief Subscript access to the data contained in the %string.
1122  * @param __pos The index of the character to access.
1123  * @return Read-only (constant) reference to the character.
1124  *
1125  * This operator allows for easy, array-style, data access.
1126  * Note that data access with this operator is unchecked and
1127  * out_of_range lookups are not defined. (For checked lookups
1128  * see at().)
1129  */
1130  const_reference
1131  operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
1132  {
1133  __glibcxx_assert(__pos <= size());
1134  return _M_data()[__pos];
1135  }
1136 
1137  /**
1138  * @brief Subscript access to the data contained in the %string.
1139  * @param __pos The index of the character to access.
1140  * @return Read/write reference to the character.
1141  *
1142  * This operator allows for easy, array-style, data access.
1143  * Note that data access with this operator is unchecked and
1144  * out_of_range lookups are not defined. (For checked lookups
1145  * see at().) Unshares the string.
1146  */
1147  reference
1148  operator[](size_type __pos)
1149  {
1150  // Allow pos == size() both in C++98 mode, as v3 extension,
1151  // and in C++11 mode.
1152  __glibcxx_assert(__pos <= size());
1153  // In pedantic mode be strict in C++98 mode.
1154  _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1155  _M_leak();
1156  return _M_data()[__pos];
1157  }
1158 
1159  /**
1160  * @brief Provides access to the data contained in the %string.
1161  * @param __n The index of the character to access.
1162  * @return Read-only (const) reference to the character.
1163  * @throw std::out_of_range If @a n is an invalid index.
1164  *
1165  * This function provides for safer data access. The parameter is
1166  * first checked that it is in the range of the string. The function
1167  * throws out_of_range if the check fails.
1168  */
1169  const_reference
1170  at(size_type __n) const
1171  {
1172  if (__n >= this->size())
1173  __throw_out_of_range_fmt(__N("basic_string::at: __n "
1174  "(which is %zu) >= this->size() "
1175  "(which is %zu)"),
1176  __n, this->size());
1177  return _M_data()[__n];
1178  }
1179 
1180  /**
1181  * @brief Provides access to the data contained in the %string.
1182  * @param __n The index of the character to access.
1183  * @return Read/write reference to the character.
1184  * @throw std::out_of_range If @a n is an invalid index.
1185  *
1186  * This function provides for safer data access. The parameter is
1187  * first checked that it is in the range of the string. The function
1188  * throws out_of_range if the check fails. Success results in
1189  * unsharing the string.
1190  */
1191  reference
1192  at(size_type __n)
1193  {
1194  if (__n >= size())
1195  __throw_out_of_range_fmt(__N("basic_string::at: __n "
1196  "(which is %zu) >= this->size() "
1197  "(which is %zu)"),
1198  __n, this->size());
1199  _M_leak();
1200  return _M_data()[__n];
1201  }
1202 
1203 #if __cplusplus >= 201103L
1204  /**
1205  * Returns a read/write reference to the data at the first
1206  * element of the %string.
1207  */
1208  reference
1210  {
1211  __glibcxx_assert(!empty());
1212  return operator[](0);
1213  }
1214 
1215  /**
1216  * Returns a read-only (constant) reference to the data at the first
1217  * element of the %string.
1218  */
1219  const_reference
1220  front() const noexcept
1221  {
1222  __glibcxx_assert(!empty());
1223  return operator[](0);
1224  }
1225 
1226  /**
1227  * Returns a read/write reference to the data at the last
1228  * element of the %string.
1229  */
1230  reference
1232  {
1233  __glibcxx_assert(!empty());
1234  return operator[](this->size() - 1);
1235  }
1236 
1237  /**
1238  * Returns a read-only (constant) reference to the data at the
1239  * last element of the %string.
1240  */
1241  const_reference
1242  back() const noexcept
1243  {
1244  __glibcxx_assert(!empty());
1245  return operator[](this->size() - 1);
1246  }
1247 #endif
1248 
1249  // Modifiers:
1250  /**
1251  * @brief Append a string to this string.
1252  * @param __str The string to append.
1253  * @return Reference to this string.
1254  */
1255  basic_string&
1256  operator+=(const basic_string& __str)
1257  { return this->append(__str); }
1258 
1259  /**
1260  * @brief Append a C string.
1261  * @param __s The C string to append.
1262  * @return Reference to this string.
1263  */
1264  basic_string&
1265  operator+=(const _CharT* __s)
1266  { return this->append(__s); }
1267 
1268  /**
1269  * @brief Append a character.
1270  * @param __c The character to append.
1271  * @return Reference to this string.
1272  */
1273  basic_string&
1274  operator+=(_CharT __c)
1275  {
1276  this->push_back(__c);
1277  return *this;
1278  }
1279 
1280 #if __cplusplus >= 201103L
1281  /**
1282  * @brief Append an initializer_list of characters.
1283  * @param __l The initializer_list of characters to be appended.
1284  * @return Reference to this string.
1285  */
1286  basic_string&
1288  { return this->append(__l.begin(), __l.size()); }
1289 #endif // C++11
1290 
1291 #ifdef __glibcxx_string_view // >= C++17
1292  /**
1293  * @brief Append a string_view.
1294  * @param __svt The object convertible to string_view to be appended.
1295  * @return Reference to this string.
1296  */
1297  template<typename _Tp>
1298  _If_sv<_Tp, basic_string&>
1299  operator+=(const _Tp& __svt)
1300  { return this->append(__svt); }
1301 #endif // C++17
1302 
1303  /**
1304  * @brief Append a string to this string.
1305  * @param __str The string to append.
1306  * @return Reference to this string.
1307  */
1308  basic_string&
1309  append(const basic_string& __str);
1310 
1311  /**
1312  * @brief Append a substring.
1313  * @param __str The string to append.
1314  * @param __pos Index of the first character of str to append.
1315  * @param __n The number of characters to append.
1316  * @return Reference to this string.
1317  * @throw std::out_of_range if @a __pos is not a valid index.
1318  *
1319  * This function appends @a __n characters from @a __str
1320  * starting at @a __pos to this string. If @a __n is is larger
1321  * than the number of available characters in @a __str, the
1322  * remainder of @a __str is appended.
1323  */
1324  basic_string&
1325  append(const basic_string& __str, size_type __pos, size_type __n = npos);
1326 
1327  /**
1328  * @brief Append a C substring.
1329  * @param __s The C string to append.
1330  * @param __n The number of characters to append.
1331  * @return Reference to this string.
1332  */
1333  basic_string&
1334  append(const _CharT* __s, size_type __n);
1335 
1336  /**
1337  * @brief Append a C string.
1338  * @param __s The C string to append.
1339  * @return Reference to this string.
1340  */
1341  basic_string&
1342  append(const _CharT* __s)
1343  {
1344  __glibcxx_requires_string(__s);
1345  return this->append(__s, traits_type::length(__s));
1346  }
1347 
1348  /**
1349  * @brief Append multiple characters.
1350  * @param __n The number of characters to append.
1351  * @param __c The character to use.
1352  * @return Reference to this string.
1353  *
1354  * Appends __n copies of __c to this string.
1355  */
1356  basic_string&
1357  append(size_type __n, _CharT __c);
1358 
1359 #if __glibcxx_containers_ranges // C++ >= 23
1360  /**
1361  * @brief Append a range to the string.
1362  * @since C++23
1363  */
1364  template<__detail::__container_compatible_range<_CharT> _Rg>
1365  basic_string&
1366  append_range(_Rg&& __rg)
1367  {
1368  basic_string __s(from_range, std::forward<_Rg>(__rg),
1369  get_allocator());
1370  append(__s);
1371  return *this;
1372  }
1373 #endif
1374 
1375 #if __cplusplus >= 201103L
1376  /**
1377  * @brief Append an initializer_list of characters.
1378  * @param __l The initializer_list of characters to append.
1379  * @return Reference to this string.
1380  */
1381  basic_string&
1383  { return this->append(__l.begin(), __l.size()); }
1384 #endif // C++11
1385 
1386  /**
1387  * @brief Append a range of characters.
1388  * @param __first Iterator referencing the first character to append.
1389  * @param __last Iterator marking the end of the range.
1390  * @return Reference to this string.
1391  *
1392  * Appends characters in the range [__first,__last) to this string.
1393  */
1394  template<class _InputIterator>
1395  basic_string&
1396  append(_InputIterator __first, _InputIterator __last)
1397  { return this->replace(_M_iend(), _M_iend(), __first, __last); }
1398 
1399 #ifdef __glibcxx_string_view // >= C++17
1400  /**
1401  * @brief Append a string_view.
1402  * @param __svt The object convertible to string_view to be appended.
1403  * @return Reference to this string.
1404  */
1405  template<typename _Tp>
1406  _If_sv<_Tp, basic_string&>
1407  append(const _Tp& __svt)
1408  {
1409  __sv_type __sv = __svt;
1410  return this->append(__sv.data(), __sv.size());
1411  }
1412 
1413  /**
1414  * @brief Append a range of characters from a string_view.
1415  * @param __svt The object convertible to string_view to be appended
1416  * from.
1417  * @param __pos The position in the string_view to append from.
1418  * @param __n The number of characters to append from the string_view.
1419  * @return Reference to this string.
1420  */
1421  template<typename _Tp>
1422  _If_sv<_Tp, basic_string&>
1423  append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1424  {
1425  __sv_type __sv = __svt;
1426  return append(__sv.data()
1427  + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
1428  std::__sv_limit(__sv.size(), __pos, __n));
1429  }
1430 #endif // C++17
1431 
1432  /**
1433  * @brief Append a single character.
1434  * @param __c Character to append.
1435  */
1436  void
1437  push_back(_CharT __c)
1438  {
1439  const size_type __len = 1 + this->size();
1440  if (__len > this->capacity() || _M_rep()->_M_is_shared())
1441  this->reserve(__len);
1442  traits_type::assign(_M_data()[this->size()], __c);
1443  _M_rep()->_M_set_length_and_sharable(__len);
1444  }
1445 
1446  /**
1447  * @brief Set value to contents of another string.
1448  * @param __str Source string to use.
1449  * @return Reference to this string.
1450  */
1451  basic_string&
1452  assign(const basic_string& __str);
1453 
1454 #if __cplusplus >= 201103L
1455  /**
1456  * @brief Set value to contents of another string.
1457  * @param __str Source string to use.
1458  * @return Reference to this string.
1459  *
1460  * This function sets this string to the exact contents of @a __str.
1461  * @a __str is a valid, but unspecified string.
1462  */
1463  basic_string&
1466  {
1467  this->swap(__str);
1468  return *this;
1469  }
1470 #endif // C++11
1471 
1472  /**
1473  * @brief Set value to a substring of a string.
1474  * @param __str The string to use.
1475  * @param __pos Index of the first character of str.
1476  * @param __n Number of characters to use.
1477  * @return Reference to this string.
1478  * @throw std::out_of_range if @a pos is not a valid index.
1479  *
1480  * This function sets this string to the substring of @a __str
1481  * consisting of @a __n characters at @a __pos. If @a __n is
1482  * is larger than the number of available characters in @a
1483  * __str, the remainder of @a __str is used.
1484  */
1485  basic_string&
1486  assign(const basic_string& __str, size_type __pos, size_type __n = npos)
1487  { return this->assign(__str._M_data()
1488  + __str._M_check(__pos, "basic_string::assign"),
1489  __str._M_limit(__pos, __n)); }
1490 
1491  /**
1492  * @brief Set value to a C substring.
1493  * @param __s The C string to use.
1494  * @param __n Number of characters to use.
1495  * @return Reference to this string.
1496  *
1497  * This function sets the value of this string to the first @a __n
1498  * characters of @a __s. If @a __n is is larger than the number of
1499  * available characters in @a __s, the remainder of @a __s is used.
1500  */
1501  basic_string&
1502  assign(const _CharT* __s, size_type __n);
1503 
1504  /**
1505  * @brief Set value to contents of a C string.
1506  * @param __s The C string to use.
1507  * @return Reference to this string.
1508  *
1509  * This function sets the value of this string to the value of @a __s.
1510  * The data is copied, so there is no dependence on @a __s once the
1511  * function returns.
1512  */
1513  basic_string&
1514  assign(const _CharT* __s)
1515  {
1516  __glibcxx_requires_string(__s);
1517  return this->assign(__s, traits_type::length(__s));
1518  }
1519 
1520  /**
1521  * @brief Set value to multiple characters.
1522  * @param __n Length of the resulting string.
1523  * @param __c The character to use.
1524  * @return Reference to this string.
1525  *
1526  * This function sets the value of this string to @a __n copies of
1527  * character @a __c.
1528  */
1529  basic_string&
1530  assign(size_type __n, _CharT __c)
1531  { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1532 
1533  /**
1534  * @brief Set value to a range of characters.
1535  * @param __first Iterator referencing the first character to append.
1536  * @param __last Iterator marking the end of the range.
1537  * @return Reference to this string.
1538  *
1539  * Sets value of string to characters in the range [__first,__last).
1540  */
1541  template<class _InputIterator>
1542  basic_string&
1543  assign(_InputIterator __first, _InputIterator __last)
1544  { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
1545 
1546 #if __glibcxx_containers_ranges // C++ >= 23
1547  /**
1548  * @brief Set value to a range of characters.
1549  * @since C++23
1550  */
1551  template<__detail::__container_compatible_range<_CharT> _Rg>
1552  basic_string&
1553  assign_range(_Rg&& __rg)
1554  {
1555  basic_string __s(from_range, std::forward<_Rg>(__rg),
1556  get_allocator());
1557  assign(std::move(__s));
1558  return *this;
1559  }
1560 #endif
1561 
1562 #if __cplusplus >= 201103L
1563  /**
1564  * @brief Set value to an initializer_list of characters.
1565  * @param __l The initializer_list of characters to assign.
1566  * @return Reference to this string.
1567  */
1568  basic_string&
1570  { return this->assign(__l.begin(), __l.size()); }
1571 #endif // C++11
1572 
1573 #ifdef __glibcxx_string_view // >= C++17
1574  /**
1575  * @brief Set value from a string_view.
1576  * @param __svt The source object convertible to string_view.
1577  * @return Reference to this string.
1578  */
1579  template<typename _Tp>
1580  _If_sv<_Tp, basic_string&>
1581  assign(const _Tp& __svt)
1582  {
1583  __sv_type __sv = __svt;
1584  return this->assign(__sv.data(), __sv.size());
1585  }
1586 
1587  /**
1588  * @brief Set value from a range of characters in a string_view.
1589  * @param __svt The source object convertible to string_view.
1590  * @param __pos The position in the string_view to assign from.
1591  * @param __n The number of characters to assign.
1592  * @return Reference to this string.
1593  */
1594  template<typename _Tp>
1595  _If_sv<_Tp, basic_string&>
1596  assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
1597  {
1598  __sv_type __sv = __svt;
1599  return assign(__sv.data()
1600  + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
1601  std::__sv_limit(__sv.size(), __pos, __n));
1602  }
1603 #endif // C++17
1604 
1605  /**
1606  * @brief Insert multiple characters.
1607  * @param __p Iterator referencing location in string to insert at.
1608  * @param __n Number of characters to insert
1609  * @param __c The character to insert.
1610  * @throw std::length_error If new length exceeds @c max_size().
1611  *
1612  * Inserts @a __n copies of character @a __c starting at the
1613  * position referenced by iterator @a __p. If adding
1614  * characters causes the length to exceed max_size(),
1615  * length_error is thrown. The value of the string doesn't
1616  * change if an error is thrown.
1617  */
1618  void
1619  insert(iterator __p, size_type __n, _CharT __c)
1620  { this->replace(__p, __p, __n, __c); }
1621 
1622  /**
1623  * @brief Insert a range of characters.
1624  * @param __p Iterator referencing location in string to insert at.
1625  * @param __beg Start of range.
1626  * @param __end End of range.
1627  * @throw std::length_error If new length exceeds @c max_size().
1628  *
1629  * Inserts characters in range [__beg,__end). If adding
1630  * characters causes the length to exceed max_size(),
1631  * length_error is thrown. The value of the string doesn't
1632  * change if an error is thrown.
1633  */
1634  template<class _InputIterator>
1635  void
1636  insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1637  { this->replace(__p, __p, __beg, __end); }
1638 
1639 #if __glibcxx_containers_ranges // C++ >= 23
1640  /**
1641  * @brief Insert a range into the string.
1642  * @since C++23
1643  */
1644  template<__detail::__container_compatible_range<_CharT> _Rg>
1645  iterator
1646  insert_range(const_iterator __p, _Rg&& __rg)
1647  {
1648  auto __pos = __p - cbegin();
1649 
1650  if constexpr (ranges::forward_range<_Rg>)
1651  if (ranges::empty(__rg))
1652  return begin() + __pos;
1653 
1654  if (__p == cend())
1655  append_range(std::forward<_Rg>(__rg));
1656  else
1657  {
1658  basic_string __s(from_range, std::forward<_Rg>(__rg),
1659  get_allocator());
1660  insert(__pos, __s);
1661  }
1662  return begin() + __pos;
1663  }
1664 #endif
1665 
1666 #if __cplusplus >= 201103L
1667  /**
1668  * @brief Insert an initializer_list of characters.
1669  * @param __p Iterator referencing location in string to insert at.
1670  * @param __l The initializer_list of characters to insert.
1671  * @throw std::length_error If new length exceeds @c max_size().
1672  */
1673  void
1675  {
1676  _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1677  this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
1678  }
1679 #endif // C++11
1680 
1681  /**
1682  * @brief Insert value of a string.
1683  * @param __pos1 Position in string to insert at.
1684  * @param __str The string to insert.
1685  * @return Reference to this string.
1686  * @throw std::length_error If new length exceeds @c max_size().
1687  *
1688  * Inserts value of @a __str starting at @a __pos1. If adding
1689  * characters causes the length to exceed max_size(),
1690  * length_error is thrown. The value of the string doesn't
1691  * change if an error is thrown.
1692  */
1693  basic_string&
1694  insert(size_type __pos1, const basic_string& __str)
1695  { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1696 
1697  /**
1698  * @brief Insert a substring.
1699  * @param __pos1 Position in string to insert at.
1700  * @param __str The string to insert.
1701  * @param __pos2 Start of characters in str to insert.
1702  * @param __n Number of characters to insert.
1703  * @return Reference to this string.
1704  * @throw std::length_error If new length exceeds @c max_size().
1705  * @throw std::out_of_range If @a pos1 > size() or
1706  * @a __pos2 > @a str.size().
1707  *
1708  * Starting at @a pos1, insert @a __n character of @a __str
1709  * beginning with @a __pos2. If adding characters causes the
1710  * length to exceed max_size(), length_error is thrown. If @a
1711  * __pos1 is beyond the end of this string or @a __pos2 is
1712  * beyond the end of @a __str, out_of_range is thrown. The
1713  * value of the string doesn't change if an error is thrown.
1714  */
1715  basic_string&
1716  insert(size_type __pos1, const basic_string& __str,
1717  size_type __pos2, size_type __n = npos)
1718  { return this->insert(__pos1, __str._M_data()
1719  + __str._M_check(__pos2, "basic_string::insert"),
1720  __str._M_limit(__pos2, __n)); }
1721 
1722  /**
1723  * @brief Insert a C substring.
1724  * @param __pos Position in string to insert at.
1725  * @param __s The C string to insert.
1726  * @param __n The number of characters to insert.
1727  * @return Reference to this string.
1728  * @throw std::length_error If new length exceeds @c max_size().
1729  * @throw std::out_of_range If @a __pos is beyond the end of this
1730  * string.
1731  *
1732  * Inserts the first @a __n characters of @a __s starting at @a
1733  * __pos. If adding characters causes the length to exceed
1734  * max_size(), length_error is thrown. If @a __pos is beyond
1735  * end(), out_of_range is thrown. The value of the string
1736  * doesn't change if an error is thrown.
1737  */
1738  basic_string&
1739  insert(size_type __pos, const _CharT* __s, size_type __n);
1740 
1741  /**
1742  * @brief Insert a C string.
1743  * @param __pos Position in string to insert at.
1744  * @param __s The C string to insert.
1745  * @return Reference to this string.
1746  * @throw std::length_error If new length exceeds @c max_size().
1747  * @throw std::out_of_range If @a pos is beyond the end of this
1748  * string.
1749  *
1750  * Inserts the first @a n characters of @a __s starting at @a __pos. If
1751  * adding characters causes the length to exceed max_size(),
1752  * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1753  * thrown. The value of the string doesn't change if an error is
1754  * thrown.
1755  */
1756  basic_string&
1757  insert(size_type __pos, const _CharT* __s)
1758  {
1759  __glibcxx_requires_string(__s);
1760  return this->insert(__pos, __s, traits_type::length(__s));
1761  }
1762 
1763  /**
1764  * @brief Insert multiple characters.
1765  * @param __pos Index in string to insert at.
1766  * @param __n Number of characters to insert
1767  * @param __c The character to insert.
1768  * @return Reference to this string.
1769  * @throw std::length_error If new length exceeds @c max_size().
1770  * @throw std::out_of_range If @a __pos is beyond the end of this
1771  * string.
1772  *
1773  * Inserts @a __n copies of character @a __c starting at index
1774  * @a __pos. If adding characters causes the length to exceed
1775  * max_size(), length_error is thrown. If @a __pos > length(),
1776  * out_of_range is thrown. The value of the string doesn't
1777  * change if an error is thrown.
1778  */
1779  basic_string&
1780  insert(size_type __pos, size_type __n, _CharT __c)
1781  { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1782  size_type(0), __n, __c); }
1783 
1784  /**
1785  * @brief Insert one character.
1786  * @param __p Iterator referencing position in string to insert at.
1787  * @param __c The character to insert.
1788  * @return Iterator referencing newly inserted char.
1789  * @throw std::length_error If new length exceeds @c max_size().
1790  *
1791  * Inserts character @a __c at position referenced by @a __p.
1792  * If adding character causes the length to exceed max_size(),
1793  * length_error is thrown. If @a __p is beyond end of string,
1794  * out_of_range is thrown. The value of the string doesn't
1795  * change if an error is thrown.
1796  */
1797  iterator
1798  insert(iterator __p, _CharT __c)
1799  {
1800  _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1801  const size_type __pos = __p - _M_ibegin();
1802  _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1803  _M_rep()->_M_set_leaked();
1804  return iterator(_M_data() + __pos);
1805  }
1806 
1807 #ifdef __glibcxx_string_view // >= C++17
1808  /**
1809  * @brief Insert a string_view.
1810  * @param __pos Position in string to insert at.
1811  * @param __svt The object convertible to string_view to insert.
1812  * @return Reference to this string.
1813  */
1814  template<typename _Tp>
1815  _If_sv<_Tp, basic_string&>
1816  insert(size_type __pos, const _Tp& __svt)
1817  {
1818  __sv_type __sv = __svt;
1819  return this->insert(__pos, __sv.data(), __sv.size());
1820  }
1821 
1822  /**
1823  * @brief Insert a string_view.
1824  * @param __pos1 Position in string to insert at.
1825  * @param __svt The object convertible to string_view to insert from.
1826  * @param __pos2 Position in string_view to insert from.
1827  * @param __n The number of characters to insert.
1828  * @return Reference to this string.
1829  */
1830  template<typename _Tp>
1831  _If_sv<_Tp, basic_string&>
1832  insert(size_type __pos1, const _Tp& __svt,
1833  size_type __pos2, size_type __n = npos)
1834  {
1835  __sv_type __sv = __svt;
1836  return this->replace(__pos1, size_type(0), __sv.data()
1837  + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
1838  std::__sv_limit(__sv.size(), __pos2, __n));
1839  }
1840 #endif // C++17
1841 
1842  /**
1843  * @brief Remove characters.
1844  * @param __pos Index of first character to remove (default 0).
1845  * @param __n Number of characters to remove (default remainder).
1846  * @return Reference to this string.
1847  * @throw std::out_of_range If @a pos is beyond the end of this
1848  * string.
1849  *
1850  * Removes @a __n characters from this string starting at @a
1851  * __pos. The length of the string is reduced by @a __n. If
1852  * there are < @a __n characters to remove, the remainder of
1853  * the string is truncated. If @a __p is beyond end of string,
1854  * out_of_range is thrown. The value of the string doesn't
1855  * change if an error is thrown.
1856  */
1857  basic_string&
1858  erase(size_type __pos = 0, size_type __n = npos)
1859  {
1860  _M_mutate(_M_check(__pos, "basic_string::erase"),
1861  _M_limit(__pos, __n), size_type(0));
1862  return *this;
1863  }
1864 
1865  /**
1866  * @brief Remove one character.
1867  * @param __position Iterator referencing the character to remove.
1868  * @return iterator referencing same location after removal.
1869  *
1870  * Removes the character at @a __position from this string. The value
1871  * of the string doesn't change if an error is thrown.
1872  */
1873  iterator
1874  erase(iterator __position)
1875  {
1876  _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1877  && __position < _M_iend());
1878  const size_type __pos = __position - _M_ibegin();
1879  _M_mutate(__pos, size_type(1), size_type(0));
1880  _M_rep()->_M_set_leaked();
1881  return iterator(_M_data() + __pos);
1882  }
1883 
1884  /**
1885  * @brief Remove a range of characters.
1886  * @param __first Iterator referencing the first character to remove.
1887  * @param __last Iterator referencing the end of the range.
1888  * @return Iterator referencing location of first after removal.
1889  *
1890  * Removes the characters in the range [first,last) from this string.
1891  * The value of the string doesn't change if an error is thrown.
1892  */
1893  iterator
1894  erase(iterator __first, iterator __last);
1895 
1896 #if __cplusplus >= 201103L
1897  /**
1898  * @brief Remove the last character.
1899  *
1900  * The string must be non-empty.
1901  */
1902  void
1903  pop_back() // FIXME C++11: should be noexcept.
1904  {
1905  __glibcxx_assert(!empty());
1906  erase(size() - 1, 1);
1907  }
1908 #endif // C++11
1909 
1910  /**
1911  * @brief Replace characters with value from another string.
1912  * @param __pos Index of first character to replace.
1913  * @param __n Number of characters to be replaced.
1914  * @param __str String to insert.
1915  * @return Reference to this string.
1916  * @throw std::out_of_range If @a pos is beyond the end of this
1917  * string.
1918  * @throw std::length_error If new length exceeds @c max_size().
1919  *
1920  * Removes the characters in the range [__pos,__pos+__n) from
1921  * this string. In place, the value of @a __str is inserted.
1922  * If @a __pos is beyond end of string, out_of_range is thrown.
1923  * If the length of the result exceeds max_size(), length_error
1924  * is thrown. The value of the string doesn't change if an
1925  * error is thrown.
1926  */
1927  basic_string&
1928  replace(size_type __pos, size_type __n, const basic_string& __str)
1929  { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1930 
1931  /**
1932  * @brief Replace characters with value from another string.
1933  * @param __pos1 Index of first character to replace.
1934  * @param __n1 Number of characters to be replaced.
1935  * @param __str String to insert.
1936  * @param __pos2 Index of first character of str to use.
1937  * @param __n2 Number of characters from str to use.
1938  * @return Reference to this string.
1939  * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1940  * __str.size().
1941  * @throw std::length_error If new length exceeds @c max_size().
1942  *
1943  * Removes the characters in the range [__pos1,__pos1 + n) from this
1944  * string. In place, the value of @a __str is inserted. If @a __pos is
1945  * beyond end of string, out_of_range is thrown. If the length of the
1946  * result exceeds max_size(), length_error is thrown. The value of the
1947  * string doesn't change if an error is thrown.
1948  */
1949  basic_string&
1950  replace(size_type __pos1, size_type __n1, const basic_string& __str,
1951  size_type __pos2, size_type __n2 = npos)
1952  { return this->replace(__pos1, __n1, __str._M_data()
1953  + __str._M_check(__pos2, "basic_string::replace"),
1954  __str._M_limit(__pos2, __n2)); }
1955 
1956  /**
1957  * @brief Replace characters with value of a C substring.
1958  * @param __pos Index of first character to replace.
1959  * @param __n1 Number of characters to be replaced.
1960  * @param __s C string to insert.
1961  * @param __n2 Number of characters from @a s to use.
1962  * @return Reference to this string.
1963  * @throw std::out_of_range If @a pos1 > size().
1964  * @throw std::length_error If new length exceeds @c max_size().
1965  *
1966  * Removes the characters in the range [__pos,__pos + __n1)
1967  * from this string. In place, the first @a __n2 characters of
1968  * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1969  * @a __pos is beyond end of string, out_of_range is thrown. If
1970  * the length of result exceeds max_size(), length_error is
1971  * thrown. The value of the string doesn't change if an error
1972  * is thrown.
1973  */
1974  basic_string&
1975  replace(size_type __pos, size_type __n1, const _CharT* __s,
1976  size_type __n2);
1977 
1978  /**
1979  * @brief Replace characters with value of a C string.
1980  * @param __pos Index of first character to replace.
1981  * @param __n1 Number of characters to be replaced.
1982  * @param __s C string to insert.
1983  * @return Reference to this string.
1984  * @throw std::out_of_range If @a pos > size().
1985  * @throw std::length_error If new length exceeds @c max_size().
1986  *
1987  * Removes the characters in the range [__pos,__pos + __n1)
1988  * from this string. In place, the characters of @a __s are
1989  * inserted. If @a __pos is beyond end of string, out_of_range
1990  * is thrown. If the length of result exceeds max_size(),
1991  * length_error is thrown. The value of the string doesn't
1992  * change if an error is thrown.
1993  */
1994  basic_string&
1995  replace(size_type __pos, size_type __n1, const _CharT* __s)
1996  {
1997  __glibcxx_requires_string(__s);
1998  return this->replace(__pos, __n1, __s, traits_type::length(__s));
1999  }
2000 
2001  /**
2002  * @brief Replace characters with multiple characters.
2003  * @param __pos Index of first character to replace.
2004  * @param __n1 Number of characters to be replaced.
2005  * @param __n2 Number of characters to insert.
2006  * @param __c Character to insert.
2007  * @return Reference to this string.
2008  * @throw std::out_of_range If @a __pos > size().
2009  * @throw std::length_error If new length exceeds @c max_size().
2010  *
2011  * Removes the characters in the range [pos,pos + n1) from this
2012  * string. In place, @a __n2 copies of @a __c are inserted.
2013  * If @a __pos is beyond end of string, out_of_range is thrown.
2014  * If the length of result exceeds max_size(), length_error is
2015  * thrown. The value of the string doesn't change if an error
2016  * is thrown.
2017  */
2018  basic_string&
2019  replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
2020  { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
2021  _M_limit(__pos, __n1), __n2, __c); }
2022 
2023  /**
2024  * @brief Replace range of characters with string.
2025  * @param __i1 Iterator referencing start of range to replace.
2026  * @param __i2 Iterator referencing end of range to replace.
2027  * @param __str String value to insert.
2028  * @return Reference to this string.
2029  * @throw std::length_error If new length exceeds @c max_size().
2030  *
2031  * Removes the characters in the range [__i1,__i2). In place,
2032  * the value of @a __str is inserted. If the length of result
2033  * exceeds max_size(), length_error is thrown. The value of
2034  * the string doesn't change if an error is thrown.
2035  */
2036  basic_string&
2037  replace(iterator __i1, iterator __i2, const basic_string& __str)
2038  { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
2039 
2040  /**
2041  * @brief Replace range of characters with C substring.
2042  * @param __i1 Iterator referencing start of range to replace.
2043  * @param __i2 Iterator referencing end of range to replace.
2044  * @param __s C string value to insert.
2045  * @param __n Number of characters from s to insert.
2046  * @return Reference to this string.
2047  * @throw std::length_error If new length exceeds @c max_size().
2048  *
2049  * Removes the characters in the range [__i1,__i2). In place,
2050  * the first @a __n characters of @a __s are inserted. If the
2051  * length of result exceeds max_size(), length_error is thrown.
2052  * The value of the string doesn't change if an error is
2053  * thrown.
2054  */
2055  basic_string&
2056  replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
2057  {
2058  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2059  && __i2 <= _M_iend());
2060  return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
2061  }
2062 
2063  /**
2064  * @brief Replace range of characters with C string.
2065  * @param __i1 Iterator referencing start of range to replace.
2066  * @param __i2 Iterator referencing end of range to replace.
2067  * @param __s C string value to insert.
2068  * @return Reference to this string.
2069  * @throw std::length_error If new length exceeds @c max_size().
2070  *
2071  * Removes the characters in the range [__i1,__i2). In place,
2072  * the characters of @a __s are inserted. If the length of
2073  * result exceeds max_size(), length_error is thrown. The
2074  * value of the string doesn't change if an error is thrown.
2075  */
2076  basic_string&
2077  replace(iterator __i1, iterator __i2, const _CharT* __s)
2078  {
2079  __glibcxx_requires_string(__s);
2080  return this->replace(__i1, __i2, __s, traits_type::length(__s));
2081  }
2082 
2083  /**
2084  * @brief Replace range of characters with multiple characters
2085  * @param __i1 Iterator referencing start of range to replace.
2086  * @param __i2 Iterator referencing end of range to replace.
2087  * @param __n Number of characters to insert.
2088  * @param __c Character to insert.
2089  * @return Reference to this string.
2090  * @throw std::length_error If new length exceeds @c max_size().
2091  *
2092  * Removes the characters in the range [__i1,__i2). In place,
2093  * @a __n copies of @a __c are inserted. If the length of
2094  * result exceeds max_size(), length_error is thrown. The
2095  * value of the string doesn't change if an error is thrown.
2096  */
2097  basic_string&
2098  replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
2099  {
2100  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2101  && __i2 <= _M_iend());
2102  return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
2103  }
2104 
2105  /**
2106  * @brief Replace range of characters with range.
2107  * @param __i1 Iterator referencing start of range to replace.
2108  * @param __i2 Iterator referencing end of range to replace.
2109  * @param __k1 Iterator referencing start of range to insert.
2110  * @param __k2 Iterator referencing end of range to insert.
2111  * @return Reference to this string.
2112  * @throw std::length_error If new length exceeds @c max_size().
2113  *
2114  * Removes the characters in the range [__i1,__i2). In place,
2115  * characters in the range [__k1,__k2) are inserted. If the
2116  * length of result exceeds max_size(), length_error is thrown.
2117  * The value of the string doesn't change if an error is
2118  * thrown.
2119  */
2120  template<class _InputIterator>
2121  basic_string&
2122  replace(iterator __i1, iterator __i2,
2123  _InputIterator __k1, _InputIterator __k2)
2124  {
2125  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2126  && __i2 <= _M_iend());
2127  __glibcxx_requires_valid_range(__k1, __k2);
2128  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2129  return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2130  }
2131 
2132  // Specializations for the common case of pointer and iterator:
2133  // useful to avoid the overhead of temporary buffering in _M_replace.
2134  basic_string&
2135  replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
2136  {
2137  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2138  && __i2 <= _M_iend());
2139  __glibcxx_requires_valid_range(__k1, __k2);
2140  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2141  __k1, __k2 - __k1);
2142  }
2143 
2144  basic_string&
2145  replace(iterator __i1, iterator __i2,
2146  const _CharT* __k1, const _CharT* __k2)
2147  {
2148  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2149  && __i2 <= _M_iend());
2150  __glibcxx_requires_valid_range(__k1, __k2);
2151  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2152  __k1, __k2 - __k1);
2153  }
2154 
2155  basic_string&
2156  replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
2157  {
2158  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2159  && __i2 <= _M_iend());
2160  __glibcxx_requires_valid_range(__k1, __k2);
2161  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2162  __k1.base(), __k2 - __k1);
2163  }
2164 
2165  basic_string&
2166  replace(iterator __i1, iterator __i2,
2167  const_iterator __k1, const_iterator __k2)
2168  {
2169  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2170  && __i2 <= _M_iend());
2171  __glibcxx_requires_valid_range(__k1, __k2);
2172  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2173  __k1.base(), __k2 - __k1);
2174  }
2175 
2176 #if __glibcxx_containers_ranges // C++ >= 23
2177  /**
2178  * @brief Replace part of the string with a range.
2179  * @since C++23
2180  */
2181  template<__detail::__container_compatible_range<_CharT> _Rg>
2182  basic_string&
2183  replace_with_range(const_iterator __i1, const_iterator __i2, _Rg&& __rg)
2184  {
2185  if (__i1 == cend())
2186  append_range(std::forward<_Rg>(__rg));
2187  else
2188  {
2189  basic_string __s(from_range, std::forward<_Rg>(__rg),
2190  get_allocator());
2191  replace(__i1 - cbegin(), __i2 - __i1, __s);
2192  }
2193  return *this;
2194  }
2195 #endif
2196 
2197 #if __cplusplus >= 201103L
2198  /**
2199  * @brief Replace range of characters with initializer_list.
2200  * @param __i1 Iterator referencing start of range to replace.
2201  * @param __i2 Iterator referencing end of range to replace.
2202  * @param __l The initializer_list of characters to insert.
2203  * @return Reference to this string.
2204  * @throw std::length_error If new length exceeds @c max_size().
2205  *
2206  * Removes the characters in the range [__i1,__i2). In place,
2207  * characters in the range [__k1,__k2) are inserted. If the
2208  * length of result exceeds max_size(), length_error is thrown.
2209  * The value of the string doesn't change if an error is
2210  * thrown.
2211  */
2212  basic_string& replace(iterator __i1, iterator __i2,
2214  { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
2215 #endif // C++11
2216 
2217 #ifdef __glibcxx_string_view // >= C++17
2218  /**
2219  * @brief Replace range of characters with string_view.
2220  * @param __pos The position to replace at.
2221  * @param __n The number of characters to replace.
2222  * @param __svt The object convertible to string_view to insert.
2223  * @return Reference to this string.
2224  */
2225  template<typename _Tp>
2226  _If_sv<_Tp, basic_string&>
2227  replace(size_type __pos, size_type __n, const _Tp& __svt)
2228  {
2229  __sv_type __sv = __svt;
2230  return this->replace(__pos, __n, __sv.data(), __sv.size());
2231  }
2232 
2233  /**
2234  * @brief Replace range of characters with string_view.
2235  * @param __pos1 The position to replace at.
2236  * @param __n1 The number of characters to replace.
2237  * @param __svt The object convertible to string_view to insert from.
2238  * @param __pos2 The position in the string_view to insert from.
2239  * @param __n2 The number of characters to insert.
2240  * @return Reference to this string.
2241  */
2242  template<typename _Tp>
2243  _If_sv<_Tp, basic_string&>
2244  replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2245  size_type __pos2, size_type __n2 = npos)
2246  {
2247  __sv_type __sv = __svt;
2248  return this->replace(__pos1, __n1,
2249  __sv.data()
2250  + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
2251  std::__sv_limit(__sv.size(), __pos2, __n2));
2252  }
2253 
2254  /**
2255  * @brief Replace range of characters with string_view.
2256  * @param __i1 An iterator referencing the start position
2257  * to replace at.
2258  * @param __i2 An iterator referencing the end position
2259  * for the replace.
2260  * @param __svt The object convertible to string_view to insert from.
2261  * @return Reference to this string.
2262  */
2263  template<typename _Tp>
2264  _If_sv<_Tp, basic_string&>
2265  replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2266  {
2267  __sv_type __sv = __svt;
2268  return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2269  }
2270 #endif // C++17
2271 
2272  private:
2273  template<class _Integer>
2274  basic_string&
2275  _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
2276  _Integer __val, __true_type)
2277  { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
2278 
2279  template<class _InputIterator>
2280  basic_string&
2281  _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
2282  _InputIterator __k2, __false_type);
2283 
2284  basic_string&
2285  _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2286  _CharT __c);
2287 
2288  basic_string&
2289  _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
2290  size_type __n2);
2291 
2292  // _S_construct_aux is used to implement the 21.3.1 para 15 which
2293  // requires special behaviour if _InIter is an integral type
2294  template<class _InIterator>
2295  static _CharT*
2296  _S_construct_aux(_InIterator __beg, _InIterator __end,
2297  const _Alloc& __a, __false_type)
2298  {
2299  typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
2300  return _S_construct(__beg, __end, __a, _Tag());
2301  }
2302 
2303  // _GLIBCXX_RESOLVE_LIB_DEFECTS
2304  // 438. Ambiguity in the "do the right thing" clause
2305  template<class _Integer>
2306  static _CharT*
2307  _S_construct_aux(_Integer __beg, _Integer __end,
2308  const _Alloc& __a, __true_type)
2309  { return _S_construct_aux_2(static_cast<size_type>(__beg),
2310  __end, __a); }
2311 
2312  static _CharT*
2313  _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
2314  { return _S_construct(__req, __c, __a); }
2315 
2316  template<class _InIterator>
2317  static _CharT*
2318  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
2319  {
2320  typedef typename std::__is_integer<_InIterator>::__type _Integral;
2321  return _S_construct_aux(__beg, __end, __a, _Integral());
2322  }
2323 
2324  // For Input Iterators, used in istreambuf_iterators, etc.
2325  template<class _InIterator>
2326  static _CharT*
2327  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
2328  input_iterator_tag);
2329 
2330  // For forward_iterators up to random_access_iterators, used for
2331  // string::iterator, _CharT*, etc.
2332  template<class _FwdIterator>
2333  static _CharT*
2334  _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
2335  forward_iterator_tag);
2336 
2337  static _CharT*
2338  _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
2339 
2340  public:
2341 
2342  /**
2343  * @brief Copy substring into C string.
2344  * @param __s C string to copy value into.
2345  * @param __n Number of characters to copy.
2346  * @param __pos Index of first character to copy.
2347  * @return Number of characters actually copied
2348  * @throw std::out_of_range If __pos > size().
2349  *
2350  * Copies up to @a __n characters starting at @a __pos into the
2351  * C string @a __s. If @a __pos is %greater than size(),
2352  * out_of_range is thrown.
2353  */
2354  size_type
2355  copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2356 
2357  /**
2358  * @brief Swap contents with another string.
2359  * @param __s String to swap with.
2360  *
2361  * Exchanges the contents of this string with that of @a __s in constant
2362  * time.
2363  */
2364  void
2367 
2368  // String operations:
2369  /**
2370  * @brief Return const pointer to null-terminated contents.
2371  *
2372  * This is a handle to internal data. Do not modify or dire things may
2373  * happen.
2374  */
2375  const _CharT*
2376  c_str() const _GLIBCXX_NOEXCEPT
2377  { return _M_data(); }
2378 
2379  /**
2380  * @brief Return const pointer to contents.
2381  *
2382  * This is a pointer to internal data. It is undefined to modify
2383  * the contents through the returned pointer. To get a pointer that
2384  * allows modifying the contents use @c &str[0] instead,
2385  * (or in C++17 the non-const @c str.data() overload).
2386  */
2387  const _CharT*
2388  data() const _GLIBCXX_NOEXCEPT
2389  { return _M_data(); }
2390 
2391 #if __cplusplus >= 201703L
2392  /**
2393  * @brief Return non-const pointer to contents.
2394  *
2395  * This is a pointer to the character sequence held by the string.
2396  * Modifying the characters in the sequence is allowed.
2397  *
2398  * The standard requires this function to be `noexcept` but for the
2399  * Copy-On-Write string implementation it can throw. This function
2400  * allows modifying the string contents directly, which means we
2401  * must copy-on-write to unshare it, which requires allocating memory.
2402  */
2403  _CharT*
2404  data() noexcept(false)
2405  {
2406  _M_leak();
2407  return _M_data();
2408  }
2409 #endif
2410 
2411  /**
2412  * @brief Return copy of allocator used to construct this string.
2413  */
2414  allocator_type
2415  get_allocator() const _GLIBCXX_NOEXCEPT
2416  { return _M_dataplus; }
2417 
2418  /**
2419  * @brief Find position of a C substring.
2420  * @param __s C string to locate.
2421  * @param __pos Index of character to search from.
2422  * @param __n Number of characters from @a s to search for.
2423  * @return Index of start of first occurrence.
2424  *
2425  * Starting from @a __pos, searches forward for the first @a
2426  * __n characters in @a __s within this string. If found,
2427  * returns the index where it begins. If not found, returns
2428  * npos.
2429  */
2430  size_type
2431  find(const _CharT* __s, size_type __pos, size_type __n) const
2432  _GLIBCXX_NOEXCEPT;
2433 
2434  /**
2435  * @brief Find position of a string.
2436  * @param __str String to locate.
2437  * @param __pos Index of character to search from (default 0).
2438  * @return Index of start of first occurrence.
2439  *
2440  * Starting from @a __pos, searches forward for value of @a __str within
2441  * this string. If found, returns the index where it begins. If not
2442  * found, returns npos.
2443  */
2444  size_type
2445  find(const basic_string& __str, size_type __pos = 0) const
2446  _GLIBCXX_NOEXCEPT
2447  { return this->find(__str.data(), __pos, __str.size()); }
2448 
2449  /**
2450  * @brief Find position of a C string.
2451  * @param __s C string to locate.
2452  * @param __pos Index of character to search from (default 0).
2453  * @return Index of start of first occurrence.
2454  *
2455  * Starting from @a __pos, searches forward for the value of @a
2456  * __s within this string. If found, returns the index where
2457  * it begins. If not found, returns npos.
2458  */
2459  size_type
2460  find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2461  {
2462  __glibcxx_requires_string(__s);
2463  return this->find(__s, __pos, traits_type::length(__s));
2464  }
2465 
2466  /**
2467  * @brief Find position of a character.
2468  * @param __c Character to locate.
2469  * @param __pos Index of character to search from (default 0).
2470  * @return Index of first occurrence.
2471  *
2472  * Starting from @a __pos, searches forward for @a __c within
2473  * this string. If found, returns the index where it was
2474  * found. If not found, returns npos.
2475  */
2476  size_type
2477  find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
2478 
2479 #ifdef __glibcxx_string_view // >= C++17
2480  /**
2481  * @brief Find position of a string_view.
2482  * @param __svt The object convertible to string_view to locate.
2483  * @param __pos Index of character to search from (default 0).
2484  * @return Index of start of first occurrence.
2485  */
2486  template<typename _Tp>
2487  _If_sv<_Tp, size_type>
2488  find(const _Tp& __svt, size_type __pos = 0) const
2489  noexcept(is_same<_Tp, __sv_type>::value)
2490  {
2491  __sv_type __sv = __svt;
2492  return this->find(__sv.data(), __pos, __sv.size());
2493  }
2494 #endif // C++17
2495 
2496  /**
2497  * @brief Find last position of a string.
2498  * @param __str String to locate.
2499  * @param __pos Index of character to search back from (default end).
2500  * @return Index of start of last occurrence.
2501  *
2502  * Starting from @a __pos, searches backward for value of @a
2503  * __str within this string. If found, returns the index where
2504  * it begins. If not found, returns npos.
2505  */
2506  size_type
2507  rfind(const basic_string& __str, size_type __pos = npos) const
2508  _GLIBCXX_NOEXCEPT
2509  { return this->rfind(__str.data(), __pos, __str.size()); }
2510 
2511  /**
2512  * @brief Find last position of a C substring.
2513  * @param __s C string to locate.
2514  * @param __pos Index of character to search back from.
2515  * @param __n Number of characters from s to search for.
2516  * @return Index of start of last occurrence.
2517  *
2518  * Starting from @a __pos, searches backward for the first @a
2519  * __n characters in @a __s within this string. If found,
2520  * returns the index where it begins. If not found, returns
2521  * npos.
2522  */
2523  size_type
2524  rfind(const _CharT* __s, size_type __pos, size_type __n) const
2525  _GLIBCXX_NOEXCEPT;
2526 
2527  /**
2528  * @brief Find last position of a C string.
2529  * @param __s C string to locate.
2530  * @param __pos Index of character to start search at (default end).
2531  * @return Index of start of last occurrence.
2532  *
2533  * Starting from @a __pos, searches backward for the value of
2534  * @a __s within this string. If found, returns the index
2535  * where it begins. If not found, returns npos.
2536  */
2537  size_type
2538  rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2539  {
2540  __glibcxx_requires_string(__s);
2541  return this->rfind(__s, __pos, traits_type::length(__s));
2542  }
2543 
2544  /**
2545  * @brief Find last position of a character.
2546  * @param __c Character to locate.
2547  * @param __pos Index of character to search back from (default end).
2548  * @return Index of last occurrence.
2549  *
2550  * Starting from @a __pos, searches backward for @a __c within
2551  * this string. If found, returns the index where it was
2552  * found. If not found, returns npos.
2553  */
2554  size_type
2555  rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
2556 
2557 #ifdef __glibcxx_string_view // >= C++17
2558  /**
2559  * @brief Find last position of a string_view.
2560  * @param __svt The object convertible to string_view to locate.
2561  * @param __pos Index of character to search back from (default end).
2562  * @return Index of start of last occurrence.
2563  */
2564  template<typename _Tp>
2565  _If_sv<_Tp, size_type>
2566  rfind(const _Tp& __svt, size_type __pos = npos) const
2568  {
2569  __sv_type __sv = __svt;
2570  return this->rfind(__sv.data(), __pos, __sv.size());
2571  }
2572 #endif // C++17
2573 
2574  /**
2575  * @brief Find position of a character of string.
2576  * @param __str String containing characters to locate.
2577  * @param __pos Index of character to search from (default 0).
2578  * @return Index of first occurrence.
2579  *
2580  * Starting from @a __pos, searches forward for one of the
2581  * characters of @a __str within this string. If found,
2582  * returns the index where it was found. If not found, returns
2583  * npos.
2584  */
2585  size_type
2586  find_first_of(const basic_string& __str, size_type __pos = 0) const
2587  _GLIBCXX_NOEXCEPT
2588  { return this->find_first_of(__str.data(), __pos, __str.size()); }
2589 
2590  /**
2591  * @brief Find position of a character of C substring.
2592  * @param __s String containing characters to locate.
2593  * @param __pos Index of character to search from.
2594  * @param __n Number of characters from s to search for.
2595  * @return Index of first occurrence.
2596  *
2597  * Starting from @a __pos, searches forward for one of the
2598  * first @a __n characters of @a __s within this string. If
2599  * found, returns the index where it was found. If not found,
2600  * returns npos.
2601  */
2602  size_type
2603  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
2604  _GLIBCXX_NOEXCEPT;
2605 
2606  /**
2607  * @brief Find position of a character of C string.
2608  * @param __s String containing characters to locate.
2609  * @param __pos Index of character to search from (default 0).
2610  * @return Index of first occurrence.
2611  *
2612  * Starting from @a __pos, searches forward for one of the
2613  * characters of @a __s within this string. If found, returns
2614  * the index where it was found. If not found, returns npos.
2615  */
2616  size_type
2617  find_first_of(const _CharT* __s, size_type __pos = 0) const
2618  _GLIBCXX_NOEXCEPT
2619  {
2620  __glibcxx_requires_string(__s);
2621  return this->find_first_of(__s, __pos, traits_type::length(__s));
2622  }
2623 
2624  /**
2625  * @brief Find position of a character.
2626  * @param __c Character to locate.
2627  * @param __pos Index of character to search from (default 0).
2628  * @return Index of first occurrence.
2629  *
2630  * Starting from @a __pos, searches forward for the character
2631  * @a __c within this string. If found, returns the index
2632  * where it was found. If not found, returns npos.
2633  *
2634  * Note: equivalent to find(__c, __pos).
2635  */
2636  size_type
2637  find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2638  { return this->find(__c, __pos); }
2639 
2640 #ifdef __glibcxx_string_view // >= C++17
2641  /**
2642  * @brief Find position of a character of a string_view.
2643  * @param __svt An object convertible to string_view containing
2644  * characters to locate.
2645  * @param __pos Index of character to search from (default 0).
2646  * @return Index of first occurrence.
2647  */
2648  template<typename _Tp>
2649  _If_sv<_Tp, size_type>
2650  find_first_of(const _Tp& __svt, size_type __pos = 0) const
2651  noexcept(is_same<_Tp, __sv_type>::value)
2652  {
2653  __sv_type __sv = __svt;
2654  return this->find_first_of(__sv.data(), __pos, __sv.size());
2655  }
2656 #endif // C++17
2657 
2658  /**
2659  * @brief Find last position of a character of string.
2660  * @param __str String containing characters to locate.
2661  * @param __pos Index of character to search back from (default end).
2662  * @return Index of last occurrence.
2663  *
2664  * Starting from @a __pos, searches backward for one of the
2665  * characters of @a __str within this string. If found,
2666  * returns the index where it was found. If not found, returns
2667  * npos.
2668  */
2669  size_type
2670  find_last_of(const basic_string& __str, size_type __pos = npos) const
2671  _GLIBCXX_NOEXCEPT
2672  { return this->find_last_of(__str.data(), __pos, __str.size()); }
2673 
2674  /**
2675  * @brief Find last position of a character of C substring.
2676  * @param __s C string containing characters to locate.
2677  * @param __pos Index of character to search back from.
2678  * @param __n Number of characters from s to search for.
2679  * @return Index of last occurrence.
2680  *
2681  * Starting from @a __pos, searches backward for one of the
2682  * first @a __n characters of @a __s within this string. If
2683  * found, returns the index where it was found. If not found,
2684  * returns npos.
2685  */
2686  size_type
2687  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
2688  _GLIBCXX_NOEXCEPT;
2689 
2690  /**
2691  * @brief Find last position of a character of C string.
2692  * @param __s C string containing characters to locate.
2693  * @param __pos Index of character to search back from (default end).
2694  * @return Index of last occurrence.
2695  *
2696  * Starting from @a __pos, searches backward for one of the
2697  * characters of @a __s within this string. If found, returns
2698  * the index where it was found. If not found, returns npos.
2699  */
2700  size_type
2701  find_last_of(const _CharT* __s, size_type __pos = npos) const
2702  _GLIBCXX_NOEXCEPT
2703  {
2704  __glibcxx_requires_string(__s);
2705  return this->find_last_of(__s, __pos, traits_type::length(__s));
2706  }
2707 
2708  /**
2709  * @brief Find last position of a character.
2710  * @param __c Character to locate.
2711  * @param __pos Index of character to search back from (default end).
2712  * @return Index of last occurrence.
2713  *
2714  * Starting from @a __pos, searches backward for @a __c within
2715  * this string. If found, returns the index where it was
2716  * found. If not found, returns npos.
2717  *
2718  * Note: equivalent to rfind(__c, __pos).
2719  */
2720  size_type
2721  find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2722  { return this->rfind(__c, __pos); }
2723 
2724 #ifdef __glibcxx_string_view // >= C++17
2725  /**
2726  * @brief Find last position of a character of string.
2727  * @param __svt An object convertible to string_view containing
2728  * characters to locate.
2729  * @param __pos Index of character to search back from (default end).
2730  * @return Index of last occurrence.
2731  */
2732  template<typename _Tp>
2733  _If_sv<_Tp, size_type>
2734  find_last_of(const _Tp& __svt, size_type __pos = npos) const
2736  {
2737  __sv_type __sv = __svt;
2738  return this->find_last_of(__sv.data(), __pos, __sv.size());
2739  }
2740 #endif // C++17
2741 
2742  /**
2743  * @brief Find position of a character not in string.
2744  * @param __str String containing characters to avoid.
2745  * @param __pos Index of character to search from (default 0).
2746  * @return Index of first occurrence.
2747  *
2748  * Starting from @a __pos, searches forward for a character not contained
2749  * in @a __str within this string. If found, returns the index where it
2750  * was found. If not found, returns npos.
2751  */
2752  size_type
2753  find_first_not_of(const basic_string& __str, size_type __pos = 0) const
2754  _GLIBCXX_NOEXCEPT
2755  { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2756 
2757  /**
2758  * @brief Find position of a character not in C substring.
2759  * @param __s C string containing characters to avoid.
2760  * @param __pos Index of character to search from.
2761  * @param __n Number of characters from __s to consider.
2762  * @return Index of first occurrence.
2763  *
2764  * Starting from @a __pos, searches forward for a character not
2765  * contained in the first @a __n characters of @a __s within
2766  * this string. If found, returns the index where it was
2767  * found. If not found, returns npos.
2768  */
2769  size_type
2770  find_first_not_of(const _CharT* __s, size_type __pos,
2771  size_type __n) const _GLIBCXX_NOEXCEPT;
2772 
2773  /**
2774  * @brief Find position of a character not in C string.
2775  * @param __s C string containing characters to avoid.
2776  * @param __pos Index of character to search from (default 0).
2777  * @return Index of first occurrence.
2778  *
2779  * Starting from @a __pos, searches forward for a character not
2780  * contained in @a __s within this string. If found, returns
2781  * the index where it was found. If not found, returns npos.
2782  */
2783  size_type
2784  find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2785  _GLIBCXX_NOEXCEPT
2786  {
2787  __glibcxx_requires_string(__s);
2788  return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2789  }
2790 
2791  /**
2792  * @brief Find position of a different character.
2793  * @param __c Character to avoid.
2794  * @param __pos Index of character to search from (default 0).
2795  * @return Index of first occurrence.
2796  *
2797  * Starting from @a __pos, searches forward for a character
2798  * other than @a __c within this string. If found, returns the
2799  * index where it was found. If not found, returns npos.
2800  */
2801  size_type
2802  find_first_not_of(_CharT __c, size_type __pos = 0) const
2803  _GLIBCXX_NOEXCEPT;
2804 
2805 #ifdef __glibcxx_string_view // >= C++17
2806  /**
2807  * @brief Find position of a character not in a string_view.
2808  * @param __svt An object convertible to string_view containing
2809  * characters to avoid.
2810  * @param __pos Index of character to search from (default 0).
2811  * @return Index of first occurrence.
2812  */
2813  template<typename _Tp>
2814  _If_sv<_Tp, size_type>
2815  find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
2816  noexcept(is_same<_Tp, __sv_type>::value)
2817  {
2818  __sv_type __sv = __svt;
2819  return this->find_first_not_of(__sv.data(), __pos, __sv.size());
2820  }
2821 #endif // C++17
2822 
2823  /**
2824  * @brief Find last position of a character not in string.
2825  * @param __str String containing characters to avoid.
2826  * @param __pos Index of character to search back from (default end).
2827  * @return Index of last occurrence.
2828  *
2829  * Starting from @a __pos, searches backward for a character
2830  * not contained in @a __str within this string. If found,
2831  * returns the index where it was found. If not found, returns
2832  * npos.
2833  */
2834  size_type
2835  find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2836  _GLIBCXX_NOEXCEPT
2837  { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2838 
2839  /**
2840  * @brief Find last position of a character not in C substring.
2841  * @param __s C string containing characters to avoid.
2842  * @param __pos Index of character to search back from.
2843  * @param __n Number of characters from s to consider.
2844  * @return Index of last occurrence.
2845  *
2846  * Starting from @a __pos, searches backward for a character not
2847  * contained in the first @a __n characters of @a __s within this string.
2848  * If found, returns the index where it was found. If not found,
2849  * returns npos.
2850  */
2851  size_type
2852  find_last_not_of(const _CharT* __s, size_type __pos,
2853  size_type __n) const _GLIBCXX_NOEXCEPT;
2854  /**
2855  * @brief Find last position of a character not in C string.
2856  * @param __s C string containing characters to avoid.
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 a character
2861  * not contained in @a __s within this string. If found,
2862  * returns the index where it was found. If not found, returns
2863  * npos.
2864  */
2865  size_type
2866  find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2867  _GLIBCXX_NOEXCEPT
2868  {
2869  __glibcxx_requires_string(__s);
2870  return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2871  }
2872 
2873  /**
2874  * @brief Find last position of a different character.
2875  * @param __c Character to avoid.
2876  * @param __pos Index of character to search back from (default end).
2877  * @return Index of last occurrence.
2878  *
2879  * Starting from @a __pos, searches backward for a character other than
2880  * @a __c within this string. If found, returns the index where it was
2881  * found. If not found, returns npos.
2882  */
2883  size_type
2884  find_last_not_of(_CharT __c, size_type __pos = npos) const
2885  _GLIBCXX_NOEXCEPT;
2886 
2887 #ifdef __glibcxx_string_view // >= C++17
2888  /**
2889  * @brief Find last position of a character not in a string_view.
2890  * @param __svt An object convertible to string_view containing
2891  * characters to avoid.
2892  * @param __pos Index of character to search back from (default end).
2893  * @return Index of last occurrence.
2894  */
2895  template<typename _Tp>
2896  _If_sv<_Tp, size_type>
2897  find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
2899  {
2900  __sv_type __sv = __svt;
2901  return this->find_last_not_of(__sv.data(), __pos, __sv.size());
2902  }
2903 #endif // C++17
2904 
2905  /**
2906  * @brief Get a substring.
2907  * @param __pos Index of first character (default 0).
2908  * @param __n Number of characters in substring (default remainder).
2909  * @return The new string.
2910  * @throw std::out_of_range If __pos > size().
2911  *
2912  * Construct and return a new string using the @a __n
2913  * characters starting at @a __pos. If the string is too
2914  * short, use the remainder of the characters. If @a __pos is
2915  * beyond the end of the string, out_of_range is thrown.
2916  */
2917  basic_string
2918  substr(size_type __pos = 0, size_type __n = npos) const
2919  { return basic_string(*this,
2920  _M_check(__pos, "basic_string::substr"), __n); }
2921 
2922  /**
2923  * @brief Compare to a string.
2924  * @param __str String to compare against.
2925  * @return Integer < 0, 0, or > 0.
2926  *
2927  * Returns an integer < 0 if this string is ordered before @a
2928  * __str, 0 if their values are equivalent, or > 0 if this
2929  * string is ordered after @a __str. Determines the effective
2930  * length rlen of the strings to compare as the smallest of
2931  * size() and str.size(). The function then compares the two
2932  * strings by calling traits::compare(data(), str.data(),rlen).
2933  * If the result of the comparison is nonzero returns it,
2934  * otherwise the shorter one is ordered first.
2935  */
2936  int
2937  compare(const basic_string& __str) const
2938  {
2939  const size_type __size = this->size();
2940  const size_type __osize = __str.size();
2941  const size_type __len = std::min(__size, __osize);
2942 
2943  int __r = traits_type::compare(_M_data(), __str.data(), __len);
2944  if (!__r)
2945  __r = _S_compare(__size, __osize);
2946  return __r;
2947  }
2948 
2949 #ifdef __glibcxx_string_view // >= C++17
2950  /**
2951  * @brief Compare to a string_view.
2952  * @param __svt An object convertible to string_view to compare against.
2953  * @return Integer < 0, 0, or > 0.
2954  */
2955  template<typename _Tp>
2956  _If_sv<_Tp, int>
2957  compare(const _Tp& __svt) const
2959  {
2960  __sv_type __sv = __svt;
2961  const size_type __size = this->size();
2962  const size_type __osize = __sv.size();
2963  const size_type __len = std::min(__size, __osize);
2964 
2965  int __r = traits_type::compare(_M_data(), __sv.data(), __len);
2966  if (!__r)
2967  __r = _S_compare(__size, __osize);
2968  return __r;
2969  }
2970 
2971  /**
2972  * @brief Compare to a string_view.
2973  * @param __pos A position in the string to start comparing from.
2974  * @param __n The number of characters to compare.
2975  * @param __svt An object convertible to string_view to compare
2976  * against.
2977  * @return Integer < 0, 0, or > 0.
2978  */
2979  template<typename _Tp>
2980  _If_sv<_Tp, int>
2981  compare(size_type __pos, size_type __n, const _Tp& __svt) const
2982  noexcept(is_same<_Tp, __sv_type>::value)
2983  {
2984  __sv_type __sv = __svt;
2985  return __sv_type(*this).substr(__pos, __n).compare(__sv);
2986  }
2987 
2988  /**
2989  * @brief Compare to a string_view.
2990  * @param __pos1 A position in the string to start comparing from.
2991  * @param __n1 The number of characters to compare.
2992  * @param __svt An object convertible to string_view to compare
2993  * against.
2994  * @param __pos2 A position in the string_view to start comparing from.
2995  * @param __n2 The number of characters to compare.
2996  * @return Integer < 0, 0, or > 0.
2997  */
2998  template<typename _Tp>
2999  _If_sv<_Tp, int>
3000  compare(size_type __pos1, size_type __n1, const _Tp& __svt,
3001  size_type __pos2, size_type __n2 = npos) const
3002  noexcept(is_same<_Tp, __sv_type>::value)
3003  {
3004  __sv_type __sv = __svt;
3005  return __sv_type(*this)
3006  .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3007  }
3008 #endif // C++17
3009 
3010  /**
3011  * @brief Compare substring to a string.
3012  * @param __pos Index of first character of substring.
3013  * @param __n Number of characters in substring.
3014  * @param __str String to compare against.
3015  * @return Integer < 0, 0, or > 0.
3016  *
3017  * Form the substring of this string from the @a __n characters
3018  * starting at @a __pos. Returns an integer < 0 if the
3019  * substring is ordered before @a __str, 0 if their values are
3020  * equivalent, or > 0 if the substring is ordered after @a
3021  * __str. Determines the effective length rlen of the strings
3022  * to compare as the smallest of the length of the substring
3023  * and @a __str.size(). The function then compares the two
3024  * strings by calling
3025  * traits::compare(substring.data(),str.data(),rlen). If the
3026  * result of the comparison is nonzero returns it, otherwise
3027  * the shorter one is ordered first.
3028  */
3029  int
3030  compare(size_type __pos, size_type __n, const basic_string& __str) const
3031  {
3032  _M_check(__pos, "basic_string::compare");
3033  __n = _M_limit(__pos, __n);
3034  const size_type __osize = __str.size();
3035  const size_type __len = std::min(__n, __osize);
3036  int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
3037  if (!__r)
3038  __r = _S_compare(__n, __osize);
3039  return __r;
3040  }
3041 
3042  /**
3043  * @brief Compare substring to a substring.
3044  * @param __pos1 Index of first character of substring.
3045  * @param __n1 Number of characters in substring.
3046  * @param __str String to compare against.
3047  * @param __pos2 Index of first character of substring of str.
3048  * @param __n2 Number of characters in substring of str.
3049  * @return Integer < 0, 0, or > 0.
3050  *
3051  * Form the substring of this string from the @a __n1
3052  * characters starting at @a __pos1. Form the substring of @a
3053  * __str from the @a __n2 characters starting at @a __pos2.
3054  * Returns an integer < 0 if this substring is ordered before
3055  * the substring of @a __str, 0 if their values are equivalent,
3056  * or > 0 if this substring is ordered after the substring of
3057  * @a __str. Determines the effective length rlen of the
3058  * strings to compare as the smallest of the lengths of the
3059  * substrings. The function then compares the two strings by
3060  * calling
3061  * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
3062  * If the result of the comparison is nonzero returns it,
3063  * otherwise the shorter one is ordered first.
3064  */
3065  int
3066  compare(size_type __pos1, size_type __n1, const basic_string& __str,
3067  size_type __pos2, size_type __n2 = npos) const
3068  {
3069  _M_check(__pos1, "basic_string::compare");
3070  __str._M_check(__pos2, "basic_string::compare");
3071  __n1 = _M_limit(__pos1, __n1);
3072  __n2 = __str._M_limit(__pos2, __n2);
3073  const size_type __len = std::min(__n1, __n2);
3074  int __r = traits_type::compare(_M_data() + __pos1,
3075  __str.data() + __pos2, __len);
3076  if (!__r)
3077  __r = _S_compare(__n1, __n2);
3078  return __r;
3079  }
3080 
3081  /**
3082  * @brief Compare to a C string.
3083  * @param __s C string to compare against.
3084  * @return Integer < 0, 0, or > 0.
3085  *
3086  * Returns an integer < 0 if this string is ordered before @a __s, 0 if
3087  * their values are equivalent, or > 0 if this string is ordered after
3088  * @a __s. Determines the effective length rlen of the strings to
3089  * compare as the smallest of size() and the length of a string
3090  * constructed from @a __s. The function then compares the two strings
3091  * by calling traits::compare(data(),s,rlen). If the result of the
3092  * comparison is nonzero returns it, otherwise the shorter one is
3093  * ordered first.
3094  */
3095  int
3096  compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3097  {
3098  __glibcxx_requires_string(__s);
3099  const size_type __size = this->size();
3100  const size_type __osize = traits_type::length(__s);
3101  const size_type __len = std::min(__size, __osize);
3102  int __r = traits_type::compare(_M_data(), __s, __len);
3103  if (!__r)
3104  __r = _S_compare(__size, __osize);
3105  return __r;
3106  }
3107 
3108  // _GLIBCXX_RESOLVE_LIB_DEFECTS
3109  // 5 String::compare specification questionable
3110  /**
3111  * @brief Compare substring to a C string.
3112  * @param __pos Index of first character of substring.
3113  * @param __n1 Number of characters in substring.
3114  * @param __s C string to compare against.
3115  * @return Integer < 0, 0, or > 0.
3116  *
3117  * Form the substring of this string from the @a __n1
3118  * characters starting at @a pos. Returns an integer < 0 if
3119  * the substring is ordered before @a __s, 0 if their values
3120  * are equivalent, or > 0 if the substring is ordered after @a
3121  * __s. Determines the effective length rlen of the strings to
3122  * compare as the smallest of the length of the substring and
3123  * the length of a string constructed from @a __s. The
3124  * function then compares the two string by calling
3125  * traits::compare(substring.data(),__s,rlen). If the result of
3126  * the comparison is nonzero returns it, otherwise the shorter
3127  * one is ordered first.
3128  */
3129  int
3130  compare(size_type __pos, size_type __n1, const _CharT* __s) const
3131  {
3132  __glibcxx_requires_string(__s);
3133  _M_check(__pos, "basic_string::compare");
3134  __n1 = _M_limit(__pos, __n1);
3135  const size_type __osize = traits_type::length(__s);
3136  const size_type __len = std::min(__n1, __osize);
3137  int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3138  if (!__r)
3139  __r = _S_compare(__n1, __osize);
3140  return __r;
3141  }
3142 
3143  /**
3144  * @brief Compare substring against a character %array.
3145  * @param __pos Index of first character of substring.
3146  * @param __n1 Number of characters in substring.
3147  * @param __s character %array to compare against.
3148  * @param __n2 Number of characters of s.
3149  * @return Integer < 0, 0, or > 0.
3150  *
3151  * Form the substring of this string from the @a __n1
3152  * characters starting at @a __pos. Form a string from the
3153  * first @a __n2 characters of @a __s. Returns an integer < 0
3154  * if this substring is ordered before the string from @a __s,
3155  * 0 if their values are equivalent, or > 0 if this substring
3156  * is ordered after the string from @a __s. Determines the
3157  * effective length rlen of the strings to compare as the
3158  * smallest of the length of the substring and @a __n2. The
3159  * function then compares the two strings by calling
3160  * traits::compare(substring.data(),s,rlen). If the result of
3161  * the comparison is nonzero returns it, otherwise the shorter
3162  * one is ordered first.
3163  *
3164  * NB: s must have at least n2 characters, &apos;\\0&apos; has
3165  * no special meaning.
3166  */
3167  int
3168  compare(size_type __pos, size_type __n1, const _CharT* __s,
3169  size_type __n2) const
3170  {
3171  __glibcxx_requires_string_len(__s, __n2);
3172  _M_check(__pos, "basic_string::compare");
3173  __n1 = _M_limit(__pos, __n1);
3174  const size_type __len = std::min(__n1, __n2);
3175  int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3176  if (!__r)
3177  __r = _S_compare(__n1, __n2);
3178  return __r;
3179  }
3180 
3181 #if __cplusplus > 201703L
3182  bool
3183  starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3184  { return __sv_type(this->data(), this->size()).starts_with(__x); }
3185 
3186  bool
3187  starts_with(_CharT __x) const noexcept
3188  { return __sv_type(this->data(), this->size()).starts_with(__x); }
3189 
3190  [[__gnu__::__nonnull__]]
3191  bool
3192  starts_with(const _CharT* __x) const noexcept
3193  { return __sv_type(this->data(), this->size()).starts_with(__x); }
3194 
3195  bool
3196  ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3197  { return __sv_type(this->data(), this->size()).ends_with(__x); }
3198 
3199  bool
3200  ends_with(_CharT __x) const noexcept
3201  { return __sv_type(this->data(), this->size()).ends_with(__x); }
3202 
3203  [[__gnu__::__nonnull__]]
3204  bool
3205  ends_with(const _CharT* __x) const noexcept
3206  { return __sv_type(this->data(), this->size()).ends_with(__x); }
3207 #endif // C++20
3208 
3209 #if __cplusplus > 202011L
3210  bool
3211  contains(basic_string_view<_CharT, _Traits> __x) const noexcept
3212  { return __sv_type(this->data(), this->size()).contains(__x); }
3213 
3214  bool
3215  contains(_CharT __x) const noexcept
3216  { return __sv_type(this->data(), this->size()).contains(__x); }
3217 
3218  [[__gnu__::__nonnull__]]
3219  bool
3220  contains(const _CharT* __x) const noexcept
3221  { return __sv_type(this->data(), this->size()).contains(__x); }
3222 #endif // C++23
3223 
3224 # ifdef _GLIBCXX_TM_TS_INTERNAL
3225  friend void
3226  ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
3227  void* exc);
3228  friend const char*
3229  ::_txnal_cow_string_c_str(const void *that);
3230  friend void
3231  ::_txnal_cow_string_D1(void *that);
3232  friend void
3233  ::_txnal_cow_string_D1_commit(void *that);
3234 # endif
3235  };
3236 
3237  template<typename _CharT, typename _Traits, typename _Alloc>
3238  const typename basic_string<_CharT, _Traits, _Alloc>::size_type
3239  basic_string<_CharT, _Traits, _Alloc>::
3240  _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
3241 
3242  template<typename _CharT, typename _Traits, typename _Alloc>
3243  const _CharT
3244  basic_string<_CharT, _Traits, _Alloc>::
3245  _Rep::_S_terminal = _CharT();
3246 
3247  template<typename _CharT, typename _Traits, typename _Alloc>
3248  const typename basic_string<_CharT, _Traits, _Alloc>::size_type
3250 
3251  // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
3252  // at static init time (before static ctors are run).
3253  template<typename _CharT, typename _Traits, typename _Alloc>
3254  typename basic_string<_CharT, _Traits, _Alloc>::size_type
3255  basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
3256  (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
3257  sizeof(size_type)];
3258 
3259  // NB: This is the special case for Input Iterators, used in
3260  // istreambuf_iterators, etc.
3261  // Input Iterators have a cost structure very different from
3262  // pointers, calling for a different coding style.
3263  template<typename _CharT, typename _Traits, typename _Alloc>
3264  template<typename _InIterator>
3265  _CharT*
3266  basic_string<_CharT, _Traits, _Alloc>::
3267  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
3268  input_iterator_tag)
3269  {
3270 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3271  if (__beg == __end && __a == _Alloc())
3272  return _S_empty_rep()._M_refdata();
3273 #endif
3274  // Avoid reallocation for common case.
3275  _CharT __buf[128];
3276  size_type __len = 0;
3277  while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
3278  {
3279  __buf[__len++] = *__beg;
3280  ++__beg;
3281  }
3282  _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
3283  _M_copy(__r->_M_refdata(), __buf, __len);
3284  __try
3285  {
3286  while (__beg != __end)
3287  {
3288  if (__len == __r->_M_capacity)
3289  {
3290  // Allocate more space.
3291  _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
3292  _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
3293  __r->_M_destroy(__a);
3294  __r = __another;
3295  }
3296  __r->_M_refdata()[__len++] = *__beg;
3297  ++__beg;
3298  }
3299  }
3300  __catch(...)
3301  {
3302  __r->_M_destroy(__a);
3303  __throw_exception_again;
3304  }
3305  __r->_M_set_length_and_sharable(__len);
3306  return __r->_M_refdata();
3307  }
3308 
3309  template<typename _CharT, typename _Traits, typename _Alloc>
3310  template <typename _InIterator>
3311  _CharT*
3312  basic_string<_CharT, _Traits, _Alloc>::
3313  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
3314  forward_iterator_tag)
3315  {
3316 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3317  if (__beg == __end && __a == _Alloc())
3318  return _S_empty_rep()._M_refdata();
3319 #endif
3320  // NB: Not required, but considered best practice.
3321  if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
3322  __throw_logic_error(__N("basic_string::_S_construct null not valid"));
3323 
3324  const size_type __dnew = static_cast<size_type>(std::distance(__beg,
3325  __end));
3326  // Check for out_of_range and length_error exceptions.
3327  _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
3328  __try
3329  { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
3330  __catch(...)
3331  {
3332  __r->_M_destroy(__a);
3333  __throw_exception_again;
3334  }
3335  __r->_M_set_length_and_sharable(__dnew);
3336  return __r->_M_refdata();
3337  }
3338 
3339  template<typename _CharT, typename _Traits, typename _Alloc>
3340  _CharT*
3341  basic_string<_CharT, _Traits, _Alloc>::
3342  _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
3343  {
3344 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3345  if (__n == 0 && __a == _Alloc())
3346  return _S_empty_rep()._M_refdata();
3347 #endif
3348  // Check for out_of_range and length_error exceptions.
3349  _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
3350  if (__n)
3351  _M_assign(__r->_M_refdata(), __n, __c);
3352 
3353  __r->_M_set_length_and_sharable(__n);
3354  return __r->_M_refdata();
3355  }
3356 
3357  template<typename _CharT, typename _Traits, typename _Alloc>
3359  basic_string(const basic_string& __str, size_type __pos, const _Alloc& __a)
3360  : _M_dataplus(_S_construct(__str._M_data()
3361  + __str._M_check(__pos,
3362  "basic_string::basic_string"),
3363  __str._M_data() + __str._M_limit(__pos, npos)
3364  + __pos, __a), __a)
3365  { }
3366 
3367  template<typename _CharT, typename _Traits, typename _Alloc>
3369  basic_string(const basic_string& __str, size_type __pos, size_type __n)
3370  : _M_dataplus(_S_construct(__str._M_data()
3371  + __str._M_check(__pos,
3372  "basic_string::basic_string"),
3373  __str._M_data() + __str._M_limit(__pos, __n)
3374  + __pos, _Alloc()), _Alloc())
3375  { }
3376 
3377  template<typename _CharT, typename _Traits, typename _Alloc>
3379  basic_string(const basic_string& __str, size_type __pos,
3380  size_type __n, const _Alloc& __a)
3381  : _M_dataplus(_S_construct(__str._M_data()
3382  + __str._M_check(__pos,
3383  "basic_string::basic_string"),
3384  __str._M_data() + __str._M_limit(__pos, __n)
3385  + __pos, __a), __a)
3386  { }
3387 
3388  template<typename _CharT, typename _Traits, typename _Alloc>
3391  assign(const basic_string& __str)
3392  {
3393  if (_M_rep() != __str._M_rep())
3394  {
3395  // XXX MT
3396  const allocator_type __a = this->get_allocator();
3397  _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
3398  _M_rep()->_M_dispose(__a);
3399  _M_data(__tmp);
3400  }
3401  return *this;
3402  }
3403 
3404  template<typename _CharT, typename _Traits, typename _Alloc>
3407  assign(const _CharT* __s, size_type __n)
3408  {
3409  __glibcxx_requires_string_len(__s, __n);
3410  _M_check_length(this->size(), __n, "basic_string::assign");
3411  if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
3412  return _M_replace_safe(size_type(0), this->size(), __s, __n);
3413  else
3414  {
3415  // Work in-place.
3416  const size_type __pos = __s - _M_data();
3417  if (__pos >= __n)
3418  _M_copy(_M_data(), __s, __n);
3419  else if (__pos)
3420  _M_move(_M_data(), __s, __n);
3421  _M_rep()->_M_set_length_and_sharable(__n);
3422  return *this;
3423  }
3424  }
3425 
3426  template<typename _CharT, typename _Traits, typename _Alloc>
3429  append(size_type __n, _CharT __c)
3430  {
3431  if (__n)
3432  {
3433  _M_check_length(size_type(0), __n, "basic_string::append");
3434  const size_type __len = __n + this->size();
3435  if (__len > this->capacity() || _M_rep()->_M_is_shared())
3436  this->reserve(__len);
3437  _M_assign(_M_data() + this->size(), __n, __c);
3438  _M_rep()->_M_set_length_and_sharable(__len);
3439  }
3440  return *this;
3441  }
3442 
3443  template<typename _CharT, typename _Traits, typename _Alloc>
3446  append(const _CharT* __s, size_type __n)
3447  {
3448  __glibcxx_requires_string_len(__s, __n);
3449  if (__n)
3450  {
3451  _M_check_length(size_type(0), __n, "basic_string::append");
3452  const size_type __len = __n + this->size();
3453  if (__len > this->capacity() || _M_rep()->_M_is_shared())
3454  {
3455  if (_M_disjunct(__s))
3456  this->reserve(__len);
3457  else
3458  {
3459  const size_type __off = __s - _M_data();
3460  this->reserve(__len);
3461  __s = _M_data() + __off;
3462  }
3463  }
3464  _M_copy(_M_data() + this->size(), __s, __n);
3465  _M_rep()->_M_set_length_and_sharable(__len);
3466  }
3467  return *this;
3468  }
3469 
3470  template<typename _CharT, typename _Traits, typename _Alloc>
3473  append(const basic_string& __str)
3474  {
3475  const size_type __size = __str.size();
3476  if (__size)
3477  {
3478  const size_type __len = __size + this->size();
3479  if (__len > this->capacity() || _M_rep()->_M_is_shared())
3480  this->reserve(__len);
3481  _M_copy(_M_data() + this->size(), __str._M_data(), __size);
3482  _M_rep()->_M_set_length_and_sharable(__len);
3483  }
3484  return *this;
3485  }
3486 
3487  template<typename _CharT, typename _Traits, typename _Alloc>
3490  append(const basic_string& __str, size_type __pos, size_type __n)
3491  {
3492  __str._M_check(__pos, "basic_string::append");
3493  __n = __str._M_limit(__pos, __n);
3494  if (__n)
3495  {
3496  const size_type __len = __n + this->size();
3497  if (__len > this->capacity() || _M_rep()->_M_is_shared())
3498  this->reserve(__len);
3499  _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
3500  _M_rep()->_M_set_length_and_sharable(__len);
3501  }
3502  return *this;
3503  }
3504 
3505  template<typename _CharT, typename _Traits, typename _Alloc>
3508  insert(size_type __pos, const _CharT* __s, size_type __n)
3509  {
3510  __glibcxx_requires_string_len(__s, __n);
3511  _M_check(__pos, "basic_string::insert");
3512  _M_check_length(size_type(0), __n, "basic_string::insert");
3513  if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
3514  return _M_replace_safe(__pos, size_type(0), __s, __n);
3515  else
3516  {
3517  // Work in-place.
3518  const size_type __off = __s - _M_data();
3519  _M_mutate(__pos, 0, __n);
3520  __s = _M_data() + __off;
3521  _CharT* __p = _M_data() + __pos;
3522  if (__s + __n <= __p)
3523  _M_copy(__p, __s, __n);
3524  else if (__s >= __p)
3525  _M_copy(__p, __s + __n, __n);
3526  else
3527  {
3528  const size_type __nleft = __p - __s;
3529  _M_copy(__p, __s, __nleft);
3530  _M_copy(__p + __nleft, __p + __n, __n - __nleft);
3531  }
3532  return *this;
3533  }
3534  }
3535 
3536  template<typename _CharT, typename _Traits, typename _Alloc>
3537  typename basic_string<_CharT, _Traits, _Alloc>::iterator
3539  erase(iterator __first, iterator __last)
3540  {
3541  _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
3542  && __last <= _M_iend());
3543 
3544  // NB: This isn't just an optimization (bail out early when
3545  // there is nothing to do, really), it's also a correctness
3546  // issue vs MT, see libstdc++/40518.
3547  const size_type __size = __last - __first;
3548  if (__size)
3549  {
3550  const size_type __pos = __first - _M_ibegin();
3551  _M_mutate(__pos, __size, size_type(0));
3552  _M_rep()->_M_set_leaked();
3553  return iterator(_M_data() + __pos);
3554  }
3555  else
3556  return __first;
3557  }
3558 
3559  template<typename _CharT, typename _Traits, typename _Alloc>
3562  replace(size_type __pos, size_type __n1, const _CharT* __s,
3563  size_type __n2)
3564  {
3565  __glibcxx_requires_string_len(__s, __n2);
3566  _M_check(__pos, "basic_string::replace");
3567  __n1 = _M_limit(__pos, __n1);
3568  _M_check_length(__n1, __n2, "basic_string::replace");
3569  bool __left;
3570  if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
3571  return _M_replace_safe(__pos, __n1, __s, __n2);
3572  else if ((__left = __s + __n2 <= _M_data() + __pos)
3573  || _M_data() + __pos + __n1 <= __s)
3574  {
3575  // Work in-place: non-overlapping case.
3576  size_type __off = __s - _M_data();
3577  __left ? __off : (__off += __n2 - __n1);
3578  _M_mutate(__pos, __n1, __n2);
3579  _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
3580  return *this;
3581  }
3582  else
3583  {
3584  // TODO: overlapping case.
3585  const basic_string __tmp(__s, __n2);
3586  return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
3587  }
3588  }
3589 
3590  template<typename _CharT, typename _Traits, typename _Alloc>
3591  void
3593  _M_destroy(const _Alloc& __a) throw ()
3594  {
3595  const size_type __size = sizeof(_Rep_base)
3596  + (this->_M_capacity + 1) * sizeof(_CharT);
3597  _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
3598  }
3599 
3600  template<typename _CharT, typename _Traits, typename _Alloc>
3601  void
3602  basic_string<_CharT, _Traits, _Alloc>::
3603  _M_leak_hard()
3604  {
3605  // No need to create a new copy of an empty string when a non-const
3606  // reference/pointer/iterator into it is obtained. Modifying the
3607  // trailing null character is undefined, so the ref/pointer/iterator
3608  // is effectively const anyway.
3609  if (this->empty())
3610  return;
3611 
3612  if (_M_rep()->_M_is_shared())
3613  _M_mutate(0, 0, 0);
3614  _M_rep()->_M_set_leaked();
3615  }
3616 
3617  template<typename _CharT, typename _Traits, typename _Alloc>
3618  void
3619  basic_string<_CharT, _Traits, _Alloc>::
3620  _M_mutate(size_type __pos, size_type __len1, size_type __len2)
3621  {
3622  const size_type __old_size = this->size();
3623  const size_type __new_size = __old_size + __len2 - __len1;
3624  const size_type __how_much = __old_size - __pos - __len1;
3625 
3626  if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
3627  {
3628  // Must reallocate.
3629  const allocator_type __a = get_allocator();
3630  _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
3631 
3632  if (__pos)
3633  _M_copy(__r->_M_refdata(), _M_data(), __pos);
3634  if (__how_much)
3635  _M_copy(__r->_M_refdata() + __pos + __len2,
3636  _M_data() + __pos + __len1, __how_much);
3637 
3638  _M_rep()->_M_dispose(__a);
3639  _M_data(__r->_M_refdata());
3640  }
3641  else if (__how_much && __len1 != __len2)
3642  {
3643  // Work in-place.
3644  _M_move(_M_data() + __pos + __len2,
3645  _M_data() + __pos + __len1, __how_much);
3646  }
3647  _M_rep()->_M_set_length_and_sharable(__new_size);
3648  }
3649 
3650  template<typename _CharT, typename _Traits, typename _Alloc>
3651  void
3653  reserve(size_type __res)
3654  {
3655  const size_type __capacity = capacity();
3656 
3657  // _GLIBCXX_RESOLVE_LIB_DEFECTS
3658  // 2968. Inconsistencies between basic_string reserve and
3659  // vector/unordered_map/unordered_set reserve functions
3660  // P0966 reserve should not shrink
3661  if (__res <= __capacity)
3662  {
3663  if (!_M_rep()->_M_is_shared())
3664  return;
3665 
3666  // unshare, but keep same capacity
3667  __res = __capacity;
3668  }
3669 
3670  const allocator_type __a = get_allocator();
3671  _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
3672  _M_rep()->_M_dispose(__a);
3673  _M_data(__tmp);
3674  }
3675 
3676  template<typename _CharT, typename _Traits, typename _Alloc>
3677  void
3679  swap(basic_string& __s)
3681  {
3682  if (_M_rep()->_M_is_leaked())
3683  _M_rep()->_M_set_sharable();
3684  if (__s._M_rep()->_M_is_leaked())
3685  __s._M_rep()->_M_set_sharable();
3686  if (this->get_allocator() == __s.get_allocator())
3687  {
3688  _CharT* __tmp = _M_data();
3689  _M_data(__s._M_data());
3690  __s._M_data(__tmp);
3691  }
3692  // The code below can usually be optimized away.
3693  else
3694  {
3695  const basic_string __tmp1(_M_ibegin(), _M_iend(),
3696  __s.get_allocator());
3697  const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
3698  this->get_allocator());
3699  *this = __tmp2;
3700  __s = __tmp1;
3701  }
3702  }
3703 
3704  template<typename _CharT, typename _Traits, typename _Alloc>
3707  _S_create(size_type __capacity, size_type __old_capacity,
3708  const _Alloc& __alloc)
3709  {
3710  // _GLIBCXX_RESOLVE_LIB_DEFECTS
3711  // 83. String::npos vs. string::max_size()
3712  if (__capacity > _S_max_size)
3713  __throw_length_error(__N("basic_string::_S_create"));
3714 
3715  // The standard places no restriction on allocating more memory
3716  // than is strictly needed within this layer at the moment or as
3717  // requested by an explicit application call to reserve(n).
3718 
3719  // Many malloc implementations perform quite poorly when an
3720  // application attempts to allocate memory in a stepwise fashion
3721  // growing each allocation size by only 1 char. Additionally,
3722  // it makes little sense to allocate less linear memory than the
3723  // natural blocking size of the malloc implementation.
3724  // Unfortunately, we would need a somewhat low-level calculation
3725  // with tuned parameters to get this perfect for any particular
3726  // malloc implementation. Fortunately, generalizations about
3727  // common features seen among implementations seems to suffice.
3728 
3729  // __pagesize need not match the actual VM page size for good
3730  // results in practice, thus we pick a common value on the low
3731  // side. __malloc_header_size is an estimate of the amount of
3732  // overhead per memory allocation (in practice seen N * sizeof
3733  // (void*) where N is 0, 2 or 4). According to folklore,
3734  // picking this value on the high side is better than
3735  // low-balling it (especially when this algorithm is used with
3736  // malloc implementations that allocate memory blocks rounded up
3737  // to a size which is a power of 2).
3738  const size_type __pagesize = 4096;
3739  const size_type __malloc_header_size = 4 * sizeof(void*);
3740 
3741  // The below implements an exponential growth policy, necessary to
3742  // meet amortized linear time requirements of the library: see
3743  // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
3744  // It's active for allocations requiring an amount of memory above
3745  // system pagesize. This is consistent with the requirements of the
3746  // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
3747  if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
3748  __capacity = 2 * __old_capacity;
3749 
3750  // NB: Need an array of char_type[__capacity], plus a terminating
3751  // null char_type() element, plus enough for the _Rep data structure.
3752  // Whew. Seemingly so needy, yet so elemental.
3753  size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
3754 
3755  const size_type __adj_size = __size + __malloc_header_size;
3756  if (__adj_size > __pagesize && __capacity > __old_capacity)
3757  {
3758  const size_type __extra = __pagesize - __adj_size % __pagesize;
3759  __capacity += __extra / sizeof(_CharT);
3760  // Never allocate a string bigger than _S_max_size.
3761  if (__capacity > _S_max_size)
3762  __capacity = _S_max_size;
3763  __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
3764  }
3765 
3766  // NB: Might throw, but no worries about a leak, mate: _Rep()
3767  // does not throw.
3768  void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
3769  _Rep *__p = new (__place) _Rep;
3770  __p->_M_capacity = __capacity;
3771  // ABI compatibility - 3.4.x set in _S_create both
3772  // _M_refcount and _M_length. All callers of _S_create
3773  // in basic_string.tcc then set just _M_length.
3774  // In 4.0.x and later both _M_refcount and _M_length
3775  // are initialized in the callers, unfortunately we can
3776  // have 3.4.x compiled code with _S_create callers inlined
3777  // calling 4.0.x+ _S_create.
3778  __p->_M_set_sharable();
3779  return __p;
3780  }
3781 
3782  template<typename _CharT, typename _Traits, typename _Alloc>
3783  _CharT*
3784  basic_string<_CharT, _Traits, _Alloc>::_Rep::
3785  _M_clone(const _Alloc& __alloc, size_type __res)
3786  {
3787  // Requested capacity of the clone.
3788  const size_type __requested_cap = this->_M_length + __res;
3789  _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
3790  __alloc);
3791  if (this->_M_length)
3792  _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
3793 
3794  __r->_M_set_length_and_sharable(this->_M_length);
3795  return __r->_M_refdata();
3796  }
3797 
3798  template<typename _CharT, typename _Traits, typename _Alloc>
3799  void
3801  resize(size_type __n, _CharT __c)
3802  {
3803  const size_type __size = this->size();
3804  _M_check_length(__size, __n, "basic_string::resize");
3805  if (__size < __n)
3806  this->append(__n - __size, __c);
3807  else if (__n < __size)
3808  this->erase(__n);
3809  // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
3810  }
3811 
3812  template<typename _CharT, typename _Traits, typename _Alloc>
3813  template<typename _InputIterator>
3816  _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
3817  _InputIterator __k2, __false_type)
3818  {
3819  const basic_string __s(__k1, __k2);
3820  const size_type __n1 = __i2 - __i1;
3821  _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
3822  return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
3823  __s.size());
3824  }
3825 
3826  template<typename _CharT, typename _Traits, typename _Alloc>
3827  basic_string<_CharT, _Traits, _Alloc>&
3828  basic_string<_CharT, _Traits, _Alloc>::
3829  _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
3830  _CharT __c)
3831  {
3832  _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
3833  _M_mutate(__pos1, __n1, __n2);
3834  if (__n2)
3835  _M_assign(_M_data() + __pos1, __n2, __c);
3836  return *this;
3837  }
3838 
3839  template<typename _CharT, typename _Traits, typename _Alloc>
3840  basic_string<_CharT, _Traits, _Alloc>&
3841  basic_string<_CharT, _Traits, _Alloc>::
3842  _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
3843  size_type __n2)
3844  {
3845  _M_mutate(__pos1, __n1, __n2);
3846  if (__n2)
3847  _M_copy(_M_data() + __pos1, __s, __n2);
3848  return *this;
3849  }
3850 
3851  template<typename _CharT, typename _Traits, typename _Alloc>
3852  void
3854  reserve()
3855  {
3856 #if __cpp_exceptions
3857  if (length() < capacity() || _M_rep()->_M_is_shared())
3858  try
3859  {
3860  const allocator_type __a = get_allocator();
3861  _CharT* __tmp = _M_rep()->_M_clone(__a);
3862  _M_rep()->_M_dispose(__a);
3863  _M_data(__tmp);
3864  }
3865  catch (const __cxxabiv1::__forced_unwind&)
3866  { throw; }
3867  catch (...)
3868  { /* swallow the exception */ }
3869 #endif
3870  }
3871 
3872  template<typename _CharT, typename _Traits, typename _Alloc>
3873  typename basic_string<_CharT, _Traits, _Alloc>::size_type
3875  copy(_CharT* __s, size_type __n, size_type __pos) const
3876  {
3877  _M_check(__pos, "basic_string::copy");
3878  __n = _M_limit(__pos, __n);
3879  __glibcxx_requires_string_len(__s, __n);
3880  if (__n)
3881  _M_copy(__s, _M_data() + __pos, __n);
3882  // 21.3.5.7 par 3: do not append null. (good.)
3883  return __n;
3884  }
3885 
3886 #ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
3887  template<typename _CharT, typename _Traits, typename _Alloc>
3888  template<typename _Operation>
3889  [[__gnu__::__always_inline__]]
3890  inline void
3892  __resize_and_overwrite(const size_type __n, _Operation __op)
3893  { resize_and_overwrite<_Operation&>(__n, __op); }
3894 #endif
3895 
3896 #if __cplusplus >= 201103L
3897  template<typename _CharT, typename _Traits, typename _Alloc>
3898  template<typename _Operation>
3899  void
3900  basic_string<_CharT, _Traits, _Alloc>::
3901 #ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
3902  resize_and_overwrite(const size_type __n, _Operation __op)
3903 #else
3904  __resize_and_overwrite(const size_type __n, _Operation __op)
3905 #endif
3906  {
3907  const size_type __capacity = capacity();
3908  _CharT* __p;
3909  if (__n > __capacity || _M_rep()->_M_is_shared())
3910  this->reserve(__n);
3911  __p = _M_data();
3912  struct _Terminator {
3913  ~_Terminator() { _M_this->_M_rep()->_M_set_length_and_sharable(_M_r); }
3914  basic_string* _M_this;
3915  size_type _M_r;
3916  };
3917  _Terminator __term{this, 0};
3918  auto __r = std::move(__op)(__p + 0, __n + 0);
3919 #ifdef __cpp_lib_concepts
3920  static_assert(ranges::__detail::__is_integer_like<decltype(__r)>);
3921 #else
3922  static_assert(__gnu_cxx::__is_integer_nonstrict<decltype(__r)>::__value,
3923  "resize_and_overwrite operation must return an integer");
3924 #endif
3925  _GLIBCXX_DEBUG_ASSERT(__r >= 0 && size_type(__r) <= __n);
3926  __term._M_r = size_type(__r);
3927  if (__term._M_r > __n)
3928  __builtin_unreachable();
3929  }
3930 #endif // C++11
3931 
3932 
3933 _GLIBCXX_END_NAMESPACE_VERSION
3934 } // namespace std
3935 #endif // ! _GLIBCXX_USE_CXX11_ABI
3936 #endif // _COW_STRING_H
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition: type_traits:2837
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:138
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
Definition: valarray:1251
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition: valarray:1229
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:234
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
Definition: range_access.h:294
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition: range_access.h:274
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
Definition: range_access.h:324
initializer_list
is_same
Definition: type_traits:1540
Uniform interface to all allocator types.
Managing sequences of characters and character-like objects.
Definition: cow_string.h:109
int compare(size_type __pos1, size_type __n1, const basic_string &__str, size_type __pos2, size_type __n2=npos) const
Compare substring to a substring.
Definition: cow_string.h:3066
const_reverse_iterator crbegin() const noexcept
Definition: cow_string.h:936
void swap(basic_string &__s) noexcept(/*conditional */)
Swap contents with another string.
Definition: cow_string.h:3679
basic_string & assign(size_type __n, _CharT __c)
Set value to multiple characters.
Definition: cow_string.h:1530
basic_string & append(const basic_string &__str, size_type __pos, size_type __n=npos)
Append a substring.
Definition: cow_string.h:3490
void push_back(_CharT __c)
Append a single character.
Definition: cow_string.h:1437
const_iterator cend() const noexcept
Definition: cow_string.h:927
size_type find(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a C string.
Definition: cow_string.h:2460
basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc &__a=_Alloc())
Construct string as copy of a range.
Definition: cow_string.h:725
basic_string & replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
Replace characters with multiple characters.
Definition: cow_string.h:2019
basic_string & replace(iterator __i1, iterator __i2, const _CharT *__s)
Replace range of characters with C string.
Definition: cow_string.h:2077
size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
Definition: cow_string.h:2586
basic_string & append(_InputIterator __first, _InputIterator __last)
Append a range of characters.
Definition: cow_string.h:1396
basic_string & operator=(initializer_list< _CharT > __l)
Set value to string constructed from initializer list.
Definition: cow_string.h:813
iterator erase(iterator __first, iterator __last)
Remove a range of characters.
Definition: cow_string.h:3539
basic_string(const _Alloc &__a)
Construct an empty string using allocator a.
Definition: cow_string.h:533
const _CharT * c_str() const noexcept
Return const pointer to null-terminated contents.
Definition: cow_string.h:2376
basic_string & assign(const _CharT *__s)
Set value to contents of a C string.
Definition: cow_string.h:1514
size_type find_last_of(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a character of C string.
Definition: cow_string.h:2701
int compare(const _CharT *__s) const noexcept
Compare to a C string.
Definition: cow_string.h:3096
void insert(iterator __p, initializer_list< _CharT > __l)
Insert an initializer_list of characters.
Definition: cow_string.h:1674
void __resize_and_overwrite(size_type __n, _Operation __op)
Non-standard version of resize_and_overwrite for C++11 and above.
size_type rfind(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a C string.
Definition: cow_string.h:2538
basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
Definition: cow_string.h:2918
iterator erase(iterator __position)
Remove one character.
Definition: cow_string.h:1874
size_type find(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a C substring.
size_type find(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a string.
Definition: cow_string.h:2445
basic_string & assign(const _CharT *__s, size_type __n)
Set value to a C substring.
Definition: cow_string.h:3407
size_type find_last_not_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character not in string.
Definition: cow_string.h:2835
int compare(size_type __pos, size_type __n, const basic_string &__str) const
Compare substring to a string.
Definition: cow_string.h:3030
basic_string(const basic_string &__str)
Construct string with copy of value of str.
Definition: cow_string.h:542
basic_string & replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
Replace range of characters with multiple characters.
Definition: cow_string.h:2098
int compare(const basic_string &__str) const
Compare to a string.
Definition: cow_string.h:2937
int compare(size_type __pos, size_type __n1, const _CharT *__s) const
Compare substring to a C string.
Definition: cow_string.h:3130
int compare(size_type __pos, size_type __n1, const _CharT *__s, size_type __n2) const
Compare substring against a character array.
Definition: cow_string.h:3168
reverse_iterator rend()
Definition: cow_string.h:901
basic_string & operator=(const _CharT *__s)
Copy contents of s into this string.
Definition: cow_string.h:774
basic_string(const basic_string &__str, size_type __pos, const _Alloc &__a=_Alloc())
Construct string as copy of a substring.
Definition: cow_string.h:3359
basic_string(const basic_string &__str, size_type __pos, size_type __n)
Construct string as copy of a substring.
Definition: cow_string.h:3369
size_type find_first_not_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character not in string.
Definition: cow_string.h:2753
basic_string & replace(size_type __pos, size_type __n1, const _CharT *__s)
Replace characters with value of a C string.
Definition: cow_string.h:1995
const_reference front() const noexcept
Definition: cow_string.h:1220
size_type find_last_not_of(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a character not in C string.
Definition: cow_string.h:2866
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
Definition: cow_string.h:1619
basic_string & operator+=(const basic_string &__str)
Append a string to this string.
Definition: cow_string.h:1256
basic_string & assign(const basic_string &__str)
Set value to contents of another string.
Definition: cow_string.h:3391
basic_string & append(size_type __n, _CharT __c)
Append multiple characters.
Definition: cow_string.h:3429
reverse_iterator rbegin()
Definition: cow_string.h:883
basic_string & operator+=(initializer_list< _CharT > __l)
Append an initializer_list of characters.
Definition: cow_string.h:1287
basic_string(initializer_list< _CharT > __l, const _Alloc &__a=_Alloc())
Construct string from an initializer list.
Definition: cow_string.h:689
basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
Definition: cow_string.h:1928
reference front()
Definition: cow_string.h:1209
basic_string(const basic_string &__str, size_type __pos, size_type __n, const _Alloc &__a)
Construct string as copy of a substring.
Definition: cow_string.h:3379
basic_string & replace(size_type __pos, size_type __n1, const _CharT *__s, size_type __n2)
Replace characters with value of a C substring.
Definition: cow_string.h:3562
basic_string & assign(_InputIterator __first, _InputIterator __last)
Set value to a range of characters.
Definition: cow_string.h:1543
void pop_back()
Remove the last character.
Definition: cow_string.h:1903
basic_string & insert(size_type __pos1, const basic_string &__str)
Insert value of a string.
Definition: cow_string.h:1694
basic_string(basic_string &&__str) noexcept
Move construct string.
Definition: cow_string.h:624
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
Definition: cow_string.h:3875
size_type length() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition: cow_string.h:967
size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
Definition: cow_string.h:2670
basic_string & replace(iterator __i1, iterator __i2, initializer_list< _CharT > __l)
Replace range of characters with initializer_list.
Definition: cow_string.h:2212
basic_string & insert(size_type __pos, const _CharT *__s, size_type __n)
Insert a C substring.
Definition: cow_string.h:3508
basic_string & insert(size_type __pos1, const basic_string &__str, size_type __pos2, size_type __n=npos)
Insert a substring.
Definition: cow_string.h:1716
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition: cow_string.h:955
_CharT * data() noexcept(false)
Return non-const pointer to contents.
Definition: cow_string.h:2404
size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
Definition: cow_string.h:2507
basic_string & assign(initializer_list< _CharT > __l)
Set value to an initializer_list of characters.
Definition: cow_string.h:1569
basic_string(size_type __n, _CharT __c, const _Alloc &__a=_Alloc())
Construct string as multiple characters.
Definition: cow_string.h:612
void shrink_to_fit() noexcept
A non-binding request to reduce capacity() to size().
Definition: cow_string.h:1007
basic_string & replace(iterator __i1, iterator __i2, const _CharT *__s, size_type __n)
Replace range of characters with C substring.
Definition: cow_string.h:2056
basic_string & assign(basic_string &&__str) noexcept(allocator_traits< _Alloc >::is_always_equal::value)
Set value to contents of another string.
Definition: cow_string.h:1464
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
Definition: cow_string.h:3801
void reserve()
Equivalent to shrink_to_fit().
Definition: cow_string.h:3854
const _CharT * data() const noexcept
Return const pointer to contents.
Definition: cow_string.h:2388
basic_string & operator=(_CharT __c)
Set value to string of length 1.
Definition: cow_string.h:785
const_reference at(size_type __n) const
Provides access to the data contained in the string.
Definition: cow_string.h:1170
const_reference back() const noexcept
Definition: cow_string.h:1242
const_reverse_iterator rend() const noexcept
Definition: cow_string.h:910
const_iterator end() const noexcept
Definition: cow_string.h:874
basic_string & operator+=(_CharT __c)
Append a character.
Definition: cow_string.h:1274
size_type find_last_of(_CharT __c, size_type __pos=npos) const noexcept
Find last position of a character.
Definition: cow_string.h:2721
iterator begin()
Definition: cow_string.h:844
basic_string & append(const basic_string &__str)
Append a string to this string.
Definition: cow_string.h:3473
const_iterator begin() const noexcept
Definition: cow_string.h:855
const_reverse_iterator crend() const noexcept
Definition: cow_string.h:945
void resize(size_type __n)
Resizes the string to the specified number of characters.
Definition: cow_string.h:999
const_reverse_iterator rbegin() const noexcept
Definition: cow_string.h:892
iterator end()
Definition: cow_string.h:863
basic_string & operator=(const basic_string &__str)
Assign the value of str to this string.
Definition: cow_string.h:766
const_reference operator[](size_type __pos) const noexcept
Subscript access to the data contained in the string.
Definition: cow_string.h:1131
basic_string(const _CharT *__s, const _Alloc &__a=_Alloc())
Construct string as copy of a C string.
Definition: cow_string.h:601
basic_string & operator=(basic_string &&__str) noexcept(/*conditional */)
Move assign the value of str to this string.
Definition: cow_string.h:800
void clear() noexcept
Definition: cow_string.h:1094
size_type find_first_of(_CharT __c, size_type __pos=0) const noexcept
Find position of a character.
Definition: cow_string.h:2637
basic_string & replace(iterator __i1, iterator __i2, _InputIterator __k1, _InputIterator __k2)
Replace range of characters with range.
Definition: cow_string.h:2122
bool empty() const noexcept
Definition: cow_string.h:1116
basic_string & append(initializer_list< _CharT > __l)
Append an initializer_list of characters.
Definition: cow_string.h:1382
reference back()
Definition: cow_string.h:1231
static const size_type npos
Value returned by various member functions when they fail.
Definition: cow_string.h:322
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
Definition: cow_string.h:2415
basic_string & insert(size_type __pos, const _CharT *__s)
Insert a C string.
Definition: cow_string.h:1757
const_iterator cbegin() const noexcept
Definition: cow_string.h:919
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
Definition: cow_string.h:1858
basic_string & replace(iterator __i1, iterator __i2, const basic_string &__str)
Replace range of characters with string.
Definition: cow_string.h:2037
~basic_string() noexcept
Destroy the string instance.
Definition: cow_string.h:758
size_type capacity() const noexcept
Definition: cow_string.h:1059
basic_string & operator+=(const _CharT *__s)
Append a C string.
Definition: cow_string.h:1265
basic_string() noexcept
Default constructor creates an empty string.
Definition: cow_string.h:515
void insert(iterator __p, _InputIterator __beg, _InputIterator __end)
Insert a range of characters.
Definition: cow_string.h:1636
size_type find_first_of(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a character of C string.
Definition: cow_string.h:2617
basic_string & replace(size_type __pos1, size_type __n1, const basic_string &__str, size_type __pos2, size_type __n2=npos)
Replace characters with value from another string.
Definition: cow_string.h:1950
size_type max_size() const noexcept
Returns the size() of the largest possible string.
Definition: cow_string.h:972
reference operator[](size_type __pos)
Subscript access to the data contained in the string.
Definition: cow_string.h:1148
basic_string & insert(size_type __pos, size_type __n, _CharT __c)
Insert multiple characters.
Definition: cow_string.h:1780
basic_string & append(const _CharT *__s, size_type __n)
Append a C substring.
Definition: cow_string.h:3446
basic_string(const _CharT *__s, size_type __n, const _Alloc &__a=_Alloc())
Construct string initialized by a character array.
Definition: cow_string.h:586
basic_string & assign(const basic_string &__str, size_type __pos, size_type __n=npos)
Set value to a substring of a string.
Definition: cow_string.h:1486
basic_string & append(const _CharT *__s)
Append a C string.
Definition: cow_string.h:1342
size_type find_first_not_of(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a character not in C string.
Definition: cow_string.h:2784
reference at(size_type __n)
Provides access to the data contained in the string.
Definition: cow_string.h:1192
iterator insert(iterator __p, _CharT __c)
Insert one character.
Definition: cow_string.h:1798
Thrown as part of forced unwinding.
Definition: cxxabi_forced.h:51
Common iterator class.
Uniform interface to C++98 and C++11 allocators.