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 __pos1 Position in string to insert at.
4711  * @param __svt The object convertible to string_view to insert from.
4712  * @param __pos2 Position in string_view to insert from.
4713  * @param __n The number of characters to insert.
4714  * @return Reference to this string.
4715  */
4716  template<typename _Tp>
4717  _If_sv<_Tp, basic_string&>
4718  insert(size_type __pos1, const _Tp& __svt,
4719  size_type __pos2, size_type __n = npos)
4720  {
4721  __sv_type __sv = __svt;
4722  return this->replace(__pos1, size_type(0), __sv.data()
4723  + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
4724  std::__sv_limit(__sv.size(), __pos2, __n));
4725  }
4726 #endif // C++17
4727 
4728  /**
4729  * @brief Remove characters.
4730  * @param __pos Index of first character to remove (default 0).
4731  * @param __n Number of characters to remove (default remainder).
4732  * @return Reference to this string.
4733  * @throw std::out_of_range If @a pos is beyond the end of this
4734  * string.
4735  *
4736  * Removes @a __n characters from this string starting at @a
4737  * __pos. The length of the string is reduced by @a __n. If
4738  * there are < @a __n characters to remove, the remainder of
4739  * the string is truncated. If @a __p is beyond end of string,
4740  * out_of_range is thrown. The value of the string doesn't
4741  * change if an error is thrown.
4742  */
4743  basic_string&
4744  erase(size_type __pos = 0, size_type __n = npos)
4745  {
4746  _M_mutate(_M_check(__pos, "basic_string::erase"),
4747  _M_limit(__pos, __n), size_type(0));
4748  return *this;
4749  }
4750 
4751  /**
4752  * @brief Remove one character.
4753  * @param __position Iterator referencing the character to remove.
4754  * @return iterator referencing same location after removal.
4755  *
4756  * Removes the character at @a __position from this string. The value
4757  * of the string doesn't change if an error is thrown.
4758  */
4759  iterator
4760  erase(iterator __position)
4761  {
4762  _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
4763  && __position < _M_iend());
4764  const size_type __pos = __position - _M_ibegin();
4765  _M_mutate(__pos, size_type(1), size_type(0));
4766  _M_rep()->_M_set_leaked();
4767  return iterator(_M_data() + __pos);
4768  }
4769 
4770  /**
4771  * @brief Remove a range of characters.
4772  * @param __first Iterator referencing the first character to remove.
4773  * @param __last Iterator referencing the end of the range.
4774  * @return Iterator referencing location of first after removal.
4775  *
4776  * Removes the characters in the range [first,last) from this string.
4777  * The value of the string doesn't change if an error is thrown.
4778  */
4779  iterator
4780  erase(iterator __first, iterator __last);
4781 
4782 #if __cplusplus >= 201103L
4783  /**
4784  * @brief Remove the last character.
4785  *
4786  * The string must be non-empty.
4787  */
4788  void
4789  pop_back() // FIXME C++11: should be noexcept.
4790  {
4791  __glibcxx_assert(!empty());
4792  erase(size() - 1, 1);
4793  }
4794 #endif // C++11
4795 
4796  /**
4797  * @brief Replace characters with value from another string.
4798  * @param __pos Index of first character to replace.
4799  * @param __n Number of characters to be replaced.
4800  * @param __str String to insert.
4801  * @return Reference to this string.
4802  * @throw std::out_of_range If @a pos is beyond the end of this
4803  * string.
4804  * @throw std::length_error If new length exceeds @c max_size().
4805  *
4806  * Removes the characters in the range [__pos,__pos+__n) from
4807  * this string. In place, the value of @a __str is inserted.
4808  * If @a __pos is beyond end of string, out_of_range is thrown.
4809  * If the length of the result exceeds max_size(), length_error
4810  * is thrown. The value of the string doesn't change if an
4811  * error is thrown.
4812  */
4813  basic_string&
4814  replace(size_type __pos, size_type __n, const basic_string& __str)
4815  { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
4816 
4817  /**
4818  * @brief Replace characters with value from another string.
4819  * @param __pos1 Index of first character to replace.
4820  * @param __n1 Number of characters to be replaced.
4821  * @param __str String to insert.
4822  * @param __pos2 Index of first character of str to use.
4823  * @param __n2 Number of characters from str to use.
4824  * @return Reference to this string.
4825  * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4826  * __str.size().
4827  * @throw std::length_error If new length exceeds @c max_size().
4828  *
4829  * Removes the characters in the range [__pos1,__pos1 + n) from this
4830  * string. In place, the value of @a __str is inserted. If @a __pos is
4831  * beyond end of string, out_of_range is thrown. If the length of the
4832  * result exceeds max_size(), length_error is thrown. The value of the
4833  * string doesn't change if an error is thrown.
4834  */
4835  basic_string&
4836  replace(size_type __pos1, size_type __n1, const basic_string& __str,
4837  size_type __pos2, size_type __n2 = npos)
4838  { return this->replace(__pos1, __n1, __str._M_data()
4839  + __str._M_check(__pos2, "basic_string::replace"),
4840  __str._M_limit(__pos2, __n2)); }
4841 
4842  /**
4843  * @brief Replace characters with value of a C substring.
4844  * @param __pos Index of first character to replace.
4845  * @param __n1 Number of characters to be replaced.
4846  * @param __s C string to insert.
4847  * @param __n2 Number of characters from @a s to use.
4848  * @return Reference to this string.
4849  * @throw std::out_of_range If @a pos1 > size().
4850  * @throw std::length_error If new length exceeds @c max_size().
4851  *
4852  * Removes the characters in the range [__pos,__pos + __n1)
4853  * from this string. In place, the first @a __n2 characters of
4854  * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4855  * @a __pos is beyond end of string, out_of_range is thrown. If
4856  * the length of result exceeds max_size(), length_error is
4857  * thrown. The value of the string doesn't change if an error
4858  * is thrown.
4859  */
4860  basic_string&
4861  replace(size_type __pos, size_type __n1, const _CharT* __s,
4862  size_type __n2);
4863 
4864  /**
4865  * @brief Replace characters with value of a C string.
4866  * @param __pos Index of first character to replace.
4867  * @param __n1 Number of characters to be replaced.
4868  * @param __s C string to insert.
4869  * @return Reference to this string.
4870  * @throw std::out_of_range If @a pos > size().
4871  * @throw std::length_error If new length exceeds @c max_size().
4872  *
4873  * Removes the characters in the range [__pos,__pos + __n1)
4874  * from this string. In place, the characters of @a __s are
4875  * inserted. If @a __pos is beyond end of string, out_of_range
4876  * is thrown. If the length of result exceeds max_size(),
4877  * length_error is thrown. The value of the string doesn't
4878  * change if an error is thrown.
4879  */
4880  basic_string&
4881  replace(size_type __pos, size_type __n1, const _CharT* __s)
4882  {
4883  __glibcxx_requires_string(__s);
4884  return this->replace(__pos, __n1, __s, traits_type::length(__s));
4885  }
4886 
4887  /**
4888  * @brief Replace characters with multiple characters.
4889  * @param __pos Index of first character to replace.
4890  * @param __n1 Number of characters to be replaced.
4891  * @param __n2 Number of characters to insert.
4892  * @param __c Character to insert.
4893  * @return Reference to this string.
4894  * @throw std::out_of_range If @a __pos > size().
4895  * @throw std::length_error If new length exceeds @c max_size().
4896  *
4897  * Removes the characters in the range [pos,pos + n1) from this
4898  * string. In place, @a __n2 copies of @a __c are inserted.
4899  * If @a __pos is beyond end of string, out_of_range is thrown.
4900  * If the length of result exceeds max_size(), length_error is
4901  * thrown. The value of the string doesn't change if an error
4902  * is thrown.
4903  */
4904  basic_string&
4905  replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
4906  { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
4907  _M_limit(__pos, __n1), __n2, __c); }
4908 
4909  /**
4910  * @brief Replace range of characters with string.
4911  * @param __i1 Iterator referencing start of range to replace.
4912  * @param __i2 Iterator referencing end of range to replace.
4913  * @param __str String value to insert.
4914  * @return Reference to this string.
4915  * @throw std::length_error If new length exceeds @c max_size().
4916  *
4917  * Removes the characters in the range [__i1,__i2). In place,
4918  * the value of @a __str is inserted. If the length of result
4919  * exceeds max_size(), length_error is thrown. The value of
4920  * the string doesn't change if an error is thrown.
4921  */
4922  basic_string&
4923  replace(iterator __i1, iterator __i2, const basic_string& __str)
4924  { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
4925 
4926  /**
4927  * @brief Replace range of characters with C substring.
4928  * @param __i1 Iterator referencing start of range to replace.
4929  * @param __i2 Iterator referencing end of range to replace.
4930  * @param __s C string value to insert.
4931  * @param __n Number of characters from s to insert.
4932  * @return Reference to this string.
4933  * @throw std::length_error If new length exceeds @c max_size().
4934  *
4935  * Removes the characters in the range [__i1,__i2). In place,
4936  * the first @a __n characters of @a __s are inserted. If the
4937  * length of result exceeds max_size(), length_error is thrown.
4938  * The value of the string doesn't change if an error is
4939  * thrown.
4940  */
4941  basic_string&
4942  replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
4943  {
4944  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4945  && __i2 <= _M_iend());
4946  return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
4947  }
4948 
4949  /**
4950  * @brief Replace range of characters with C string.
4951  * @param __i1 Iterator referencing start of range to replace.
4952  * @param __i2 Iterator referencing end of range to replace.
4953  * @param __s C string value to insert.
4954  * @return Reference to this string.
4955  * @throw std::length_error If new length exceeds @c max_size().
4956  *
4957  * Removes the characters in the range [__i1,__i2). In place,
4958  * the characters of @a __s are inserted. If the length of
4959  * result exceeds max_size(), length_error is thrown. The
4960  * value of the string doesn't change if an error is thrown.
4961  */
4962  basic_string&
4963  replace(iterator __i1, iterator __i2, const _CharT* __s)
4964  {
4965  __glibcxx_requires_string(__s);
4966  return this->replace(__i1, __i2, __s, traits_type::length(__s));
4967  }
4968 
4969  /**
4970  * @brief Replace range of characters with multiple characters
4971  * @param __i1 Iterator referencing start of range to replace.
4972  * @param __i2 Iterator referencing end of range to replace.
4973  * @param __n Number of characters to insert.
4974  * @param __c Character to insert.
4975  * @return Reference to this string.
4976  * @throw std::length_error If new length exceeds @c max_size().
4977  *
4978  * Removes the characters in the range [__i1,__i2). In place,
4979  * @a __n copies of @a __c are inserted. If the length of
4980  * result exceeds max_size(), length_error is thrown. The
4981  * value of the string doesn't change if an error is thrown.
4982  */
4983  basic_string&
4984  replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
4985  {
4986  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4987  && __i2 <= _M_iend());
4988  return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
4989  }
4990 
4991  /**
4992  * @brief Replace range of characters with range.
4993  * @param __i1 Iterator referencing start of range to replace.
4994  * @param __i2 Iterator referencing end of range to replace.
4995  * @param __k1 Iterator referencing start of range to insert.
4996  * @param __k2 Iterator referencing end of range to insert.
4997  * @return Reference to this string.
4998  * @throw std::length_error If new length exceeds @c max_size().
4999  *
5000  * Removes the characters in the range [__i1,__i2). In place,
5001  * characters in the range [__k1,__k2) are inserted. If the
5002  * length of result exceeds max_size(), length_error is thrown.
5003  * The value of the string doesn't change if an error is
5004  * thrown.
5005  */
5006  template<class _InputIterator>
5007  basic_string&
5008  replace(iterator __i1, iterator __i2,
5009  _InputIterator __k1, _InputIterator __k2)
5010  {
5011  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5012  && __i2 <= _M_iend());
5013  __glibcxx_requires_valid_range(__k1, __k2);
5014  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
5015  return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
5016  }
5017 
5018  // Specializations for the common case of pointer and iterator:
5019  // useful to avoid the overhead of temporary buffering in _M_replace.
5020  basic_string&
5021  replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
5022  {
5023  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5024  && __i2 <= _M_iend());
5025  __glibcxx_requires_valid_range(__k1, __k2);
5026  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
5027  __k1, __k2 - __k1);
5028  }
5029 
5030  basic_string&
5031  replace(iterator __i1, iterator __i2,
5032  const _CharT* __k1, const _CharT* __k2)
5033  {
5034  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5035  && __i2 <= _M_iend());
5036  __glibcxx_requires_valid_range(__k1, __k2);
5037  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
5038  __k1, __k2 - __k1);
5039  }
5040 
5041  basic_string&
5042  replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
5043  {
5044  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5045  && __i2 <= _M_iend());
5046  __glibcxx_requires_valid_range(__k1, __k2);
5047  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
5048  __k1.base(), __k2 - __k1);
5049  }
5050 
5051  basic_string&
5052  replace(iterator __i1, iterator __i2,
5053  const_iterator __k1, const_iterator __k2)
5054  {
5055  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5056  && __i2 <= _M_iend());
5057  __glibcxx_requires_valid_range(__k1, __k2);
5058  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
5059  __k1.base(), __k2 - __k1);
5060  }
5061 
5062 #if __cplusplus >= 201103L
5063  /**
5064  * @brief Replace range of characters with initializer_list.
5065  * @param __i1 Iterator referencing start of range to replace.
5066  * @param __i2 Iterator referencing end of range to replace.
5067  * @param __l The initializer_list of characters to insert.
5068  * @return Reference to this string.
5069  * @throw std::length_error If new length exceeds @c max_size().
5070  *
5071  * Removes the characters in the range [__i1,__i2). In place,
5072  * characters in the range [__k1,__k2) are inserted. If the
5073  * length of result exceeds max_size(), length_error is thrown.
5074  * The value of the string doesn't change if an error is
5075  * thrown.
5076  */
5077  basic_string& replace(iterator __i1, iterator __i2,
5079  { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
5080 #endif // C++11
5081 
5082 #if __cplusplus >= 201703L
5083  /**
5084  * @brief Replace range of characters with string_view.
5085  * @param __pos The position to replace at.
5086  * @param __n The number of characters to replace.
5087  * @param __svt The object convertible to string_view to insert.
5088  * @return Reference to this string.
5089  */
5090  template<typename _Tp>
5091  _If_sv<_Tp, basic_string&>
5092  replace(size_type __pos, size_type __n, const _Tp& __svt)
5093  {
5094  __sv_type __sv = __svt;
5095  return this->replace(__pos, __n, __sv.data(), __sv.size());
5096  }
5097 
5098  /**
5099  * @brief Replace range of characters with string_view.
5100  * @param __pos1 The position to replace at.
5101  * @param __n1 The number of characters to replace.
5102  * @param __svt The object convertible to string_view to insert from.
5103  * @param __pos2 The position in the string_view to insert from.
5104  * @param __n2 The number of characters to insert.
5105  * @return Reference to this string.
5106  */
5107  template<typename _Tp>
5108  _If_sv<_Tp, basic_string&>
5109  replace(size_type __pos1, size_type __n1, const _Tp& __svt,
5110  size_type __pos2, size_type __n2 = npos)
5111  {
5112  __sv_type __sv = __svt;
5113  return this->replace(__pos1, __n1,
5114  __sv.data()
5115  + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
5116  std::__sv_limit(__sv.size(), __pos2, __n2));
5117  }
5118 
5119  /**
5120  * @brief Replace range of characters with string_view.
5121  * @param __i1 An iterator referencing the start position
5122  to replace at.
5123  * @param __i2 An iterator referencing the end position
5124  for the replace.
5125  * @param __svt The object convertible to string_view to insert from.
5126  * @return Reference to this string.
5127  */
5128  template<typename _Tp>
5129  _If_sv<_Tp, basic_string&>
5130  replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
5131  {
5132  __sv_type __sv = __svt;
5133  return this->replace(__i1 - begin(), __i2 - __i1, __sv);
5134  }
5135 #endif // C++17
5136 
5137  private:
5138  template<class _Integer>
5139  basic_string&
5140  _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
5141  _Integer __val, __true_type)
5142  { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
5143 
5144  template<class _InputIterator>
5145  basic_string&
5146  _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
5147  _InputIterator __k2, __false_type);
5148 
5149  basic_string&
5150  _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
5151  _CharT __c);
5152 
5153  basic_string&
5154  _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
5155  size_type __n2);
5156 
5157  // _S_construct_aux is used to implement the 21.3.1 para 15 which
5158  // requires special behaviour if _InIter is an integral type
5159  template<class _InIterator>
5160  static _CharT*
5161  _S_construct_aux(_InIterator __beg, _InIterator __end,
5162  const _Alloc& __a, __false_type)
5163  {
5164  typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
5165  return _S_construct(__beg, __end, __a, _Tag());
5166  }
5167 
5168  // _GLIBCXX_RESOLVE_LIB_DEFECTS
5169  // 438. Ambiguity in the "do the right thing" clause
5170  template<class _Integer>
5171  static _CharT*
5172  _S_construct_aux(_Integer __beg, _Integer __end,
5173  const _Alloc& __a, __true_type)
5174  { return _S_construct_aux_2(static_cast<size_type>(__beg),
5175  __end, __a); }
5176 
5177  static _CharT*
5178  _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
5179  { return _S_construct(__req, __c, __a); }
5180 
5181  template<class _InIterator>
5182  static _CharT*
5183  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
5184  {
5185  typedef typename std::__is_integer<_InIterator>::__type _Integral;
5186  return _S_construct_aux(__beg, __end, __a, _Integral());
5187  }
5188 
5189  // For Input Iterators, used in istreambuf_iterators, etc.
5190  template<class _InIterator>
5191  static _CharT*
5192  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
5193  input_iterator_tag);
5194 
5195  // For forward_iterators up to random_access_iterators, used for
5196  // string::iterator, _CharT*, etc.
5197  template<class _FwdIterator>
5198  static _CharT*
5199  _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
5200  forward_iterator_tag);
5201 
5202  static _CharT*
5203  _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
5204 
5205  public:
5206 
5207  /**
5208  * @brief Copy substring into C string.
5209  * @param __s C string to copy value into.
5210  * @param __n Number of characters to copy.
5211  * @param __pos Index of first character to copy.
5212  * @return Number of characters actually copied
5213  * @throw std::out_of_range If __pos > size().
5214  *
5215  * Copies up to @a __n characters starting at @a __pos into the
5216  * C string @a __s. If @a __pos is %greater than size(),
5217  * out_of_range is thrown.
5218  */
5219  size_type
5220  copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
5221 
5222  /**
5223  * @brief Swap contents with another string.
5224  * @param __s String to swap with.
5225  *
5226  * Exchanges the contents of this string with that of @a __s in constant
5227  * time.
5228  */
5229  void
5230  swap(basic_string& __s)
5231  _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value);
5232 
5233  // String operations:
5234  /**
5235  * @brief Return const pointer to null-terminated contents.
5236  *
5237  * This is a handle to internal data. Do not modify or dire things may
5238  * happen.
5239  */
5240  const _CharT*
5241  c_str() const _GLIBCXX_NOEXCEPT
5242  { return _M_data(); }
5243 
5244  /**
5245  * @brief Return const pointer to contents.
5246  *
5247  * This is a pointer to internal data. It is undefined to modify
5248  * the contents through the returned pointer. To get a pointer that
5249  * allows modifying the contents use @c &str[0] instead,
5250  * (or in C++17 the non-const @c str.data() overload).
5251  */
5252  const _CharT*
5253  data() const _GLIBCXX_NOEXCEPT
5254  { return _M_data(); }
5255 
5256 #if __cplusplus >= 201703L
5257  /**
5258  * @brief Return non-const pointer to contents.
5259  *
5260  * This is a pointer to the character sequence held by the string.
5261  * Modifying the characters in the sequence is allowed.
5262  */
5263  _CharT*
5264  data() noexcept
5265  {
5266  _M_leak();
5267  return _M_data();
5268  }
5269 #endif
5270 
5271  /**
5272  * @brief Return copy of allocator used to construct this string.
5273  */
5274  allocator_type
5275  get_allocator() const _GLIBCXX_NOEXCEPT
5276  { return _M_dataplus; }
5277 
5278  /**
5279  * @brief Find position of a C substring.
5280  * @param __s C string to locate.
5281  * @param __pos Index of character to search from.
5282  * @param __n Number of characters from @a s to search for.
5283  * @return Index of start of first occurrence.
5284  *
5285  * Starting from @a __pos, searches forward for the first @a
5286  * __n characters in @a __s within this string. If found,
5287  * returns the index where it begins. If not found, returns
5288  * npos.
5289  */
5290  size_type
5291  find(const _CharT* __s, size_type __pos, size_type __n) const
5292  _GLIBCXX_NOEXCEPT;
5293 
5294  /**
5295  * @brief Find position of a string.
5296  * @param __str String to locate.
5297  * @param __pos Index of character to search from (default 0).
5298  * @return Index of start of first occurrence.
5299  *
5300  * Starting from @a __pos, searches forward for value of @a __str within
5301  * this string. If found, returns the index where it begins. If not
5302  * found, returns npos.
5303  */
5304  size_type
5305  find(const basic_string& __str, size_type __pos = 0) const
5306  _GLIBCXX_NOEXCEPT
5307  { return this->find(__str.data(), __pos, __str.size()); }
5308 
5309  /**
5310  * @brief Find position of a C string.
5311  * @param __s C string to locate.
5312  * @param __pos Index of character to search from (default 0).
5313  * @return Index of start of first occurrence.
5314  *
5315  * Starting from @a __pos, searches forward for the value of @a
5316  * __s within this string. If found, returns the index where
5317  * it begins. If not found, returns npos.
5318  */
5319  size_type
5320  find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
5321  {
5322  __glibcxx_requires_string(__s);
5323  return this->find(__s, __pos, traits_type::length(__s));
5324  }
5325 
5326  /**
5327  * @brief Find position of a character.
5328  * @param __c Character to locate.
5329  * @param __pos Index of character to search from (default 0).
5330  * @return Index of first occurrence.
5331  *
5332  * Starting from @a __pos, searches forward for @a __c within
5333  * this string. If found, returns the index where it was
5334  * found. If not found, returns npos.
5335  */
5336  size_type
5337  find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
5338 
5339 #if __cplusplus >= 201703L
5340  /**
5341  * @brief Find position of a string_view.
5342  * @param __svt The object convertible to string_view to locate.
5343  * @param __pos Index of character to search from (default 0).
5344  * @return Index of start of first occurrence.
5345  */
5346  template<typename _Tp>
5347  _If_sv<_Tp, size_type>
5348  find(const _Tp& __svt, size_type __pos = 0) const
5349  noexcept(is_same<_Tp, __sv_type>::value)
5350  {
5351  __sv_type __sv = __svt;
5352  return this->find(__sv.data(), __pos, __sv.size());
5353  }
5354 #endif // C++17
5355 
5356  /**
5357  * @brief Find last position of a string.
5358  * @param __str String to locate.
5359  * @param __pos Index of character to search back from (default end).
5360  * @return Index of start of last occurrence.
5361  *
5362  * Starting from @a __pos, searches backward for value of @a
5363  * __str within this string. If found, returns the index where
5364  * it begins. If not found, returns npos.
5365  */
5366  size_type
5367  rfind(const basic_string& __str, size_type __pos = npos) const
5368  _GLIBCXX_NOEXCEPT
5369  { return this->rfind(__str.data(), __pos, __str.size()); }
5370 
5371  /**
5372  * @brief Find last position of a C substring.
5373  * @param __s C string to locate.
5374  * @param __pos Index of character to search back from.
5375  * @param __n Number of characters from s to search for.
5376  * @return Index of start of last occurrence.
5377  *
5378  * Starting from @a __pos, searches backward for the first @a
5379  * __n characters in @a __s within this string. If found,
5380  * returns the index where it begins. If not found, returns
5381  * npos.
5382  */
5383  size_type
5384  rfind(const _CharT* __s, size_type __pos, size_type __n) const
5385  _GLIBCXX_NOEXCEPT;
5386 
5387  /**
5388  * @brief Find last position of a C string.
5389  * @param __s C string to locate.
5390  * @param __pos Index of character to start search at (default end).
5391  * @return Index of start of last occurrence.
5392  *
5393  * Starting from @a __pos, searches backward for the value of
5394  * @a __s within this string. If found, returns the index
5395  * where it begins. If not found, returns npos.
5396  */
5397  size_type
5398  rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
5399  {
5400  __glibcxx_requires_string(__s);
5401  return this->rfind(__s, __pos, traits_type::length(__s));
5402  }
5403 
5404  /**
5405  * @brief Find last position of a character.
5406  * @param __c Character to locate.
5407  * @param __pos Index of character to search back from (default end).
5408  * @return Index of last occurrence.
5409  *
5410  * Starting from @a __pos, searches backward for @a __c within
5411  * this string. If found, returns the index where it was
5412  * found. If not found, returns npos.
5413  */
5414  size_type
5415  rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
5416 
5417 #if __cplusplus >= 201703L
5418  /**
5419  * @brief Find last position of a string_view.
5420  * @param __svt The object convertible to string_view to locate.
5421  * @param __pos Index of character to search back from (default end).
5422  * @return Index of start of last occurrence.
5423  */
5424  template<typename _Tp>
5425  _If_sv<_Tp, size_type>
5426  rfind(const _Tp& __svt, size_type __pos = npos) const
5428  {
5429  __sv_type __sv = __svt;
5430  return this->rfind(__sv.data(), __pos, __sv.size());
5431  }
5432 #endif // C++17
5433 
5434  /**
5435  * @brief Find position of a character of string.
5436  * @param __str String containing characters to locate.
5437  * @param __pos Index of character to search from (default 0).
5438  * @return Index of first occurrence.
5439  *
5440  * Starting from @a __pos, searches forward for one of the
5441  * characters of @a __str within this string. If found,
5442  * returns the index where it was found. If not found, returns
5443  * npos.
5444  */
5445  size_type
5446  find_first_of(const basic_string& __str, size_type __pos = 0) const
5447  _GLIBCXX_NOEXCEPT
5448  { return this->find_first_of(__str.data(), __pos, __str.size()); }
5449 
5450  /**
5451  * @brief Find position of a character of C substring.
5452  * @param __s String containing characters to locate.
5453  * @param __pos Index of character to search from.
5454  * @param __n Number of characters from s to search for.
5455  * @return Index of first occurrence.
5456  *
5457  * Starting from @a __pos, searches forward for one of the
5458  * first @a __n characters of @a __s within this string. If
5459  * found, returns the index where it was found. If not found,
5460  * returns npos.
5461  */
5462  size_type
5463  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
5464  _GLIBCXX_NOEXCEPT;
5465 
5466  /**
5467  * @brief Find position of a character of C string.
5468  * @param __s String containing characters to locate.
5469  * @param __pos Index of character to search from (default 0).
5470  * @return Index of first occurrence.
5471  *
5472  * Starting from @a __pos, searches forward for one of the
5473  * characters of @a __s within this string. If found, returns
5474  * the index where it was found. If not found, returns npos.
5475  */
5476  size_type
5477  find_first_of(const _CharT* __s, size_type __pos = 0) const
5478  _GLIBCXX_NOEXCEPT
5479  {
5480  __glibcxx_requires_string(__s);
5481  return this->find_first_of(__s, __pos, traits_type::length(__s));
5482  }
5483 
5484  /**
5485  * @brief Find position of a character.
5486  * @param __c Character to locate.
5487  * @param __pos Index of character to search from (default 0).
5488  * @return Index of first occurrence.
5489  *
5490  * Starting from @a __pos, searches forward for the character
5491  * @a __c within this string. If found, returns the index
5492  * where it was found. If not found, returns npos.
5493  *
5494  * Note: equivalent to find(__c, __pos).
5495  */
5496  size_type
5497  find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
5498  { return this->find(__c, __pos); }
5499 
5500 #if __cplusplus >= 201703L
5501  /**
5502  * @brief Find position of a character of a string_view.
5503  * @param __svt An object convertible to string_view containing
5504  * characters to locate.
5505  * @param __pos Index of character to search from (default 0).
5506  * @return Index of first occurrence.
5507  */
5508  template<typename _Tp>
5509  _If_sv<_Tp, size_type>
5510  find_first_of(const _Tp& __svt, size_type __pos = 0) const
5511  noexcept(is_same<_Tp, __sv_type>::value)
5512  {
5513  __sv_type __sv = __svt;
5514  return this->find_first_of(__sv.data(), __pos, __sv.size());
5515  }
5516 #endif // C++17
5517 
5518  /**
5519  * @brief Find last position of a character of string.
5520  * @param __str String containing characters to locate.
5521  * @param __pos Index of character to search back from (default end).
5522  * @return Index of last occurrence.
5523  *
5524  * Starting from @a __pos, searches backward for one of the
5525  * characters of @a __str within this string. If found,
5526  * returns the index where it was found. If not found, returns
5527  * npos.
5528  */
5529  size_type
5530  find_last_of(const basic_string& __str, size_type __pos = npos) const
5531  _GLIBCXX_NOEXCEPT
5532  { return this->find_last_of(__str.data(), __pos, __str.size()); }
5533 
5534  /**
5535  * @brief Find last position of a character of C substring.
5536  * @param __s C string containing characters to locate.
5537  * @param __pos Index of character to search back from.
5538  * @param __n Number of characters from s to search for.
5539  * @return Index of last occurrence.
5540  *
5541  * Starting from @a __pos, searches backward for one of the
5542  * first @a __n characters of @a __s within this string. If
5543  * found, returns the index where it was found. If not found,
5544  * returns npos.
5545  */
5546  size_type
5547  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
5548  _GLIBCXX_NOEXCEPT;
5549 
5550  /**
5551  * @brief Find last position of a character of C string.
5552  * @param __s C string containing characters to locate.
5553  * @param __pos Index of character to search back from (default end).
5554  * @return Index of last occurrence.
5555  *
5556  * Starting from @a __pos, searches backward for one of the
5557  * characters of @a __s within this string. If found, returns
5558  * the index where it was found. If not found, returns npos.
5559  */
5560  size_type
5561  find_last_of(const _CharT* __s, size_type __pos = npos) const
5562  _GLIBCXX_NOEXCEPT
5563  {
5564  __glibcxx_requires_string(__s);
5565  return this->find_last_of(__s, __pos, traits_type::length(__s));
5566  }
5567 
5568  /**
5569  * @brief Find last position of a character.
5570  * @param __c Character to locate.
5571  * @param __pos Index of character to search back from (default end).
5572  * @return Index of last occurrence.
5573  *
5574  * Starting from @a __pos, searches backward for @a __c within
5575  * this string. If found, returns the index where it was
5576  * found. If not found, returns npos.
5577  *
5578  * Note: equivalent to rfind(__c, __pos).
5579  */
5580  size_type
5581  find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
5582  { return this->rfind(__c, __pos); }
5583 
5584 #if __cplusplus >= 201703L
5585  /**
5586  * @brief Find last position of a character of string.
5587  * @param __svt An object convertible to string_view containing
5588  * characters to locate.
5589  * @param __pos Index of character to search back from (default end).
5590  * @return Index of last occurrence.
5591  */
5592  template<typename _Tp>
5593  _If_sv<_Tp, size_type>
5594  find_last_of(const _Tp& __svt, size_type __pos = npos) const
5596  {
5597  __sv_type __sv = __svt;
5598  return this->find_last_of(__sv.data(), __pos, __sv.size());
5599  }
5600 #endif // C++17
5601 
5602  /**
5603  * @brief Find position of a character not in string.
5604  * @param __str String containing characters to avoid.
5605  * @param __pos Index of character to search from (default 0).
5606  * @return Index of first occurrence.
5607  *
5608  * Starting from @a __pos, searches forward for a character not contained
5609  * in @a __str within this string. If found, returns the index where it
5610  * was found. If not found, returns npos.
5611  */
5612  size_type
5613  find_first_not_of(const basic_string& __str, size_type __pos = 0) const
5614  _GLIBCXX_NOEXCEPT
5615  { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
5616 
5617  /**
5618  * @brief Find position of a character not in C substring.
5619  * @param __s C string containing characters to avoid.
5620  * @param __pos Index of character to search from.
5621  * @param __n Number of characters from __s to consider.
5622  * @return Index of first occurrence.
5623  *
5624  * Starting from @a __pos, searches forward for a character not
5625  * contained in the first @a __n characters of @a __s within
5626  * this string. If found, returns the index where it was
5627  * found. If not found, returns npos.
5628  */
5629  size_type
5630  find_first_not_of(const _CharT* __s, size_type __pos,
5631  size_type __n) const _GLIBCXX_NOEXCEPT;
5632 
5633  /**
5634  * @brief Find position of a character not in C string.
5635  * @param __s C string containing characters to avoid.
5636  * @param __pos Index of character to search from (default 0).
5637  * @return Index of first occurrence.
5638  *
5639  * Starting from @a __pos, searches forward for a character not
5640  * contained in @a __s within this string. If found, returns
5641  * the index where it was found. If not found, returns npos.
5642  */
5643  size_type
5644  find_first_not_of(const _CharT* __s, size_type __pos = 0) const
5645  _GLIBCXX_NOEXCEPT
5646  {
5647  __glibcxx_requires_string(__s);
5648  return this->find_first_not_of(__s, __pos, traits_type::length(__s));
5649  }
5650 
5651  /**
5652  * @brief Find position of a different character.
5653  * @param __c Character to avoid.
5654  * @param __pos Index of character to search from (default 0).
5655  * @return Index of first occurrence.
5656  *
5657  * Starting from @a __pos, searches forward for a character
5658  * other than @a __c within this string. If found, returns the
5659  * index where it was found. If not found, returns npos.
5660  */
5661  size_type
5662  find_first_not_of(_CharT __c, size_type __pos = 0) const
5663  _GLIBCXX_NOEXCEPT;
5664 
5665 #if __cplusplus >= 201703L
5666  /**
5667  * @brief Find position of a character not in a string_view.
5668  * @param __svt An object convertible to string_view containing
5669  * characters to avoid.
5670  * @param __pos Index of character to search from (default 0).
5671  * @return Index of first occurrence.
5672  */
5673  template<typename _Tp>
5674  _If_sv<_Tp, size_type>
5675  find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
5676  noexcept(is_same<_Tp, __sv_type>::value)
5677  {
5678  __sv_type __sv = __svt;
5679  return this->find_first_not_of(__sv.data(), __pos, __sv.size());
5680  }
5681 #endif // C++17
5682 
5683  /**
5684  * @brief Find last position of a character not in string.
5685  * @param __str String containing characters to avoid.
5686  * @param __pos Index of character to search back from (default end).
5687  * @return Index of last occurrence.
5688  *
5689  * Starting from @a __pos, searches backward for a character
5690  * not contained in @a __str within this string. If found,
5691  * returns the index where it was found. If not found, returns
5692  * npos.
5693  */
5694  size_type
5695  find_last_not_of(const basic_string& __str, size_type __pos = npos) const
5696  _GLIBCXX_NOEXCEPT
5697  { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
5698 
5699  /**
5700  * @brief Find last position of a character not in C substring.
5701  * @param __s C string containing characters to avoid.
5702  * @param __pos Index of character to search back from.
5703  * @param __n Number of characters from s to consider.
5704  * @return Index of last occurrence.
5705  *
5706  * Starting from @a __pos, searches backward for a character not
5707  * contained in the first @a __n characters of @a __s within this string.
5708  * If found, returns the index where it was found. If not found,
5709  * returns npos.
5710  */
5711  size_type
5712  find_last_not_of(const _CharT* __s, size_type __pos,
5713  size_type __n) const _GLIBCXX_NOEXCEPT;
5714  /**
5715  * @brief Find last position of a character not in C string.
5716  * @param __s C string containing characters to avoid.
5717  * @param __pos Index of character to search back from (default end).
5718  * @return Index of last occurrence.
5719  *
5720  * Starting from @a __pos, searches backward for a character
5721  * not contained in @a __s within this string. If found,
5722  * returns the index where it was found. If not found, returns
5723  * npos.
5724  */
5725  size_type
5726  find_last_not_of(const _CharT* __s, size_type __pos = npos) const
5727  _GLIBCXX_NOEXCEPT
5728  {
5729  __glibcxx_requires_string(__s);
5730  return this->find_last_not_of(__s, __pos, traits_type::length(__s));
5731  }
5732 
5733  /**
5734  * @brief Find last position of a different character.
5735  * @param __c Character to avoid.
5736  * @param __pos Index of character to search back from (default end).
5737  * @return Index of last occurrence.
5738  *
5739  * Starting from @a __pos, searches backward for a character other than
5740  * @a __c within this string. If found, returns the index where it was
5741  * found. If not found, returns npos.
5742  */
5743  size_type
5744  find_last_not_of(_CharT __c, size_type __pos = npos) const
5745  _GLIBCXX_NOEXCEPT;
5746 
5747 #if __cplusplus >= 201703L
5748  /**
5749  * @brief Find last position of a character not in a string_view.
5750  * @param __svt An object convertible to string_view containing
5751  * characters to avoid.
5752  * @param __pos Index of character to search back from (default end).
5753  * @return Index of last occurrence.
5754  */
5755  template<typename _Tp>
5756  _If_sv<_Tp, size_type>
5757  find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
5759  {
5760  __sv_type __sv = __svt;
5761  return this->find_last_not_of(__sv.data(), __pos, __sv.size());
5762  }
5763 #endif // C++17
5764 
5765  /**
5766  * @brief Get a substring.
5767  * @param __pos Index of first character (default 0).
5768  * @param __n Number of characters in substring (default remainder).
5769  * @return The new string.
5770  * @throw std::out_of_range If __pos > size().
5771  *
5772  * Construct and return a new string using the @a __n
5773  * characters starting at @a __pos. If the string is too
5774  * short, use the remainder of the characters. If @a __pos is
5775  * beyond the end of the string, out_of_range is thrown.
5776  */
5777  basic_string
5778  substr(size_type __pos = 0, size_type __n = npos) const
5779  { return basic_string(*this,
5780  _M_check(__pos, "basic_string::substr"), __n); }
5781 
5782  /**
5783  * @brief Compare to a string.
5784  * @param __str String to compare against.
5785  * @return Integer < 0, 0, or > 0.
5786  *
5787  * Returns an integer < 0 if this string is ordered before @a
5788  * __str, 0 if their values are equivalent, or > 0 if this
5789  * string is ordered after @a __str. Determines the effective
5790  * length rlen of the strings to compare as the smallest of
5791  * size() and str.size(). The function then compares the two
5792  * strings by calling traits::compare(data(), str.data(),rlen).
5793  * If the result of the comparison is nonzero returns it,
5794  * otherwise the shorter one is ordered first.
5795  */
5796  int
5797  compare(const basic_string& __str) const
5798  {
5799  const size_type __size = this->size();
5800  const size_type __osize = __str.size();
5801  const size_type __len = std::min(__size, __osize);
5802 
5803  int __r = traits_type::compare(_M_data(), __str.data(), __len);
5804  if (!__r)
5805  __r = _S_compare(__size, __osize);
5806  return __r;
5807  }
5808 
5809 #if __cplusplus >= 201703L
5810  /**
5811  * @brief Compare to a string_view.
5812  * @param __svt An object convertible to string_view to compare against.
5813  * @return Integer < 0, 0, or > 0.
5814  */
5815  template<typename _Tp>
5816  _If_sv<_Tp, int>
5817  compare(const _Tp& __svt) const
5819  {
5820  __sv_type __sv = __svt;
5821  const size_type __size = this->size();
5822  const size_type __osize = __sv.size();
5823  const size_type __len = std::min(__size, __osize);
5824 
5825  int __r = traits_type::compare(_M_data(), __sv.data(), __len);
5826  if (!__r)
5827  __r = _S_compare(__size, __osize);
5828  return __r;
5829  }
5830 
5831  /**
5832  * @brief Compare to a string_view.
5833  * @param __pos A position in the string to start comparing from.
5834  * @param __n The number of characters to compare.
5835  * @param __svt An object convertible to string_view to compare
5836  * against.
5837  * @return Integer < 0, 0, or > 0.
5838  */
5839  template<typename _Tp>
5840  _If_sv<_Tp, int>
5841  compare(size_type __pos, size_type __n, const _Tp& __svt) const
5843  {
5844  __sv_type __sv = __svt;
5845  return __sv_type(*this).substr(__pos, __n).compare(__sv);
5846  }
5847 
5848  /**
5849  * @brief Compare to a string_view.
5850  * @param __pos1 A position in the string to start comparing from.
5851  * @param __n1 The number of characters to compare.
5852  * @param __svt An object convertible to string_view to compare
5853  * against.
5854  * @param __pos2 A position in the string_view to start comparing from.
5855  * @param __n2 The number of characters to compare.
5856  * @return Integer < 0, 0, or > 0.
5857  */
5858  template<typename _Tp>
5859  _If_sv<_Tp, int>
5860  compare(size_type __pos1, size_type __n1, const _Tp& __svt,
5861  size_type __pos2, size_type __n2 = npos) const
5863  {
5864  __sv_type __sv = __svt;
5865  return __sv_type(*this)
5866  .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
5867  }
5868 #endif // C++17
5869 
5870  /**
5871  * @brief Compare substring to a string.
5872  * @param __pos Index of first character of substring.
5873  * @param __n Number of characters in substring.
5874  * @param __str String to compare against.
5875  * @return Integer < 0, 0, or > 0.
5876  *
5877  * Form the substring of this string from the @a __n characters
5878  * starting at @a __pos. Returns an integer < 0 if the
5879  * substring is ordered before @a __str, 0 if their values are
5880  * equivalent, or > 0 if the substring is ordered after @a
5881  * __str. Determines the effective length rlen of the strings
5882  * to compare as the smallest of the length of the substring
5883  * and @a __str.size(). The function then compares the two
5884  * strings by calling
5885  * traits::compare(substring.data(),str.data(),rlen). If the
5886  * result of the comparison is nonzero returns it, otherwise
5887  * the shorter one is ordered first.
5888  */
5889  int
5890  compare(size_type __pos, size_type __n, const basic_string& __str) const;
5891 
5892  /**
5893  * @brief Compare substring to a substring.
5894  * @param __pos1 Index of first character of substring.
5895  * @param __n1 Number of characters in substring.
5896  * @param __str String to compare against.
5897  * @param __pos2 Index of first character of substring of str.
5898  * @param __n2 Number of characters in substring of str.
5899  * @return Integer < 0, 0, or > 0.
5900  *
5901  * Form the substring of this string from the @a __n1
5902  * characters starting at @a __pos1. Form the substring of @a
5903  * __str from the @a __n2 characters starting at @a __pos2.
5904  * Returns an integer < 0 if this substring is ordered before
5905  * the substring of @a __str, 0 if their values are equivalent,
5906  * or > 0 if this substring is ordered after the substring of
5907  * @a __str. Determines the effective length rlen of the
5908  * strings to compare as the smallest of the lengths of the
5909  * substrings. The function then compares the two strings by
5910  * calling
5911  * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5912  * If the result of the comparison is nonzero returns it,
5913  * otherwise the shorter one is ordered first.
5914  */
5915  int
5916  compare(size_type __pos1, size_type __n1, const basic_string& __str,
5917  size_type __pos2, size_type __n2 = npos) const;
5918 
5919  /**
5920  * @brief Compare to a C string.
5921  * @param __s C string to compare against.
5922  * @return Integer < 0, 0, or > 0.
5923  *
5924  * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5925  * their values are equivalent, or > 0 if this string is ordered after
5926  * @a __s. Determines the effective length rlen of the strings to
5927  * compare as the smallest of size() and the length of a string
5928  * constructed from @a __s. The function then compares the two strings
5929  * by calling traits::compare(data(),s,rlen). If the result of the
5930  * comparison is nonzero returns it, otherwise the shorter one is
5931  * ordered first.
5932  */
5933  int
5934  compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
5935 
5936  // _GLIBCXX_RESOLVE_LIB_DEFECTS
5937  // 5 String::compare specification questionable
5938  /**
5939  * @brief Compare substring to a C string.
5940  * @param __pos Index of first character of substring.
5941  * @param __n1 Number of characters in substring.
5942  * @param __s C string to compare against.
5943  * @return Integer < 0, 0, or > 0.
5944  *
5945  * Form the substring of this string from the @a __n1
5946  * characters starting at @a pos. Returns an integer < 0 if
5947  * the substring is ordered before @a __s, 0 if their values
5948  * are equivalent, or > 0 if the substring is ordered after @a
5949  * __s. Determines the effective length rlen of the strings to
5950  * compare as the smallest of the length of the substring and
5951  * the length of a string constructed from @a __s. The
5952  * function then compares the two string by calling
5953  * traits::compare(substring.data(),__s,rlen). If the result of
5954  * the comparison is nonzero returns it, otherwise the shorter
5955  * one is ordered first.
5956  */
5957  int
5958  compare(size_type __pos, size_type __n1, const _CharT* __s) const;
5959 
5960  /**
5961  * @brief Compare substring against a character %array.
5962  * @param __pos Index of first character of substring.
5963  * @param __n1 Number of characters in substring.
5964  * @param __s character %array to compare against.
5965  * @param __n2 Number of characters of s.
5966  * @return Integer < 0, 0, or > 0.
5967  *
5968  * Form the substring of this string from the @a __n1
5969  * characters starting at @a __pos. Form a string from the
5970  * first @a __n2 characters of @a __s. Returns an integer < 0
5971  * if this substring is ordered before the string from @a __s,
5972  * 0 if their values are equivalent, or > 0 if this substring
5973  * is ordered after the string from @a __s. Determines the
5974  * effective length rlen of the strings to compare as the
5975  * smallest of the length of the substring and @a __n2. The
5976  * function then compares the two strings by calling
5977  * traits::compare(substring.data(),s,rlen). If the result of
5978  * the comparison is nonzero returns it, otherwise the shorter
5979  * one is ordered first.
5980  *
5981  * NB: s must have at least n2 characters, &apos;\\0&apos; has
5982  * no special meaning.
5983  */
5984  int
5985  compare(size_type __pos, size_type __n1, const _CharT* __s,
5986  size_type __n2) const;
5987 
5988 #if __cplusplus > 201703L
5989  bool
5990  starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
5991  { return __sv_type(this->data(), this->size()).starts_with(__x); }
5992 
5993  bool
5994  starts_with(_CharT __x) const noexcept
5995  { return __sv_type(this->data(), this->size()).starts_with(__x); }
5996 
5997  bool
5998  starts_with(const _CharT* __x) const noexcept
5999  { return __sv_type(this->data(), this->size()).starts_with(__x); }
6000 
6001  bool
6002  ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
6003  { return __sv_type(this->data(), this->size()).ends_with(__x); }
6004 
6005  bool
6006  ends_with(_CharT __x) const noexcept
6007  { return __sv_type(this->data(), this->size()).ends_with(__x); }
6008 
6009  bool
6010  ends_with(const _CharT* __x) const noexcept
6011  { return __sv_type(this->data(), this->size()).ends_with(__x); }
6012 #endif // C++20
6013 
6014 #if __cplusplus >= 202011L \
6015  || (__cplusplus == 202002L && !defined __STRICT_ANSI__)
6016  bool
6017  contains(basic_string_view<_CharT, _Traits> __x) const noexcept
6018  { return __sv_type(this->data(), this->size()).contains(__x); }
6019 
6020  bool
6021  contains(_CharT __x) const noexcept
6022  { return __sv_type(this->data(), this->size()).contains(__x); }
6023 
6024  bool
6025  contains(const _CharT* __x) const noexcept
6026  { return __sv_type(this->data(), this->size()).contains(__x); }
6027 #endif // C++23
6028 
6029 # ifdef _GLIBCXX_TM_TS_INTERNAL
6030  friend void
6031  ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
6032  void* exc);
6033  friend const char*
6034  ::_txnal_cow_string_c_str(const void *that);
6035  friend void
6036  ::_txnal_cow_string_D1(void *that);
6037  friend void
6038  ::_txnal_cow_string_D1_commit(void *that);
6039 # endif
6040  };
6041 #endif // !_GLIBCXX_USE_CXX11_ABI
6042 
6043 #if __cpp_deduction_guides >= 201606
6044 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6045  template<typename _InputIterator, typename _CharT
6046  = typename iterator_traits<_InputIterator>::value_type,
6047  typename _Allocator = allocator<_CharT>,
6048  typename = _RequireInputIter<_InputIterator>,
6049  typename = _RequireAllocator<_Allocator>>
6050  basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
6051  -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
6052 
6053  // _GLIBCXX_RESOLVE_LIB_DEFECTS
6054  // 3075. basic_string needs deduction guides from basic_string_view
6055  template<typename _CharT, typename _Traits,
6056  typename _Allocator = allocator<_CharT>,
6057  typename = _RequireAllocator<_Allocator>>
6058  basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
6059  -> basic_string<_CharT, _Traits, _Allocator>;
6060 
6061  template<typename _CharT, typename _Traits,
6062  typename _Allocator = allocator<_CharT>,
6063  typename = _RequireAllocator<_Allocator>>
6064  basic_string(basic_string_view<_CharT, _Traits>,
6065  typename basic_string<_CharT, _Traits, _Allocator>::size_type,
6066  typename basic_string<_CharT, _Traits, _Allocator>::size_type,
6067  const _Allocator& = _Allocator())
6068  -> basic_string<_CharT, _Traits, _Allocator>;
6069 _GLIBCXX_END_NAMESPACE_CXX11
6070 #endif
6071 
6072  // operator+
6073  /**
6074  * @brief Concatenate two strings.
6075  * @param __lhs First string.
6076  * @param __rhs Last string.
6077  * @return New string with value of @a __lhs followed by @a __rhs.
6078  */
6079  template<typename _CharT, typename _Traits, typename _Alloc>
6080  basic_string<_CharT, _Traits, _Alloc>
6083  {
6085  __str.append(__rhs);
6086  return __str;
6087  }
6088 
6089  /**
6090  * @brief Concatenate C string and string.
6091  * @param __lhs First string.
6092  * @param __rhs Last string.
6093  * @return New string with value of @a __lhs followed by @a __rhs.
6094  */
6095  template<typename _CharT, typename _Traits, typename _Alloc>
6096  basic_string<_CharT,_Traits,_Alloc>
6097  operator+(const _CharT* __lhs,
6098  const basic_string<_CharT,_Traits,_Alloc>& __rhs);
6099 
6100  /**
6101  * @brief Concatenate character and string.
6102  * @param __lhs First string.
6103  * @param __rhs Last string.
6104  * @return New string with @a __lhs followed by @a __rhs.
6105  */
6106  template<typename _CharT, typename _Traits, typename _Alloc>
6107  basic_string<_CharT,_Traits,_Alloc>
6108  operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
6109 
6110  /**
6111  * @brief Concatenate string and C string.
6112  * @param __lhs First string.
6113  * @param __rhs Last string.
6114  * @return New string with @a __lhs followed by @a __rhs.
6115  */
6116  template<typename _CharT, typename _Traits, typename _Alloc>
6117  inline basic_string<_CharT, _Traits, _Alloc>
6119  const _CharT* __rhs)
6120  {
6122  __str.append(__rhs);
6123  return __str;
6124  }
6125 
6126  /**
6127  * @brief Concatenate string and character.
6128  * @param __lhs First string.
6129  * @param __rhs Last string.
6130  * @return New string with @a __lhs followed by @a __rhs.
6131  */
6132  template<typename _CharT, typename _Traits, typename _Alloc>
6133  inline basic_string<_CharT, _Traits, _Alloc>
6135  {
6136  typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
6137  typedef typename __string_type::size_type __size_type;
6138  __string_type __str(__lhs);
6139  __str.append(__size_type(1), __rhs);
6140  return __str;
6141  }
6142 
6143 #if __cplusplus >= 201103L
6144  template<typename _CharT, typename _Traits, typename _Alloc>
6145  inline basic_string<_CharT, _Traits, _Alloc>
6146  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6147  const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6148  { return std::move(__lhs.append(__rhs)); }
6149 
6150  template<typename _CharT, typename _Traits, typename _Alloc>
6151  inline basic_string<_CharT, _Traits, _Alloc>
6152  operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6153  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6154  { return std::move(__rhs.insert(0, __lhs)); }
6155 
6156  template<typename _CharT, typename _Traits, typename _Alloc>
6157  inline basic_string<_CharT, _Traits, _Alloc>
6158  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6159  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6160  {
6161 #if _GLIBCXX_USE_CXX11_ABI
6162  using _Alloc_traits = allocator_traits<_Alloc>;
6163  bool __use_rhs = false;
6164  if _GLIBCXX17_CONSTEXPR (typename _Alloc_traits::is_always_equal{})
6165  __use_rhs = true;
6166  else if (__lhs.get_allocator() == __rhs.get_allocator())
6167  __use_rhs = true;
6168  if (__use_rhs)
6169 #endif
6170  {
6171  const auto __size = __lhs.size() + __rhs.size();
6172  if (__size > __lhs.capacity() && __size <= __rhs.capacity())
6173  return std::move(__rhs.insert(0, __lhs));
6174  }
6175  return std::move(__lhs.append(__rhs));
6176  }
6177 
6178  template<typename _CharT, typename _Traits, typename _Alloc>
6179  inline basic_string<_CharT, _Traits, _Alloc>
6180  operator+(const _CharT* __lhs,
6181  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6182  { return std::move(__rhs.insert(0, __lhs)); }
6183 
6184  template<typename _CharT, typename _Traits, typename _Alloc>
6185  inline basic_string<_CharT, _Traits, _Alloc>
6186  operator+(_CharT __lhs,
6187  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6188  { return std::move(__rhs.insert(0, 1, __lhs)); }
6189 
6190  template<typename _CharT, typename _Traits, typename _Alloc>
6191  inline basic_string<_CharT, _Traits, _Alloc>
6192  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6193  const _CharT* __rhs)
6194  { return std::move(__lhs.append(__rhs)); }
6195 
6196  template<typename _CharT, typename _Traits, typename _Alloc>
6197  inline basic_string<_CharT, _Traits, _Alloc>
6198  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6199  _CharT __rhs)
6200  { return std::move(__lhs.append(1, __rhs)); }
6201 #endif
6202 
6203  // operator ==
6204  /**
6205  * @brief Test equivalence of two strings.
6206  * @param __lhs First string.
6207  * @param __rhs Second string.
6208  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6209  */
6210  template<typename _CharT, typename _Traits, typename _Alloc>
6211  inline bool
6212  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6214  _GLIBCXX_NOEXCEPT
6215  { return __lhs.compare(__rhs) == 0; }
6216 
6217  template<typename _CharT>
6218  inline
6219  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
6220  operator==(const basic_string<_CharT>& __lhs,
6221  const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT
6222  { return (__lhs.size() == __rhs.size()
6223  && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
6224  __lhs.size())); }
6225 
6226  /**
6227  * @brief Test equivalence of string and C string.
6228  * @param __lhs String.
6229  * @param __rhs C string.
6230  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6231  */
6232  template<typename _CharT, typename _Traits, typename _Alloc>
6233  inline bool
6234  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6235  const _CharT* __rhs)
6236  { return __lhs.compare(__rhs) == 0; }
6237 
6238 #if __cpp_lib_three_way_comparison
6239  /**
6240  * @brief Three-way comparison of a string and a C string.
6241  * @param __lhs A string.
6242  * @param __rhs A null-terminated string.
6243  * @return A value indicating whether `__lhs` is less than, equal to,
6244  * greater than, or incomparable with `__rhs`.
6245  */
6246  template<typename _CharT, typename _Traits, typename _Alloc>
6247  inline auto
6248  operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6249  const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept
6250  -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
6251  { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
6252 
6253  /**
6254  * @brief Three-way comparison of a string and a C string.
6255  * @param __lhs A string.
6256  * @param __rhs A null-terminated string.
6257  * @return A value indicating whether `__lhs` is less than, equal to,
6258  * greater than, or incomparable with `__rhs`.
6259  */
6260  template<typename _CharT, typename _Traits, typename _Alloc>
6261  inline auto
6262  operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6263  const _CharT* __rhs) noexcept
6264  -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
6265  { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
6266 #else
6267  /**
6268  * @brief Test equivalence of C string and string.
6269  * @param __lhs C string.
6270  * @param __rhs String.
6271  * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
6272  */
6273  template<typename _CharT, typename _Traits, typename _Alloc>
6274  inline bool
6275  operator==(const _CharT* __lhs,
6277  { return __rhs.compare(__lhs) == 0; }
6278 
6279  // operator !=
6280  /**
6281  * @brief Test difference of two strings.
6282  * @param __lhs First string.
6283  * @param __rhs Second string.
6284  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6285  */
6286  template<typename _CharT, typename _Traits, typename _Alloc>
6287  inline bool
6288  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6290  _GLIBCXX_NOEXCEPT
6291  { return !(__lhs == __rhs); }
6292 
6293  /**
6294  * @brief Test difference of C string and string.
6295  * @param __lhs C string.
6296  * @param __rhs String.
6297  * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
6298  */
6299  template<typename _CharT, typename _Traits, typename _Alloc>
6300  inline bool
6301  operator!=(const _CharT* __lhs,
6303  { return !(__lhs == __rhs); }
6304 
6305  /**
6306  * @brief Test difference of string and C string.
6307  * @param __lhs String.
6308  * @param __rhs C string.
6309  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6310  */
6311  template<typename _CharT, typename _Traits, typename _Alloc>
6312  inline bool
6313  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6314  const _CharT* __rhs)
6315  { return !(__lhs == __rhs); }
6316 
6317  // operator <
6318  /**
6319  * @brief Test if string precedes string.
6320  * @param __lhs First string.
6321  * @param __rhs Second string.
6322  * @return True if @a __lhs precedes @a __rhs. False otherwise.
6323  */
6324  template<typename _CharT, typename _Traits, typename _Alloc>
6325  inline bool
6326  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6328  _GLIBCXX_NOEXCEPT
6329  { return __lhs.compare(__rhs) < 0; }
6330 
6331  /**
6332  * @brief Test if string precedes C string.
6333  * @param __lhs String.
6334  * @param __rhs C string.
6335  * @return True if @a __lhs precedes @a __rhs. False otherwise.
6336  */
6337  template<typename _CharT, typename _Traits, typename _Alloc>
6338  inline bool
6339  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6340  const _CharT* __rhs)
6341  { return __lhs.compare(__rhs) < 0; }
6342 
6343  /**
6344  * @brief Test if C string precedes string.
6345  * @param __lhs C string.
6346  * @param __rhs String.
6347  * @return True if @a __lhs precedes @a __rhs. False otherwise.
6348  */
6349  template<typename _CharT, typename _Traits, typename _Alloc>
6350  inline bool
6351  operator<(const _CharT* __lhs,
6353  { return __rhs.compare(__lhs) > 0; }
6354 
6355  // operator >
6356  /**
6357  * @brief Test if string follows string.
6358  * @param __lhs First string.
6359  * @param __rhs Second string.
6360  * @return True if @a __lhs follows @a __rhs. False otherwise.
6361  */
6362  template<typename _CharT, typename _Traits, typename _Alloc>
6363  inline bool
6366  _GLIBCXX_NOEXCEPT
6367  { return __lhs.compare(__rhs) > 0; }
6368 
6369  /**
6370  * @brief Test if string follows C string.
6371  * @param __lhs String.
6372  * @param __rhs C string.
6373  * @return True if @a __lhs follows @a __rhs. False otherwise.
6374  */
6375  template<typename _CharT, typename _Traits, typename _Alloc>
6376  inline bool
6378  const _CharT* __rhs)
6379  { return __lhs.compare(__rhs) > 0; }
6380 
6381  /**
6382  * @brief Test if C string follows string.
6383  * @param __lhs C string.
6384  * @param __rhs String.
6385  * @return True if @a __lhs follows @a __rhs. False otherwise.
6386  */
6387  template<typename _CharT, typename _Traits, typename _Alloc>
6388  inline bool
6389  operator>(const _CharT* __lhs,
6391  { return __rhs.compare(__lhs) < 0; }
6392 
6393  // operator <=
6394  /**
6395  * @brief Test if string doesn't follow string.
6396  * @param __lhs First string.
6397  * @param __rhs Second string.
6398  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6399  */
6400  template<typename _CharT, typename _Traits, typename _Alloc>
6401  inline bool
6402  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6404  _GLIBCXX_NOEXCEPT
6405  { return __lhs.compare(__rhs) <= 0; }
6406 
6407  /**
6408  * @brief Test if string doesn't follow C string.
6409  * @param __lhs String.
6410  * @param __rhs C string.
6411  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6412  */
6413  template<typename _CharT, typename _Traits, typename _Alloc>
6414  inline bool
6415  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6416  const _CharT* __rhs)
6417  { return __lhs.compare(__rhs) <= 0; }
6418 
6419  /**
6420  * @brief Test if C string doesn't follow string.
6421  * @param __lhs C string.
6422  * @param __rhs String.
6423  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6424  */
6425  template<typename _CharT, typename _Traits, typename _Alloc>
6426  inline bool
6427  operator<=(const _CharT* __lhs,
6429  { return __rhs.compare(__lhs) >= 0; }
6430 
6431  // operator >=
6432  /**
6433  * @brief Test if string doesn't precede string.
6434  * @param __lhs First string.
6435  * @param __rhs Second string.
6436  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6437  */
6438  template<typename _CharT, typename _Traits, typename _Alloc>
6439  inline bool
6440  operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6442  _GLIBCXX_NOEXCEPT
6443  { return __lhs.compare(__rhs) >= 0; }
6444 
6445  /**
6446  * @brief Test if string doesn't precede C string.
6447  * @param __lhs String.
6448  * @param __rhs C string.
6449  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6450  */
6451  template<typename _CharT, typename _Traits, typename _Alloc>
6452  inline bool
6453  operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6454  const _CharT* __rhs)
6455  { return __lhs.compare(__rhs) >= 0; }
6456 
6457  /**
6458  * @brief Test if C string doesn't precede string.
6459  * @param __lhs C string.
6460  * @param __rhs String.
6461  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6462  */
6463  template<typename _CharT, typename _Traits, typename _Alloc>
6464  inline bool
6465  operator>=(const _CharT* __lhs,
6467  { return __rhs.compare(__lhs) <= 0; }
6468 #endif // three-way comparison
6469 
6470  /**
6471  * @brief Swap contents of two strings.
6472  * @param __lhs First string.
6473  * @param __rhs Second string.
6474  *
6475  * Exchanges the contents of @a __lhs and @a __rhs in constant time.
6476  */
6477  template<typename _CharT, typename _Traits, typename _Alloc>
6478  inline void
6481  _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
6482  { __lhs.swap(__rhs); }
6483 
6484 
6485  /**
6486  * @brief Read stream into a string.
6487  * @param __is Input stream.
6488  * @param __str Buffer to store into.
6489  * @return Reference to the input stream.
6490  *
6491  * Stores characters from @a __is into @a __str until whitespace is
6492  * found, the end of the stream is encountered, or str.max_size()
6493  * is reached. If is.width() is non-zero, that is the limit on the
6494  * number of characters stored into @a __str. Any previous
6495  * contents of @a __str are erased.
6496  */
6497  template<typename _CharT, typename _Traits, typename _Alloc>
6498  basic_istream<_CharT, _Traits>&
6499  operator>>(basic_istream<_CharT, _Traits>& __is,
6500  basic_string<_CharT, _Traits, _Alloc>& __str);
6501 
6502  template<>
6503  basic_istream<char>&
6505 
6506  /**
6507  * @brief Write string to a stream.
6508  * @param __os Output stream.
6509  * @param __str String to write out.
6510  * @return Reference to the output stream.
6511  *
6512  * Output characters of @a __str into os following the same rules as for
6513  * writing a C string.
6514  */
6515  template<typename _CharT, typename _Traits, typename _Alloc>
6519  {
6520  // _GLIBCXX_RESOLVE_LIB_DEFECTS
6521  // 586. string inserter not a formatted function
6522  return __ostream_insert(__os, __str.data(), __str.size());
6523  }
6524 
6525  /**
6526  * @brief Read a line from stream into a string.
6527  * @param __is Input stream.
6528  * @param __str Buffer to store into.
6529  * @param __delim Character marking end of line.
6530  * @return Reference to the input stream.
6531  *
6532  * Stores characters from @a __is into @a __str until @a __delim is
6533  * found, the end of the stream is encountered, or str.max_size()
6534  * is reached. Any previous contents of @a __str are erased. If
6535  * @a __delim is encountered, it is extracted but not stored into
6536  * @a __str.
6537  */
6538  template<typename _CharT, typename _Traits, typename _Alloc>
6539  basic_istream<_CharT, _Traits>&
6540  getline(basic_istream<_CharT, _Traits>& __is,
6541  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
6542 
6543  /**
6544  * @brief Read a line from stream into a string.
6545  * @param __is Input stream.
6546  * @param __str Buffer to store into.
6547  * @return Reference to the input stream.
6548  *
6549  * Stores characters from is into @a __str until &apos;\n&apos; is
6550  * found, the end of the stream is encountered, or str.max_size()
6551  * is reached. Any previous contents of @a __str are erased. If
6552  * end of line is encountered, it is extracted but not stored into
6553  * @a __str.
6554  */
6555  template<typename _CharT, typename _Traits, typename _Alloc>
6556  inline basic_istream<_CharT, _Traits>&
6559  { return std::getline(__is, __str, __is.widen('\n')); }
6560 
6561 #if __cplusplus >= 201103L
6562  /// Read a line from an rvalue stream into a string.
6563  template<typename _CharT, typename _Traits, typename _Alloc>
6564  inline basic_istream<_CharT, _Traits>&
6566  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
6567  { return std::getline(__is, __str, __delim); }
6568 
6569  /// Read a line from an rvalue stream into a string.
6570  template<typename _CharT, typename _Traits, typename _Alloc>
6571  inline basic_istream<_CharT, _Traits>&
6574  { return std::getline(__is, __str); }
6575 #endif
6576 
6577  template<>
6578  basic_istream<char>&
6579  getline(basic_istream<char>& __in, basic_string<char>& __str,
6580  char __delim);
6581 
6582 #ifdef _GLIBCXX_USE_WCHAR_T
6583  template<>
6584  basic_istream<wchar_t>&
6585  getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
6586  wchar_t __delim);
6587 #endif
6588 
6589 _GLIBCXX_END_NAMESPACE_VERSION
6590 } // namespace
6591 
6592 #if __cplusplus >= 201103L
6593 
6594 #include <ext/string_conversions.h>
6595 #include <bits/charconv.h>
6596 
6597 namespace std _GLIBCXX_VISIBILITY(default)
6598 {
6599 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6600 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6601 
6602 #if _GLIBCXX_USE_C99_STDLIB
6603  // 21.4 Numeric Conversions [string.conversions].
6604  inline int
6605  stoi(const string& __str, size_t* __idx = 0, int __base = 10)
6606  { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
6607  __idx, __base); }
6608 
6609  inline long
6610  stol(const string& __str, size_t* __idx = 0, int __base = 10)
6611  { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
6612  __idx, __base); }
6613 
6614  inline unsigned long
6615  stoul(const string& __str, size_t* __idx = 0, int __base = 10)
6616  { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
6617  __idx, __base); }
6618 
6619  inline long long
6620  stoll(const string& __str, size_t* __idx = 0, int __base = 10)
6621  { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
6622  __idx, __base); }
6623 
6624  inline unsigned long long
6625  stoull(const string& __str, size_t* __idx = 0, int __base = 10)
6626  { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
6627  __idx, __base); }
6628 
6629  // NB: strtof vs strtod.
6630  inline float
6631  stof(const string& __str, size_t* __idx = 0)
6632  { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
6633 
6634  inline double
6635  stod(const string& __str, size_t* __idx = 0)
6636  { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
6637 
6638  inline long double
6639  stold(const string& __str, size_t* __idx = 0)
6640  { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
6641 #endif // _GLIBCXX_USE_C99_STDLIB
6642 
6643  // DR 1261. Insufficent overloads for to_string / to_wstring
6644 
6645  inline string
6646  to_string(int __val)
6647  {
6648  const bool __neg = __val < 0;
6649  const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
6650  const auto __len = __detail::__to_chars_len(__uval);
6651  string __str(__neg + __len, '-');
6652  __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
6653  return __str;
6654  }
6655 
6656  inline string
6657  to_string(unsigned __val)
6658  {
6659  string __str(__detail::__to_chars_len(__val), '\0');
6660  __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
6661  return __str;
6662  }
6663 
6664  inline string
6665  to_string(long __val)
6666  {
6667  const bool __neg = __val < 0;
6668  const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
6669  const auto __len = __detail::__to_chars_len(__uval);
6670  string __str(__neg + __len, '-');
6671  __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
6672  return __str;
6673  }
6674 
6675  inline string
6676  to_string(unsigned long __val)
6677  {
6678  string __str(__detail::__to_chars_len(__val), '\0');
6679  __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
6680  return __str;
6681  }
6682 
6683  inline string
6684  to_string(long long __val)
6685  {
6686  const bool __neg = __val < 0;
6687  const unsigned long long __uval
6688  = __neg ? (unsigned long long)~__val + 1ull : __val;
6689  const auto __len = __detail::__to_chars_len(__uval);
6690  string __str(__neg + __len, '-');
6691  __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
6692  return __str;
6693  }
6694 
6695  inline string
6696  to_string(unsigned long long __val)
6697  {
6698  string __str(__detail::__to_chars_len(__val), '\0');
6699  __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
6700  return __str;
6701  }
6702 
6703 #if _GLIBCXX_USE_C99_STDIO
6704  // NB: (v)snprintf vs sprintf.
6705 
6706  inline string
6707  to_string(float __val)
6708  {
6709  const int __n =
6710  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6711  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6712  "%f", __val);
6713  }
6714 
6715  inline string
6716  to_string(double __val)
6717  {
6718  const int __n =
6719  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6720  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6721  "%f", __val);
6722  }
6723 
6724  inline string
6725  to_string(long double __val)
6726  {
6727  const int __n =
6728  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6729  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6730  "%Lf", __val);
6731  }
6732 #endif // _GLIBCXX_USE_C99_STDIO
6733 
6734 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
6735  inline int
6736  stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
6737  { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
6738  __idx, __base); }
6739 
6740  inline long
6741  stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
6742  { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
6743  __idx, __base); }
6744 
6745  inline unsigned long
6746  stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
6747  { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
6748  __idx, __base); }
6749 
6750  inline long long
6751  stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
6752  { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
6753  __idx, __base); }
6754 
6755  inline unsigned long long
6756  stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
6757  { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
6758  __idx, __base); }
6759 
6760  // NB: wcstof vs wcstod.
6761  inline float
6762  stof(const wstring& __str, size_t* __idx = 0)
6763  { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
6764 
6765  inline double
6766  stod(const wstring& __str, size_t* __idx = 0)
6767  { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
6768 
6769  inline long double
6770  stold(const wstring& __str, size_t* __idx = 0)
6771  { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
6772 
6773 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6774  // DR 1261.
6775  inline wstring
6776  to_wstring(int __val)
6777  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
6778  L"%d", __val); }
6779 
6780  inline wstring
6781  to_wstring(unsigned __val)
6782  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6783  4 * sizeof(unsigned),
6784  L"%u", __val); }
6785 
6786  inline wstring
6787  to_wstring(long __val)
6788  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
6789  L"%ld", __val); }
6790 
6791  inline wstring
6792  to_wstring(unsigned long __val)
6793  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6794  4 * sizeof(unsigned long),
6795  L"%lu", __val); }
6796 
6797  inline wstring
6798  to_wstring(long long __val)
6799  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6800  4 * sizeof(long long),
6801  L"%lld", __val); }
6802 
6803  inline wstring
6804  to_wstring(unsigned long long __val)
6805  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6806  4 * sizeof(unsigned long long),
6807  L"%llu", __val); }
6808 
6809  inline wstring
6810  to_wstring(float __val)
6811  {
6812  const int __n =
6813  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6814  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6815  L"%f", __val);
6816  }
6817 
6818  inline wstring
6819  to_wstring(double __val)
6820  {
6821  const int __n =
6822  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6823  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6824  L"%f", __val);
6825  }
6826 
6827  inline wstring
6828  to_wstring(long double __val)
6829  {
6830  const int __n =
6831  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6832  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6833  L"%Lf", __val);
6834  }
6835 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6836 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6837 
6838 _GLIBCXX_END_NAMESPACE_CXX11
6839 _GLIBCXX_END_NAMESPACE_VERSION
6840 } // namespace
6841 
6842 #endif /* C++11 */
6843 
6844 #if __cplusplus >= 201103L
6845 
6846 #include <bits/functional_hash.h>
6847 
6848 namespace std _GLIBCXX_VISIBILITY(default)
6849 {
6850 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6851 
6852  // DR 1182.
6853 
6854 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6855  /// std::hash specialization for string.
6856  template<>
6857  struct hash<string>
6858  : public __hash_base<size_t, string>
6859  {
6860  size_t
6861  operator()(const string& __s) const noexcept
6862  { return std::_Hash_impl::hash(__s.data(), __s.length()); }
6863  };
6864 
6865  template<>
6866  struct __is_fast_hash<hash<string>> : std::false_type
6867  { };
6868 
6869 #ifdef _GLIBCXX_USE_WCHAR_T
6870  /// std::hash specialization for wstring.
6871  template<>
6872  struct hash<wstring>
6873  : public __hash_base<size_t, wstring>
6874  {
6875  size_t
6876  operator()(const wstring& __s) const noexcept
6877  { return std::_Hash_impl::hash(__s.data(),
6878  __s.length() * sizeof(wchar_t)); }
6879  };
6880 
6881  template<>
6882  struct __is_fast_hash<hash<wstring>> : std::false_type
6883  { };
6884 #endif
6885 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6886 
6887 #ifdef _GLIBCXX_USE_CHAR8_T
6888  /// std::hash specialization for u8string.
6889  template<>
6890  struct hash<u8string>
6891  : public __hash_base<size_t, u8string>
6892  {
6893  size_t
6894  operator()(const u8string& __s) const noexcept
6895  { return std::_Hash_impl::hash(__s.data(),
6896  __s.length() * sizeof(char8_t)); }
6897  };
6898 
6899  template<>
6900  struct __is_fast_hash<hash<u8string>> : std::false_type
6901  { };
6902 #endif
6903 
6904  /// std::hash specialization for u16string.
6905  template<>
6906  struct hash<u16string>
6907  : public __hash_base<size_t, u16string>
6908  {
6909  size_t
6910  operator()(const u16string& __s) const noexcept
6911  { return std::_Hash_impl::hash(__s.data(),
6912  __s.length() * sizeof(char16_t)); }
6913  };
6914 
6915  template<>
6916  struct __is_fast_hash<hash<u16string>> : std::false_type
6917  { };
6918 
6919  /// std::hash specialization for u32string.
6920  template<>
6921  struct hash<u32string>
6922  : public __hash_base<size_t, u32string>
6923  {
6924  size_t
6925  operator()(const u32string& __s) const noexcept
6926  { return std::_Hash_impl::hash(__s.data(),
6927  __s.length() * sizeof(char32_t)); }
6928  };
6929 
6930  template<>
6931  struct __is_fast_hash<hash<u32string>> : std::false_type
6932  { };
6933 
6934 #if __cplusplus >= 201402L
6935 
6936 #define __cpp_lib_string_udls 201304
6937 
6938  inline namespace literals
6939  {
6940  inline namespace string_literals
6941  {
6942 #pragma GCC diagnostic push
6943 #pragma GCC diagnostic ignored "-Wliteral-suffix"
6944  _GLIBCXX_DEFAULT_ABI_TAG
6945  inline basic_string<char>
6946  operator""s(const char* __str, size_t __len)
6947  { return basic_string<char>{__str, __len}; }
6948 
6949 #ifdef _GLIBCXX_USE_WCHAR_T
6950  _GLIBCXX_DEFAULT_ABI_TAG
6951  inline basic_string<wchar_t>
6952  operator""s(const wchar_t* __str, size_t __len)
6953  { return basic_string<wchar_t>{__str, __len}; }
6954 #endif
6955 
6956 #ifdef _GLIBCXX_USE_CHAR8_T
6957  _GLIBCXX_DEFAULT_ABI_TAG
6958  inline basic_string<char8_t>
6959  operator""s(const char8_t* __str, size_t __len)
6960  { return basic_string<char8_t>{__str, __len}; }
6961 #endif
6962 
6963  _GLIBCXX_DEFAULT_ABI_TAG
6964  inline basic_string<char16_t>
6965  operator""s(const char16_t* __str, size_t __len)
6966  { return basic_string<char16_t>{__str, __len}; }
6967 
6968  _GLIBCXX_DEFAULT_ABI_TAG
6969  inline basic_string<char32_t>
6970  operator""s(const char32_t* __str, size_t __len)
6971  { return basic_string<char32_t>{__str, __len}; }
6972 
6973 #pragma GCC diagnostic pop
6974  } // inline namespace string_literals
6975  } // inline namespace literals
6976 
6977 #if __cplusplus >= 201703L
6978  namespace __detail::__variant
6979  {
6980  template<typename> struct _Never_valueless_alt; // see <variant>
6981 
6982  // Provide the strong exception-safety guarantee when emplacing a
6983  // basic_string into a variant, but only if moving the string cannot throw.
6984  template<typename _Tp, typename _Traits, typename _Alloc>
6985  struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
6986  : __and_<
6987  is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
6988  is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
6989  >::type
6990  { };
6991  } // namespace __detail::__variant
6992 #endif // C++17
6993 #endif // C++14
6994 
6995 _GLIBCXX_END_NAMESPACE_VERSION
6996 } // namespace std
6997 
6998 #endif // C++11
6999 
7000 #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:104
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
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1540
basic_istream< _CharT, _Traits > & getline(basic_istream< _CharT, _Traits > &__is, basic_string< _CharT, _Traits, _Alloc > &__str, _CharT __delim)
Read a line from stream into a string.
constexpr _Iterator __base(_Iterator __it)
initializer_list
char_type widen(char __c) const
Widens characters.
Definition: basic_ios.h:449
Template class basic_ostream.
Definition: ostream:59
Primary class template hash.
integral_constant
Definition: type_traits:58
is_same
Definition: type_traits: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.