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