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