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