libstdc++
basic_string.h
Go to the documentation of this file.
1 // Components for manipulating sequences of characters -*- C++ -*-
2 
3 // Copyright (C) 1997-2017 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file bits/basic_string.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{string}
28  */
29 
30 //
31 // ISO C++ 14882: 21 Strings library
32 //
33 
34 #ifndef _BASIC_STRING_H
35 #define _BASIC_STRING_H 1
36 
37 #pragma GCC system_header
38 
39 #include <ext/atomicity.h>
40 #include <ext/alloc_traits.h>
41 #include <debug/debug.h>
42 
43 #if __cplusplus >= 201103L
44 #include <initializer_list>
45 #endif
46 
47 #if __cplusplus > 201402L
48 # include <string_view>
49 #endif
50 
51 
52 namespace std _GLIBCXX_VISIBILITY(default)
53 {
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 
56 #if _GLIBCXX_USE_CXX11_ABI
57 _GLIBCXX_BEGIN_NAMESPACE_CXX11
58  /**
59  * @class basic_string basic_string.h <string>
60  * @brief Managing sequences of characters and character-like objects.
61  *
62  * @ingroup strings
63  * @ingroup sequences
64  *
65  * @tparam _CharT Type of character
66  * @tparam _Traits Traits for character type, defaults to
67  * char_traits<_CharT>.
68  * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
69  *
70  * Meets the requirements of a <a href="tables.html#65">container</a>, a
71  * <a href="tables.html#66">reversible container</a>, and a
72  * <a href="tables.html#67">sequence</a>. Of the
73  * <a href="tables.html#68">optional sequence requirements</a>, only
74  * @c push_back, @c at, and @c %array access are supported.
75  */
76  template<typename _CharT, typename _Traits, typename _Alloc>
77  class basic_string
78  {
80  rebind<_CharT>::other _Char_alloc_type;
81  typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
82 
83  // Types:
84  public:
85  typedef _Traits traits_type;
86  typedef typename _Traits::char_type value_type;
87  typedef _Char_alloc_type allocator_type;
88  typedef typename _Alloc_traits::size_type size_type;
89  typedef typename _Alloc_traits::difference_type difference_type;
90  typedef typename _Alloc_traits::reference reference;
91  typedef typename _Alloc_traits::const_reference const_reference;
92  typedef typename _Alloc_traits::pointer pointer;
93  typedef typename _Alloc_traits::const_pointer const_pointer;
94  typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
95  typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
96  const_iterator;
97  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
98  typedef std::reverse_iterator<iterator> reverse_iterator;
99 
100  /// Value returned by various member functions when they fail.
101  static const size_type npos = static_cast<size_type>(-1);
102 
103  private:
104  // type used for positions in insert, erase etc.
105 #if __cplusplus < 201103L
106  typedef iterator __const_iterator;
107 #else
108  typedef const_iterator __const_iterator;
109 #endif
110 
111 #if __cplusplus > 201402L
112  // A helper type for avoiding boiler-plate.
113  typedef basic_string_view<_CharT, _Traits> __sv_type;
114 
115  template<typename _Tp, typename _Res>
116  using _If_sv = enable_if_t<
117  __and_<is_convertible<const _Tp&, __sv_type>,
118  __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
119  _Res>;
120 #endif
121 
122  // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
123  struct _Alloc_hider : allocator_type // TODO check __is_final
124  {
125 #if __cplusplus < 201103L
126  _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
127  : allocator_type(__a), _M_p(__dat) { }
128 #else
129  _Alloc_hider(pointer __dat, const _Alloc& __a)
130  : allocator_type(__a), _M_p(__dat) { }
131 
132  _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
133  : allocator_type(std::move(__a)), _M_p(__dat) { }
134 #endif
135 
136  pointer _M_p; // The actual data.
137  };
138 
139  _Alloc_hider _M_dataplus;
140  size_type _M_string_length;
141 
142  enum { _S_local_capacity = 15 / sizeof(_CharT) };
143 
144  union
145  {
146  _CharT _M_local_buf[_S_local_capacity + 1];
147  size_type _M_allocated_capacity;
148  };
149 
150  void
151  _M_data(pointer __p)
152  { _M_dataplus._M_p = __p; }
153 
154  void
155  _M_length(size_type __length)
156  { _M_string_length = __length; }
157 
158  pointer
159  _M_data() const
160  { return _M_dataplus._M_p; }
161 
162  pointer
163  _M_local_data()
164  {
165 #if __cplusplus >= 201103L
166  return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
167 #else
168  return pointer(_M_local_buf);
169 #endif
170  }
171 
172  const_pointer
173  _M_local_data() const
174  {
175 #if __cplusplus >= 201103L
177 #else
178  return const_pointer(_M_local_buf);
179 #endif
180  }
181 
182  void
183  _M_capacity(size_type __capacity)
184  { _M_allocated_capacity = __capacity; }
185 
186  void
187  _M_set_length(size_type __n)
188  {
189  _M_length(__n);
190  traits_type::assign(_M_data()[__n], _CharT());
191  }
192 
193  bool
194  _M_is_local() const
195  { return _M_data() == _M_local_data(); }
196 
197  // Create & Destroy
198  pointer
199  _M_create(size_type&, size_type);
200 
201  void
202  _M_dispose()
203  {
204  if (!_M_is_local())
205  _M_destroy(_M_allocated_capacity);
206  }
207 
208  void
209  _M_destroy(size_type __size) throw()
210  { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
211 
212  // _M_construct_aux is used to implement the 21.3.1 para 15 which
213  // requires special behaviour if _InIterator is an integral type
214  template<typename _InIterator>
215  void
216  _M_construct_aux(_InIterator __beg, _InIterator __end,
217  std::__false_type)
218  {
219  typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
220  _M_construct(__beg, __end, _Tag());
221  }
222 
223  // _GLIBCXX_RESOLVE_LIB_DEFECTS
224  // 438. Ambiguity in the "do the right thing" clause
225  template<typename _Integer>
226  void
227  _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
228  { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
229 
230  void
231  _M_construct_aux_2(size_type __req, _CharT __c)
232  { _M_construct(__req, __c); }
233 
234  template<typename _InIterator>
235  void
236  _M_construct(_InIterator __beg, _InIterator __end)
237  {
238  typedef typename std::__is_integer<_InIterator>::__type _Integral;
239  _M_construct_aux(__beg, __end, _Integral());
240  }
241 
242  // For Input Iterators, used in istreambuf_iterators, etc.
243  template<typename _InIterator>
244  void
245  _M_construct(_InIterator __beg, _InIterator __end,
247 
248  // For forward_iterators up to random_access_iterators, used for
249  // string::iterator, _CharT*, etc.
250  template<typename _FwdIterator>
251  void
252  _M_construct(_FwdIterator __beg, _FwdIterator __end,
254 
255  void
256  _M_construct(size_type __req, _CharT __c);
257 
258  allocator_type&
259  _M_get_allocator()
260  { return _M_dataplus; }
261 
262  const allocator_type&
263  _M_get_allocator() const
264  { return _M_dataplus; }
265 
266  private:
267 
268 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
269  // The explicit instantiations in misc-inst.cc require this due to
270  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
271  template<typename _Tp, bool _Requires =
272  !__are_same<_Tp, _CharT*>::__value
273  && !__are_same<_Tp, const _CharT*>::__value
274  && !__are_same<_Tp, iterator>::__value
275  && !__are_same<_Tp, const_iterator>::__value>
276  struct __enable_if_not_native_iterator
277  { typedef basic_string& __type; };
278  template<typename _Tp>
279  struct __enable_if_not_native_iterator<_Tp, false> { };
280 #endif
281 
282  size_type
283  _M_check(size_type __pos, const char* __s) const
284  {
285  if (__pos > this->size())
286  __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
287  "this->size() (which is %zu)"),
288  __s, __pos, this->size());
289  return __pos;
290  }
291 
292  void
293  _M_check_length(size_type __n1, size_type __n2, const char* __s) const
294  {
295  if (this->max_size() - (this->size() - __n1) < __n2)
296  __throw_length_error(__N(__s));
297  }
298 
299 
300  // NB: _M_limit doesn't check for a bad __pos value.
301  size_type
302  _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
303  {
304  const bool __testoff = __off < this->size() - __pos;
305  return __testoff ? __off : this->size() - __pos;
306  }
307 
308  // True if _Rep and source do not overlap.
309  bool
310  _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
311  {
312  return (less<const _CharT*>()(__s, _M_data())
313  || less<const _CharT*>()(_M_data() + this->size(), __s));
314  }
315 
316  // When __n = 1 way faster than the general multichar
317  // traits_type::copy/move/assign.
318  static void
319  _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
320  {
321  if (__n == 1)
322  traits_type::assign(*__d, *__s);
323  else
324  traits_type::copy(__d, __s, __n);
325  }
326 
327  static void
328  _S_move(_CharT* __d, const _CharT* __s, size_type __n)
329  {
330  if (__n == 1)
331  traits_type::assign(*__d, *__s);
332  else
333  traits_type::move(__d, __s, __n);
334  }
335 
336  static void
337  _S_assign(_CharT* __d, size_type __n, _CharT __c)
338  {
339  if (__n == 1)
340  traits_type::assign(*__d, __c);
341  else
342  traits_type::assign(__d, __n, __c);
343  }
344 
345  // _S_copy_chars is a separate template to permit specialization
346  // to optimize for the common case of pointers as iterators.
347  template<class _Iterator>
348  static void
349  _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
350  {
351  for (; __k1 != __k2; ++__k1, (void)++__p)
352  traits_type::assign(*__p, *__k1); // These types are off.
353  }
354 
355  static void
356  _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
357  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
358 
359  static void
360  _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
361  _GLIBCXX_NOEXCEPT
362  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
363 
364  static void
365  _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
366  { _S_copy(__p, __k1, __k2 - __k1); }
367 
368  static void
369  _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
370  _GLIBCXX_NOEXCEPT
371  { _S_copy(__p, __k1, __k2 - __k1); }
372 
373  static int
374  _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
375  {
376  const difference_type __d = difference_type(__n1 - __n2);
377 
378  if (__d > __gnu_cxx::__numeric_traits<int>::__max)
379  return __gnu_cxx::__numeric_traits<int>::__max;
380  else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
381  return __gnu_cxx::__numeric_traits<int>::__min;
382  else
383  return int(__d);
384  }
385 
386  void
387  _M_assign(const basic_string&);
388 
389  void
390  _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
391  size_type __len2);
392 
393  void
394  _M_erase(size_type __pos, size_type __n);
395 
396  public:
397  // Construct/copy/destroy:
398  // NB: We overload ctors in some cases instead of using default
399  // arguments, per 17.4.4.4 para. 2 item 2.
400 
401  /**
402  * @brief Default constructor creates an empty string.
403  */
404  basic_string()
405  _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
406  : _M_dataplus(_M_local_data())
407  { _M_set_length(0); }
408 
409  /**
410  * @brief Construct an empty string using allocator @a a.
411  */
412  explicit
413  basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
414  : _M_dataplus(_M_local_data(), __a)
415  { _M_set_length(0); }
416 
417  /**
418  * @brief Construct string with copy of value of @a __str.
419  * @param __str Source string.
420  */
421  basic_string(const basic_string& __str)
422  : _M_dataplus(_M_local_data(),
423  _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
424  { _M_construct(__str._M_data(), __str._M_data() + __str.length()); }
425 
426  // _GLIBCXX_RESOLVE_LIB_DEFECTS
427  // 2583. no way to supply an allocator for basic_string(str, pos)
428  /**
429  * @brief Construct string as copy of a substring.
430  * @param __str Source string.
431  * @param __pos Index of first character to copy from.
432  * @param __a Allocator to use.
433  */
434  basic_string(const basic_string& __str, size_type __pos,
435  const _Alloc& __a = _Alloc())
436  : _M_dataplus(_M_local_data(), __a)
437  {
438  const _CharT* __start = __str._M_data()
439  + __str._M_check(__pos, "basic_string::basic_string");
440  _M_construct(__start, __start + __str._M_limit(__pos, npos));
441  }
442 
443  /**
444  * @brief Construct string as copy of a substring.
445  * @param __str Source string.
446  * @param __pos Index of first character to copy from.
447  * @param __n Number of characters to copy.
448  */
449  basic_string(const basic_string& __str, size_type __pos,
450  size_type __n)
451  : _M_dataplus(_M_local_data())
452  {
453  const _CharT* __start = __str._M_data()
454  + __str._M_check(__pos, "basic_string::basic_string");
455  _M_construct(__start, __start + __str._M_limit(__pos, __n));
456  }
457 
458  /**
459  * @brief Construct string as copy of a substring.
460  * @param __str Source string.
461  * @param __pos Index of first character to copy from.
462  * @param __n Number of characters to copy.
463  * @param __a Allocator to use.
464  */
465  basic_string(const basic_string& __str, size_type __pos,
466  size_type __n, const _Alloc& __a)
467  : _M_dataplus(_M_local_data(), __a)
468  {
469  const _CharT* __start
470  = __str._M_data() + __str._M_check(__pos, "string::string");
471  _M_construct(__start, __start + __str._M_limit(__pos, __n));
472  }
473 
474  /**
475  * @brief Construct string initialized by a character %array.
476  * @param __s Source character %array.
477  * @param __n Number of characters to copy.
478  * @param __a Allocator to use (default is default allocator).
479  *
480  * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
481  * has no special meaning.
482  */
483  basic_string(const _CharT* __s, size_type __n,
484  const _Alloc& __a = _Alloc())
485  : _M_dataplus(_M_local_data(), __a)
486  { _M_construct(__s, __s + __n); }
487 
488  /**
489  * @brief Construct string as copy of a C string.
490  * @param __s Source C string.
491  * @param __a Allocator to use (default is default allocator).
492  */
493  basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
494  : _M_dataplus(_M_local_data(), __a)
495  { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); }
496 
497  /**
498  * @brief Construct string as multiple characters.
499  * @param __n Number of characters.
500  * @param __c Character to use.
501  * @param __a Allocator to use (default is default allocator).
502  */
503  basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
504  : _M_dataplus(_M_local_data(), __a)
505  { _M_construct(__n, __c); }
506 
507 #if __cplusplus >= 201103L
508  /**
509  * @brief Move construct string.
510  * @param __str Source string.
511  *
512  * The newly-created string contains the exact contents of @a __str.
513  * @a __str is a valid, but unspecified string.
514  **/
515  basic_string(basic_string&& __str) noexcept
516  : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
517  {
518  if (__str._M_is_local())
519  {
520  traits_type::copy(_M_local_buf, __str._M_local_buf,
521  _S_local_capacity + 1);
522  }
523  else
524  {
525  _M_data(__str._M_data());
526  _M_capacity(__str._M_allocated_capacity);
527  }
528 
529  // Must use _M_length() here not _M_set_length() because
530  // basic_stringbuf relies on writing into unallocated capacity so
531  // we mess up the contents if we put a '\0' in the string.
532  _M_length(__str.length());
533  __str._M_data(__str._M_local_data());
534  __str._M_set_length(0);
535  }
536 
537  /**
538  * @brief Construct string from an initializer %list.
539  * @param __l std::initializer_list of characters.
540  * @param __a Allocator to use (default is default allocator).
541  */
542  basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
543  : _M_dataplus(_M_local_data(), __a)
544  { _M_construct(__l.begin(), __l.end()); }
545 
546  basic_string(const basic_string& __str, const _Alloc& __a)
547  : _M_dataplus(_M_local_data(), __a)
548  { _M_construct(__str.begin(), __str.end()); }
549 
550  basic_string(basic_string&& __str, const _Alloc& __a)
551  noexcept(_Alloc_traits::_S_always_equal())
552  : _M_dataplus(_M_local_data(), __a)
553  {
554  if (__str._M_is_local())
555  {
556  traits_type::copy(_M_local_buf, __str._M_local_buf,
557  _S_local_capacity + 1);
558  _M_length(__str.length());
559  __str._M_set_length(0);
560  }
561  else if (_Alloc_traits::_S_always_equal()
562  || __str.get_allocator() == __a)
563  {
564  _M_data(__str._M_data());
565  _M_length(__str.length());
566  _M_capacity(__str._M_allocated_capacity);
567  __str._M_data(__str._M_local_buf);
568  __str._M_set_length(0);
569  }
570  else
571  _M_construct(__str.begin(), __str.end());
572  }
573 
574 #endif // C++11
575 
576  /**
577  * @brief Construct string as copy of a range.
578  * @param __beg Start of range.
579  * @param __end End of range.
580  * @param __a Allocator to use (default is default allocator).
581  */
582 #if __cplusplus >= 201103L
583  template<typename _InputIterator,
584  typename = std::_RequireInputIter<_InputIterator>>
585 #else
586  template<typename _InputIterator>
587 #endif
588  basic_string(_InputIterator __beg, _InputIterator __end,
589  const _Alloc& __a = _Alloc())
590  : _M_dataplus(_M_local_data(), __a)
591  { _M_construct(__beg, __end); }
592 
593 #if __cplusplus > 201402L
594  /**
595  * @brief Construct string from a substring of a string_view.
596  * @param __t Source string view.
597  * @param __pos The index of the first character to copy from __t.
598  * @param __n The number of characters to copy from __t.
599  * @param __a Allocator to use.
600  */
601  template<typename _Tp, typename = _If_sv<_Tp, void>>
602  basic_string(const _Tp& __t, size_type __pos, size_type __n,
603  const _Alloc& __a = _Alloc())
604  : basic_string(__sv_type(__t).substr(__pos, __n), __a) { }
605 
606  /**
607  * @brief Construct string from a string_view.
608  * @param __sv Source string view.
609  * @param __a Allocator to use (default is default allocator).
610  */
611  explicit
612  basic_string(__sv_type __sv, const _Alloc& __a = _Alloc())
613  : basic_string(__sv.data(), __sv.size(), __a) { }
614 #endif // C++17
615 
616  /**
617  * @brief Destroy the string instance.
618  */
619  ~basic_string()
620  { _M_dispose(); }
621 
622  /**
623  * @brief Assign the value of @a str to this string.
624  * @param __str Source string.
625  */
626  basic_string&
627  operator=(const basic_string& __str)
628  {
629 #if __cplusplus >= 201103L
630  if (_Alloc_traits::_S_propagate_on_copy_assign())
631  {
632  if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
633  && _M_get_allocator() != __str._M_get_allocator())
634  {
635  // Propagating allocator cannot free existing storage so must
636  // deallocate it before replacing current allocator.
637  if (__str.size() <= _S_local_capacity)
638  {
639  _M_destroy(_M_allocated_capacity);
640  _M_data(_M_local_data());
641  _M_set_length(0);
642  }
643  else
644  {
645  const auto __len = __str.size();
646  auto __alloc = __str._M_get_allocator();
647  // If this allocation throws there are no effects:
648  auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1);
649  _M_destroy(_M_allocated_capacity);
650  _M_data(__ptr);
651  _M_capacity(__len);
652  _M_set_length(__len);
653  }
654  }
655  std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
656  }
657 #endif
658  return this->assign(__str);
659  }
660 
661  /**
662  * @brief Copy contents of @a s into this string.
663  * @param __s Source null-terminated string.
664  */
665  basic_string&
666  operator=(const _CharT* __s)
667  { return this->assign(__s); }
668 
669  /**
670  * @brief Set value to string of length 1.
671  * @param __c Source character.
672  *
673  * Assigning to a character makes this string length 1 and
674  * (*this)[0] == @a c.
675  */
676  basic_string&
677  operator=(_CharT __c)
678  {
679  this->assign(1, __c);
680  return *this;
681  }
682 
683 #if __cplusplus >= 201103L
684  /**
685  * @brief Move assign the value of @a str to this string.
686  * @param __str Source string.
687  *
688  * The contents of @a str are moved into this string (without copying).
689  * @a str is a valid, but unspecified string.
690  **/
691  // PR 58265, this should be noexcept.
692  // _GLIBCXX_RESOLVE_LIB_DEFECTS
693  // 2063. Contradictory requirements for string move assignment
694  basic_string&
695  operator=(basic_string&& __str)
696  noexcept(_Alloc_traits::_S_nothrow_move())
697  {
698  if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
699  && !_Alloc_traits::_S_always_equal()
700  && _M_get_allocator() != __str._M_get_allocator())
701  {
702  // Destroy existing storage before replacing allocator.
703  _M_destroy(_M_allocated_capacity);
704  _M_data(_M_local_data());
705  _M_set_length(0);
706  }
707  // Replace allocator if POCMA is true.
708  std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
709 
710  if (!__str._M_is_local()
711  && (_Alloc_traits::_S_propagate_on_move_assign()
712  || _Alloc_traits::_S_always_equal()))
713  {
714  pointer __data = nullptr;
715  size_type __capacity;
716  if (!_M_is_local())
717  {
718  if (_Alloc_traits::_S_always_equal())
719  {
720  __data = _M_data();
721  __capacity = _M_allocated_capacity;
722  }
723  else
724  _M_destroy(_M_allocated_capacity);
725  }
726 
727  _M_data(__str._M_data());
728  _M_length(__str.length());
729  _M_capacity(__str._M_allocated_capacity);
730  if (__data)
731  {
732  __str._M_data(__data);
733  __str._M_capacity(__capacity);
734  }
735  else
736  __str._M_data(__str._M_local_buf);
737  }
738  else
739  assign(__str);
740  __str.clear();
741  return *this;
742  }
743 
744  /**
745  * @brief Set value to string constructed from initializer %list.
746  * @param __l std::initializer_list.
747  */
748  basic_string&
749  operator=(initializer_list<_CharT> __l)
750  {
751  this->assign(__l.begin(), __l.size());
752  return *this;
753  }
754 #endif // C++11
755 
756 #if __cplusplus > 201402L
757  /**
758  * @brief Set value to string constructed from a string_view.
759  * @param __sv A string_view.
760  */
761  template<typename _Tp>
762  _If_sv<_Tp, basic_string&>
763  operator=(_Tp __sv)
764  { return this->assign(__sv); }
765 
766  /**
767  * @brief Convert to a string_view.
768  * @return A string_view.
769  */
770  operator __sv_type() const noexcept
771  { return __sv_type(data(), size()); }
772 #endif // C++17
773 
774  // Iterators:
775  /**
776  * Returns a read/write iterator that points to the first character in
777  * the %string.
778  */
779  iterator
780  begin() _GLIBCXX_NOEXCEPT
781  { return iterator(_M_data()); }
782 
783  /**
784  * Returns a read-only (constant) iterator that points to the first
785  * character in the %string.
786  */
787  const_iterator
788  begin() const _GLIBCXX_NOEXCEPT
789  { return const_iterator(_M_data()); }
790 
791  /**
792  * Returns a read/write iterator that points one past the last
793  * character in the %string.
794  */
795  iterator
796  end() _GLIBCXX_NOEXCEPT
797  { return iterator(_M_data() + this->size()); }
798 
799  /**
800  * Returns a read-only (constant) iterator that points one past the
801  * last character in the %string.
802  */
803  const_iterator
804  end() const _GLIBCXX_NOEXCEPT
805  { return const_iterator(_M_data() + this->size()); }
806 
807  /**
808  * Returns a read/write reverse iterator that points to the last
809  * character in the %string. Iteration is done in reverse element
810  * order.
811  */
812  reverse_iterator
813  rbegin() _GLIBCXX_NOEXCEPT
814  { return reverse_iterator(this->end()); }
815 
816  /**
817  * Returns a read-only (constant) reverse iterator that points
818  * to the last character in the %string. Iteration is done in
819  * reverse element order.
820  */
821  const_reverse_iterator
822  rbegin() const _GLIBCXX_NOEXCEPT
823  { return const_reverse_iterator(this->end()); }
824 
825  /**
826  * Returns a read/write reverse iterator that points to one before the
827  * first character in the %string. Iteration is done in reverse
828  * element order.
829  */
830  reverse_iterator
831  rend() _GLIBCXX_NOEXCEPT
832  { return reverse_iterator(this->begin()); }
833 
834  /**
835  * Returns a read-only (constant) reverse iterator that points
836  * to one before the first character in the %string. Iteration
837  * is done in reverse element order.
838  */
839  const_reverse_iterator
840  rend() const _GLIBCXX_NOEXCEPT
841  { return const_reverse_iterator(this->begin()); }
842 
843 #if __cplusplus >= 201103L
844  /**
845  * Returns a read-only (constant) iterator that points to the first
846  * character in the %string.
847  */
848  const_iterator
849  cbegin() const noexcept
850  { return const_iterator(this->_M_data()); }
851 
852  /**
853  * Returns a read-only (constant) iterator that points one past the
854  * last character in the %string.
855  */
856  const_iterator
857  cend() const noexcept
858  { return const_iterator(this->_M_data() + this->size()); }
859 
860  /**
861  * Returns a read-only (constant) reverse iterator that points
862  * to the last character in the %string. Iteration is done in
863  * reverse element order.
864  */
865  const_reverse_iterator
866  crbegin() const noexcept
867  { return const_reverse_iterator(this->end()); }
868 
869  /**
870  * Returns a read-only (constant) reverse iterator that points
871  * to one before the first character in the %string. Iteration
872  * is done in reverse element order.
873  */
874  const_reverse_iterator
875  crend() const noexcept
876  { return const_reverse_iterator(this->begin()); }
877 #endif
878 
879  public:
880  // Capacity:
881  /// Returns the number of characters in the string, not including any
882  /// null-termination.
883  size_type
884  size() const _GLIBCXX_NOEXCEPT
885  { return _M_string_length; }
886 
887  /// Returns the number of characters in the string, not including any
888  /// null-termination.
889  size_type
890  length() const _GLIBCXX_NOEXCEPT
891  { return _M_string_length; }
892 
893  /// Returns the size() of the largest possible %string.
894  size_type
895  max_size() const _GLIBCXX_NOEXCEPT
896  { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
897 
898  /**
899  * @brief Resizes the %string to the specified number of characters.
900  * @param __n Number of characters the %string should contain.
901  * @param __c Character to fill any new elements.
902  *
903  * This function will %resize the %string to the specified
904  * number of characters. If the number is smaller than the
905  * %string's current size the %string is truncated, otherwise
906  * the %string is extended and new elements are %set to @a __c.
907  */
908  void
909  resize(size_type __n, _CharT __c);
910 
911  /**
912  * @brief Resizes the %string to the specified number of characters.
913  * @param __n Number of characters the %string should contain.
914  *
915  * This function will resize the %string to the specified length. If
916  * the new size is smaller than the %string's current size the %string
917  * is truncated, otherwise the %string is extended and new characters
918  * are default-constructed. For basic types such as char, this means
919  * setting them to 0.
920  */
921  void
922  resize(size_type __n)
923  { this->resize(__n, _CharT()); }
924 
925 #if __cplusplus >= 201103L
926  /// A non-binding request to reduce capacity() to size().
927  void
928  shrink_to_fit() noexcept
929  {
930 #if __cpp_exceptions
931  if (capacity() > size())
932  {
933  try
934  { reserve(0); }
935  catch(...)
936  { }
937  }
938 #endif
939  }
940 #endif
941 
942  /**
943  * Returns the total number of characters that the %string can hold
944  * before needing to allocate more memory.
945  */
946  size_type
947  capacity() const _GLIBCXX_NOEXCEPT
948  {
949  return _M_is_local() ? size_type(_S_local_capacity)
950  : _M_allocated_capacity;
951  }
952 
953  /**
954  * @brief Attempt to preallocate enough memory for specified number of
955  * characters.
956  * @param __res_arg Number of characters required.
957  * @throw std::length_error If @a __res_arg exceeds @c max_size().
958  *
959  * This function attempts to reserve enough memory for the
960  * %string to hold the specified number of characters. If the
961  * number requested is more than max_size(), length_error is
962  * thrown.
963  *
964  * The advantage of this function is that if optimal code is a
965  * necessity and the user can determine the string length that will be
966  * required, the user can reserve the memory in %advance, and thus
967  * prevent a possible reallocation of memory and copying of %string
968  * data.
969  */
970  void
971  reserve(size_type __res_arg = 0);
972 
973  /**
974  * Erases the string, making it empty.
975  */
976  void
977  clear() _GLIBCXX_NOEXCEPT
978  { _M_set_length(0); }
979 
980  /**
981  * Returns true if the %string is empty. Equivalent to
982  * <code>*this == ""</code>.
983  */
984  bool
985  empty() const _GLIBCXX_NOEXCEPT
986  { return this->size() == 0; }
987 
988  // Element access:
989  /**
990  * @brief Subscript access to the data contained in the %string.
991  * @param __pos The index of the character to access.
992  * @return Read-only (constant) reference to the character.
993  *
994  * This operator allows for easy, array-style, data access.
995  * Note that data access with this operator is unchecked and
996  * out_of_range lookups are not defined. (For checked lookups
997  * see at().)
998  */
999  const_reference
1000  operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
1001  {
1002  __glibcxx_assert(__pos <= size());
1003  return _M_data()[__pos];
1004  }
1005 
1006  /**
1007  * @brief Subscript access to the data contained in the %string.
1008  * @param __pos The index of the character to access.
1009  * @return Read/write reference to the character.
1010  *
1011  * This operator allows for easy, array-style, data access.
1012  * Note that data access with this operator is unchecked and
1013  * out_of_range lookups are not defined. (For checked lookups
1014  * see at().)
1015  */
1016  reference
1017  operator[](size_type __pos)
1018  {
1019  // Allow pos == size() both in C++98 mode, as v3 extension,
1020  // and in C++11 mode.
1021  __glibcxx_assert(__pos <= size());
1022  // In pedantic mode be strict in C++98 mode.
1023  _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1024  return _M_data()[__pos];
1025  }
1026 
1027  /**
1028  * @brief Provides access to the data contained in the %string.
1029  * @param __n The index of the character to access.
1030  * @return Read-only (const) reference to the character.
1031  * @throw std::out_of_range If @a n is an invalid index.
1032  *
1033  * This function provides for safer data access. The parameter is
1034  * first checked that it is in the range of the string. The function
1035  * throws out_of_range if the check fails.
1036  */
1037  const_reference
1038  at(size_type __n) const
1039  {
1040  if (__n >= this->size())
1041  __throw_out_of_range_fmt(__N("basic_string::at: __n "
1042  "(which is %zu) >= this->size() "
1043  "(which is %zu)"),
1044  __n, this->size());
1045  return _M_data()[__n];
1046  }
1047 
1048  /**
1049  * @brief Provides access to the data contained in the %string.
1050  * @param __n The index of the character to access.
1051  * @return Read/write reference to the character.
1052  * @throw std::out_of_range If @a n is an invalid index.
1053  *
1054  * This function provides for safer data access. The parameter is
1055  * first checked that it is in the range of the string. The function
1056  * throws out_of_range if the check fails.
1057  */
1058  reference
1059  at(size_type __n)
1060  {
1061  if (__n >= size())
1062  __throw_out_of_range_fmt(__N("basic_string::at: __n "
1063  "(which is %zu) >= this->size() "
1064  "(which is %zu)"),
1065  __n, this->size());
1066  return _M_data()[__n];
1067  }
1068 
1069 #if __cplusplus >= 201103L
1070  /**
1071  * Returns a read/write reference to the data at the first
1072  * element of the %string.
1073  */
1074  reference
1075  front() noexcept
1076  {
1077  __glibcxx_assert(!empty());
1078  return operator[](0);
1079  }
1080 
1081  /**
1082  * Returns a read-only (constant) reference to the data at the first
1083  * element of the %string.
1084  */
1085  const_reference
1086  front() const noexcept
1087  {
1088  __glibcxx_assert(!empty());
1089  return operator[](0);
1090  }
1091 
1092  /**
1093  * Returns a read/write reference to the data at the last
1094  * element of the %string.
1095  */
1096  reference
1097  back() noexcept
1098  {
1099  __glibcxx_assert(!empty());
1100  return operator[](this->size() - 1);
1101  }
1102 
1103  /**
1104  * Returns a read-only (constant) reference to the data at the
1105  * last element of the %string.
1106  */
1107  const_reference
1108  back() const noexcept
1109  {
1110  __glibcxx_assert(!empty());
1111  return operator[](this->size() - 1);
1112  }
1113 #endif
1114 
1115  // Modifiers:
1116  /**
1117  * @brief Append a string to this string.
1118  * @param __str The string to append.
1119  * @return Reference to this string.
1120  */
1121  basic_string&
1122  operator+=(const basic_string& __str)
1123  { return this->append(__str); }
1124 
1125  /**
1126  * @brief Append a C string.
1127  * @param __s The C string to append.
1128  * @return Reference to this string.
1129  */
1130  basic_string&
1131  operator+=(const _CharT* __s)
1132  { return this->append(__s); }
1133 
1134  /**
1135  * @brief Append a character.
1136  * @param __c The character to append.
1137  * @return Reference to this string.
1138  */
1139  basic_string&
1140  operator+=(_CharT __c)
1141  {
1142  this->push_back(__c);
1143  return *this;
1144  }
1145 
1146 #if __cplusplus >= 201103L
1147  /**
1148  * @brief Append an initializer_list of characters.
1149  * @param __l The initializer_list of characters to be appended.
1150  * @return Reference to this string.
1151  */
1152  basic_string&
1153  operator+=(initializer_list<_CharT> __l)
1154  { return this->append(__l.begin(), __l.size()); }
1155 #endif // C++11
1156 
1157 #if __cplusplus > 201402L
1158  /**
1159  * @brief Append a string_view.
1160  * @param __sv The string_view to be appended.
1161  * @return Reference to this string.
1162  */
1163  basic_string&
1164  operator+=(__sv_type __sv)
1165  { return this->append(__sv); }
1166 #endif // C++17
1167 
1168  /**
1169  * @brief Append a string to this string.
1170  * @param __str The string to append.
1171  * @return Reference to this string.
1172  */
1173  basic_string&
1174  append(const basic_string& __str)
1175  { return _M_append(__str._M_data(), __str.size()); }
1176 
1177  /**
1178  * @brief Append a substring.
1179  * @param __str The string to append.
1180  * @param __pos Index of the first character of str to append.
1181  * @param __n The number of characters to append.
1182  * @return Reference to this string.
1183  * @throw std::out_of_range if @a __pos is not a valid index.
1184  *
1185  * This function appends @a __n characters from @a __str
1186  * starting at @a __pos to this string. If @a __n is is larger
1187  * than the number of available characters in @a __str, the
1188  * remainder of @a __str is appended.
1189  */
1190  basic_string&
1191  append(const basic_string& __str, size_type __pos, size_type __n)
1192  { return _M_append(__str._M_data()
1193  + __str._M_check(__pos, "basic_string::append"),
1194  __str._M_limit(__pos, __n)); }
1195 
1196  /**
1197  * @brief Append a C substring.
1198  * @param __s The C string to append.
1199  * @param __n The number of characters to append.
1200  * @return Reference to this string.
1201  */
1202  basic_string&
1203  append(const _CharT* __s, size_type __n)
1204  {
1205  __glibcxx_requires_string_len(__s, __n);
1206  _M_check_length(size_type(0), __n, "basic_string::append");
1207  return _M_append(__s, __n);
1208  }
1209 
1210  /**
1211  * @brief Append a C string.
1212  * @param __s The C string to append.
1213  * @return Reference to this string.
1214  */
1215  basic_string&
1216  append(const _CharT* __s)
1217  {
1218  __glibcxx_requires_string(__s);
1219  const size_type __n = traits_type::length(__s);
1220  _M_check_length(size_type(0), __n, "basic_string::append");
1221  return _M_append(__s, __n);
1222  }
1223 
1224  /**
1225  * @brief Append multiple characters.
1226  * @param __n The number of characters to append.
1227  * @param __c The character to use.
1228  * @return Reference to this string.
1229  *
1230  * Appends __n copies of __c to this string.
1231  */
1232  basic_string&
1233  append(size_type __n, _CharT __c)
1234  { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
1235 
1236 #if __cplusplus >= 201103L
1237  /**
1238  * @brief Append an initializer_list of characters.
1239  * @param __l The initializer_list of characters to append.
1240  * @return Reference to this string.
1241  */
1242  basic_string&
1243  append(initializer_list<_CharT> __l)
1244  { return this->append(__l.begin(), __l.size()); }
1245 #endif // C++11
1246 
1247  /**
1248  * @brief Append a range of characters.
1249  * @param __first Iterator referencing the first character to append.
1250  * @param __last Iterator marking the end of the range.
1251  * @return Reference to this string.
1252  *
1253  * Appends characters in the range [__first,__last) to this string.
1254  */
1255 #if __cplusplus >= 201103L
1256  template<class _InputIterator,
1257  typename = std::_RequireInputIter<_InputIterator>>
1258 #else
1259  template<class _InputIterator>
1260 #endif
1261  basic_string&
1262  append(_InputIterator __first, _InputIterator __last)
1263  { return this->replace(end(), end(), __first, __last); }
1264 
1265 #if __cplusplus > 201402L
1266  /**
1267  * @brief Append a string_view.
1268  * @param __sv The string_view to be appended.
1269  * @return Reference to this string.
1270  */
1271  basic_string&
1272  append(__sv_type __sv)
1273  { return this->append(__sv.data(), __sv.size()); }
1274 
1275  /**
1276  * @brief Append a range of characters from a string_view.
1277  * @param __sv The string_view to be appended from.
1278  * @param __pos The position in the string_view to append from.
1279  * @param __n The number of characters to append from the string_view.
1280  * @return Reference to this string.
1281  */
1282  template <typename _Tp>
1283  _If_sv<_Tp, basic_string&>
1284  append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1285  {
1286  __sv_type __sv = __svt;
1287  return _M_append(__sv.data()
1288  + __sv._M_check(__pos, "basic_string::append"),
1289  __sv._M_limit(__pos, __n));
1290  }
1291 #endif // C++17
1292 
1293  /**
1294  * @brief Append a single character.
1295  * @param __c Character to append.
1296  */
1297  void
1298  push_back(_CharT __c)
1299  {
1300  const size_type __size = this->size();
1301  if (__size + 1 > this->capacity())
1302  this->_M_mutate(__size, size_type(0), 0, size_type(1));
1303  traits_type::assign(this->_M_data()[__size], __c);
1304  this->_M_set_length(__size + 1);
1305  }
1306 
1307  /**
1308  * @brief Set value to contents of another string.
1309  * @param __str Source string to use.
1310  * @return Reference to this string.
1311  */
1312  basic_string&
1313  assign(const basic_string& __str)
1314  {
1315  this->_M_assign(__str);
1316  return *this;
1317  }
1318 
1319 #if __cplusplus >= 201103L
1320  /**
1321  * @brief Set value to contents of another string.
1322  * @param __str Source string to use.
1323  * @return Reference to this string.
1324  *
1325  * This function sets this string to the exact contents of @a __str.
1326  * @a __str is a valid, but unspecified string.
1327  */
1328  basic_string&
1329  assign(basic_string&& __str)
1330  noexcept(_Alloc_traits::_S_nothrow_move())
1331  {
1332  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1333  // 2063. Contradictory requirements for string move assignment
1334  return *this = std::move(__str);
1335  }
1336 #endif // C++11
1337 
1338  /**
1339  * @brief Set value to a substring of a string.
1340  * @param __str The string to use.
1341  * @param __pos Index of the first character of str.
1342  * @param __n Number of characters to use.
1343  * @return Reference to this string.
1344  * @throw std::out_of_range if @a pos is not a valid index.
1345  *
1346  * This function sets this string to the substring of @a __str
1347  * consisting of @a __n characters at @a __pos. If @a __n is
1348  * is larger than the number of available characters in @a
1349  * __str, the remainder of @a __str is used.
1350  */
1351  basic_string&
1352  assign(const basic_string& __str, size_type __pos, size_type __n)
1353  { return _M_replace(size_type(0), this->size(), __str._M_data()
1354  + __str._M_check(__pos, "basic_string::assign"),
1355  __str._M_limit(__pos, __n)); }
1356 
1357  /**
1358  * @brief Set value to a C substring.
1359  * @param __s The C string to use.
1360  * @param __n Number of characters to use.
1361  * @return Reference to this string.
1362  *
1363  * This function sets the value of this string to the first @a __n
1364  * characters of @a __s. If @a __n is is larger than the number of
1365  * available characters in @a __s, the remainder of @a __s is used.
1366  */
1367  basic_string&
1368  assign(const _CharT* __s, size_type __n)
1369  {
1370  __glibcxx_requires_string_len(__s, __n);
1371  return _M_replace(size_type(0), this->size(), __s, __n);
1372  }
1373 
1374  /**
1375  * @brief Set value to contents of a C string.
1376  * @param __s The C string to use.
1377  * @return Reference to this string.
1378  *
1379  * This function sets the value of this string to the value of @a __s.
1380  * The data is copied, so there is no dependence on @a __s once the
1381  * function returns.
1382  */
1383  basic_string&
1384  assign(const _CharT* __s)
1385  {
1386  __glibcxx_requires_string(__s);
1387  return _M_replace(size_type(0), this->size(), __s,
1388  traits_type::length(__s));
1389  }
1390 
1391  /**
1392  * @brief Set value to multiple characters.
1393  * @param __n Length of the resulting string.
1394  * @param __c The character to use.
1395  * @return Reference to this string.
1396  *
1397  * This function sets the value of this string to @a __n copies of
1398  * character @a __c.
1399  */
1400  basic_string&
1401  assign(size_type __n, _CharT __c)
1402  { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1403 
1404  /**
1405  * @brief Set value to a range of characters.
1406  * @param __first Iterator referencing the first character to append.
1407  * @param __last Iterator marking the end of the range.
1408  * @return Reference to this string.
1409  *
1410  * Sets value of string to characters in the range [__first,__last).
1411  */
1412 #if __cplusplus >= 201103L
1413  template<class _InputIterator,
1414  typename = std::_RequireInputIter<_InputIterator>>
1415 #else
1416  template<class _InputIterator>
1417 #endif
1418  basic_string&
1419  assign(_InputIterator __first, _InputIterator __last)
1420  { return this->replace(begin(), end(), __first, __last); }
1421 
1422 #if __cplusplus >= 201103L
1423  /**
1424  * @brief Set value to an initializer_list of characters.
1425  * @param __l The initializer_list of characters to assign.
1426  * @return Reference to this string.
1427  */
1428  basic_string&
1429  assign(initializer_list<_CharT> __l)
1430  { return this->assign(__l.begin(), __l.size()); }
1431 #endif // C++11
1432 
1433 #if __cplusplus > 201402L
1434  /**
1435  * @brief Set value from a string_view.
1436  * @param __sv The source string_view.
1437  * @return Reference to this string.
1438  */
1439  basic_string&
1440  assign(__sv_type __sv)
1441  { return this->assign(__sv.data(), __sv.size()); }
1442 
1443  /**
1444  * @brief Set value from a range of characters in a string_view.
1445  * @param __sv The source string_view.
1446  * @param __pos The position in the string_view to assign from.
1447  * @param __n The number of characters to assign.
1448  * @return Reference to this string.
1449  */
1450  template <typename _Tp>
1451  _If_sv<_Tp, basic_string&>
1452  assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
1453  {
1454  __sv_type __sv = __svt;
1455  return _M_replace(size_type(0), this->size(), __sv.data()
1456  + __sv._M_check(__pos, "basic_string::assign"),
1457  __sv._M_limit(__pos, __n));
1458  }
1459 #endif // C++17
1460 
1461 #if __cplusplus >= 201103L
1462  /**
1463  * @brief Insert multiple characters.
1464  * @param __p Const_iterator referencing location in string to
1465  * insert at.
1466  * @param __n Number of characters to insert
1467  * @param __c The character to insert.
1468  * @return Iterator referencing the first inserted char.
1469  * @throw std::length_error If new length exceeds @c max_size().
1470  *
1471  * Inserts @a __n copies of character @a __c starting at the
1472  * position referenced by iterator @a __p. If adding
1473  * characters causes the length to exceed max_size(),
1474  * length_error is thrown. The value of the string doesn't
1475  * change if an error is thrown.
1476  */
1477  iterator
1478  insert(const_iterator __p, size_type __n, _CharT __c)
1479  {
1480  _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1481  const size_type __pos = __p - begin();
1482  this->replace(__p, __p, __n, __c);
1483  return iterator(this->_M_data() + __pos);
1484  }
1485 #else
1486  /**
1487  * @brief Insert multiple characters.
1488  * @param __p Iterator referencing location in string to insert at.
1489  * @param __n Number of characters to insert
1490  * @param __c The character to insert.
1491  * @throw std::length_error If new length exceeds @c max_size().
1492  *
1493  * Inserts @a __n copies of character @a __c starting at the
1494  * position referenced by iterator @a __p. If adding
1495  * characters causes the length to exceed max_size(),
1496  * length_error is thrown. The value of the string doesn't
1497  * change if an error is thrown.
1498  */
1499  void
1500  insert(iterator __p, size_type __n, _CharT __c)
1501  { this->replace(__p, __p, __n, __c); }
1502 #endif
1503 
1504 #if __cplusplus >= 201103L
1505  /**
1506  * @brief Insert a range of characters.
1507  * @param __p Const_iterator referencing location in string to
1508  * insert at.
1509  * @param __beg Start of range.
1510  * @param __end End of range.
1511  * @return Iterator referencing the first inserted char.
1512  * @throw std::length_error If new length exceeds @c max_size().
1513  *
1514  * Inserts characters in range [beg,end). If adding characters
1515  * causes the length to exceed max_size(), length_error is
1516  * thrown. The value of the string doesn't change if an error
1517  * is thrown.
1518  */
1519  template<class _InputIterator,
1520  typename = std::_RequireInputIter<_InputIterator>>
1521  iterator
1522  insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
1523  {
1524  _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1525  const size_type __pos = __p - begin();
1526  this->replace(__p, __p, __beg, __end);
1527  return iterator(this->_M_data() + __pos);
1528  }
1529 #else
1530  /**
1531  * @brief Insert a range of characters.
1532  * @param __p Iterator referencing location in string to insert at.
1533  * @param __beg Start of range.
1534  * @param __end End of range.
1535  * @throw std::length_error If new length exceeds @c max_size().
1536  *
1537  * Inserts characters in range [__beg,__end). If adding
1538  * characters causes the length to exceed max_size(),
1539  * length_error is thrown. The value of the string doesn't
1540  * change if an error is thrown.
1541  */
1542  template<class _InputIterator>
1543  void
1544  insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1545  { this->replace(__p, __p, __beg, __end); }
1546 #endif
1547 
1548 #if __cplusplus >= 201103L
1549  /**
1550  * @brief Insert an initializer_list of characters.
1551  * @param __p Iterator referencing location in string to insert at.
1552  * @param __l The initializer_list of characters to insert.
1553  * @throw std::length_error If new length exceeds @c max_size().
1554  */
1555  void
1556  insert(iterator __p, initializer_list<_CharT> __l)
1557  {
1558  _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1559  this->insert(__p - begin(), __l.begin(), __l.size());
1560  }
1561 #endif // C++11
1562 
1563  /**
1564  * @brief Insert value of a string.
1565  * @param __pos1 Iterator referencing location in string to insert at.
1566  * @param __str The string to insert.
1567  * @return Reference to this string.
1568  * @throw std::length_error If new length exceeds @c max_size().
1569  *
1570  * Inserts value of @a __str starting at @a __pos1. If adding
1571  * characters causes the length to exceed max_size(),
1572  * length_error is thrown. The value of the string doesn't
1573  * change if an error is thrown.
1574  */
1575  basic_string&
1576  insert(size_type __pos1, const basic_string& __str)
1577  { return this->replace(__pos1, size_type(0),
1578  __str._M_data(), __str.size()); }
1579 
1580  /**
1581  * @brief Insert a substring.
1582  * @param __pos1 Iterator referencing location in string to insert at.
1583  * @param __str The string to insert.
1584  * @param __pos2 Start of characters in str to insert.
1585  * @param __n Number of characters to insert.
1586  * @return Reference to this string.
1587  * @throw std::length_error If new length exceeds @c max_size().
1588  * @throw std::out_of_range If @a pos1 > size() or
1589  * @a __pos2 > @a str.size().
1590  *
1591  * Starting at @a pos1, insert @a __n character of @a __str
1592  * beginning with @a __pos2. If adding characters causes the
1593  * length to exceed max_size(), length_error is thrown. If @a
1594  * __pos1 is beyond the end of this string or @a __pos2 is
1595  * beyond the end of @a __str, out_of_range is thrown. The
1596  * value of the string doesn't change if an error is thrown.
1597  */
1598  basic_string&
1599  insert(size_type __pos1, const basic_string& __str,
1600  size_type __pos2, size_type __n)
1601  { return this->replace(__pos1, size_type(0), __str._M_data()
1602  + __str._M_check(__pos2, "basic_string::insert"),
1603  __str._M_limit(__pos2, __n)); }
1604 
1605  /**
1606  * @brief Insert a C substring.
1607  * @param __pos Iterator referencing location in string to insert at.
1608  * @param __s The C string to insert.
1609  * @param __n The number of characters to insert.
1610  * @return Reference to this string.
1611  * @throw std::length_error If new length exceeds @c max_size().
1612  * @throw std::out_of_range If @a __pos is beyond the end of this
1613  * string.
1614  *
1615  * Inserts the first @a __n characters of @a __s starting at @a
1616  * __pos. If adding characters causes the length to exceed
1617  * max_size(), length_error is thrown. If @a __pos is beyond
1618  * end(), out_of_range is thrown. The value of the string
1619  * doesn't change if an error is thrown.
1620  */
1621  basic_string&
1622  insert(size_type __pos, const _CharT* __s, size_type __n)
1623  { return this->replace(__pos, size_type(0), __s, __n); }
1624 
1625  /**
1626  * @brief Insert a C string.
1627  * @param __pos Iterator referencing location in string to insert at.
1628  * @param __s The C string to insert.
1629  * @return Reference to this string.
1630  * @throw std::length_error If new length exceeds @c max_size().
1631  * @throw std::out_of_range If @a pos is beyond the end of this
1632  * string.
1633  *
1634  * Inserts the first @a n characters of @a __s starting at @a __pos. If
1635  * adding characters causes the length to exceed max_size(),
1636  * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1637  * thrown. The value of the string doesn't change if an error is
1638  * thrown.
1639  */
1640  basic_string&
1641  insert(size_type __pos, const _CharT* __s)
1642  {
1643  __glibcxx_requires_string(__s);
1644  return this->replace(__pos, size_type(0), __s,
1645  traits_type::length(__s));
1646  }
1647 
1648  /**
1649  * @brief Insert multiple characters.
1650  * @param __pos Index in string to insert at.
1651  * @param __n Number of characters to insert
1652  * @param __c The character to insert.
1653  * @return Reference to this string.
1654  * @throw std::length_error If new length exceeds @c max_size().
1655  * @throw std::out_of_range If @a __pos is beyond the end of this
1656  * string.
1657  *
1658  * Inserts @a __n copies of character @a __c starting at index
1659  * @a __pos. If adding characters causes the length to exceed
1660  * max_size(), length_error is thrown. If @a __pos > length(),
1661  * out_of_range is thrown. The value of the string doesn't
1662  * change if an error is thrown.
1663  */
1664  basic_string&
1665  insert(size_type __pos, size_type __n, _CharT __c)
1666  { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1667  size_type(0), __n, __c); }
1668 
1669  /**
1670  * @brief Insert one character.
1671  * @param __p Iterator referencing position in string to insert at.
1672  * @param __c The character to insert.
1673  * @return Iterator referencing newly inserted char.
1674  * @throw std::length_error If new length exceeds @c max_size().
1675  *
1676  * Inserts character @a __c at position referenced by @a __p.
1677  * If adding character causes the length to exceed max_size(),
1678  * length_error is thrown. If @a __p is beyond end of string,
1679  * out_of_range is thrown. The value of the string doesn't
1680  * change if an error is thrown.
1681  */
1682  iterator
1683  insert(__const_iterator __p, _CharT __c)
1684  {
1685  _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1686  const size_type __pos = __p - begin();
1687  _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1688  return iterator(_M_data() + __pos);
1689  }
1690 
1691 #if __cplusplus > 201402L
1692  /**
1693  * @brief Insert a string_view.
1694  * @param __pos Iterator referencing position in string to insert at.
1695  * @param __sv The string_view to insert.
1696  * @return Reference to this string.
1697  */
1698  basic_string&
1699  insert(size_type __pos, __sv_type __sv)
1700  { return this->insert(__pos, __sv.data(), __sv.size()); }
1701 
1702  /**
1703  * @brief Insert a string_view.
1704  * @param __pos Iterator referencing position in string to insert at.
1705  * @param __sv The string_view to insert from.
1706  * @param __pos Iterator referencing position in string_view to insert
1707  * from.
1708  * @param __n The number of characters to insert.
1709  * @return Reference to this string.
1710  */
1711  template <typename _Tp>
1712  _If_sv<_Tp, basic_string&>
1713  insert(size_type __pos1, const _Tp& __svt,
1714  size_type __pos2, size_type __n = npos)
1715  {
1716  __sv_type __sv = __svt;
1717  return this->replace(__pos1, size_type(0), __sv.data()
1718  + __sv._M_check(__pos2, "basic_string::insert"),
1719  __sv._M_limit(__pos2, __n));
1720  }
1721 #endif // C++17
1722 
1723  /**
1724  * @brief Remove characters.
1725  * @param __pos Index of first character to remove (default 0).
1726  * @param __n Number of characters to remove (default remainder).
1727  * @return Reference to this string.
1728  * @throw std::out_of_range If @a pos is beyond the end of this
1729  * string.
1730  *
1731  * Removes @a __n characters from this string starting at @a
1732  * __pos. The length of the string is reduced by @a __n. If
1733  * there are < @a __n characters to remove, the remainder of
1734  * the string is truncated. If @a __p is beyond end of string,
1735  * out_of_range is thrown. The value of the string doesn't
1736  * change if an error is thrown.
1737  */
1738  basic_string&
1739  erase(size_type __pos = 0, size_type __n = npos)
1740  {
1741  _M_check(__pos, "basic_string::erase");
1742  if (__n == npos)
1743  this->_M_set_length(__pos);
1744  else if (__n != 0)
1745  this->_M_erase(__pos, _M_limit(__pos, __n));
1746  return *this;
1747  }
1748 
1749  /**
1750  * @brief Remove one character.
1751  * @param __position Iterator referencing the character to remove.
1752  * @return iterator referencing same location after removal.
1753  *
1754  * Removes the character at @a __position from this string. The value
1755  * of the string doesn't change if an error is thrown.
1756  */
1757  iterator
1758  erase(__const_iterator __position)
1759  {
1760  _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
1761  && __position < end());
1762  const size_type __pos = __position - begin();
1763  this->_M_erase(__pos, size_type(1));
1764  return iterator(_M_data() + __pos);
1765  }
1766 
1767  /**
1768  * @brief Remove a range of characters.
1769  * @param __first Iterator referencing the first character to remove.
1770  * @param __last Iterator referencing the end of the range.
1771  * @return Iterator referencing location of first after removal.
1772  *
1773  * Removes the characters in the range [first,last) from this string.
1774  * The value of the string doesn't change if an error is thrown.
1775  */
1776  iterator
1777  erase(__const_iterator __first, __const_iterator __last)
1778  {
1779  _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
1780  && __last <= end());
1781  const size_type __pos = __first - begin();
1782  if (__last == end())
1783  this->_M_set_length(__pos);
1784  else
1785  this->_M_erase(__pos, __last - __first);
1786  return iterator(this->_M_data() + __pos);
1787  }
1788 
1789 #if __cplusplus >= 201103L
1790  /**
1791  * @brief Remove the last character.
1792  *
1793  * The string must be non-empty.
1794  */
1795  void
1796  pop_back() noexcept
1797  {
1798  __glibcxx_assert(!empty());
1799  _M_erase(size() - 1, 1);
1800  }
1801 #endif // C++11
1802 
1803  /**
1804  * @brief Replace characters with value from another string.
1805  * @param __pos Index of first character to replace.
1806  * @param __n Number of characters to be replaced.
1807  * @param __str String to insert.
1808  * @return Reference to this string.
1809  * @throw std::out_of_range If @a pos is beyond the end of this
1810  * string.
1811  * @throw std::length_error If new length exceeds @c max_size().
1812  *
1813  * Removes the characters in the range [__pos,__pos+__n) from
1814  * this string. In place, the value of @a __str is inserted.
1815  * If @a __pos is beyond end of string, out_of_range is thrown.
1816  * If the length of the result exceeds max_size(), length_error
1817  * is thrown. The value of the string doesn't change if an
1818  * error is thrown.
1819  */
1820  basic_string&
1821  replace(size_type __pos, size_type __n, const basic_string& __str)
1822  { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1823 
1824  /**
1825  * @brief Replace characters with value from another string.
1826  * @param __pos1 Index of first character to replace.
1827  * @param __n1 Number of characters to be replaced.
1828  * @param __str String to insert.
1829  * @param __pos2 Index of first character of str to use.
1830  * @param __n2 Number of characters from str to use.
1831  * @return Reference to this string.
1832  * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1833  * __str.size().
1834  * @throw std::length_error If new length exceeds @c max_size().
1835  *
1836  * Removes the characters in the range [__pos1,__pos1 + n) from this
1837  * string. In place, the value of @a __str is inserted. If @a __pos is
1838  * beyond end of string, out_of_range is thrown. If the length of the
1839  * result exceeds max_size(), length_error is thrown. The value of the
1840  * string doesn't change if an error is thrown.
1841  */
1842  basic_string&
1843  replace(size_type __pos1, size_type __n1, const basic_string& __str,
1844  size_type __pos2, size_type __n2)
1845  { return this->replace(__pos1, __n1, __str._M_data()
1846  + __str._M_check(__pos2, "basic_string::replace"),
1847  __str._M_limit(__pos2, __n2)); }
1848 
1849  /**
1850  * @brief Replace characters with value of a C substring.
1851  * @param __pos Index of first character to replace.
1852  * @param __n1 Number of characters to be replaced.
1853  * @param __s C string to insert.
1854  * @param __n2 Number of characters from @a s to use.
1855  * @return Reference to this string.
1856  * @throw std::out_of_range If @a pos1 > size().
1857  * @throw std::length_error If new length exceeds @c max_size().
1858  *
1859  * Removes the characters in the range [__pos,__pos + __n1)
1860  * from this string. In place, the first @a __n2 characters of
1861  * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1862  * @a __pos is beyond end of string, out_of_range is thrown. If
1863  * the length of result exceeds max_size(), length_error is
1864  * thrown. The value of the string doesn't change if an error
1865  * is thrown.
1866  */
1867  basic_string&
1868  replace(size_type __pos, size_type __n1, const _CharT* __s,
1869  size_type __n2)
1870  {
1871  __glibcxx_requires_string_len(__s, __n2);
1872  return _M_replace(_M_check(__pos, "basic_string::replace"),
1873  _M_limit(__pos, __n1), __s, __n2);
1874  }
1875 
1876  /**
1877  * @brief Replace characters with value of a C string.
1878  * @param __pos Index of first character to replace.
1879  * @param __n1 Number of characters to be replaced.
1880  * @param __s C string to insert.
1881  * @return Reference to this string.
1882  * @throw std::out_of_range If @a pos > size().
1883  * @throw std::length_error If new length exceeds @c max_size().
1884  *
1885  * Removes the characters in the range [__pos,__pos + __n1)
1886  * from this string. In place, the characters of @a __s are
1887  * inserted. If @a __pos is beyond end of string, out_of_range
1888  * is thrown. If the length of result exceeds max_size(),
1889  * length_error is thrown. The value of the string doesn't
1890  * change if an error is thrown.
1891  */
1892  basic_string&
1893  replace(size_type __pos, size_type __n1, const _CharT* __s)
1894  {
1895  __glibcxx_requires_string(__s);
1896  return this->replace(__pos, __n1, __s, traits_type::length(__s));
1897  }
1898 
1899  /**
1900  * @brief Replace characters with multiple characters.
1901  * @param __pos Index of first character to replace.
1902  * @param __n1 Number of characters to be replaced.
1903  * @param __n2 Number of characters to insert.
1904  * @param __c Character to insert.
1905  * @return Reference to this string.
1906  * @throw std::out_of_range If @a __pos > size().
1907  * @throw std::length_error If new length exceeds @c max_size().
1908  *
1909  * Removes the characters in the range [pos,pos + n1) from this
1910  * string. In place, @a __n2 copies of @a __c are inserted.
1911  * If @a __pos is beyond end of string, out_of_range is thrown.
1912  * If the length of result exceeds max_size(), length_error is
1913  * thrown. The value of the string doesn't change if an error
1914  * is thrown.
1915  */
1916  basic_string&
1917  replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1918  { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1919  _M_limit(__pos, __n1), __n2, __c); }
1920 
1921  /**
1922  * @brief Replace range of characters with string.
1923  * @param __i1 Iterator referencing start of range to replace.
1924  * @param __i2 Iterator referencing end of range to replace.
1925  * @param __str String value to insert.
1926  * @return Reference to this string.
1927  * @throw std::length_error If new length exceeds @c max_size().
1928  *
1929  * Removes the characters in the range [__i1,__i2). In place,
1930  * the value of @a __str is inserted. If the length of result
1931  * exceeds max_size(), length_error is thrown. The value of
1932  * the string doesn't change if an error is thrown.
1933  */
1934  basic_string&
1935  replace(__const_iterator __i1, __const_iterator __i2,
1936  const basic_string& __str)
1937  { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1938 
1939  /**
1940  * @brief Replace range of characters with C substring.
1941  * @param __i1 Iterator referencing start of range to replace.
1942  * @param __i2 Iterator referencing end of range to replace.
1943  * @param __s C string value to insert.
1944  * @param __n Number of characters from s to insert.
1945  * @return Reference to this string.
1946  * @throw std::length_error If new length exceeds @c max_size().
1947  *
1948  * Removes the characters in the range [__i1,__i2). In place,
1949  * the first @a __n characters of @a __s are inserted. If the
1950  * length of result exceeds max_size(), length_error is thrown.
1951  * The value of the string doesn't change if an error is
1952  * thrown.
1953  */
1954  basic_string&
1955  replace(__const_iterator __i1, __const_iterator __i2,
1956  const _CharT* __s, size_type __n)
1957  {
1958  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
1959  && __i2 <= end());
1960  return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
1961  }
1962 
1963  /**
1964  * @brief Replace range of characters with C string.
1965  * @param __i1 Iterator referencing start of range to replace.
1966  * @param __i2 Iterator referencing end of range to replace.
1967  * @param __s C string value to insert.
1968  * @return Reference to this string.
1969  * @throw std::length_error If new length exceeds @c max_size().
1970  *
1971  * Removes the characters in the range [__i1,__i2). In place,
1972  * the characters of @a __s are inserted. If the length of
1973  * result exceeds max_size(), length_error is thrown. The
1974  * value of the string doesn't change if an error is thrown.
1975  */
1976  basic_string&
1977  replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
1978  {
1979  __glibcxx_requires_string(__s);
1980  return this->replace(__i1, __i2, __s, traits_type::length(__s));
1981  }
1982 
1983  /**
1984  * @brief Replace range of characters with multiple characters
1985  * @param __i1 Iterator referencing start of range to replace.
1986  * @param __i2 Iterator referencing end of range to replace.
1987  * @param __n Number of characters to insert.
1988  * @param __c Character to insert.
1989  * @return Reference to this string.
1990  * @throw std::length_error If new length exceeds @c max_size().
1991  *
1992  * Removes the characters in the range [__i1,__i2). In place,
1993  * @a __n copies of @a __c are inserted. If the length of
1994  * result exceeds max_size(), length_error is thrown. The
1995  * value of the string doesn't change if an error is thrown.
1996  */
1997  basic_string&
1998  replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
1999  _CharT __c)
2000  {
2001  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2002  && __i2 <= end());
2003  return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
2004  }
2005 
2006  /**
2007  * @brief Replace range of characters with range.
2008  * @param __i1 Iterator referencing start of range to replace.
2009  * @param __i2 Iterator referencing end of range to replace.
2010  * @param __k1 Iterator referencing start of range to insert.
2011  * @param __k2 Iterator referencing end of range to insert.
2012  * @return Reference to this string.
2013  * @throw std::length_error If new length exceeds @c max_size().
2014  *
2015  * Removes the characters in the range [__i1,__i2). In place,
2016  * characters in the range [__k1,__k2) are inserted. If the
2017  * length of result exceeds max_size(), length_error is thrown.
2018  * The value of the string doesn't change if an error is
2019  * thrown.
2020  */
2021 #if __cplusplus >= 201103L
2022  template<class _InputIterator,
2023  typename = std::_RequireInputIter<_InputIterator>>
2024  basic_string&
2025  replace(const_iterator __i1, const_iterator __i2,
2026  _InputIterator __k1, _InputIterator __k2)
2027  {
2028  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2029  && __i2 <= end());
2030  __glibcxx_requires_valid_range(__k1, __k2);
2031  return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
2032  std::__false_type());
2033  }
2034 #else
2035  template<class _InputIterator>
2036 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2037  typename __enable_if_not_native_iterator<_InputIterator>::__type
2038 #else
2039  basic_string&
2040 #endif
2041  replace(iterator __i1, iterator __i2,
2042  _InputIterator __k1, _InputIterator __k2)
2043  {
2044  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2045  && __i2 <= end());
2046  __glibcxx_requires_valid_range(__k1, __k2);
2047  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2048  return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2049  }
2050 #endif
2051 
2052  // Specializations for the common case of pointer and iterator:
2053  // useful to avoid the overhead of temporary buffering in _M_replace.
2054  basic_string&
2055  replace(__const_iterator __i1, __const_iterator __i2,
2056  _CharT* __k1, _CharT* __k2)
2057  {
2058  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2059  && __i2 <= end());
2060  __glibcxx_requires_valid_range(__k1, __k2);
2061  return this->replace(__i1 - begin(), __i2 - __i1,
2062  __k1, __k2 - __k1);
2063  }
2064 
2065  basic_string&
2066  replace(__const_iterator __i1, __const_iterator __i2,
2067  const _CharT* __k1, const _CharT* __k2)
2068  {
2069  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2070  && __i2 <= end());
2071  __glibcxx_requires_valid_range(__k1, __k2);
2072  return this->replace(__i1 - begin(), __i2 - __i1,
2073  __k1, __k2 - __k1);
2074  }
2075 
2076  basic_string&
2077  replace(__const_iterator __i1, __const_iterator __i2,
2078  iterator __k1, iterator __k2)
2079  {
2080  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2081  && __i2 <= end());
2082  __glibcxx_requires_valid_range(__k1, __k2);
2083  return this->replace(__i1 - begin(), __i2 - __i1,
2084  __k1.base(), __k2 - __k1);
2085  }
2086 
2087  basic_string&
2088  replace(__const_iterator __i1, __const_iterator __i2,
2089  const_iterator __k1, const_iterator __k2)
2090  {
2091  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2092  && __i2 <= end());
2093  __glibcxx_requires_valid_range(__k1, __k2);
2094  return this->replace(__i1 - begin(), __i2 - __i1,
2095  __k1.base(), __k2 - __k1);
2096  }
2097 
2098 #if __cplusplus >= 201103L
2099  /**
2100  * @brief Replace range of characters with initializer_list.
2101  * @param __i1 Iterator referencing start of range to replace.
2102  * @param __i2 Iterator referencing end of range to replace.
2103  * @param __l The initializer_list of characters to insert.
2104  * @return Reference to this string.
2105  * @throw std::length_error If new length exceeds @c max_size().
2106  *
2107  * Removes the characters in the range [__i1,__i2). In place,
2108  * characters in the range [__k1,__k2) are inserted. If the
2109  * length of result exceeds max_size(), length_error is thrown.
2110  * The value of the string doesn't change if an error is
2111  * thrown.
2112  */
2113  basic_string& replace(const_iterator __i1, const_iterator __i2,
2114  initializer_list<_CharT> __l)
2115  { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
2116 #endif // C++11
2117 
2118 #if __cplusplus > 201402L
2119  /**
2120  * @brief Replace range of characters with string_view.
2121  * @param __pos The position to replace at.
2122  * @param __n The number of characters to replace.
2123  * @param __sv The string_view to insert.
2124  * @return Reference to this string.
2125  */
2126  basic_string&
2127  replace(size_type __pos, size_type __n, __sv_type __sv)
2128  { return this->replace(__pos, __n, __sv.data(), __sv.size()); }
2129 
2130  /**
2131  * @brief Replace range of characters with string_view.
2132  * @param __pos1 The position to replace at.
2133  * @param __n1 The number of characters to replace.
2134  * @param __sv The string_view to insert from.
2135  * @param __pos2 The position in the string_view to insert from.
2136  * @param __n2 The number of characters to insert.
2137  * @return Reference to this string.
2138  */
2139  template <typename _Tp>
2140  _If_sv<_Tp, basic_string&>
2141  replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2142  size_type __pos2, size_type __n2 = npos)
2143  {
2144  __sv_type __sv = __svt;
2145  return this->replace(__pos1, __n1, __sv.data()
2146  + __sv._M_check(__pos2, "basic_string::replace"),
2147  __sv._M_limit(__pos2, __n2));
2148  }
2149 
2150  /**
2151  * @brief Replace range of characters with string_view.
2152  * @param __i1 An iterator referencing the start position
2153  to replace at.
2154  * @param __i2 An iterator referencing the end position
2155  for the replace.
2156  * @param __sv The string_view to insert from.
2157  * @return Reference to this string.
2158  */
2159  basic_string&
2160  replace(const_iterator __i1, const_iterator __i2, __sv_type __sv)
2161  { return this->replace(__i1 - begin(), __i2 - __i1, __sv); }
2162 #endif // C++17
2163 
2164  private:
2165  template<class _Integer>
2166  basic_string&
2167  _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2168  _Integer __n, _Integer __val, __true_type)
2169  { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
2170 
2171  template<class _InputIterator>
2172  basic_string&
2173  _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2174  _InputIterator __k1, _InputIterator __k2,
2175  __false_type);
2176 
2177  basic_string&
2178  _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2179  _CharT __c);
2180 
2181  basic_string&
2182  _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
2183  const size_type __len2);
2184 
2185  basic_string&
2186  _M_append(const _CharT* __s, size_type __n);
2187 
2188  public:
2189 
2190  /**
2191  * @brief Copy substring into C string.
2192  * @param __s C string to copy value into.
2193  * @param __n Number of characters to copy.
2194  * @param __pos Index of first character to copy.
2195  * @return Number of characters actually copied
2196  * @throw std::out_of_range If __pos > size().
2197  *
2198  * Copies up to @a __n characters starting at @a __pos into the
2199  * C string @a __s. If @a __pos is %greater than size(),
2200  * out_of_range is thrown.
2201  */
2202  size_type
2203  copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2204 
2205  /**
2206  * @brief Swap contents with another string.
2207  * @param __s String to swap with.
2208  *
2209  * Exchanges the contents of this string with that of @a __s in constant
2210  * time.
2211  */
2212  void
2213  swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
2214 
2215  // String operations:
2216  /**
2217  * @brief Return const pointer to null-terminated contents.
2218  *
2219  * This is a handle to internal data. Do not modify or dire things may
2220  * happen.
2221  */
2222  const _CharT*
2223  c_str() const _GLIBCXX_NOEXCEPT
2224  { return _M_data(); }
2225 
2226  /**
2227  * @brief Return const pointer to contents.
2228  *
2229  * This is a pointer to internal data. It is undefined to modify
2230  * the contents through the returned pointer. To get a pointer that
2231  * allows modifying the contents use @c &str[0] instead,
2232  * (or in C++17 the non-const @c str.data() overload).
2233  */
2234  const _CharT*
2235  data() const _GLIBCXX_NOEXCEPT
2236  { return _M_data(); }
2237 
2238 #if __cplusplus > 201402L
2239  /**
2240  * @brief Return non-const pointer to contents.
2241  *
2242  * This is a pointer to the character sequence held by the string.
2243  * Modifying the characters in the sequence is allowed.
2244  */
2245  _CharT*
2246  data() noexcept
2247  { return _M_data(); }
2248 #endif
2249 
2250  /**
2251  * @brief Return copy of allocator used to construct this string.
2252  */
2253  allocator_type
2254  get_allocator() const _GLIBCXX_NOEXCEPT
2255  { return _M_get_allocator(); }
2256 
2257  /**
2258  * @brief Find position of a C substring.
2259  * @param __s C string to locate.
2260  * @param __pos Index of character to search from.
2261  * @param __n Number of characters from @a s to search for.
2262  * @return Index of start of first occurrence.
2263  *
2264  * Starting from @a __pos, searches forward for the first @a
2265  * __n characters in @a __s within this string. If found,
2266  * returns the index where it begins. If not found, returns
2267  * npos.
2268  */
2269  size_type
2270  find(const _CharT* __s, size_type __pos, size_type __n) const
2271  _GLIBCXX_NOEXCEPT;
2272 
2273  /**
2274  * @brief Find position of a string.
2275  * @param __str String to locate.
2276  * @param __pos Index of character to search from (default 0).
2277  * @return Index of start of first occurrence.
2278  *
2279  * Starting from @a __pos, searches forward for value of @a __str within
2280  * this string. If found, returns the index where it begins. If not
2281  * found, returns npos.
2282  */
2283  size_type
2284  find(const basic_string& __str, size_type __pos = 0) const
2285  _GLIBCXX_NOEXCEPT
2286  { return this->find(__str.data(), __pos, __str.size()); }
2287 
2288 #if __cplusplus > 201402L
2289  /**
2290  * @brief Find position of a string_view.
2291  * @param __sv The string_view to locate.
2292  * @param __pos Index of character to search from (default 0).
2293  * @return Index of start of first occurrence.
2294  */
2295  size_type
2296  find(__sv_type __sv, size_type __pos = 0) const noexcept
2297  { return this->find(__sv.data(), __pos, __sv.size()); }
2298 #endif // C++17
2299 
2300  /**
2301  * @brief Find position of a C string.
2302  * @param __s C string to locate.
2303  * @param __pos Index of character to search from (default 0).
2304  * @return Index of start of first occurrence.
2305  *
2306  * Starting from @a __pos, searches forward for the value of @a
2307  * __s within this string. If found, returns the index where
2308  * it begins. If not found, returns npos.
2309  */
2310  size_type
2311  find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2312  {
2313  __glibcxx_requires_string(__s);
2314  return this->find(__s, __pos, traits_type::length(__s));
2315  }
2316 
2317  /**
2318  * @brief Find position of a character.
2319  * @param __c Character to locate.
2320  * @param __pos Index of character to search from (default 0).
2321  * @return Index of first occurrence.
2322  *
2323  * Starting from @a __pos, searches forward for @a __c within
2324  * this string. If found, returns the index where it was
2325  * found. If not found, returns npos.
2326  */
2327  size_type
2328  find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
2329 
2330  /**
2331  * @brief Find last position of a string.
2332  * @param __str String to locate.
2333  * @param __pos Index of character to search back from (default end).
2334  * @return Index of start of last occurrence.
2335  *
2336  * Starting from @a __pos, searches backward for value of @a
2337  * __str within this string. If found, returns the index where
2338  * it begins. If not found, returns npos.
2339  */
2340  size_type
2341  rfind(const basic_string& __str, size_type __pos = npos) const
2342  _GLIBCXX_NOEXCEPT
2343  { return this->rfind(__str.data(), __pos, __str.size()); }
2344 
2345 #if __cplusplus > 201402L
2346  /**
2347  * @brief Find last position of a string_view.
2348  * @param __sv The string_view to locate.
2349  * @param __pos Index of character to search back from (default end).
2350  * @return Index of start of last occurrence.
2351  */
2352  size_type
2353  rfind(__sv_type __sv, size_type __pos = npos) const noexcept
2354  { return this->rfind(__sv.data(), __pos, __sv.size()); }
2355 #endif // C++17
2356 
2357  /**
2358  * @brief Find last position of a C substring.
2359  * @param __s C string to locate.
2360  * @param __pos Index of character to search back from.
2361  * @param __n Number of characters from s to search for.
2362  * @return Index of start of last occurrence.
2363  *
2364  * Starting from @a __pos, searches backward for the first @a
2365  * __n characters in @a __s within this string. If found,
2366  * returns the index where it begins. If not found, returns
2367  * npos.
2368  */
2369  size_type
2370  rfind(const _CharT* __s, size_type __pos, size_type __n) const
2371  _GLIBCXX_NOEXCEPT;
2372 
2373  /**
2374  * @brief Find last position of a C string.
2375  * @param __s C string to locate.
2376  * @param __pos Index of character to start search at (default end).
2377  * @return Index of start of last occurrence.
2378  *
2379  * Starting from @a __pos, searches backward for the value of
2380  * @a __s within this string. If found, returns the index
2381  * where it begins. If not found, returns npos.
2382  */
2383  size_type
2384  rfind(const _CharT* __s, size_type __pos = npos) const
2385  {
2386  __glibcxx_requires_string(__s);
2387  return this->rfind(__s, __pos, traits_type::length(__s));
2388  }
2389 
2390  /**
2391  * @brief Find last position of a character.
2392  * @param __c Character to locate.
2393  * @param __pos Index of character to search back from (default end).
2394  * @return Index of last occurrence.
2395  *
2396  * Starting from @a __pos, searches backward for @a __c within
2397  * this string. If found, returns the index where it was
2398  * found. If not found, returns npos.
2399  */
2400  size_type
2401  rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
2402 
2403  /**
2404  * @brief Find position of a character of string.
2405  * @param __str String containing characters to locate.
2406  * @param __pos Index of character to search from (default 0).
2407  * @return Index of first occurrence.
2408  *
2409  * Starting from @a __pos, searches forward for one of the
2410  * characters of @a __str within this string. If found,
2411  * returns the index where it was found. If not found, returns
2412  * npos.
2413  */
2414  size_type
2415  find_first_of(const basic_string& __str, size_type __pos = 0) const
2416  _GLIBCXX_NOEXCEPT
2417  { return this->find_first_of(__str.data(), __pos, __str.size()); }
2418 
2419 #if __cplusplus > 201402L
2420  /**
2421  * @brief Find position of a character of a string_view.
2422  * @param __sv A string_view containing characters to locate.
2423  * @param __pos Index of character to search from (default 0).
2424  * @return Index of first occurrence.
2425  */
2426  size_type
2427  find_first_of(__sv_type __sv, size_type __pos = 0) const noexcept
2428  { return this->find_first_of(__sv.data(), __pos, __sv.size()); }
2429 #endif // C++17
2430 
2431  /**
2432  * @brief Find position of a character of C substring.
2433  * @param __s String containing characters to locate.
2434  * @param __pos Index of character to search from.
2435  * @param __n Number of characters from s to search for.
2436  * @return Index of first occurrence.
2437  *
2438  * Starting from @a __pos, searches forward for one of the
2439  * first @a __n characters of @a __s within this string. If
2440  * found, returns the index where it was found. If not found,
2441  * returns npos.
2442  */
2443  size_type
2444  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
2445  _GLIBCXX_NOEXCEPT;
2446 
2447  /**
2448  * @brief Find position of a character of C string.
2449  * @param __s String containing characters to locate.
2450  * @param __pos Index of character to search from (default 0).
2451  * @return Index of first occurrence.
2452  *
2453  * Starting from @a __pos, searches forward for one of the
2454  * characters of @a __s within this string. If found, returns
2455  * the index where it was found. If not found, returns npos.
2456  */
2457  size_type
2458  find_first_of(const _CharT* __s, size_type __pos = 0) const
2459  _GLIBCXX_NOEXCEPT
2460  {
2461  __glibcxx_requires_string(__s);
2462  return this->find_first_of(__s, __pos, traits_type::length(__s));
2463  }
2464 
2465  /**
2466  * @brief Find position of a character.
2467  * @param __c Character to locate.
2468  * @param __pos Index of character to search from (default 0).
2469  * @return Index of first occurrence.
2470  *
2471  * Starting from @a __pos, searches forward for the character
2472  * @a __c within this string. If found, returns the index
2473  * where it was found. If not found, returns npos.
2474  *
2475  * Note: equivalent to find(__c, __pos).
2476  */
2477  size_type
2478  find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2479  { return this->find(__c, __pos); }
2480 
2481  /**
2482  * @brief Find last position of a character of string.
2483  * @param __str String containing characters to locate.
2484  * @param __pos Index of character to search back from (default end).
2485  * @return Index of last occurrence.
2486  *
2487  * Starting from @a __pos, searches backward for one of the
2488  * characters of @a __str within this string. If found,
2489  * returns the index where it was found. If not found, returns
2490  * npos.
2491  */
2492  size_type
2493  find_last_of(const basic_string& __str, size_type __pos = npos) const
2494  _GLIBCXX_NOEXCEPT
2495  { return this->find_last_of(__str.data(), __pos, __str.size()); }
2496 
2497 #if __cplusplus > 201402L
2498  /**
2499  * @brief Find last position of a character of string.
2500  * @param __sv A string_view containing characters to locate.
2501  * @param __pos Index of character to search back from (default end).
2502  * @return Index of last occurrence.
2503  */
2504  size_type
2505  find_last_of(__sv_type __sv, size_type __pos = npos) const noexcept
2506  { return this->find_last_of(__sv.data(), __pos, __sv.size()); }
2507 #endif // C++17
2508 
2509  /**
2510  * @brief Find last position of a character of C substring.
2511  * @param __s C string containing characters to locate.
2512  * @param __pos Index of character to search back from.
2513  * @param __n Number of characters from s to search for.
2514  * @return Index of last occurrence.
2515  *
2516  * Starting from @a __pos, searches backward for one of the
2517  * first @a __n characters of @a __s within this string. If
2518  * found, returns the index where it was found. If not found,
2519  * returns npos.
2520  */
2521  size_type
2522  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
2523  _GLIBCXX_NOEXCEPT;
2524 
2525  /**
2526  * @brief Find last position of a character of C string.
2527  * @param __s C string containing characters to locate.
2528  * @param __pos Index of character to search back from (default end).
2529  * @return Index of last occurrence.
2530  *
2531  * Starting from @a __pos, searches backward for one of the
2532  * characters of @a __s within this string. If found, returns
2533  * the index where it was found. If not found, returns npos.
2534  */
2535  size_type
2536  find_last_of(const _CharT* __s, size_type __pos = npos) const
2537  _GLIBCXX_NOEXCEPT
2538  {
2539  __glibcxx_requires_string(__s);
2540  return this->find_last_of(__s, __pos, traits_type::length(__s));
2541  }
2542 
2543  /**
2544  * @brief Find last position of a character.
2545  * @param __c Character to locate.
2546  * @param __pos Index of character to search back from (default end).
2547  * @return Index of last occurrence.
2548  *
2549  * Starting from @a __pos, searches backward for @a __c within
2550  * this string. If found, returns the index where it was
2551  * found. If not found, returns npos.
2552  *
2553  * Note: equivalent to rfind(__c, __pos).
2554  */
2555  size_type
2556  find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2557  { return this->rfind(__c, __pos); }
2558 
2559  /**
2560  * @brief Find position of a character not in string.
2561  * @param __str String containing characters to avoid.
2562  * @param __pos Index of character to search from (default 0).
2563  * @return Index of first occurrence.
2564  *
2565  * Starting from @a __pos, searches forward for a character not contained
2566  * in @a __str within this string. If found, returns the index where it
2567  * was found. If not found, returns npos.
2568  */
2569  size_type
2570  find_first_not_of(const basic_string& __str, size_type __pos = 0) const
2571  _GLIBCXX_NOEXCEPT
2572  { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2573 
2574 #if __cplusplus > 201402L
2575  /**
2576  * @brief Find position of a character not in a string_view.
2577  * @param __sv A string_view containing characters to avoid.
2578  * @param __pos Index of character to search from (default 0).
2579  * @return Index of first occurrence.
2580  */
2581  size_type
2582  find_first_not_of(__sv_type __sv, size_type __pos = 0) const noexcept
2583  { return this->find_first_not_of(__sv.data(), __pos, __sv.size()); }
2584 #endif // C++17
2585 
2586  /**
2587  * @brief Find position of a character not in C substring.
2588  * @param __s C string containing characters to avoid.
2589  * @param __pos Index of character to search from.
2590  * @param __n Number of characters from __s to consider.
2591  * @return Index of first occurrence.
2592  *
2593  * Starting from @a __pos, searches forward for a character not
2594  * contained in the first @a __n characters of @a __s within
2595  * this string. If found, returns the index where it was
2596  * found. If not found, returns npos.
2597  */
2598  size_type
2599  find_first_not_of(const _CharT* __s, size_type __pos,
2600  size_type __n) const _GLIBCXX_NOEXCEPT;
2601 
2602  /**
2603  * @brief Find position of a character not in C string.
2604  * @param __s C string containing characters to avoid.
2605  * @param __pos Index of character to search from (default 0).
2606  * @return Index of first occurrence.
2607  *
2608  * Starting from @a __pos, searches forward for a character not
2609  * contained in @a __s within this string. If found, returns
2610  * the index where it was found. If not found, returns npos.
2611  */
2612  size_type
2613  find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2614  _GLIBCXX_NOEXCEPT
2615  {
2616  __glibcxx_requires_string(__s);
2617  return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2618  }
2619 
2620  /**
2621  * @brief Find position of a different character.
2622  * @param __c Character to avoid.
2623  * @param __pos Index of character to search from (default 0).
2624  * @return Index of first occurrence.
2625  *
2626  * Starting from @a __pos, searches forward for a character
2627  * other than @a __c within this string. If found, returns the
2628  * index where it was found. If not found, returns npos.
2629  */
2630  size_type
2631  find_first_not_of(_CharT __c, size_type __pos = 0) const
2632  _GLIBCXX_NOEXCEPT;
2633 
2634  /**
2635  * @brief Find last position of a character not in string.
2636  * @param __str String containing characters to avoid.
2637  * @param __pos Index of character to search back from (default end).
2638  * @return Index of last occurrence.
2639  *
2640  * Starting from @a __pos, searches backward for a character
2641  * not contained in @a __str within this string. If found,
2642  * returns the index where it was found. If not found, returns
2643  * npos.
2644  */
2645  size_type
2646  find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2647  _GLIBCXX_NOEXCEPT
2648  { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2649 
2650 #if __cplusplus > 201402L
2651  /**
2652  * @brief Find last position of a character not in a string_view.
2653  * @param __sv A string_view containing characters to avoid.
2654  * @param __pos Index of character to search back from (default end).
2655  * @return Index of last occurrence.
2656  */
2657  size_type
2658  find_last_not_of(__sv_type __sv, size_type __pos = npos) const noexcept
2659  { return this->find_last_not_of(__sv.data(), __pos, __sv.size()); }
2660 #endif // C++17
2661 
2662  /**
2663  * @brief Find last position of a character not in C substring.
2664  * @param __s C string containing characters to avoid.
2665  * @param __pos Index of character to search back from.
2666  * @param __n Number of characters from s to consider.
2667  * @return Index of last occurrence.
2668  *
2669  * Starting from @a __pos, searches backward for a character not
2670  * contained in the first @a __n characters of @a __s within this string.
2671  * If found, returns the index where it was found. If not found,
2672  * returns npos.
2673  */
2674  size_type
2675  find_last_not_of(const _CharT* __s, size_type __pos,
2676  size_type __n) const _GLIBCXX_NOEXCEPT;
2677  /**
2678  * @brief Find last position of a character not in C string.
2679  * @param __s C string containing characters to avoid.
2680  * @param __pos Index of character to search back from (default end).
2681  * @return Index of last occurrence.
2682  *
2683  * Starting from @a __pos, searches backward for a character
2684  * not contained in @a __s within this string. If found,
2685  * returns the index where it was found. If not found, returns
2686  * npos.
2687  */
2688  size_type
2689  find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2690  _GLIBCXX_NOEXCEPT
2691  {
2692  __glibcxx_requires_string(__s);
2693  return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2694  }
2695 
2696  /**
2697  * @brief Find last position of a different character.
2698  * @param __c Character to avoid.
2699  * @param __pos Index of character to search back from (default end).
2700  * @return Index of last occurrence.
2701  *
2702  * Starting from @a __pos, searches backward for a character other than
2703  * @a __c within this string. If found, returns the index where it was
2704  * found. If not found, returns npos.
2705  */
2706  size_type
2707  find_last_not_of(_CharT __c, size_type __pos = npos) const
2708  _GLIBCXX_NOEXCEPT;
2709 
2710  /**
2711  * @brief Get a substring.
2712  * @param __pos Index of first character (default 0).
2713  * @param __n Number of characters in substring (default remainder).
2714  * @return The new string.
2715  * @throw std::out_of_range If __pos > size().
2716  *
2717  * Construct and return a new string using the @a __n
2718  * characters starting at @a __pos. If the string is too
2719  * short, use the remainder of the characters. If @a __pos is
2720  * beyond the end of the string, out_of_range is thrown.
2721  */
2722  basic_string
2723  substr(size_type __pos = 0, size_type __n = npos) const
2724  { return basic_string(*this,
2725  _M_check(__pos, "basic_string::substr"), __n); }
2726 
2727  /**
2728  * @brief Compare to a string.
2729  * @param __str String to compare against.
2730  * @return Integer < 0, 0, or > 0.
2731  *
2732  * Returns an integer < 0 if this string is ordered before @a
2733  * __str, 0 if their values are equivalent, or > 0 if this
2734  * string is ordered after @a __str. Determines the effective
2735  * length rlen of the strings to compare as the smallest of
2736  * size() and str.size(). The function then compares the two
2737  * strings by calling traits::compare(data(), str.data(),rlen).
2738  * If the result of the comparison is nonzero returns it,
2739  * otherwise the shorter one is ordered first.
2740  */
2741  int
2742  compare(const basic_string& __str) const
2743  {
2744  const size_type __size = this->size();
2745  const size_type __osize = __str.size();
2746  const size_type __len = std::min(__size, __osize);
2747 
2748  int __r = traits_type::compare(_M_data(), __str.data(), __len);
2749  if (!__r)
2750  __r = _S_compare(__size, __osize);
2751  return __r;
2752  }
2753 
2754 #if __cplusplus > 201402L
2755  /**
2756  * @brief Compare to a string_view.
2757  * @param __sv A string_view to compare against.
2758  * @return Integer < 0, 0, or > 0.
2759  */
2760  int
2761  compare(__sv_type __sv) const
2762  {
2763  const size_type __size = this->size();
2764  const size_type __osize = __sv.size();
2765  const size_type __len = std::min(__size, __osize);
2766 
2767  int __r = traits_type::compare(_M_data(), __sv.data(), __len);
2768  if (!__r)
2769  __r = _S_compare(__size, __osize);
2770  return __r;
2771  }
2772 
2773  /**
2774  * @brief Compare to a string_view.
2775  * @param __pos A position in the string to start comparing from.
2776  * @param __n The number of characters to compare.
2777  * @param __sv A string_view to compare against.
2778  * @return Integer < 0, 0, or > 0.
2779  */
2780  int
2781  compare(size_type __pos, size_type __n, __sv_type __sv) const
2782  { return __sv_type(*this).substr(__pos, __n).compare(__sv); }
2783 
2784  /**
2785  * @brief Compare to a string_view.
2786  * @param __pos1 A position in the string to start comparing from.
2787  * @param __n1 The number of characters to compare.
2788  * @param __sv A string_view to compare against.
2789  * @param __pos2 A position in the string_view to start comparing from.
2790  * @param __n2 The number of characters to compare.
2791  * @return Integer < 0, 0, or > 0.
2792  */
2793  template <typename _Tp>
2794  _If_sv<_Tp, int>
2795  compare(size_type __pos1, size_type __n1, const _Tp& __svt,
2796  size_type __pos2, size_type __n2 = npos) const
2797  {
2798  __sv_type __sv = __svt;
2799  return __sv_type(*this)
2800  .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
2801  }
2802 #endif // C++17
2803 
2804  /**
2805  * @brief Compare substring to a string.
2806  * @param __pos Index of first character of substring.
2807  * @param __n Number of characters in substring.
2808  * @param __str String to compare against.
2809  * @return Integer < 0, 0, or > 0.
2810  *
2811  * Form the substring of this string from the @a __n characters
2812  * starting at @a __pos. Returns an integer < 0 if the
2813  * substring is ordered before @a __str, 0 if their values are
2814  * equivalent, or > 0 if the substring is ordered after @a
2815  * __str. Determines the effective length rlen of the strings
2816  * to compare as the smallest of the length of the substring
2817  * and @a __str.size(). The function then compares the two
2818  * strings by calling
2819  * traits::compare(substring.data(),str.data(),rlen). If the
2820  * result of the comparison is nonzero returns it, otherwise
2821  * the shorter one is ordered first.
2822  */
2823  int
2824  compare(size_type __pos, size_type __n, const basic_string& __str) const;
2825 
2826  /**
2827  * @brief Compare substring to a substring.
2828  * @param __pos1 Index of first character of substring.
2829  * @param __n1 Number of characters in substring.
2830  * @param __str String to compare against.
2831  * @param __pos2 Index of first character of substring of str.
2832  * @param __n2 Number of characters in substring of str.
2833  * @return Integer < 0, 0, or > 0.
2834  *
2835  * Form the substring of this string from the @a __n1
2836  * characters starting at @a __pos1. Form the substring of @a
2837  * __str from the @a __n2 characters starting at @a __pos2.
2838  * Returns an integer < 0 if this substring is ordered before
2839  * the substring of @a __str, 0 if their values are equivalent,
2840  * or > 0 if this substring is ordered after the substring of
2841  * @a __str. Determines the effective length rlen of the
2842  * strings to compare as the smallest of the lengths of the
2843  * substrings. The function then compares the two strings by
2844  * calling
2845  * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2846  * If the result of the comparison is nonzero returns it,
2847  * otherwise the shorter one is ordered first.
2848  */
2849  int
2850  compare(size_type __pos1, size_type __n1, const basic_string& __str,
2851  size_type __pos2, size_type __n2) const;
2852 
2853  /**
2854  * @brief Compare to a C string.
2855  * @param __s C string to compare against.
2856  * @return Integer < 0, 0, or > 0.
2857  *
2858  * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2859  * their values are equivalent, or > 0 if this string is ordered after
2860  * @a __s. Determines the effective length rlen of the strings to
2861  * compare as the smallest of size() and the length of a string
2862  * constructed from @a __s. The function then compares the two strings
2863  * by calling traits::compare(data(),s,rlen). If the result of the
2864  * comparison is nonzero returns it, otherwise the shorter one is
2865  * ordered first.
2866  */
2867  int
2868  compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
2869 
2870  // _GLIBCXX_RESOLVE_LIB_DEFECTS
2871  // 5 String::compare specification questionable
2872  /**
2873  * @brief Compare substring to a C string.
2874  * @param __pos Index of first character of substring.
2875  * @param __n1 Number of characters in substring.
2876  * @param __s C string to compare against.
2877  * @return Integer < 0, 0, or > 0.
2878  *
2879  * Form the substring of this string from the @a __n1
2880  * characters starting at @a pos. Returns an integer < 0 if
2881  * the substring is ordered before @a __s, 0 if their values
2882  * are equivalent, or > 0 if the substring is ordered after @a
2883  * __s. Determines the effective length rlen of the strings to
2884  * compare as the smallest of the length of the substring and
2885  * the length of a string constructed from @a __s. The
2886  * function then compares the two string by calling
2887  * traits::compare(substring.data(),__s,rlen). If the result of
2888  * the comparison is nonzero returns it, otherwise the shorter
2889  * one is ordered first.
2890  */
2891  int
2892  compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2893 
2894  /**
2895  * @brief Compare substring against a character %array.
2896  * @param __pos Index of first character of substring.
2897  * @param __n1 Number of characters in substring.
2898  * @param __s character %array to compare against.
2899  * @param __n2 Number of characters of s.
2900  * @return Integer < 0, 0, or > 0.
2901  *
2902  * Form the substring of this string from the @a __n1
2903  * characters starting at @a __pos. Form a string from the
2904  * first @a __n2 characters of @a __s. Returns an integer < 0
2905  * if this substring is ordered before the string from @a __s,
2906  * 0 if their values are equivalent, or > 0 if this substring
2907  * is ordered after the string from @a __s. Determines the
2908  * effective length rlen of the strings to compare as the
2909  * smallest of the length of the substring and @a __n2. The
2910  * function then compares the two strings by calling
2911  * traits::compare(substring.data(),s,rlen). If the result of
2912  * the comparison is nonzero returns it, otherwise the shorter
2913  * one is ordered first.
2914  *
2915  * NB: s must have at least n2 characters, &apos;\\0&apos; has
2916  * no special meaning.
2917  */
2918  int
2919  compare(size_type __pos, size_type __n1, const _CharT* __s,
2920  size_type __n2) const;
2921 
2922  // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
2923  template<typename, typename, typename> friend class basic_stringbuf;
2924  };
2925 _GLIBCXX_END_NAMESPACE_CXX11
2926 #else // !_GLIBCXX_USE_CXX11_ABI
2927  // Reference-counted COW string implentation
2928 
2929  /**
2930  * @class basic_string basic_string.h <string>
2931  * @brief Managing sequences of characters and character-like objects.
2932  *
2933  * @ingroup strings
2934  * @ingroup sequences
2935  *
2936  * @tparam _CharT Type of character
2937  * @tparam _Traits Traits for character type, defaults to
2938  * char_traits<_CharT>.
2939  * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
2940  *
2941  * Meets the requirements of a <a href="tables.html#65">container</a>, a
2942  * <a href="tables.html#66">reversible container</a>, and a
2943  * <a href="tables.html#67">sequence</a>. Of the
2944  * <a href="tables.html#68">optional sequence requirements</a>, only
2945  * @c push_back, @c at, and @c %array access are supported.
2946  *
2947  * @doctodo
2948  *
2949  *
2950  * Documentation? What's that?
2951  * Nathan Myers <ncm@cantrip.org>.
2952  *
2953  * A string looks like this:
2954  *
2955  * @code
2956  * [_Rep]
2957  * _M_length
2958  * [basic_string<char_type>] _M_capacity
2959  * _M_dataplus _M_refcount
2960  * _M_p ----------------> unnamed array of char_type
2961  * @endcode
2962  *
2963  * Where the _M_p points to the first character in the string, and
2964  * you cast it to a pointer-to-_Rep and subtract 1 to get a
2965  * pointer to the header.
2966  *
2967  * This approach has the enormous advantage that a string object
2968  * requires only one allocation. All the ugliness is confined
2969  * within a single %pair of inline functions, which each compile to
2970  * a single @a add instruction: _Rep::_M_data(), and
2971  * string::_M_rep(); and the allocation function which gets a
2972  * block of raw bytes and with room enough and constructs a _Rep
2973  * object at the front.
2974  *
2975  * The reason you want _M_data pointing to the character %array and
2976  * not the _Rep is so that the debugger can see the string
2977  * contents. (Probably we should add a non-inline member to get
2978  * the _Rep for the debugger to use, so users can check the actual
2979  * string length.)
2980  *
2981  * Note that the _Rep object is a POD so that you can have a
2982  * static <em>empty string</em> _Rep object already @a constructed before
2983  * static constructors have run. The reference-count encoding is
2984  * chosen so that a 0 indicates one reference, so you never try to
2985  * destroy the empty-string _Rep object.
2986  *
2987  * All but the last paragraph is considered pretty conventional
2988  * for a C++ string implementation.
2989  */
2990  // 21.3 Template class basic_string
2991  template<typename _CharT, typename _Traits, typename _Alloc>
2992  class basic_string
2993  {
2994  typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
2995 
2996  // Types:
2997  public:
2998  typedef _Traits traits_type;
2999  typedef typename _Traits::char_type value_type;
3000  typedef _Alloc allocator_type;
3001  typedef typename _CharT_alloc_type::size_type size_type;
3002  typedef typename _CharT_alloc_type::difference_type difference_type;
3003  typedef typename _CharT_alloc_type::reference reference;
3004  typedef typename _CharT_alloc_type::const_reference const_reference;
3005  typedef typename _CharT_alloc_type::pointer pointer;
3006  typedef typename _CharT_alloc_type::const_pointer const_pointer;
3007  typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
3008  typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
3009  const_iterator;
3010  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
3011  typedef std::reverse_iterator<iterator> reverse_iterator;
3012 
3013  private:
3014  // _Rep: string representation
3015  // Invariants:
3016  // 1. String really contains _M_length + 1 characters: due to 21.3.4
3017  // must be kept null-terminated.
3018  // 2. _M_capacity >= _M_length
3019  // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
3020  // 3. _M_refcount has three states:
3021  // -1: leaked, one reference, no ref-copies allowed, non-const.
3022  // 0: one reference, non-const.
3023  // n>0: n + 1 references, operations require a lock, const.
3024  // 4. All fields==0 is an empty string, given the extra storage
3025  // beyond-the-end for a null terminator; thus, the shared
3026  // empty string representation needs no constructor.
3027 
3028  struct _Rep_base
3029  {
3030  size_type _M_length;
3031  size_type _M_capacity;
3032  _Atomic_word _M_refcount;
3033  };
3034 
3035  struct _Rep : _Rep_base
3036  {
3037  // Types:
3038  typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
3039 
3040  // (Public) Data members:
3041 
3042  // The maximum number of individual char_type elements of an
3043  // individual string is determined by _S_max_size. This is the
3044  // value that will be returned by max_size(). (Whereas npos
3045  // is the maximum number of bytes the allocator can allocate.)
3046  // If one was to divvy up the theoretical largest size string,
3047  // with a terminating character and m _CharT elements, it'd
3048  // look like this:
3049  // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
3050  // Solving for m:
3051  // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
3052  // In addition, this implementation quarters this amount.
3053  static const size_type _S_max_size;
3054  static const _CharT _S_terminal;
3055 
3056  // The following storage is init'd to 0 by the linker, resulting
3057  // (carefully) in an empty string with one reference.
3058  static size_type _S_empty_rep_storage[];
3059 
3060  static _Rep&
3061  _S_empty_rep() _GLIBCXX_NOEXCEPT
3062  {
3063  // NB: Mild hack to avoid strict-aliasing warnings. Note that
3064  // _S_empty_rep_storage is never modified and the punning should
3065  // be reasonably safe in this case.
3066  void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
3067  return *reinterpret_cast<_Rep*>(__p);
3068  }
3069 
3070  bool
3071  _M_is_leaked() const _GLIBCXX_NOEXCEPT
3072  {
3073 #if defined(__GTHREADS)
3074  // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3075  // so we need to use an atomic load. However, _M_is_leaked
3076  // predicate does not change concurrently (i.e. the string is either
3077  // leaked or not), so a relaxed load is enough.
3078  return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
3079 #else
3080  return this->_M_refcount < 0;
3081 #endif
3082  }
3083 
3084  bool
3085  _M_is_shared() const _GLIBCXX_NOEXCEPT
3086  {
3087 #if defined(__GTHREADS)
3088  // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3089  // so we need to use an atomic load. Another thread can drop last
3090  // but one reference concurrently with this check, so we need this
3091  // load to be acquire to synchronize with release fetch_and_add in
3092  // _M_dispose.
3093  return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
3094 #else
3095  return this->_M_refcount > 0;
3096 #endif
3097  }
3098 
3099  void
3100  _M_set_leaked() _GLIBCXX_NOEXCEPT
3101  { this->_M_refcount = -1; }
3102 
3103  void
3104  _M_set_sharable() _GLIBCXX_NOEXCEPT
3105  { this->_M_refcount = 0; }
3106 
3107  void
3108  _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
3109  {
3110 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3111  if (__builtin_expect(this != &_S_empty_rep(), false))
3112 #endif
3113  {
3114  this->_M_set_sharable(); // One reference.
3115  this->_M_length = __n;
3116  traits_type::assign(this->_M_refdata()[__n], _S_terminal);
3117  // grrr. (per 21.3.4)
3118  // You cannot leave those LWG people alone for a second.
3119  }
3120  }
3121 
3122  _CharT*
3123  _M_refdata() throw()
3124  { return reinterpret_cast<_CharT*>(this + 1); }
3125 
3126  _CharT*
3127  _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
3128  {
3129  return (!_M_is_leaked() && __alloc1 == __alloc2)
3130  ? _M_refcopy() : _M_clone(__alloc1);
3131  }
3132 
3133  // Create & Destroy
3134  static _Rep*
3135  _S_create(size_type, size_type, const _Alloc&);
3136 
3137  void
3138  _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
3139  {
3140 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3141  if (__builtin_expect(this != &_S_empty_rep(), false))
3142 #endif
3143  {
3144  // Be race-detector-friendly. For more info see bits/c++config.
3145  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
3146  // Decrement of _M_refcount is acq_rel, because:
3147  // - all but last decrements need to release to synchronize with
3148  // the last decrement that will delete the object.
3149  // - the last decrement needs to acquire to synchronize with
3150  // all the previous decrements.
3151  // - last but one decrement needs to release to synchronize with
3152  // the acquire load in _M_is_shared that will conclude that
3153  // the object is not shared anymore.
3154  if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
3155  -1) <= 0)
3156  {
3157  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
3158  _M_destroy(__a);
3159  }
3160  }
3161  } // XXX MT
3162 
3163  void
3164  _M_destroy(const _Alloc&) throw();
3165 
3166  _CharT*
3167  _M_refcopy() throw()
3168  {
3169 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3170  if (__builtin_expect(this != &_S_empty_rep(), false))
3171 #endif
3172  __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
3173  return _M_refdata();
3174  } // XXX MT
3175 
3176  _CharT*
3177  _M_clone(const _Alloc&, size_type __res = 0);
3178  };
3179 
3180  // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
3181  struct _Alloc_hider : _Alloc
3182  {
3183  _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
3184  : _Alloc(__a), _M_p(__dat) { }
3185 
3186  _CharT* _M_p; // The actual data.
3187  };
3188 
3189  public:
3190  // Data Members (public):
3191  // NB: This is an unsigned type, and thus represents the maximum
3192  // size that the allocator can hold.
3193  /// Value returned by various member functions when they fail.
3194  static const size_type npos = static_cast<size_type>(-1);
3195 
3196  private:
3197  // Data Members (private):
3198  mutable _Alloc_hider _M_dataplus;
3199 
3200  _CharT*
3201  _M_data() const _GLIBCXX_NOEXCEPT
3202  { return _M_dataplus._M_p; }
3203 
3204  _CharT*
3205  _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
3206  { return (_M_dataplus._M_p = __p); }
3207 
3208  _Rep*
3209  _M_rep() const _GLIBCXX_NOEXCEPT
3210  { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
3211 
3212  // For the internal use we have functions similar to `begin'/`end'
3213  // but they do not call _M_leak.
3214  iterator
3215  _M_ibegin() const _GLIBCXX_NOEXCEPT
3216  { return iterator(_M_data()); }
3217 
3218  iterator
3219  _M_iend() const _GLIBCXX_NOEXCEPT
3220  { return iterator(_M_data() + this->size()); }
3221 
3222  void
3223  _M_leak() // for use in begin() & non-const op[]
3224  {
3225  if (!_M_rep()->_M_is_leaked())
3226  _M_leak_hard();
3227  }
3228 
3229  size_type
3230  _M_check(size_type __pos, const char* __s) const
3231  {
3232  if (__pos > this->size())
3233  __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
3234  "this->size() (which is %zu)"),
3235  __s, __pos, this->size());
3236  return __pos;
3237  }
3238 
3239  void
3240  _M_check_length(size_type __n1, size_type __n2, const char* __s) const
3241  {
3242  if (this->max_size() - (this->size() - __n1) < __n2)
3243  __throw_length_error(__N(__s));
3244  }
3245 
3246  // NB: _M_limit doesn't check for a bad __pos value.
3247  size_type
3248  _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
3249  {
3250  const bool __testoff = __off < this->size() - __pos;
3251  return __testoff ? __off : this->size() - __pos;
3252  }
3253 
3254  // True if _Rep and source do not overlap.
3255  bool
3256  _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3257  {
3258  return (less<const _CharT*>()(__s, _M_data())
3259  || less<const _CharT*>()(_M_data() + this->size(), __s));
3260  }
3261 
3262  // When __n = 1 way faster than the general multichar
3263  // traits_type::copy/move/assign.
3264  static void
3265  _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
3266  {
3267  if (__n == 1)
3268  traits_type::assign(*__d, *__s);
3269  else
3270  traits_type::copy(__d, __s, __n);
3271  }
3272 
3273  static void
3274  _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
3275  {
3276  if (__n == 1)
3277  traits_type::assign(*__d, *__s);
3278  else
3279  traits_type::move(__d, __s, __n);
3280  }
3281 
3282  static void
3283  _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
3284  {
3285  if (__n == 1)
3286  traits_type::assign(*__d, __c);
3287  else
3288  traits_type::assign(__d, __n, __c);
3289  }
3290 
3291  // _S_copy_chars is a separate template to permit specialization
3292  // to optimize for the common case of pointers as iterators.
3293  template<class _Iterator>
3294  static void
3295  _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
3296  {
3297  for (; __k1 != __k2; ++__k1, (void)++__p)
3298  traits_type::assign(*__p, *__k1); // These types are off.
3299  }
3300 
3301  static void
3302  _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
3303  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3304 
3305  static void
3306  _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
3307  _GLIBCXX_NOEXCEPT
3308  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3309 
3310  static void
3311  _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
3312  { _M_copy(__p, __k1, __k2 - __k1); }
3313 
3314  static void
3315  _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
3316  _GLIBCXX_NOEXCEPT
3317  { _M_copy(__p, __k1, __k2 - __k1); }
3318 
3319  static int
3320  _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
3321  {
3322  const difference_type __d = difference_type(__n1 - __n2);
3323 
3324  if (__d > __gnu_cxx::__numeric_traits<int>::__max)
3325  return __gnu_cxx::__numeric_traits<int>::__max;
3326  else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
3327  return __gnu_cxx::__numeric_traits<int>::__min;
3328  else
3329  return int(__d);
3330  }
3331 
3332  void
3333  _M_mutate(size_type __pos, size_type __len1, size_type __len2);
3334 
3335  void
3336  _M_leak_hard();
3337 
3338  static _Rep&
3339  _S_empty_rep() _GLIBCXX_NOEXCEPT
3340  { return _Rep::_S_empty_rep(); }
3341 
3342 #if __cplusplus > 201402L
3343  // A helper type for avoiding boiler-plate.
3344  typedef basic_string_view<_CharT, _Traits> __sv_type;
3345 
3346  template<typename _Tp, typename _Res>
3347  using _If_sv = enable_if_t<
3348  __and_<is_convertible<const _Tp&, __sv_type>,
3349  __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
3350  _Res>;
3351 #endif
3352 
3353  public:
3354  // Construct/copy/destroy:
3355  // NB: We overload ctors in some cases instead of using default
3356  // arguments, per 17.4.4.4 para. 2 item 2.
3357 
3358  /**
3359  * @brief Default constructor creates an empty string.
3360  */
3362 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3363  : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
3364 #else
3365  : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
3366 #endif
3367 
3368  /**
3369  * @brief Construct an empty string using allocator @a a.
3370  */
3371  explicit
3372  basic_string(const _Alloc& __a);
3373 
3374  // NB: per LWG issue 42, semantics different from IS:
3375  /**
3376  * @brief Construct string with copy of value of @a str.
3377  * @param __str Source string.
3378  */
3379  basic_string(const basic_string& __str);
3380 
3381  // _GLIBCXX_RESOLVE_LIB_DEFECTS
3382  // 2583. no way to supply an allocator for basic_string(str, pos)
3383  /**
3384  * @brief Construct string as copy of a substring.
3385  * @param __str Source string.
3386  * @param __pos Index of first character to copy from.
3387  * @param __a Allocator to use.
3388  */
3389  basic_string(const basic_string& __str, size_type __pos,
3390  const _Alloc& __a = _Alloc());
3391 
3392  /**
3393  * @brief Construct string as copy of a substring.
3394  * @param __str Source string.
3395  * @param __pos Index of first character to copy from.
3396  * @param __n Number of characters to copy.
3397  */
3398  basic_string(const basic_string& __str, size_type __pos,
3399  size_type __n);
3400  /**
3401  * @brief Construct string as copy of a substring.
3402  * @param __str Source string.
3403  * @param __pos Index of first character to copy from.
3404  * @param __n Number of characters to copy.
3405  * @param __a Allocator to use.
3406  */
3407  basic_string(const basic_string& __str, size_type __pos,
3408  size_type __n, const _Alloc& __a);
3409 
3410  /**
3411  * @brief Construct string initialized by a character %array.
3412  * @param __s Source character %array.
3413  * @param __n Number of characters to copy.
3414  * @param __a Allocator to use (default is default allocator).
3415  *
3416  * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
3417  * has no special meaning.
3418  */
3419  basic_string(const _CharT* __s, size_type __n,
3420  const _Alloc& __a = _Alloc());
3421  /**
3422  * @brief Construct string as copy of a C string.
3423  * @param __s Source C string.
3424  * @param __a Allocator to use (default is default allocator).
3425  */
3426  basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
3427  /**
3428  * @brief Construct string as multiple characters.
3429  * @param __n Number of characters.
3430  * @param __c Character to use.
3431  * @param __a Allocator to use (default is default allocator).
3432  */
3433  basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
3434 
3435 #if __cplusplus >= 201103L
3436  /**
3437  * @brief Move construct string.
3438  * @param __str Source string.
3439  *
3440  * The newly-created string contains the exact contents of @a __str.
3441  * @a __str is a valid, but unspecified string.
3442  **/
3443  basic_string(basic_string&& __str)
3444 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3445  noexcept // FIXME C++11: should always be noexcept.
3446 #endif
3447  : _M_dataplus(__str._M_dataplus)
3448  {
3449 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3450  __str._M_data(_S_empty_rep()._M_refdata());
3451 #else
3452  __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3453 #endif
3454  }
3455 
3456  /**
3457  * @brief Construct string from an initializer %list.
3458  * @param __l std::initializer_list of characters.
3459  * @param __a Allocator to use (default is default allocator).
3460  */
3461  basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
3462 #endif // C++11
3463 
3464  /**
3465  * @brief Construct string as copy of a range.
3466  * @param __beg Start of range.
3467  * @param __end End of range.
3468  * @param __a Allocator to use (default is default allocator).
3469  */
3470  template<class _InputIterator>
3471  basic_string(_InputIterator __beg, _InputIterator __end,
3472  const _Alloc& __a = _Alloc());
3473 
3474 #if __cplusplus > 201402L
3475  /**
3476  * @brief Construct string from a substring of a string_view.
3477  * @param __t Source string view.
3478  * @param __pos The index of the first character to copy from __t.
3479  * @param __n The number of characters to copy from __t.
3480  * @param __a Allocator to use.
3481  */
3482  template<typename _Tp, typename = _If_sv<_Tp, void>>
3483  basic_string(const _Tp& __t, size_type __pos, size_type __n,
3484  const _Alloc& __a = _Alloc())
3485  : basic_string(__sv_type(__t).substr(__pos, __n), __a) { }
3486 
3487  /**
3488  * @brief Construct string from a string_view.
3489  * @param __sv Source string view.
3490  * @param __a Allocator to use (default is default allocator).
3491  */
3492  explicit
3493  basic_string(__sv_type __sv, const _Alloc& __a = _Alloc())
3494  : basic_string(__sv.data(), __sv.size(), __a) { }
3495 #endif // C++17
3496 
3497  /**
3498  * @brief Destroy the string instance.
3499  */
3500  ~basic_string() _GLIBCXX_NOEXCEPT
3501  { _M_rep()->_M_dispose(this->get_allocator()); }
3502 
3503  /**
3504  * @brief Assign the value of @a str to this string.
3505  * @param __str Source string.
3506  */
3507  basic_string&
3508  operator=(const basic_string& __str)
3509  { return this->assign(__str); }
3510 
3511  /**
3512  * @brief Copy contents of @a s into this string.
3513  * @param __s Source null-terminated string.
3514  */
3515  basic_string&
3516  operator=(const _CharT* __s)
3517  { return this->assign(__s); }
3518 
3519  /**
3520  * @brief Set value to string of length 1.
3521  * @param __c Source character.
3522  *
3523  * Assigning to a character makes this string length 1 and
3524  * (*this)[0] == @a c.
3525  */
3526  basic_string&
3527  operator=(_CharT __c)
3528  {
3529  this->assign(1, __c);
3530  return *this;
3531  }
3532 
3533 #if __cplusplus >= 201103L
3534  /**
3535  * @brief Move assign the value of @a str to this string.
3536  * @param __str Source string.
3537  *
3538  * The contents of @a str are moved into this string (without copying).
3539  * @a str is a valid, but unspecified string.
3540  **/
3541  // PR 58265, this should be noexcept.
3542  basic_string&
3543  operator=(basic_string&& __str)
3544  {
3545  // NB: DR 1204.
3546  this->swap(__str);
3547  return *this;
3548  }
3549 
3550  /**
3551  * @brief Set value to string constructed from initializer %list.
3552  * @param __l std::initializer_list.
3553  */
3554  basic_string&
3556  {
3557  this->assign(__l.begin(), __l.size());
3558  return *this;
3559  }
3560 #endif // C++11
3561 
3562 #if __cplusplus > 201402L
3563  /**
3564  * @brief Set value to string constructed from a string_view.
3565  * @param __sv A string_view.
3566  */
3567  template<typename _Tp>
3568  _If_sv<_Tp, basic_string&>
3569  operator=(_Tp __sv)
3570  { return this->assign(__sv); }
3571 
3572  /**
3573  * @brief Convert to a string_view.
3574  * @return A string_view.
3575  */
3576  operator __sv_type() const noexcept
3577  { return __sv_type(data(), size()); }
3578 #endif // C++17
3579 
3580  // Iterators:
3581  /**
3582  * Returns a read/write iterator that points to the first character in
3583  * the %string. Unshares the string.
3584  */
3585  iterator
3586  begin() // FIXME C++11: should be noexcept.
3587  {
3588  _M_leak();
3589  return iterator(_M_data());
3590  }
3591 
3592  /**
3593  * Returns a read-only (constant) iterator that points to the first
3594  * character in the %string.
3595  */
3596  const_iterator
3597  begin() const _GLIBCXX_NOEXCEPT
3598  { return const_iterator(_M_data()); }
3599 
3600  /**
3601  * Returns a read/write iterator that points one past the last
3602  * character in the %string. Unshares the string.
3603  */
3604  iterator
3605  end() // FIXME C++11: should be noexcept.
3606  {
3607  _M_leak();
3608  return iterator(_M_data() + this->size());
3609  }
3610 
3611  /**
3612  * Returns a read-only (constant) iterator that points one past the
3613  * last character in the %string.
3614  */
3615  const_iterator
3616  end() const _GLIBCXX_NOEXCEPT
3617  { return const_iterator(_M_data() + this->size()); }
3618 
3619  /**
3620  * Returns a read/write reverse iterator that points to the last
3621  * character in the %string. Iteration is done in reverse element
3622  * order. Unshares the string.
3623  */
3624  reverse_iterator
3625  rbegin() // FIXME C++11: should be noexcept.
3626  { return reverse_iterator(this->end()); }
3627 
3628  /**
3629  * Returns a read-only (constant) reverse iterator that points
3630  * to the last character in the %string. Iteration is done in
3631  * reverse element order.
3632  */
3633  const_reverse_iterator
3634  rbegin() const _GLIBCXX_NOEXCEPT
3635  { return const_reverse_iterator(this->end()); }
3636 
3637  /**
3638  * Returns a read/write reverse iterator that points to one before the
3639  * first character in the %string. Iteration is done in reverse
3640  * element order. Unshares the string.
3641  */
3642  reverse_iterator
3643  rend() // FIXME C++11: should be noexcept.
3644  { return reverse_iterator(this->begin()); }
3645 
3646  /**
3647  * Returns a read-only (constant) reverse iterator that points
3648  * to one before the first character in the %string. Iteration
3649  * is done in reverse element order.
3650  */
3651  const_reverse_iterator
3652  rend() const _GLIBCXX_NOEXCEPT
3653  { return const_reverse_iterator(this->begin()); }
3654 
3655 #if __cplusplus >= 201103L
3656  /**
3657  * Returns a read-only (constant) iterator that points to the first
3658  * character in the %string.
3659  */
3660  const_iterator
3661  cbegin() const noexcept
3662  { return const_iterator(this->_M_data()); }
3663 
3664  /**
3665  * Returns a read-only (constant) iterator that points one past the
3666  * last character in the %string.
3667  */
3668  const_iterator
3669  cend() const noexcept
3670  { return const_iterator(this->_M_data() + this->size()); }
3671 
3672  /**
3673  * Returns a read-only (constant) reverse iterator that points
3674  * to the last character in the %string. Iteration is done in
3675  * reverse element order.
3676  */
3677  const_reverse_iterator
3678  crbegin() const noexcept
3679  { return const_reverse_iterator(this->end()); }
3680 
3681  /**
3682  * Returns a read-only (constant) reverse iterator that points
3683  * to one before the first character in the %string. Iteration
3684  * is done in reverse element order.
3685  */
3686  const_reverse_iterator
3687  crend() const noexcept
3688  { return const_reverse_iterator(this->begin()); }
3689 #endif
3690 
3691  public:
3692  // Capacity:
3693  /// Returns the number of characters in the string, not including any
3694  /// null-termination.
3695  size_type
3696  size() const _GLIBCXX_NOEXCEPT
3697  { return _M_rep()->_M_length; }
3698 
3699  /// Returns the number of characters in the string, not including any
3700  /// null-termination.
3701  size_type
3702  length() const _GLIBCXX_NOEXCEPT
3703  { return _M_rep()->_M_length; }
3704 
3705  /// Returns the size() of the largest possible %string.
3706  size_type
3707  max_size() const _GLIBCXX_NOEXCEPT
3708  { return _Rep::_S_max_size; }
3709 
3710  /**
3711  * @brief Resizes the %string to the specified number of characters.
3712  * @param __n Number of characters the %string should contain.
3713  * @param __c Character to fill any new elements.
3714  *
3715  * This function will %resize the %string to the specified
3716  * number of characters. If the number is smaller than the
3717  * %string's current size the %string is truncated, otherwise
3718  * the %string is extended and new elements are %set to @a __c.
3719  */
3720  void
3721  resize(size_type __n, _CharT __c);
3722 
3723  /**
3724  * @brief Resizes the %string to the specified number of characters.
3725  * @param __n Number of characters the %string should contain.
3726  *
3727  * This function will resize the %string to the specified length. If
3728  * the new size is smaller than the %string's current size the %string
3729  * is truncated, otherwise the %string is extended and new characters
3730  * are default-constructed. For basic types such as char, this means
3731  * setting them to 0.
3732  */
3733  void
3734  resize(size_type __n)
3735  { this->resize(__n, _CharT()); }
3736 
3737 #if __cplusplus >= 201103L
3738  /// A non-binding request to reduce capacity() to size().
3739  void
3740  shrink_to_fit() _GLIBCXX_NOEXCEPT
3741  {
3742 #if __cpp_exceptions
3743  if (capacity() > size())
3744  {
3745  try
3746  { reserve(0); }
3747  catch(...)
3748  { }
3749  }
3750 #endif
3751  }
3752 #endif
3753 
3754  /**
3755  * Returns the total number of characters that the %string can hold
3756  * before needing to allocate more memory.
3757  */
3758  size_type
3759  capacity() const _GLIBCXX_NOEXCEPT
3760  { return _M_rep()->_M_capacity; }
3761 
3762  /**
3763  * @brief Attempt to preallocate enough memory for specified number of
3764  * characters.
3765  * @param __res_arg Number of characters required.
3766  * @throw std::length_error If @a __res_arg exceeds @c max_size().
3767  *
3768  * This function attempts to reserve enough memory for the
3769  * %string to hold the specified number of characters. If the
3770  * number requested is more than max_size(), length_error is
3771  * thrown.
3772  *
3773  * The advantage of this function is that if optimal code is a
3774  * necessity and the user can determine the string length that will be
3775  * required, the user can reserve the memory in %advance, and thus
3776  * prevent a possible reallocation of memory and copying of %string
3777  * data.
3778  */
3779  void
3780  reserve(size_type __res_arg = 0);
3781 
3782  /**
3783  * Erases the string, making it empty.
3784  */
3785 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3786  void
3787  clear() _GLIBCXX_NOEXCEPT
3788  {
3789  if (_M_rep()->_M_is_shared())
3790  {
3791  _M_rep()->_M_dispose(this->get_allocator());
3792  _M_data(_S_empty_rep()._M_refdata());
3793  }
3794  else
3795  _M_rep()->_M_set_length_and_sharable(0);
3796  }
3797 #else
3798  // PR 56166: this should not throw.
3799  void
3800  clear()
3801  { _M_mutate(0, this->size(), 0); }
3802 #endif
3803 
3804  /**
3805  * Returns true if the %string is empty. Equivalent to
3806  * <code>*this == ""</code>.
3807  */
3808  bool
3809  empty() const _GLIBCXX_NOEXCEPT
3810  { return this->size() == 0; }
3811 
3812  // Element access:
3813  /**
3814  * @brief Subscript access to the data contained in the %string.
3815  * @param __pos The index of the character to access.
3816  * @return Read-only (constant) reference to the character.
3817  *
3818  * This operator allows for easy, array-style, data access.
3819  * Note that data access with this operator is unchecked and
3820  * out_of_range lookups are not defined. (For checked lookups
3821  * see at().)
3822  */
3823  const_reference
3824  operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
3825  {
3826  __glibcxx_assert(__pos <= size());
3827  return _M_data()[__pos];
3828  }
3829 
3830  /**
3831  * @brief Subscript access to the data contained in the %string.
3832  * @param __pos The index of the character to access.
3833  * @return Read/write reference to the character.
3834  *
3835  * This operator allows for easy, array-style, data access.
3836  * Note that data access with this operator is unchecked and
3837  * out_of_range lookups are not defined. (For checked lookups
3838  * see at().) Unshares the string.
3839  */
3840  reference
3841  operator[](size_type __pos)
3842  {
3843  // Allow pos == size() both in C++98 mode, as v3 extension,
3844  // and in C++11 mode.
3845  __glibcxx_assert(__pos <= size());
3846  // In pedantic mode be strict in C++98 mode.
3847  _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
3848  _M_leak();
3849  return _M_data()[__pos];
3850  }
3851 
3852  /**
3853  * @brief Provides access to the data contained in the %string.
3854  * @param __n The index of the character to access.
3855  * @return Read-only (const) reference to the character.
3856  * @throw std::out_of_range If @a n is an invalid index.
3857  *
3858  * This function provides for safer data access. The parameter is
3859  * first checked that it is in the range of the string. The function
3860  * throws out_of_range if the check fails.
3861  */
3862  const_reference
3863  at(size_type __n) const
3864  {
3865  if (__n >= this->size())
3866  __throw_out_of_range_fmt(__N("basic_string::at: __n "
3867  "(which is %zu) >= this->size() "
3868  "(which is %zu)"),
3869  __n, this->size());
3870  return _M_data()[__n];
3871  }
3872 
3873  /**
3874  * @brief Provides access to the data contained in the %string.
3875  * @param __n The index of the character to access.
3876  * @return Read/write reference to the character.
3877  * @throw std::out_of_range If @a n is an invalid index.
3878  *
3879  * This function provides for safer data access. The parameter is
3880  * first checked that it is in the range of the string. The function
3881  * throws out_of_range if the check fails. Success results in
3882  * unsharing the string.
3883  */
3884  reference
3885  at(size_type __n)
3886  {
3887  if (__n >= size())
3888  __throw_out_of_range_fmt(__N("basic_string::at: __n "
3889  "(which is %zu) >= this->size() "
3890  "(which is %zu)"),
3891  __n, this->size());
3892  _M_leak();
3893  return _M_data()[__n];
3894  }
3895 
3896 #if __cplusplus >= 201103L
3897  /**
3898  * Returns a read/write reference to the data at the first
3899  * element of the %string.
3900  */
3901  reference
3903  {
3904  __glibcxx_assert(!empty());
3905  return operator[](0);
3906  }
3907 
3908  /**
3909  * Returns a read-only (constant) reference to the data at the first
3910  * element of the %string.
3911  */
3912  const_reference
3913  front() const noexcept
3914  {
3915  __glibcxx_assert(!empty());
3916  return operator[](0);
3917  }
3918 
3919  /**
3920  * Returns a read/write reference to the data at the last
3921  * element of the %string.
3922  */
3923  reference
3925  {
3926  __glibcxx_assert(!empty());
3927  return operator[](this->size() - 1);
3928  }
3929 
3930  /**
3931  * Returns a read-only (constant) reference to the data at the
3932  * last element of the %string.
3933  */
3934  const_reference
3935  back() const noexcept
3936  {
3937  __glibcxx_assert(!empty());
3938  return operator[](this->size() - 1);
3939  }
3940 #endif
3941 
3942  // Modifiers:
3943  /**
3944  * @brief Append a string to this string.
3945  * @param __str The string to append.
3946  * @return Reference to this string.
3947  */
3948  basic_string&
3949  operator+=(const basic_string& __str)
3950  { return this->append(__str); }
3951 
3952  /**
3953  * @brief Append a C string.
3954  * @param __s The C string to append.
3955  * @return Reference to this string.
3956  */
3957  basic_string&
3958  operator+=(const _CharT* __s)
3959  { return this->append(__s); }
3960 
3961  /**
3962  * @brief Append a character.
3963  * @param __c The character to append.
3964  * @return Reference to this string.
3965  */
3966  basic_string&
3967  operator+=(_CharT __c)
3968  {
3969  this->push_back(__c);
3970  return *this;
3971  }
3972 
3973 #if __cplusplus >= 201103L
3974  /**
3975  * @brief Append an initializer_list of characters.
3976  * @param __l The initializer_list of characters to be appended.
3977  * @return Reference to this string.
3978  */
3979  basic_string&
3981  { return this->append(__l.begin(), __l.size()); }
3982 #endif // C++11
3983 
3984 #if __cplusplus > 201402L
3985  /**
3986  * @brief Append a string_view.
3987  * @param __sv The string_view to be appended.
3988  * @return Reference to this string.
3989  */
3990  basic_string&
3991  operator+=(__sv_type __sv)
3992  { return this->append(__sv); }
3993 #endif // C++17
3994 
3995  /**
3996  * @brief Append a string to this string.
3997  * @param __str The string to append.
3998  * @return Reference to this string.
3999  */
4000  basic_string&
4001  append(const basic_string& __str);
4002 
4003  /**
4004  * @brief Append a substring.
4005  * @param __str The string to append.
4006  * @param __pos Index of the first character of str to append.
4007  * @param __n The number of characters to append.
4008  * @return Reference to this string.
4009  * @throw std::out_of_range if @a __pos is not a valid index.
4010  *
4011  * This function appends @a __n characters from @a __str
4012  * starting at @a __pos to this string. If @a __n is is larger
4013  * than the number of available characters in @a __str, the
4014  * remainder of @a __str is appended.
4015  */
4016  basic_string&
4017  append(const basic_string& __str, size_type __pos, size_type __n);
4018 
4019  /**
4020  * @brief Append a C substring.
4021  * @param __s The C string to append.
4022  * @param __n The number of characters to append.
4023  * @return Reference to this string.
4024  */
4025  basic_string&
4026  append(const _CharT* __s, size_type __n);
4027 
4028  /**
4029  * @brief Append a C string.
4030  * @param __s The C string to append.
4031  * @return Reference to this string.
4032  */
4033  basic_string&
4034  append(const _CharT* __s)
4035  {
4036  __glibcxx_requires_string(__s);
4037  return this->append(__s, traits_type::length(__s));
4038  }
4039 
4040  /**
4041  * @brief Append multiple characters.
4042  * @param __n The number of characters to append.
4043  * @param __c The character to use.
4044  * @return Reference to this string.
4045  *
4046  * Appends __n copies of __c to this string.
4047  */
4048  basic_string&
4049  append(size_type __n, _CharT __c);
4050 
4051 #if __cplusplus >= 201103L
4052  /**
4053  * @brief Append an initializer_list of characters.
4054  * @param __l The initializer_list of characters to append.
4055  * @return Reference to this string.
4056  */
4057  basic_string&
4059  { return this->append(__l.begin(), __l.size()); }
4060 #endif // C++11
4061 
4062  /**
4063  * @brief Append a range of characters.
4064  * @param __first Iterator referencing the first character to append.
4065  * @param __last Iterator marking the end of the range.
4066  * @return Reference to this string.
4067  *
4068  * Appends characters in the range [__first,__last) to this string.
4069  */
4070  template<class _InputIterator>
4071  basic_string&
4072  append(_InputIterator __first, _InputIterator __last)
4073  { return this->replace(_M_iend(), _M_iend(), __first, __last); }
4074 
4075 #if __cplusplus > 201402L
4076  /**
4077  * @brief Append a string_view.
4078  * @param __sv The string_view to be appended.
4079  * @return Reference to this string.
4080  */
4081  basic_string&
4082  append(__sv_type __sv)
4083  { return this->append(__sv.data(), __sv.size()); }
4084 
4085  /**
4086  * @brief Append a range of characters from a string_view.
4087  * @param __sv The string_view to be appended from.
4088  * @param __pos The position in the string_view to append from.
4089  * @param __n The number of characters to append from the string_view.
4090  * @return Reference to this string.
4091  */
4092  template <typename _Tp>
4093  _If_sv<_Tp, basic_string&>
4094  append(const _Tp& __svt, size_type __pos, size_type __n = npos)
4095  {
4096  __sv_type __sv = __svt;
4097  return append(__sv.data()
4098  + __sv._M_check(__pos, "basic_string::append"),
4099  __sv._M_limit(__pos, __n));
4100  }
4101 #endif // C++17
4102 
4103  /**
4104  * @brief Append a single character.
4105  * @param __c Character to append.
4106  */
4107  void
4108  push_back(_CharT __c)
4109  {
4110  const size_type __len = 1 + this->size();
4111  if (__len > this->capacity() || _M_rep()->_M_is_shared())
4112  this->reserve(__len);
4113  traits_type::assign(_M_data()[this->size()], __c);
4114  _M_rep()->_M_set_length_and_sharable(__len);
4115  }
4116 
4117  /**
4118  * @brief Set value to contents of another string.
4119  * @param __str Source string to use.
4120  * @return Reference to this string.
4121  */
4122  basic_string&
4123  assign(const basic_string& __str);
4124 
4125 #if __cplusplus >= 201103L
4126  /**
4127  * @brief Set value to contents of another string.
4128  * @param __str Source string to use.
4129  * @return Reference to this string.
4130  *
4131  * This function sets this string to the exact contents of @a __str.
4132  * @a __str is a valid, but unspecified string.
4133  */
4134  // PR 58265, this should be noexcept.
4135  basic_string&
4136  assign(basic_string&& __str)
4137  {
4138  this->swap(__str);
4139  return *this;
4140  }
4141 #endif // C++11
4142 
4143  /**
4144  * @brief Set value to a substring of a string.
4145  * @param __str The string to use.
4146  * @param __pos Index of the first character of str.
4147  * @param __n Number of characters to use.
4148  * @return Reference to this string.
4149  * @throw std::out_of_range if @a pos is not a valid index.
4150  *
4151  * This function sets this string to the substring of @a __str
4152  * consisting of @a __n characters at @a __pos. If @a __n is
4153  * is larger than the number of available characters in @a
4154  * __str, the remainder of @a __str is used.
4155  */
4156  basic_string&
4157  assign(const basic_string& __str, size_type __pos, size_type __n)
4158  { return this->assign(__str._M_data()
4159  + __str._M_check(__pos, "basic_string::assign"),
4160  __str._M_limit(__pos, __n)); }
4161 
4162  /**
4163  * @brief Set value to a C substring.
4164  * @param __s The C string to use.
4165  * @param __n Number of characters to use.
4166  * @return Reference to this string.
4167  *
4168  * This function sets the value of this string to the first @a __n
4169  * characters of @a __s. If @a __n is is larger than the number of
4170  * available characters in @a __s, the remainder of @a __s is used.
4171  */
4172  basic_string&
4173  assign(const _CharT* __s, size_type __n);
4174 
4175  /**
4176  * @brief Set value to contents of a C string.
4177  * @param __s The C string to use.
4178  * @return Reference to this string.
4179  *
4180  * This function sets the value of this string to the value of @a __s.
4181  * The data is copied, so there is no dependence on @a __s once the
4182  * function returns.
4183  */
4184  basic_string&
4185  assign(const _CharT* __s)
4186  {
4187  __glibcxx_requires_string(__s);
4188  return this->assign(__s, traits_type::length(__s));
4189  }
4190 
4191  /**
4192  * @brief Set value to multiple characters.
4193  * @param __n Length of the resulting string.
4194  * @param __c The character to use.
4195  * @return Reference to this string.
4196  *
4197  * This function sets the value of this string to @a __n copies of
4198  * character @a __c.
4199  */
4200  basic_string&
4201  assign(size_type __n, _CharT __c)
4202  { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
4203 
4204  /**
4205  * @brief Set value to a range of characters.
4206  * @param __first Iterator referencing the first character to append.
4207  * @param __last Iterator marking the end of the range.
4208  * @return Reference to this string.
4209  *
4210  * Sets value of string to characters in the range [__first,__last).
4211  */
4212  template<class _InputIterator>
4213  basic_string&
4214  assign(_InputIterator __first, _InputIterator __last)
4215  { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
4216 
4217 #if __cplusplus >= 201103L
4218  /**
4219  * @brief Set value to an initializer_list of characters.
4220  * @param __l The initializer_list of characters to assign.
4221  * @return Reference to this string.
4222  */
4223  basic_string&
4225  { return this->assign(__l.begin(), __l.size()); }
4226 #endif // C++11
4227 
4228 #if __cplusplus > 201402L
4229  /**
4230  * @brief Set value from a string_view.
4231  * @param __sv The source string_view.
4232  * @return Reference to this string.
4233  */
4234  basic_string&
4235  assign(__sv_type __sv)
4236  { return this->assign(__sv.data(), __sv.size()); }
4237 
4238  /**
4239  * @brief Set value from a range of characters in a string_view.
4240  * @param __sv The source string_view.
4241  * @param __pos The position in the string_view to assign from.
4242  * @param __n The number of characters to assign.
4243  * @return Reference to this string.
4244  */
4245  template <typename _Tp>
4246  _If_sv<_Tp, basic_string&>
4247  assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
4248  {
4249  __sv_type __sv = __svt;
4250  return assign(__sv.data()
4251  + __sv._M_check(__pos, "basic_string::assign"),
4252  __sv._M_limit(__pos, __n));
4253  }
4254 #endif // C++17
4255 
4256  /**
4257  * @brief Insert multiple characters.
4258  * @param __p Iterator referencing location in string to insert at.
4259  * @param __n Number of characters to insert
4260  * @param __c The character to insert.
4261  * @throw std::length_error If new length exceeds @c max_size().
4262  *
4263  * Inserts @a __n copies of character @a __c starting at the
4264  * position referenced by iterator @a __p. If adding
4265  * characters causes the length to exceed max_size(),
4266  * length_error is thrown. The value of the string doesn't
4267  * change if an error is thrown.
4268  */
4269  void
4270  insert(iterator __p, size_type __n, _CharT __c)
4271  { this->replace(__p, __p, __n, __c); }
4272 
4273  /**
4274  * @brief Insert a range of characters.
4275  * @param __p Iterator referencing location in string to insert at.
4276  * @param __beg Start of range.
4277  * @param __end End of range.
4278  * @throw std::length_error If new length exceeds @c max_size().
4279  *
4280  * Inserts characters in range [__beg,__end). If adding
4281  * characters causes the length to exceed max_size(),
4282  * length_error is thrown. The value of the string doesn't
4283  * change if an error is thrown.
4284  */
4285  template<class _InputIterator>
4286  void
4287  insert(iterator __p, _InputIterator __beg, _InputIterator __end)
4288  { this->replace(__p, __p, __beg, __end); }
4289 
4290 #if __cplusplus >= 201103L
4291  /**
4292  * @brief Insert an initializer_list of characters.
4293  * @param __p Iterator referencing location in string to insert at.
4294  * @param __l The initializer_list of characters to insert.
4295  * @throw std::length_error If new length exceeds @c max_size().
4296  */
4297  void
4299  {
4300  _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4301  this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
4302  }
4303 #endif // C++11
4304 
4305  /**
4306  * @brief Insert value of a string.
4307  * @param __pos1 Iterator referencing location in string to insert at.
4308  * @param __str The string to insert.
4309  * @return Reference to this string.
4310  * @throw std::length_error If new length exceeds @c max_size().
4311  *
4312  * Inserts value of @a __str starting at @a __pos1. If adding
4313  * characters causes the length to exceed max_size(),
4314  * length_error is thrown. The value of the string doesn't
4315  * change if an error is thrown.
4316  */
4317  basic_string&
4318  insert(size_type __pos1, const basic_string& __str)
4319  { return this->insert(__pos1, __str, size_type(0), __str.size()); }
4320 
4321  /**
4322  * @brief Insert a substring.
4323  * @param __pos1 Iterator referencing location in string to insert at.
4324  * @param __str The string to insert.
4325  * @param __pos2 Start of characters in str to insert.
4326  * @param __n Number of characters to insert.
4327  * @return Reference to this string.
4328  * @throw std::length_error If new length exceeds @c max_size().
4329  * @throw std::out_of_range If @a pos1 > size() or
4330  * @a __pos2 > @a str.size().
4331  *
4332  * Starting at @a pos1, insert @a __n character of @a __str
4333  * beginning with @a __pos2. If adding characters causes the
4334  * length to exceed max_size(), length_error is thrown. If @a
4335  * __pos1 is beyond the end of this string or @a __pos2 is
4336  * beyond the end of @a __str, out_of_range is thrown. The
4337  * value of the string doesn't change if an error is thrown.
4338  */
4339  basic_string&
4340  insert(size_type __pos1, const basic_string& __str,
4341  size_type __pos2, size_type __n)
4342  { return this->insert(__pos1, __str._M_data()
4343  + __str._M_check(__pos2, "basic_string::insert"),
4344  __str._M_limit(__pos2, __n)); }
4345 
4346  /**
4347  * @brief Insert a C substring.
4348  * @param __pos Iterator referencing location in string to insert at.
4349  * @param __s The C string to insert.
4350  * @param __n The number of characters to insert.
4351  * @return Reference to this string.
4352  * @throw std::length_error If new length exceeds @c max_size().
4353  * @throw std::out_of_range If @a __pos is beyond the end of this
4354  * string.
4355  *
4356  * Inserts the first @a __n characters of @a __s starting at @a
4357  * __pos. If adding characters causes the length to exceed
4358  * max_size(), length_error is thrown. If @a __pos is beyond
4359  * end(), out_of_range is thrown. The value of the string
4360  * doesn't change if an error is thrown.
4361  */
4362  basic_string&
4363  insert(size_type __pos, const _CharT* __s, size_type __n);
4364 
4365  /**
4366  * @brief Insert a C string.
4367  * @param __pos Iterator referencing location in string to insert at.
4368  * @param __s The C string to insert.
4369  * @return Reference to this string.
4370  * @throw std::length_error If new length exceeds @c max_size().
4371  * @throw std::out_of_range If @a pos is beyond the end of this
4372  * string.
4373  *
4374  * Inserts the first @a n characters of @a __s starting at @a __pos. If
4375  * adding characters causes the length to exceed max_size(),
4376  * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4377  * thrown. The value of the string doesn't change if an error is
4378  * thrown.
4379  */
4380  basic_string&
4381  insert(size_type __pos, const _CharT* __s)
4382  {
4383  __glibcxx_requires_string(__s);
4384  return this->insert(__pos, __s, traits_type::length(__s));
4385  }
4386 
4387  /**
4388  * @brief Insert multiple characters.
4389  * @param __pos Index in string to insert at.
4390  * @param __n Number of characters to insert
4391  * @param __c The character to insert.
4392  * @return Reference to this string.
4393  * @throw std::length_error If new length exceeds @c max_size().
4394  * @throw std::out_of_range If @a __pos is beyond the end of this
4395  * string.
4396  *
4397  * Inserts @a __n copies of character @a __c starting at index
4398  * @a __pos. If adding characters causes the length to exceed
4399  * max_size(), length_error is thrown. If @a __pos > length(),
4400  * out_of_range is thrown. The value of the string doesn't
4401  * change if an error is thrown.
4402  */
4403  basic_string&
4404  insert(size_type __pos, size_type __n, _CharT __c)
4405  { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
4406  size_type(0), __n, __c); }
4407 
4408  /**
4409  * @brief Insert one character.
4410  * @param __p Iterator referencing position in string to insert at.
4411  * @param __c The character to insert.
4412  * @return Iterator referencing newly inserted char.
4413  * @throw std::length_error If new length exceeds @c max_size().
4414  *
4415  * Inserts character @a __c at position referenced by @a __p.
4416  * If adding character causes the length to exceed max_size(),
4417  * length_error is thrown. If @a __p is beyond end of string,
4418  * out_of_range is thrown. The value of the string doesn't
4419  * change if an error is thrown.
4420  */
4421  iterator
4422  insert(iterator __p, _CharT __c)
4423  {
4424  _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4425  const size_type __pos = __p - _M_ibegin();
4426  _M_replace_aux(__pos, size_type(0), size_type(1), __c);
4427  _M_rep()->_M_set_leaked();
4428  return iterator(_M_data() + __pos);
4429  }
4430 
4431 #if __cplusplus > 201402L
4432  /**
4433  * @brief Insert a string_view.
4434  * @param __pos Iterator referencing position in string to insert at.
4435  * @param __sv The string_view to insert.
4436  * @return Reference to this string.
4437  */
4438  basic_string&
4439  insert(size_type __pos, __sv_type __sv)
4440  { return this->insert(__pos, __sv.data(), __sv.size()); }
4441 
4442  /**
4443  * @brief Insert a string_view.
4444  * @param __pos Iterator referencing position in string to insert at.
4445  * @param __sv The string_view to insert from.
4446  * @param __pos Iterator referencing position in string_view to insert
4447  * from.
4448  * @param __n The number of characters to insert.
4449  * @return Reference to this string.
4450  */
4451  template <typename _Tp>
4452  _If_sv<_Tp, basic_string&>
4453  insert(size_type __pos1, const _Tp& __svt,
4454  size_type __pos2, size_type __n = npos)
4455  {
4456  __sv_type __sv = __svt;
4457  return this->replace(__pos1, size_type(0), __sv.data()
4458  + __sv._M_check(__pos2, "basic_string::insert"),
4459  __sv._M_limit(__pos2, __n));
4460  }
4461 #endif // C++17
4462 
4463  /**
4464  * @brief Remove characters.
4465  * @param __pos Index of first character to remove (default 0).
4466  * @param __n Number of characters to remove (default remainder).
4467  * @return Reference to this string.
4468  * @throw std::out_of_range If @a pos is beyond the end of this
4469  * string.
4470  *
4471  * Removes @a __n characters from this string starting at @a
4472  * __pos. The length of the string is reduced by @a __n. If
4473  * there are < @a __n characters to remove, the remainder of
4474  * the string is truncated. If @a __p is beyond end of string,
4475  * out_of_range is thrown. The value of the string doesn't
4476  * change if an error is thrown.
4477  */
4478  basic_string&
4479  erase(size_type __pos = 0, size_type __n = npos)
4480  {
4481  _M_mutate(_M_check(__pos, "basic_string::erase"),
4482  _M_limit(__pos, __n), size_type(0));
4483  return *this;
4484  }
4485 
4486  /**
4487  * @brief Remove one character.
4488  * @param __position Iterator referencing the character to remove.
4489  * @return iterator referencing same location after removal.
4490  *
4491  * Removes the character at @a __position from this string. The value
4492  * of the string doesn't change if an error is thrown.
4493  */
4494  iterator
4495  erase(iterator __position)
4496  {
4497  _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
4498  && __position < _M_iend());
4499  const size_type __pos = __position - _M_ibegin();
4500  _M_mutate(__pos, size_type(1), size_type(0));
4501  _M_rep()->_M_set_leaked();
4502  return iterator(_M_data() + __pos);
4503  }
4504 
4505  /**
4506  * @brief Remove a range of characters.
4507  * @param __first Iterator referencing the first character to remove.
4508  * @param __last Iterator referencing the end of the range.
4509  * @return Iterator referencing location of first after removal.
4510  *
4511  * Removes the characters in the range [first,last) from this string.
4512  * The value of the string doesn't change if an error is thrown.
4513  */
4514  iterator
4515  erase(iterator __first, iterator __last);
4516 
4517 #if __cplusplus >= 201103L
4518  /**
4519  * @brief Remove the last character.
4520  *
4521  * The string must be non-empty.
4522  */
4523  void
4524  pop_back() // FIXME C++11: should be noexcept.
4525  {
4526  __glibcxx_assert(!empty());
4527  erase(size() - 1, 1);
4528  }
4529 #endif // C++11
4530 
4531  /**
4532  * @brief Replace characters with value from another string.
4533  * @param __pos Index of first character to replace.
4534  * @param __n Number of characters to be replaced.
4535  * @param __str String to insert.
4536  * @return Reference to this string.
4537  * @throw std::out_of_range If @a pos is beyond the end of this
4538  * string.
4539  * @throw std::length_error If new length exceeds @c max_size().
4540  *
4541  * Removes the characters in the range [__pos,__pos+__n) from
4542  * this string. In place, the value of @a __str is inserted.
4543  * If @a __pos is beyond end of string, out_of_range is thrown.
4544  * If the length of the result exceeds max_size(), length_error
4545  * is thrown. The value of the string doesn't change if an
4546  * error is thrown.
4547  */
4548  basic_string&
4549  replace(size_type __pos, size_type __n, const basic_string& __str)
4550  { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
4551 
4552  /**
4553  * @brief Replace characters with value from another string.
4554  * @param __pos1 Index of first character to replace.
4555  * @param __n1 Number of characters to be replaced.
4556  * @param __str String to insert.
4557  * @param __pos2 Index of first character of str to use.
4558  * @param __n2 Number of characters from str to use.
4559  * @return Reference to this string.
4560  * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4561  * __str.size().
4562  * @throw std::length_error If new length exceeds @c max_size().
4563  *
4564  * Removes the characters in the range [__pos1,__pos1 + n) from this
4565  * string. In place, the value of @a __str is inserted. If @a __pos is
4566  * beyond end of string, out_of_range is thrown. If the length of the
4567  * result exceeds max_size(), length_error is thrown. The value of the
4568  * string doesn't change if an error is thrown.
4569  */
4570  basic_string&
4571  replace(size_type __pos1, size_type __n1, const basic_string& __str,
4572  size_type __pos2, size_type __n2)
4573  { return this->replace(__pos1, __n1, __str._M_data()
4574  + __str._M_check(__pos2, "basic_string::replace"),
4575  __str._M_limit(__pos2, __n2)); }
4576 
4577  /**
4578  * @brief Replace characters with value of a C substring.
4579  * @param __pos Index of first character to replace.
4580  * @param __n1 Number of characters to be replaced.
4581  * @param __s C string to insert.
4582  * @param __n2 Number of characters from @a s to use.
4583  * @return Reference to this string.
4584  * @throw std::out_of_range If @a pos1 > size().
4585  * @throw std::length_error If new length exceeds @c max_size().
4586  *
4587  * Removes the characters in the range [__pos,__pos + __n1)
4588  * from this string. In place, the first @a __n2 characters of
4589  * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4590  * @a __pos is beyond end of string, out_of_range is thrown. If
4591  * the length of result exceeds max_size(), length_error is
4592  * thrown. The value of the string doesn't change if an error
4593  * is thrown.
4594  */
4595  basic_string&
4596  replace(size_type __pos, size_type __n1, const _CharT* __s,
4597  size_type __n2);
4598 
4599  /**
4600  * @brief Replace characters with value of a C string.
4601  * @param __pos Index of first character to replace.
4602  * @param __n1 Number of characters to be replaced.
4603  * @param __s C string to insert.
4604  * @return Reference to this string.
4605  * @throw std::out_of_range If @a pos > size().
4606  * @throw std::length_error If new length exceeds @c max_size().
4607  *
4608  * Removes the characters in the range [__pos,__pos + __n1)
4609  * from this string. In place, the characters of @a __s are
4610  * inserted. If @a __pos is beyond end of string, out_of_range
4611  * is thrown. If the length of result exceeds max_size(),
4612  * length_error is thrown. The value of the string doesn't
4613  * change if an error is thrown.
4614  */
4615  basic_string&
4616  replace(size_type __pos, size_type __n1, const _CharT* __s)
4617  {
4618  __glibcxx_requires_string(__s);
4619  return this->replace(__pos, __n1, __s, traits_type::length(__s));
4620  }
4621 
4622  /**
4623  * @brief Replace characters with multiple characters.
4624  * @param __pos Index of first character to replace.
4625  * @param __n1 Number of characters to be replaced.
4626  * @param __n2 Number of characters to insert.
4627  * @param __c Character to insert.
4628  * @return Reference to this string.
4629  * @throw std::out_of_range If @a __pos > size().
4630  * @throw std::length_error If new length exceeds @c max_size().
4631  *
4632  * Removes the characters in the range [pos,pos + n1) from this
4633  * string. In place, @a __n2 copies of @a __c are inserted.
4634  * If @a __pos is beyond end of string, out_of_range is thrown.
4635  * If the length of result exceeds max_size(), length_error is
4636  * thrown. The value of the string doesn't change if an error
4637  * is thrown.
4638  */
4639  basic_string&
4640  replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
4641  { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
4642  _M_limit(__pos, __n1), __n2, __c); }
4643 
4644  /**
4645  * @brief Replace range of characters with string.
4646  * @param __i1 Iterator referencing start of range to replace.
4647  * @param __i2 Iterator referencing end of range to replace.
4648  * @param __str String value to insert.
4649  * @return Reference to this string.
4650  * @throw std::length_error If new length exceeds @c max_size().
4651  *
4652  * Removes the characters in the range [__i1,__i2). In place,
4653  * the value of @a __str is inserted. If the length of result
4654  * exceeds max_size(), length_error is thrown. The value of
4655  * the string doesn't change if an error is thrown.
4656  */
4657  basic_string&
4658  replace(iterator __i1, iterator __i2, const basic_string& __str)
4659  { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
4660 
4661  /**
4662  * @brief Replace range of characters with C substring.
4663  * @param __i1 Iterator referencing start of range to replace.
4664  * @param __i2 Iterator referencing end of range to replace.
4665  * @param __s C string value to insert.
4666  * @param __n Number of characters from s to insert.
4667  * @return Reference to this string.
4668  * @throw std::length_error If new length exceeds @c max_size().
4669  *
4670  * Removes the characters in the range [__i1,__i2). In place,
4671  * the first @a __n characters of @a __s are inserted. If the
4672  * length of result exceeds max_size(), length_error is thrown.
4673  * The value of the string doesn't change if an error is
4674  * thrown.
4675  */
4676  basic_string&
4677  replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
4678  {
4679  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4680  && __i2 <= _M_iend());
4681  return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
4682  }
4683 
4684  /**
4685  * @brief Replace range of characters with C string.
4686  * @param __i1 Iterator referencing start of range to replace.
4687  * @param __i2 Iterator referencing end of range to replace.
4688  * @param __s C string value to insert.
4689  * @return Reference to this string.
4690  * @throw std::length_error If new length exceeds @c max_size().
4691  *
4692  * Removes the characters in the range [__i1,__i2). In place,
4693  * the characters of @a __s are inserted. If the length of
4694  * result exceeds max_size(), length_error is thrown. The
4695  * value of the string doesn't change if an error is thrown.
4696  */
4697  basic_string&
4698  replace(iterator __i1, iterator __i2, const _CharT* __s)
4699  {
4700  __glibcxx_requires_string(__s);
4701  return this->replace(__i1, __i2, __s, traits_type::length(__s));
4702  }
4703 
4704  /**
4705  * @brief Replace range of characters with multiple characters
4706  * @param __i1 Iterator referencing start of range to replace.
4707  * @param __i2 Iterator referencing end of range to replace.
4708  * @param __n Number of characters to insert.
4709  * @param __c Character to insert.
4710  * @return Reference to this string.
4711  * @throw std::length_error If new length exceeds @c max_size().
4712  *
4713  * Removes the characters in the range [__i1,__i2). In place,
4714  * @a __n copies of @a __c are inserted. If the length of
4715  * result exceeds max_size(), length_error is thrown. The
4716  * value of the string doesn't change if an error is thrown.
4717  */
4718  basic_string&
4719  replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
4720  {
4721  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4722  && __i2 <= _M_iend());
4723  return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
4724  }
4725 
4726  /**
4727  * @brief Replace range of characters with range.
4728  * @param __i1 Iterator referencing start of range to replace.
4729  * @param __i2 Iterator referencing end of range to replace.
4730  * @param __k1 Iterator referencing start of range to insert.
4731  * @param __k2 Iterator referencing end of range to insert.
4732  * @return Reference to this string.
4733  * @throw std::length_error If new length exceeds @c max_size().
4734  *
4735  * Removes the characters in the range [__i1,__i2). In place,
4736  * characters in the range [__k1,__k2) are inserted. If the
4737  * length of result exceeds max_size(), length_error is thrown.
4738  * The value of the string doesn't change if an error is
4739  * thrown.
4740  */
4741  template<class _InputIterator>
4742  basic_string&
4743  replace(iterator __i1, iterator __i2,
4744  _InputIterator __k1, _InputIterator __k2)
4745  {
4746  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4747  && __i2 <= _M_iend());
4748  __glibcxx_requires_valid_range(__k1, __k2);
4749  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
4750  return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
4751  }
4752 
4753  // Specializations for the common case of pointer and iterator:
4754  // useful to avoid the overhead of temporary buffering in _M_replace.
4755  basic_string&
4756  replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
4757  {
4758  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4759  && __i2 <= _M_iend());
4760  __glibcxx_requires_valid_range(__k1, __k2);
4761  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4762  __k1, __k2 - __k1);
4763  }
4764 
4765  basic_string&
4766  replace(iterator __i1, iterator __i2,
4767  const _CharT* __k1, const _CharT* __k2)
4768  {
4769  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4770  && __i2 <= _M_iend());
4771  __glibcxx_requires_valid_range(__k1, __k2);
4772  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4773  __k1, __k2 - __k1);
4774  }
4775 
4776  basic_string&
4777  replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
4778  {
4779  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4780  && __i2 <= _M_iend());
4781  __glibcxx_requires_valid_range(__k1, __k2);
4782  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4783  __k1.base(), __k2 - __k1);
4784  }
4785 
4786  basic_string&
4787  replace(iterator __i1, iterator __i2,
4788  const_iterator __k1, const_iterator __k2)
4789  {
4790  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4791  && __i2 <= _M_iend());
4792  __glibcxx_requires_valid_range(__k1, __k2);
4793  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4794  __k1.base(), __k2 - __k1);
4795  }
4796 
4797 #if __cplusplus >= 201103L
4798  /**
4799  * @brief Replace range of characters with initializer_list.
4800  * @param __i1 Iterator referencing start of range to replace.
4801  * @param __i2 Iterator referencing end of range to replace.
4802  * @param __l The initializer_list of characters to insert.
4803  * @return Reference to this string.
4804  * @throw std::length_error If new length exceeds @c max_size().
4805  *
4806  * Removes the characters in the range [__i1,__i2). In place,
4807  * characters in the range [__k1,__k2) are inserted. If the
4808  * length of result exceeds max_size(), length_error is thrown.
4809  * The value of the string doesn't change if an error is
4810  * thrown.
4811  */
4812  basic_string& replace(iterator __i1, iterator __i2,
4814  { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
4815 #endif // C++11
4816 
4817 #if __cplusplus > 201402L
4818  /**
4819  * @brief Replace range of characters with string_view.
4820  * @param __pos The position to replace at.
4821  * @param __n The number of characters to replace.
4822  * @param __sv The string_view to insert.
4823  * @return Reference to this string.
4824  */
4825  basic_string&
4826  replace(size_type __pos, size_type __n, __sv_type __sv)
4827  { return this->replace(__pos, __n, __sv.data(), __sv.size()); }
4828 
4829  /**
4830  * @brief Replace range of characters with string_view.
4831  * @param __pos1 The position to replace at.
4832  * @param __n1 The number of characters to replace.
4833  * @param __sv The string_view to insert from.
4834  * @param __pos2 The position in the string_view to insert from.
4835  * @param __n2 The number of characters to insert.
4836  * @return Reference to this string.
4837  */
4838  template <typename _Tp>
4839  _If_sv<_Tp, basic_string&>
4840  replace(size_type __pos1, size_type __n1, const _Tp& __svt,
4841  size_type __pos2, size_type __n2 = npos)
4842  {
4843  __sv_type __sv = __svt;
4844  return this->replace(__pos1, __n1, __sv.data()
4845  + __sv._M_check(__pos2, "basic_string::replace"),
4846  __sv._M_limit(__pos2, __n2));
4847  }
4848 
4849  /**
4850  * @brief Replace range of characters with string_view.
4851  * @param __i1 An iterator referencing the start position
4852  to replace at.
4853  * @param __i2 An iterator referencing the end position
4854  for the replace.
4855  * @param __sv The string_view to insert from.
4856  * @return Reference to this string.
4857  */
4858  basic_string&
4859  replace(const_iterator __i1, const_iterator __i2, __sv_type __sv)
4860  { return this->replace(__i1 - begin(), __i2 - __i1, __sv); }
4861 #endif // C++17
4862 
4863  private:
4864  template<class _Integer>
4865  basic_string&
4866  _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
4867  _Integer __val, __true_type)
4868  { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
4869 
4870  template<class _InputIterator>
4871  basic_string&
4872  _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
4873  _InputIterator __k2, __false_type);
4874 
4875  basic_string&
4876  _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
4877  _CharT __c);
4878 
4879  basic_string&
4880  _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
4881  size_type __n2);
4882 
4883  // _S_construct_aux is used to implement the 21.3.1 para 15 which
4884  // requires special behaviour if _InIter is an integral type
4885  template<class _InIterator>
4886  static _CharT*
4887  _S_construct_aux(_InIterator __beg, _InIterator __end,
4888  const _Alloc& __a, __false_type)
4889  {
4890  typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
4891  return _S_construct(__beg, __end, __a, _Tag());
4892  }
4893 
4894  // _GLIBCXX_RESOLVE_LIB_DEFECTS
4895  // 438. Ambiguity in the "do the right thing" clause
4896  template<class _Integer>
4897  static _CharT*
4898  _S_construct_aux(_Integer __beg, _Integer __end,
4899  const _Alloc& __a, __true_type)
4900  { return _S_construct_aux_2(static_cast<size_type>(__beg),
4901  __end, __a); }
4902 
4903  static _CharT*
4904  _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
4905  { return _S_construct(__req, __c, __a); }
4906 
4907  template<class _InIterator>
4908  static _CharT*
4909  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
4910  {
4911  typedef typename std::__is_integer<_InIterator>::__type _Integral;
4912  return _S_construct_aux(__beg, __end, __a, _Integral());
4913  }
4914 
4915  // For Input Iterators, used in istreambuf_iterators, etc.
4916  template<class _InIterator>
4917  static _CharT*
4918  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
4920 
4921  // For forward_iterators up to random_access_iterators, used for
4922  // string::iterator, _CharT*, etc.
4923  template<class _FwdIterator>
4924  static _CharT*
4925  _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
4927 
4928  static _CharT*
4929  _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
4930 
4931  public:
4932 
4933  /**
4934  * @brief Copy substring into C string.
4935  * @param __s C string to copy value into.
4936  * @param __n Number of characters to copy.
4937  * @param __pos Index of first character to copy.
4938  * @return Number of characters actually copied
4939  * @throw std::out_of_range If __pos > size().
4940  *
4941  * Copies up to @a __n characters starting at @a __pos into the
4942  * C string @a __s. If @a __pos is %greater than size(),
4943  * out_of_range is thrown.
4944  */
4945  size_type
4946  copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
4947 
4948  /**
4949  * @brief Swap contents with another string.
4950  * @param __s String to swap with.
4951  *
4952  * Exchanges the contents of this string with that of @a __s in constant
4953  * time.
4954  */
4955  // PR 58265, this should be noexcept.
4956  void
4957  swap(basic_string& __s);
4958 
4959  // String operations:
4960  /**
4961  * @brief Return const pointer to null-terminated contents.
4962  *
4963  * This is a handle to internal data. Do not modify or dire things may
4964  * happen.
4965  */
4966  const _CharT*
4967  c_str() const _GLIBCXX_NOEXCEPT
4968  { return _M_data(); }
4969 
4970  /**
4971  * @brief Return const pointer to contents.
4972  *
4973  * This is a pointer to internal data. It is undefined to modify
4974  * the contents through the returned pointer. To get a pointer that
4975  * allows modifying the contents use @c &str[0] instead,
4976  * (or in C++17 the non-const @c str.data() overload).
4977  */
4978  const _CharT*
4979  data() const _GLIBCXX_NOEXCEPT
4980  { return _M_data(); }
4981 
4982 #if __cplusplus > 201402L
4983  /**
4984  * @brief Return non-const pointer to contents.
4985  *
4986  * This is a pointer to the character sequence held by the string.
4987  * Modifying the characters in the sequence is allowed.
4988  */
4989  _CharT*
4990  data() noexcept
4991  { return _M_data(); }
4992 #endif
4993 
4994  /**
4995  * @brief Return copy of allocator used to construct this string.
4996  */
4997  allocator_type
4998  get_allocator() const _GLIBCXX_NOEXCEPT
4999  { return _M_dataplus; }
5000 
5001  /**
5002  * @brief Find position of a C substring.
5003  * @param __s C string to locate.
5004  * @param __pos Index of character to search from.
5005  * @param __n Number of characters from @a s to search for.
5006  * @return Index of start of first occurrence.
5007  *
5008  * Starting from @a __pos, searches forward for the first @a
5009  * __n characters in @a __s within this string. If found,
5010  * returns the index where it begins. If not found, returns
5011  * npos.
5012  */
5013  size_type
5014  find(const _CharT* __s, size_type __pos, size_type __n) const
5015  _GLIBCXX_NOEXCEPT;
5016 
5017  /**
5018  * @brief Find position of a string.
5019  * @param __str String to locate.
5020  * @param __pos Index of character to search from (default 0).
5021  * @return Index of start of first occurrence.
5022  *
5023  * Starting from @a __pos, searches forward for value of @a __str within
5024  * this string. If found, returns the index where it begins. If not
5025  * found, returns npos.
5026  */
5027  size_type
5028  find(const basic_string& __str, size_type __pos = 0) const
5029  _GLIBCXX_NOEXCEPT
5030  { return this->find(__str.data(), __pos, __str.size()); }
5031 
5032  /**
5033  * @brief Find position of a C string.
5034  * @param __s C string to locate.
5035  * @param __pos Index of character to search from (default 0).
5036  * @return Index of start of first occurrence.
5037  *
5038  * Starting from @a __pos, searches forward for the value of @a
5039  * __s within this string. If found, returns the index where
5040  * it begins. If not found, returns npos.
5041  */
5042  size_type
5043  find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
5044  {
5045  __glibcxx_requires_string(__s);
5046  return this->find(__s, __pos, traits_type::length(__s));
5047  }
5048 
5049  /**
5050  * @brief Find position of a character.
5051  * @param __c Character to locate.
5052  * @param __pos Index of character to search from (default 0).
5053  * @return Index of first occurrence.
5054  *
5055  * Starting from @a __pos, searches forward for @a __c within
5056  * this string. If found, returns the index where it was
5057  * found. If not found, returns npos.
5058  */
5059  size_type
5060  find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
5061 
5062 #if __cplusplus > 201402L
5063  /**
5064  * @brief Find position of a string_view.
5065  * @param __sv The string_view to locate.
5066  * @param __pos Index of character to search from (default 0).
5067  * @return Index of start of first occurrence.
5068  */
5069  size_type
5070  find(__sv_type __sv, size_type __pos = 0) const noexcept
5071  { return this->find(__sv.data(), __pos, __sv.size()); }
5072 #endif // C++17
5073 
5074  /**
5075  * @brief Find last position of a string.
5076  * @param __str String to locate.
5077  * @param __pos Index of character to search back from (default end).
5078  * @return Index of start of last occurrence.
5079  *
5080  * Starting from @a __pos, searches backward for value of @a
5081  * __str within this string. If found, returns the index where
5082  * it begins. If not found, returns npos.
5083  */
5084  size_type
5085  rfind(const basic_string& __str, size_type __pos = npos) const
5086  _GLIBCXX_NOEXCEPT
5087  { return this->rfind(__str.data(), __pos, __str.size()); }
5088 
5089  /**
5090  * @brief Find last position of a C substring.
5091  * @param __s C string to locate.
5092  * @param __pos Index of character to search back from.
5093  * @param __n Number of characters from s to search for.
5094  * @return Index of start of last occurrence.
5095  *
5096  * Starting from @a __pos, searches backward for the first @a
5097  * __n characters in @a __s within this string. If found,
5098  * returns the index where it begins. If not found, returns
5099  * npos.
5100  */
5101  size_type
5102  rfind(const _CharT* __s, size_type __pos, size_type __n) const
5103  _GLIBCXX_NOEXCEPT;
5104 
5105  /**
5106  * @brief Find last position of a C string.
5107  * @param __s C string to locate.
5108  * @param __pos Index of character to start search at (default end).
5109  * @return Index of start of last occurrence.
5110  *
5111  * Starting from @a __pos, searches backward for the value of
5112  * @a __s within this string. If found, returns the index
5113  * where it begins. If not found, returns npos.
5114  */
5115  size_type
5116  rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
5117  {
5118  __glibcxx_requires_string(__s);
5119  return this->rfind(__s, __pos, traits_type::length(__s));
5120  }
5121 
5122  /**
5123  * @brief Find last position of a character.
5124  * @param __c Character to locate.
5125  * @param __pos Index of character to search back from (default end).
5126  * @return Index of last occurrence.
5127  *
5128  * Starting from @a __pos, searches backward for @a __c within
5129  * this string. If found, returns the index where it was
5130  * found. If not found, returns npos.
5131  */
5132  size_type
5133  rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
5134 
5135 #if __cplusplus > 201402L
5136  /**
5137  * @brief Find last position of a string_view.
5138  * @param __sv The string_view to locate.
5139  * @param __pos Index of character to search back from (default end).
5140  * @return Index of start of last occurrence.
5141  */
5142  size_type
5143  rfind(__sv_type __sv, size_type __pos = npos) const noexcept
5144  { return this->rfind(__sv.data(), __pos, __sv.size()); }
5145 #endif // C++17
5146 
5147  /**
5148  * @brief Find position of a character of string.
5149  * @param __str String containing characters to locate.
5150  * @param __pos Index of character to search from (default 0).
5151  * @return Index of first occurrence.
5152  *
5153  * Starting from @a __pos, searches forward for one of the
5154  * characters of @a __str within this string. If found,
5155  * returns the index where it was found. If not found, returns
5156  * npos.
5157  */
5158  size_type
5159  find_first_of(const basic_string& __str, size_type __pos = 0) const
5160  _GLIBCXX_NOEXCEPT
5161  { return this->find_first_of(__str.data(), __pos, __str.size()); }
5162 
5163  /**
5164  * @brief Find position of a character of C substring.
5165  * @param __s String containing characters to locate.
5166  * @param __pos Index of character to search from.
5167  * @param __n Number of characters from s to search for.
5168  * @return Index of first occurrence.
5169  *
5170  * Starting from @a __pos, searches forward for one of the
5171  * first @a __n characters of @a __s within this string. If
5172  * found, returns the index where it was found. If not found,
5173  * returns npos.
5174  */
5175  size_type
5176  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
5177  _GLIBCXX_NOEXCEPT;
5178 
5179  /**
5180  * @brief Find position of a character of C string.
5181  * @param __s String containing characters to locate.
5182  * @param __pos Index of character to search from (default 0).
5183  * @return Index of first occurrence.
5184  *
5185  * Starting from @a __pos, searches forward for one of the
5186  * characters of @a __s within this string. If found, returns
5187  * the index where it was found. If not found, returns npos.
5188  */
5189  size_type
5190  find_first_of(const _CharT* __s, size_type __pos = 0) const
5191  _GLIBCXX_NOEXCEPT
5192  {
5193  __glibcxx_requires_string(__s);
5194  return this->find_first_of(__s, __pos, traits_type::length(__s));
5195  }
5196 
5197  /**
5198  * @brief Find position of a character.
5199  * @param __c Character to locate.
5200  * @param __pos Index of character to search from (default 0).
5201  * @return Index of first occurrence.
5202  *
5203  * Starting from @a __pos, searches forward for the character
5204  * @a __c within this string. If found, returns the index
5205  * where it was found. If not found, returns npos.
5206  *
5207  * Note: equivalent to find(__c, __pos).
5208  */
5209  size_type
5210  find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
5211  { return this->find(__c, __pos); }
5212 
5213 #if __cplusplus > 201402L
5214  /**
5215  * @brief Find position of a character of a string_view.
5216  * @param __sv A string_view containing characters to locate.
5217  * @param __pos Index of character to search from (default 0).
5218  * @return Index of first occurrence.
5219  */
5220  size_type
5221  find_first_of(__sv_type __sv, size_type __pos = 0) const noexcept
5222  { return this->find_first_of(__sv.data(), __pos, __sv.size()); }
5223 #endif // C++17
5224 
5225  /**
5226  * @brief Find last position of a character of string.
5227  * @param __str String containing characters to locate.
5228  * @param __pos Index of character to search back from (default end).
5229  * @return Index of last occurrence.
5230  *
5231  * Starting from @a __pos, searches backward for one of the
5232  * characters of @a __str within this string. If found,
5233  * returns the index where it was found. If not found, returns
5234  * npos.
5235  */
5236  size_type
5237  find_last_of(const basic_string& __str, size_type __pos = npos) const
5238  _GLIBCXX_NOEXCEPT
5239  { return this->find_last_of(__str.data(), __pos, __str.size()); }
5240 
5241  /**
5242  * @brief Find last position of a character of C substring.
5243  * @param __s C string containing characters to locate.
5244  * @param __pos Index of character to search back from.
5245  * @param __n Number of characters from s to search for.
5246  * @return Index of last occurrence.
5247  *
5248  * Starting from @a __pos, searches backward for one of the
5249  * first @a __n characters of @a __s within this string. If
5250  * found, returns the index where it was found. If not found,
5251  * returns npos.
5252  */
5253  size_type
5254  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
5255  _GLIBCXX_NOEXCEPT;
5256 
5257  /**
5258  * @brief Find last position of a character of C string.
5259  * @param __s C string containing characters to locate.
5260  * @param __pos Index of character to search back from (default end).
5261  * @return Index of last occurrence.
5262  *
5263  * Starting from @a __pos, searches backward for one of the
5264  * characters of @a __s within this string. If found, returns
5265  * the index where it was found. If not found, returns npos.
5266  */
5267  size_type
5268  find_last_of(const _CharT* __s, size_type __pos = npos) const
5269  _GLIBCXX_NOEXCEPT
5270  {
5271  __glibcxx_requires_string(__s);
5272  return this->find_last_of(__s, __pos, traits_type::length(__s));
5273  }
5274 
5275  /**
5276  * @brief Find last position of a character.
5277  * @param __c Character to locate.
5278  * @param __pos Index of character to search back from (default end).
5279  * @return Index of last occurrence.
5280  *
5281  * Starting from @a __pos, searches backward for @a __c within
5282  * this string. If found, returns the index where it was
5283  * found. If not found, returns npos.
5284  *
5285  * Note: equivalent to rfind(__c, __pos).
5286  */
5287  size_type
5288  find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
5289  { return this->rfind(__c, __pos); }
5290 
5291 #if __cplusplus > 201402L
5292  /**
5293  * @brief Find last position of a character of string.
5294  * @param __sv A string_view containing characters to locate.
5295  * @param __pos Index of character to search back from (default end).
5296  * @return Index of last occurrence.
5297  */
5298  size_type
5299  find_last_of(__sv_type __sv, size_type __pos = npos) const noexcept
5300  { return this->find_last_of(__sv.data(), __pos, __sv.size()); }
5301 #endif // C++17
5302 
5303  /**
5304  * @brief Find position of a character not in string.
5305  * @param __str String containing characters to avoid.
5306  * @param __pos Index of character to search from (default 0).
5307  * @return Index of first occurrence.
5308  *
5309  * Starting from @a __pos, searches forward for a character not contained
5310  * in @a __str within this string. If found, returns the index where it
5311  * was found. If not found, returns npos.
5312  */
5313  size_type
5314  find_first_not_of(const basic_string& __str, size_type __pos = 0) const
5315  _GLIBCXX_NOEXCEPT
5316  { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
5317 
5318  /**
5319  * @brief Find position of a character not in C substring.
5320  * @param __s C string containing characters to avoid.
5321  * @param __pos Index of character to search from.
5322  * @param __n Number of characters from __s to consider.
5323  * @return Index of first occurrence.
5324  *
5325  * Starting from @a __pos, searches forward for a character not
5326  * contained in the first @a __n characters of @a __s within
5327  * this string. If found, returns the index where it was
5328  * found. If not found, returns npos.
5329  */
5330  size_type
5331  find_first_not_of(const _CharT* __s, size_type __pos,
5332  size_type __n) const _GLIBCXX_NOEXCEPT;
5333 
5334  /**
5335  * @brief Find position of a character not in C string.
5336  * @param __s C string containing characters to avoid.
5337  * @param __pos Index of character to search from (default 0).
5338  * @return Index of first occurrence.
5339  *
5340  * Starting from @a __pos, searches forward for a character not
5341  * contained in @a __s within this string. If found, returns
5342  * the index where it was found. If not found, returns npos.
5343  */
5344  size_type
5345  find_first_not_of(const _CharT* __s, size_type __pos = 0) const
5346  _GLIBCXX_NOEXCEPT
5347  {
5348  __glibcxx_requires_string(__s);
5349  return this->find_first_not_of(__s, __pos, traits_type::length(__s));
5350  }
5351 
5352  /**
5353  * @brief Find position of a different character.
5354  * @param __c Character to avoid.
5355  * @param __pos Index of character to search from (default 0).
5356  * @return Index of first occurrence.
5357  *
5358  * Starting from @a __pos, searches forward for a character
5359  * other than @a __c within this string. If found, returns the
5360  * index where it was found. If not found, returns npos.
5361  */
5362  size_type
5363  find_first_not_of(_CharT __c, size_type __pos = 0) const
5364  _GLIBCXX_NOEXCEPT;
5365 
5366 #if __cplusplus > 201402L
5367  /**
5368  * @brief Find position of a character not in a string_view.
5369  * @param __sv A string_view containing characters to avoid.
5370  * @param __pos Index of character to search from (default 0).
5371  * @return Index of first occurrence.
5372  */
5373  size_type
5374  find_first_not_of(__sv_type __sv, size_type __pos = 0) const noexcept
5375  { return this->find_first_not_of(__sv.data(), __pos, __sv.size()); }
5376 #endif // C++17
5377 
5378  /**
5379  * @brief Find last position of a character not in string.
5380  * @param __str String containing characters to avoid.
5381  * @param __pos Index of character to search back from (default end).
5382  * @return Index of last occurrence.
5383  *
5384  * Starting from @a __pos, searches backward for a character
5385  * not contained in @a __str within this string. If found,
5386  * returns the index where it was found. If not found, returns
5387  * npos.
5388  */
5389  size_type
5390  find_last_not_of(const basic_string& __str, size_type __pos = npos) const
5391  _GLIBCXX_NOEXCEPT
5392  { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
5393 
5394  /**
5395  * @brief Find last position of a character not in C substring.
5396  * @param __s C string containing characters to avoid.
5397  * @param __pos Index of character to search back from.
5398  * @param __n Number of characters from s to consider.
5399  * @return Index of last occurrence.
5400  *
5401  * Starting from @a __pos, searches backward for a character not
5402  * contained in the first @a __n characters of @a __s within this string.
5403  * If found, returns the index where it was found. If not found,
5404  * returns npos.
5405  */
5406  size_type
5407  find_last_not_of(const _CharT* __s, size_type __pos,
5408  size_type __n) const _GLIBCXX_NOEXCEPT;
5409  /**
5410  * @brief Find last position of a character not in C string.
5411  * @param __s C string containing characters to avoid.
5412  * @param __pos Index of character to search back from (default end).
5413  * @return Index of last occurrence.
5414  *
5415  * Starting from @a __pos, searches backward for a character
5416  * not contained in @a __s within this string. If found,
5417  * returns the index where it was found. If not found, returns
5418  * npos.
5419  */
5420  size_type
5421  find_last_not_of(const _CharT* __s, size_type __pos = npos) const
5422  _GLIBCXX_NOEXCEPT
5423  {
5424  __glibcxx_requires_string(__s);
5425  return this->find_last_not_of(__s, __pos, traits_type::length(__s));
5426  }
5427 
5428  /**
5429  * @brief Find last position of a different character.
5430  * @param __c Character to avoid.
5431  * @param __pos Index of character to search back from (default end).
5432  * @return Index of last occurrence.
5433  *
5434  * Starting from @a __pos, searches backward for a character other than
5435  * @a __c within this string. If found, returns the index where it was
5436  * found. If not found, returns npos.
5437  */
5438  size_type
5439  find_last_not_of(_CharT __c, size_type __pos = npos) const
5440  _GLIBCXX_NOEXCEPT;
5441 
5442 #if __cplusplus > 201402L
5443  /**
5444  * @brief Find last position of a character not in a string_view.
5445  * @param __sv A string_view containing characters to avoid.
5446  * @param __pos Index of character to search back from (default end).
5447  * @return Index of last occurrence.
5448  */
5449  size_type
5450  find_last_not_of(__sv_type __sv, size_type __pos = npos) const noexcept
5451  { return this->find_last_not_of(__sv.data(), __pos, __sv.size()); }
5452 #endif // C++17
5453 
5454  /**
5455  * @brief Get a substring.
5456  * @param __pos Index of first character (default 0).
5457  * @param __n Number of characters in substring (default remainder).
5458  * @return The new string.
5459  * @throw std::out_of_range If __pos > size().
5460  *
5461  * Construct and return a new string using the @a __n
5462  * characters starting at @a __pos. If the string is too
5463  * short, use the remainder of the characters. If @a __pos is
5464  * beyond the end of the string, out_of_range is thrown.
5465  */
5466  basic_string
5467  substr(size_type __pos = 0, size_type __n = npos) const
5468  { return basic_string(*this,
5469  _M_check(__pos, "basic_string::substr"), __n); }
5470 
5471  /**
5472  * @brief Compare to a string.
5473  * @param __str String to compare against.
5474  * @return Integer < 0, 0, or > 0.
5475  *
5476  * Returns an integer < 0 if this string is ordered before @a
5477  * __str, 0 if their values are equivalent, or > 0 if this
5478  * string is ordered after @a __str. Determines the effective
5479  * length rlen of the strings to compare as the smallest of
5480  * size() and str.size(). The function then compares the two
5481  * strings by calling traits::compare(data(), str.data(),rlen).
5482  * If the result of the comparison is nonzero returns it,
5483  * otherwise the shorter one is ordered first.
5484  */
5485  int
5486  compare(const basic_string& __str) const
5487  {
5488  const size_type __size = this->size();
5489  const size_type __osize = __str.size();
5490  const size_type __len = std::min(__size, __osize);
5491 
5492  int __r = traits_type::compare(_M_data(), __str.data(), __len);
5493  if (!__r)
5494  __r = _S_compare(__size, __osize);
5495  return __r;
5496  }
5497 
5498 #if __cplusplus > 201402L
5499  /**
5500  * @brief Compare to a string_view.
5501  * @param __sv A string_view to compare against.
5502  * @return Integer < 0, 0, or > 0.
5503  */
5504  int
5505  compare(__sv_type __sv) const
5506  {
5507  const size_type __size = this->size();
5508  const size_type __osize = __sv.size();
5509  const size_type __len = std::min(__size, __osize);
5510 
5511  int __r = traits_type::compare(_M_data(), __sv.data(), __len);
5512  if (!__r)
5513  __r = _S_compare(__size, __osize);
5514  return __r;
5515  }
5516 
5517  /**
5518  * @brief Compare to a string_view.
5519  * @param __pos A position in the string to start comparing from.
5520  * @param __n The number of characters to compare.
5521  * @param __sv A string_view to compare against.
5522  * @return Integer < 0, 0, or > 0.
5523  */
5524  int
5525  compare(size_type __pos, size_type __n, __sv_type __sv) const
5526  { return __sv_type(*this).substr(__pos, __n).compare(__sv); }
5527 
5528  /**
5529  * @brief Compare to a string_view.
5530  * @param __pos1 A position in the string to start comparing from.
5531  * @param __n1 The number of characters to compare.
5532  * @param __sv A string_view to compare against.
5533  * @param __pos2 A position in the string_view to start comparing from.
5534  * @param __n2 The number of characters to compare.
5535  * @return Integer < 0, 0, or > 0.
5536  */
5537  template <typename _Tp>
5538  _If_sv<_Tp, int>
5539  compare(size_type __pos1, size_type __n1, const _Tp& __svt,
5540  size_type __pos2, size_type __n2 = npos) const
5541  {
5542  __sv_type __sv = __svt;
5543  return __sv_type(*this)
5544  .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
5545  }
5546 #endif // C++17
5547 
5548  /**
5549  * @brief Compare substring to a string.
5550  * @param __pos Index of first character of substring.
5551  * @param __n Number of characters in substring.
5552  * @param __str String to compare against.
5553  * @return Integer < 0, 0, or > 0.
5554  *
5555  * Form the substring of this string from the @a __n characters
5556  * starting at @a __pos. Returns an integer < 0 if the
5557  * substring is ordered before @a __str, 0 if their values are
5558  * equivalent, or > 0 if the substring is ordered after @a
5559  * __str. Determines the effective length rlen of the strings
5560  * to compare as the smallest of the length of the substring
5561  * and @a __str.size(). The function then compares the two
5562  * strings by calling
5563  * traits::compare(substring.data(),str.data(),rlen). If the
5564  * result of the comparison is nonzero returns it, otherwise
5565  * the shorter one is ordered first.
5566  */
5567  int
5568  compare(size_type __pos, size_type __n, const basic_string& __str) const;
5569 
5570  /**
5571  * @brief Compare substring to a substring.
5572  * @param __pos1 Index of first character of substring.
5573  * @param __n1 Number of characters in substring.
5574  * @param __str String to compare against.
5575  * @param __pos2 Index of first character of substring of str.
5576  * @param __n2 Number of characters in substring of str.
5577  * @return Integer < 0, 0, or > 0.
5578  *
5579  * Form the substring of this string from the @a __n1
5580  * characters starting at @a __pos1. Form the substring of @a
5581  * __str from the @a __n2 characters starting at @a __pos2.
5582  * Returns an integer < 0 if this substring is ordered before
5583  * the substring of @a __str, 0 if their values are equivalent,
5584  * or > 0 if this substring is ordered after the substring of
5585  * @a __str. Determines the effective length rlen of the
5586  * strings to compare as the smallest of the lengths of the
5587  * substrings. The function then compares the two strings by
5588  * calling
5589  * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5590  * If the result of the comparison is nonzero returns it,
5591  * otherwise the shorter one is ordered first.
5592  */
5593  int
5594  compare(size_type __pos1, size_type __n1, const basic_string& __str,
5595  size_type __pos2, size_type __n2) const;
5596 
5597  /**
5598  * @brief Compare to a C string.
5599  * @param __s C string to compare against.
5600  * @return Integer < 0, 0, or > 0.
5601  *
5602  * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5603  * their values are equivalent, or > 0 if this string is ordered after
5604  * @a __s. Determines the effective length rlen of the strings to
5605  * compare as the smallest of size() and the length of a string
5606  * constructed from @a __s. The function then compares the two strings
5607  * by calling traits::compare(data(),s,rlen). If the result of the
5608  * comparison is nonzero returns it, otherwise the shorter one is
5609  * ordered first.
5610  */
5611  int
5612  compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
5613 
5614  // _GLIBCXX_RESOLVE_LIB_DEFECTS
5615  // 5 String::compare specification questionable
5616  /**
5617  * @brief Compare substring to a C string.
5618  * @param __pos Index of first character of substring.
5619  * @param __n1 Number of characters in substring.
5620  * @param __s C string to compare against.
5621  * @return Integer < 0, 0, or > 0.
5622  *
5623  * Form the substring of this string from the @a __n1
5624  * characters starting at @a pos. Returns an integer < 0 if
5625  * the substring is ordered before @a __s, 0 if their values
5626  * are equivalent, or > 0 if the substring is ordered after @a
5627  * __s. Determines the effective length rlen of the strings to
5628  * compare as the smallest of the length of the substring and
5629  * the length of a string constructed from @a __s. The
5630  * function then compares the two string by calling
5631  * traits::compare(substring.data(),__s,rlen). If the result of
5632  * the comparison is nonzero returns it, otherwise the shorter
5633  * one is ordered first.
5634  */
5635  int
5636  compare(size_type __pos, size_type __n1, const _CharT* __s) const;
5637 
5638  /**
5639  * @brief Compare substring against a character %array.
5640  * @param __pos Index of first character of substring.
5641  * @param __n1 Number of characters in substring.
5642  * @param __s character %array to compare against.
5643  * @param __n2 Number of characters of s.
5644  * @return Integer < 0, 0, or > 0.
5645  *
5646  * Form the substring of this string from the @a __n1
5647  * characters starting at @a __pos. Form a string from the
5648  * first @a __n2 characters of @a __s. Returns an integer < 0
5649  * if this substring is ordered before the string from @a __s,
5650  * 0 if their values are equivalent, or > 0 if this substring
5651  * is ordered after the string from @a __s. Determines the
5652  * effective length rlen of the strings to compare as the
5653  * smallest of the length of the substring and @a __n2. The
5654  * function then compares the two strings by calling
5655  * traits::compare(substring.data(),s,rlen). If the result of
5656  * the comparison is nonzero returns it, otherwise the shorter
5657  * one is ordered first.
5658  *
5659  * NB: s must have at least n2 characters, &apos;\\0&apos; has
5660  * no special meaning.
5661  */
5662  int
5663  compare(size_type __pos, size_type __n1, const _CharT* __s,
5664  size_type __n2) const;
5665 
5666 # ifdef _GLIBCXX_TM_TS_INTERNAL
5667  friend void
5668  ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
5669  void* exc);
5670  friend const char*
5671  ::_txnal_cow_string_c_str(const void *that);
5672  friend void
5673  ::_txnal_cow_string_D1(void *that);
5674  friend void
5675  ::_txnal_cow_string_D1_commit(void *that);
5676 # endif
5677  };
5678 #endif // !_GLIBCXX_USE_CXX11_ABI
5679 
5680  // operator+
5681  /**
5682  * @brief Concatenate two strings.
5683  * @param __lhs First string.
5684  * @param __rhs Last string.
5685  * @return New string with value of @a __lhs followed by @a __rhs.
5686  */
5687  template<typename _CharT, typename _Traits, typename _Alloc>
5691  {
5693  __str.append(__rhs);
5694  return __str;
5695  }
5696 
5697  /**
5698  * @brief Concatenate C string and string.
5699  * @param __lhs First string.
5700  * @param __rhs Last string.
5701  * @return New string with value of @a __lhs followed by @a __rhs.
5702  */
5703  template<typename _CharT, typename _Traits, typename _Alloc>
5705  operator+(const _CharT* __lhs,
5707 
5708  /**
5709  * @brief Concatenate character and string.
5710  * @param __lhs First string.
5711  * @param __rhs Last string.
5712  * @return New string with @a __lhs followed by @a __rhs.
5713  */
5714  template<typename _CharT, typename _Traits, typename _Alloc>
5716  operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
5717 
5718  /**
5719  * @brief Concatenate string and C string.
5720  * @param __lhs First string.
5721  * @param __rhs Last string.
5722  * @return New string with @a __lhs followed by @a __rhs.
5723  */
5724  template<typename _CharT, typename _Traits, typename _Alloc>
5727  const _CharT* __rhs)
5728  {
5730  __str.append(__rhs);
5731  return __str;
5732  }
5733 
5734  /**
5735  * @brief Concatenate string and character.
5736  * @param __lhs First string.
5737  * @param __rhs Last string.
5738  * @return New string with @a __lhs followed by @a __rhs.
5739  */
5740  template<typename _CharT, typename _Traits, typename _Alloc>
5743  {
5744  typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
5745  typedef typename __string_type::size_type __size_type;
5746  __string_type __str(__lhs);
5747  __str.append(__size_type(1), __rhs);
5748  return __str;
5749  }
5750 
5751 #if __cplusplus >= 201103L
5752  template<typename _CharT, typename _Traits, typename _Alloc>
5756  { return std::move(__lhs.append(__rhs)); }
5757 
5758  template<typename _CharT, typename _Traits, typename _Alloc>
5762  { return std::move(__rhs.insert(0, __lhs)); }
5763 
5764  template<typename _CharT, typename _Traits, typename _Alloc>
5768  {
5769  const auto __size = __lhs.size() + __rhs.size();
5770  const bool __cond = (__size > __lhs.capacity()
5771  && __size <= __rhs.capacity());
5772  return __cond ? std::move(__rhs.insert(0, __lhs))
5773  : std::move(__lhs.append(__rhs));
5774  }
5775 
5776  template<typename _CharT, typename _Traits, typename _Alloc>
5778  operator+(const _CharT* __lhs,
5780  { return std::move(__rhs.insert(0, __lhs)); }
5781 
5782  template<typename _CharT, typename _Traits, typename _Alloc>
5784  operator+(_CharT __lhs,
5786  { return std::move(__rhs.insert(0, 1, __lhs)); }
5787 
5788  template<typename _CharT, typename _Traits, typename _Alloc>
5791  const _CharT* __rhs)
5792  { return std::move(__lhs.append(__rhs)); }
5793 
5794  template<typename _CharT, typename _Traits, typename _Alloc>
5797  _CharT __rhs)
5798  { return std::move(__lhs.append(1, __rhs)); }
5799 #endif
5800 
5801  // operator ==
5802  /**
5803  * @brief Test equivalence of two strings.
5804  * @param __lhs First string.
5805  * @param __rhs Second string.
5806  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
5807  */
5808  template<typename _CharT, typename _Traits, typename _Alloc>
5809  inline bool
5810  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5812  _GLIBCXX_NOEXCEPT
5813  { return __lhs.compare(__rhs) == 0; }
5814 
5815  template<typename _CharT>
5816  inline
5817  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
5818  operator==(const basic_string<_CharT>& __lhs,
5819  const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT
5820  { return (__lhs.size() == __rhs.size()
5821  && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
5822  __lhs.size())); }
5823 
5824  /**
5825  * @brief Test equivalence of C string and string.
5826  * @param __lhs C string.
5827  * @param __rhs String.
5828  * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
5829  */
5830  template<typename _CharT, typename _Traits, typename _Alloc>
5831  inline bool
5832  operator==(const _CharT* __lhs,
5834  { return __rhs.compare(__lhs) == 0; }
5835 
5836  /**
5837  * @brief Test equivalence of string and C string.
5838  * @param __lhs String.
5839  * @param __rhs C string.
5840  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
5841  */
5842  template<typename _CharT, typename _Traits, typename _Alloc>
5843  inline bool
5844  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5845  const _CharT* __rhs)
5846  { return __lhs.compare(__rhs) == 0; }
5847 
5848  // operator !=
5849  /**
5850  * @brief Test difference of two strings.
5851  * @param __lhs First string.
5852  * @param __rhs Second string.
5853  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5854  */
5855  template<typename _CharT, typename _Traits, typename _Alloc>
5856  inline bool
5857  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5859  _GLIBCXX_NOEXCEPT
5860  { return !(__lhs == __rhs); }
5861 
5862  /**
5863  * @brief Test difference of C string and string.
5864  * @param __lhs C string.
5865  * @param __rhs String.
5866  * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
5867  */
5868  template<typename _CharT, typename _Traits, typename _Alloc>
5869  inline bool
5870  operator!=(const _CharT* __lhs,
5872  { return !(__lhs == __rhs); }
5873 
5874  /**
5875  * @brief Test difference of string and C string.
5876  * @param __lhs String.
5877  * @param __rhs C string.
5878  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5879  */
5880  template<typename _CharT, typename _Traits, typename _Alloc>
5881  inline bool
5882  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5883  const _CharT* __rhs)
5884  { return !(__lhs == __rhs); }
5885 
5886  // operator <
5887  /**
5888  * @brief Test if string precedes string.
5889  * @param __lhs First string.
5890  * @param __rhs Second string.
5891  * @return True if @a __lhs precedes @a __rhs. False otherwise.
5892  */
5893  template<typename _CharT, typename _Traits, typename _Alloc>
5894  inline bool
5895  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5897  _GLIBCXX_NOEXCEPT
5898  { return __lhs.compare(__rhs) < 0; }
5899 
5900  /**
5901  * @brief Test if string precedes C string.
5902  * @param __lhs String.
5903  * @param __rhs C string.
5904  * @return True if @a __lhs precedes @a __rhs. False otherwise.
5905  */
5906  template<typename _CharT, typename _Traits, typename _Alloc>
5907  inline bool
5908  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5909  const _CharT* __rhs)
5910  { return __lhs.compare(__rhs) < 0; }
5911 
5912  /**
5913  * @brief Test if C string precedes string.
5914  * @param __lhs C string.
5915  * @param __rhs String.
5916  * @return True if @a __lhs precedes @a __rhs. False otherwise.
5917  */
5918  template<typename _CharT, typename _Traits, typename _Alloc>
5919  inline bool
5920  operator<(const _CharT* __lhs,
5922  { return __rhs.compare(__lhs) > 0; }
5923 
5924  // operator >
5925  /**
5926  * @brief Test if string follows string.
5927  * @param __lhs First string.
5928  * @param __rhs Second string.
5929  * @return True if @a __lhs follows @a __rhs. False otherwise.
5930  */
5931  template<typename _CharT, typename _Traits, typename _Alloc>
5932  inline bool
5935  _GLIBCXX_NOEXCEPT
5936  { return __lhs.compare(__rhs) > 0; }
5937 
5938  /**
5939  * @brief Test if string follows C string.
5940  * @param __lhs String.
5941  * @param __rhs C string.
5942  * @return True if @a __lhs follows @a __rhs. False otherwise.
5943  */
5944  template<typename _CharT, typename _Traits, typename _Alloc>
5945  inline bool
5947  const _CharT* __rhs)
5948  { return __lhs.compare(__rhs) > 0; }
5949 
5950  /**
5951  * @brief Test if C string follows string.
5952  * @param __lhs C string.
5953  * @param __rhs String.
5954  * @return True if @a __lhs follows @a __rhs. False otherwise.
5955  */
5956  template<typename _CharT, typename _Traits, typename _Alloc>
5957  inline bool
5958  operator>(const _CharT* __lhs,
5960  { return __rhs.compare(__lhs) < 0; }
5961 
5962  // operator <=
5963  /**
5964  * @brief Test if string doesn't follow string.
5965  * @param __lhs First string.
5966  * @param __rhs Second string.
5967  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5968  */
5969  template<typename _CharT, typename _Traits, typename _Alloc>
5970  inline bool
5971  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5973  _GLIBCXX_NOEXCEPT
5974  { return __lhs.compare(__rhs) <= 0; }
5975 
5976  /**
5977  * @brief Test if string doesn't follow C string.
5978  * @param __lhs String.
5979  * @param __rhs C string.
5980  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5981  */
5982  template<typename _CharT, typename _Traits, typename _Alloc>
5983  inline bool
5984  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5985  const _CharT* __rhs)
5986  { return __lhs.compare(__rhs) <= 0; }
5987 
5988  /**
5989  * @brief Test if C string doesn't follow string.
5990  * @param __lhs C string.
5991  * @param __rhs String.
5992  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5993  */
5994  template<typename _CharT, typename _Traits, typename _Alloc>
5995  inline bool
5996  operator<=(const _CharT* __lhs,
5998  { return __rhs.compare(__lhs) >= 0; }
5999 
6000  // operator >=
6001  /**
6002  * @brief Test if string doesn't precede string.
6003  * @param __lhs First string.
6004  * @param __rhs Second string.
6005  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6006  */
6007  template<typename _CharT, typename _Traits, typename _Alloc>
6008  inline bool
6009  operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6011  _GLIBCXX_NOEXCEPT
6012  { return __lhs.compare(__rhs) >= 0; }
6013 
6014  /**
6015  * @brief Test if string doesn't precede C string.
6016  * @param __lhs String.
6017  * @param __rhs C string.
6018  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6019  */
6020  template<typename _CharT, typename _Traits, typename _Alloc>
6021  inline bool
6022  operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6023  const _CharT* __rhs)
6024  { return __lhs.compare(__rhs) >= 0; }
6025 
6026  /**
6027  * @brief Test if C string doesn't precede string.
6028  * @param __lhs C string.
6029  * @param __rhs String.
6030  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6031  */
6032  template<typename _CharT, typename _Traits, typename _Alloc>
6033  inline bool
6034  operator>=(const _CharT* __lhs,
6036  { return __rhs.compare(__lhs) <= 0; }
6037 
6038  /**
6039  * @brief Swap contents of two strings.
6040  * @param __lhs First string.
6041  * @param __rhs Second string.
6042  *
6043  * Exchanges the contents of @a __lhs and @a __rhs in constant time.
6044  */
6045  template<typename _CharT, typename _Traits, typename _Alloc>
6046  inline void
6049  _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
6050  { __lhs.swap(__rhs); }
6051 
6052 
6053  /**
6054  * @brief Read stream into a string.
6055  * @param __is Input stream.
6056  * @param __str Buffer to store into.
6057  * @return Reference to the input stream.
6058  *
6059  * Stores characters from @a __is into @a __str until whitespace is
6060  * found, the end of the stream is encountered, or str.max_size()
6061  * is reached. If is.width() is non-zero, that is the limit on the
6062  * number of characters stored into @a __str. Any previous
6063  * contents of @a __str are erased.
6064  */
6065  template<typename _CharT, typename _Traits, typename _Alloc>
6069 
6070  template<>
6073 
6074  /**
6075  * @brief Write string to a stream.
6076  * @param __os Output stream.
6077  * @param __str String to write out.
6078  * @return Reference to the output stream.
6079  *
6080  * Output characters of @a __str into os following the same rules as for
6081  * writing a C string.
6082  */
6083  template<typename _CharT, typename _Traits, typename _Alloc>
6085  operator<<(basic_ostream<_CharT, _Traits>& __os,
6087  {
6088  // _GLIBCXX_RESOLVE_LIB_DEFECTS
6089  // 586. string inserter not a formatted function
6090  return __ostream_insert(__os, __str.data(), __str.size());
6091  }
6092 
6093  /**
6094  * @brief Read a line from stream into a string.
6095  * @param __is Input stream.
6096  * @param __str Buffer to store into.
6097  * @param __delim Character marking end of line.
6098  * @return Reference to the input stream.
6099  *
6100  * Stores characters from @a __is into @a __str until @a __delim is
6101  * found, the end of the stream is encountered, or str.max_size()
6102  * is reached. Any previous contents of @a __str are erased. If
6103  * @a __delim is encountered, it is extracted but not stored into
6104  * @a __str.
6105  */
6106  template<typename _CharT, typename _Traits, typename _Alloc>
6109  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
6110 
6111  /**
6112  * @brief Read a line from stream into a string.
6113  * @param __is Input stream.
6114  * @param __str Buffer to store into.
6115  * @return Reference to the input stream.
6116  *
6117  * Stores characters from is into @a __str until &apos;\n&apos; is
6118  * found, the end of the stream is encountered, or str.max_size()
6119  * is reached. Any previous contents of @a __str are erased. If
6120  * end of line is encountered, it is extracted but not stored into
6121  * @a __str.
6122  */
6123  template<typename _CharT, typename _Traits, typename _Alloc>
6127  { return std::getline(__is, __str, __is.widen('\n')); }
6128 
6129 #if __cplusplus >= 201103L
6130  /// Read a line from an rvalue stream into a string.
6131  template<typename _CharT, typename _Traits, typename _Alloc>
6134  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
6135  { return std::getline(__is, __str, __delim); }
6136 
6137  /// Read a line from an rvalue stream into a string.
6138  template<typename _CharT, typename _Traits, typename _Alloc>
6142  { return std::getline(__is, __str); }
6143 #endif
6144 
6145  template<>
6148  char __delim);
6149 
6150 #ifdef _GLIBCXX_USE_WCHAR_T
6151  template<>
6154  wchar_t __delim);
6155 #endif
6156 
6157 _GLIBCXX_END_NAMESPACE_VERSION
6158 } // namespace
6159 
6160 #if __cplusplus >= 201103L
6161 
6162 #include <ext/string_conversions.h>
6163 
6164 namespace std _GLIBCXX_VISIBILITY(default)
6165 {
6166 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6167 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6168 
6169 #if _GLIBCXX_USE_C99_STDLIB
6170  // 21.4 Numeric Conversions [string.conversions].
6171  inline int
6172  stoi(const string& __str, size_t* __idx = 0, int __base = 10)
6173  { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
6174  __idx, __base); }
6175 
6176  inline long
6177  stol(const string& __str, size_t* __idx = 0, int __base = 10)
6178  { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
6179  __idx, __base); }
6180 
6181  inline unsigned long
6182  stoul(const string& __str, size_t* __idx = 0, int __base = 10)
6183  { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
6184  __idx, __base); }
6185 
6186  inline long long
6187  stoll(const string& __str, size_t* __idx = 0, int __base = 10)
6188  { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
6189  __idx, __base); }
6190 
6191  inline unsigned long long
6192  stoull(const string& __str, size_t* __idx = 0, int __base = 10)
6193  { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
6194  __idx, __base); }
6195 
6196  // NB: strtof vs strtod.
6197  inline float
6198  stof(const string& __str, size_t* __idx = 0)
6199  { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
6200 
6201  inline double
6202  stod(const string& __str, size_t* __idx = 0)
6203  { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
6204 
6205  inline long double
6206  stold(const string& __str, size_t* __idx = 0)
6207  { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
6208 #endif // _GLIBCXX_USE_C99_STDLIB
6209 
6210 #if _GLIBCXX_USE_C99_STDIO
6211  // NB: (v)snprintf vs sprintf.
6212 
6213  // DR 1261.
6214  inline string
6215  to_string(int __val)
6216  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
6217  "%d", __val); }
6218 
6219  inline string
6220  to_string(unsigned __val)
6221  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6222  4 * sizeof(unsigned),
6223  "%u", __val); }
6224 
6225  inline string
6226  to_string(long __val)
6227  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
6228  "%ld", __val); }
6229 
6230  inline string
6231  to_string(unsigned long __val)
6232  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6233  4 * sizeof(unsigned long),
6234  "%lu", __val); }
6235 
6236  inline string
6237  to_string(long long __val)
6238  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6239  4 * sizeof(long long),
6240  "%lld", __val); }
6241 
6242  inline string
6243  to_string(unsigned long long __val)
6244  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
6245  4 * sizeof(unsigned long long),
6246  "%llu", __val); }
6247 
6248  inline string
6249  to_string(float __val)
6250  {
6251  const int __n =
6252  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6253  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6254  "%f", __val);
6255  }
6256 
6257  inline string
6258  to_string(double __val)
6259  {
6260  const int __n =
6261  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6262  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6263  "%f", __val);
6264  }
6265 
6266  inline string
6267  to_string(long double __val)
6268  {
6269  const int __n =
6270  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6271  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6272  "%Lf", __val);
6273  }
6274 #endif // _GLIBCXX_USE_C99_STDIO
6275 
6276 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
6277  inline int
6278  stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
6279  { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
6280  __idx, __base); }
6281 
6282  inline long
6283  stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
6284  { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
6285  __idx, __base); }
6286 
6287  inline unsigned long
6288  stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
6289  { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
6290  __idx, __base); }
6291 
6292  inline long long
6293  stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
6294  { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
6295  __idx, __base); }
6296 
6297  inline unsigned long long
6298  stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
6299  { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
6300  __idx, __base); }
6301 
6302  // NB: wcstof vs wcstod.
6303  inline float
6304  stof(const wstring& __str, size_t* __idx = 0)
6305  { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
6306 
6307  inline double
6308  stod(const wstring& __str, size_t* __idx = 0)
6309  { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
6310 
6311  inline long double
6312  stold(const wstring& __str, size_t* __idx = 0)
6313  { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
6314 
6315 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6316  // DR 1261.
6317  inline wstring
6318  to_wstring(int __val)
6319  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
6320  L"%d", __val); }
6321 
6322  inline wstring
6323  to_wstring(unsigned __val)
6324  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6325  4 * sizeof(unsigned),
6326  L"%u", __val); }
6327 
6328  inline wstring
6329  to_wstring(long __val)
6330  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
6331  L"%ld", __val); }
6332 
6333  inline wstring
6334  to_wstring(unsigned long __val)
6335  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6336  4 * sizeof(unsigned long),
6337  L"%lu", __val); }
6338 
6339  inline wstring
6340  to_wstring(long long __val)
6341  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6342  4 * sizeof(long long),
6343  L"%lld", __val); }
6344 
6345  inline wstring
6346  to_wstring(unsigned long long __val)
6347  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6348  4 * sizeof(unsigned long long),
6349  L"%llu", __val); }
6350 
6351  inline wstring
6352  to_wstring(float __val)
6353  {
6354  const int __n =
6355  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6356  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6357  L"%f", __val);
6358  }
6359 
6360  inline wstring
6361  to_wstring(double __val)
6362  {
6363  const int __n =
6364  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6365  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6366  L"%f", __val);
6367  }
6368 
6369  inline wstring
6370  to_wstring(long double __val)
6371  {
6372  const int __n =
6373  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6374  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6375  L"%Lf", __val);
6376  }
6377 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6378 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6379 
6380 _GLIBCXX_END_NAMESPACE_CXX11
6381 _GLIBCXX_END_NAMESPACE_VERSION
6382 } // namespace
6383 
6384 #endif /* C++11 */
6385 
6386 #if __cplusplus >= 201103L
6387 
6388 #include <bits/functional_hash.h>
6389 
6390 namespace std _GLIBCXX_VISIBILITY(default)
6391 {
6392 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6393 
6394  // DR 1182.
6395 
6396 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6397  /// std::hash specialization for string.
6398  template<>
6399  struct hash<string>
6400  : public __hash_base<size_t, string>
6401  {
6402  size_t
6403  operator()(const string& __s) const noexcept
6404  { return std::_Hash_impl::hash(__s.data(), __s.length()); }
6405  };
6406 
6407  template<>
6408  struct __is_fast_hash<hash<string>> : std::false_type
6409  { };
6410 
6411 #ifdef _GLIBCXX_USE_WCHAR_T
6412  /// std::hash specialization for wstring.
6413  template<>
6414  struct hash<wstring>
6415  : public __hash_base<size_t, wstring>
6416  {
6417  size_t
6418  operator()(const wstring& __s) const noexcept
6419  { return std::_Hash_impl::hash(__s.data(),
6420  __s.length() * sizeof(wchar_t)); }
6421  };
6422 
6423  template<>
6424  struct __is_fast_hash<hash<wstring>> : std::false_type
6425  { };
6426 #endif
6427 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6428 
6429 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6430  /// std::hash specialization for u16string.
6431  template<>
6432  struct hash<u16string>
6433  : public __hash_base<size_t, u16string>
6434  {
6435  size_t
6436  operator()(const u16string& __s) const noexcept
6437  { return std::_Hash_impl::hash(__s.data(),
6438  __s.length() * sizeof(char16_t)); }
6439  };
6440 
6441  template<>
6442  struct __is_fast_hash<hash<u16string>> : std::false_type
6443  { };
6444 
6445  /// std::hash specialization for u32string.
6446  template<>
6447  struct hash<u32string>
6448  : public __hash_base<size_t, u32string>
6449  {
6450  size_t
6451  operator()(const u32string& __s) const noexcept
6452  { return std::_Hash_impl::hash(__s.data(),
6453  __s.length() * sizeof(char32_t)); }
6454  };
6455 
6456  template<>
6457  struct __is_fast_hash<hash<u32string>> : std::false_type
6458  { };
6459 #endif
6460 
6461 _GLIBCXX_END_NAMESPACE_VERSION
6462 
6463 #if __cplusplus > 201103L
6464 
6465 #define __cpp_lib_string_udls 201304
6466 
6467  inline namespace literals
6468  {
6469  inline namespace string_literals
6470  {
6471 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6472 
6473  _GLIBCXX_DEFAULT_ABI_TAG
6474  inline basic_string<char>
6475  operator""s(const char* __str, size_t __len)
6476  { return basic_string<char>{__str, __len}; }
6477 
6478 #ifdef _GLIBCXX_USE_WCHAR_T
6479  _GLIBCXX_DEFAULT_ABI_TAG
6480  inline basic_string<wchar_t>
6481  operator""s(const wchar_t* __str, size_t __len)
6482  { return basic_string<wchar_t>{__str, __len}; }
6483 #endif
6484 
6485 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6486  _GLIBCXX_DEFAULT_ABI_TAG
6487  inline basic_string<char16_t>
6488  operator""s(const char16_t* __str, size_t __len)
6489  { return basic_string<char16_t>{__str, __len}; }
6490 
6491  _GLIBCXX_DEFAULT_ABI_TAG
6492  inline basic_string<char32_t>
6493  operator""s(const char32_t* __str, size_t __len)
6494  { return basic_string<char32_t>{__str, __len}; }
6495 #endif
6496 
6497 _GLIBCXX_END_NAMESPACE_VERSION
6498  } // inline namespace string_literals
6499  } // inline namespace literals
6500 
6501 #endif // __cplusplus > 201103L
6502 
6503 } // namespace std
6504 
6505 #endif // C++11
6506 
6507 #endif /* _BASIC_STRING_H */
basic_string & operator+=(initializer_list< _CharT > __l)
Append an initializer_list of characters.
const_iterator cbegin() const noexcept
Uniform interface to C++98 and C++11 allocators.
basic_string & operator=(_CharT __c)
Set value to string of length 1.
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
size_type find_last_of(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a character of C string.
reverse_iterator rend()
const_iterator begin() const noexcept
basic_string & operator=(const basic_string &__str)
Assign the value of str to this string.
char_type widen(char __c) const
Widens characters.
Definition: basic_ios.h:449
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
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.
size_type find_last_of(_CharT __c, size_type __pos=npos) const noexcept
Find last position of a character.
void pop_back()
Remove the last character.
size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
basic_string & append(const _CharT *__s)
Append a C string.
void push_back(_CharT __c)
Append a single character.
reference front()
basic_string & assign(size_type __n, _CharT __c)
Set value to multiple characters.
basic_string & replace(iterator __i1, iterator __i2, const basic_string &__str)
Replace range of characters with string.
iterator insert(iterator __p, _CharT __c)
Insert one character.
integral_constant
Definition: type_traits:69
basic_string & assign(const basic_string &__str, size_type __pos, size_type __n)
Set value to a substring of a string.
basic_string & replace(size_type __pos1, size_type __n1, const basic_string &__str, size_type __pos2, size_type __n2)
Replace characters with value from another string.
const_reverse_iterator crbegin() const noexcept
basic_string & assign(initializer_list< _CharT > __l)
Set value to an initializer_list of characters.
basic_string & operator+=(const basic_string &__str)
Append a string to this string.
const_reference operator[](size_type __pos) const noexcept
Subscript access to the data contained in the string.
initializer_list
void clear() noexcept
reference at(size_type __n)
Provides access to the data contained in the string.
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
ISO C++ entities toplevel namespace is std.
const_reverse_iterator rend() const noexcept
void insert(iterator __p, initializer_list< _CharT > __l)
Insert an initializer_list of characters.
size_type find_first_not_of(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a character not in C string.
basic_string & assign(const basic_string &__str)
Set value to contents of another string.
void shrink_to_fit() noexcept
A non-binding request to reduce capacity() to size().
reverse_iterator rbegin()
basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
basic_string & assign(basic_string &&__str)
Set value to contents of another string.
basic_string(basic_string &&__str) noexcept
Move construct string.
Template class basic_ostream.
Definition: iosfwd:86
basic_string & operator=(initializer_list< _CharT > __l)
Set value to string constructed from initializer list.
const_reverse_iterator crend() const noexcept
~basic_string() noexcept
Destroy the string instance.
void reserve(size_type __res_arg=0)
Attempt to preallocate enough memory for specified number of characters.
void resize(size_type __n)
Resizes the string to the specified number of characters.
const_iterator cend() const noexcept
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
basic_string & assign(const _CharT *__s)
Set value to contents of a C string.
basic_string & insert(size_type __pos1, const basic_string &__str)
Insert value of a string.
reference back()
size_type rfind(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a C string.
size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
size_type find(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a C string.
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
const_iterator end() const noexcept
basic_string & insert(size_type __pos1, const basic_string &__str, size_type __pos2, size_type __n)
Insert a substring.
basic_string & replace(iterator __i1, iterator __i2, const _CharT *__s)
Replace range of characters with C string.
const _CharT * data() const noexcept
Return const pointer to contents.
basic_string & replace(iterator __i1, iterator __i2, initializer_list< _CharT > __l)
Replace range of characters with initializer_list.
const_reference at(size_type __n) const
Provides access to the data contained in the string.
reference operator[](size_type __pos)
Subscript access to the data contained in the string.
basic_string & operator=(const _CharT *__s)
Copy contents of s into this string.
Marking input iterators.
basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
bool empty() const noexcept
basic_string & operator+=(_CharT __c)
Append a character.
const_reference back() const noexcept
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1462
size_type find(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a C substring.
Definition: