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