libstdc++
basic_string.h
Go to the documentation of this file.
1 // Components for manipulating sequences of characters -*- C++ -*-
2 
3 // Copyright (C) 1997-2021 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 
52 namespace std _GLIBCXX_VISIBILITY(default)
53 {
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 
56 #if _GLIBCXX_USE_CXX11_ABI
57 _GLIBCXX_BEGIN_NAMESPACE_CXX11
58  /**
59  * @class basic_string basic_string.h <string>
60  * @brief Managing sequences of characters and character-like objects.
61  *
62  * @ingroup strings
63  * @ingroup sequences
64  *
65  * @tparam _CharT Type of character
66  * @tparam _Traits Traits for character type, defaults to
67  * char_traits<_CharT>.
68  * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
69  *
70  * Meets the requirements of a <a href="tables.html#65">container</a>, a
71  * <a href="tables.html#66">reversible container</a>, and a
72  * <a href="tables.html#67">sequence</a>. Of the
73  * <a href="tables.html#68">optional sequence requirements</a>, only
74  * @c push_back, @c at, and @c %array access are supported.
75  */
76  template<typename _CharT, typename _Traits, typename _Alloc>
77  class basic_string
78  {
80  rebind<_CharT>::other _Char_alloc_type;
81  typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
82 
83  // Types:
84  public:
85  typedef _Traits traits_type;
86  typedef typename _Traits::char_type value_type;
87  typedef _Char_alloc_type allocator_type;
88  typedef typename _Alloc_traits::size_type size_type;
89  typedef typename _Alloc_traits::difference_type difference_type;
90  typedef typename _Alloc_traits::reference reference;
91  typedef typename _Alloc_traits::const_reference const_reference;
92  typedef typename _Alloc_traits::pointer pointer;
93  typedef typename _Alloc_traits::const_pointer const_pointer;
94  typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
95  typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
96  const_iterator;
97  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
98  typedef std::reverse_iterator<iterator> reverse_iterator;
99 
100  /// Value returned by various member functions when they fail.
101  static const size_type npos = static_cast<size_type>(-1);
102 
103  protected:
104  // type used for positions in insert, erase etc.
105 #if __cplusplus < 201103L
106  typedef iterator __const_iterator;
107 #else
108  typedef const_iterator __const_iterator;
109 #endif
110 
111  private:
112 #if __cplusplus >= 201703L
113  // A helper type for avoiding boiler-plate.
114  typedef basic_string_view<_CharT, _Traits> __sv_type;
115 
116  template<typename _Tp, typename _Res>
117  using _If_sv = enable_if_t<
118  __and_<is_convertible<const _Tp&, __sv_type>,
119  __not_<is_convertible<const _Tp*, const basic_string*>>,
120  __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
121  _Res>;
122 
123  // Allows an implicit conversion to __sv_type.
124  static __sv_type
125  _S_to_string_view(__sv_type __svt) noexcept
126  { return __svt; }
127 
128  // Wraps a string_view by explicit conversion and thus
129  // allows to add an internal constructor that does not
130  // participate in overload resolution when a string_view
131  // is provided.
132  struct __sv_wrapper
133  {
134  explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
135  __sv_type _M_sv;
136  };
137 
138  /**
139  * @brief Only internally used: Construct string from a string view
140  * wrapper.
141  * @param __svw string view wrapper.
142  * @param __a Allocator to use.
143  */
144  explicit
145  basic_string(__sv_wrapper __svw, const _Alloc& __a)
146  : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
147 #endif
148 
149  // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
150  struct _Alloc_hider : allocator_type // TODO check __is_final
151  {
152 #if __cplusplus < 201103L
153  _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
154  : allocator_type(__a), _M_p(__dat) { }
155 #else
156  _Alloc_hider(pointer __dat, const _Alloc& __a)
157  : allocator_type(__a), _M_p(__dat) { }
158 
159  _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
160  : allocator_type(std::move(__a)), _M_p(__dat) { }
161 #endif
162 
163  pointer _M_p; // The actual data.
164  };
165 
166  _Alloc_hider _M_dataplus;
167  size_type _M_string_length;
168 
169  enum { _S_local_capacity = 15 / sizeof(_CharT) };
170 
171  union
172  {
173  _CharT _M_local_buf[_S_local_capacity + 1];
174  size_type _M_allocated_capacity;
175  };
176 
177  void
178  _M_data(pointer __p)
179  { _M_dataplus._M_p = __p; }
180 
181  void
182  _M_length(size_type __length)
183  { _M_string_length = __length; }
184 
185  pointer
186  _M_data() const
187  { return _M_dataplus._M_p; }
188 
189  pointer
190  _M_local_data()
191  {
192 #if __cplusplus >= 201103L
193  return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
194 #else
195  return pointer(_M_local_buf);
196 #endif
197  }
198 
199  const_pointer
200  _M_local_data() const
201  {
202 #if __cplusplus >= 201103L
204 #else
205  return const_pointer(_M_local_buf);
206 #endif
207  }
208 
209  void
210  _M_capacity(size_type __capacity)
211  { _M_allocated_capacity = __capacity; }
212 
213  void
214  _M_set_length(size_type __n)
215  {
216  _M_length(__n);
217  traits_type::assign(_M_data()[__n], _CharT());
218  }
219 
220  bool
221  _M_is_local() const
222  { return _M_data() == _M_local_data(); }
223 
224  // Create & Destroy
225  pointer
226  _M_create(size_type&, size_type);
227 
228  void
229  _M_dispose()
230  {
231  if (!_M_is_local())
232  _M_destroy(_M_allocated_capacity);
233  }
234 
235  void
236  _M_destroy(size_type __size) throw()
237  { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
238 
239  // _M_construct_aux is used to implement the 21.3.1 para 15 which
240  // requires special behaviour if _InIterator is an integral type
241  template<typename _InIterator>
242  void
243  _M_construct_aux(_InIterator __beg, _InIterator __end,
244  std::__false_type)
245  {
246  typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
247  _M_construct(__beg, __end, _Tag());
248  }
249 
250  // _GLIBCXX_RESOLVE_LIB_DEFECTS
251  // 438. Ambiguity in the "do the right thing" clause
252  template<typename _Integer>
253  void
254  _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
255  { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
256 
257  void
258  _M_construct_aux_2(size_type __req, _CharT __c)
259  { _M_construct(__req, __c); }
260 
261  template<typename _InIterator>
262  void
263  _M_construct(_InIterator __beg, _InIterator __end)
264  {
265  typedef typename std::__is_integer<_InIterator>::__type _Integral;
266  _M_construct_aux(__beg, __end, _Integral());
267  }
268 
269  // For Input Iterators, used in istreambuf_iterators, etc.
270  template<typename _InIterator>
271  void
272  _M_construct(_InIterator __beg, _InIterator __end,
274 
275  // For forward_iterators up to random_access_iterators, used for
276  // string::iterator, _CharT*, etc.
277  template<typename _FwdIterator>
278  void
279  _M_construct(_FwdIterator __beg, _FwdIterator __end,
281 
282  void
283  _M_construct(size_type __req, _CharT __c);
284 
285  allocator_type&
286  _M_get_allocator()
287  { return _M_dataplus; }
288 
289  const allocator_type&
290  _M_get_allocator() const
291  { return _M_dataplus; }
292 
293  private:
294 
295 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
296  // The explicit instantiations in misc-inst.cc require this due to
297  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
298  template<typename _Tp, bool _Requires =
299  !__are_same<_Tp, _CharT*>::__value
300  && !__are_same<_Tp, const _CharT*>::__value
301  && !__are_same<_Tp, iterator>::__value
302  && !__are_same<_Tp, const_iterator>::__value>
303  struct __enable_if_not_native_iterator
304  { typedef basic_string& __type; };
305  template<typename _Tp>
306  struct __enable_if_not_native_iterator<_Tp, false> { };
307 #endif
308 
309  size_type
310  _M_check(size_type __pos, const char* __s) const
311  {
312  if (__pos > this->size())
313  __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
314  "this->size() (which is %zu)"),
315  __s, __pos, this->size());
316  return __pos;
317  }
318 
319  void
320  _M_check_length(size_type __n1, size_type __n2, const char* __s) const
321  {
322  if (this->max_size() - (this->size() - __n1) < __n2)
323  __throw_length_error(__N(__s));
324  }
325 
326 
327  // NB: _M_limit doesn't check for a bad __pos value.
328  size_type
329  _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
330  {
331  const bool __testoff = __off < this->size() - __pos;
332  return __testoff ? __off : this->size() - __pos;
333  }
334 
335  // True if _Rep and source do not overlap.
336  bool
337  _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
338  {
339  return (less<const _CharT*>()(__s, _M_data())
340  || less<const _CharT*>()(_M_data() + this->size(), __s));
341  }
342 
343  // When __n = 1 way faster than the general multichar
344  // traits_type::copy/move/assign.
345  static void
346  _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
347  {
348  if (__n == 1)
349  traits_type::assign(*__d, *__s);
350  else
351  traits_type::copy(__d, __s, __n);
352  }
353 
354  static void
355  _S_move(_CharT* __d, const _CharT* __s, size_type __n)
356  {
357  if (__n == 1)
358  traits_type::assign(*__d, *__s);
359  else
360  traits_type::move(__d, __s, __n);
361  }
362 
363  static void
364  _S_assign(_CharT* __d, size_type __n, _CharT __c)
365  {
366  if (__n == 1)
367  traits_type::assign(*__d, __c);
368  else
369  traits_type::assign(__d, __n, __c);
370  }
371 
372  // _S_copy_chars is a separate template to permit specialization
373  // to optimize for the common case of pointers as iterators.
374  template<class _Iterator>
375  static void
376  _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
377  {
378  for (; __k1 != __k2; ++__k1, (void)++__p)
379  traits_type::assign(*__p, *__k1); // These types are off.
380  }
381 
382  static void
383  _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
384  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
385 
386  static void
387  _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
388  _GLIBCXX_NOEXCEPT
389  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
390 
391  static void
392  _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
393  { _S_copy(__p, __k1, __k2 - __k1); }
394 
395  static void
396  _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
397  _GLIBCXX_NOEXCEPT
398  { _S_copy(__p, __k1, __k2 - __k1); }
399 
400  static int
401  _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
402  {
403  const difference_type __d = difference_type(__n1 - __n2);
404 
405  if (__d > __gnu_cxx::__numeric_traits<int>::__max)
406  return __gnu_cxx::__numeric_traits<int>::__max;
407  else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
408  return __gnu_cxx::__numeric_traits<int>::__min;
409  else
410  return int(__d);
411  }
412 
413  void
414  _M_assign(const basic_string&);
415 
416  void
417  _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
418  size_type __len2);
419 
420  void
421  _M_erase(size_type __pos, size_type __n);
422 
423  public:
424  // Construct/copy/destroy:
425  // NB: We overload ctors in some cases instead of using default
426  // arguments, per 17.4.4.4 para. 2 item 2.
427 
428  /**
429  * @brief Default constructor creates an empty string.
430  */
431  basic_string()
432  _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
433  : _M_dataplus(_M_local_data())
434  { _M_set_length(0); }
435 
436  /**
437  * @brief Construct an empty string using allocator @a a.
438  */
439  explicit
440  basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
441  : _M_dataplus(_M_local_data(), __a)
442  { _M_set_length(0); }
443 
444  /**
445  * @brief Construct string with copy of value of @a __str.
446  * @param __str Source string.
447  */
448  basic_string(const basic_string& __str)
449  : _M_dataplus(_M_local_data(),
450  _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
451  { _M_construct(__str._M_data(), __str._M_data() + __str.length()); }
452 
453  // _GLIBCXX_RESOLVE_LIB_DEFECTS
454  // 2583. no way to supply an allocator for basic_string(str, pos)
455  /**
456  * @brief Construct string as copy of a substring.
457  * @param __str Source string.
458  * @param __pos Index of first character to copy from.
459  * @param __a Allocator to use.
460  */
461  basic_string(const basic_string& __str, size_type __pos,
462  const _Alloc& __a = _Alloc())
463  : _M_dataplus(_M_local_data(), __a)
464  {
465  const _CharT* __start = __str._M_data()
466  + __str._M_check(__pos, "basic_string::basic_string");
467  _M_construct(__start, __start + __str._M_limit(__pos, npos));
468  }
469 
470  /**
471  * @brief Construct string as copy of a substring.
472  * @param __str Source string.
473  * @param __pos Index of first character to copy from.
474  * @param __n Number of characters to copy.
475  */
476  basic_string(const basic_string& __str, size_type __pos,
477  size_type __n)
478  : _M_dataplus(_M_local_data())
479  {
480  const _CharT* __start = __str._M_data()
481  + __str._M_check(__pos, "basic_string::basic_string");
482  _M_construct(__start, __start + __str._M_limit(__pos, __n));
483  }
484 
485  /**
486  * @brief Construct string as copy of a substring.
487  * @param __str Source string.
488  * @param __pos Index of first character to copy from.
489  * @param __n Number of characters to copy.
490  * @param __a Allocator to use.
491  */
492  basic_string(const basic_string& __str, size_type __pos,
493  size_type __n, const _Alloc& __a)
494  : _M_dataplus(_M_local_data(), __a)
495  {
496  const _CharT* __start
497  = __str._M_data() + __str._M_check(__pos, "string::string");
498  _M_construct(__start, __start + __str._M_limit(__pos, __n));
499  }
500 
501  /**
502  * @brief Construct string initialized by a character %array.
503  * @param __s Source character %array.
504  * @param __n Number of characters to copy.
505  * @param __a Allocator to use (default is default allocator).
506  *
507  * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
508  * has no special meaning.
509  */
510  basic_string(const _CharT* __s, size_type __n,
511  const _Alloc& __a = _Alloc())
512  : _M_dataplus(_M_local_data(), __a)
513  { _M_construct(__s, __s + __n); }
514 
515  /**
516  * @brief Construct string as copy of a C string.
517  * @param __s Source C string.
518  * @param __a Allocator to use (default is default allocator).
519  */
520 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
521  // _GLIBCXX_RESOLVE_LIB_DEFECTS
522  // 3076. basic_string CTAD ambiguity
523  template<typename = _RequireAllocator<_Alloc>>
524 #endif
525  basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
526  : _M_dataplus(_M_local_data(), __a)
527  { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); }
528 
529  /**
530  * @brief Construct string as multiple characters.
531  * @param __n Number of characters.
532  * @param __c Character to use.
533  * @param __a Allocator to use (default is default allocator).
534  */
535 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
536  // _GLIBCXX_RESOLVE_LIB_DEFECTS
537  // 3076. basic_string CTAD ambiguity
538  template<typename = _RequireAllocator<_Alloc>>
539 #endif
540  basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
541  : _M_dataplus(_M_local_data(), __a)
542  { _M_construct(__n, __c); }
543 
544 #if __cplusplus >= 201103L
545  /**
546  * @brief Move construct string.
547  * @param __str Source string.
548  *
549  * The newly-created string contains the exact contents of @a __str.
550  * @a __str is a valid, but unspecified string.
551  */
552  basic_string(basic_string&& __str) noexcept
553  : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
554  {
555  if (__str._M_is_local())
556  {
557  traits_type::copy(_M_local_buf, __str._M_local_buf,
558  _S_local_capacity + 1);
559  }
560  else
561  {
562  _M_data(__str._M_data());
563  _M_capacity(__str._M_allocated_capacity);
564  }
565 
566  // Must use _M_length() here not _M_set_length() because
567  // basic_stringbuf relies on writing into unallocated capacity so
568  // we mess up the contents if we put a '\0' in the string.
569  _M_length(__str.length());
570  __str._M_data(__str._M_local_data());
571  __str._M_set_length(0);
572  }
573 
574  /**
575  * @brief Construct string from an initializer %list.
576  * @param __l std::initializer_list of characters.
577  * @param __a Allocator to use (default is default allocator).
578  */
579  basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
580  : _M_dataplus(_M_local_data(), __a)
581  { _M_construct(__l.begin(), __l.end()); }
582 
583  basic_string(const basic_string& __str, const _Alloc& __a)
584  : _M_dataplus(_M_local_data(), __a)
585  { _M_construct(__str.begin(), __str.end()); }
586 
587  basic_string(basic_string&& __str, const _Alloc& __a)
588  noexcept(_Alloc_traits::_S_always_equal())
589  : _M_dataplus(_M_local_data(), __a)
590  {
591  if (__str._M_is_local())
592  {
593  traits_type::copy(_M_local_buf, __str._M_local_buf,
594  _S_local_capacity + 1);
595  _M_length(__str.length());
596  __str._M_set_length(0);
597  }
598  else if (_Alloc_traits::_S_always_equal()
599  || __str.get_allocator() == __a)
600  {
601  _M_data(__str._M_data());
602  _M_length(__str.length());
603  _M_capacity(__str._M_allocated_capacity);
604  __str._M_data(__str._M_local_buf);
605  __str._M_set_length(0);
606  }
607  else
608  _M_construct(__str.begin(), __str.end());
609  }
610 
611 #endif // C++11
612 
613  /**
614  * @brief Construct string as copy of a range.
615  * @param __beg Start of range.
616  * @param __end End of range.
617  * @param __a Allocator to use (default is default allocator).
618  */
619 #if __cplusplus >= 201103L
620  template<typename _InputIterator,
621  typename = std::_RequireInputIter<_InputIterator>>
622 #else
623  template<typename _InputIterator>
624 #endif
625  basic_string(_InputIterator __beg, _InputIterator __end,
626  const _Alloc& __a = _Alloc())
627  : _M_dataplus(_M_local_data(), __a)
628  { _M_construct(__beg, __end); }
629 
630 #if __cplusplus >= 201703L
631  /**
632  * @brief Construct string from a substring of a string_view.
633  * @param __t Source object convertible to string view.
634  * @param __pos The index of the first character to copy from __t.
635  * @param __n The number of characters to copy from __t.
636  * @param __a Allocator to use.
637  */
638  template<typename _Tp, typename = _If_sv<_Tp, void>>
639  basic_string(const _Tp& __t, size_type __pos, size_type __n,
640  const _Alloc& __a = _Alloc())
641  : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
642 
643  /**
644  * @brief Construct string from a string_view.
645  * @param __t Source object convertible to string view.
646  * @param __a Allocator to use (default is default allocator).
647  */
648  template<typename _Tp, typename = _If_sv<_Tp, void>>
649  explicit
650  basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
651  : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
652 #endif // C++17
653 
654  /**
655  * @brief Destroy the string instance.
656  */
657  ~basic_string()
658  { _M_dispose(); }
659 
660  /**
661  * @brief Assign the value of @a str to this string.
662  * @param __str Source string.
663  */
664  basic_string&
665  operator=(const basic_string& __str)
666  {
667  return this->assign(__str);
668  }
669 
670  /**
671  * @brief Copy contents of @a s into this string.
672  * @param __s Source null-terminated string.
673  */
674  basic_string&
675  operator=(const _CharT* __s)
676  { return this->assign(__s); }
677 
678  /**
679  * @brief Set value to string of length 1.
680  * @param __c Source character.
681  *
682  * Assigning to a character makes this string length 1 and
683  * (*this)[0] == @a c.
684  */
685  basic_string&
686  operator=(_CharT __c)
687  {
688  this->assign(1, __c);
689  return *this;
690  }
691 
692 #if __cplusplus >= 201103L
693  /**
694  * @brief Move assign the value of @a str to this string.
695  * @param __str Source string.
696  *
697  * The contents of @a str are moved into this string (without copying).
698  * @a str is a valid, but unspecified string.
699  */
700  // _GLIBCXX_RESOLVE_LIB_DEFECTS
701  // 2063. Contradictory requirements for string move assignment
702  basic_string&
703  operator=(basic_string&& __str)
704  noexcept(_Alloc_traits::_S_nothrow_move())
705  {
706  if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
707  && !_Alloc_traits::_S_always_equal()
708  && _M_get_allocator() != __str._M_get_allocator())
709  {
710  // Destroy existing storage before replacing allocator.
711  _M_destroy(_M_allocated_capacity);
712  _M_data(_M_local_data());
713  _M_set_length(0);
714  }
715  // Replace allocator if POCMA is true.
716  std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
717 
718  if (__str._M_is_local())
719  {
720  // We've always got room for a short string, just copy it
721  // (unless this is a self-move, because that would violate the
722  // char_traits::copy precondition that the ranges don't overlap).
723  if (__builtin_expect(std::__addressof(__str) != this, true))
724  {
725  if (__str.size())
726  this->_S_copy(_M_data(), __str._M_data(), __str.size());
727  _M_set_length(__str.size());
728  }
729  }
730  else if (_Alloc_traits::_S_propagate_on_move_assign()
731  || _Alloc_traits::_S_always_equal()
732  || _M_get_allocator() == __str._M_get_allocator())
733  {
734  // Just move the allocated pointer, our allocator can free it.
735  pointer __data = nullptr;
736  size_type __capacity;
737  if (!_M_is_local())
738  {
739  if (_Alloc_traits::_S_always_equal())
740  {
741  // __str can reuse our existing storage.
742  __data = _M_data();
743  __capacity = _M_allocated_capacity;
744  }
745  else // __str can't use it, so free it.
746  _M_destroy(_M_allocated_capacity);
747  }
748 
749  _M_data(__str._M_data());
750  _M_length(__str.length());
751  _M_capacity(__str._M_allocated_capacity);
752  if (__data)
753  {
754  __str._M_data(__data);
755  __str._M_capacity(__capacity);
756  }
757  else
758  __str._M_data(__str._M_local_buf);
759  }
760  else // Need to do a deep copy
761  assign(__str);
762  __str.clear();
763  return *this;
764  }
765 
766  /**
767  * @brief Set value to string constructed from initializer %list.
768  * @param __l std::initializer_list.
769  */
770  basic_string&
771  operator=(initializer_list<_CharT> __l)
772  {
773  this->assign(__l.begin(), __l.size());
774  return *this;
775  }
776 #endif // C++11
777 
778 #if __cplusplus >= 201703L
779  /**
780  * @brief Set value to string constructed from a string_view.
781  * @param __svt An object convertible to string_view.
782  */
783  template<typename _Tp>
784  _If_sv<_Tp, basic_string&>
785  operator=(const _Tp& __svt)
786  { return this->assign(__svt); }
787 
788  /**
789  * @brief Convert to a string_view.
790  * @return A string_view.
791  */
792  operator __sv_type() const noexcept
793  { return __sv_type(data(), size()); }
794 #endif // C++17
795 
796  // Iterators:
797  /**
798  * Returns a read/write iterator that points to the first character in
799  * the %string.
800  */
801  iterator
802  begin() _GLIBCXX_NOEXCEPT
803  { return iterator(_M_data()); }
804 
805  /**
806  * Returns a read-only (constant) iterator that points to the first
807  * character in the %string.
808  */
809  const_iterator
810  begin() const _GLIBCXX_NOEXCEPT
811  { return const_iterator(_M_data()); }
812 
813  /**
814  * Returns a read/write iterator that points one past the last
815  * character in the %string.
816  */
817  iterator
818  end() _GLIBCXX_NOEXCEPT
819  { return iterator(_M_data() + this->size()); }
820 
821  /**
822  * Returns a read-only (constant) iterator that points one past the
823  * last character in the %string.
824  */
825  const_iterator
826  end() const _GLIBCXX_NOEXCEPT
827  { return const_iterator(_M_data() + this->size()); }
828 
829  /**
830  * Returns a read/write reverse iterator that points to the last
831  * character in the %string. Iteration is done in reverse element
832  * order.
833  */
834  reverse_iterator
835  rbegin() _GLIBCXX_NOEXCEPT
836  { return reverse_iterator(this->end()); }
837 
838  /**
839  * Returns a read-only (constant) reverse iterator that points
840  * to the last character in the %string. Iteration is done in
841  * reverse element order.
842  */
843  const_reverse_iterator
844  rbegin() const _GLIBCXX_NOEXCEPT
845  { return const_reverse_iterator(this->end()); }
846 
847  /**
848  * Returns a read/write reverse iterator that points to one before the
849  * first character in the %string. Iteration is done in reverse
850  * element order.
851  */
852  reverse_iterator
853  rend() _GLIBCXX_NOEXCEPT
854  { return reverse_iterator(this->begin()); }
855 
856  /**
857  * Returns a read-only (constant) reverse iterator that points
858  * to one before the first character in the %string. Iteration
859  * is done in reverse element order.
860  */
861  const_reverse_iterator
862  rend() const _GLIBCXX_NOEXCEPT
863  { return const_reverse_iterator(this->begin()); }
864 
865 #if __cplusplus >= 201103L
866  /**
867  * Returns a read-only (constant) iterator that points to the first
868  * character in the %string.
869  */
870  const_iterator
871  cbegin() const noexcept
872  { return const_iterator(this->_M_data()); }
873 
874  /**
875  * Returns a read-only (constant) iterator that points one past the
876  * last character in the %string.
877  */
878  const_iterator
879  cend() const noexcept
880  { return const_iterator(this->_M_data() + this->size()); }
881 
882  /**
883  * Returns a read-only (constant) reverse iterator that points
884  * to the last character in the %string. Iteration is done in
885  * reverse element order.
886  */
887  const_reverse_iterator
888  crbegin() const noexcept
889  { return const_reverse_iterator(this->end()); }
890 
891  /**
892  * Returns a read-only (constant) reverse iterator that points
893  * to one before the first character in the %string. Iteration
894  * is done in reverse element order.
895  */
896  const_reverse_iterator
897  crend() const noexcept
898  { return const_reverse_iterator(this->begin()); }
899 #endif
900 
901  public:
902  // Capacity:
903  /// Returns the number of characters in the string, not including any
904  /// null-termination.
905  size_type
906  size() const _GLIBCXX_NOEXCEPT
907  { return _M_string_length; }
908 
909  /// Returns the number of characters in the string, not including any
910  /// null-termination.
911  size_type
912  length() const _GLIBCXX_NOEXCEPT
913  { return _M_string_length; }
914 
915  /// Returns the size() of the largest possible %string.
916  size_type
917  max_size() const _GLIBCXX_NOEXCEPT
918  { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
919 
920  /**
921  * @brief Resizes the %string to the specified number of characters.
922  * @param __n Number of characters the %string should contain.
923  * @param __c Character to fill any new elements.
924  *
925  * This function will %resize the %string to the specified
926  * number of characters. If the number is smaller than the
927  * %string's current size the %string is truncated, otherwise
928  * the %string is extended and new elements are %set to @a __c.
929  */
930  void
931  resize(size_type __n, _CharT __c);
932 
933  /**
934  * @brief Resizes the %string to the specified number of characters.
935  * @param __n Number of characters the %string should contain.
936  *
937  * This function will resize the %string to the specified length. If
938  * the new size is smaller than the %string's current size the %string
939  * is truncated, otherwise the %string is extended and new characters
940  * are default-constructed. For basic types such as char, this means
941  * setting them to 0.
942  */
943  void
944  resize(size_type __n)
945  { this->resize(__n, _CharT()); }
946 
947 #if __cplusplus >= 201103L
948 #pragma GCC diagnostic push
949 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
950  /// A non-binding request to reduce capacity() to size().
951  void
952  shrink_to_fit() noexcept
953  { reserve(); }
954 #pragma GCC diagnostic pop
955 #endif
956 
957  /**
958  * Returns the total number of characters that the %string can hold
959  * before needing to allocate more memory.
960  */
961  size_type
962  capacity() const _GLIBCXX_NOEXCEPT
963  {
964  return _M_is_local() ? size_type(_S_local_capacity)
965  : _M_allocated_capacity;
966  }
967 
968  /**
969  * @brief Attempt to preallocate enough memory for specified number of
970  * characters.
971  * @param __res_arg Number of characters required.
972  * @throw std::length_error If @a __res_arg exceeds @c max_size().
973  *
974  * This function attempts to reserve enough memory for the
975  * %string to hold the specified number of characters. If the
976  * number requested is more than max_size(), length_error is
977  * thrown.
978  *
979  * The advantage of this function is that if optimal code is a
980  * necessity and the user can determine the string length that will be
981  * required, the user can reserve the memory in %advance, and thus
982  * prevent a possible reallocation of memory and copying of %string
983  * data.
984  */
985  void
986  reserve(size_type __res_arg);
987 
988  /**
989  * Equivalent to shrink_to_fit().
990  */
991 #if __cplusplus > 201703L
992  [[deprecated("use shrink_to_fit() instead")]]
993 #endif
994  void
995  reserve();
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 #if __cplusplus > 202002L
3077  bool
3078  contains(basic_string_view<_CharT, _Traits> __x) const noexcept
3079  { return __sv_type(this->data(), this->size()).contains(__x); }
3080 
3081  bool
3082  contains(_CharT __x) const noexcept
3083  { return __sv_type(this->data(), this->size()).contains(__x); }
3084 
3085  bool
3086  contains(const _CharT* __x) const noexcept
3087  { return __sv_type(this->data(), this->size()).contains(__x); }
3088 #endif // C++23
3089 
3090  // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3091  template<typename, typename, typename> friend class basic_stringbuf;
3092  };
3093 _GLIBCXX_END_NAMESPACE_CXX11
3094 #else // !_GLIBCXX_USE_CXX11_ABI
3095  // Reference-counted COW string implentation
3096 
3097  /**
3098  * @class basic_string basic_string.h <string>
3099  * @brief Managing sequences of characters and character-like objects.
3100  *
3101  * @ingroup strings
3102  * @ingroup sequences
3103  *
3104  * @tparam _CharT Type of character
3105  * @tparam _Traits Traits for character type, defaults to
3106  * char_traits<_CharT>.
3107  * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
3108  *
3109  * Meets the requirements of a <a href="tables.html#65">container</a>, a
3110  * <a href="tables.html#66">reversible container</a>, and a
3111  * <a href="tables.html#67">sequence</a>. Of the
3112  * <a href="tables.html#68">optional sequence requirements</a>, only
3113  * @c push_back, @c at, and @c %array access are supported.
3114  *
3115  * @doctodo
3116  *
3117  *
3118  * Documentation? What's that?
3119  * Nathan Myers <ncm@cantrip.org>.
3120  *
3121  * A string looks like this:
3122  *
3123  * @code
3124  * [_Rep]
3125  * _M_length
3126  * [basic_string<char_type>] _M_capacity
3127  * _M_dataplus _M_refcount
3128  * _M_p ----------------> unnamed array of char_type
3129  * @endcode
3130  *
3131  * Where the _M_p points to the first character in the string, and
3132  * you cast it to a pointer-to-_Rep and subtract 1 to get a
3133  * pointer to the header.
3134  *
3135  * This approach has the enormous advantage that a string object
3136  * requires only one allocation. All the ugliness is confined
3137  * within a single %pair of inline functions, which each compile to
3138  * a single @a add instruction: _Rep::_M_data(), and
3139  * string::_M_rep(); and the allocation function which gets a
3140  * block of raw bytes and with room enough and constructs a _Rep
3141  * object at the front.
3142  *
3143  * The reason you want _M_data pointing to the character %array and
3144  * not the _Rep is so that the debugger can see the string
3145  * contents. (Probably we should add a non-inline member to get
3146  * the _Rep for the debugger to use, so users can check the actual
3147  * string length.)
3148  *
3149  * Note that the _Rep object is a POD so that you can have a
3150  * static <em>empty string</em> _Rep object already @a constructed before
3151  * static constructors have run. The reference-count encoding is
3152  * chosen so that a 0 indicates one reference, so you never try to
3153  * destroy the empty-string _Rep object.
3154  *
3155  * All but the last paragraph is considered pretty conventional
3156  * for a C++ string implementation.
3157  */
3158  // 21.3 Template class basic_string
3159  template<typename _CharT, typename _Traits, typename _Alloc>
3161  {
3163  rebind<_CharT>::other _CharT_alloc_type;
3165 
3166  // Types:
3167  public:
3168  typedef _Traits traits_type;
3169  typedef typename _Traits::char_type value_type;
3170  typedef _Alloc allocator_type;
3171  typedef typename _CharT_alloc_traits::size_type size_type;
3172  typedef typename _CharT_alloc_traits::difference_type difference_type;
3173 #if __cplusplus < 201103L
3174  typedef typename _CharT_alloc_type::reference reference;
3175  typedef typename _CharT_alloc_type::const_reference const_reference;
3176 #else
3177  typedef value_type& reference;
3178  typedef const value_type& const_reference;
3179 #endif
3180  typedef typename _CharT_alloc_traits::pointer pointer;
3181  typedef typename _CharT_alloc_traits::const_pointer const_pointer;
3182  typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
3183  typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
3184  const_iterator;
3187 
3188  protected:
3189  // type used for positions in insert, erase etc.
3190  typedef iterator __const_iterator;
3191 
3192  private:
3193  // _Rep: string representation
3194  // Invariants:
3195  // 1. String really contains _M_length + 1 characters: due to 21.3.4
3196  // must be kept null-terminated.
3197  // 2. _M_capacity >= _M_length
3198  // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
3199  // 3. _M_refcount has three states:
3200  // -1: leaked, one reference, no ref-copies allowed, non-const.
3201  // 0: one reference, non-const.
3202  // n>0: n + 1 references, operations require a lock, const.
3203  // 4. All fields==0 is an empty string, given the extra storage
3204  // beyond-the-end for a null terminator; thus, the shared
3205  // empty string representation needs no constructor.
3206 
3207  struct _Rep_base
3208  {
3209  size_type _M_length;
3210  size_type _M_capacity;
3211  _Atomic_word _M_refcount;
3212  };
3213 
3214  struct _Rep : _Rep_base
3215  {
3216  // Types:
3218  rebind<char>::other _Raw_bytes_alloc;
3219 
3220  // (Public) Data members:
3221 
3222  // The maximum number of individual char_type elements of an
3223  // individual string is determined by _S_max_size. This is the
3224  // value that will be returned by max_size(). (Whereas npos
3225  // is the maximum number of bytes the allocator can allocate.)
3226  // If one was to divvy up the theoretical largest size string,
3227  // with a terminating character and m _CharT elements, it'd
3228  // look like this:
3229  // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
3230  // Solving for m:
3231  // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
3232  // In addition, this implementation quarters this amount.
3233  static const size_type _S_max_size;
3234  static const _CharT _S_terminal;
3235 
3236  // The following storage is init'd to 0 by the linker, resulting
3237  // (carefully) in an empty string with one reference.
3238  static size_type _S_empty_rep_storage[];
3239 
3240  static _Rep&
3241  _S_empty_rep() _GLIBCXX_NOEXCEPT
3242  {
3243  // NB: Mild hack to avoid strict-aliasing warnings. Note that
3244  // _S_empty_rep_storage is never modified and the punning should
3245  // be reasonably safe in this case.
3246  void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
3247  return *reinterpret_cast<_Rep*>(__p);
3248  }
3249 
3250  bool
3251  _M_is_leaked() 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. However, _M_is_leaked
3256  // predicate does not change concurrently (i.e. the string is either
3257  // leaked or not), so a relaxed load is enough.
3258  return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
3259 #else
3260  return this->_M_refcount < 0;
3261 #endif
3262  }
3263 
3264  bool
3265  _M_is_shared() const _GLIBCXX_NOEXCEPT
3266  {
3267 #if defined(__GTHREADS)
3268  // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3269  // so we need to use an atomic load. Another thread can drop last
3270  // but one reference concurrently with this check, so we need this
3271  // load to be acquire to synchronize with release fetch_and_add in
3272  // _M_dispose.
3273  return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
3274 #else
3275  return this->_M_refcount > 0;
3276 #endif
3277  }
3278 
3279  void
3280  _M_set_leaked() _GLIBCXX_NOEXCEPT
3281  { this->_M_refcount = -1; }
3282 
3283  void
3284  _M_set_sharable() _GLIBCXX_NOEXCEPT
3285  { this->_M_refcount = 0; }
3286 
3287  void
3288  _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
3289  {
3290 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3291  if (__builtin_expect(this != &_S_empty_rep(), false))
3292 #endif
3293  {
3294  this->_M_set_sharable(); // One reference.
3295  this->_M_length = __n;
3296  traits_type::assign(this->_M_refdata()[__n], _S_terminal);
3297  // grrr. (per 21.3.4)
3298  // You cannot leave those LWG people alone for a second.
3299  }
3300  }
3301 
3302  _CharT*
3303  _M_refdata() throw()
3304  { return reinterpret_cast<_CharT*>(this + 1); }
3305 
3306  _CharT*
3307  _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
3308  {
3309  return (!_M_is_leaked() && __alloc1 == __alloc2)
3310  ? _M_refcopy() : _M_clone(__alloc1);
3311  }
3312 
3313  // Create & Destroy
3314  static _Rep*
3315  _S_create(size_type, size_type, const _Alloc&);
3316 
3317  void
3318  _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
3319  {
3320 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3321  if (__builtin_expect(this != &_S_empty_rep(), false))
3322 #endif
3323  {
3324  // Be race-detector-friendly. For more info see bits/c++config.
3325  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
3326  // Decrement of _M_refcount is acq_rel, because:
3327  // - all but last decrements need to release to synchronize with
3328  // the last decrement that will delete the object.
3329  // - the last decrement needs to acquire to synchronize with
3330  // all the previous decrements.
3331  // - last but one decrement needs to release to synchronize with
3332  // the acquire load in _M_is_shared that will conclude that
3333  // the object is not shared anymore.
3334  if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
3335  -1) <= 0)
3336  {
3337  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
3338  _M_destroy(__a);
3339  }
3340  }
3341  } // XXX MT
3342 
3343  void
3344  _M_destroy(const _Alloc&) throw();
3345 
3346  _CharT*
3347  _M_refcopy() throw()
3348  {
3349 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3350  if (__builtin_expect(this != &_S_empty_rep(), false))
3351 #endif
3352  __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
3353  return _M_refdata();
3354  } // XXX MT
3355 
3356  _CharT*
3357  _M_clone(const _Alloc&, size_type __res = 0);
3358  };
3359 
3360  // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
3361  struct _Alloc_hider : _Alloc
3362  {
3363  _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
3364  : _Alloc(__a), _M_p(__dat) { }
3365 
3366  _CharT* _M_p; // The actual data.
3367  };
3368 
3369  public:
3370  // Data Members (public):
3371  // NB: This is an unsigned type, and thus represents the maximum
3372  // size that the allocator can hold.
3373  /// Value returned by various member functions when they fail.
3374  static const size_type npos = static_cast<size_type>(-1);
3375 
3376  private:
3377  // Data Members (private):
3378  mutable _Alloc_hider _M_dataplus;
3379 
3380  _CharT*
3381  _M_data() const _GLIBCXX_NOEXCEPT
3382  { return _M_dataplus._M_p; }
3383 
3384  _CharT*
3385  _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
3386  { return (_M_dataplus._M_p = __p); }
3387 
3388  _Rep*
3389  _M_rep() const _GLIBCXX_NOEXCEPT
3390  { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
3391 
3392  // For the internal use we have functions similar to `begin'/`end'
3393  // but they do not call _M_leak.
3394  iterator
3395  _M_ibegin() const _GLIBCXX_NOEXCEPT
3396  { return iterator(_M_data()); }
3397 
3398  iterator
3399  _M_iend() const _GLIBCXX_NOEXCEPT
3400  { return iterator(_M_data() + this->size()); }
3401 
3402  void
3403  _M_leak() // for use in begin() & non-const op[]
3404  {
3405  if (!_M_rep()->_M_is_leaked())
3406  _M_leak_hard();
3407  }
3408 
3409  size_type
3410  _M_check(size_type __pos, const char* __s) const
3411  {
3412  if (__pos > this->size())
3413  __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
3414  "this->size() (which is %zu)"),
3415  __s, __pos, this->size());
3416  return __pos;
3417  }
3418 
3419  void
3420  _M_check_length(size_type __n1, size_type __n2, const char* __s) const
3421  {
3422  if (this->max_size() - (this->size() - __n1) < __n2)
3423  __throw_length_error(__N(__s));
3424  }
3425 
3426  // NB: _M_limit doesn't check for a bad __pos value.
3427  size_type
3428  _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
3429  {
3430  const bool __testoff = __off < this->size() - __pos;
3431  return __testoff ? __off : this->size() - __pos;
3432  }
3433 
3434  // True if _Rep and source do not overlap.
3435  bool
3436  _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3437  {
3438  return (less<const _CharT*>()(__s, _M_data())
3439  || less<const _CharT*>()(_M_data() + this->size(), __s));
3440  }
3441 
3442  // When __n = 1 way faster than the general multichar
3443  // traits_type::copy/move/assign.
3444  static void
3445  _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
3446  {
3447  if (__n == 1)
3448  traits_type::assign(*__d, *__s);
3449  else
3450  traits_type::copy(__d, __s, __n);
3451  }
3452 
3453  static void
3454  _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
3455  {
3456  if (__n == 1)
3457  traits_type::assign(*__d, *__s);
3458  else
3459  traits_type::move(__d, __s, __n);
3460  }
3461 
3462  static void
3463  _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
3464  {
3465  if (__n == 1)
3466  traits_type::assign(*__d, __c);
3467  else
3468  traits_type::assign(__d, __n, __c);
3469  }
3470 
3471  // _S_copy_chars is a separate template to permit specialization
3472  // to optimize for the common case of pointers as iterators.
3473  template<class _Iterator>
3474  static void
3475  _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
3476  {
3477  for (; __k1 != __k2; ++__k1, (void)++__p)
3478  traits_type::assign(*__p, *__k1); // These types are off.
3479  }
3480 
3481  static void
3482  _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
3483  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3484 
3485  static void
3486  _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
3487  _GLIBCXX_NOEXCEPT
3488  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3489 
3490  static void
3491  _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
3492  { _M_copy(__p, __k1, __k2 - __k1); }
3493 
3494  static void
3495  _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
3496  _GLIBCXX_NOEXCEPT
3497  { _M_copy(__p, __k1, __k2 - __k1); }
3498 
3499  static int
3500  _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
3501  {
3502  const difference_type __d = difference_type(__n1 - __n2);
3503 
3504  if (__d > __gnu_cxx::__numeric_traits<int>::__max)
3505  return __gnu_cxx::__numeric_traits<int>::__max;
3506  else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
3507  return __gnu_cxx::__numeric_traits<int>::__min;
3508  else
3509  return int(__d);
3510  }
3511 
3512  void
3513  _M_mutate(size_type __pos, size_type __len1, size_type __len2);
3514 
3515  void
3516  _M_leak_hard();
3517 
3518  static _Rep&
3519  _S_empty_rep() _GLIBCXX_NOEXCEPT
3520  { return _Rep::_S_empty_rep(); }
3521 
3522 #if __cplusplus >= 201703L
3523  // A helper type for avoiding boiler-plate.
3524  typedef basic_string_view<_CharT, _Traits> __sv_type;
3525 
3526  template<typename _Tp, typename _Res>
3527  using _If_sv = enable_if_t<
3528  __and_<is_convertible<const _Tp&, __sv_type>,
3529  __not_<is_convertible<const _Tp*, const basic_string*>>,
3530  __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
3531  _Res>;
3532 
3533  // Allows an implicit conversion to __sv_type.
3534  static __sv_type
3535  _S_to_string_view(__sv_type __svt) noexcept
3536  { return __svt; }
3537 
3538  // Wraps a string_view by explicit conversion and thus
3539  // allows to add an internal constructor that does not
3540  // participate in overload resolution when a string_view
3541  // is provided.
3542  struct __sv_wrapper
3543  {
3544  explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
3545  __sv_type _M_sv;
3546  };
3547 
3548  /**
3549  * @brief Only internally used: Construct string from a string view
3550  * wrapper.
3551  * @param __svw string view wrapper.
3552  * @param __a Allocator to use.
3553  */
3554  explicit
3555  basic_string(__sv_wrapper __svw, const _Alloc& __a)
3556  : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
3557 #endif
3558 
3559  public:
3560  // Construct/copy/destroy:
3561  // NB: We overload ctors in some cases instead of using default
3562  // arguments, per 17.4.4.4 para. 2 item 2.
3563 
3564  /**
3565  * @brief Default constructor creates an empty string.
3566  */
3568 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3569  _GLIBCXX_NOEXCEPT
3570  : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
3571 #else
3572  : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
3573 #endif
3574  { }
3575 
3576  /**
3577  * @brief Construct an empty string using allocator @a a.
3578  */
3579  explicit
3580  basic_string(const _Alloc& __a)
3581  : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
3582  { }
3583 
3584  // NB: per LWG issue 42, semantics different from IS:
3585  /**
3586  * @brief Construct string with copy of value of @a str.
3587  * @param __str Source string.
3588  */
3590  : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
3591  __str.get_allocator()),
3592  __str.get_allocator())
3593  { }
3594 
3595  // _GLIBCXX_RESOLVE_LIB_DEFECTS
3596  // 2583. no way to supply an allocator for basic_string(str, pos)
3597  /**
3598  * @brief Construct string as copy of a substring.
3599  * @param __str Source string.
3600  * @param __pos Index of first character to copy from.
3601  * @param __a Allocator to use.
3602  */
3603  basic_string(const basic_string& __str, size_type __pos,
3604  const _Alloc& __a = _Alloc());
3605 
3606  /**
3607  * @brief Construct string as copy of a substring.
3608  * @param __str Source string.
3609  * @param __pos Index of first character to copy from.
3610  * @param __n Number of characters to copy.
3611  */
3612  basic_string(const basic_string& __str, size_type __pos,
3613  size_type __n);
3614  /**
3615  * @brief Construct string as copy of a substring.
3616  * @param __str Source string.
3617  * @param __pos Index of first character to copy from.
3618  * @param __n Number of characters to copy.
3619  * @param __a Allocator to use.
3620  */
3621  basic_string(const basic_string& __str, size_type __pos,
3622  size_type __n, const _Alloc& __a);
3623 
3624  /**
3625  * @brief Construct string initialized by a character %array.
3626  * @param __s Source character %array.
3627  * @param __n Number of characters to copy.
3628  * @param __a Allocator to use (default is default allocator).
3629  *
3630  * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
3631  * has no special meaning.
3632  */
3633  basic_string(const _CharT* __s, size_type __n,
3634  const _Alloc& __a = _Alloc())
3635  : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
3636  { }
3637 
3638  /**
3639  * @brief Construct string as copy of a C string.
3640  * @param __s Source C string.
3641  * @param __a Allocator to use (default is default allocator).
3642  */
3643 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
3644  // _GLIBCXX_RESOLVE_LIB_DEFECTS
3645  // 3076. basic_string CTAD ambiguity
3646  template<typename = _RequireAllocator<_Alloc>>
3647 #endif
3648  basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
3649  : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
3650  __s + npos, __a), __a)
3651  { }
3652 
3653  /**
3654  * @brief Construct string as multiple characters.
3655  * @param __n Number of characters.
3656  * @param __c Character to use.
3657  * @param __a Allocator to use (default is default allocator).
3658  */
3659  basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
3660  : _M_dataplus(_S_construct(__n, __c, __a), __a)
3661  { }
3662 
3663 #if __cplusplus >= 201103L
3664  /**
3665  * @brief Move construct string.
3666  * @param __str Source string.
3667  *
3668  * The newly-created string contains the exact contents of @a __str.
3669  * @a __str is a valid, but unspecified string.
3670  */
3672 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3673  noexcept // FIXME C++11: should always be noexcept.
3674 #endif
3675  : _M_dataplus(std::move(__str._M_dataplus))
3676  {
3677 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3678  __str._M_data(_S_empty_rep()._M_refdata());
3679 #else
3680  __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3681 #endif
3682  }
3683 
3684  /**
3685  * @brief Construct string from an initializer %list.
3686  * @param __l std::initializer_list of characters.
3687  * @param __a Allocator to use (default is default allocator).
3688  */
3689  basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
3690  : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a)
3691  { }
3692 
3693  basic_string(const basic_string& __str, const _Alloc& __a)
3694  : _M_dataplus(__str._M_rep()->_M_grab(__a, __str.get_allocator()), __a)
3695  { }
3696 
3697  basic_string(basic_string&& __str, const _Alloc& __a)
3698  : _M_dataplus(__str._M_data(), __a)
3699  {
3700  if (__a == __str.get_allocator())
3701  {
3702 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3703  __str._M_data(_S_empty_rep()._M_refdata());
3704 #else
3705  __str._M_data(_S_construct(size_type(), _CharT(), __a));
3706 #endif
3707  }
3708  else
3709  _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
3710  }
3711 #endif // C++11
3712 
3713  /**
3714  * @brief Construct string as copy of a range.
3715  * @param __beg Start of range.
3716  * @param __end End of range.
3717  * @param __a Allocator to use (default is default allocator).
3718  */
3719  template<class _InputIterator>
3720  basic_string(_InputIterator __beg, _InputIterator __end,
3721  const _Alloc& __a = _Alloc())
3722  : _M_dataplus(_S_construct(__beg, __end, __a), __a)
3723  { }
3724 
3725 #if __cplusplus >= 201703L
3726  /**
3727  * @brief Construct string from a substring of a string_view.
3728  * @param __t Source object convertible to string view.
3729  * @param __pos The index of the first character to copy from __t.
3730  * @param __n The number of characters to copy from __t.
3731  * @param __a Allocator to use.
3732  */
3733  template<typename _Tp, typename = _If_sv<_Tp, void>>
3734  basic_string(const _Tp& __t, size_type __pos, size_type __n,
3735  const _Alloc& __a = _Alloc())
3736  : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
3737 
3738  /**
3739  * @brief Construct string from a string_view.
3740  * @param __t Source object convertible to string view.
3741  * @param __a Allocator to use (default is default allocator).
3742  */
3743  template<typename _Tp, typename = _If_sv<_Tp, void>>
3744  explicit
3745  basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
3746  : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
3747 #endif // C++17
3748 
3749  /**
3750  * @brief Destroy the string instance.
3751  */
3752  ~basic_string() _GLIBCXX_NOEXCEPT
3753  { _M_rep()->_M_dispose(this->get_allocator()); }
3754 
3755  /**
3756  * @brief Assign the value of @a str to this string.
3757  * @param __str Source string.
3758  */
3759  basic_string&
3760  operator=(const basic_string& __str)
3761  { return this->assign(__str); }
3762 
3763  /**
3764  * @brief Copy contents of @a s into this string.
3765  * @param __s Source null-terminated string.
3766  */
3767  basic_string&
3768  operator=(const _CharT* __s)
3769  { return this->assign(__s); }
3770 
3771  /**
3772  * @brief Set value to string of length 1.
3773  * @param __c Source character.
3774  *
3775  * Assigning to a character makes this string length 1 and
3776  * (*this)[0] == @a c.
3777  */
3778  basic_string&
3779  operator=(_CharT __c)
3780  {
3781  this->assign(1, __c);
3782  return *this;
3783  }
3784 
3785 #if __cplusplus >= 201103L
3786  /**
3787  * @brief Move assign the value of @a str to this string.
3788  * @param __str Source string.
3789  *
3790  * The contents of @a str are moved into this string (without copying).
3791  * @a str is a valid, but unspecified string.
3792  */
3793  basic_string&
3796  {
3797  // NB: DR 1204.
3798  this->swap(__str);
3799  return *this;
3800  }
3801 
3802  /**
3803  * @brief Set value to string constructed from initializer %list.
3804  * @param __l std::initializer_list.
3805  */
3806  basic_string&
3808  {
3809  this->assign(__l.begin(), __l.size());
3810  return *this;
3811  }
3812 #endif // C++11
3813 
3814 #if __cplusplus >= 201703L
3815  /**
3816  * @brief Set value to string constructed from a string_view.
3817  * @param __svt An object convertible to string_view.
3818  */
3819  template<typename _Tp>
3820  _If_sv<_Tp, basic_string&>
3821  operator=(const _Tp& __svt)
3822  { return this->assign(__svt); }
3823 
3824  /**
3825  * @brief Convert to a string_view.
3826  * @return A string_view.
3827  */
3828  operator __sv_type() const noexcept
3829  { return __sv_type(data(), size()); }
3830 #endif // C++17
3831 
3832  // Iterators:
3833  /**
3834  * Returns a read/write iterator that points to the first character in
3835  * the %string. Unshares the string.
3836  */
3837  iterator
3838  begin() // FIXME C++11: should be noexcept.
3839  {
3840  _M_leak();
3841  return iterator(_M_data());
3842  }
3843 
3844  /**
3845  * Returns a read-only (constant) iterator that points to the first
3846  * character in the %string.
3847  */
3848  const_iterator
3849  begin() const _GLIBCXX_NOEXCEPT
3850  { return const_iterator(_M_data()); }
3851 
3852  /**
3853  * Returns a read/write iterator that points one past the last
3854  * character in the %string. Unshares the string.
3855  */
3856  iterator
3857  end() // FIXME C++11: should be noexcept.
3858  {
3859  _M_leak();
3860  return iterator(_M_data() + this->size());
3861  }
3862 
3863  /**
3864  * Returns a read-only (constant) iterator that points one past the
3865  * last character in the %string.
3866  */
3867  const_iterator
3868  end() const _GLIBCXX_NOEXCEPT
3869  { return const_iterator(_M_data() + this->size()); }
3870 
3871  /**
3872  * Returns a read/write reverse iterator that points to the last
3873  * character in the %string. Iteration is done in reverse element
3874  * order. Unshares the string.
3875  */
3876  reverse_iterator
3877  rbegin() // FIXME C++11: should be noexcept.
3878  { return reverse_iterator(this->end()); }
3879 
3880  /**
3881  * Returns a read-only (constant) reverse iterator that points
3882  * to the last character in the %string. Iteration is done in
3883  * reverse element order.
3884  */
3885  const_reverse_iterator
3886  rbegin() const _GLIBCXX_NOEXCEPT
3887  { return const_reverse_iterator(this->end()); }
3888 
3889  /**
3890  * Returns a read/write reverse iterator that points to one before the
3891  * first character in the %string. Iteration is done in reverse
3892  * element order. Unshares the string.
3893  */
3894  reverse_iterator
3895  rend() // FIXME C++11: should be noexcept.
3896  { return reverse_iterator(this->begin()); }
3897 
3898  /**
3899  * Returns a read-only (constant) reverse iterator that points
3900  * to one before the first character in the %string. Iteration
3901  * is done in reverse element order.
3902  */
3903  const_reverse_iterator
3904  rend() const _GLIBCXX_NOEXCEPT
3905  { return const_reverse_iterator(this->begin()); }
3906 
3907 #if __cplusplus >= 201103L
3908  /**
3909  * Returns a read-only (constant) iterator that points to the first
3910  * character in the %string.
3911  */
3912  const_iterator
3913  cbegin() const noexcept
3914  { return const_iterator(this->_M_data()); }
3915 
3916  /**
3917  * Returns a read-only (constant) iterator that points one past the
3918  * last character in the %string.
3919  */
3920  const_iterator
3921  cend() const noexcept
3922  { return const_iterator(this->_M_data() + this->size()); }
3923 
3924  /**
3925  * Returns a read-only (constant) reverse iterator that points
3926  * to the last character in the %string. Iteration is done in
3927  * reverse element order.
3928  */
3929  const_reverse_iterator
3930  crbegin() const noexcept
3931  { return const_reverse_iterator(this->end()); }
3932 
3933  /**
3934  * Returns a read-only (constant) reverse iterator that points
3935  * to one before the first character in the %string. Iteration
3936  * is done in reverse element order.
3937  */
3938  const_reverse_iterator
3939  crend() const noexcept
3940  { return const_reverse_iterator(this->begin()); }
3941 #endif
3942 
3943  public:
3944  // Capacity:
3945  /// Returns the number of characters in the string, not including any
3946  /// null-termination.
3947  size_type
3948  size() const _GLIBCXX_NOEXCEPT
3949  { return _M_rep()->_M_length; }
3950 
3951  /// Returns the number of characters in the string, not including any
3952  /// null-termination.
3953  size_type
3954  length() const _GLIBCXX_NOEXCEPT
3955  { return _M_rep()->_M_length; }
3956 
3957  /// Returns the size() of the largest possible %string.
3958  size_type
3959  max_size() const _GLIBCXX_NOEXCEPT
3960  { return _Rep::_S_max_size; }
3961 
3962  /**
3963  * @brief Resizes the %string to the specified number of characters.
3964  * @param __n Number of characters the %string should contain.
3965  * @param __c Character to fill any new elements.
3966  *
3967  * This function will %resize the %string to the specified
3968  * number of characters. If the number is smaller than the
3969  * %string's current size the %string is truncated, otherwise
3970  * the %string is extended and new elements are %set to @a __c.
3971  */
3972  void
3973  resize(size_type __n, _CharT __c);
3974 
3975  /**
3976  * @brief Resizes the %string to the specified number of characters.
3977  * @param __n Number of characters the %string should contain.
3978  *
3979  * This function will resize the %string to the specified length. If
3980  * the new size is smaller than the %string's current size the %string
3981  * is truncated, otherwise the %string is extended and new characters
3982  * are default-constructed. For basic types such as char, this means
3983  * setting them to 0.
3984  */
3985  void
3986  resize(size_type __n)
3987  { this->resize(__n, _CharT()); }
3988 
3989 #if __cplusplus >= 201103L
3990 #pragma GCC diagnostic push
3991 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
3992  /// A non-binding request to reduce capacity() to size().
3993  void
3994  shrink_to_fit() noexcept
3995  { reserve(); }
3996 #pragma GCC diagnostic pop
3997 #endif
3998 
3999  /**
4000  * Returns the total number of characters that the %string can hold
4001  * before needing to allocate more memory.
4002  */
4003  size_type
4004  capacity() const _GLIBCXX_NOEXCEPT
4005  { return _M_rep()->_M_capacity; }
4006 
4007  /**
4008  * @brief Attempt to preallocate enough memory for specified number of
4009  * characters.
4010  * @param __res_arg Number of characters required.
4011  * @throw std::length_error If @a __res_arg exceeds @c max_size().
4012  *
4013  * This function attempts to reserve enough memory for the
4014  * %string to hold the specified number of characters. If the
4015  * number requested is more than max_size(), length_error is
4016  * thrown.
4017  *
4018  * The advantage of this function is that if optimal code is a
4019  * necessity and the user can determine the string length that will be
4020  * required, the user can reserve the memory in %advance, and thus
4021  * prevent a possible reallocation of memory and copying of %string
4022  * data.
4023  */
4024  void
4025  reserve(size_type __res_arg);
4026 
4027  /// Equivalent to shrink_to_fit().
4028 #if __cplusplus > 201703L
4029  [[deprecated("use shrink_to_fit() instead")]]
4030 #endif
4031  void
4032  reserve();
4033 
4034  /**
4035  * Erases the string, making it empty.
4036  */
4037 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
4038  void
4039  clear() _GLIBCXX_NOEXCEPT
4040  {
4041  if (_M_rep()->_M_is_shared())
4042  {
4043  _M_rep()->_M_dispose(this->get_allocator());
4044  _M_data(_S_empty_rep()._M_refdata());
4045  }
4046  else
4047  _M_rep()->_M_set_length_and_sharable(0);
4048  }
4049 #else
4050  // PR 56166: this should not throw.
4051  void
4052  clear()
4053  { _M_mutate(0, this->size(), 0); }
4054 #endif
4055 
4056  /**
4057  * Returns true if the %string is empty. Equivalent to
4058  * <code>*this == ""</code>.
4059  */
4060  _GLIBCXX_NODISCARD bool
4061  empty() const _GLIBCXX_NOEXCEPT
4062  { return this->size() == 0; }
4063 
4064  // Element access:
4065  /**
4066  * @brief Subscript access to the data contained in the %string.
4067  * @param __pos The index of the character to access.
4068  * @return Read-only (constant) reference to the character.
4069  *
4070  * This operator allows for easy, array-style, data access.
4071  * Note that data access with this operator is unchecked and
4072  * out_of_range lookups are not defined. (For checked lookups
4073  * see at().)
4074  */
4075  const_reference
4076  operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
4077  {
4078  __glibcxx_assert(__pos <= size());
4079  return _M_data()[__pos];
4080  }
4081 
4082  /**
4083  * @brief Subscript access to the data contained in the %string.
4084  * @param __pos The index of the character to access.
4085  * @return Read/write reference to the character.
4086  *
4087  * This operator allows for easy, array-style, data access.
4088  * Note that data access with this operator is unchecked and
4089  * out_of_range lookups are not defined. (For checked lookups
4090  * see at().) Unshares the string.
4091  */
4092  reference
4093  operator[](size_type __pos)
4094  {
4095  // Allow pos == size() both in C++98 mode, as v3 extension,
4096  // and in C++11 mode.
4097  __glibcxx_assert(__pos <= size());
4098  // In pedantic mode be strict in C++98 mode.
4099  _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
4100  _M_leak();
4101  return _M_data()[__pos];
4102  }
4103 
4104  /**
4105  * @brief Provides access to the data contained in the %string.
4106  * @param __n The index of the character to access.
4107  * @return Read-only (const) reference to the character.
4108  * @throw std::out_of_range If @a n is an invalid index.
4109  *
4110  * This function provides for safer data access. The parameter is
4111  * first checked that it is in the range of the string. The function
4112  * throws out_of_range if the check fails.
4113  */
4114  const_reference
4115  at(size_type __n) const
4116  {
4117  if (__n >= this->size())
4118  __throw_out_of_range_fmt(__N("basic_string::at: __n "
4119  "(which is %zu) >= this->size() "
4120  "(which is %zu)"),
4121  __n, this->size());
4122  return _M_data()[__n];
4123  }
4124 
4125  /**
4126  * @brief Provides access to the data contained in the %string.
4127  * @param __n The index of the character to access.
4128  * @return Read/write reference to the character.
4129  * @throw std::out_of_range If @a n is an invalid index.
4130  *
4131  * This function provides for safer data access. The parameter is
4132  * first checked that it is in the range of the string. The function
4133  * throws out_of_range if the check fails. Success results in
4134  * unsharing the string.
4135  */
4136  reference
4137  at(size_type __n)
4138  {
4139  if (__n >= size())
4140  __throw_out_of_range_fmt(__N("basic_string::at: __n "
4141  "(which is %zu) >= this->size() "
4142  "(which is %zu)"),
4143  __n, this->size());
4144  _M_leak();
4145  return _M_data()[__n];
4146  }
4147 
4148 #if __cplusplus >= 201103L
4149  /**
4150  * Returns a read/write reference to the data at the first
4151  * element of the %string.
4152  */
4153  reference
4155  {
4156  __glibcxx_assert(!empty());
4157  return operator[](0);
4158  }
4159 
4160  /**
4161  * Returns a read-only (constant) reference to the data at the first
4162  * element of the %string.
4163  */
4164  const_reference
4165  front() const noexcept
4166  {
4167  __glibcxx_assert(!empty());
4168  return operator[](0);
4169  }
4170 
4171  /**
4172  * Returns a read/write reference to the data at the last
4173  * element of the %string.
4174  */
4175  reference
4177  {
4178  __glibcxx_assert(!empty());
4179  return operator[](this->size() - 1);
4180  }
4181 
4182  /**
4183  * Returns a read-only (constant) reference to the data at the
4184  * last element of the %string.
4185  */
4186  const_reference
4187  back() const noexcept
4188  {
4189  __glibcxx_assert(!empty());
4190  return operator[](this->size() - 1);
4191  }
4192 #endif
4193 
4194  // Modifiers:
4195  /**
4196  * @brief Append a string to this string.
4197  * @param __str The string to append.
4198  * @return Reference to this string.
4199  */
4200  basic_string&
4201  operator+=(const basic_string& __str)
4202  { return this->append(__str); }
4203 
4204  /**
4205  * @brief Append a C string.
4206  * @param __s The C string to append.
4207  * @return Reference to this string.
4208  */
4209  basic_string&
4210  operator+=(const _CharT* __s)
4211  { return this->append(__s); }
4212 
4213  /**
4214  * @brief Append a character.
4215  * @param __c The character to append.
4216  * @return Reference to this string.
4217  */
4218  basic_string&
4219  operator+=(_CharT __c)
4220  {
4221  this->push_back(__c);
4222  return *this;
4223  }
4224 
4225 #if __cplusplus >= 201103L
4226  /**
4227  * @brief Append an initializer_list of characters.
4228  * @param __l The initializer_list of characters to be appended.
4229  * @return Reference to this string.
4230  */
4231  basic_string&
4233  { return this->append(__l.begin(), __l.size()); }
4234 #endif // C++11
4235 
4236 #if __cplusplus >= 201703L
4237  /**
4238  * @brief Append a string_view.
4239  * @param __svt The object convertible to string_view to be appended.
4240  * @return Reference to this string.
4241  */
4242  template<typename _Tp>
4243  _If_sv<_Tp, basic_string&>
4244  operator+=(const _Tp& __svt)
4245  { return this->append(__svt); }
4246 #endif // C++17
4247 
4248  /**
4249  * @brief Append a string to this string.
4250  * @param __str The string to append.
4251  * @return Reference to this string.
4252  */
4253  basic_string&
4254  append(const basic_string& __str);
4255 
4256  /**
4257  * @brief Append a substring.
4258  * @param __str The string to append.
4259  * @param __pos Index of the first character of str to append.
4260  * @param __n The number of characters to append.
4261  * @return Reference to this string.
4262  * @throw std::out_of_range if @a __pos is not a valid index.
4263  *
4264  * This function appends @a __n characters from @a __str
4265  * starting at @a __pos to this string. If @a __n is is larger
4266  * than the number of available characters in @a __str, the
4267  * remainder of @a __str is appended.
4268  */
4269  basic_string&
4270  append(const basic_string& __str, size_type __pos, size_type __n = npos);
4271 
4272  /**
4273  * @brief Append a C substring.
4274  * @param __s The C string to append.
4275  * @param __n The number of characters to append.
4276  * @return Reference to this string.
4277  */
4278  basic_string&
4279  append(const _CharT* __s, size_type __n);
4280 
4281  /**
4282  * @brief Append a C string.
4283  * @param __s The C string to append.
4284  * @return Reference to this string.
4285  */
4286  basic_string&
4287  append(const _CharT* __s)
4288  {
4289  __glibcxx_requires_string(__s);
4290  return this->append(__s, traits_type::length(__s));
4291  }
4292 
4293  /**
4294  * @brief Append multiple characters.
4295  * @param __n The number of characters to append.
4296  * @param __c The character to use.
4297  * @return Reference to this string.
4298  *
4299  * Appends __n copies of __c to this string.
4300  */
4301  basic_string&
4302  append(size_type __n, _CharT __c);
4303 
4304 #if __cplusplus >= 201103L
4305  /**
4306  * @brief Append an initializer_list of characters.
4307  * @param __l The initializer_list of characters to append.
4308  * @return Reference to this string.
4309  */
4310  basic_string&
4312  { return this->append(__l.begin(), __l.size()); }
4313 #endif // C++11
4314 
4315  /**
4316  * @brief Append a range of characters.
4317  * @param __first Iterator referencing the first character to append.
4318  * @param __last Iterator marking the end of the range.
4319  * @return Reference to this string.
4320  *
4321  * Appends characters in the range [__first,__last) to this string.
4322  */
4323  template<class _InputIterator>
4324  basic_string&
4325  append(_InputIterator __first, _InputIterator __last)
4326  { return this->replace(_M_iend(), _M_iend(), __first, __last); }
4327 
4328 #if __cplusplus >= 201703L
4329  /**
4330  * @brief Append a string_view.
4331  * @param __svt The object convertible to string_view to be appended.
4332  * @return Reference to this string.
4333  */
4334  template<typename _Tp>
4335  _If_sv<_Tp, basic_string&>
4336  append(const _Tp& __svt)
4337  {
4338  __sv_type __sv = __svt;
4339  return this->append(__sv.data(), __sv.size());
4340  }
4341 
4342  /**
4343  * @brief Append a range of characters from a string_view.
4344  * @param __svt The object convertible to string_view to be appended
4345  * from.
4346  * @param __pos The position in the string_view to append from.
4347  * @param __n The number of characters to append from the string_view.
4348  * @return Reference to this string.
4349  */
4350  template<typename _Tp>
4351  _If_sv<_Tp, basic_string&>
4352  append(const _Tp& __svt, size_type __pos, size_type __n = npos)
4353  {
4354  __sv_type __sv = __svt;
4355  return append(__sv.data()
4356  + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
4357  std::__sv_limit(__sv.size(), __pos, __n));
4358  }
4359 #endif // C++17
4360 
4361  /**
4362  * @brief Append a single character.
4363  * @param __c Character to append.
4364  */
4365  void
4366  push_back(_CharT __c)
4367  {
4368  const size_type __len = 1 + this->size();
4369  if (__len > this->capacity() || _M_rep()->_M_is_shared())
4370  this->reserve(__len);
4371  traits_type::assign(_M_data()[this->size()], __c);
4372  _M_rep()->_M_set_length_and_sharable(__len);
4373  }
4374 
4375  /**
4376  * @brief Set value to contents of another string.
4377  * @param __str Source string to use.
4378  * @return Reference to this string.
4379  */
4380  basic_string&
4381  assign(const basic_string& __str);
4382 
4383 #if __cplusplus >= 201103L
4384  /**
4385  * @brief Set value to contents of another string.
4386  * @param __str Source string to use.
4387  * @return Reference to this string.
4388  *
4389  * This function sets this string to the exact contents of @a __str.
4390  * @a __str is a valid, but unspecified string.
4391  */
4392  basic_string&
4395  {
4396  this->swap(__str);
4397  return *this;
4398  }
4399 #endif // C++11
4400 
4401  /**
4402  * @brief Set value to a substring of a string.
4403  * @param __str The string to use.
4404  * @param __pos Index of the first character of str.
4405  * @param __n Number of characters to use.
4406  * @return Reference to this string.
4407  * @throw std::out_of_range if @a pos is not a valid index.
4408  *
4409  * This function sets this string to the substring of @a __str
4410  * consisting of @a __n characters at @a __pos. If @a __n is
4411  * is larger than the number of available characters in @a
4412  * __str, the remainder of @a __str is used.
4413  */
4414  basic_string&
4415  assign(const basic_string& __str, size_type __pos, size_type __n = npos)
4416  { return this->assign(__str._M_data()
4417  + __str._M_check(__pos, "basic_string::assign"),
4418  __str._M_limit(__pos, __n)); }
4419 
4420  /**
4421  * @brief Set value to a C substring.
4422  * @param __s The C string to use.
4423  * @param __n Number of characters to use.
4424  * @return Reference to this string.
4425  *
4426  * This function sets the value of this string to the first @a __n
4427  * characters of @a __s. If @a __n is is larger than the number of
4428  * available characters in @a __s, the remainder of @a __s is used.
4429  */
4430  basic_string&
4431  assign(const _CharT* __s, size_type __n);
4432 
4433  /**
4434  * @brief Set value to contents of a C string.
4435  * @param __s The C string to use.
4436  * @return Reference to this string.
4437  *
4438  * This function sets the value of this string to the value of @a __s.
4439  * The data is copied, so there is no dependence on @a __s once the
4440  * function returns.
4441  */
4442  basic_string&
4443  assign(const _CharT* __s)
4444  {
4445  __glibcxx_requires_string(__s);
4446  return this->assign(__s, traits_type::length(__s));
4447  }
4448 
4449  /**
4450  * @brief Set value to multiple characters.
4451  * @param __n Length of the resulting string.
4452  * @param __c The character to use.
4453  * @return Reference to this string.
4454  *
4455  * This function sets the value of this string to @a __n copies of
4456  * character @a __c.
4457  */
4458  basic_string&
4459  assign(size_type __n, _CharT __c)
4460  { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
4461 
4462  /**
4463  * @brief Set value to a range of characters.
4464  * @param __first Iterator referencing the first character to append.
4465  * @param __last Iterator marking the end of the range.
4466  * @return Reference to this string.
4467  *
4468  * Sets value of string to characters in the range [__first,__last).
4469  */
4470  template<class _InputIterator>
4471  basic_string&
4472  assign(_InputIterator __first, _InputIterator __last)
4473  { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
4474 
4475 #if __cplusplus >= 201103L
4476  /**
4477  * @brief Set value to an initializer_list of characters.
4478  * @param __l The initializer_list of characters to assign.
4479  * @return Reference to this string.
4480  */
4481  basic_string&
4483  { return this->assign(__l.begin(), __l.size()); }
4484 #endif // C++11
4485 
4486 #if __cplusplus >= 201703L
4487  /**
4488  * @brief Set value from a string_view.
4489  * @param __svt The source object convertible to string_view.
4490  * @return Reference to this string.
4491  */
4492  template<typename _Tp>
4493  _If_sv<_Tp, basic_string&>
4494  assign(const _Tp& __svt)
4495  {
4496  __sv_type __sv = __svt;
4497  return this->assign(__sv.data(), __sv.size());
4498  }
4499 
4500  /**
4501  * @brief Set value from a range of characters in a string_view.
4502  * @param __svt The source object convertible to string_view.
4503  * @param __pos The position in the string_view to assign from.
4504  * @param __n The number of characters to assign.
4505  * @return Reference to this string.
4506  */
4507  template<typename _Tp>
4508  _If_sv<_Tp, basic_string&>
4509  assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
4510  {
4511  __sv_type __sv = __svt;
4512  return assign(__sv.data()
4513  + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
4514  std::__sv_limit(__sv.size(), __pos, __n));
4515  }
4516 #endif // C++17
4517 
4518  /**
4519  * @brief Insert multiple characters.
4520  * @param __p Iterator referencing location in string to insert at.
4521  * @param __n Number of characters to insert
4522  * @param __c The character to insert.
4523  * @throw std::length_error If new length exceeds @c max_size().
4524  *
4525  * Inserts @a __n copies of character @a __c starting at the
4526  * position referenced by iterator @a __p. If adding
4527  * characters causes the length to exceed max_size(),
4528  * length_error is thrown. The value of the string doesn't
4529  * change if an error is thrown.
4530  */
4531  void
4532  insert(iterator __p, size_type __n, _CharT __c)
4533  { this->replace(__p, __p, __n, __c); }
4534 
4535  /**
4536  * @brief Insert a range of characters.
4537  * @param __p Iterator referencing location in string to insert at.
4538  * @param __beg Start of range.
4539  * @param __end End of range.
4540  * @throw std::length_error If new length exceeds @c max_size().
4541  *
4542  * Inserts characters in range [__beg,__end). If adding
4543  * characters causes the length to exceed max_size(),
4544  * length_error is thrown. The value of the string doesn't
4545  * change if an error is thrown.
4546  */
4547  template<class _InputIterator>
4548  void
4549  insert(iterator __p, _InputIterator __beg, _InputIterator __end)
4550  { this->replace(__p, __p, __beg, __end); }
4551 
4552 #if __cplusplus >= 201103L
4553  /**
4554  * @brief Insert an initializer_list of characters.
4555  * @param __p Iterator referencing location in string to insert at.
4556  * @param __l The initializer_list of characters to insert.
4557  * @throw std::length_error If new length exceeds @c max_size().
4558  */
4559  void
4561  {
4562  _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4563  this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
4564  }
4565 #endif // C++11
4566 
4567  /**
4568  * @brief Insert value of a string.
4569  * @param __pos1 Position in string to insert at.
4570  * @param __str The string to insert.
4571  * @return Reference to this string.
4572  * @throw std::length_error If new length exceeds @c max_size().
4573  *
4574  * Inserts value of @a __str starting at @a __pos1. If adding
4575  * characters causes the length to exceed max_size(),
4576  * length_error is thrown. The value of the string doesn't
4577  * change if an error is thrown.
4578  */
4579  basic_string&
4580  insert(size_type __pos1, const basic_string& __str)
4581  { return this->insert(__pos1, __str, size_type(0), __str.size()); }
4582 
4583  /**
4584  * @brief Insert a substring.
4585  * @param __pos1 Position in string to insert at.
4586  * @param __str The string to insert.
4587  * @param __pos2 Start of characters in str to insert.
4588  * @param __n Number of characters to insert.
4589  * @return Reference to this string.
4590  * @throw std::length_error If new length exceeds @c max_size().
4591  * @throw std::out_of_range If @a pos1 > size() or
4592  * @a __pos2 > @a str.size().
4593  *
4594  * Starting at @a pos1, insert @a __n character of @a __str
4595  * beginning with @a __pos2. If adding characters causes the
4596  * length to exceed max_size(), length_error is thrown. If @a
4597  * __pos1 is beyond the end of this string or @a __pos2 is
4598  * beyond the end of @a __str, out_of_range is thrown. The
4599  * value of the string doesn't change if an error is thrown.
4600  */
4601  basic_string&
4602  insert(size_type __pos1, const basic_string& __str,
4603  size_type __pos2, size_type __n = npos)
4604  { return this->insert(__pos1, __str._M_data()
4605  + __str._M_check(__pos2, "basic_string::insert"),
4606  __str._M_limit(__pos2, __n)); }
4607 
4608  /**
4609  * @brief Insert a C substring.
4610  * @param __pos Position in string to insert at.
4611  * @param __s The C string to insert.
4612  * @param __n The number of characters to insert.
4613  * @return Reference to this string.
4614  * @throw std::length_error If new length exceeds @c max_size().
4615  * @throw std::out_of_range If @a __pos is beyond the end of this
4616  * string.
4617  *
4618  * Inserts the first @a __n characters of @a __s starting at @a
4619  * __pos. If adding characters causes the length to exceed
4620  * max_size(), length_error is thrown. If @a __pos is beyond
4621  * end(), out_of_range is thrown. The value of the string
4622  * doesn't change if an error is thrown.
4623  */
4624  basic_string&
4625  insert(size_type __pos, const _CharT* __s, size_type __n);
4626 
4627  /**
4628  * @brief Insert a C string.
4629  * @param __pos Position in string to insert at.
4630  * @param __s The C string to insert.
4631  * @return Reference to this string.
4632  * @throw std::length_error If new length exceeds @c max_size().
4633  * @throw std::out_of_range If @a pos is beyond the end of this
4634  * string.
4635  *
4636  * Inserts the first @a n characters of @a __s starting at @a __pos. If
4637  * adding characters causes the length to exceed max_size(),
4638  * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4639  * thrown. The value of the string doesn't change if an error is
4640  * thrown.
4641  */
4642  basic_string&
4643  insert(size_type __pos, const _CharT* __s)
4644  {
4645  __glibcxx_requires_string(__s);
4646  return this->insert(__pos, __s, traits_type::length(__s));
4647  }
4648 
4649  /**
4650  * @brief Insert multiple characters.
4651  * @param __pos Index in string to insert at.
4652  * @param __n Number of characters to insert
4653  * @param __c The character to insert.
4654  * @return Reference to this string.
4655  * @throw std::length_error If new length exceeds @c max_size().
4656  * @throw std::out_of_range If @a __pos is beyond the end of this
4657  * string.
4658  *
4659  * Inserts @a __n copies of character @a __c starting at index
4660  * @a __pos. If adding characters causes the length to exceed
4661  * max_size(), length_error is thrown. If @a __pos > length(),
4662  * out_of_range is thrown. The value of the string doesn't
4663  * change if an error is thrown.
4664  */
4665  basic_string&
4666  insert(size_type __pos, size_type __n, _CharT __c)
4667  { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
4668  size_type(0), __n, __c); }
4669 
4670  /**
4671  * @brief Insert one character.
4672  * @param __p Iterator referencing position in string to insert at.
4673  * @param __c The character to insert.
4674  * @return Iterator referencing newly inserted char.
4675  * @throw std::length_error If new length exceeds @c max_size().
4676  *
4677  * Inserts character @a __c at position referenced by @a __p.
4678  * If adding character causes the length to exceed max_size(),
4679  * length_error is thrown. If @a __p is beyond end of string,
4680  * out_of_range is thrown. The value of the string doesn't
4681  * change if an error is thrown.
4682  */
4683  iterator
4684  insert(iterator __p, _CharT __c)
4685  {
4686  _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4687  const size_type __pos = __p - _M_ibegin();
4688  _M_replace_aux(__pos, size_type(0), size_type(1), __c);
4689  _M_rep()->_M_set_leaked();
4690  return iterator(_M_data() + __pos);
4691  }
4692 
4693 #if __cplusplus >= 201703L
4694  /**
4695  * @brief Insert a string_view.
4696  * @param __pos Position in string to insert at.
4697  * @param __svt The object convertible to string_view to insert.
4698  * @return Reference to this string.
4699  */
4700  template<typename _Tp>
4701  _If_sv<_Tp, basic_string&>
4702  insert(size_type __pos, const _Tp& __svt)
4703  {
4704  __sv_type __sv = __svt;
4705  return this->insert(__pos, __sv.data(), __sv.size());
4706  }
4707 
4708  /**
4709  * @brief Insert a string_view.
4710  * @param __pos Position in string to insert at.
4711  * @param __svt The object convertible to string_view to insert from.
4712  * @param __pos Position in string_view to insert
4713  * from.
4714  * @param __n The number of characters to insert.
4715  * @return Reference to this string.
4716  */
4717  template<typename _Tp>
4718  _If_sv<_Tp, basic_string&>
4719  insert(size_type __pos1, const _Tp& __svt,
4720  size_type __pos2, size_type __n = npos)
4721  {
4722  __sv_type __sv = __svt;
4723  return this->replace(__pos1, size_type(0), __sv.data()
4724  + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
4725  std::__sv_limit(__sv.size(), __pos2, __n));
4726  }
4727 #endif // C++17
4728 
4729  /**
4730  * @brief Remove characters.
4731  * @param __pos Index of first character to remove (default 0).
4732  * @param __n Number of characters to remove (default remainder).
4733  * @return Reference to this string.
4734  * @throw std::out_of_range If @a pos is beyond the end of this
4735  * string.
4736  *
4737  * Removes @a __n characters from this string starting at @a
4738  * __pos. The length of the string is reduced by @a __n. If
4739  * there are < @a __n characters to remove, the remainder of
4740  * the string is truncated. If @a __p is beyond end of string,
4741  * out_of_range is thrown. The value of the string doesn't
4742  * change if an error is thrown.
4743  */
4744  basic_string&
4745  erase(size_type __pos = 0, size_type __n = npos)
4746  {
4747  _M_mutate(_M_check(__pos, "basic_string::erase"),
4748  _M_limit(__pos, __n), size_type(0));
4749  return *this;
4750  }
4751 
4752  /**
4753  * @brief Remove one character.
4754  * @param __position Iterator referencing the character to remove.
4755  * @return iterator referencing same location after removal.
4756  *
4757  * Removes the character at @a __position from this string. The value
4758  * of the string doesn't change if an error is thrown.
4759  */
4760  iterator
4761  erase(iterator __position)
4762  {
4763  _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
4764  && __position < _M_iend());
4765  const size_type __pos = __position - _M_ibegin();
4766  _M_mutate(__pos, size_type(1), size_type(0));
4767  _M_rep()->_M_set_leaked();
4768  return iterator(_M_data() + __pos);
4769  }
4770 
4771  /**
4772  * @brief Remove a range of characters.
4773  * @param __first Iterator referencing the first character to remove.
4774  * @param __last Iterator referencing the end of the range.
4775  * @return Iterator referencing location of first after removal.
4776  *
4777  * Removes the characters in the range [first,last) from this string.
4778  * The value of the string doesn't change if an error is thrown.
4779  */
4780  iterator
4781  erase(iterator __first, iterator __last);
4782 
4783 #if __cplusplus >= 201103L
4784  /**
4785  * @brief Remove the last character.
4786  *
4787  * The string must be non-empty.
4788  */
4789  void
4790  pop_back() // FIXME C++11: should be noexcept.
4791  {
4792  __glibcxx_assert(!empty());
4793  erase(size() - 1, 1);
4794  }
4795 #endif // C++11
4796 
4797  /**
4798  * @brief Replace characters with value from another string.
4799  * @param __pos Index of first character to replace.
4800  * @param __n Number of characters to be replaced.
4801  * @param __str String to insert.
4802  * @return Reference to this string.
4803  * @throw std::out_of_range If @a pos is beyond the end of this
4804  * string.
4805  * @throw std::length_error If new length exceeds @c max_size().
4806  *
4807  * Removes the characters in the range [__pos,__pos+__n) from
4808  * this string. In place, the value of @a __str is inserted.
4809  * If @a __pos is beyond end of string, out_of_range is thrown.
4810  * If the length of the result exceeds max_size(), length_error
4811  * is thrown. The value of the string doesn't change if an
4812  * error is thrown.
4813  */
4814  basic_string&
4815  replace(size_type __pos, size_type __n, const basic_string& __str)
4816  { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
4817 
4818  /**
4819  * @brief Replace characters with value from another string.
4820  * @param __pos1 Index of first character to replace.
4821  * @param __n1 Number of characters to be replaced.
4822  * @param __str String to insert.
4823  * @param __pos2 Index of first character of str to use.
4824  * @param __n2 Number of characters from str to use.
4825  * @return Reference to this string.
4826  * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4827  * __str.size().
4828  * @throw std::length_error If new length exceeds @c max_size().
4829  *
4830  * Removes the characters in the range [__pos1,__pos1 + n) from this
4831  * string. In place, the value of @a __str is inserted. If @a __pos is
4832  * beyond end of string, out_of_range is thrown. If the length of the
4833  * result exceeds max_size(), length_error is thrown. The value of the
4834  * string doesn't change if an error is thrown.
4835  */
4836  basic_string&
4837  replace(size_type __pos1, size_type __n1, const basic_string& __str,
4838  size_type __pos2, size_type __n2 = npos)
4839  { return this->replace(__pos1, __n1, __str._M_data()
4840  + __str._M_check(__pos2, "basic_string::replace"),
4841  __str._M_limit(__pos2, __n2)); }
4842 
4843  /**
4844  * @brief Replace characters with value of a C substring.
4845  * @param __pos Index of first character to replace.
4846  * @param __n1 Number of characters to be replaced.
4847  * @param __s C string to insert.
4848  * @param __n2 Number of characters from @a s to use.
4849  * @return Reference to this string.
4850  * @throw std::out_of_range If @a pos1 > size().
4851  * @throw std::length_error If new length exceeds @c max_size().
4852  *
4853  * Removes the characters in the range [__pos,__pos + __n1)
4854  * from this string. In place, the first @a __n2 characters of
4855  * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4856  * @a __pos is beyond end of string, out_of_range is thrown. If
4857  * the length of result exceeds max_size(), length_error is
4858  * thrown. The value of the string doesn't change if an error
4859  * is thrown.
4860  */
4861  basic_string&
4862  replace(size_type __pos, size_type __n1, const _CharT* __s,
4863  size_type __n2);
4864 
4865  /**
4866  * @brief Replace characters with value of a C string.
4867  * @param __pos Index of first character to replace.
4868  * @param __n1 Number of characters to be replaced.
4869  * @param __s C string to insert.
4870  * @return Reference to this string.
4871  * @throw std::out_of_range If @a pos > size().
4872  * @throw std::length_error If new length exceeds @c max_size().
4873  *
4874  * Removes the characters in the range [__pos,__pos + __n1)
4875  * from this string. In place, the characters of @a __s are
4876  * inserted. If @a __pos is beyond end of string, out_of_range
4877  * is thrown. If the length of result exceeds max_size(),
4878  * length_error is thrown. The value of the string doesn't
4879  * change if an error is thrown.
4880  */
4881  basic_string&
4882  replace(size_type __pos, size_type __n1, const _CharT* __s)
4883  {
4884  __glibcxx_requires_string(__s);
4885  return this->replace(__pos, __n1, __s, traits_type::length(__s));
4886  }
4887 
4888  /**
4889  * @brief Replace characters with multiple characters.
4890  * @param __pos Index of first character to replace.
4891  * @param __n1 Number of characters to be replaced.
4892  * @param __n2 Number of characters to insert.
4893  * @param __c Character to insert.
4894  * @return Reference to this string.
4895  * @throw std::out_of_range If @a __pos > size().
4896  * @throw std::length_error If new length exceeds @c max_size().
4897  *
4898  * Removes the characters in the range [pos,pos + n1) from this
4899  * string. In place, @a __n2 copies of @a __c are inserted.
4900  * If @a __pos is beyond end of string, out_of_range is thrown.
4901  * If the length of result exceeds max_size(), length_error is
4902  * thrown. The value of the string doesn't change if an error
4903  * is thrown.
4904  */
4905  basic_string&
4906  replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
4907  { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
4908  _M_limit(__pos, __n1), __n2, __c); }
4909 
4910  /**
4911  * @brief Replace range of characters with string.
4912  * @param __i1 Iterator referencing start of range to replace.
4913  * @param __i2 Iterator referencing end of range to replace.
4914  * @param __str String value to insert.
4915  * @return Reference to this string.
4916  * @throw std::length_error If new length exceeds @c max_size().
4917  *
4918  * Removes the characters in the range [__i1,__i2). In place,
4919  * the value of @a __str is inserted. If the length of result
4920  * exceeds max_size(), length_error is thrown. The value of
4921  * the string doesn't change if an error is thrown.
4922  */
4923  basic_string&
4924  replace(iterator __i1, iterator __i2, const basic_string& __str)
4925  { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
4926 
4927  /**
4928  * @brief Replace range of characters with C substring.
4929  * @param __i1 Iterator referencing start of range to replace.
4930  * @param __i2 Iterator referencing end of range to replace.
4931  * @param __s C string value to insert.
4932  * @param __n Number of characters from s to insert.
4933  * @return Reference to this string.
4934  * @throw std::length_error If new length exceeds @c max_size().
4935  *
4936  * Removes the characters in the range [__i1,__i2). In place,
4937  * the first @a __n characters of @a __s are inserted. If the
4938  * length of result exceeds max_size(), length_error is thrown.
4939  * The value of the string doesn't change if an error is
4940  * thrown.
4941  */
4942  basic_string&
4943  replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
4944  {
4945  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4946  && __i2 <= _M_iend());
4947  return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
4948  }
4949 
4950  /**
4951  * @brief Replace range of characters with C string.
4952  * @param __i1 Iterator referencing start of range to replace.
4953  * @param __i2 Iterator referencing end of range to replace.
4954  * @param __s C string value to insert.
4955  * @return Reference to this string.
4956  * @throw std::length_error If new length exceeds @c max_size().
4957  *
4958  * Removes the characters in the range [__i1,__i2). In place,
4959  * the characters of @a __s are inserted. If the length of
4960  * result exceeds max_size(), length_error is thrown. The
4961  * value of the string doesn't change if an error is thrown.
4962  */
4963  basic_string&
4964  replace(iterator __i1, iterator __i2, const _CharT* __s)
4965  {
4966  __glibcxx_requires_string(__s);
4967  return this->replace(__i1, __i2, __s, traits_type::length(__s));
4968  }
4969 
4970  /**
4971  * @brief Replace range of characters with multiple characters
4972  * @param __i1 Iterator referencing start of range to replace.
4973  * @param __i2 Iterator referencing end of range to replace.
4974  * @param __n Number of characters to insert.
4975  * @param __c Character to insert.
4976  * @return Reference to this string.
4977  * @throw std::length_error If new length exceeds @c max_size().
4978  *
4979  * Removes the characters in the range [__i1,__i2). In place,
4980  * @a __n copies of @a __c are inserted. If the length of
4981  * result exceeds max_size(), length_error is thrown. The
4982  * value of the string doesn't change if an error is thrown.
4983  */
4984  basic_string&
4985  replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
4986  {
4987  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4988  && __i2 <= _M_iend());
4989  return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
4990  }
4991 
4992  /**
4993  * @brief Replace range of characters with range.
4994  * @param __i1 Iterator referencing start of range to replace.
4995  * @param __i2 Iterator referencing end of range to replace.
4996  * @param __k1 Iterator referencing start of range to insert.
4997  * @param __k2 Iterator referencing end of range to insert.
4998  * @return Reference to this string.
4999  * @throw std::length_error If new length exceeds @c max_size().
5000  *
5001  * Removes the characters in the range [__i1,__i2). In place,
5002  * characters in the range [__k1,__k2) are inserted. If the
5003  * length of result exceeds max_size(), length_error is thrown.
5004  * The value of the string doesn't change if an error is
5005  * thrown.
5006  */
5007  template<class _InputIterator>
5008  basic_string&
5009  replace(iterator __i1, iterator __i2,
5010  _InputIterator __k1, _InputIterator __k2)
5011  {
5012  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5013  && __i2 <= _M_iend());
5014  __glibcxx_requires_valid_range(__k1, __k2);
5015  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
5016  return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
5017  }
5018 
5019  // Specializations for the common case of pointer and iterator:
5020  // useful to avoid the overhead of temporary buffering in _M_replace.
5021  basic_string&
5022  replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
5023  {
5024  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5025  && __i2 <= _M_iend());
5026  __glibcxx_requires_valid_range(__k1, __k2);
5027  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
5028  __k1, __k2 - __k1);
5029  }
5030 
5031  basic_string&
5032  replace(iterator __i1, iterator __i2,
5033  const _CharT* __k1, const _CharT* __k2)
5034  {
5035  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5036  && __i2 <= _M_iend());
5037  __glibcxx_requires_valid_range(__k1, __k2);
5038  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
5039  __k1, __k2 - __k1);
5040  }
5041 
5042  basic_string&
5043  replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
5044  {
5045  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5046  && __i2 <= _M_iend());
5047  __glibcxx_requires_valid_range(__k1, __k2);
5048  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
5049  __k1.base(), __k2 - __k1);
5050  }
5051 
5052  basic_string&
5053  replace(iterator __i1, iterator __i2,
5054  const_iterator __k1, const_iterator __k2)
5055  {
5056  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5057  && __i2 <= _M_iend());
5058  __glibcxx_requires_valid_range(__k1, __k2);
5059  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
5060  __k1.base(), __k2 - __k1);
5061  }
5062 
5063 #if __cplusplus >= 201103L
5064  /**
5065  * @brief Replace range of characters with initializer_list.
5066  * @param __i1 Iterator referencing start of range to replace.
5067  * @param __i2 Iterator referencing end of range to replace.
5068  * @param __l The initializer_list of characters to insert.
5069  * @return Reference to this string.
5070  * @throw std::length_error If new length exceeds @c max_size().
5071  *
5072  * Removes the characters in the range [__i1,__i2). In place,
5073  * characters in the range [__k1,__k2) are inserted. If the
5074  * length of result exceeds max_size(), length_error is thrown.
5075  * The value of the string doesn't change if an error is
5076  * thrown.
5077  */
5078  basic_string& replace(iterator __i1, iterator __i2,
5080  { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
5081 #endif // C++11
5082 
5083 #if __cplusplus >= 201703L
5084  /**
5085  * @brief Replace range of characters with string_view.
5086  * @param __pos The position to replace at.
5087  * @param __n The number of characters to replace.
5088  * @param __svt The object convertible to string_view to insert.
5089  * @return Reference to this string.
5090  */
5091  template<typename _Tp>
5092  _If_sv<_Tp, basic_string&>
5093  replace(size_type __pos, size_type __n, const _Tp& __svt)
5094  {
5095  __sv_type __sv = __svt;
5096  return this->replace(__pos, __n, __sv.data(), __sv.size());
5097  }
5098 
5099  /**
5100  * @brief Replace range of characters with string_view.
5101  * @param __pos1 The position to replace at.
5102  * @param __n1 The number of characters to replace.
5103  * @param __svt The object convertible to string_view to insert from.
5104  * @param __pos2 The position in the string_view to insert from.
5105  * @param __n2 The number of characters to insert.
5106  * @return Reference to this string.
5107  */
5108  template<typename _Tp>
5109  _If_sv<_Tp, basic_string&>
5110  replace(size_type __pos1, size_type __n1, const _Tp& __svt,
5111  size_type __pos2, size_type __n2 = npos)
5112  {
5113  __sv_type __sv = __svt;
5114  return this->replace(__pos1, __n1,
5115  __sv.data()
5116  + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
5117  std::__sv_limit(__sv.size(), __pos2, __n2));
5118  }
5119 
5120  /**
5121  * @brief Replace range of characters with string_view.
5122  * @param __i1 An iterator referencing the start position
5123  to replace at.
5124  * @param __i2 An iterator referencing the end position
5125  for the replace.
5126  * @param __svt The object convertible to string_view to insert from.
5127  * @return Reference to this string.
5128  */
5129  template<typename _Tp>
5130  _If_sv<_Tp, basic_string&>
5131  replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
5132  {
5133  __sv_type __sv = __svt;
5134  return this->replace(__i1 - begin(), __i2 - __i1, __sv);
5135  }
5136 #endif // C++17
5137 
5138  private:
5139  template<class _Integer>
5140  basic_string&
5141  _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
5142  _Integer __val, __true_type)
5143  { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
5144 
5145  template<class _InputIterator>
5146  basic_string&
5147  _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
5148  _InputIterator __k2, __false_type);
5149 
5150  basic_string&
5151  _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
5152  _CharT __c);
5153 
5154  basic_string&
5155  _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
5156  size_type __n2);
5157 
5158  // _S_construct_aux is used to implement the 21.3.1 para 15 which
5159  // requires special behaviour if _InIter is an integral type
5160  template<class _InIterator>
5161  static _CharT*
5162  _S_construct_aux(_InIterator __beg, _InIterator __end,
5163  const _Alloc& __a, __false_type)
5164  {
5165  typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
5166  return _S_construct(__beg, __end, __a, _Tag());
5167  }
5168 
5169  // _GLIBCXX_RESOLVE_LIB_DEFECTS
5170  // 438. Ambiguity in the "do the right thing" clause
5171  template<class _Integer>
5172  static _CharT*
5173  _S_construct_aux(_Integer __beg, _Integer __end,
5174  const _Alloc& __a, __true_type)
5175  { return _S_construct_aux_2(static_cast<size_type>(__beg),
5176  __end, __a); }
5177 
5178  static _CharT*
5179  _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
5180  { return _S_construct(__req, __c, __a); }
5181 
5182  template<class _InIterator>
5183  static _CharT*
5184  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
5185  {
5186  typedef typename std::__is_integer<_InIterator>::__type _Integral;
5187  return _S_construct_aux(__beg, __end, __a, _Integral());
5188  }
5189 
5190  // For Input Iterators, used in istreambuf_iterators, etc.
5191  template<class _InIterator>
5192  static _CharT*
5193  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
5194  input_iterator_tag);
5195 
5196  // For forward_iterators up to random_access_iterators, used for
5197  // string::iterator, _CharT*, etc.
5198  template<class _FwdIterator>
5199  static _CharT*
5200  _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
5201  forward_iterator_tag);
5202 
5203  static _CharT*
5204  _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
5205 
5206  public:
5207 
5208  /**
5209  * @brief Copy substring into C string.
5210  * @param __s C string to copy value into.
5211  * @param __n Number of characters to copy.
5212  * @param __pos Index of first character to copy.
5213  * @return Number of characters actually copied
5214  * @throw std::out_of_range If __pos > size().
5215  *
5216  * Copies up to @a __n characters starting at @a __pos into the
5217  * C string @a __s. If @a __pos is %greater than size(),
5218  * out_of_range is thrown.
5219  */
5220  size_type
5221  copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
5222 
5223  /**
5224  * @brief Swap contents with another string.
5225  * @param __s String to swap with.
5226  *
5227  * Exchanges the contents of this string with that of @a __s in constant
5228  * time.
5229  */
5230  void
5231  swap(basic_string& __s)
5232  _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value);
5233 
5234  // String operations:
5235  /**
5236  * @brief Return const pointer to null-terminated contents.
5237  *
5238  * This is a handle to internal data. Do not modify or dire things may
5239  * happen.
5240  */
5241  const _CharT*
5242  c_str() const _GLIBCXX_NOEXCEPT
5243  { return _M_data(); }
5244 
5245  /**
5246  * @brief Return const pointer to contents.
5247  *
5248  * This is a pointer to internal data. It is undefined to modify
5249  * the contents through the returned pointer. To get a pointer that
5250  * allows modifying the contents use @c &str[0] instead,
5251  * (or in C++17 the non-const @c str.data() overload).
5252  */
5253  const _CharT*
5254  data() const _GLIBCXX_NOEXCEPT
5255  { return _M_data(); }
5256 
5257 #if __cplusplus >= 201703L
5258  /**
5259  * @brief Return non-const pointer to contents.
5260  *
5261  * This is a pointer to the character sequence held by the string.
5262  * Modifying the characters in the sequence is allowed.
5263  */
5264  _CharT*
5265  data() noexcept
5266  {
5267  _M_leak();
5268  return _M_data();
5269  }
5270 #endif
5271 
5272  /**
5273  * @brief Return copy of allocator used to construct this string.
5274  */
5275  allocator_type
5276  get_allocator() const _GLIBCXX_NOEXCEPT
5277  { return _M_dataplus; }
5278 
5279  /**
5280  * @brief Find position of a C substring.
5281  * @param __s C string to locate.
5282  * @param __pos Index of character to search from.
5283  * @param __n Number of characters from @a s to search for.
5284  * @return Index of start of first occurrence.
5285  *
5286  * Starting from @a __pos, searches forward for the first @a
5287  * __n characters in @a __s within this string. If found,
5288  * returns the index where it begins. If not found, returns
5289  * npos.
5290  */
5291  size_type
5292  find(const _CharT* __s, size_type __pos, size_type __n) const
5293  _GLIBCXX_NOEXCEPT;
5294 
5295  /**
5296  * @brief Find position of a string.
5297  * @param __str String to locate.
5298  * @param __pos Index of character to search from (default 0).
5299  * @return Index of start of first occurrence.
5300  *
5301  * Starting from @a __pos, searches forward for value of @a __str within
5302  * this string. If found, returns the index where it begins. If not
5303  * found, returns npos.
5304  */
5305  size_type
5306  find(const basic_string& __str, size_type __pos = 0) const
5307  _GLIBCXX_NOEXCEPT
5308  { return this->find(__str.data(), __pos, __str.size()); }
5309 
5310  /**
5311  * @brief Find position of a C string.
5312  * @param __s C string to locate.
5313  * @param __pos Index of character to search from (default 0).
5314  * @return Index of start of first occurrence.
5315  *
5316  * Starting from @a __pos, searches forward for the value of @a
5317  * __s within this string. If found, returns the index where
5318  * it begins. If not found, returns npos.
5319  */
5320  size_type
5321  find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
5322  {
5323  __glibcxx_requires_string(__s);
5324  return this->find(__s, __pos, traits_type::length(__s));
5325  }
5326 
5327  /**
5328  * @brief Find position of a character.
5329  * @param __c Character to locate.
5330  * @param __pos Index of character to search from (default 0).
5331  * @return Index of first occurrence.
5332  *
5333  * Starting from @a __pos, searches forward for @a __c within
5334  * this string. If found, returns the index where it was
5335  * found. If not found, returns npos.
5336  */
5337  size_type
5338  find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
5339 
5340 #if __cplusplus >= 201703L
5341  /**
5342  * @brief Find position of a string_view.
5343  * @param __svt The object convertible to string_view to locate.
5344  * @param __pos Index of character to search from (default 0).
5345  * @return Index of start of first occurrence.
5346  */
5347  template<typename _Tp>
5348  _If_sv<_Tp, size_type>
5349  find(const _Tp& __svt, size_type __pos = 0) const
5350  noexcept(is_same<_Tp, __sv_type>::value)
5351  {
5352  __sv_type __sv = __svt;
5353  return this->find(__sv.data(), __pos, __sv.size());
5354  }
5355 #endif // C++17
5356 
5357  /**
5358  * @brief Find last position of a string.
5359  * @param __str String to locate.
5360  * @param __pos Index of character to search back from (default end).
5361  * @return Index of start of last occurrence.
5362  *
5363  * Starting from @a __pos, searches backward for value of @a
5364  * __str within this string. If found, returns the index where
5365  * it begins. If not found, returns npos.
5366  */
5367  size_type
5368  rfind(const basic_string& __str, size_type __pos = npos) const
5369  _GLIBCXX_NOEXCEPT
5370  { return this->rfind(__str.data(), __pos, __str.size()); }
5371 
5372  /**
5373  * @brief Find last position of a C substring.
5374  * @param __s C string to locate.
5375  * @param __pos Index of character to search back from.
5376  * @param __n Number of characters from s to search for.
5377  * @return Index of start of last occurrence.
5378  *
5379  * Starting from @a __pos, searches backward for the first @a
5380  * __n characters in @a __s within this string. If found,
5381  * returns the index where it begins. If not found, returns
5382  * npos.
5383  */
5384  size_type
5385  rfind(const _CharT* __s, size_type __pos, size_type __n) const
5386  _GLIBCXX_NOEXCEPT;
5387 
5388  /**
5389  * @brief Find last position of a C string.
5390  * @param __s C string to locate.
5391  * @param __pos Index of character to start search at (default end).
5392  * @return Index of start of last occurrence.
5393  *
5394  * Starting from @a __pos, searches backward for the value of
5395  * @a __s within this string. If found, returns the index
5396  * where it begins. If not found, returns npos.
5397  */
5398  size_type
5399  rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
5400  {
5401  __glibcxx_requires_string(__s);
5402  return this->rfind(__s, __pos, traits_type::length(__s));
5403  }
5404 
5405  /**
5406  * @brief Find last position of a character.
5407  * @param __c Character to locate.
5408  * @param __pos Index of character to search back from (default end).
5409  * @return Index of last occurrence.
5410  *
5411  * Starting from @a __pos, searches backward for @a __c within
5412  * this string. If found, returns the index where it was
5413  * found. If not found, returns npos.
5414  */
5415  size_type
5416  rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
5417 
5418 #if __cplusplus >= 201703L
5419  /**
5420  * @brief Find last position of a string_view.
5421  * @param __svt The object convertible to string_view to locate.
5422  * @param __pos Index of character to search back from (default end).
5423  * @return Index of start of last occurrence.
5424  */
5425  template<typename _Tp>
5426  _If_sv<_Tp, size_type>
5427  rfind(const _Tp& __svt, size_type __pos = npos) const
5429  {
5430  __sv_type __sv = __svt;
5431  return this->rfind(__sv.data(), __pos, __sv.size());
5432  }
5433 #endif // C++17
5434 
5435  /**
5436  * @brief Find position of a character of string.
5437  * @param __str String containing characters to locate.
5438  * @param __pos Index of character to search from (default 0).
5439  * @return Index of first occurrence.
5440  *
5441  * Starting from @a __pos, searches forward for one of the
5442  * characters of @a __str within this string. If found,
5443  * returns the index where it was found. If not found, returns
5444  * npos.
5445  */
5446  size_type
5447  find_first_of(const basic_string& __str, size_type __pos = 0) const
5448  _GLIBCXX_NOEXCEPT
5449  { return this->find_first_of(__str.data(), __pos, __str.size()); }
5450 
5451  /**
5452  * @brief Find position of a character of C substring.
5453  * @param __s String containing characters to locate.
5454  * @param __pos Index of character to search from.
5455  * @param __n Number of characters from s to search for.
5456  * @return Index of first occurrence.
5457  *
5458  * Starting from @a __pos, searches forward for one of the
5459  * first @a __n characters of @a __s within this string. If
5460  * found, returns the index where it was found. If not found,
5461  * returns npos.
5462  */
5463  size_type
5464  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
5465  _GLIBCXX_NOEXCEPT;
5466 
5467  /**
5468  * @brief Find position of a character of C string.
5469  * @param __s String containing characters to locate.
5470  * @param __pos Index of character to search from (default 0).
5471  * @return Index of first occurrence.
5472  *
5473  * Starting from @a __pos, searches forward for one of the
5474  * characters of @a __s within this string. If found, returns
5475  * the index where it was found. If not found, returns npos.
5476  */
5477  size_type
5478  find_first_of(const _CharT* __s, size_type __pos = 0) const
5479  _GLIBCXX_NOEXCEPT
5480  {
5481  __glibcxx_requires_string(__s);
5482  return this->find_first_of(__s, __pos, traits_type::length(__s));
5483  }
5484 
5485  /**
5486  * @brief Find position of a character.
5487  * @param __c Character to locate.
5488  * @param __pos Index of character to search from (default 0).
5489  * @return Index of first occurrence.
5490  *
5491  * Starting from @a __pos, searches forward for the character
5492  * @a __c within this string. If found, returns the index
5493  * where it was found. If not found, returns npos.
5494  *
5495  * Note: equivalent to find(__c, __pos).
5496  */
5497  size_type
5498  find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
5499  { return this->find(__c, __pos); }
5500 
5501 #if __cplusplus >= 201703L
5502  /**
5503  * @brief Find position of a character of a string_view.
5504  * @param __svt An object convertible to string_view containing
5505  * characters to locate.
5506  * @param __pos Index of character to search from (default 0).
5507  * @return Index of first occurrence.
5508  */
5509  template<typename _Tp>
5510  _If_sv<_Tp, size_type>
5511  find_first_of(const _Tp& __svt, size_type __pos = 0) const
5512  noexcept(is_same<_Tp, __sv_type>::value)
5513  {
5514  __sv_type __sv = __svt;
5515  return this->find_first_of(__sv.data(), __pos, __sv.size());
5516  }
5517 #endif // C++17
5518 
5519  /**
5520  * @brief Find last position of a character of string.
5521  * @param __str String containing characters to locate.
5522  * @param __pos Index of character to search back from (default end).
5523  * @return Index of last occurrence.
5524  *
5525  * Starting from @a __pos, searches backward for one of the
5526  * characters of @a __str within this string. If found,
5527  * returns the index where it was found. If not found, returns
5528  * npos.
5529  */
5530  size_type
5531  find_last_of(const basic_string& __str, size_type __pos = npos) const
5532  _GLIBCXX_NOEXCEPT
5533  { return this->find_last_of(__str.data(), __pos, __str.size()); }
5534 
5535  /**
5536  * @brief Find last position of a character of C substring.
5537  * @param __s C string containing characters to locate.
5538  * @param __pos Index of character to search back from.
5539  * @param __n Number of characters from s to search for.
5540  * @return Index of last occurrence.
5541  *
5542  * Starting from @a __pos, searches backward for one of the
5543  * first @a __n characters of @a __s within this string. If
5544  * found, returns the index where it was found. If not found,
5545  * returns npos.
5546  */
5547  size_type
5548  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
5549  _GLIBCXX_NOEXCEPT;
5550 
5551  /**
5552  * @brief Find last position of a character of C string.
5553  * @param __s C string containing characters to locate.
5554  * @param __pos Index of character to search back from (default end).
5555  * @return Index of last occurrence.
5556  *
5557  * Starting from @a __pos, searches backward for one of the
5558  * characters of @a __s within this string. If found, returns
5559  * the index where it was found. If not found, returns npos.
5560  */
5561  size_type
5562  find_last_of(const _CharT* __s, size_type __pos = npos) const
5563  _GLIBCXX_NOEXCEPT
5564  {
5565  __glibcxx_requires_string(__s);
5566  return this->find_last_of(__s, __pos, traits_type::length(__s));
5567  }
5568 
5569  /**
5570  * @brief Find last position of a character.
5571  * @param __c Character to locate.
5572  * @param __pos Index of character to search back from (default end).
5573  * @return Index of last occurrence.
5574  *
5575  * Starting from @a __pos, searches backward for @a __c within
5576  * this string. If found, returns the index where it was
5577  * found. If not found, returns npos.
5578  *
5579  * Note: equivalent to rfind(__c, __pos).
5580  */
5581  size_type
5582  find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
5583  { return this->rfind(__c, __pos); }
5584 
5585 #if __cplusplus >= 201703L
5586  /**
5587  * @brief Find last position of a character of string.
5588  * @param __svt An object convertible to string_view containing
5589  * characters to locate.
5590  * @param __pos Index of character to search back from (default end).
5591  * @return Index of last occurrence.
5592  */
5593  template<typename _Tp>
5594  _If_sv<_Tp, size_type>
5595  find_last_of(const _Tp& __svt, size_type __pos = npos) const
5597  {
5598  __sv_type __sv = __svt;
5599  return this->find_last_of(__sv.data(), __pos, __sv.size());
5600  }
5601 #endif // C++17
5602 
5603  /**
5604  * @brief Find position of a character not in string.
5605  * @param __str String containing characters to avoid.
5606  * @param __pos Index of character to search from (default 0).
5607  * @return Index of first occurrence.
5608  *
5609  * Starting from @a __pos, searches forward for a character not contained
5610  * in @a __str within this string. If found, returns the index where it
5611  * was found. If not found, returns npos.
5612  */
5613  size_type
5614  find_first_not_of(const basic_string& __str, size_type __pos = 0) const
5615  _GLIBCXX_NOEXCEPT
5616  { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
5617 
5618  /**
5619  * @brief Find position of a character not in C substring.
5620  * @param __s C string containing characters to avoid.
5621  * @param __pos Index of character to search from.
5622  * @param __n Number of characters from __s to consider.
5623  * @return Index of first occurrence.
5624  *
5625  * Starting from @a __pos, searches forward for a character not
5626  * contained in the first @a __n characters of @a __s within
5627  * this string. If found, returns the index where it was
5628  * found. If not found, returns npos.
5629  */
5630  size_type
5631  find_first_not_of(const _CharT* __s, size_type __pos,
5632  size_type __n) const _GLIBCXX_NOEXCEPT;
5633 
5634  /**
5635  * @brief Find position of a character not in C string.
5636  * @param __s C string containing characters to avoid.
5637  * @param __pos Index of character to search from (default 0).
5638  * @return Index of first occurrence.
5639  *
5640  * Starting from @a __pos, searches forward for a character not
5641  * contained in @a __s within this string. If found, returns
5642  * the index where it was found. If not found, returns npos.
5643  */
5644  size_type
5645  find_first_not_of(const _CharT* __s, size_type __pos = 0) const
5646  _GLIBCXX_NOEXCEPT
5647  {
5648  __glibcxx_requires_string(__s);
5649  return this->find_first_not_of(__s, __pos, traits_type::length(__s));
5650  }
5651 
5652  /**
5653  * @brief Find position of a different character.
5654  * @param __c Character to avoid.
5655  * @param __pos Index of character to search from (default 0).
5656  * @return Index of first occurrence.
5657  *
5658  * Starting from @a __pos, searches forward for a character
5659  * other than @a __c within this string. If found, returns the
5660  * index where it was found. If not found, returns npos.
5661  */
5662  size_type
5663  find_first_not_of(_CharT __c, size_type __pos = 0) const
5664  _GLIBCXX_NOEXCEPT;
5665 
5666 #if __cplusplus >= 201703L
5667  /**
5668  * @brief Find position of a character not in a string_view.
5669  * @param __svt An object convertible to string_view containing
5670  * characters to avoid.
5671  * @param __pos Index of character to search from (default 0).
5672  * @return Index of first occurrence.
5673  */
5674  template<typename _Tp>
5675  _If_sv<_Tp, size_type>
5676  find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
5677  noexcept(is_same<_Tp, __sv_type>::value)
5678  {
5679  __sv_type __sv = __svt;
5680  return this->find_first_not_of(__sv.data(), __pos, __sv.size());
5681  }
5682 #endif // C++17
5683 
5684  /**
5685  * @brief Find last position of a character not in string.
5686  * @param __str String containing characters to avoid.
5687  * @param __pos Index of character to search back from (default end).
5688  * @return Index of last occurrence.
5689  *
5690  * Starting from @a __pos, searches backward for a character
5691  * not contained in @a __str within this string. If found,
5692  * returns the index where it was found. If not found, returns
5693  * npos.
5694  */
5695  size_type
5696  find_last_not_of(const basic_string& __str, size_type __pos = npos) const
5697  _GLIBCXX_NOEXCEPT
5698  { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
5699 
5700  /**
5701  * @brief Find last position of a character not in C substring.
5702  * @param __s C string containing characters to avoid.
5703  * @param __pos Index of character to search back from.
5704  * @param __n Number of characters from s to consider.
5705  * @return Index of last occurrence.
5706  *
5707  * Starting from @a __pos, searches backward for a character not
5708  * contained in the first @a __n characters of @a __s within this string.
5709  * If found, returns the index where it was found. If not found,
5710  * returns npos.
5711  */
5712  size_type
5713  find_last_not_of(const _CharT* __s, size_type __pos,
5714  size_type __n) const _GLIBCXX_NOEXCEPT;
5715  /**
5716  * @brief Find last position of a character not in C string.
5717  * @param __s C string containing characters to avoid.
5718  * @param __pos Index of character to search back from (default end).
5719  * @return Index of last occurrence.
5720  *
5721  * Starting from @a __pos, searches backward for a character
5722  * not contained in @a __s within this string. If found,
5723  * returns the index where it was found. If not found, returns
5724  * npos.
5725  */
5726  size_type
5727  find_last_not_of(const _CharT* __s, size_type __pos = npos) const
5728  _GLIBCXX_NOEXCEPT
5729  {
5730  __glibcxx_requires_string(__s);
5731  return this->find_last_not_of(__s, __pos, traits_type::length(__s));
5732  }
5733 
5734  /**
5735  * @brief Find last position of a different character.
5736  * @param __c Character to avoid.
5737  * @param __pos Index of character to search back from (default end).
5738  * @return Index of last occurrence.
5739  *
5740  * Starting from @a __pos, searches backward for a character other than
5741  * @a __c within this string. If found, returns the index where it was
5742  * found. If not found, returns npos.
5743  */
5744  size_type
5745  find_last_not_of(_CharT __c, size_type __pos = npos) const
5746  _GLIBCXX_NOEXCEPT;
5747 
5748 #if __cplusplus >= 201703L
5749  /**
5750  * @brief Find last position of a character not in a string_view.
5751  * @param __svt An object convertible to string_view containing
5752  * characters to avoid.
5753  * @param __pos Index of character to search back from (default end).
5754  * @return Index of last occurrence.
5755  */
5756  template<typename _Tp>
5757  _If_sv<_Tp, size_type>
5758  find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
5760  {
5761  __sv_type __sv = __svt;
5762  return this->find_last_not_of(__sv.data(), __pos, __sv.size());
5763  }
5764 #endif // C++17
5765 
5766  /**
5767  * @brief Get a substring.
5768  * @param __pos Index of first character (default 0).
5769  * @param __n Number of characters in substring (default remainder).
5770  * @return The new string.
5771  * @throw std::out_of_range If __pos > size().
5772  *
5773  * Construct and return a new string using the @a __n
5774  * characters starting at @a __pos. If the string is too
5775  * short, use the remainder of the characters. If @a __pos is
5776  * beyond the end of the string, out_of_range is thrown.
5777  */
5778  basic_string
5779  substr(size_type __pos = 0, size_type __n = npos) const
5780  { return basic_string(*this,
5781  _M_check(__pos, "basic_string::substr"), __n); }
5782 
5783  /**
5784  * @brief Compare to a string.
5785  * @param __str String to compare against.
5786  * @return Integer < 0, 0, or > 0.
5787  *
5788  * Returns an integer < 0 if this string is ordered before @a
5789  * __str, 0 if their values are equivalent, or > 0 if this
5790  * string is ordered after @a __str. Determines the effective
5791  * length rlen of the strings to compare as the smallest of
5792  * size() and str.size(). The function then compares the two
5793  * strings by calling traits::compare(data(), str.data(),rlen).
5794  * If the result of the comparison is nonzero returns it,
5795  * otherwise the shorter one is ordered first.
5796  */
5797  int
5798  compare(const basic_string& __str) const
5799  {
5800  const size_type __size = this->size();
5801  const size_type __osize = __str.size();
5802  const size_type __len = std::min(__size, __osize);
5803 
5804  int __r = traits_type::compare(_M_data(), __str.data(), __len);
5805  if (!__r)
5806  __r = _S_compare(__size, __osize);
5807  return __r;
5808  }
5809 
5810 #if __cplusplus >= 201703L
5811  /**
5812  * @brief Compare to a string_view.
5813  * @param __svt An object convertible to string_view to compare against.
5814  * @return Integer < 0, 0, or > 0.
5815  */
5816  template<typename _Tp>
5817  _If_sv<_Tp, int>
5818  compare(const _Tp& __svt) const
5820  {
5821  __sv_type __sv = __svt;
5822  const size_type __size = this->size();
5823  const size_type __osize = __sv.size();
5824  const size_type __len = std::min(__size, __osize);
5825 
5826  int __r = traits_type::compare(_M_data(), __sv.data(), __len);
5827  if (!__r)
5828  __r = _S_compare(__size, __osize);
5829  return __r;
5830  }
5831 
5832  /**
5833  * @brief Compare to a string_view.
5834  * @param __pos A position in the string to start comparing from.
5835  * @param __n The number of characters to compare.
5836  * @param __svt An object convertible to string_view to compare
5837  * against.
5838  * @return Integer < 0, 0, or > 0.
5839  */
5840  template<typename _Tp>
5841  _If_sv<_Tp, int>
5842  compare(size_type __pos, size_type __n, const _Tp& __svt) const
5844  {
5845  __sv_type __sv = __svt;
5846  return __sv_type(*this).substr(__pos, __n).compare(__sv);
5847  }
5848 
5849  /**
5850  * @brief Compare to a string_view.
5851  * @param __pos1 A position in the string to start comparing from.
5852  * @param __n1 The number of characters to compare.
5853  * @param __svt An object convertible to string_view to compare
5854  * against.
5855  * @param __pos2 A position in the string_view to start comparing from.
5856  * @param __n2 The number of characters to compare.
5857  * @return Integer < 0, 0, or > 0.
5858  */
5859  template<typename _Tp>
5860  _If_sv<_Tp, int>
5861  compare(size_type __pos1, size_type __n1, const _Tp& __svt,
5862  size_type __pos2, size_type __n2 = npos) const
5864  {
5865  __sv_type __sv = __svt;
5866  return __sv_type(*this)
5867  .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
5868  }
5869 #endif // C++17
5870 
5871  /**
5872  * @brief Compare substring to a string.
5873  * @param __pos Index of first character of substring.
5874  * @param __n Number of characters in substring.
5875  * @param __str String to compare against.
5876  * @return Integer < 0, 0, or > 0.
5877  *
5878  * Form the substring of this string from the @a __n characters
5879  * starting at @a __pos. Returns an integer < 0 if the
5880  * substring is ordered before @a __str, 0 if their values are
5881  * equivalent, or > 0 if the substring is ordered after @a
5882  * __str. Determines the effective length rlen of the strings
5883  * to compare as the smallest of the length of the substring
5884  * and @a __str.size(). The function then compares the two
5885  * strings by calling
5886  * traits::compare(substring.data(),str.data(),rlen). If the
5887  * result of the comparison is nonzero returns it, otherwise
5888  * the shorter one is ordered first.
5889  */
5890  int
5891  compare(size_type __pos, size_type __n, const basic_string& __str) const;
5892 
5893  /**
5894  * @brief Compare substring to a substring.
5895  * @param __pos1 Index of first character of substring.
5896  * @param __n1 Number of characters in substring.
5897  * @param __str String to compare against.
5898  * @param __pos2 Index of first character of substring of str.
5899  * @param __n2 Number of characters in substring of str.
5900  * @return Integer < 0, 0, or > 0.
5901  *
5902  * Form the substring of this string from the @a __n1
5903  * characters starting at @a __pos1. Form the substring of @a
5904  * __str from the @a __n2 characters starting at @a __pos2.
5905  * Returns an integer < 0 if this substring is ordered before
5906  * the substring of @a __str, 0 if their values are equivalent,
5907  * or > 0 if this substring is ordered after the substring of
5908  * @a __str. Determines the effective length rlen of the
5909  * strings to compare as the smallest of the lengths of the
5910  * substrings. The function then compares the two strings by
5911  * calling
5912  * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5913  * If the result of the comparison is nonzero returns it,
5914  * otherwise the shorter one is ordered first.
5915  */
5916  int
5917  compare(size_type __pos1, size_type __n1, const basic_string& __str,
5918  size_type __pos2, size_type __n2 = npos) const;
5919 
5920  /**
5921  * @brief Compare to a C string.
5922  * @param __s C string to compare against.
5923  * @return Integer < 0, 0, or > 0.
5924  *
5925  * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5926  * their values are equivalent, or > 0 if this string is ordered after
5927  * @a __s. Determines the effective length rlen of the strings to
5928  * compare as the smallest of size() and the length of a string
5929  * constructed from @a __s. The function then compares the two strings
5930  * by calling traits::compare(data(),s,rlen). If the result of the
5931  * comparison is nonzero returns it, otherwise the shorter one is
5932  * ordered first.
5933  */
5934  int
5935  compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
5936 
5937  // _GLIBCXX_RESOLVE_LIB_DEFECTS
5938  // 5 String::compare specification questionable
5939  /**
5940  * @brief Compare substring to a C string.
5941  * @param __pos Index of first character of substring.
5942  * @param __n1 Number of characters in substring.
5943  * @param __s C string to compare against.
5944  * @return Integer < 0, 0, or > 0.
5945  *
5946  * Form the substring of this string from the @a __n1
5947  * characters starting at @a pos. Returns an integer < 0 if
5948  * the substring is ordered before @a __s, 0 if their values
5949  * are equivalent, or > 0 if the substring is ordered after @a
5950  * __s. Determines the effective length rlen of the strings to
5951  * compare as the smallest of the length of the substring and
5952  * the length of a string constructed from @a __s. The
5953  * function then compares the two string by calling
5954  * traits::compare(substring.data(),__s,rlen). If the result of
5955  * the comparison is nonzero returns it, otherwise the shorter
5956  * one is ordered first.
5957  */
5958  int
5959  compare(size_type __pos, size_type __n1, const _CharT* __s) const;
5960 
5961  /**
5962  * @brief Compare substring against a character %array.
5963  * @param __pos Index of first character of substring.
5964  * @param __n1 Number of characters in substring.
5965  * @param __s character %array to compare against.
5966  * @param __n2 Number of characters of s.
5967  * @return Integer < 0, 0, or > 0.
5968  *
5969  * Form the substring of this string from the @a __n1
5970  * characters starting at @a __pos. Form a string from the
5971  * first @a __n2 characters of @a __s. Returns an integer < 0
5972  * if this substring is ordered before the string from @a __s,
5973  * 0 if their values are equivalent, or > 0 if this substring
5974  * is ordered after the string from @a __s. Determines the
5975  * effective length rlen of the strings to compare as the
5976  * smallest of the length of the substring and @a __n2. The
5977  * function then compares the two strings by calling
5978  * traits::compare(substring.data(),s,rlen). If the result of
5979  * the comparison is nonzero returns it, otherwise the shorter
5980  * one is ordered first.
5981  *
5982  * NB: s must have at least n2 characters, &apos;\\0&apos; has
5983  * no special meaning.
5984  */
5985  int
5986  compare(size_type __pos, size_type __n1, const _CharT* __s,
5987  size_type __n2) const;
5988 
5989 #if __cplusplus > 201703L
5990  bool
5991  starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
5992  { return __sv_type(this->data(), this->size()).starts_with(__x); }
5993 
5994  bool
5995  starts_with(_CharT __x) const noexcept
5996  { return __sv_type(this->data(), this->size()).starts_with(__x); }
5997 
5998  bool
5999  starts_with(const _CharT* __x) const noexcept
6000  { return __sv_type(this->data(), this->size()).starts_with(__x); }
6001 
6002  bool
6003  ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
6004  { return __sv_type(this->data(), this->size()).ends_with(__x); }
6005 
6006  bool
6007  ends_with(_CharT __x) const noexcept
6008  { return __sv_type(this->data(), this->size()).ends_with(__x); }
6009 
6010  bool
6011  ends_with(const _CharT* __x) const noexcept
6012  { return __sv_type(this->data(), this->size()).ends_with(__x); }
6013 #endif // C++20
6014 
6015 #if __cplusplus >= 202011L \
6016  || (__cplusplus == 202002L && !defined __STRICT_ANSI__)
6017  bool
6018  contains(basic_string_view<_CharT, _Traits> __x) const noexcept
6019  { return __sv_type(this->data(), this->size()).contains(__x); }
6020 
6021  bool
6022  contains(_CharT __x) const noexcept
6023  { return __sv_type(this->data(), this->size()).contains(__x); }
6024 
6025  bool
6026  contains(const _CharT* __x) const noexcept
6027  { return __sv_type(this->data(), this->size()).contains(__x); }
6028 #endif // C++23
6029 
6030 # ifdef _GLIBCXX_TM_TS_INTERNAL
6031  friend void
6032  ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
6033  void* exc);
6034  friend const char*
6035  ::_txnal_cow_string_c_str(const void *that);
6036  friend void
6037  ::_txnal_cow_string_D1(void *that);
6038  friend void
6039  ::_txnal_cow_string_D1_commit(void *that);
6040 # endif
6041  };
6042 #endif // !_GLIBCXX_USE_CXX11_ABI
6043 
6044 #if __cpp_deduction_guides >= 201606
6045 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6046  template<typename _InputIterator, typename _CharT
6047  = typename iterator_traits<_InputIterator>::value_type,
6048  typename _Allocator = allocator<_CharT>,
6049  typename = _RequireInputIter<_InputIterator>,
6050  typename = _RequireAllocator<_Allocator>>
6051  basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
6052  -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
6053 
6054  // _GLIBCXX_RESOLVE_LIB_DEFECTS
6055  // 3075. basic_string needs deduction guides from basic_string_view
6056  template<typename _CharT, typename _Traits,
6057  typename _Allocator = allocator<_CharT>,
6058  typename = _RequireAllocator<_Allocator>>
6059  basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
6060  -> basic_string<_CharT, _Traits, _Allocator>;
6061 
6062  template<typename _CharT, typename _Traits,
6063  typename _Allocator = allocator<_CharT>,
6064  typename = _RequireAllocator<_Allocator>>
6065  basic_string(basic_string_view<_CharT, _Traits>,
6066  typename basic_string<_CharT, _Traits, _Allocator>::size_type,
6067  typename basic_string<_CharT, _Traits, _Allocator>::size_type,
6068  const _Allocator& = _Allocator())
6069  -> basic_string<_CharT, _Traits, _Allocator>;
6070 _GLIBCXX_END_NAMESPACE_CXX11
6071 #endif
6072 
6073  // operator+
6074  /**
6075  * @brief Concatenate two strings.
6076  * @param __lhs First string.
6077  * @param __rhs Last string.
6078  * @return New string with value of @a __lhs followed by @a __rhs.
6079  */
6080  template<typename _CharT, typename _Traits, typename _Alloc>
6081  basic_string<_CharT, _Traits, _Alloc>
6084  {
6086  __str.append(__rhs);
6087  return __str;
6088  }
6089 
6090  /**
6091  * @brief Concatenate C string and string.
6092  * @param __lhs First string.
6093  * @param __rhs Last string.
6094  * @return New string with value of @a __lhs followed by @a __rhs.
6095  */
6096  template<typename _CharT, typename _Traits, typename _Alloc>
6097  basic_string<_CharT,_Traits,_Alloc>
6098  operator+(const _CharT* __lhs,
6099  const basic_string<_CharT,_Traits,_Alloc>& __rhs);
6100 
6101  /**
6102  * @brief Concatenate character and string.
6103  * @param __lhs First string.
6104  * @param __rhs Last string.
6105  * @return New string with @a __lhs followed by @a __rhs.
6106  */
6107  template<typename _CharT, typename _Traits, typename _Alloc>
6108  basic_string<_CharT,_Traits,_Alloc>
6109  operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
6110 
6111  /**
6112  * @brief Concatenate string and C string.
6113  * @param __lhs First string.
6114  * @param __rhs Last string.
6115  * @return New string with @a __lhs followed by @a __rhs.
6116  */
6117  template<typename _CharT, typename _Traits, typename _Alloc>
6118  inline basic_string<_CharT, _Traits, _Alloc>
6120  const _CharT* __rhs)
6121  {
6123  __str.append(__rhs);
6124  return __str;
6125  }
6126 
6127  /**
6128  * @brief Concatenate string and character.
6129  * @param __lhs First string.
6130  * @param __rhs Last string.
6131  * @return New string with @a __lhs followed by @a __rhs.
6132  */
6133  template<typename _CharT, typename _Traits, typename _Alloc>
6134  inline basic_string<_CharT, _Traits, _Alloc>
6136  {
6137  typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
6138  typedef typename __string_type::size_type __size_type;
6139  __string_type __str(__lhs);
6140  __str.append(__size_type(1), __rhs);
6141  return __str;
6142  }
6143 
6144 #if __cplusplus >= 201103L
6145  template<typename _CharT, typename _Traits, typename _Alloc>
6146  inline basic_string<_CharT, _Traits, _Alloc>
6147  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6148  const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6149  { return std::move(__lhs.append(__rhs)); }
6150 
6151  template<typename _CharT, typename _Traits, typename _Alloc>
6152  inline basic_string<_CharT, _Traits, _Alloc>
6153  operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6154  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6155  { return std::move(__rhs.insert(0, __lhs)); }
6156 
6157  template<typename _CharT, typename _Traits, typename _Alloc>
6158  inline basic_string<_CharT, _Traits, _Alloc>
6159  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6160  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6161  {
6162 #if _GLIBCXX_USE_CXX11_ABI
6163  using _Alloc_traits = allocator_traits<_Alloc>;
6164  bool __use_rhs = false;
6165  if _GLIBCXX17_CONSTEXPR (typename _Alloc_traits::is_always_equal{})
6166  __use_rhs = true;
6167  else if (__lhs.get_allocator() == __rhs.get_allocator())
6168  __use_rhs = true;
6169  if (__use_rhs)
6170 #endif
6171  {
6172  const auto __size = __lhs.size() + __rhs.size();
6173  if (__size > __lhs.capacity() && __size <= __rhs.capacity())
6174  return std::move(__rhs.insert(0, __lhs));
6175  }
6176  return std::move(__lhs.append(__rhs));
6177  }
6178 
6179  template<typename _CharT, typename _Traits, typename _Alloc>
6180  inline basic_string<_CharT, _Traits, _Alloc>
6181  operator+(const _CharT* __lhs,
6182  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6183  { return std::move(__rhs.insert(0, __lhs)); }
6184 
6185  template<typename _CharT, typename _Traits, typename _Alloc>
6186  inline basic_string<_CharT, _Traits, _Alloc>
6187  operator+(_CharT __lhs,
6188  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6189  { return std::move(__rhs.insert(0, 1, __lhs)); }
6190 
6191  template<typename _CharT, typename _Traits, typename _Alloc>
6192  inline basic_string<_CharT, _Traits, _Alloc>
6193  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6194  const _CharT* __rhs)
6195  { return std::move(__lhs.append(__rhs)); }
6196 
6197  template<typename _CharT, typename _Traits, typename _Alloc>
6198  inline basic_string<_CharT, _Traits, _Alloc>
6199  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6200  _CharT __rhs)
6201  { return std::move(__lhs.append(1, __rhs)); }
6202 #endif
6203 
6204  // operator ==
6205  /**
6206  * @brief Test equivalence of two strings.
6207  * @param __lhs First string.
6208  * @param __rhs Second string.
6209  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6210  */
6211  template<typename _CharT, typename _Traits, typename _Alloc>
6212  inline bool
6213  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6215  _GLIBCXX_NOEXCEPT
6216  { return __lhs.compare(__rhs) == 0; }
6217 
6218  template<typename _CharT>
6219  inline
6220  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
6221  operator==(const basic_string<_CharT>& __lhs,
6222  const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT
6223  { return (__lhs.size() == __rhs.size()
6224  && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
6225  __lhs.size())); }
6226 
6227  /**
6228  * @brief Test equivalence of string and C string.
6229  * @param __lhs String.
6230  * @param __rhs C string.
6231  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6232  */
6233  template<typename _CharT, typename _Traits, typename _Alloc>
6234  inline bool
6235  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6236  const _CharT* __rhs)
6237  { return __lhs.compare(__rhs) == 0; }
6238 
6239 #if __cpp_lib_three_way_comparison
6240  /**
6241  * @brief Three-way comparison of a string and a C string.
6242  * @param __lhs A string.
6243  * @param __rhs A null-terminated string.
6244  * @return A value indicating whether `__lhs` is less than, equal to,
6245  * greater than, or incomparable with `__rhs`.
6246  */
6247  template<typename _CharT, typename _Traits, typename _Alloc>
6248  inline auto
6249  operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6250  const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept
6251  -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
6252  { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
6253 
6254  /**
6255  * @brief Three-way comparison of a string and a C string.
6256  * @param __lhs A string.
6257  * @param __rhs A null-terminated string.
6258  * @return A value indicating whether `__lhs` is less than, equal to,
6259  * greater than, or incomparable with `__rhs`.
6260  */
6261  template<typename _CharT, typename _Traits, typename _Alloc>
6262  inline auto
6263  operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6264  const _CharT* __rhs) noexcept
6265  -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
6266  { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
6267 #else
6268  /**
6269  * @brief Test equivalence of C string and string.
6270  * @param __lhs C string.
6271  * @param __rhs String.
6272  * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
6273  */
6274  template<typename _CharT, typename _Traits, typename _Alloc>
6275  inline bool
6276  operator==(const _CharT* __lhs,
6278  { return __rhs.compare(__lhs) == 0; }
6279 
6280  // operator !=
6281  /**
6282  * @brief Test difference of two strings.
6283  * @param __lhs First string.
6284  * @param __rhs Second string.
6285  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6286  */
6287  template<typename _CharT, typename _Traits, typename _Alloc>
6288  inline bool
6289  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6291  _GLIBCXX_NOEXCEPT
6292  { return !(__lhs == __rhs); }
6293 
6294  /**
6295  * @brief Test difference of C string and string.
6296  * @param __lhs C string.
6297  * @param __rhs String.
6298  * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
6299  */
6300  template<typename _CharT, typename _Traits, typename _Alloc>
6301  inline bool
6302  operator!=(const _CharT* __lhs,
6304  { return !(__lhs == __rhs); }
6305 
6306  /**
6307  * @brief Test difference of string and C string.
6308  * @param __lhs String.
6309  * @param __rhs C string.
6310  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6311  */
6312  template<typename _CharT, typename _Traits, typename _Alloc>
6313  inline bool
6314  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6315  const _CharT* __rhs)
6316  { return !(__lhs == __rhs); }
6317 
6318  // operator <
6319  /**
6320  * @brief Test if string precedes string.
6321  * @param __lhs First string.
6322  * @param __rhs Second string.
6323  * @return True if @a __lhs precedes @a __rhs. False otherwise.
6324  */
6325  template<typename _CharT, typename _Traits, typename _Alloc>
6326  inline bool
6327  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6329  _GLIBCXX_NOEXCEPT
6330  { return __lhs.compare(__rhs) < 0; }
6331 
6332  /**
6333  * @brief Test if string precedes C string.
6334  * @param __lhs String.
6335  * @param __rhs C string.
6336  * @return True if @a __lhs precedes @a __rhs. False otherwise.
6337  */
6338  template<typename _CharT, typename _Traits, typename _Alloc>
6339  inline bool
6340  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6341  const _CharT* __rhs)
6342  { return __lhs.compare(__rhs) < 0; }
6343 
6344  /**
6345  * @brief Test if C string precedes string.
6346  * @param __lhs C string.
6347  * @param __rhs String.
6348  * @return True if @a __lhs precedes @a __rhs. False otherwise.
6349  */
6350  template<typename _CharT, typename _Traits, typename _Alloc>
6351  inline bool
6352  operator<(const _CharT* __lhs,
6354  { return __rhs.compare(__lhs) > 0; }
6355 
6356  // operator >
6357  /**
6358  * @brief Test if string follows string.
6359  * @param __lhs First string.
6360  * @param __rhs Second string.
6361  * @return True if @a __lhs follows @a __rhs. False otherwise.
6362  */
6363  template<typename _CharT, typename _Traits, typename _Alloc>
6364  inline bool
6367  _GLIBCXX_NOEXCEPT
6368  { return __lhs.compare(__rhs) > 0; }
6369 
6370  /**
6371  * @brief Test if string follows C string.
6372  * @param __lhs String.
6373  * @param __rhs C string.
6374  * @return True if @a __lhs follows @a __rhs. False otherwise.
6375  */
6376  template<typename _CharT, typename _Traits, typename _Alloc>
6377  inline bool
6379  const _CharT* __rhs)
6380  { return __lhs.compare(__rhs) > 0; }
6381 
6382  /**
6383  * @brief Test if C string follows string.
6384  * @param __lhs C string.
6385  * @param __rhs String.
6386  * @return True if @a __lhs follows @a __rhs. False otherwise.
6387  */
6388  template<typename _CharT, typename _Traits, typename _Alloc>
6389  inline bool
6390  operator>(const _CharT* __lhs,
6392  { return __rhs.compare(__lhs) < 0; }
6393 
6394  // operator <=
6395  /**
6396  * @brief Test if string doesn't follow string.
6397  * @param __lhs First string.
6398  * @param __rhs Second string.
6399  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6400  */
6401  template<typename _CharT, typename _Traits, typename _Alloc>
6402  inline bool
6403  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6405  _GLIBCXX_NOEXCEPT
6406  { return __lhs.compare(__rhs) <= 0; }
6407 
6408  /**
6409  * @brief Test if string doesn't follow C string.
6410  * @param __lhs String.
6411  * @param __rhs C string.
6412  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6413  */
6414  template<typename _CharT, typename _Traits, typename _Alloc>
6415  inline bool
6416  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6417  const _CharT* __rhs)
6418  { return __lhs.compare(__rhs) <= 0; }
6419 
6420  /**
6421  * @brief Test if C string doesn't follow string.
6422  * @param __lhs C string.
6423  * @param __rhs String.
6424  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6425  */
6426  template<typename _CharT, typename _Traits, typename _Alloc>
6427  inline bool
6428  operator<=(const _CharT* __lhs,
6430  { return __rhs.compare(__lhs) >= 0; }
6431 
6432  // operator >=
6433  /**
6434  * @brief Test if string doesn't precede string.
6435  * @param __lhs First string.
6436  * @param __rhs Second string.
6437  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6438  */
6439  template<typename _CharT, typename _Traits, typename _Alloc>
6440  inline bool
6441  operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6443  _GLIBCXX_NOEXCEPT
6444  { return __lhs.compare(__rhs) >= 0; }
6445 
6446  /**
6447  * @brief Test if string doesn't precede C string.
6448  * @param __lhs String.
6449  * @param __rhs C string.
6450  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6451  */
6452  template<typename _CharT, typename _Traits, typename _Alloc>
6453  inline bool
6454  operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6455  const _CharT* __rhs)
6456  { return __lhs.compare(__rhs) >= 0; }
6457 
6458  /**
6459  * @brief Test if C string doesn't precede string.
6460  * @param __lhs C string.
6461  * @param __rhs String.
6462  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6463  */
6464  template<typename _CharT, typename _Traits, typename _Alloc>
6465  inline bool
6466  operator>=(const _CharT* __lhs,
6468  { return __rhs.compare(__lhs) <= 0; }
6469 #endif // three-way comparison
6470 
6471  /**
6472  * @brief Swap contents of two strings.
6473  * @param __lhs First string.
6474  * @param __rhs Second string.
6475  *
6476  * Exchanges the contents of @a __lhs and @a __rhs in constant time.
6477  */
6478  template<typename _CharT, typename _Traits, typename _Alloc>
6479  inline void
6482  _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
6483  { __lhs.swap(__rhs); }
6484 
6485 
6486  /**
6487  * @brief Read stream into a string.
6488  * @param __is Input stream.
6489  * @param __str Buffer to store into.
6490  * @return Reference to the input stream.
6491  *
6492  * Stores characters from @a __is into @a __str until whitespace is
6493  * found, the end of the stream is encountered, or str.max_size()
6494  * is reached. If is.width() is non-zero, that is the limit on the
6495  * number of characters stored into @a __str. Any previous
6496  * contents of @a __str are erased.
6497  */
6498  template<typename _CharT, typename _Traits, typename _Alloc>
6499  basic_istream<_CharT, _Traits>&
6500  operator>>(basic_istream<_CharT, _Traits>& __is,
6501  basic_string<_CharT, _Traits, _Alloc>& __str);
6502 
6503  template<>
6504  basic_istream<char>&
6506 
6507  /**
6508  * @brief Write string to a stream.
6509  * @param __os Output stream.
6510  * @param __str String to write out.
6511  * @return Reference to the output stream.
6512  *
6513  * Output characters of @a __str into os following the same rules as for
6514  * writing a C string.
6515  */
6516  template<typename _CharT, typename _Traits, typename _Alloc>
6518  operator<<(basic_ostream<_CharT, _Traits>& __os,
6520  {
6521  // _GLIBCXX_RESOLVE_LIB_DEFECTS
6522  // 586. string inserter not a formatted function
6523  return __ostream_insert(__os, __str.data(), __str.size());
6524  }
6525 
6526  /**
6527  * @brief Read a line from stream into a string.
6528  * @param __is Input stream.
6529  * @param __str Buffer to store into.
6530  * @param __delim Character marking end of line.
6531  * @return Reference to the input stream.
6532  *
6533  * Stores characters from @a __is into @a __str until @a __delim is
6534  * found, the end of the stream is encountered, or str.max_size()
6535  * is reached. Any previous contents of @a __str are erased. If
6536  * @a __delim is encountered, it is extracted but not stored into
6537  * @a __str.
6538  */
6539  template<typename _CharT, typename _Traits, typename _Alloc>
6540  basic_istream<_CharT, _Traits>&
6541  getline(basic_istream<_CharT, _Traits>& __is,
6542  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
6543 
6544  /**
6545  * @brief Read a line from stream into a string.
6546  * @param __is Input stream.
6547  * @param __str Buffer to store into.
6548  * @return Reference to the input stream.
6549  *
6550  * Stores characters from is into @a __str until &apos;\n&apos; is
6551  * found, the end of the stream is encountered, or str.max_size()
6552  * is reached. Any previous contents of @a __str are erased. If
6553  * end of line is encountered, it is extracted but not stored into
6554  * @a __str.
6555  */
6556  template<typename _CharT, typename _Traits, typename _Alloc>
6557  inline basic_istream<_CharT, _Traits>&
6560  { return std::getline(__is, __str, __is.widen('\n')); }
6561 
6562 #if __cplusplus >= 201103L
6563  /// Read a line from an rvalue stream into a string.
6564  template<typename _CharT, typename _Traits, typename _Alloc>
6565  inline basic_istream<_CharT, _Traits>&
6567  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
6568  { return std::getline(__is, __str, __delim); }
6569 
6570  /// Read a line from an rvalue stream into a string.
6571  template<typename _CharT, typename _Traits, typename _Alloc>
6572  inline basic_istream<_CharT, _Traits>&
6575  { return std::getline(__is, __str); }
6576 #endif
6577 
6578  template<>
6579  basic_istream<char>&
6580  getline(basic_istream<char>& __in, basic_string<char>& __str,
6581  char __delim);
6582 
6583 #ifdef _GLIBCXX_USE_WCHAR_T
6584  template<>
6585  basic_istream<wchar_t>&
6586  getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
6587  wchar_t __delim);
6588 #endif
6589 
6590 _GLIBCXX_END_NAMESPACE_VERSION
6591 } // namespace
6592 
6593 #if __cplusplus >= 201103L
6594 
6595 #include <ext/string_conversions.h>
6596 #include <bits/charconv.h>
6597 
6598 namespace std _GLIBCXX_VISIBILITY(default)
6599 {
6600 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6601 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6602 
6603 #if _GLIBCXX_USE_C99_STDLIB
6604  // 21.4 Numeric Conversions [string.conversions].
6605  inline int
6606  stoi(const string& __str, size_t* __idx = 0, int __base = 10)
6607  { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
6608  __idx, __base); }
6609 
6610  inline long
6611  stol(const string& __str, size_t* __idx = 0, int __base = 10)
6612  { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
6613  __idx, __base); }
6614 
6615  inline unsigned long
6616  stoul(const string& __str, size_t* __idx = 0, int __base = 10)
6617  { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
6618  __idx, __base); }
6619 
6620  inline long long
6621  stoll(const string& __str, size_t* __idx = 0, int __base = 10)
6622  { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
6623  __idx, __base); }
6624 
6625  inline unsigned long long
6626  stoull(const string& __str, size_t* __idx = 0, int __base = 10)
6627  { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
6628  __idx, __base); }
6629 
6630  // NB: strtof vs strtod.
6631  inline float
6632  stof(const string& __str, size_t* __idx = 0)
6633  { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
6634 
6635  inline double
6636  stod(const string& __str, size_t* __idx = 0)
6637  { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
6638 
6639  inline long double
6640  stold(const string& __str, size_t* __idx = 0)
6641  { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
6642 #endif // _GLIBCXX_USE_C99_STDLIB
6643 
6644  // DR 1261. Insufficent overloads for to_string / to_wstring
6645 
6646  inline string
6647  to_string(int __val)
6648  {
6649  const bool __neg = __val < 0;
6650  const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
6651  const auto __len = __detail::__to_chars_len(__uval);
6652  string __str(__neg + __len, '-');
6653  __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
6654  return __str;
6655  }
6656 
6657  inline string
6658  to_string(unsigned __val)
6659  {
6660  string __str(__detail::__to_chars_len(__val), '\0');
6661  __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
6662  return __str;
6663  }
6664 
6665  inline string
6666  to_string(long __val)
6667  {
6668  const bool __neg = __val < 0;
6669  const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
6670  const auto __len = __detail::__to_chars_len(__uval);
6671  string __str(__neg + __len, '-');
6672  __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
6673  return __str;
6674  }
6675 
6676  inline string
6677  to_string(unsigned long __val)
6678  {
6679  string __str(__detail::__to_chars_len(__val), '\0');
6680  __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
6681  return __str;
6682  }
6683 
6684  inline string
6685  to_string(long long __val)
6686  {
6687  const bool __neg = __val < 0;
6688  const unsigned long long __uval
6689  = __neg ? (unsigned long long)~__val + 1ull : __val;
6690  const auto __len = __detail::__to_chars_len(__uval);
6691  string __str(__neg + __len, '-');
6692  __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
6693  return __str;
6694  }
6695 
6696  inline string
6697  to_string(unsigned long long __val)
6698  {
6699  string __str(__detail::__to_chars_len(__val), '\0');
6700  __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
6701  return __str;
6702  }
6703 
6704 #if _GLIBCXX_USE_C99_STDIO
6705  // NB: (v)snprintf vs sprintf.
6706 
6707  inline string
6708  to_string(float __val)
6709  {
6710  const int __n =
6711  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6712  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6713  "%f", __val);
6714  }
6715 
6716  inline string
6717  to_string(double __val)
6718  {
6719  const int __n =
6720  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6721  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6722  "%f", __val);
6723  }
6724 
6725  inline string
6726  to_string(long double __val)
6727  {
6728  const int __n =
6729  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6730  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6731  "%Lf", __val);
6732  }
6733 #endif // _GLIBCXX_USE_C99_STDIO
6734 
6735 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
6736  inline int
6737  stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
6738  { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
6739  __idx, __base); }
6740 
6741  inline long
6742  stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
6743  { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
6744  __idx, __base); }
6745 
6746  inline unsigned long
6747  stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
6748  { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
6749  __idx, __base); }
6750 
6751  inline long long
6752  stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
6753  { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
6754  __idx, __base); }
6755 
6756  inline unsigned long long
6757  stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
6758  { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
6759  __idx, __base); }
6760 
6761  // NB: wcstof vs wcstod.
6762  inline float
6763  stof(const wstring& __str, size_t* __idx = 0)
6764  { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
6765 
6766  inline double
6767  stod(const wstring& __str, size_t* __idx = 0)
6768  { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
6769 
6770  inline long double
6771  stold(const wstring& __str, size_t* __idx = 0)
6772  { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
6773 
6774 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6775  // DR 1261.
6776  inline wstring
6777  to_wstring(int __val)
6778  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
6779  L"%d", __val); }
6780 
6781  inline wstring
6782  to_wstring(unsigned __val)
6783  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6784  4 * sizeof(unsigned),
6785  L"%u", __val); }
6786 
6787  inline wstring
6788  to_wstring(long __val)
6789  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
6790  L"%ld", __val); }
6791 
6792  inline wstring
6793  to_wstring(unsigned long __val)
6794  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6795  4 * sizeof(unsigned long),
6796  L"%lu", __val); }
6797 
6798  inline wstring
6799  to_wstring(long long __val)
6800  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6801  4 * sizeof(long long),
6802  L"%lld", __val); }
6803 
6804  inline wstring
6805  to_wstring(unsigned long long __val)
6806  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6807  4 * sizeof(unsigned long long),
6808  L"%llu", __val); }
6809 
6810  inline wstring
6811  to_wstring(float __val)
6812  {
6813  const int __n =
6814  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6815  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6816  L"%f", __val);
6817  }
6818 
6819  inline wstring
6820  to_wstring(double __val)
6821  {
6822  const int __n =
6823  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6824  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6825  L"%f", __val);
6826  }
6827 
6828  inline wstring
6829  to_wstring(long double __val)
6830  {
6831  const int __n =
6832  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6833  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6834  L"%Lf", __val);
6835  }
6836 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6837 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6838 
6839 _GLIBCXX_END_NAMESPACE_CXX11
6840 _GLIBCXX_END_NAMESPACE_VERSION
6841 } // namespace
6842 
6843 #endif /* C++11 */
6844 
6845 #if __cplusplus >= 201103L
6846 
6847 #include <bits/functional_hash.h>
6848 
6849 namespace std _GLIBCXX_VISIBILITY(default)
6850 {
6851 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6852 
6853  // DR 1182.
6854 
6855 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6856  /// std::hash specialization for string.
6857  template<>
6858  struct hash<string>
6859  : public __hash_base<size_t, string>
6860  {
6861  size_t
6862  operator()(const string& __s) const noexcept
6863  { return std::_Hash_impl::hash(__s.data(), __s.length()); }
6864  };
6865 
6866  template<>
6867  struct __is_fast_hash<hash<string>> : std::false_type
6868  { };
6869 
6870 #ifdef _GLIBCXX_USE_WCHAR_T
6871  /// std::hash specialization for wstring.
6872  template<>
6873  struct hash<wstring>
6874  : public __hash_base<size_t, wstring>
6875  {
6876  size_t
6877  operator()(const wstring& __s) const noexcept
6878  { return std::_Hash_impl::hash(__s.data(),
6879  __s.length() * sizeof(wchar_t)); }
6880  };
6881 
6882  template<>
6883  struct __is_fast_hash<hash<wstring>> : std::false_type
6884  { };
6885 #endif
6886 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6887 
6888 #ifdef _GLIBCXX_USE_CHAR8_T
6889  /// std::hash specialization for u8string.
6890  template<>
6891  struct hash<u8string>
6892  : public __hash_base<size_t, u8string>
6893  {
6894  size_t
6895  operator()(const u8string& __s) const noexcept
6896  { return std::_Hash_impl::hash(__s.data(),
6897  __s.length() * sizeof(char8_t)); }
6898  };
6899 
6900  template<>
6901  struct __is_fast_hash<hash<u8string>> : std::false_type
6902  { };
6903 #endif
6904 
6905  /// std::hash specialization for u16string.
6906  template<>
6907  struct hash<u16string>
6908  : public __hash_base<size_t, u16string>
6909  {
6910  size_t
6911  operator()(const u16string& __s) const noexcept
6912  { return std::_Hash_impl::hash(__s.data(),
6913  __s.length() * sizeof(char16_t)); }
6914  };
6915 
6916  template<>
6917  struct __is_fast_hash<hash<u16string>> : std::false_type
6918  { };
6919 
6920  /// std::hash specialization for u32string.
6921  template<>
6922  struct hash<u32string>
6923  : public __hash_base<size_t, u32string>
6924  {
6925  size_t
6926  operator()(const u32string& __s) const noexcept
6927  { return std::_Hash_impl::hash(__s.data(),
6928  __s.length() * sizeof(char32_t)); }
6929  };
6930 
6931  template<>
6932  struct __is_fast_hash<hash<u32string>> : std::false_type
6933  { };
6934 
6935 #if __cplusplus >= 201402L
6936 
6937 #define __cpp_lib_string_udls 201304
6938 
6939  inline namespace literals
6940  {
6941  inline namespace string_literals
6942  {
6943 #pragma GCC diagnostic push
6944 #pragma GCC diagnostic ignored "-Wliteral-suffix"
6945  _GLIBCXX_DEFAULT_ABI_TAG
6946  inline basic_string<char>
6947  operator""s(const char* __str, size_t __len)
6948  { return basic_string<char>{__str, __len}; }
6949 
6950 #ifdef _GLIBCXX_USE_WCHAR_T
6951  _GLIBCXX_DEFAULT_ABI_TAG
6952  inline basic_string<wchar_t>
6953  operator""s(const wchar_t* __str, size_t __len)
6954  { return basic_string<wchar_t>{__str, __len}; }
6955 #endif
6956 
6957 #ifdef _GLIBCXX_USE_CHAR8_T
6958  _GLIBCXX_DEFAULT_ABI_TAG
6959  inline basic_string<char8_t>
6960  operator""s(const char8_t* __str, size_t __len)
6961  { return basic_string<char8_t>{__str, __len}; }
6962 #endif
6963 
6964  _GLIBCXX_DEFAULT_ABI_TAG
6965  inline basic_string<char16_t>
6966  operator""s(const char16_t* __str, size_t __len)
6967  { return basic_string<char16_t>{__str, __len}; }
6968 
6969  _GLIBCXX_DEFAULT_ABI_TAG
6970  inline basic_string<char32_t>
6971  operator""s(const char32_t* __str, size_t __len)
6972  { return basic_string<char32_t>{__str, __len}; }
6973 
6974 #pragma GCC diagnostic pop
6975  } // inline namespace string_literals
6976  } // inline namespace literals
6977 
6978 #if __cplusplus >= 201703L
6979  namespace __detail::__variant
6980  {
6981  template<typename> struct _Never_valueless_alt; // see <variant>
6982 
6983  // Provide the strong exception-safety guarantee when emplacing a
6984  // basic_string into a variant, but only if moving the string cannot throw.
6985  template<typename _Tp, typename _Traits, typename _Alloc>
6986  struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
6987  : __and_<
6988  is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
6989  is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
6990  >::type
6991  { };
6992  } // namespace __detail::__variant
6993 #endif // C++17
6994 #endif // C++14
6995 
6996 _GLIBCXX_END_NAMESPACE_VERSION
6997 } // namespace std
6998 
6999 #endif // C++11
7000 
7001 #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:332
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition: type_traits:2514
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:49
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:101
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
Definition: any:412
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
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:1360
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.
_If_sv< _Tp, basic_string & > append(const _Tp &__svt)
Append a string_view.
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(_InputIterator __beg, _InputIterator __end, const _Alloc &__a=_Alloc())
Construct string as copy of a range.
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.
_If_sv< _Tp, size_type > find_last_of(const _Tp &__svt, size_type __pos=npos) const noexcept(is_same< _Tp, __sv_type >::value)
Find last position of a character of 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.
_If_sv< _Tp, size_type > find(const _Tp &__svt, size_type __pos=0) const noexcept(is_same< _Tp, __sv_type >::value)
Find position of a string_view.
basic_string(const _Alloc &__a)
Construct an empty string using allocator a.
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.
_If_sv< _Tp, basic_string & > append(const _Tp &__svt, size_type __pos, size_type __n=npos)
Append a range of characters from a string_view.
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.
_If_sv< _Tp, basic_string & > operator=(const _Tp &__svt)
Set value to string constructed from a string_view.
basic_string(const basic_string &__str)
Construct string with copy of value of str.
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()
_If_sv< _Tp, basic_string & > replace(const_iterator __i1, const_iterator __i2, const _Tp &__svt)
Replace range of characters with string_view.
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.
_If_sv< _Tp, basic_string & > insert(size_type __pos, const _Tp &__svt)
Insert a string_view.
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.
basic_string(const _Tp &__t, size_type __pos, size_type __n, const _Alloc &__a=_Alloc())
Construct string from a substring of a string_view.
reverse_iterator rbegin()
basic_string & operator+=(initializer_list< _CharT > __l)
Append an initializer_list of characters.
_If_sv< _Tp, basic_string & > operator+=(const _Tp &__svt)
Append a string_view.
basic_string(initializer_list< _CharT > __l, const _Alloc &__a=_Alloc())
Construct string from an initializer list.
basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
reference front()
basic_string(const _Tp &__t, const _Alloc &__a=_Alloc())
Construct string from a string_view.
_If_sv< _Tp, basic_string & > replace(size_type __pos, size_type __n, const _Tp &__svt)
Replace range of characters with string_view.
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.
_If_sv< _Tp, basic_string & > assign(const _Tp &__svt)
Set value from a string_view.
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.
_If_sv< _Tp, int > compare(const _Tp &__svt) const noexcept(is_same< _Tp, __sv_type >::value)
Compare to a string_view.
_If_sv< _Tp, size_type > rfind(const _Tp &__svt, size_type __pos=npos) const noexcept(is_same< _Tp, __sv_type >::value)
Find last position of a string_view.
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.
basic_string(size_type __n, _CharT __c, const _Alloc &__a=_Alloc())
Construct string as multiple characters.
_If_sv< _Tp, basic_string & > assign(const _Tp &__svt, size_type __pos, size_type __n=npos)
Set value from a range of characters in a string_view.
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.
void reserve()
Equivalent to shrink_to_fit().
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
_If_sv< _Tp, basic_string & > insert(size_type __pos1, const _Tp &__svt, size_type __pos2, size_type __n=npos)
Insert a string_view.
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.
_If_sv< _Tp, size_type > find_first_not_of(const _Tp &__svt, size_type __pos=0) const noexcept(is_same< _Tp, __sv_type >::value)
Find position of a character not in a string_view.
basic_string & append(const basic_string &__str)
Append a string to this string.
_CharT * data() noexcept
Return non-const pointer to contents.
const_iterator begin() const noexcept
_If_sv< _Tp, int > compare(size_type __pos1, size_type __n1, const _Tp &__svt, size_type __pos2, size_type __n2=npos) const noexcept(is_same< _Tp, __sv_type >::value)
Compare to a string_view.
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
_If_sv< _Tp, basic_string & > replace(size_type __pos1, size_type __n1, const _Tp &__svt, size_type __pos2, size_type __n2=npos)
Replace range of characters with string_view.
_If_sv< _Tp, int > compare(size_type __pos, size_type __n, const _Tp &__svt) const noexcept(is_same< _Tp, __sv_type >::value)
Compare to a string_view.
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.
_If_sv< _Tp, size_type > find_last_not_of(const _Tp &__svt, size_type __pos=npos) const noexcept(is_same< _Tp, __sv_type >::value)
Find last position of a character not in a string_view.
basic_string(const _CharT *__s, size_type __n, const _Alloc &__a=_Alloc())
Construct string initialized by a character array.
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.
_If_sv< _Tp, size_type > find_first_of(const _Tp &__svt, size_type __pos=0) const noexcept(is_same< _Tp, __sv_type >::value)
Find position of a character of a string_view.
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.