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