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