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