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