libstdc++
basic_string.h
Go to the documentation of this file.
1 // Components for manipulating sequences of characters -*- C++ -*-
2 
3 // Copyright (C) 1997-2023 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/alloc_traits.h>
40 #include <debug/debug.h>
41 
42 #if __cplusplus >= 201103L
43 #include <initializer_list>
44 #endif
45 
46 #if __cplusplus >= 201703L
47 # include <string_view>
48 #endif
49 
50 #if ! _GLIBCXX_USE_CXX11_ABI
51 # include "cow_string.h"
52 #else
53 namespace std _GLIBCXX_VISIBILITY(default)
54 {
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 _GLIBCXX_BEGIN_NAMESPACE_CXX11
57 
58 #ifdef __cpp_lib_is_constant_evaluated
59 // Support P0980R1 in C++20.
60 # define __cpp_lib_constexpr_string 201907L
61 #elif __cplusplus >= 201703L && _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
62 // Support P0426R1 changes to char_traits in C++17.
63 # define __cpp_lib_constexpr_string 201611L
64 #endif
65 
66  /**
67  * @class basic_string basic_string.h <string>
68  * @brief Managing sequences of characters and character-like objects.
69  *
70  * @ingroup strings
71  * @ingroup sequences
72  * @headerfile string
73  * @since C++98
74  *
75  * @tparam _CharT Type of character
76  * @tparam _Traits Traits for character type, defaults to
77  * char_traits<_CharT>.
78  * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
79  *
80  * Meets the requirements of a <a href="tables.html#65">container</a>, a
81  * <a href="tables.html#66">reversible container</a>, and a
82  * <a href="tables.html#67">sequence</a>. Of the
83  * <a href="tables.html#68">optional sequence requirements</a>, only
84  * @c push_back, @c at, and @c %array access are supported.
85  */
86  template<typename _CharT, typename _Traits, typename _Alloc>
87  class basic_string
88  {
90  rebind<_CharT>::other _Char_alloc_type;
91 
92  typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
93 
94  // Types:
95  public:
96  typedef _Traits traits_type;
97  typedef typename _Traits::char_type value_type;
98  typedef _Char_alloc_type allocator_type;
99  typedef typename _Alloc_traits::size_type size_type;
100  typedef typename _Alloc_traits::difference_type difference_type;
101  typedef typename _Alloc_traits::reference reference;
102  typedef typename _Alloc_traits::const_reference const_reference;
103  typedef typename _Alloc_traits::pointer pointer;
104  typedef typename _Alloc_traits::const_pointer const_pointer;
105  typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
106  typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
107  const_iterator;
108  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
109  typedef std::reverse_iterator<iterator> reverse_iterator;
110 
111  /// Value returned by various member functions when they fail.
112  static const size_type npos = static_cast<size_type>(-1);
113 
114  protected:
115  // type used for positions in insert, erase etc.
116 #if __cplusplus < 201103L
117  typedef iterator __const_iterator;
118 #else
119  typedef const_iterator __const_iterator;
120 #endif
121 
122  private:
123  static _GLIBCXX20_CONSTEXPR pointer
124  _S_allocate(_Char_alloc_type& __a, size_type __n)
125  {
126  pointer __p = _Alloc_traits::allocate(__a, __n);
127 #if __cpp_lib_constexpr_string >= 201907L
128  // std::char_traits begins the lifetime of characters,
129  // but custom traits might not, so do it here.
130  if constexpr (!is_same_v<_Traits, char_traits<_CharT>>)
131  if (std::__is_constant_evaluated())
132  // Begin the lifetime of characters in allocated storage.
133  for (size_type __i = 0; __i < __n; ++__i)
134  std::construct_at(__builtin_addressof(__p[__i]));
135 #endif
136  return __p;
137  }
138 
139 #if __cplusplus >= 201703L
140  // A helper type for avoiding boiler-plate.
141  typedef basic_string_view<_CharT, _Traits> __sv_type;
142 
143  template<typename _Tp, typename _Res>
144  using _If_sv = enable_if_t<
145  __and_<is_convertible<const _Tp&, __sv_type>,
146  __not_<is_convertible<const _Tp*, const basic_string*>>,
147  __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
148  _Res>;
149 
150  // Allows an implicit conversion to __sv_type.
151  _GLIBCXX20_CONSTEXPR
152  static __sv_type
153  _S_to_string_view(__sv_type __svt) noexcept
154  { return __svt; }
155 
156  // Wraps a string_view by explicit conversion and thus
157  // allows to add an internal constructor that does not
158  // participate in overload resolution when a string_view
159  // is provided.
160  struct __sv_wrapper
161  {
162  _GLIBCXX20_CONSTEXPR explicit
163  __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
164 
165  __sv_type _M_sv;
166  };
167 
168  /**
169  * @brief Only internally used: Construct string from a string view
170  * wrapper.
171  * @param __svw string view wrapper.
172  * @param __a Allocator to use.
173  */
174  _GLIBCXX20_CONSTEXPR
175  explicit
176  basic_string(__sv_wrapper __svw, const _Alloc& __a)
177  : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
178 #endif
179 
180  // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
181  struct _Alloc_hider : allocator_type // TODO check __is_final
182  {
183 #if __cplusplus < 201103L
184  _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
185  : allocator_type(__a), _M_p(__dat) { }
186 #else
187  _GLIBCXX20_CONSTEXPR
188  _Alloc_hider(pointer __dat, const _Alloc& __a)
189  : allocator_type(__a), _M_p(__dat) { }
190 
191  _GLIBCXX20_CONSTEXPR
192  _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
193  : allocator_type(std::move(__a)), _M_p(__dat) { }
194 #endif
195 
196  pointer _M_p; // The actual data.
197  };
198 
199  _Alloc_hider _M_dataplus;
200  size_type _M_string_length;
201 
202  enum { _S_local_capacity = 15 / sizeof(_CharT) };
203 
204  union
205  {
206  _CharT _M_local_buf[_S_local_capacity + 1];
207  size_type _M_allocated_capacity;
208  };
209 
210  _GLIBCXX20_CONSTEXPR
211  void
212  _M_data(pointer __p)
213  { _M_dataplus._M_p = __p; }
214 
215  _GLIBCXX20_CONSTEXPR
216  void
217  _M_length(size_type __length)
218  { _M_string_length = __length; }
219 
220  _GLIBCXX20_CONSTEXPR
221  pointer
222  _M_data() const
223  { return _M_dataplus._M_p; }
224 
225  _GLIBCXX20_CONSTEXPR
226  pointer
227  _M_local_data()
228  {
229 #if __cplusplus >= 201103L
230  return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
231 #else
232  return pointer(_M_local_buf);
233 #endif
234  }
235 
236  _GLIBCXX20_CONSTEXPR
237  const_pointer
238  _M_local_data() const
239  {
240 #if __cplusplus >= 201103L
242 #else
243  return const_pointer(_M_local_buf);
244 #endif
245  }
246 
247  _GLIBCXX20_CONSTEXPR
248  void
249  _M_capacity(size_type __capacity)
250  { _M_allocated_capacity = __capacity; }
251 
252  _GLIBCXX20_CONSTEXPR
253  void
254  _M_set_length(size_type __n)
255  {
256  _M_length(__n);
257  traits_type::assign(_M_data()[__n], _CharT());
258  }
259 
260  _GLIBCXX20_CONSTEXPR
261  bool
262  _M_is_local() const
263  {
264  if (_M_data() == _M_local_data())
265  {
266  if (_M_string_length > _S_local_capacity)
267  __builtin_unreachable();
268  return true;
269  }
270  return false;
271  }
272 
273  // Create & Destroy
274  _GLIBCXX20_CONSTEXPR
275  pointer
276  _M_create(size_type&, size_type);
277 
278  _GLIBCXX20_CONSTEXPR
279  void
280  _M_dispose()
281  {
282  if (!_M_is_local())
283  _M_destroy(_M_allocated_capacity);
284  }
285 
286  _GLIBCXX20_CONSTEXPR
287  void
288  _M_destroy(size_type __size) throw()
289  { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
290 
291 #if __cplusplus < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
292  // _M_construct_aux is used to implement the 21.3.1 para 15 which
293  // requires special behaviour if _InIterator is an integral type
294  template<typename _InIterator>
295  void
296  _M_construct_aux(_InIterator __beg, _InIterator __end,
297  std::__false_type)
298  {
299  typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
300  _M_construct(__beg, __end, _Tag());
301  }
302 
303  // _GLIBCXX_RESOLVE_LIB_DEFECTS
304  // 438. Ambiguity in the "do the right thing" clause
305  template<typename _Integer>
306  void
307  _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
308  { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
309 
310  void
311  _M_construct_aux_2(size_type __req, _CharT __c)
312  { _M_construct(__req, __c); }
313 #endif
314 
315  // For Input Iterators, used in istreambuf_iterators, etc.
316  template<typename _InIterator>
317  _GLIBCXX20_CONSTEXPR
318  void
319  _M_construct(_InIterator __beg, _InIterator __end,
321 
322  // For forward_iterators up to random_access_iterators, used for
323  // string::iterator, _CharT*, etc.
324  template<typename _FwdIterator>
325  _GLIBCXX20_CONSTEXPR
326  void
327  _M_construct(_FwdIterator __beg, _FwdIterator __end,
329 
330  _GLIBCXX20_CONSTEXPR
331  void
332  _M_construct(size_type __req, _CharT __c);
333 
334  _GLIBCXX20_CONSTEXPR
335  allocator_type&
336  _M_get_allocator()
337  { return _M_dataplus; }
338 
339  _GLIBCXX20_CONSTEXPR
340  const allocator_type&
341  _M_get_allocator() const
342  { return _M_dataplus; }
343 
344  // Ensure that _M_local_buf is the active member of the union.
345  __attribute__((__always_inline__))
346  _GLIBCXX14_CONSTEXPR
347  void
348  _M_init_local_buf() _GLIBCXX_NOEXCEPT
349  {
350 #if __cpp_lib_is_constant_evaluated
352  for (size_type __i = 0; __i <= _S_local_capacity; ++__i)
353  _M_local_buf[__i] = _CharT();
354 #endif
355  }
356 
357  __attribute__((__always_inline__))
358  _GLIBCXX14_CONSTEXPR
359  pointer
360  _M_use_local_data() _GLIBCXX_NOEXCEPT
361  {
362 #if __cpp_lib_is_constant_evaluated
363  _M_init_local_buf();
364 #endif
365  return _M_local_data();
366  }
367 
368  private:
369 
370 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
371  // The explicit instantiations in misc-inst.cc require this due to
372  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
373  template<typename _Tp, bool _Requires =
374  !__are_same<_Tp, _CharT*>::__value
375  && !__are_same<_Tp, const _CharT*>::__value
376  && !__are_same<_Tp, iterator>::__value
377  && !__are_same<_Tp, const_iterator>::__value>
378  struct __enable_if_not_native_iterator
379  { typedef basic_string& __type; };
380  template<typename _Tp>
381  struct __enable_if_not_native_iterator<_Tp, false> { };
382 #endif
383 
384  _GLIBCXX20_CONSTEXPR
385  size_type
386  _M_check(size_type __pos, const char* __s) const
387  {
388  if (__pos > this->size())
389  __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
390  "this->size() (which is %zu)"),
391  __s, __pos, this->size());
392  return __pos;
393  }
394 
395  _GLIBCXX20_CONSTEXPR
396  void
397  _M_check_length(size_type __n1, size_type __n2, const char* __s) const
398  {
399  if (this->max_size() - (this->size() - __n1) < __n2)
400  __throw_length_error(__N(__s));
401  }
402 
403 
404  // NB: _M_limit doesn't check for a bad __pos value.
405  _GLIBCXX20_CONSTEXPR
406  size_type
407  _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
408  {
409  const bool __testoff = __off < this->size() - __pos;
410  return __testoff ? __off : this->size() - __pos;
411  }
412 
413  // True if _Rep and source do not overlap.
414  bool
415  _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
416  {
417  return (less<const _CharT*>()(__s, _M_data())
418  || less<const _CharT*>()(_M_data() + this->size(), __s));
419  }
420 
421  // When __n = 1 way faster than the general multichar
422  // traits_type::copy/move/assign.
423  _GLIBCXX20_CONSTEXPR
424  static void
425  _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
426  {
427  if (__n == 1)
428  traits_type::assign(*__d, *__s);
429  else
430  traits_type::copy(__d, __s, __n);
431  }
432 
433  _GLIBCXX20_CONSTEXPR
434  static void
435  _S_move(_CharT* __d, const _CharT* __s, size_type __n)
436  {
437  if (__n == 1)
438  traits_type::assign(*__d, *__s);
439  else
440  traits_type::move(__d, __s, __n);
441  }
442 
443  _GLIBCXX20_CONSTEXPR
444  static void
445  _S_assign(_CharT* __d, size_type __n, _CharT __c)
446  {
447  if (__n == 1)
448  traits_type::assign(*__d, __c);
449  else
450  traits_type::assign(__d, __n, __c);
451  }
452 
453  // _S_copy_chars is a separate template to permit specialization
454  // to optimize for the common case of pointers as iterators.
455  template<class _Iterator>
456  _GLIBCXX20_CONSTEXPR
457  static void
458  _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
459  {
460  for (; __k1 != __k2; ++__k1, (void)++__p)
461  traits_type::assign(*__p, *__k1); // These types are off.
462  }
463 
464  _GLIBCXX20_CONSTEXPR
465  static void
466  _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
467  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
468 
469  _GLIBCXX20_CONSTEXPR
470  static void
471  _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
472  _GLIBCXX_NOEXCEPT
473  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
474 
475  _GLIBCXX20_CONSTEXPR
476  static void
477  _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
478  { _S_copy(__p, __k1, __k2 - __k1); }
479 
480  _GLIBCXX20_CONSTEXPR
481  static void
482  _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
483  _GLIBCXX_NOEXCEPT
484  { _S_copy(__p, __k1, __k2 - __k1); }
485 
486  _GLIBCXX20_CONSTEXPR
487  static int
488  _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
489  {
490  const difference_type __d = difference_type(__n1 - __n2);
491 
492  if (__d > __gnu_cxx::__numeric_traits<int>::__max)
493  return __gnu_cxx::__numeric_traits<int>::__max;
494  else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
495  return __gnu_cxx::__numeric_traits<int>::__min;
496  else
497  return int(__d);
498  }
499 
500  _GLIBCXX20_CONSTEXPR
501  void
502  _M_assign(const basic_string&);
503 
504  _GLIBCXX20_CONSTEXPR
505  void
506  _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
507  size_type __len2);
508 
509  _GLIBCXX20_CONSTEXPR
510  void
511  _M_erase(size_type __pos, size_type __n);
512 
513  public:
514  // Construct/copy/destroy:
515  // NB: We overload ctors in some cases instead of using default
516  // arguments, per 17.4.4.4 para. 2 item 2.
517 
518  /**
519  * @brief Default constructor creates an empty string.
520  */
521  _GLIBCXX20_CONSTEXPR
522  basic_string()
523  _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
524  : _M_dataplus(_M_local_data())
525  {
526  _M_init_local_buf();
527  _M_set_length(0);
528  }
529 
530  /**
531  * @brief Construct an empty string using allocator @a a.
532  */
533  _GLIBCXX20_CONSTEXPR
534  explicit
535  basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
536  : _M_dataplus(_M_local_data(), __a)
537  {
538  _M_init_local_buf();
539  _M_set_length(0);
540  }
541 
542  /**
543  * @brief Construct string with copy of value of @a __str.
544  * @param __str Source string.
545  */
546  _GLIBCXX20_CONSTEXPR
547  basic_string(const basic_string& __str)
548  : _M_dataplus(_M_local_data(),
549  _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
550  {
551  _M_construct(__str._M_data(), __str._M_data() + __str.length(),
553  }
554 
555  // _GLIBCXX_RESOLVE_LIB_DEFECTS
556  // 2583. no way to supply an allocator for basic_string(str, pos)
557  /**
558  * @brief Construct string as copy of a substring.
559  * @param __str Source string.
560  * @param __pos Index of first character to copy from.
561  * @param __a Allocator to use.
562  */
563  _GLIBCXX20_CONSTEXPR
564  basic_string(const basic_string& __str, size_type __pos,
565  const _Alloc& __a = _Alloc())
566  : _M_dataplus(_M_local_data(), __a)
567  {
568  const _CharT* __start = __str._M_data()
569  + __str._M_check(__pos, "basic_string::basic_string");
570  _M_construct(__start, __start + __str._M_limit(__pos, npos),
572  }
573 
574  /**
575  * @brief Construct string as copy of a substring.
576  * @param __str Source string.
577  * @param __pos Index of first character to copy from.
578  * @param __n Number of characters to copy.
579  */
580  _GLIBCXX20_CONSTEXPR
581  basic_string(const basic_string& __str, size_type __pos,
582  size_type __n)
583  : _M_dataplus(_M_local_data())
584  {
585  const _CharT* __start = __str._M_data()
586  + __str._M_check(__pos, "basic_string::basic_string");
587  _M_construct(__start, __start + __str._M_limit(__pos, __n),
589  }
590 
591  /**
592  * @brief Construct string as copy of a substring.
593  * @param __str Source string.
594  * @param __pos Index of first character to copy from.
595  * @param __n Number of characters to copy.
596  * @param __a Allocator to use.
597  */
598  _GLIBCXX20_CONSTEXPR
599  basic_string(const basic_string& __str, size_type __pos,
600  size_type __n, const _Alloc& __a)
601  : _M_dataplus(_M_local_data(), __a)
602  {
603  const _CharT* __start
604  = __str._M_data() + __str._M_check(__pos, "string::string");
605  _M_construct(__start, __start + __str._M_limit(__pos, __n),
607  }
608 
609  /**
610  * @brief Construct string initialized by a character %array.
611  * @param __s Source character %array.
612  * @param __n Number of characters to copy.
613  * @param __a Allocator to use (default is default allocator).
614  *
615  * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
616  * has no special meaning.
617  */
618  _GLIBCXX20_CONSTEXPR
619  basic_string(const _CharT* __s, size_type __n,
620  const _Alloc& __a = _Alloc())
621  : _M_dataplus(_M_local_data(), __a)
622  {
623  // NB: Not required, but considered best practice.
624  if (__s == 0 && __n > 0)
625  std::__throw_logic_error(__N("basic_string: "
626  "construction from null is not valid"));
627  _M_construct(__s, __s + __n, std::forward_iterator_tag());
628  }
629 
630  /**
631  * @brief Construct string as copy of a C string.
632  * @param __s Source C string.
633  * @param __a Allocator to use (default is default allocator).
634  */
635 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
636  // _GLIBCXX_RESOLVE_LIB_DEFECTS
637  // 3076. basic_string CTAD ambiguity
638  template<typename = _RequireAllocator<_Alloc>>
639 #endif
640  _GLIBCXX20_CONSTEXPR
641  basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
642  : _M_dataplus(_M_local_data(), __a)
643  {
644  // NB: Not required, but considered best practice.
645  if (__s == 0)
646  std::__throw_logic_error(__N("basic_string: "
647  "construction from null is not valid"));
648  const _CharT* __end = __s + traits_type::length(__s);
649  _M_construct(__s, __end, forward_iterator_tag());
650  }
651 
652  /**
653  * @brief Construct string as multiple characters.
654  * @param __n Number of characters.
655  * @param __c Character to use.
656  * @param __a Allocator to use (default is default allocator).
657  */
658 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
659  // _GLIBCXX_RESOLVE_LIB_DEFECTS
660  // 3076. basic_string CTAD ambiguity
661  template<typename = _RequireAllocator<_Alloc>>
662 #endif
663  _GLIBCXX20_CONSTEXPR
664  basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
665  : _M_dataplus(_M_local_data(), __a)
666  { _M_construct(__n, __c); }
667 
668 #if __cplusplus >= 201103L
669  /**
670  * @brief Move construct string.
671  * @param __str Source string.
672  *
673  * The newly-created string contains the exact contents of @a __str.
674  * @a __str is a valid, but unspecified string.
675  */
676  _GLIBCXX20_CONSTEXPR
677  basic_string(basic_string&& __str) noexcept
678  : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
679  {
680  if (__str._M_is_local())
681  {
682  _M_init_local_buf();
683  traits_type::copy(_M_local_buf, __str._M_local_buf,
684  __str.length() + 1);
685  }
686  else
687  {
688  _M_data(__str._M_data());
689  _M_capacity(__str._M_allocated_capacity);
690  }
691 
692  // Must use _M_length() here not _M_set_length() because
693  // basic_stringbuf relies on writing into unallocated capacity so
694  // we mess up the contents if we put a '\0' in the string.
695  _M_length(__str.length());
696  __str._M_data(__str._M_use_local_data());
697  __str._M_set_length(0);
698  }
699 
700  /**
701  * @brief Construct string from an initializer %list.
702  * @param __l std::initializer_list of characters.
703  * @param __a Allocator to use (default is default allocator).
704  */
705  _GLIBCXX20_CONSTEXPR
706  basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
707  : _M_dataplus(_M_local_data(), __a)
708  { _M_construct(__l.begin(), __l.end(), std::forward_iterator_tag()); }
709 
710  _GLIBCXX20_CONSTEXPR
711  basic_string(const basic_string& __str, const _Alloc& __a)
712  : _M_dataplus(_M_local_data(), __a)
713  { _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); }
714 
715  _GLIBCXX20_CONSTEXPR
716  basic_string(basic_string&& __str, const _Alloc& __a)
717  noexcept(_Alloc_traits::_S_always_equal())
718  : _M_dataplus(_M_local_data(), __a)
719  {
720  if (__str._M_is_local())
721  {
722  _M_init_local_buf();
723  traits_type::copy(_M_local_buf, __str._M_local_buf,
724  __str.length() + 1);
725  _M_length(__str.length());
726  __str._M_set_length(0);
727  }
728  else if (_Alloc_traits::_S_always_equal()
729  || __str.get_allocator() == __a)
730  {
731  _M_data(__str._M_data());
732  _M_length(__str.length());
733  _M_capacity(__str._M_allocated_capacity);
734  __str._M_data(__str._M_use_local_data());
735  __str._M_set_length(0);
736  }
737  else
738  _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag());
739  }
740 #endif // C++11
741 
742 #if __cplusplus >= 202100L
743  basic_string(nullptr_t) = delete;
744  basic_string& operator=(nullptr_t) = delete;
745 #endif // C++23
746 
747  /**
748  * @brief Construct string as copy of a range.
749  * @param __beg Start of range.
750  * @param __end End of range.
751  * @param __a Allocator to use (default is default allocator).
752  */
753 #if __cplusplus >= 201103L
754  template<typename _InputIterator,
755  typename = std::_RequireInputIter<_InputIterator>>
756 #else
757  template<typename _InputIterator>
758 #endif
759  _GLIBCXX20_CONSTEXPR
760  basic_string(_InputIterator __beg, _InputIterator __end,
761  const _Alloc& __a = _Alloc())
762  : _M_dataplus(_M_local_data(), __a), _M_string_length(0)
763  {
764 #if __cplusplus >= 201103L
765  _M_construct(__beg, __end, std::__iterator_category(__beg));
766 #else
767  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
768  _M_construct_aux(__beg, __end, _Integral());
769 #endif
770  }
771 
772 #if __cplusplus >= 201703L
773  /**
774  * @brief Construct string from a substring of a string_view.
775  * @param __t Source object convertible to string view.
776  * @param __pos The index of the first character to copy from __t.
777  * @param __n The number of characters to copy from __t.
778  * @param __a Allocator to use.
779  */
780  template<typename _Tp,
781  typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>>
782  _GLIBCXX20_CONSTEXPR
783  basic_string(const _Tp& __t, size_type __pos, size_type __n,
784  const _Alloc& __a = _Alloc())
785  : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
786 
787  /**
788  * @brief Construct string from a string_view.
789  * @param __t Source object convertible to string view.
790  * @param __a Allocator to use (default is default allocator).
791  */
792  template<typename _Tp, typename = _If_sv<_Tp, void>>
793  _GLIBCXX20_CONSTEXPR
794  explicit
795  basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
796  : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
797 #endif // C++17
798 
799  /**
800  * @brief Destroy the string instance.
801  */
802  _GLIBCXX20_CONSTEXPR
803  ~basic_string()
804  { _M_dispose(); }
805 
806  /**
807  * @brief Assign the value of @a str to this string.
808  * @param __str Source string.
809  */
810  _GLIBCXX20_CONSTEXPR
811  basic_string&
812  operator=(const basic_string& __str)
813  {
814  return this->assign(__str);
815  }
816 
817  /**
818  * @brief Copy contents of @a s into this string.
819  * @param __s Source null-terminated string.
820  */
821  _GLIBCXX20_CONSTEXPR
822  basic_string&
823  operator=(const _CharT* __s)
824  { return this->assign(__s); }
825 
826  /**
827  * @brief Set value to string of length 1.
828  * @param __c Source character.
829  *
830  * Assigning to a character makes this string length 1 and
831  * (*this)[0] == @a c.
832  */
833  _GLIBCXX20_CONSTEXPR
834  basic_string&
835  operator=(_CharT __c)
836  {
837  this->assign(1, __c);
838  return *this;
839  }
840 
841 #if __cplusplus >= 201103L
842  /**
843  * @brief Move assign the value of @a str to this string.
844  * @param __str Source string.
845  *
846  * The contents of @a str are moved into this string (without copying).
847  * @a str is a valid, but unspecified string.
848  */
849  // _GLIBCXX_RESOLVE_LIB_DEFECTS
850  // 2063. Contradictory requirements for string move assignment
851  _GLIBCXX20_CONSTEXPR
852  basic_string&
853  operator=(basic_string&& __str)
854  noexcept(_Alloc_traits::_S_nothrow_move())
855  {
856  const bool __equal_allocs = _Alloc_traits::_S_always_equal()
857  || _M_get_allocator() == __str._M_get_allocator();
858  if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
859  && !__equal_allocs)
860  {
861  // Destroy existing storage before replacing allocator.
862  _M_destroy(_M_allocated_capacity);
863  _M_data(_M_local_data());
864  _M_set_length(0);
865  }
866  // Replace allocator if POCMA is true.
867  std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
868 
869  if (__str._M_is_local())
870  {
871  // We've always got room for a short string, just copy it
872  // (unless this is a self-move, because that would violate the
873  // char_traits::copy precondition that the ranges don't overlap).
874  if (__builtin_expect(std::__addressof(__str) != this, true))
875  {
876  if (__str.size())
877  this->_S_copy(_M_data(), __str._M_data(), __str.size());
878  _M_set_length(__str.size());
879  }
880  }
881  else if (_Alloc_traits::_S_propagate_on_move_assign() || __equal_allocs)
882  {
883  // Just move the allocated pointer, our allocator can free it.
884  pointer __data = nullptr;
885  size_type __capacity;
886  if (!_M_is_local())
887  {
888  if (__equal_allocs)
889  {
890  // __str can reuse our existing storage.
891  __data = _M_data();
892  __capacity = _M_allocated_capacity;
893  }
894  else // __str can't use it, so free it.
895  _M_destroy(_M_allocated_capacity);
896  }
897 
898  _M_data(__str._M_data());
899  _M_length(__str.length());
900  _M_capacity(__str._M_allocated_capacity);
901  if (__data)
902  {
903  __str._M_data(__data);
904  __str._M_capacity(__capacity);
905  }
906  else
907  __str._M_data(__str._M_local_buf);
908  }
909  else // Need to do a deep copy
910  assign(__str);
911  __str.clear();
912  return *this;
913  }
914 
915  /**
916  * @brief Set value to string constructed from initializer %list.
917  * @param __l std::initializer_list.
918  */
919  _GLIBCXX20_CONSTEXPR
920  basic_string&
921  operator=(initializer_list<_CharT> __l)
922  {
923  this->assign(__l.begin(), __l.size());
924  return *this;
925  }
926 #endif // C++11
927 
928 #if __cplusplus >= 201703L
929  /**
930  * @brief Set value to string constructed from a string_view.
931  * @param __svt An object convertible to string_view.
932  */
933  template<typename _Tp>
934  _GLIBCXX20_CONSTEXPR
935  _If_sv<_Tp, basic_string&>
936  operator=(const _Tp& __svt)
937  { return this->assign(__svt); }
938 
939  /**
940  * @brief Convert to a string_view.
941  * @return A string_view.
942  */
943  _GLIBCXX20_CONSTEXPR
944  operator __sv_type() const noexcept
945  { return __sv_type(data(), size()); }
946 #endif // C++17
947 
948  // Iterators:
949  /**
950  * Returns a read/write iterator that points to the first character in
951  * the %string.
952  */
953  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
954  iterator
955  begin() _GLIBCXX_NOEXCEPT
956  { return iterator(_M_data()); }
957 
958  /**
959  * Returns a read-only (constant) iterator that points to the first
960  * character in the %string.
961  */
962  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
963  const_iterator
964  begin() const _GLIBCXX_NOEXCEPT
965  { return const_iterator(_M_data()); }
966 
967  /**
968  * Returns a read/write iterator that points one past the last
969  * character in the %string.
970  */
971  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
972  iterator
973  end() _GLIBCXX_NOEXCEPT
974  { return iterator(_M_data() + this->size()); }
975 
976  /**
977  * Returns a read-only (constant) iterator that points one past the
978  * last character in the %string.
979  */
980  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
981  const_iterator
982  end() const _GLIBCXX_NOEXCEPT
983  { return const_iterator(_M_data() + this->size()); }
984 
985  /**
986  * Returns a read/write reverse iterator that points to the last
987  * character in the %string. Iteration is done in reverse element
988  * order.
989  */
990  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
991  reverse_iterator
992  rbegin() _GLIBCXX_NOEXCEPT
993  { return reverse_iterator(this->end()); }
994 
995  /**
996  * Returns a read-only (constant) reverse iterator that points
997  * to the last character in the %string. Iteration is done in
998  * reverse element order.
999  */
1000  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1001  const_reverse_iterator
1002  rbegin() const _GLIBCXX_NOEXCEPT
1003  { return const_reverse_iterator(this->end()); }
1004 
1005  /**
1006  * Returns a read/write reverse iterator that points to one before the
1007  * first character in the %string. Iteration is done in reverse
1008  * element order.
1009  */
1010  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1011  reverse_iterator
1012  rend() _GLIBCXX_NOEXCEPT
1013  { return reverse_iterator(this->begin()); }
1014 
1015  /**
1016  * Returns a read-only (constant) reverse iterator that points
1017  * to one before the first character in the %string. Iteration
1018  * is done in reverse element order.
1019  */
1020  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1021  const_reverse_iterator
1022  rend() const _GLIBCXX_NOEXCEPT
1023  { return const_reverse_iterator(this->begin()); }
1024 
1025 #if __cplusplus >= 201103L
1026  /**
1027  * Returns a read-only (constant) iterator that points to the first
1028  * character in the %string.
1029  */
1030  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1031  const_iterator
1032  cbegin() const noexcept
1033  { return const_iterator(this->_M_data()); }
1034 
1035  /**
1036  * Returns a read-only (constant) iterator that points one past the
1037  * last character in the %string.
1038  */
1039  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1040  const_iterator
1041  cend() const noexcept
1042  { return const_iterator(this->_M_data() + this->size()); }
1043 
1044  /**
1045  * Returns a read-only (constant) reverse iterator that points
1046  * to the last character in the %string. Iteration is done in
1047  * reverse element order.
1048  */
1049  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1050  const_reverse_iterator
1051  crbegin() const noexcept
1052  { return const_reverse_iterator(this->end()); }
1053 
1054  /**
1055  * Returns a read-only (constant) reverse iterator that points
1056  * to one before the first character in the %string. Iteration
1057  * is done in reverse element order.
1058  */
1059  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1060  const_reverse_iterator
1061  crend() const noexcept
1062  { return const_reverse_iterator(this->begin()); }
1063 #endif
1064 
1065  public:
1066  // Capacity:
1067  /// Returns the number of characters in the string, not including any
1068  /// null-termination.
1069  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1070  size_type
1071  size() const _GLIBCXX_NOEXCEPT
1072  { return _M_string_length; }
1073 
1074  /// Returns the number of characters in the string, not including any
1075  /// null-termination.
1076  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1077  size_type
1078  length() const _GLIBCXX_NOEXCEPT
1079  { return _M_string_length; }
1080 
1081  /// Returns the size() of the largest possible %string.
1082  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1083  size_type
1084  max_size() const _GLIBCXX_NOEXCEPT
1085  { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
1086 
1087  /**
1088  * @brief Resizes the %string to the specified number of characters.
1089  * @param __n Number of characters the %string should contain.
1090  * @param __c Character to fill any new elements.
1091  *
1092  * This function will %resize the %string to the specified
1093  * number of characters. If the number is smaller than the
1094  * %string's current size the %string is truncated, otherwise
1095  * the %string is extended and new elements are %set to @a __c.
1096  */
1097  _GLIBCXX20_CONSTEXPR
1098  void
1099  resize(size_type __n, _CharT __c);
1100 
1101  /**
1102  * @brief Resizes the %string to the specified number of characters.
1103  * @param __n Number of characters the %string should contain.
1104  *
1105  * This function will resize the %string to the specified length. If
1106  * the new size is smaller than the %string's current size the %string
1107  * is truncated, otherwise the %string is extended and new characters
1108  * are default-constructed. For basic types such as char, this means
1109  * setting them to 0.
1110  */
1111  _GLIBCXX20_CONSTEXPR
1112  void
1113  resize(size_type __n)
1114  { this->resize(__n, _CharT()); }
1115 
1116 #if __cplusplus >= 201103L
1117 #pragma GCC diagnostic push
1118 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1119  /// A non-binding request to reduce capacity() to size().
1120  _GLIBCXX20_CONSTEXPR
1121  void
1122  shrink_to_fit() noexcept
1123  { reserve(); }
1124 #pragma GCC diagnostic pop
1125 #endif
1126 
1127 #if __cplusplus > 202002L
1128 #define __cpp_lib_string_resize_and_overwrite 202110L
1129  /** Resize the string and call a function to fill it.
1130  *
1131  * @param __n The maximum size requested.
1132  * @param __op A callable object that writes characters to the string.
1133  *
1134  * This is a low-level function that is easy to misuse, be careful.
1135  *
1136  * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n`
1137  * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`,
1138  * and finally set the string length to `n2` (adding a null terminator
1139  * at the end). The function object `op` is allowed to write to the
1140  * extra capacity added by the initial reserve operation, which is not
1141  * allowed if you just call `str.reserve(n)` yourself.
1142  *
1143  * This can be used to efficiently fill a `string` buffer without the
1144  * overhead of zero-initializing characters that will be overwritten
1145  * anyway.
1146  *
1147  * The callable `op` must not access the string directly (only through
1148  * the pointer passed as its first argument), must not write more than
1149  * `n` characters to the string, must return a value no greater than `n`,
1150  * and must ensure that all characters up to the returned length are
1151  * valid after it returns (i.e. there must be no uninitialized values
1152  * left in the string after the call, because accessing them would
1153  * have undefined behaviour). If `op` exits by throwing an exception
1154  * the behaviour is undefined.
1155  *
1156  * @since C++23
1157  */
1158  template<typename _Operation>
1159  constexpr void
1160  resize_and_overwrite(size_type __n, _Operation __op);
1161 #endif
1162 
1163  /**
1164  * Returns the total number of characters that the %string can hold
1165  * before needing to allocate more memory.
1166  */
1167  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1168  size_type
1169  capacity() const _GLIBCXX_NOEXCEPT
1170  {
1171  return _M_is_local() ? size_type(_S_local_capacity)
1172  : _M_allocated_capacity;
1173  }
1174 
1175  /**
1176  * @brief Attempt to preallocate enough memory for specified number of
1177  * characters.
1178  * @param __res_arg Number of characters required.
1179  * @throw std::length_error If @a __res_arg exceeds @c max_size().
1180  *
1181  * This function attempts to reserve enough memory for the
1182  * %string to hold the specified number of characters. If the
1183  * number requested is more than max_size(), length_error is
1184  * thrown.
1185  *
1186  * The advantage of this function is that if optimal code is a
1187  * necessity and the user can determine the string length that will be
1188  * required, the user can reserve the memory in %advance, and thus
1189  * prevent a possible reallocation of memory and copying of %string
1190  * data.
1191  */
1192  _GLIBCXX20_CONSTEXPR
1193  void
1194  reserve(size_type __res_arg);
1195 
1196  /**
1197  * Equivalent to shrink_to_fit().
1198  */
1199 #if __cplusplus > 201703L
1200  [[deprecated("use shrink_to_fit() instead")]]
1201 #endif
1202  _GLIBCXX20_CONSTEXPR
1203  void
1204  reserve();
1205 
1206  /**
1207  * Erases the string, making it empty.
1208  */
1209  _GLIBCXX20_CONSTEXPR
1210  void
1211  clear() _GLIBCXX_NOEXCEPT
1212  { _M_set_length(0); }
1213 
1214  /**
1215  * Returns true if the %string is empty. Equivalent to
1216  * <code>*this == ""</code>.
1217  */
1218  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1219  bool
1220  empty() const _GLIBCXX_NOEXCEPT
1221  { return this->size() == 0; }
1222 
1223  // Element access:
1224  /**
1225  * @brief Subscript access to the data contained in the %string.
1226  * @param __pos The index of the character to access.
1227  * @return Read-only (constant) reference to the character.
1228  *
1229  * This operator allows for easy, array-style, data access.
1230  * Note that data access with this operator is unchecked and
1231  * out_of_range lookups are not defined. (For checked lookups
1232  * see at().)
1233  */
1234  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1235  const_reference
1236  operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
1237  {
1238  __glibcxx_assert(__pos <= size());
1239  return _M_data()[__pos];
1240  }
1241 
1242  /**
1243  * @brief Subscript access to the data contained in the %string.
1244  * @param __pos The index of the character to access.
1245  * @return Read/write reference to the character.
1246  *
1247  * This operator allows for easy, array-style, data access.
1248  * Note that data access with this operator is unchecked and
1249  * out_of_range lookups are not defined. (For checked lookups
1250  * see at().)
1251  */
1252  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1253  reference
1254  operator[](size_type __pos)
1255  {
1256  // Allow pos == size() both in C++98 mode, as v3 extension,
1257  // and in C++11 mode.
1258  __glibcxx_assert(__pos <= size());
1259  // In pedantic mode be strict in C++98 mode.
1260  _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1261  return _M_data()[__pos];
1262  }
1263 
1264  /**
1265  * @brief Provides access to the data contained in the %string.
1266  * @param __n The index of the character to access.
1267  * @return Read-only (const) reference to the character.
1268  * @throw std::out_of_range If @a n is an invalid index.
1269  *
1270  * This function provides for safer data access. The parameter is
1271  * first checked that it is in the range of the string. The function
1272  * throws out_of_range if the check fails.
1273  */
1274  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1275  const_reference
1276  at(size_type __n) const
1277  {
1278  if (__n >= this->size())
1279  __throw_out_of_range_fmt(__N("basic_string::at: __n "
1280  "(which is %zu) >= this->size() "
1281  "(which is %zu)"),
1282  __n, this->size());
1283  return _M_data()[__n];
1284  }
1285 
1286  /**
1287  * @brief Provides access to the data contained in the %string.
1288  * @param __n The index of the character to access.
1289  * @return Read/write reference to the character.
1290  * @throw std::out_of_range If @a n is an invalid index.
1291  *
1292  * This function provides for safer data access. The parameter is
1293  * first checked that it is in the range of the string. The function
1294  * throws out_of_range if the check fails.
1295  */
1296  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1297  reference
1298  at(size_type __n)
1299  {
1300  if (__n >= size())
1301  __throw_out_of_range_fmt(__N("basic_string::at: __n "
1302  "(which is %zu) >= this->size() "
1303  "(which is %zu)"),
1304  __n, this->size());
1305  return _M_data()[__n];
1306  }
1307 
1308 #if __cplusplus >= 201103L
1309  /**
1310  * Returns a read/write reference to the data at the first
1311  * element of the %string.
1312  */
1313  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1314  reference
1315  front() noexcept
1316  {
1317  __glibcxx_assert(!empty());
1318  return operator[](0);
1319  }
1320 
1321  /**
1322  * Returns a read-only (constant) reference to the data at the first
1323  * element of the %string.
1324  */
1325  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1326  const_reference
1327  front() const noexcept
1328  {
1329  __glibcxx_assert(!empty());
1330  return operator[](0);
1331  }
1332 
1333  /**
1334  * Returns a read/write reference to the data at the last
1335  * element of the %string.
1336  */
1337  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1338  reference
1339  back() noexcept
1340  {
1341  __glibcxx_assert(!empty());
1342  return operator[](this->size() - 1);
1343  }
1344 
1345  /**
1346  * Returns a read-only (constant) reference to the data at the
1347  * last element of the %string.
1348  */
1349  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1350  const_reference
1351  back() const noexcept
1352  {
1353  __glibcxx_assert(!empty());
1354  return operator[](this->size() - 1);
1355  }
1356 #endif
1357 
1358  // Modifiers:
1359  /**
1360  * @brief Append a string to this string.
1361  * @param __str The string to append.
1362  * @return Reference to this string.
1363  */
1364  _GLIBCXX20_CONSTEXPR
1365  basic_string&
1366  operator+=(const basic_string& __str)
1367  { return this->append(__str); }
1368 
1369  /**
1370  * @brief Append a C string.
1371  * @param __s The C string to append.
1372  * @return Reference to this string.
1373  */
1374  _GLIBCXX20_CONSTEXPR
1375  basic_string&
1376  operator+=(const _CharT* __s)
1377  { return this->append(__s); }
1378 
1379  /**
1380  * @brief Append a character.
1381  * @param __c The character to append.
1382  * @return Reference to this string.
1383  */
1384  _GLIBCXX20_CONSTEXPR
1385  basic_string&
1386  operator+=(_CharT __c)
1387  {
1388  this->push_back(__c);
1389  return *this;
1390  }
1391 
1392 #if __cplusplus >= 201103L
1393  /**
1394  * @brief Append an initializer_list of characters.
1395  * @param __l The initializer_list of characters to be appended.
1396  * @return Reference to this string.
1397  */
1398  _GLIBCXX20_CONSTEXPR
1399  basic_string&
1400  operator+=(initializer_list<_CharT> __l)
1401  { return this->append(__l.begin(), __l.size()); }
1402 #endif // C++11
1403 
1404 #if __cplusplus >= 201703L
1405  /**
1406  * @brief Append a string_view.
1407  * @param __svt An object convertible to string_view to be appended.
1408  * @return Reference to this string.
1409  */
1410  template<typename _Tp>
1411  _GLIBCXX20_CONSTEXPR
1412  _If_sv<_Tp, basic_string&>
1413  operator+=(const _Tp& __svt)
1414  { return this->append(__svt); }
1415 #endif // C++17
1416 
1417  /**
1418  * @brief Append a string to this string.
1419  * @param __str The string to append.
1420  * @return Reference to this string.
1421  */
1422  _GLIBCXX20_CONSTEXPR
1423  basic_string&
1424  append(const basic_string& __str)
1425  { return this->append(__str._M_data(), __str.size()); }
1426 
1427  /**
1428  * @brief Append a substring.
1429  * @param __str The string to append.
1430  * @param __pos Index of the first character of str to append.
1431  * @param __n The number of characters to append.
1432  * @return Reference to this string.
1433  * @throw std::out_of_range if @a __pos is not a valid index.
1434  *
1435  * This function appends @a __n characters from @a __str
1436  * starting at @a __pos to this string. If @a __n is is larger
1437  * than the number of available characters in @a __str, the
1438  * remainder of @a __str is appended.
1439  */
1440  _GLIBCXX20_CONSTEXPR
1441  basic_string&
1442  append(const basic_string& __str, size_type __pos, size_type __n = npos)
1443  { return this->append(__str._M_data()
1444  + __str._M_check(__pos, "basic_string::append"),
1445  __str._M_limit(__pos, __n)); }
1446 
1447  /**
1448  * @brief Append a C substring.
1449  * @param __s The C string to append.
1450  * @param __n The number of characters to append.
1451  * @return Reference to this string.
1452  */
1453  _GLIBCXX20_CONSTEXPR
1454  basic_string&
1455  append(const _CharT* __s, size_type __n)
1456  {
1457  __glibcxx_requires_string_len(__s, __n);
1458  _M_check_length(size_type(0), __n, "basic_string::append");
1459  return _M_append(__s, __n);
1460  }
1461 
1462  /**
1463  * @brief Append a C string.
1464  * @param __s The C string to append.
1465  * @return Reference to this string.
1466  */
1467  _GLIBCXX20_CONSTEXPR
1468  basic_string&
1469  append(const _CharT* __s)
1470  {
1471  __glibcxx_requires_string(__s);
1472  const size_type __n = traits_type::length(__s);
1473  _M_check_length(size_type(0), __n, "basic_string::append");
1474  return _M_append(__s, __n);
1475  }
1476 
1477  /**
1478  * @brief Append multiple characters.
1479  * @param __n The number of characters to append.
1480  * @param __c The character to use.
1481  * @return Reference to this string.
1482  *
1483  * Appends __n copies of __c to this string.
1484  */
1485  _GLIBCXX20_CONSTEXPR
1486  basic_string&
1487  append(size_type __n, _CharT __c)
1488  { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
1489 
1490 #if __cplusplus >= 201103L
1491  /**
1492  * @brief Append an initializer_list of characters.
1493  * @param __l The initializer_list of characters to append.
1494  * @return Reference to this string.
1495  */
1496  _GLIBCXX20_CONSTEXPR
1497  basic_string&
1498  append(initializer_list<_CharT> __l)
1499  { return this->append(__l.begin(), __l.size()); }
1500 #endif // C++11
1501 
1502  /**
1503  * @brief Append a range of characters.
1504  * @param __first Iterator referencing the first character to append.
1505  * @param __last Iterator marking the end of the range.
1506  * @return Reference to this string.
1507  *
1508  * Appends characters in the range [__first,__last) to this string.
1509  */
1510 #if __cplusplus >= 201103L
1511  template<class _InputIterator,
1512  typename = std::_RequireInputIter<_InputIterator>>
1513  _GLIBCXX20_CONSTEXPR
1514 #else
1515  template<class _InputIterator>
1516 #endif
1517  basic_string&
1518  append(_InputIterator __first, _InputIterator __last)
1519  { return this->replace(end(), end(), __first, __last); }
1520 
1521 #if __cplusplus >= 201703L
1522  /**
1523  * @brief Append a string_view.
1524  * @param __svt An object convertible to string_view to be appended.
1525  * @return Reference to this string.
1526  */
1527  template<typename _Tp>
1528  _GLIBCXX20_CONSTEXPR
1529  _If_sv<_Tp, basic_string&>
1530  append(const _Tp& __svt)
1531  {
1532  __sv_type __sv = __svt;
1533  return this->append(__sv.data(), __sv.size());
1534  }
1535 
1536  /**
1537  * @brief Append a range of characters from a string_view.
1538  * @param __svt An object convertible to string_view to be appended from.
1539  * @param __pos The position in the string_view to append from.
1540  * @param __n The number of characters to append from the string_view.
1541  * @return Reference to this string.
1542  */
1543  template<typename _Tp>
1544  _GLIBCXX20_CONSTEXPR
1545  _If_sv<_Tp, basic_string&>
1546  append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1547  {
1548  __sv_type __sv = __svt;
1549  return _M_append(__sv.data()
1550  + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
1551  std::__sv_limit(__sv.size(), __pos, __n));
1552  }
1553 #endif // C++17
1554 
1555  /**
1556  * @brief Append a single character.
1557  * @param __c Character to append.
1558  */
1559  _GLIBCXX20_CONSTEXPR
1560  void
1561  push_back(_CharT __c)
1562  {
1563  const size_type __size = this->size();
1564  if (__size + 1 > this->capacity())
1565  this->_M_mutate(__size, size_type(0), 0, size_type(1));
1566  traits_type::assign(this->_M_data()[__size], __c);
1567  this->_M_set_length(__size + 1);
1568  }
1569 
1570  /**
1571  * @brief Set value to contents of another string.
1572  * @param __str Source string to use.
1573  * @return Reference to this string.
1574  */
1575  _GLIBCXX20_CONSTEXPR
1576  basic_string&
1577  assign(const basic_string& __str)
1578  {
1579 #if __cplusplus >= 201103L
1580  if (_Alloc_traits::_S_propagate_on_copy_assign())
1581  {
1582  if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
1583  && _M_get_allocator() != __str._M_get_allocator())
1584  {
1585  // Propagating allocator cannot free existing storage so must
1586  // deallocate it before replacing current allocator.
1587  if (__str.size() <= _S_local_capacity)
1588  {
1589  _M_destroy(_M_allocated_capacity);
1590  _M_data(_M_use_local_data());
1591  _M_set_length(0);
1592  }
1593  else
1594  {
1595  const auto __len = __str.size();
1596  auto __alloc = __str._M_get_allocator();
1597  // If this allocation throws there are no effects:
1598  auto __ptr = _S_allocate(__alloc, __len + 1);
1599  _M_destroy(_M_allocated_capacity);
1600  _M_data(__ptr);
1601  _M_capacity(__len);
1602  _M_set_length(__len);
1603  }
1604  }
1605  std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
1606  }
1607 #endif
1608  this->_M_assign(__str);
1609  return *this;
1610  }
1611 
1612 #if __cplusplus >= 201103L
1613  /**
1614  * @brief Set value to contents of another string.
1615  * @param __str Source string to use.
1616  * @return Reference to this string.
1617  *
1618  * This function sets this string to the exact contents of @a __str.
1619  * @a __str is a valid, but unspecified string.
1620  */
1621  _GLIBCXX20_CONSTEXPR
1622  basic_string&
1623  assign(basic_string&& __str)
1624  noexcept(_Alloc_traits::_S_nothrow_move())
1625  {
1626  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1627  // 2063. Contradictory requirements for string move assignment
1628  return *this = std::move(__str);
1629  }
1630 #endif // C++11
1631 
1632  /**
1633  * @brief Set value to a substring of a string.
1634  * @param __str The string to use.
1635  * @param __pos Index of the first character of str.
1636  * @param __n Number of characters to use.
1637  * @return Reference to this string.
1638  * @throw std::out_of_range if @a pos is not a valid index.
1639  *
1640  * This function sets this string to the substring of @a __str
1641  * consisting of @a __n characters at @a __pos. If @a __n is
1642  * is larger than the number of available characters in @a
1643  * __str, the remainder of @a __str is used.
1644  */
1645  _GLIBCXX20_CONSTEXPR
1646  basic_string&
1647  assign(const basic_string& __str, size_type __pos, size_type __n = npos)
1648  { return _M_replace(size_type(0), this->size(), __str._M_data()
1649  + __str._M_check(__pos, "basic_string::assign"),
1650  __str._M_limit(__pos, __n)); }
1651 
1652  /**
1653  * @brief Set value to a C substring.
1654  * @param __s The C string to use.
1655  * @param __n Number of characters to use.
1656  * @return Reference to this string.
1657  *
1658  * This function sets the value of this string to the first @a __n
1659  * characters of @a __s. If @a __n is is larger than the number of
1660  * available characters in @a __s, the remainder of @a __s is used.
1661  */
1662  _GLIBCXX20_CONSTEXPR
1663  basic_string&
1664  assign(const _CharT* __s, size_type __n)
1665  {
1666  __glibcxx_requires_string_len(__s, __n);
1667  return _M_replace(size_type(0), this->size(), __s, __n);
1668  }
1669 
1670  /**
1671  * @brief Set value to contents of a C string.
1672  * @param __s The C string to use.
1673  * @return Reference to this string.
1674  *
1675  * This function sets the value of this string to the value of @a __s.
1676  * The data is copied, so there is no dependence on @a __s once the
1677  * function returns.
1678  */
1679  _GLIBCXX20_CONSTEXPR
1680  basic_string&
1681  assign(const _CharT* __s)
1682  {
1683  __glibcxx_requires_string(__s);
1684  return _M_replace(size_type(0), this->size(), __s,
1685  traits_type::length(__s));
1686  }
1687 
1688  /**
1689  * @brief Set value to multiple characters.
1690  * @param __n Length of the resulting string.
1691  * @param __c The character to use.
1692  * @return Reference to this string.
1693  *
1694  * This function sets the value of this string to @a __n copies of
1695  * character @a __c.
1696  */
1697  _GLIBCXX20_CONSTEXPR
1698  basic_string&
1699  assign(size_type __n, _CharT __c)
1700  { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1701 
1702  /**
1703  * @brief Set value to a range of characters.
1704  * @param __first Iterator referencing the first character to append.
1705  * @param __last Iterator marking the end of the range.
1706  * @return Reference to this string.
1707  *
1708  * Sets value of string to characters in the range [__first,__last).
1709  */
1710 #if __cplusplus >= 201103L
1711  template<class _InputIterator,
1712  typename = std::_RequireInputIter<_InputIterator>>
1713  _GLIBCXX20_CONSTEXPR
1714 #else
1715  template<class _InputIterator>
1716 #endif
1717  basic_string&
1718  assign(_InputIterator __first, _InputIterator __last)
1719  { return this->replace(begin(), end(), __first, __last); }
1720 
1721 #if __cplusplus >= 201103L
1722  /**
1723  * @brief Set value to an initializer_list of characters.
1724  * @param __l The initializer_list of characters to assign.
1725  * @return Reference to this string.
1726  */
1727  _GLIBCXX20_CONSTEXPR
1728  basic_string&
1729  assign(initializer_list<_CharT> __l)
1730  { return this->assign(__l.begin(), __l.size()); }
1731 #endif // C++11
1732 
1733 #if __cplusplus >= 201703L
1734  /**
1735  * @brief Set value from a string_view.
1736  * @param __svt The source object convertible to string_view.
1737  * @return Reference to this string.
1738  */
1739  template<typename _Tp>
1740  _GLIBCXX20_CONSTEXPR
1741  _If_sv<_Tp, basic_string&>
1742  assign(const _Tp& __svt)
1743  {
1744  __sv_type __sv = __svt;
1745  return this->assign(__sv.data(), __sv.size());
1746  }
1747 
1748  /**
1749  * @brief Set value from a range of characters in a string_view.
1750  * @param __svt The source object convertible to string_view.
1751  * @param __pos The position in the string_view to assign from.
1752  * @param __n The number of characters to assign.
1753  * @return Reference to this string.
1754  */
1755  template<typename _Tp>
1756  _GLIBCXX20_CONSTEXPR
1757  _If_sv<_Tp, basic_string&>
1758  assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
1759  {
1760  __sv_type __sv = __svt;
1761  return _M_replace(size_type(0), this->size(),
1762  __sv.data()
1763  + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
1764  std::__sv_limit(__sv.size(), __pos, __n));
1765  }
1766 #endif // C++17
1767 
1768 #if __cplusplus >= 201103L
1769  /**
1770  * @brief Insert multiple characters.
1771  * @param __p Const_iterator referencing location in string to
1772  * insert at.
1773  * @param __n Number of characters to insert
1774  * @param __c The character to insert.
1775  * @return Iterator referencing the first inserted char.
1776  * @throw std::length_error If new length exceeds @c max_size().
1777  *
1778  * Inserts @a __n copies of character @a __c starting at the
1779  * position referenced by iterator @a __p. If adding
1780  * characters causes the length to exceed max_size(),
1781  * length_error is thrown. The value of the string doesn't
1782  * change if an error is thrown.
1783  */
1784  _GLIBCXX20_CONSTEXPR
1785  iterator
1786  insert(const_iterator __p, size_type __n, _CharT __c)
1787  {
1788  _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1789  const size_type __pos = __p - begin();
1790  this->replace(__p, __p, __n, __c);
1791  return iterator(this->_M_data() + __pos);
1792  }
1793 #else
1794  /**
1795  * @brief Insert multiple characters.
1796  * @param __p Iterator referencing location in string to insert at.
1797  * @param __n Number of characters to insert
1798  * @param __c The character to insert.
1799  * @throw std::length_error If new length exceeds @c max_size().
1800  *
1801  * Inserts @a __n copies of character @a __c starting at the
1802  * position referenced by iterator @a __p. If adding
1803  * characters causes the length to exceed max_size(),
1804  * length_error is thrown. The value of the string doesn't
1805  * change if an error is thrown.
1806  */
1807  void
1808  insert(iterator __p, size_type __n, _CharT __c)
1809  { this->replace(__p, __p, __n, __c); }
1810 #endif
1811 
1812 #if __cplusplus >= 201103L
1813  /**
1814  * @brief Insert a range of characters.
1815  * @param __p Const_iterator referencing location in string to
1816  * insert at.
1817  * @param __beg Start of range.
1818  * @param __end End of range.
1819  * @return Iterator referencing the first inserted char.
1820  * @throw std::length_error If new length exceeds @c max_size().
1821  *
1822  * Inserts characters in range [beg,end). If adding characters
1823  * causes the length to exceed max_size(), length_error is
1824  * thrown. The value of the string doesn't change if an error
1825  * is thrown.
1826  */
1827  template<class _InputIterator,
1828  typename = std::_RequireInputIter<_InputIterator>>
1829  _GLIBCXX20_CONSTEXPR
1830  iterator
1831  insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
1832  {
1833  _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1834  const size_type __pos = __p - begin();
1835  this->replace(__p, __p, __beg, __end);
1836  return iterator(this->_M_data() + __pos);
1837  }
1838 #else
1839  /**
1840  * @brief Insert a range of characters.
1841  * @param __p Iterator referencing location in string to insert at.
1842  * @param __beg Start of range.
1843  * @param __end End of range.
1844  * @throw std::length_error If new length exceeds @c max_size().
1845  *
1846  * Inserts characters in range [__beg,__end). If adding
1847  * characters causes the length to exceed max_size(),
1848  * length_error is thrown. The value of the string doesn't
1849  * change if an error is thrown.
1850  */
1851  template<class _InputIterator>
1852  void
1853  insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1854  { this->replace(__p, __p, __beg, __end); }
1855 #endif
1856 
1857 #if __cplusplus >= 201103L
1858  /**
1859  * @brief Insert an initializer_list of characters.
1860  * @param __p Iterator referencing location in string to insert at.
1861  * @param __l The initializer_list of characters to insert.
1862  * @throw std::length_error If new length exceeds @c max_size().
1863  */
1864  _GLIBCXX20_CONSTEXPR
1865  iterator
1866  insert(const_iterator __p, initializer_list<_CharT> __l)
1867  { return this->insert(__p, __l.begin(), __l.end()); }
1868 
1869 #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
1870  // See PR libstdc++/83328
1871  void
1872  insert(iterator __p, initializer_list<_CharT> __l)
1873  {
1874  _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1875  this->insert(__p - begin(), __l.begin(), __l.size());
1876  }
1877 #endif
1878 #endif // C++11
1879 
1880  /**
1881  * @brief Insert value of a string.
1882  * @param __pos1 Position in string to insert at.
1883  * @param __str The string to insert.
1884  * @return Reference to this string.
1885  * @throw std::length_error If new length exceeds @c max_size().
1886  *
1887  * Inserts value of @a __str starting at @a __pos1. If adding
1888  * characters causes the length to exceed max_size(),
1889  * length_error is thrown. The value of the string doesn't
1890  * change if an error is thrown.
1891  */
1892  _GLIBCXX20_CONSTEXPR
1893  basic_string&
1894  insert(size_type __pos1, const basic_string& __str)
1895  { return this->replace(__pos1, size_type(0),
1896  __str._M_data(), __str.size()); }
1897 
1898  /**
1899  * @brief Insert a substring.
1900  * @param __pos1 Position in string to insert at.
1901  * @param __str The string to insert.
1902  * @param __pos2 Start of characters in str to insert.
1903  * @param __n Number of characters to insert.
1904  * @return Reference to this string.
1905  * @throw std::length_error If new length exceeds @c max_size().
1906  * @throw std::out_of_range If @a pos1 > size() or
1907  * @a __pos2 > @a str.size().
1908  *
1909  * Starting at @a pos1, insert @a __n character of @a __str
1910  * beginning with @a __pos2. If adding characters causes the
1911  * length to exceed max_size(), length_error is thrown. If @a
1912  * __pos1 is beyond the end of this string or @a __pos2 is
1913  * beyond the end of @a __str, out_of_range is thrown. The
1914  * value of the string doesn't change if an error is thrown.
1915  */
1916  _GLIBCXX20_CONSTEXPR
1917  basic_string&
1918  insert(size_type __pos1, const basic_string& __str,
1919  size_type __pos2, size_type __n = npos)
1920  { return this->replace(__pos1, size_type(0), __str._M_data()
1921  + __str._M_check(__pos2, "basic_string::insert"),
1922  __str._M_limit(__pos2, __n)); }
1923 
1924  /**
1925  * @brief Insert a C substring.
1926  * @param __pos Position in string to insert at.
1927  * @param __s The C string to insert.
1928  * @param __n The number of characters to insert.
1929  * @return Reference to this string.
1930  * @throw std::length_error If new length exceeds @c max_size().
1931  * @throw std::out_of_range If @a __pos is beyond the end of this
1932  * string.
1933  *
1934  * Inserts the first @a __n characters of @a __s starting at @a
1935  * __pos. If adding characters causes the length to exceed
1936  * max_size(), length_error is thrown. If @a __pos is beyond
1937  * end(), out_of_range is thrown. The value of the string
1938  * doesn't change if an error is thrown.
1939  */
1940  _GLIBCXX20_CONSTEXPR
1941  basic_string&
1942  insert(size_type __pos, const _CharT* __s, size_type __n)
1943  { return this->replace(__pos, size_type(0), __s, __n); }
1944 
1945  /**
1946  * @brief Insert a C string.
1947  * @param __pos Position in string to insert at.
1948  * @param __s The C string to insert.
1949  * @return Reference to this string.
1950  * @throw std::length_error If new length exceeds @c max_size().
1951  * @throw std::out_of_range If @a pos is beyond the end of this
1952  * string.
1953  *
1954  * Inserts the first @a n characters of @a __s starting at @a __pos. If
1955  * adding characters causes the length to exceed max_size(),
1956  * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1957  * thrown. The value of the string doesn't change if an error is
1958  * thrown.
1959  */
1960  _GLIBCXX20_CONSTEXPR
1961  basic_string&
1962  insert(size_type __pos, const _CharT* __s)
1963  {
1964  __glibcxx_requires_string(__s);
1965  return this->replace(__pos, size_type(0), __s,
1966  traits_type::length(__s));
1967  }
1968 
1969  /**
1970  * @brief Insert multiple characters.
1971  * @param __pos Index in string to insert at.
1972  * @param __n Number of characters to insert
1973  * @param __c The character to insert.
1974  * @return Reference to this string.
1975  * @throw std::length_error If new length exceeds @c max_size().
1976  * @throw std::out_of_range If @a __pos is beyond the end of this
1977  * string.
1978  *
1979  * Inserts @a __n copies of character @a __c starting at index
1980  * @a __pos. If adding characters causes the length to exceed
1981  * max_size(), length_error is thrown. If @a __pos > length(),
1982  * out_of_range is thrown. The value of the string doesn't
1983  * change if an error is thrown.
1984  */
1985  _GLIBCXX20_CONSTEXPR
1986  basic_string&
1987  insert(size_type __pos, size_type __n, _CharT __c)
1988  { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1989  size_type(0), __n, __c); }
1990 
1991  /**
1992  * @brief Insert one character.
1993  * @param __p Iterator referencing position in string to insert at.
1994  * @param __c The character to insert.
1995  * @return Iterator referencing newly inserted char.
1996  * @throw std::length_error If new length exceeds @c max_size().
1997  *
1998  * Inserts character @a __c at position referenced by @a __p.
1999  * If adding character causes the length to exceed max_size(),
2000  * length_error is thrown. If @a __p is beyond end of string,
2001  * out_of_range is thrown. The value of the string doesn't
2002  * change if an error is thrown.
2003  */
2004  _GLIBCXX20_CONSTEXPR
2005  iterator
2006  insert(__const_iterator __p, _CharT __c)
2007  {
2008  _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2009  const size_type __pos = __p - begin();
2010  _M_replace_aux(__pos, size_type(0), size_type(1), __c);
2011  return iterator(_M_data() + __pos);
2012  }
2013 
2014 #if __cplusplus >= 201703L
2015  /**
2016  * @brief Insert a string_view.
2017  * @param __pos Position in string to insert at.
2018  * @param __svt The object convertible to string_view to insert.
2019  * @return Reference to this string.
2020  */
2021  template<typename _Tp>
2022  _GLIBCXX20_CONSTEXPR
2023  _If_sv<_Tp, basic_string&>
2024  insert(size_type __pos, const _Tp& __svt)
2025  {
2026  __sv_type __sv = __svt;
2027  return this->insert(__pos, __sv.data(), __sv.size());
2028  }
2029 
2030  /**
2031  * @brief Insert a string_view.
2032  * @param __pos1 Position in string to insert at.
2033  * @param __svt The object convertible to string_view to insert from.
2034  * @param __pos2 Start of characters in str to insert.
2035  * @param __n The number of characters to insert.
2036  * @return Reference to this string.
2037  */
2038  template<typename _Tp>
2039  _GLIBCXX20_CONSTEXPR
2040  _If_sv<_Tp, basic_string&>
2041  insert(size_type __pos1, const _Tp& __svt,
2042  size_type __pos2, size_type __n = npos)
2043  {
2044  __sv_type __sv = __svt;
2045  return this->replace(__pos1, size_type(0),
2046  __sv.data()
2047  + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
2048  std::__sv_limit(__sv.size(), __pos2, __n));
2049  }
2050 #endif // C++17
2051 
2052  /**
2053  * @brief Remove characters.
2054  * @param __pos Index of first character to remove (default 0).
2055  * @param __n Number of characters to remove (default remainder).
2056  * @return Reference to this string.
2057  * @throw std::out_of_range If @a pos is beyond the end of this
2058  * string.
2059  *
2060  * Removes @a __n characters from this string starting at @a
2061  * __pos. The length of the string is reduced by @a __n. If
2062  * there are < @a __n characters to remove, the remainder of
2063  * the string is truncated. If @a __p is beyond end of string,
2064  * out_of_range is thrown. The value of the string doesn't
2065  * change if an error is thrown.
2066  */
2067  _GLIBCXX20_CONSTEXPR
2068  basic_string&
2069  erase(size_type __pos = 0, size_type __n = npos)
2070  {
2071  _M_check(__pos, "basic_string::erase");
2072  if (__n == npos)
2073  this->_M_set_length(__pos);
2074  else if (__n != 0)
2075  this->_M_erase(__pos, _M_limit(__pos, __n));
2076  return *this;
2077  }
2078 
2079  /**
2080  * @brief Remove one character.
2081  * @param __position Iterator referencing the character to remove.
2082  * @return iterator referencing same location after removal.
2083  *
2084  * Removes the character at @a __position from this string. The value
2085  * of the string doesn't change if an error is thrown.
2086  */
2087  _GLIBCXX20_CONSTEXPR
2088  iterator
2089  erase(__const_iterator __position)
2090  {
2091  _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
2092  && __position < end());
2093  const size_type __pos = __position - begin();
2094  this->_M_erase(__pos, size_type(1));
2095  return iterator(_M_data() + __pos);
2096  }
2097 
2098  /**
2099  * @brief Remove a range of characters.
2100  * @param __first Iterator referencing the first character to remove.
2101  * @param __last Iterator referencing the end of the range.
2102  * @return Iterator referencing location of first after removal.
2103  *
2104  * Removes the characters in the range [first,last) from this string.
2105  * The value of the string doesn't change if an error is thrown.
2106  */
2107  _GLIBCXX20_CONSTEXPR
2108  iterator
2109  erase(__const_iterator __first, __const_iterator __last)
2110  {
2111  _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
2112  && __last <= end());
2113  const size_type __pos = __first - begin();
2114  if (__last == end())
2115  this->_M_set_length(__pos);
2116  else
2117  this->_M_erase(__pos, __last - __first);
2118  return iterator(this->_M_data() + __pos);
2119  }
2120 
2121 #if __cplusplus >= 201103L
2122  /**
2123  * @brief Remove the last character.
2124  *
2125  * The string must be non-empty.
2126  */
2127  _GLIBCXX20_CONSTEXPR
2128  void
2129  pop_back() noexcept
2130  {
2131  __glibcxx_assert(!empty());
2132  _M_erase(size() - 1, 1);
2133  }
2134 #endif // C++11
2135 
2136  /**
2137  * @brief Replace characters with value from another string.
2138  * @param __pos Index of first character to replace.
2139  * @param __n Number of characters to be replaced.
2140  * @param __str String to insert.
2141  * @return Reference to this string.
2142  * @throw std::out_of_range If @a pos is beyond the end of this
2143  * string.
2144  * @throw std::length_error If new length exceeds @c max_size().
2145  *
2146  * Removes the characters in the range [__pos,__pos+__n) from
2147  * this string. In place, the value of @a __str is inserted.
2148  * If @a __pos is beyond end of string, out_of_range is thrown.
2149  * If the length of the result exceeds max_size(), length_error
2150  * is thrown. The value of the string doesn't change if an
2151  * error is thrown.
2152  */
2153  _GLIBCXX20_CONSTEXPR
2154  basic_string&
2155  replace(size_type __pos, size_type __n, const basic_string& __str)
2156  { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
2157 
2158  /**
2159  * @brief Replace characters with value from another string.
2160  * @param __pos1 Index of first character to replace.
2161  * @param __n1 Number of characters to be replaced.
2162  * @param __str String to insert.
2163  * @param __pos2 Index of first character of str to use.
2164  * @param __n2 Number of characters from str to use.
2165  * @return Reference to this string.
2166  * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
2167  * __str.size().
2168  * @throw std::length_error If new length exceeds @c max_size().
2169  *
2170  * Removes the characters in the range [__pos1,__pos1 + n) from this
2171  * string. In place, the value of @a __str is inserted. If @a __pos is
2172  * beyond end of string, out_of_range is thrown. If the length of the
2173  * result exceeds max_size(), length_error is thrown. The value of the
2174  * string doesn't change if an error is thrown.
2175  */
2176  _GLIBCXX20_CONSTEXPR
2177  basic_string&
2178  replace(size_type __pos1, size_type __n1, const basic_string& __str,
2179  size_type __pos2, size_type __n2 = npos)
2180  { return this->replace(__pos1, __n1, __str._M_data()
2181  + __str._M_check(__pos2, "basic_string::replace"),
2182  __str._M_limit(__pos2, __n2)); }
2183 
2184  /**
2185  * @brief Replace characters with value of a C substring.
2186  * @param __pos Index of first character to replace.
2187  * @param __n1 Number of characters to be replaced.
2188  * @param __s C string to insert.
2189  * @param __n2 Number of characters from @a s to use.
2190  * @return Reference to this string.
2191  * @throw std::out_of_range If @a pos1 > size().
2192  * @throw std::length_error If new length exceeds @c max_size().
2193  *
2194  * Removes the characters in the range [__pos,__pos + __n1)
2195  * from this string. In place, the first @a __n2 characters of
2196  * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
2197  * @a __pos is beyond end of string, out_of_range is thrown. If
2198  * the length of result exceeds max_size(), length_error is
2199  * thrown. The value of the string doesn't change if an error
2200  * is thrown.
2201  */
2202  _GLIBCXX20_CONSTEXPR
2203  basic_string&
2204  replace(size_type __pos, size_type __n1, const _CharT* __s,
2205  size_type __n2)
2206  {
2207  __glibcxx_requires_string_len(__s, __n2);
2208  return _M_replace(_M_check(__pos, "basic_string::replace"),
2209  _M_limit(__pos, __n1), __s, __n2);
2210  }
2211 
2212  /**
2213  * @brief Replace characters with value of a C string.
2214  * @param __pos Index of first character to replace.
2215  * @param __n1 Number of characters to be replaced.
2216  * @param __s C string to insert.
2217  * @return Reference to this string.
2218  * @throw std::out_of_range If @a pos > size().
2219  * @throw std::length_error If new length exceeds @c max_size().
2220  *
2221  * Removes the characters in the range [__pos,__pos + __n1)
2222  * from this string. In place, the characters of @a __s are
2223  * inserted. If @a __pos is beyond end of string, out_of_range
2224  * is thrown. If the length of result exceeds max_size(),
2225  * length_error is thrown. The value of the string doesn't
2226  * change if an error is thrown.
2227  */
2228  _GLIBCXX20_CONSTEXPR
2229  basic_string&
2230  replace(size_type __pos, size_type __n1, const _CharT* __s)
2231  {
2232  __glibcxx_requires_string(__s);
2233  return this->replace(__pos, __n1, __s, traits_type::length(__s));
2234  }
2235 
2236  /**
2237  * @brief Replace characters with multiple characters.
2238  * @param __pos Index of first character to replace.
2239  * @param __n1 Number of characters to be replaced.
2240  * @param __n2 Number of characters to insert.
2241  * @param __c Character to insert.
2242  * @return Reference to this string.
2243  * @throw std::out_of_range If @a __pos > size().
2244  * @throw std::length_error If new length exceeds @c max_size().
2245  *
2246  * Removes the characters in the range [pos,pos + n1) from this
2247  * string. In place, @a __n2 copies of @a __c are inserted.
2248  * If @a __pos is beyond end of string, out_of_range is thrown.
2249  * If the length of result exceeds max_size(), length_error is
2250  * thrown. The value of the string doesn't change if an error
2251  * is thrown.
2252  */
2253  _GLIBCXX20_CONSTEXPR
2254  basic_string&
2255  replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
2256  { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
2257  _M_limit(__pos, __n1), __n2, __c); }
2258 
2259  /**
2260  * @brief Replace range of characters with string.
2261  * @param __i1 Iterator referencing start of range to replace.
2262  * @param __i2 Iterator referencing end of range to replace.
2263  * @param __str String value to insert.
2264  * @return Reference to this string.
2265  * @throw std::length_error If new length exceeds @c max_size().
2266  *
2267  * Removes the characters in the range [__i1,__i2). In place,
2268  * the value of @a __str is inserted. If the length of result
2269  * exceeds max_size(), length_error is thrown. The value of
2270  * the string doesn't change if an error is thrown.
2271  */
2272  _GLIBCXX20_CONSTEXPR
2273  basic_string&
2274  replace(__const_iterator __i1, __const_iterator __i2,
2275  const basic_string& __str)
2276  { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
2277 
2278  /**
2279  * @brief Replace range of characters with C substring.
2280  * @param __i1 Iterator referencing start of range to replace.
2281  * @param __i2 Iterator referencing end of range to replace.
2282  * @param __s C string value to insert.
2283  * @param __n Number of characters from s to insert.
2284  * @return Reference to this string.
2285  * @throw std::length_error If new length exceeds @c max_size().
2286  *
2287  * Removes the characters in the range [__i1,__i2). In place,
2288  * the first @a __n characters of @a __s are inserted. If the
2289  * length of result exceeds max_size(), length_error is thrown.
2290  * The value of the string doesn't change if an error is
2291  * thrown.
2292  */
2293  _GLIBCXX20_CONSTEXPR
2294  basic_string&
2295  replace(__const_iterator __i1, __const_iterator __i2,
2296  const _CharT* __s, size_type __n)
2297  {
2298  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2299  && __i2 <= end());
2300  return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
2301  }
2302 
2303  /**
2304  * @brief Replace range of characters with C string.
2305  * @param __i1 Iterator referencing start of range to replace.
2306  * @param __i2 Iterator referencing end of range to replace.
2307  * @param __s C string value to insert.
2308  * @return Reference to this string.
2309  * @throw std::length_error If new length exceeds @c max_size().
2310  *
2311  * Removes the characters in the range [__i1,__i2). In place,
2312  * the characters of @a __s are inserted. If the length of
2313  * result exceeds max_size(), length_error is thrown. The
2314  * value of the string doesn't change if an error is thrown.
2315  */
2316  _GLIBCXX20_CONSTEXPR
2317  basic_string&
2318  replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
2319  {
2320  __glibcxx_requires_string(__s);
2321  return this->replace(__i1, __i2, __s, traits_type::length(__s));
2322  }
2323 
2324  /**
2325  * @brief Replace range of characters with multiple characters
2326  * @param __i1 Iterator referencing start of range to replace.
2327  * @param __i2 Iterator referencing end of range to replace.
2328  * @param __n Number of characters to insert.
2329  * @param __c Character to insert.
2330  * @return Reference to this string.
2331  * @throw std::length_error If new length exceeds @c max_size().
2332  *
2333  * Removes the characters in the range [__i1,__i2). In place,
2334  * @a __n copies of @a __c are inserted. If the length of
2335  * result exceeds max_size(), length_error is thrown. The
2336  * value of the string doesn't change if an error is thrown.
2337  */
2338  _GLIBCXX20_CONSTEXPR
2339  basic_string&
2340  replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
2341  _CharT __c)
2342  {
2343  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2344  && __i2 <= end());
2345  return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
2346  }
2347 
2348  /**
2349  * @brief Replace range of characters with range.
2350  * @param __i1 Iterator referencing start of range to replace.
2351  * @param __i2 Iterator referencing end of range to replace.
2352  * @param __k1 Iterator referencing start of range to insert.
2353  * @param __k2 Iterator referencing end of range to insert.
2354  * @return Reference to this string.
2355  * @throw std::length_error If new length exceeds @c max_size().
2356  *
2357  * Removes the characters in the range [__i1,__i2). In place,
2358  * characters in the range [__k1,__k2) are inserted. If the
2359  * length of result exceeds max_size(), length_error is thrown.
2360  * The value of the string doesn't change if an error is
2361  * thrown.
2362  */
2363 #if __cplusplus >= 201103L
2364  template<class _InputIterator,
2365  typename = std::_RequireInputIter<_InputIterator>>
2366  _GLIBCXX20_CONSTEXPR
2367  basic_string&
2368  replace(const_iterator __i1, const_iterator __i2,
2369  _InputIterator __k1, _InputIterator __k2)
2370  {
2371  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2372  && __i2 <= end());
2373  __glibcxx_requires_valid_range(__k1, __k2);
2374  return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
2375  std::__false_type());
2376  }
2377 #else
2378  template<class _InputIterator>
2379 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2380  typename __enable_if_not_native_iterator<_InputIterator>::__type
2381 #else
2382  basic_string&
2383 #endif
2384  replace(iterator __i1, iterator __i2,
2385  _InputIterator __k1, _InputIterator __k2)
2386  {
2387  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2388  && __i2 <= end());
2389  __glibcxx_requires_valid_range(__k1, __k2);
2390  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2391  return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2392  }
2393 #endif
2394 
2395  // Specializations for the common case of pointer and iterator:
2396  // useful to avoid the overhead of temporary buffering in _M_replace.
2397  _GLIBCXX20_CONSTEXPR
2398  basic_string&
2399  replace(__const_iterator __i1, __const_iterator __i2,
2400  _CharT* __k1, _CharT* __k2)
2401  {
2402  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2403  && __i2 <= end());
2404  __glibcxx_requires_valid_range(__k1, __k2);
2405  return this->replace(__i1 - begin(), __i2 - __i1,
2406  __k1, __k2 - __k1);
2407  }
2408 
2409  _GLIBCXX20_CONSTEXPR
2410  basic_string&
2411  replace(__const_iterator __i1, __const_iterator __i2,
2412  const _CharT* __k1, const _CharT* __k2)
2413  {
2414  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2415  && __i2 <= end());
2416  __glibcxx_requires_valid_range(__k1, __k2);
2417  return this->replace(__i1 - begin(), __i2 - __i1,
2418  __k1, __k2 - __k1);
2419  }
2420 
2421  _GLIBCXX20_CONSTEXPR
2422  basic_string&
2423  replace(__const_iterator __i1, __const_iterator __i2,
2424  iterator __k1, iterator __k2)
2425  {
2426  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2427  && __i2 <= end());
2428  __glibcxx_requires_valid_range(__k1, __k2);
2429  return this->replace(__i1 - begin(), __i2 - __i1,
2430  __k1.base(), __k2 - __k1);
2431  }
2432 
2433  _GLIBCXX20_CONSTEXPR
2434  basic_string&
2435  replace(__const_iterator __i1, __const_iterator __i2,
2436  const_iterator __k1, const_iterator __k2)
2437  {
2438  _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2439  && __i2 <= end());
2440  __glibcxx_requires_valid_range(__k1, __k2);
2441  return this->replace(__i1 - begin(), __i2 - __i1,
2442  __k1.base(), __k2 - __k1);
2443  }
2444 
2445 #if __cplusplus >= 201103L
2446  /**
2447  * @brief Replace range of characters with initializer_list.
2448  * @param __i1 Iterator referencing start of range to replace.
2449  * @param __i2 Iterator referencing end of range to replace.
2450  * @param __l The initializer_list of characters to insert.
2451  * @return Reference to this string.
2452  * @throw std::length_error If new length exceeds @c max_size().
2453  *
2454  * Removes the characters in the range [__i1,__i2). In place,
2455  * characters in the range [__k1,__k2) are inserted. If the
2456  * length of result exceeds max_size(), length_error is thrown.
2457  * The value of the string doesn't change if an error is
2458  * thrown.
2459  */
2460  _GLIBCXX20_CONSTEXPR
2461  basic_string& replace(const_iterator __i1, const_iterator __i2,
2462  initializer_list<_CharT> __l)
2463  { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
2464 #endif // C++11
2465 
2466 #if __cplusplus >= 201703L
2467  /**
2468  * @brief Replace range of characters with string_view.
2469  * @param __pos The position to replace at.
2470  * @param __n The number of characters to replace.
2471  * @param __svt The object convertible to string_view to insert.
2472  * @return Reference to this string.
2473  */
2474  template<typename _Tp>
2475  _GLIBCXX20_CONSTEXPR
2476  _If_sv<_Tp, basic_string&>
2477  replace(size_type __pos, size_type __n, const _Tp& __svt)
2478  {
2479  __sv_type __sv = __svt;
2480  return this->replace(__pos, __n, __sv.data(), __sv.size());
2481  }
2482 
2483  /**
2484  * @brief Replace range of characters with string_view.
2485  * @param __pos1 The position to replace at.
2486  * @param __n1 The number of characters to replace.
2487  * @param __svt The object convertible to string_view to insert from.
2488  * @param __pos2 The position in the string_view to insert from.
2489  * @param __n2 The number of characters to insert.
2490  * @return Reference to this string.
2491  */
2492  template<typename _Tp>
2493  _GLIBCXX20_CONSTEXPR
2494  _If_sv<_Tp, basic_string&>
2495  replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2496  size_type __pos2, size_type __n2 = npos)
2497  {
2498  __sv_type __sv = __svt;
2499  return this->replace(__pos1, __n1,
2500  __sv.data()
2501  + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
2502  std::__sv_limit(__sv.size(), __pos2, __n2));
2503  }
2504 
2505  /**
2506  * @brief Replace range of characters with string_view.
2507  * @param __i1 An iterator referencing the start position
2508  to replace at.
2509  * @param __i2 An iterator referencing the end position
2510  for the replace.
2511  * @param __svt The object convertible to string_view to insert from.
2512  * @return Reference to this string.
2513  */
2514  template<typename _Tp>
2515  _GLIBCXX20_CONSTEXPR
2516  _If_sv<_Tp, basic_string&>
2517  replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2518  {
2519  __sv_type __sv = __svt;
2520  return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2521  }
2522 #endif // C++17
2523 
2524  private:
2525  template<class _Integer>
2526  _GLIBCXX20_CONSTEXPR
2527  basic_string&
2528  _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2529  _Integer __n, _Integer __val, __true_type)
2530  { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
2531 
2532  template<class _InputIterator>
2533  _GLIBCXX20_CONSTEXPR
2534  basic_string&
2535  _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2536  _InputIterator __k1, _InputIterator __k2,
2537  __false_type);
2538 
2539  _GLIBCXX20_CONSTEXPR
2540  basic_string&
2541  _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2542  _CharT __c);
2543 
2544  __attribute__((__noinline__, __noclone__, __cold__)) void
2545  _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s,
2546  const size_type __len2, const size_type __how_much);
2547 
2548  _GLIBCXX20_CONSTEXPR
2549  basic_string&
2550  _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
2551  const size_type __len2);
2552 
2553  _GLIBCXX20_CONSTEXPR
2554  basic_string&
2555  _M_append(const _CharT* __s, size_type __n);
2556 
2557  public:
2558 
2559  /**
2560  * @brief Copy substring into C string.
2561  * @param __s C string to copy value into.
2562  * @param __n Number of characters to copy.
2563  * @param __pos Index of first character to copy.
2564  * @return Number of characters actually copied
2565  * @throw std::out_of_range If __pos > size().
2566  *
2567  * Copies up to @a __n characters starting at @a __pos into the
2568  * C string @a __s. If @a __pos is %greater than size(),
2569  * out_of_range is thrown.
2570  */
2571  _GLIBCXX20_CONSTEXPR
2572  size_type
2573  copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2574 
2575  /**
2576  * @brief Swap contents with another string.
2577  * @param __s String to swap with.
2578  *
2579  * Exchanges the contents of this string with that of @a __s in constant
2580  * time.
2581  */
2582  _GLIBCXX20_CONSTEXPR
2583  void
2584  swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
2585 
2586  // String operations:
2587  /**
2588  * @brief Return const pointer to null-terminated contents.
2589  *
2590  * This is a handle to internal data. Do not modify or dire things may
2591  * happen.
2592  */
2593  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2594  const _CharT*
2595  c_str() const _GLIBCXX_NOEXCEPT
2596  { return _M_data(); }
2597 
2598  /**
2599  * @brief Return const pointer to contents.
2600  *
2601  * This is a pointer to internal data. It is undefined to modify
2602  * the contents through the returned pointer. To get a pointer that
2603  * allows modifying the contents use @c &str[0] instead,
2604  * (or in C++17 the non-const @c str.data() overload).
2605  */
2606  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2607  const _CharT*
2608  data() const _GLIBCXX_NOEXCEPT
2609  { return _M_data(); }
2610 
2611 #if __cplusplus >= 201703L
2612  /**
2613  * @brief Return non-const pointer to contents.
2614  *
2615  * This is a pointer to the character sequence held by the string.
2616  * Modifying the characters in the sequence is allowed.
2617  */
2618  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2619  _CharT*
2620  data() noexcept
2621  { return _M_data(); }
2622 #endif
2623 
2624  /**
2625  * @brief Return copy of allocator used to construct this string.
2626  */
2627  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2628  allocator_type
2629  get_allocator() const _GLIBCXX_NOEXCEPT
2630  { return _M_get_allocator(); }
2631 
2632  /**
2633  * @brief Find position of a C substring.
2634  * @param __s C string to locate.
2635  * @param __pos Index of character to search from.
2636  * @param __n Number of characters from @a s to search for.
2637  * @return Index of start of first occurrence.
2638  *
2639  * Starting from @a __pos, searches forward for the first @a
2640  * __n characters in @a __s within this string. If found,
2641  * returns the index where it begins. If not found, returns
2642  * npos.
2643  */
2644  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2645  size_type
2646  find(const _CharT* __s, size_type __pos, size_type __n) const
2647  _GLIBCXX_NOEXCEPT;
2648 
2649  /**
2650  * @brief Find position of a string.
2651  * @param __str String to locate.
2652  * @param __pos Index of character to search from (default 0).
2653  * @return Index of start of first occurrence.
2654  *
2655  * Starting from @a __pos, searches forward for value of @a __str within
2656  * this string. If found, returns the index where it begins. If not
2657  * found, returns npos.
2658  */
2659  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2660  size_type
2661  find(const basic_string& __str, size_type __pos = 0) const
2662  _GLIBCXX_NOEXCEPT
2663  { return this->find(__str.data(), __pos, __str.size()); }
2664 
2665 #if __cplusplus >= 201703L
2666  /**
2667  * @brief Find position of a string_view.
2668  * @param __svt The object convertible to string_view to locate.
2669  * @param __pos Index of character to search from (default 0).
2670  * @return Index of start of first occurrence.
2671  */
2672  template<typename _Tp>
2673  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2674  _If_sv<_Tp, size_type>
2675  find(const _Tp& __svt, size_type __pos = 0) const
2676  noexcept(is_same<_Tp, __sv_type>::value)
2677  {
2678  __sv_type __sv = __svt;
2679  return this->find(__sv.data(), __pos, __sv.size());
2680  }
2681 #endif // C++17
2682 
2683  /**
2684  * @brief Find position of a C string.
2685  * @param __s C string to locate.
2686  * @param __pos Index of character to search from (default 0).
2687  * @return Index of start of first occurrence.
2688  *
2689  * Starting from @a __pos, searches forward for the value of @a
2690  * __s within this string. If found, returns the index where
2691  * it begins. If not found, returns npos.
2692  */
2693  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2694  size_type
2695  find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2696  {
2697  __glibcxx_requires_string(__s);
2698  return this->find(__s, __pos, traits_type::length(__s));
2699  }
2700 
2701  /**
2702  * @brief Find position of a character.
2703  * @param __c Character to locate.
2704  * @param __pos Index of character to search from (default 0).
2705  * @return Index of first occurrence.
2706  *
2707  * Starting from @a __pos, searches forward for @a __c within
2708  * this string. If found, returns the index where it was
2709  * found. If not found, returns npos.
2710  */
2711  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2712  size_type
2713  find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
2714 
2715  /**
2716  * @brief Find last position of a string.
2717  * @param __str String to locate.
2718  * @param __pos Index of character to search back from (default end).
2719  * @return Index of start of last occurrence.
2720  *
2721  * Starting from @a __pos, searches backward for value of @a
2722  * __str within this string. If found, returns the index where
2723  * it begins. If not found, returns npos.
2724  */
2725  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2726  size_type
2727  rfind(const basic_string& __str, size_type __pos = npos) const
2728  _GLIBCXX_NOEXCEPT
2729  { return this->rfind(__str.data(), __pos, __str.size()); }
2730 
2731 #if __cplusplus >= 201703L
2732  /**
2733  * @brief Find last position of a string_view.
2734  * @param __svt The object convertible to string_view to locate.
2735  * @param __pos Index of character to search back from (default end).
2736  * @return Index of start of last occurrence.
2737  */
2738  template<typename _Tp>
2739  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2740  _If_sv<_Tp, size_type>
2741  rfind(const _Tp& __svt, size_type __pos = npos) const
2742  noexcept(is_same<_Tp, __sv_type>::value)
2743  {
2744  __sv_type __sv = __svt;
2745  return this->rfind(__sv.data(), __pos, __sv.size());
2746  }
2747 #endif // C++17
2748 
2749  /**
2750  * @brief Find last position of a C substring.
2751  * @param __s C string to locate.
2752  * @param __pos Index of character to search back from.
2753  * @param __n Number of characters from s to search for.
2754  * @return Index of start of last occurrence.
2755  *
2756  * Starting from @a __pos, searches backward for the first @a
2757  * __n characters in @a __s within this string. If found,
2758  * returns the index where it begins. If not found, returns
2759  * npos.
2760  */
2761  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2762  size_type
2763  rfind(const _CharT* __s, size_type __pos, size_type __n) const
2764  _GLIBCXX_NOEXCEPT;
2765 
2766  /**
2767  * @brief Find last position of a C string.
2768  * @param __s C string to locate.
2769  * @param __pos Index of character to start search at (default end).
2770  * @return Index of start of last occurrence.
2771  *
2772  * Starting from @a __pos, searches backward for the value of
2773  * @a __s within this string. If found, returns the index
2774  * where it begins. If not found, returns npos.
2775  */
2776  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2777  size_type
2778  rfind(const _CharT* __s, size_type __pos = npos) const
2779  {
2780  __glibcxx_requires_string(__s);
2781  return this->rfind(__s, __pos, traits_type::length(__s));
2782  }
2783 
2784  /**
2785  * @brief Find last position of a character.
2786  * @param __c Character to locate.
2787  * @param __pos Index of character to search back from (default end).
2788  * @return Index of last occurrence.
2789  *
2790  * Starting from @a __pos, searches backward for @a __c within
2791  * this string. If found, returns the index where it was
2792  * found. If not found, returns npos.
2793  */
2794  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2795  size_type
2796  rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
2797 
2798  /**
2799  * @brief Find position of a character of string.
2800  * @param __str String containing characters to locate.
2801  * @param __pos Index of character to search from (default 0).
2802  * @return Index of first occurrence.
2803  *
2804  * Starting from @a __pos, searches forward for one of the
2805  * characters of @a __str within this string. If found,
2806  * returns the index where it was found. If not found, returns
2807  * npos.
2808  */
2809  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2810  size_type
2811  find_first_of(const basic_string& __str, size_type __pos = 0) const
2812  _GLIBCXX_NOEXCEPT
2813  { return this->find_first_of(__str.data(), __pos, __str.size()); }
2814 
2815 #if __cplusplus >= 201703L
2816  /**
2817  * @brief Find position of a character of a string_view.
2818  * @param __svt An object convertible to string_view containing
2819  * characters to locate.
2820  * @param __pos Index of character to search from (default 0).
2821  * @return Index of first occurrence.
2822  */
2823  template<typename _Tp>
2824  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2825  _If_sv<_Tp, size_type>
2826  find_first_of(const _Tp& __svt, size_type __pos = 0) const
2827  noexcept(is_same<_Tp, __sv_type>::value)
2828  {
2829  __sv_type __sv = __svt;
2830  return this->find_first_of(__sv.data(), __pos, __sv.size());
2831  }
2832 #endif // C++17
2833 
2834  /**
2835  * @brief Find position of a character of C substring.
2836  * @param __s String containing characters to locate.
2837  * @param __pos Index of character to search from.
2838  * @param __n Number of characters from s to search for.
2839  * @return Index of first occurrence.
2840  *
2841  * Starting from @a __pos, searches forward for one of the
2842  * first @a __n characters of @a __s within this string. If
2843  * found, returns the index where it was found. If not found,
2844  * returns npos.
2845  */
2846  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2847  size_type
2848  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
2849  _GLIBCXX_NOEXCEPT;
2850 
2851  /**
2852  * @brief Find position of a character of C string.
2853  * @param __s String containing characters to locate.
2854  * @param __pos Index of character to search from (default 0).
2855  * @return Index of first occurrence.
2856  *
2857  * Starting from @a __pos, searches forward for one of the
2858  * characters of @a __s within this string. If found, returns
2859  * the index where it was found. If not found, returns npos.
2860  */
2861  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2862  size_type
2863  find_first_of(const _CharT* __s, size_type __pos = 0) const
2864  _GLIBCXX_NOEXCEPT
2865  {
2866  __glibcxx_requires_string(__s);
2867  return this->find_first_of(__s, __pos, traits_type::length(__s));
2868  }
2869 
2870  /**
2871  * @brief Find position of a character.
2872  * @param __c Character to locate.
2873  * @param __pos Index of character to search from (default 0).
2874  * @return Index of first occurrence.
2875  *
2876  * Starting from @a __pos, searches forward for the character
2877  * @a __c within this string. If found, returns the index
2878  * where it was found. If not found, returns npos.
2879  *
2880  * Note: equivalent to find(__c, __pos).
2881  */
2882  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2883  size_type
2884  find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2885  { return this->find(__c, __pos); }
2886 
2887  /**
2888  * @brief Find last position of a character of string.
2889  * @param __str String containing characters to locate.
2890  * @param __pos Index of character to search back from (default end).
2891  * @return Index of last occurrence.
2892  *
2893  * Starting from @a __pos, searches backward for one of the
2894  * characters of @a __str within this string. If found,
2895  * returns the index where it was found. If not found, returns
2896  * npos.
2897  */
2898  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2899  size_type
2900  find_last_of(const basic_string& __str, size_type __pos = npos) const
2901  _GLIBCXX_NOEXCEPT
2902  { return this->find_last_of(__str.data(), __pos, __str.size()); }
2903 
2904 #if __cplusplus >= 201703L
2905  /**
2906  * @brief Find last position of a character of string.
2907  * @param __svt An object convertible to string_view containing
2908  * characters to locate.
2909  * @param __pos Index of character to search back from (default end).
2910  * @return Index of last occurrence.
2911  */
2912  template<typename _Tp>
2913  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2914  _If_sv<_Tp, size_type>
2915  find_last_of(const _Tp& __svt, size_type __pos = npos) const
2916  noexcept(is_same<_Tp, __sv_type>::value)
2917  {
2918  __sv_type __sv = __svt;
2919  return this->find_last_of(__sv.data(), __pos, __sv.size());
2920  }
2921 #endif // C++17
2922 
2923  /**
2924  * @brief Find last position of a character of C substring.
2925  * @param __s C string containing characters to locate.
2926  * @param __pos Index of character to search back from.
2927  * @param __n Number of characters from s to search for.
2928  * @return Index of last occurrence.
2929  *
2930  * Starting from @a __pos, searches backward for one of the
2931  * first @a __n characters of @a __s within this string. If
2932  * found, returns the index where it was found. If not found,
2933  * returns npos.
2934  */
2935  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2936  size_type
2937  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
2938  _GLIBCXX_NOEXCEPT;
2939 
2940  /**
2941  * @brief Find last position of a character of C string.
2942  * @param __s C string containing characters to locate.
2943  * @param __pos Index of character to search back from (default end).
2944  * @return Index of last occurrence.
2945  *
2946  * Starting from @a __pos, searches backward for one of the
2947  * characters of @a __s within this string. If found, returns
2948  * the index where it was found. If not found, returns npos.
2949  */
2950  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2951  size_type
2952  find_last_of(const _CharT* __s, size_type __pos = npos) const
2953  _GLIBCXX_NOEXCEPT
2954  {
2955  __glibcxx_requires_string(__s);
2956  return this->find_last_of(__s, __pos, traits_type::length(__s));
2957  }
2958 
2959  /**
2960  * @brief Find last position of a character.
2961  * @param __c Character to locate.
2962  * @param __pos Index of character to search back from (default end).
2963  * @return Index of last occurrence.
2964  *
2965  * Starting from @a __pos, searches backward for @a __c within
2966  * this string. If found, returns the index where it was
2967  * found. If not found, returns npos.
2968  *
2969  * Note: equivalent to rfind(__c, __pos).
2970  */
2971  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2972  size_type
2973  find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2974  { return this->rfind(__c, __pos); }
2975 
2976  /**
2977  * @brief Find position of a character not in string.
2978  * @param __str String containing characters to avoid.
2979  * @param __pos Index of character to search from (default 0).
2980  * @return Index of first occurrence.
2981  *
2982  * Starting from @a __pos, searches forward for a character not contained
2983  * in @a __str within this string. If found, returns the index where it
2984  * was found. If not found, returns npos.
2985  */
2986  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2987  size_type
2988  find_first_not_of(const basic_string& __str, size_type __pos = 0) const
2989  _GLIBCXX_NOEXCEPT
2990  { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2991 
2992 #if __cplusplus >= 201703L
2993  /**
2994  * @brief Find position of a character not in a string_view.
2995  * @param __svt A object convertible to string_view containing
2996  * characters to avoid.
2997  * @param __pos Index of character to search from (default 0).
2998  * @return Index of first occurrence.
2999  */
3000  template<typename _Tp>
3001  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3002  _If_sv<_Tp, size_type>
3003  find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
3004  noexcept(is_same<_Tp, __sv_type>::value)
3005  {
3006  __sv_type __sv = __svt;
3007  return this->find_first_not_of(__sv.data(), __pos, __sv.size());
3008  }
3009 #endif // C++17
3010 
3011  /**
3012  * @brief Find position of a character not in C substring.
3013  * @param __s C string containing characters to avoid.
3014  * @param __pos Index of character to search from.
3015  * @param __n Number of characters from __s to consider.
3016  * @return Index of first occurrence.
3017  *
3018  * Starting from @a __pos, searches forward for a character not
3019  * contained in the first @a __n characters of @a __s within
3020  * this string. If found, returns the index where it was
3021  * found. If not found, returns npos.
3022  */
3023  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3024  size_type
3025  find_first_not_of(const _CharT* __s, size_type __pos,
3026  size_type __n) const _GLIBCXX_NOEXCEPT;
3027 
3028  /**
3029  * @brief Find position of a character not in C string.
3030  * @param __s C string containing characters to avoid.
3031  * @param __pos Index of character to search from (default 0).
3032  * @return Index of first occurrence.
3033  *
3034  * Starting from @a __pos, searches forward for a character not
3035  * contained in @a __s within this string. If found, returns
3036  * the index where it was found. If not found, returns npos.
3037  */
3038  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3039  size_type
3040  find_first_not_of(const _CharT* __s, size_type __pos = 0) const
3041  _GLIBCXX_NOEXCEPT
3042  {
3043  __glibcxx_requires_string(__s);
3044  return this->find_first_not_of(__s, __pos, traits_type::length(__s));
3045  }
3046 
3047  /**
3048  * @brief Find position of a different character.
3049  * @param __c Character to avoid.
3050  * @param __pos Index of character to search from (default 0).
3051  * @return Index of first occurrence.
3052  *
3053  * Starting from @a __pos, searches forward for a character
3054  * other than @a __c within this string. If found, returns the
3055  * index where it was found. If not found, returns npos.
3056  */
3057  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3058  size_type
3059  find_first_not_of(_CharT __c, size_type __pos = 0) const
3060  _GLIBCXX_NOEXCEPT;
3061 
3062  /**
3063  * @brief Find last position of a character not in string.
3064  * @param __str String containing characters to avoid.
3065  * @param __pos Index of character to search back from (default end).
3066  * @return Index of last occurrence.
3067  *
3068  * Starting from @a __pos, searches backward for a character
3069  * not contained in @a __str within this string. If found,
3070  * returns the index where it was found. If not found, returns
3071  * npos.
3072  */
3073  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3074  size_type
3075  find_last_not_of(const basic_string& __str, size_type __pos = npos) const
3076  _GLIBCXX_NOEXCEPT
3077  { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
3078 
3079 #if __cplusplus >= 201703L
3080  /**
3081  * @brief Find last position of a character not in a string_view.
3082  * @param __svt An object convertible to string_view containing
3083  * characters to avoid.
3084  * @param __pos Index of character to search back from (default end).
3085  * @return Index of last occurrence.
3086  */
3087  template<typename _Tp>
3088  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3089  _If_sv<_Tp, size_type>
3090  find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
3091  noexcept(is_same<_Tp, __sv_type>::value)
3092  {
3093  __sv_type __sv = __svt;
3094  return this->find_last_not_of(__sv.data(), __pos, __sv.size());
3095  }
3096 #endif // C++17
3097 
3098  /**
3099  * @brief Find last position of a character not in C substring.
3100  * @param __s C string containing characters to avoid.
3101  * @param __pos Index of character to search back from.
3102  * @param __n Number of characters from s to consider.
3103  * @return Index of last occurrence.
3104  *
3105  * Starting from @a __pos, searches backward for a character not
3106  * contained in the first @a __n characters of @a __s within this string.
3107  * If found, returns the index where it was found. If not found,
3108  * returns npos.
3109  */
3110  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3111  size_type
3112  find_last_not_of(const _CharT* __s, size_type __pos,
3113  size_type __n) const _GLIBCXX_NOEXCEPT;
3114  /**
3115  * @brief Find last position of a character not in C string.
3116  * @param __s C string containing characters to avoid.
3117  * @param __pos Index of character to search back from (default end).
3118  * @return Index of last occurrence.
3119  *
3120  * Starting from @a __pos, searches backward for a character
3121  * not contained in @a __s within this string. If found,
3122  * returns the index where it was found. If not found, returns
3123  * npos.
3124  */
3125  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3126  size_type
3127  find_last_not_of(const _CharT* __s, size_type __pos = npos) const
3128  _GLIBCXX_NOEXCEPT
3129  {
3130  __glibcxx_requires_string(__s);
3131  return this->find_last_not_of(__s, __pos, traits_type::length(__s));
3132  }
3133 
3134  /**
3135  * @brief Find last position of a different character.
3136  * @param __c Character to avoid.
3137  * @param __pos Index of character to search back from (default end).
3138  * @return Index of last occurrence.
3139  *
3140  * Starting from @a __pos, searches backward for a character other than
3141  * @a __c within this string. If found, returns the index where it was
3142  * found. If not found, returns npos.
3143  */
3144  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3145  size_type
3146  find_last_not_of(_CharT __c, size_type __pos = npos) const
3147  _GLIBCXX_NOEXCEPT;
3148 
3149  /**
3150  * @brief Get a substring.
3151  * @param __pos Index of first character (default 0).
3152  * @param __n Number of characters in substring (default remainder).
3153  * @return The new string.
3154  * @throw std::out_of_range If __pos > size().
3155  *
3156  * Construct and return a new string using the @a __n
3157  * characters starting at @a __pos. If the string is too
3158  * short, use the remainder of the characters. If @a __pos is
3159  * beyond the end of the string, out_of_range is thrown.
3160  */
3161  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3162  basic_string
3163  substr(size_type __pos = 0, size_type __n = npos) const
3164  { return basic_string(*this,
3165  _M_check(__pos, "basic_string::substr"), __n); }
3166 
3167  /**
3168  * @brief Compare to a string.
3169  * @param __str String to compare against.
3170  * @return Integer < 0, 0, or > 0.
3171  *
3172  * Returns an integer < 0 if this string is ordered before @a
3173  * __str, 0 if their values are equivalent, or > 0 if this
3174  * string is ordered after @a __str. Determines the effective
3175  * length rlen of the strings to compare as the smallest of
3176  * size() and str.size(). The function then compares the two
3177  * strings by calling traits::compare(data(), str.data(),rlen).
3178  * If the result of the comparison is nonzero returns it,
3179  * otherwise the shorter one is ordered first.
3180  */
3181  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3182  int
3183  compare(const basic_string& __str) const
3184  {
3185  const size_type __size = this->size();
3186  const size_type __osize = __str.size();
3187  const size_type __len = std::min(__size, __osize);
3188 
3189  int __r = traits_type::compare(_M_data(), __str.data(), __len);
3190  if (!__r)
3191  __r = _S_compare(__size, __osize);
3192  return __r;
3193  }
3194 
3195 #if __cplusplus >= 201703L
3196  /**
3197  * @brief Compare to a string_view.
3198  * @param __svt An object convertible to string_view to compare against.
3199  * @return Integer < 0, 0, or > 0.
3200  */
3201  template<typename _Tp>
3202  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3203  _If_sv<_Tp, int>
3204  compare(const _Tp& __svt) const
3205  noexcept(is_same<_Tp, __sv_type>::value)
3206  {
3207  __sv_type __sv = __svt;
3208  const size_type __size = this->size();
3209  const size_type __osize = __sv.size();
3210  const size_type __len = std::min(__size, __osize);
3211 
3212  int __r = traits_type::compare(_M_data(), __sv.data(), __len);
3213  if (!__r)
3214  __r = _S_compare(__size, __osize);
3215  return __r;
3216  }
3217 
3218  /**
3219  * @brief Compare to a string_view.
3220  * @param __pos A position in the string to start comparing from.
3221  * @param __n The number of characters to compare.
3222  * @param __svt An object convertible to string_view to compare
3223  * against.
3224  * @return Integer < 0, 0, or > 0.
3225  */
3226  template<typename _Tp>
3227  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3228  _If_sv<_Tp, int>
3229  compare(size_type __pos, size_type __n, const _Tp& __svt) const
3230  noexcept(is_same<_Tp, __sv_type>::value)
3231  {
3232  __sv_type __sv = __svt;
3233  return __sv_type(*this).substr(__pos, __n).compare(__sv);
3234  }
3235 
3236  /**
3237  * @brief Compare to a string_view.
3238  * @param __pos1 A position in the string to start comparing from.
3239  * @param __n1 The number of characters to compare.
3240  * @param __svt An object convertible to string_view to compare
3241  * against.
3242  * @param __pos2 A position in the string_view to start comparing from.
3243  * @param __n2 The number of characters to compare.
3244  * @return Integer < 0, 0, or > 0.
3245  */
3246  template<typename _Tp>
3247  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3248  _If_sv<_Tp, int>
3249  compare(size_type __pos1, size_type __n1, const _Tp& __svt,
3250  size_type __pos2, size_type __n2 = npos) const
3251  noexcept(is_same<_Tp, __sv_type>::value)
3252  {
3253  __sv_type __sv = __svt;
3254  return __sv_type(*this)
3255  .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3256  }
3257 #endif // C++17
3258 
3259  /**
3260  * @brief Compare substring to a string.
3261  * @param __pos Index of first character of substring.
3262  * @param __n Number of characters in substring.
3263  * @param __str String to compare against.
3264  * @return Integer < 0, 0, or > 0.
3265  *
3266  * Form the substring of this string from the @a __n characters
3267  * starting at @a __pos. Returns an integer < 0 if the
3268  * substring is ordered before @a __str, 0 if their values are
3269  * equivalent, or > 0 if the substring is ordered after @a
3270  * __str. Determines the effective length rlen of the strings
3271  * to compare as the smallest of the length of the substring
3272  * and @a __str.size(). The function then compares the two
3273  * strings by calling
3274  * traits::compare(substring.data(),str.data(),rlen). If the
3275  * result of the comparison is nonzero returns it, otherwise
3276  * the shorter one is ordered first.
3277  */
3278  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3279  int
3280  compare(size_type __pos, size_type __n, const basic_string& __str) const
3281  {
3282  _M_check(__pos, "basic_string::compare");
3283  __n = _M_limit(__pos, __n);
3284  const size_type __osize = __str.size();
3285  const size_type __len = std::min(__n, __osize);
3286  int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
3287  if (!__r)
3288  __r = _S_compare(__n, __osize);
3289  return __r;
3290  }
3291 
3292  /**
3293  * @brief Compare substring to a substring.
3294  * @param __pos1 Index of first character of substring.
3295  * @param __n1 Number of characters in substring.
3296  * @param __str String to compare against.
3297  * @param __pos2 Index of first character of substring of str.
3298  * @param __n2 Number of characters in substring of str.
3299  * @return Integer < 0, 0, or > 0.
3300  *
3301  * Form the substring of this string from the @a __n1
3302  * characters starting at @a __pos1. Form the substring of @a
3303  * __str from the @a __n2 characters starting at @a __pos2.
3304  * Returns an integer < 0 if this substring is ordered before
3305  * the substring of @a __str, 0 if their values are equivalent,
3306  * or > 0 if this substring is ordered after the substring of
3307  * @a __str. Determines the effective length rlen of the
3308  * strings to compare as the smallest of the lengths of the
3309  * substrings. The function then compares the two strings by
3310  * calling
3311  * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
3312  * If the result of the comparison is nonzero returns it,
3313  * otherwise the shorter one is ordered first.
3314  */
3315  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3316  int
3317  compare(size_type __pos1, size_type __n1, const basic_string& __str,
3318  size_type __pos2, size_type __n2 = npos) const
3319  {
3320  _M_check(__pos1, "basic_string::compare");
3321  __str._M_check(__pos2, "basic_string::compare");
3322  __n1 = _M_limit(__pos1, __n1);
3323  __n2 = __str._M_limit(__pos2, __n2);
3324  const size_type __len = std::min(__n1, __n2);
3325  int __r = traits_type::compare(_M_data() + __pos1,
3326  __str.data() + __pos2, __len);
3327  if (!__r)
3328  __r = _S_compare(__n1, __n2);
3329  return __r;
3330  }
3331 
3332  /**
3333  * @brief Compare to a C string.
3334  * @param __s C string to compare against.
3335  * @return Integer < 0, 0, or > 0.
3336  *
3337  * Returns an integer < 0 if this string is ordered before @a __s, 0 if
3338  * their values are equivalent, or > 0 if this string is ordered after
3339  * @a __s. Determines the effective length rlen of the strings to
3340  * compare as the smallest of size() and the length of a string
3341  * constructed from @a __s. The function then compares the two strings
3342  * by calling traits::compare(data(),s,rlen). If the result of the
3343  * comparison is nonzero returns it, otherwise the shorter one is
3344  * ordered first.
3345  */
3346  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3347  int
3348  compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3349  {
3350  __glibcxx_requires_string(__s);
3351  const size_type __size = this->size();
3352  const size_type __osize = traits_type::length(__s);
3353  const size_type __len = std::min(__size, __osize);
3354  int __r = traits_type::compare(_M_data(), __s, __len);
3355  if (!__r)
3356  __r = _S_compare(__size, __osize);
3357  return __r;
3358  }
3359 
3360  // _GLIBCXX_RESOLVE_LIB_DEFECTS
3361  // 5 String::compare specification questionable
3362  /**
3363  * @brief Compare substring to a C string.
3364  * @param __pos Index of first character of substring.
3365  * @param __n1 Number of characters in substring.
3366  * @param __s C string to compare against.
3367  * @return Integer < 0, 0, or > 0.
3368  *
3369  * Form the substring of this string from the @a __n1
3370  * characters starting at @a pos. Returns an integer < 0 if
3371  * the substring is ordered before @a __s, 0 if their values
3372  * are equivalent, or > 0 if the substring is ordered after @a
3373  * __s. Determines the effective length rlen of the strings to
3374  * compare as the smallest of the length of the substring and
3375  * the length of a string constructed from @a __s. The
3376  * function then compares the two string by calling
3377  * traits::compare(substring.data(),__s,rlen). If the result of
3378  * the comparison is nonzero returns it, otherwise the shorter
3379  * one is ordered first.
3380  */
3381  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3382  int
3383  compare(size_type __pos, size_type __n1, const _CharT* __s) const
3384  {
3385  __glibcxx_requires_string(__s);
3386  _M_check(__pos, "basic_string::compare");
3387  __n1 = _M_limit(__pos, __n1);
3388  const size_type __osize = traits_type::length(__s);
3389  const size_type __len = std::min(__n1, __osize);
3390  int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3391  if (!__r)
3392  __r = _S_compare(__n1, __osize);
3393  return __r;
3394  }
3395 
3396  /**
3397  * @brief Compare substring against a character %array.
3398  * @param __pos Index of first character of substring.
3399  * @param __n1 Number of characters in substring.
3400  * @param __s character %array to compare against.
3401  * @param __n2 Number of characters of s.
3402  * @return Integer < 0, 0, or > 0.
3403  *
3404  * Form the substring of this string from the @a __n1
3405  * characters starting at @a __pos. Form a string from the
3406  * first @a __n2 characters of @a __s. Returns an integer < 0
3407  * if this substring is ordered before the string from @a __s,
3408  * 0 if their values are equivalent, or > 0 if this substring
3409  * is ordered after the string from @a __s. Determines the
3410  * effective length rlen of the strings to compare as the
3411  * smallest of the length of the substring and @a __n2. The
3412  * function then compares the two strings by calling
3413  * traits::compare(substring.data(),s,rlen). If the result of
3414  * the comparison is nonzero returns it, otherwise the shorter
3415  * one is ordered first.
3416  *
3417  * NB: s must have at least n2 characters, &apos;\\0&apos; has
3418  * no special meaning.
3419  */
3420  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3421  int
3422  compare(size_type __pos, size_type __n1, const _CharT* __s,
3423  size_type __n2) const
3424  {
3425  __glibcxx_requires_string_len(__s, __n2);
3426  _M_check(__pos, "basic_string::compare");
3427  __n1 = _M_limit(__pos, __n1);
3428  const size_type __len = std::min(__n1, __n2);
3429  int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3430  if (!__r)
3431  __r = _S_compare(__n1, __n2);
3432  return __r;
3433  }
3434 
3435 #if __cplusplus >= 202002L
3436  [[nodiscard]]
3437  constexpr bool
3438  starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3439  { return __sv_type(this->data(), this->size()).starts_with(__x); }
3440 
3441  [[nodiscard]]
3442  constexpr bool
3443  starts_with(_CharT __x) const noexcept
3444  { return __sv_type(this->data(), this->size()).starts_with(__x); }
3445 
3446  [[nodiscard, __gnu__::__nonnull__]]
3447  constexpr bool
3448  starts_with(const _CharT* __x) const noexcept
3449  { return __sv_type(this->data(), this->size()).starts_with(__x); }
3450 
3451  [[nodiscard]]
3452  constexpr bool
3453  ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3454  { return __sv_type(this->data(), this->size()).ends_with(__x); }
3455 
3456  [[nodiscard]]
3457  constexpr bool
3458  ends_with(_CharT __x) const noexcept
3459  { return __sv_type(this->data(), this->size()).ends_with(__x); }
3460 
3461  [[nodiscard, __gnu__::__nonnull__]]
3462  constexpr bool
3463  ends_with(const _CharT* __x) const noexcept
3464  { return __sv_type(this->data(), this->size()).ends_with(__x); }
3465 #endif // C++20
3466 
3467 #if __cplusplus > 202002L
3468  [[nodiscard]]
3469  constexpr bool
3470  contains(basic_string_view<_CharT, _Traits> __x) const noexcept
3471  { return __sv_type(this->data(), this->size()).contains(__x); }
3472 
3473  [[nodiscard]]
3474  constexpr bool
3475  contains(_CharT __x) const noexcept
3476  { return __sv_type(this->data(), this->size()).contains(__x); }
3477 
3478  [[nodiscard, __gnu__::__nonnull__]]
3479  constexpr bool
3480  contains(const _CharT* __x) const noexcept
3481  { return __sv_type(this->data(), this->size()).contains(__x); }
3482 #endif // C++23
3483 
3484  // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3485  template<typename, typename, typename> friend class basic_stringbuf;
3486  };
3487 _GLIBCXX_END_NAMESPACE_CXX11
3488 _GLIBCXX_END_NAMESPACE_VERSION
3489 } // namespace std
3490 #endif // _GLIBCXX_USE_CXX11_ABI
3491 
3492 namespace std _GLIBCXX_VISIBILITY(default)
3493 {
3494 _GLIBCXX_BEGIN_NAMESPACE_VERSION
3495 
3496 #if __cpp_deduction_guides >= 201606
3497 _GLIBCXX_BEGIN_NAMESPACE_CXX11
3498  template<typename _InputIterator, typename _CharT
3499  = typename iterator_traits<_InputIterator>::value_type,
3500  typename _Allocator = allocator<_CharT>,
3501  typename = _RequireInputIter<_InputIterator>,
3502  typename = _RequireAllocator<_Allocator>>
3503  basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
3504  -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
3505 
3506  // _GLIBCXX_RESOLVE_LIB_DEFECTS
3507  // 3075. basic_string needs deduction guides from basic_string_view
3508  template<typename _CharT, typename _Traits,
3509  typename _Allocator = allocator<_CharT>,
3510  typename = _RequireAllocator<_Allocator>>
3511  basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
3512  -> basic_string<_CharT, _Traits, _Allocator>;
3513 
3514  template<typename _CharT, typename _Traits,
3515  typename _Allocator = allocator<_CharT>,
3516  typename = _RequireAllocator<_Allocator>>
3517  basic_string(basic_string_view<_CharT, _Traits>,
3518  typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3519  typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3520  const _Allocator& = _Allocator())
3521  -> basic_string<_CharT, _Traits, _Allocator>;
3522 _GLIBCXX_END_NAMESPACE_CXX11
3523 #endif
3524 
3525  template<typename _Str>
3526  _GLIBCXX20_CONSTEXPR
3527  inline _Str
3528  __str_concat(typename _Str::value_type const* __lhs,
3529  typename _Str::size_type __lhs_len,
3530  typename _Str::value_type const* __rhs,
3531  typename _Str::size_type __rhs_len,
3532  typename _Str::allocator_type const& __a)
3533  {
3534  typedef typename _Str::allocator_type allocator_type;
3535  typedef __gnu_cxx::__alloc_traits<allocator_type> _Alloc_traits;
3536  _Str __str(_Alloc_traits::_S_select_on_copy(__a));
3537  __str.reserve(__lhs_len + __rhs_len);
3538  __str.append(__lhs, __lhs_len);
3539  __str.append(__rhs, __rhs_len);
3540  return __str;
3541  }
3542 
3543  // operator+
3544  /**
3545  * @brief Concatenate two strings.
3546  * @param __lhs First string.
3547  * @param __rhs Last string.
3548  * @return New string with value of @a __lhs followed by @a __rhs.
3549  */
3550  template<typename _CharT, typename _Traits, typename _Alloc>
3551  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3552  inline basic_string<_CharT, _Traits, _Alloc>
3555  {
3557  return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3558  __rhs.c_str(), __rhs.size(),
3559  __lhs.get_allocator());
3560  }
3561 
3562  /**
3563  * @brief Concatenate C string and string.
3564  * @param __lhs First string.
3565  * @param __rhs Last string.
3566  * @return New string with value of @a __lhs followed by @a __rhs.
3567  */
3568  template<typename _CharT, typename _Traits, typename _Alloc>
3569  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3570  inline basic_string<_CharT,_Traits,_Alloc>
3571  operator+(const _CharT* __lhs,
3573  {
3574  __glibcxx_requires_string(__lhs);
3576  return std::__str_concat<_Str>(__lhs, _Traits::length(__lhs),
3577  __rhs.c_str(), __rhs.size(),
3578  __rhs.get_allocator());
3579  }
3580 
3581  /**
3582  * @brief Concatenate character and string.
3583  * @param __lhs First string.
3584  * @param __rhs Last string.
3585  * @return New string with @a __lhs followed by @a __rhs.
3586  */
3587  template<typename _CharT, typename _Traits, typename _Alloc>
3588  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3589  inline basic_string<_CharT,_Traits,_Alloc>
3591  {
3593  return std::__str_concat<_Str>(__builtin_addressof(__lhs), 1,
3594  __rhs.c_str(), __rhs.size(),
3595  __rhs.get_allocator());
3596  }
3597 
3598  /**
3599  * @brief Concatenate string and C string.
3600  * @param __lhs First string.
3601  * @param __rhs Last string.
3602  * @return New string with @a __lhs followed by @a __rhs.
3603  */
3604  template<typename _CharT, typename _Traits, typename _Alloc>
3605  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3606  inline basic_string<_CharT, _Traits, _Alloc>
3608  const _CharT* __rhs)
3609  {
3610  __glibcxx_requires_string(__rhs);
3612  return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3613  __rhs, _Traits::length(__rhs),
3614  __lhs.get_allocator());
3615  }
3616  /**
3617  * @brief Concatenate string and character.
3618  * @param __lhs First string.
3619  * @param __rhs Last string.
3620  * @return New string with @a __lhs followed by @a __rhs.
3621  */
3622  template<typename _CharT, typename _Traits, typename _Alloc>
3623  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3624  inline basic_string<_CharT, _Traits, _Alloc>
3626  {
3628  return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3629  __builtin_addressof(__rhs), 1,
3630  __lhs.get_allocator());
3631  }
3632 
3633 #if __cplusplus >= 201103L
3634  template<typename _CharT, typename _Traits, typename _Alloc>
3635  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3636  inline basic_string<_CharT, _Traits, _Alloc>
3637  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3638  const basic_string<_CharT, _Traits, _Alloc>& __rhs)
3639  { return std::move(__lhs.append(__rhs)); }
3640 
3641  template<typename _CharT, typename _Traits, typename _Alloc>
3642  _GLIBCXX20_CONSTEXPR
3643  inline basic_string<_CharT, _Traits, _Alloc>
3644  operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3645  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
3646  { return std::move(__rhs.insert(0, __lhs)); }
3647 
3648  template<typename _CharT, typename _Traits, typename _Alloc>
3649  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3650  inline basic_string<_CharT, _Traits, _Alloc>
3651  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3652  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
3653  {
3654 #if _GLIBCXX_USE_CXX11_ABI
3655  using _Alloc_traits = allocator_traits<_Alloc>;
3656  bool __use_rhs = false;
3657  if _GLIBCXX17_CONSTEXPR (typename _Alloc_traits::is_always_equal{})
3658  __use_rhs = true;
3659  else if (__lhs.get_allocator() == __rhs.get_allocator())
3660  __use_rhs = true;
3661  if (__use_rhs)
3662 #endif
3663  {
3664  const auto __size = __lhs.size() + __rhs.size();
3665  if (__size > __lhs.capacity() && __size <= __rhs.capacity())
3666  return std::move(__rhs.insert(0, __lhs));
3667  }
3668  return std::move(__lhs.append(__rhs));
3669  }
3670 
3671  template<typename _CharT, typename _Traits, typename _Alloc>
3672  _GLIBCXX_NODISCARD _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3673  inline basic_string<_CharT, _Traits, _Alloc>
3674  operator+(const _CharT* __lhs,
3675  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
3676  { return std::move(__rhs.insert(0, __lhs)); }
3677 
3678  template<typename _CharT, typename _Traits, typename _Alloc>
3679  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3680  inline basic_string<_CharT, _Traits, _Alloc>
3681  operator+(_CharT __lhs,
3682  basic_string<_CharT, _Traits, _Alloc>&& __rhs)
3683  { return std::move(__rhs.insert(0, 1, __lhs)); }
3684 
3685  template<typename _CharT, typename _Traits, typename _Alloc>
3686  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3687  inline basic_string<_CharT, _Traits, _Alloc>
3688  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3689  const _CharT* __rhs)
3690  { return std::move(__lhs.append(__rhs)); }
3691 
3692  template<typename _CharT, typename _Traits, typename _Alloc>
3693  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3694  inline basic_string<_CharT, _Traits, _Alloc>
3695  operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3696  _CharT __rhs)
3697  { return std::move(__lhs.append(1, __rhs)); }
3698 #endif
3699 
3700  // operator ==
3701  /**
3702  * @brief Test equivalence of two strings.
3703  * @param __lhs First string.
3704  * @param __rhs Second string.
3705  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
3706  */
3707  template<typename _CharT, typename _Traits, typename _Alloc>
3708  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3709  inline bool
3710  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3712  _GLIBCXX_NOEXCEPT
3713  {
3714  return __lhs.size() == __rhs.size()
3715  && !_Traits::compare(__lhs.data(), __rhs.data(), __lhs.size());
3716  }
3717 
3718  /**
3719  * @brief Test equivalence of string and C string.
3720  * @param __lhs String.
3721  * @param __rhs C string.
3722  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
3723  */
3724  template<typename _CharT, typename _Traits, typename _Alloc>
3725  _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3726  inline bool
3727  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3728  const _CharT* __rhs)
3729  {
3730  return __lhs.size() == _Traits::length(__rhs)
3731  && !_Traits::compare(__lhs.data(), __rhs, __lhs.size());
3732  }
3733 
3734 #if __cpp_lib_three_way_comparison
3735  /**
3736  * @brief Three-way comparison of a string and a C string.
3737  * @param __lhs A string.
3738  * @param __rhs A null-terminated string.
3739  * @return A value indicating whether `__lhs` is less than, equal to,
3740  * greater than, or incomparable with `__rhs`.
3741  */
3742  template<typename _CharT, typename _Traits, typename _Alloc>
3743  [[nodiscard]]
3744  constexpr auto
3745  operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3746  const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept
3747  -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
3748  { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
3749 
3750  /**
3751  * @brief Three-way comparison of a string and a C string.
3752  * @param __lhs A string.
3753  * @param __rhs A null-terminated string.
3754  * @return A value indicating whether `__lhs` is less than, equal to,
3755  * greater than, or incomparable with `__rhs`.
3756  */
3757  template<typename _CharT, typename _Traits, typename _Alloc>
3758  [[nodiscard]]
3759  constexpr auto
3760  operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3761  const _CharT* __rhs) noexcept
3762  -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
3763  { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
3764 #else
3765  /**
3766  * @brief Test equivalence of C string and string.
3767  * @param __lhs C string.
3768  * @param __rhs String.
3769  * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
3770  */
3771  template<typename _CharT, typename _Traits, typename _Alloc>
3772  _GLIBCXX_NODISCARD
3773  inline bool
3774  operator==(const _CharT* __lhs,
3776  { return __rhs == __lhs; }
3777 
3778  // operator !=
3779  /**
3780  * @brief Test difference of two strings.
3781  * @param __lhs First string.
3782  * @param __rhs Second string.
3783  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
3784  */
3785  template<typename _CharT, typename _Traits, typename _Alloc>
3786  _GLIBCXX_NODISCARD
3787  inline bool
3788  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3790  _GLIBCXX_NOEXCEPT
3791  { return !(__lhs == __rhs); }
3792 
3793  /**
3794  * @brief Test difference of C string and string.
3795  * @param __lhs C string.
3796  * @param __rhs String.
3797  * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
3798  */
3799  template<typename _CharT, typename _Traits, typename _Alloc>
3800  _GLIBCXX_NODISCARD
3801  inline bool
3802  operator!=(const _CharT* __lhs,
3804  { return !(__rhs == __lhs); }
3805 
3806  /**
3807  * @brief Test difference of string and C string.
3808  * @param __lhs String.
3809  * @param __rhs C string.
3810  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
3811  */
3812  template<typename _CharT, typename _Traits, typename _Alloc>
3813  _GLIBCXX_NODISCARD
3814  inline bool
3815  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3816  const _CharT* __rhs)
3817  { return !(__lhs == __rhs); }
3818 
3819  // operator <
3820  /**
3821  * @brief Test if string precedes string.
3822  * @param __lhs First string.
3823  * @param __rhs Second string.
3824  * @return True if @a __lhs precedes @a __rhs. False otherwise.
3825  */
3826  template<typename _CharT, typename _Traits, typename _Alloc>
3827  _GLIBCXX_NODISCARD
3828  inline bool
3829  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3831  _GLIBCXX_NOEXCEPT
3832  { return __lhs.compare(__rhs) < 0; }
3833 
3834  /**
3835  * @brief Test if string precedes C string.
3836  * @param __lhs String.
3837  * @param __rhs C string.
3838  * @return True if @a __lhs precedes @a __rhs. False otherwise.
3839  */
3840  template<typename _CharT, typename _Traits, typename _Alloc>
3841  _GLIBCXX_NODISCARD
3842  inline bool
3843  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3844  const _CharT* __rhs)
3845  { return __lhs.compare(__rhs) < 0; }
3846 
3847  /**
3848  * @brief Test if C string precedes string.
3849  * @param __lhs C string.
3850  * @param __rhs String.
3851  * @return True if @a __lhs precedes @a __rhs. False otherwise.
3852  */
3853  template<typename _CharT, typename _Traits, typename _Alloc>
3854  _GLIBCXX_NODISCARD
3855  inline bool
3856  operator<(const _CharT* __lhs,
3858  { return __rhs.compare(__lhs) > 0; }
3859 
3860  // operator >
3861  /**
3862  * @brief Test if string follows string.
3863  * @param __lhs First string.
3864  * @param __rhs Second string.
3865  * @return True if @a __lhs follows @a __rhs. False otherwise.
3866  */
3867  template<typename _CharT, typename _Traits, typename _Alloc>
3868  _GLIBCXX_NODISCARD
3869  inline bool
3872  _GLIBCXX_NOEXCEPT
3873  { return __lhs.compare(__rhs) > 0; }
3874 
3875  /**
3876  * @brief Test if string follows C string.
3877  * @param __lhs String.
3878  * @param __rhs C string.
3879  * @return True if @a __lhs follows @a __rhs. False otherwise.
3880  */
3881  template<typename _CharT, typename _Traits, typename _Alloc>
3882  _GLIBCXX_NODISCARD
3883  inline bool
3885  const _CharT* __rhs)
3886  { return __lhs.compare(__rhs) > 0; }
3887 
3888  /**
3889  * @brief Test if C string follows string.
3890  * @param __lhs C string.
3891  * @param __rhs String.
3892  * @return True if @a __lhs follows @a __rhs. False otherwise.
3893  */
3894  template<typename _CharT, typename _Traits, typename _Alloc>
3895  _GLIBCXX_NODISCARD
3896  inline bool
3897  operator>(const _CharT* __lhs,
3899  { return __rhs.compare(__lhs) < 0; }
3900 
3901  // operator <=
3902  /**
3903  * @brief Test if string doesn't follow string.
3904  * @param __lhs First string.
3905  * @param __rhs Second string.
3906  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
3907  */
3908  template<typename _CharT, typename _Traits, typename _Alloc>
3909  _GLIBCXX_NODISCARD
3910  inline bool
3911  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3913  _GLIBCXX_NOEXCEPT
3914  { return __lhs.compare(__rhs) <= 0; }
3915 
3916  /**
3917  * @brief Test if string doesn't follow C string.
3918  * @param __lhs String.
3919  * @param __rhs C string.
3920  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
3921  */
3922  template<typename _CharT, typename _Traits, typename _Alloc>
3923  _GLIBCXX_NODISCARD
3924  inline bool
3925  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3926  const _CharT* __rhs)
3927  { return __lhs.compare(__rhs) <= 0; }
3928 
3929  /**
3930  * @brief Test if C string doesn't follow string.
3931  * @param __lhs C string.
3932  * @param __rhs String.
3933  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
3934  */
3935  template<typename _CharT, typename _Traits, typename _Alloc>
3936  _GLIBCXX_NODISCARD
3937  inline bool
3938  operator<=(const _CharT* __lhs,
3940  { return __rhs.compare(__lhs) >= 0; }
3941 
3942  // operator >=
3943  /**
3944  * @brief Test if string doesn't precede string.
3945  * @param __lhs First string.
3946  * @param __rhs Second string.
3947  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
3948  */
3949  template<typename _CharT, typename _Traits, typename _Alloc>
3950  _GLIBCXX_NODISCARD
3951  inline bool
3952  operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3954  _GLIBCXX_NOEXCEPT
3955  { return __lhs.compare(__rhs) >= 0; }
3956 
3957  /**
3958  * @brief Test if string doesn't precede C string.
3959  * @param __lhs String.
3960  * @param __rhs C string.
3961  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
3962  */
3963  template<typename _CharT, typename _Traits, typename _Alloc>
3964  _GLIBCXX_NODISCARD
3965  inline bool
3966  operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3967  const _CharT* __rhs)
3968  { return __lhs.compare(__rhs) >= 0; }
3969 
3970  /**
3971  * @brief Test if C string doesn't precede string.
3972  * @param __lhs C string.
3973  * @param __rhs String.
3974  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
3975  */
3976  template<typename _CharT, typename _Traits, typename _Alloc>
3977  _GLIBCXX_NODISCARD
3978  inline bool
3979  operator>=(const _CharT* __lhs,
3981  { return __rhs.compare(__lhs) <= 0; }
3982 #endif // three-way comparison
3983 
3984  /**
3985  * @brief Swap contents of two strings.
3986  * @param __lhs First string.
3987  * @param __rhs Second string.
3988  *
3989  * Exchanges the contents of @a __lhs and @a __rhs in constant time.
3990  */
3991  template<typename _CharT, typename _Traits, typename _Alloc>
3992  _GLIBCXX20_CONSTEXPR
3993  inline void
3996  _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
3997  { __lhs.swap(__rhs); }
3998 
3999 
4000  /**
4001  * @brief Read stream into a string.
4002  * @param __is Input stream.
4003  * @param __str Buffer to store into.
4004  * @return Reference to the input stream.
4005  *
4006  * Stores characters from @a __is into @a __str until whitespace is
4007  * found, the end of the stream is encountered, or str.max_size()
4008  * is reached. If is.width() is non-zero, that is the limit on the
4009  * number of characters stored into @a __str. Any previous
4010  * contents of @a __str are erased.
4011  */
4012  template<typename _CharT, typename _Traits, typename _Alloc>
4013  basic_istream<_CharT, _Traits>&
4014  operator>>(basic_istream<_CharT, _Traits>& __is,
4015  basic_string<_CharT, _Traits, _Alloc>& __str);
4016 
4017  template<>
4018  basic_istream<char>&
4020 
4021  /**
4022  * @brief Write string to a stream.
4023  * @param __os Output stream.
4024  * @param __str String to write out.
4025  * @return Reference to the output stream.
4026  *
4027  * Output characters of @a __str into os following the same rules as for
4028  * writing a C string.
4029  */
4030  template<typename _CharT, typename _Traits, typename _Alloc>
4034  {
4035  // _GLIBCXX_RESOLVE_LIB_DEFECTS
4036  // 586. string inserter not a formatted function
4037  return __ostream_insert(__os, __str.data(), __str.size());
4038  }
4039 
4040  /**
4041  * @brief Read a line from stream into a string.
4042  * @param __is Input stream.
4043  * @param __str Buffer to store into.
4044  * @param __delim Character marking end of line.
4045  * @return Reference to the input stream.
4046  *
4047  * Stores characters from @a __is into @a __str until @a __delim is
4048  * found, the end of the stream is encountered, or str.max_size()
4049  * is reached. Any previous contents of @a __str are erased. If
4050  * @a __delim is encountered, it is extracted but not stored into
4051  * @a __str.
4052  */
4053  template<typename _CharT, typename _Traits, typename _Alloc>
4054  basic_istream<_CharT, _Traits>&
4055  getline(basic_istream<_CharT, _Traits>& __is,
4056  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
4057 
4058  /**
4059  * @brief Read a line from stream into a string.
4060  * @param __is Input stream.
4061  * @param __str Buffer to store into.
4062  * @return Reference to the input stream.
4063  *
4064  * Stores characters from is into @a __str until &apos;\n&apos; is
4065  * found, the end of the stream is encountered, or str.max_size()
4066  * is reached. Any previous contents of @a __str are erased. If
4067  * end of line is encountered, it is extracted but not stored into
4068  * @a __str.
4069  */
4070  template<typename _CharT, typename _Traits, typename _Alloc>
4071  inline basic_istream<_CharT, _Traits>&
4074  { return std::getline(__is, __str, __is.widen('\n')); }
4075 
4076 #if __cplusplus >= 201103L
4077  /// Read a line from an rvalue stream into a string.
4078  template<typename _CharT, typename _Traits, typename _Alloc>
4079  inline basic_istream<_CharT, _Traits>&
4081  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
4082  { return std::getline(__is, __str, __delim); }
4083 
4084  /// Read a line from an rvalue stream into a string.
4085  template<typename _CharT, typename _Traits, typename _Alloc>
4086  inline basic_istream<_CharT, _Traits>&
4089  { return std::getline(__is, __str); }
4090 #endif
4091 
4092  template<>
4093  basic_istream<char>&
4094  getline(basic_istream<char>& __in, basic_string<char>& __str,
4095  char __delim);
4096 
4097 #ifdef _GLIBCXX_USE_WCHAR_T
4098  template<>
4099  basic_istream<wchar_t>&
4100  getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
4101  wchar_t __delim);
4102 #endif
4103 
4104 _GLIBCXX_END_NAMESPACE_VERSION
4105 } // namespace
4106 
4107 #if __cplusplus >= 201103L
4108 
4109 #include <ext/string_conversions.h>
4110 #include <bits/charconv.h>
4111 
4112 namespace std _GLIBCXX_VISIBILITY(default)
4113 {
4114 _GLIBCXX_BEGIN_NAMESPACE_VERSION
4115 _GLIBCXX_BEGIN_NAMESPACE_CXX11
4116 
4117 #if _GLIBCXX_USE_C99_STDLIB
4118  // 21.4 Numeric Conversions [string.conversions].
4119  inline int
4120  stoi(const string& __str, size_t* __idx = 0, int __base = 10)
4121  { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
4122  __idx, __base); }
4123 
4124  inline long
4125  stol(const string& __str, size_t* __idx = 0, int __base = 10)
4126  { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
4127  __idx, __base); }
4128 
4129  inline unsigned long
4130  stoul(const string& __str, size_t* __idx = 0, int __base = 10)
4131  { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
4132  __idx, __base); }
4133 
4134  inline long long
4135  stoll(const string& __str, size_t* __idx = 0, int __base = 10)
4136  { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
4137  __idx, __base); }
4138 
4139  inline unsigned long long
4140  stoull(const string& __str, size_t* __idx = 0, int __base = 10)
4141  { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
4142  __idx, __base); }
4143 
4144  // NB: strtof vs strtod.
4145  inline float
4146  stof(const string& __str, size_t* __idx = 0)
4147  { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
4148 
4149  inline double
4150  stod(const string& __str, size_t* __idx = 0)
4151  { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
4152 
4153  inline long double
4154  stold(const string& __str, size_t* __idx = 0)
4155  { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
4156 #endif // _GLIBCXX_USE_C99_STDLIB
4157 
4158  // DR 1261. Insufficent overloads for to_string / to_wstring
4159 
4160  _GLIBCXX_NODISCARD
4161  inline string
4162  to_string(int __val)
4163 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4164  noexcept // any 32-bit value fits in the SSO buffer
4165 #endif
4166  {
4167  const bool __neg = __val < 0;
4168  const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
4169  const auto __len = __detail::__to_chars_len(__uval);
4170  string __str(__neg + __len, '-');
4171  __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
4172  return __str;
4173  }
4174 
4175  _GLIBCXX_NODISCARD
4176  inline string
4177  to_string(unsigned __val)
4178 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4179  noexcept // any 32-bit value fits in the SSO buffer
4180 #endif
4181  {
4182  string __str(__detail::__to_chars_len(__val), '\0');
4183  __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
4184  return __str;
4185  }
4186 
4187  _GLIBCXX_NODISCARD
4188  inline string
4189  to_string(long __val)
4190 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4191  noexcept // any 32-bit value fits in the SSO buffer
4192 #endif
4193  {
4194  const bool __neg = __val < 0;
4195  const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
4196  const auto __len = __detail::__to_chars_len(__uval);
4197  string __str(__neg + __len, '-');
4198  __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
4199  return __str;
4200  }
4201 
4202  _GLIBCXX_NODISCARD
4203  inline string
4204  to_string(unsigned long __val)
4205 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4206  noexcept // any 32-bit value fits in the SSO buffer
4207 #endif
4208  {
4209  string __str(__detail::__to_chars_len(__val), '\0');
4210  __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
4211  return __str;
4212  }
4213 
4214  _GLIBCXX_NODISCARD
4215  inline string
4216  to_string(long long __val)
4217  {
4218  const bool __neg = __val < 0;
4219  const unsigned long long __uval
4220  = __neg ? (unsigned long long)~__val + 1ull : __val;
4221  const auto __len = __detail::__to_chars_len(__uval);
4222  string __str(__neg + __len, '-');
4223  __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
4224  return __str;
4225  }
4226 
4227  _GLIBCXX_NODISCARD
4228  inline string
4229  to_string(unsigned long long __val)
4230  {
4231  string __str(__detail::__to_chars_len(__val), '\0');
4232  __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
4233  return __str;
4234  }
4235 
4236 #if _GLIBCXX_USE_C99_STDIO
4237  // NB: (v)snprintf vs sprintf.
4238 
4239  _GLIBCXX_NODISCARD
4240  inline string
4241  to_string(float __val)
4242  {
4243  const int __n =
4244  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
4245  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4246  "%f", __val);
4247  }
4248 
4249  _GLIBCXX_NODISCARD
4250  inline string
4251  to_string(double __val)
4252  {
4253  const int __n =
4254  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
4255  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4256  "%f", __val);
4257  }
4258 
4259  _GLIBCXX_NODISCARD
4260  inline string
4261  to_string(long double __val)
4262  {
4263  const int __n =
4264  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
4265  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4266  "%Lf", __val);
4267  }
4268 #endif // _GLIBCXX_USE_C99_STDIO
4269 
4270 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
4271  inline int
4272  stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
4273  { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
4274  __idx, __base); }
4275 
4276  inline long
4277  stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
4278  { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
4279  __idx, __base); }
4280 
4281  inline unsigned long
4282  stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
4283  { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
4284  __idx, __base); }
4285 
4286  inline long long
4287  stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
4288  { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
4289  __idx, __base); }
4290 
4291  inline unsigned long long
4292  stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
4293  { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
4294  __idx, __base); }
4295 
4296  // NB: wcstof vs wcstod.
4297  inline float
4298  stof(const wstring& __str, size_t* __idx = 0)
4299  { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
4300 
4301  inline double
4302  stod(const wstring& __str, size_t* __idx = 0)
4303  { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
4304 
4305  inline long double
4306  stold(const wstring& __str, size_t* __idx = 0)
4307  { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
4308 
4309 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
4310  // DR 1261.
4311  _GLIBCXX_NODISCARD
4312  inline wstring
4313  to_wstring(int __val)
4314  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
4315  L"%d", __val); }
4316 
4317  _GLIBCXX_NODISCARD
4318  inline wstring
4319  to_wstring(unsigned __val)
4320  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
4321  4 * sizeof(unsigned),
4322  L"%u", __val); }
4323 
4324  _GLIBCXX_NODISCARD
4325  inline wstring
4326  to_wstring(long __val)
4327  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
4328  L"%ld", __val); }
4329 
4330  _GLIBCXX_NODISCARD
4331  inline wstring
4332  to_wstring(unsigned long __val)
4333  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
4334  4 * sizeof(unsigned long),
4335  L"%lu", __val); }
4336 
4337  _GLIBCXX_NODISCARD
4338  inline wstring
4339  to_wstring(long long __val)
4340  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
4341  4 * sizeof(long long),
4342  L"%lld", __val); }
4343 
4344  _GLIBCXX_NODISCARD
4345  inline wstring
4346  to_wstring(unsigned long long __val)
4347  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
4348  4 * sizeof(unsigned long long),
4349  L"%llu", __val); }
4350 
4351  _GLIBCXX_NODISCARD
4352  inline wstring
4353  to_wstring(float __val)
4354  {
4355  const int __n =
4356  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
4357  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
4358  L"%f", __val);
4359  }
4360 
4361  _GLIBCXX_NODISCARD
4362  inline wstring
4363  to_wstring(double __val)
4364  {
4365  const int __n =
4366  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
4367  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
4368  L"%f", __val);
4369  }
4370 
4371  _GLIBCXX_NODISCARD
4372  inline wstring
4373  to_wstring(long double __val)
4374  {
4375  const int __n =
4376  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
4377  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
4378  L"%Lf", __val);
4379  }
4380 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
4381 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
4382 
4383 _GLIBCXX_END_NAMESPACE_CXX11
4384 _GLIBCXX_END_NAMESPACE_VERSION
4385 } // namespace
4386 
4387 #endif /* C++11 */
4388 
4389 #if __cplusplus >= 201103L
4390 
4391 #include <bits/functional_hash.h>
4392 
4393 namespace std _GLIBCXX_VISIBILITY(default)
4394 {
4395 _GLIBCXX_BEGIN_NAMESPACE_VERSION
4396 
4397  // _GLIBCXX_RESOLVE_LIB_DEFECTS
4398  // 3705. Hashability shouldn't depend on basic_string's allocator
4399 
4400  template<typename _CharT, typename _Alloc,
4401  typename _StrT = basic_string<_CharT, char_traits<_CharT>, _Alloc>>
4402  struct __str_hash_base
4403  : public __hash_base<size_t, _StrT>
4404  {
4405  [[__nodiscard__]]
4406  size_t
4407  operator()(const _StrT& __s) const noexcept
4408  { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(_CharT)); }
4409  };
4410 
4411 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
4412  /// std::hash specialization for string.
4413  template<typename _Alloc>
4414  struct hash<basic_string<char, char_traits<char>, _Alloc>>
4415  : public __str_hash_base<char, _Alloc>
4416  { };
4417 
4418  /// std::hash specialization for wstring.
4419  template<typename _Alloc>
4420  struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Alloc>>
4421  : public __str_hash_base<wchar_t, _Alloc>
4422  { };
4423 
4424  template<typename _Alloc>
4425  struct __is_fast_hash<hash<basic_string<wchar_t, char_traits<wchar_t>,
4426  _Alloc>>>
4427  : std::false_type
4428  { };
4429 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
4430 
4431 #ifdef _GLIBCXX_USE_CHAR8_T
4432  /// std::hash specialization for u8string.
4433  template<typename _Alloc>
4434  struct hash<basic_string<char8_t, char_traits<char8_t>, _Alloc>>
4435  : public __str_hash_base<char8_t, _Alloc>
4436  { };
4437 #endif
4438 
4439  /// std::hash specialization for u16string.
4440  template<typename _Alloc>
4441  struct hash<basic_string<char16_t, char_traits<char16_t>, _Alloc>>
4442  : public __str_hash_base<char16_t, _Alloc>
4443  { };
4444 
4445  /// std::hash specialization for u32string.
4446  template<typename _Alloc>
4447  struct hash<basic_string<char32_t, char_traits<char32_t>, _Alloc>>
4448  : public __str_hash_base<char32_t, _Alloc>
4449  { };
4450 
4451 #if ! _GLIBCXX_INLINE_VERSION
4452  // PR libstdc++/105907 - __is_fast_hash affects unordered container ABI.
4453  template<> struct __is_fast_hash<hash<string>> : std::false_type { };
4454  template<> struct __is_fast_hash<hash<wstring>> : std::false_type { };
4455  template<> struct __is_fast_hash<hash<u16string>> : std::false_type { };
4456  template<> struct __is_fast_hash<hash<u32string>> : std::false_type { };
4457 #ifdef _GLIBCXX_USE_CHAR8_T
4458  template<> struct __is_fast_hash<hash<u8string>> : std::false_type { };
4459 #endif
4460 #else
4461  // For versioned namespace, assume every std::hash<basic_string<>> is slow.
4462  template<typename _CharT, typename _Traits, typename _Alloc>
4463  struct __is_fast_hash<hash<basic_string<_CharT, _Traits, _Alloc>>>
4464  : std::false_type
4465  { };
4466 #endif
4467 
4468 #if __cplusplus >= 201402L
4469 
4470 #define __cpp_lib_string_udls 201304L
4471 
4472  inline namespace literals
4473  {
4474  inline namespace string_literals
4475  {
4476 #pragma GCC diagnostic push
4477 #pragma GCC diagnostic ignored "-Wliteral-suffix"
4478 
4479 #if __cpp_lib_constexpr_string >= 201907L
4480 # define _GLIBCXX_STRING_CONSTEXPR constexpr
4481 #else
4482 # define _GLIBCXX_STRING_CONSTEXPR
4483 #endif
4484 
4485  _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4486  inline basic_string<char>
4487  operator""s(const char* __str, size_t __len)
4488  { return basic_string<char>{__str, __len}; }
4489 
4490  _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4491  inline basic_string<wchar_t>
4492  operator""s(const wchar_t* __str, size_t __len)
4493  { return basic_string<wchar_t>{__str, __len}; }
4494 
4495 #ifdef _GLIBCXX_USE_CHAR8_T
4496  _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4497  inline basic_string<char8_t>
4498  operator""s(const char8_t* __str, size_t __len)
4499  { return basic_string<char8_t>{__str, __len}; }
4500 #endif
4501 
4502  _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4503  inline basic_string<char16_t>
4504  operator""s(const char16_t* __str, size_t __len)
4505  { return basic_string<char16_t>{__str, __len}; }
4506 
4507  _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4508  inline basic_string<char32_t>
4509  operator""s(const char32_t* __str, size_t __len)
4510  { return basic_string<char32_t>{__str, __len}; }
4511 
4512 #undef _GLIBCXX_STRING_CONSTEXPR
4513 #pragma GCC diagnostic pop
4514  } // inline namespace string_literals
4515  } // inline namespace literals
4516 
4517 #if __cplusplus >= 201703L
4518  namespace __detail::__variant
4519  {
4520  template<typename> struct _Never_valueless_alt; // see <variant>
4521 
4522  // Provide the strong exception-safety guarantee when emplacing a
4523  // basic_string into a variant, but only if moving the string cannot throw.
4524  template<typename _Tp, typename _Traits, typename _Alloc>
4525  struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
4526  : __and_<
4527  is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
4528  is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
4529  >::type
4530  { };
4531  } // namespace __detail::__variant
4532 #endif // C++17
4533 #endif // C++14
4534 
4535 _GLIBCXX_END_NAMESPACE_VERSION
4536 } // namespace std
4537 
4538 #endif // C++11
4539 
4540 #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:335
constexpr bool is_constant_evaluated() noexcept
Returns true only when called during constant evaluation.
Definition: type_traits:3644
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition: type_traits:2610
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:51
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:97
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
Definition: any:429
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:233
constexpr iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
basic_string< char32_t > u32string
A string of char32_t.
Definition: stringfwd.h:92
basic_string< char16_t > u16string
A string of char16_t.
Definition: stringfwd.h:89
basic_string< wchar_t > wstring
A string of wchar_t.
Definition: stringfwd.h:80
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:1593
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1683
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)
char_type widen(char __c) const
Widens characters.
Definition: basic_ios.h:449
Template class basic_ostream.
Definition: ostream:61
Primary class template hash.
integral_constant
Definition: type_traits:63
Basis for explicit traits specializations.
Definition: char_traits.h:342
Managing sequences of characters and character-like objects.
Definition: cow_string.h:117
const_reverse_iterator crbegin() const noexcept
Definition: cow_string.h:897
void swap(basic_string &__s) noexcept(/*conditional */)
Swap contents with another string.
Definition: cow_string.h:3513
void push_back(_CharT __c)
Append a single character.
Definition: cow_string.h:1340
const_iterator cend() const noexcept
Definition: cow_string.h:888
size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
Definition: cow_string.h:2420
const _CharT * c_str() const noexcept
Return const pointer to null-terminated contents.
Definition: cow_string.h:2215
basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
Definition: cow_string.h:2752
size_type find(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a C substring.
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.
Definition: cow_string.h:2669
int compare(const basic_string &__str) const
Compare to a string.
Definition: cow_string.h:2771
reverse_iterator rend()
Definition: cow_string.h:862
size_type find_first_not_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character not in string.
Definition: cow_string.h:2587
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
Definition: cow_string.h:1506
basic_string & operator+=(const basic_string &__str)
Append a string to this string.
Definition: cow_string.h:1175
basic_string & assign(const basic_string &__str)
Set value to contents of another string.
Definition: cow_string.h:3225
reverse_iterator rbegin()
Definition: cow_string.h:844
basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
Definition: cow_string.h:1788
reference front()
Definition: cow_string.h:1128
void pop_back()
Remove the last character.
Definition: cow_string.h:1763
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
Definition: cow_string.h:3709
size_type length() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition: cow_string.h:928
size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
Definition: cow_string.h:2504
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition: cow_string.h:916
size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
Definition: cow_string.h:2341
void shrink_to_fit() noexcept
A non-binding request to reduce capacity() to size().
Definition: cow_string.h:968
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
Definition: cow_string.h:3635
void reserve()
Equivalent to shrink_to_fit().
Definition: cow_string.h:3688
const _CharT * data() const noexcept
Return const pointer to contents.
Definition: cow_string.h:2227
const_reference at(size_type __n) const
Provides access to the data contained in the string.
Definition: cow_string.h:1089
iterator begin()
Definition: cow_string.h:805
basic_string & append(const basic_string &__str)
Append a string to this string.
Definition: cow_string.h:3307
const_reverse_iterator crend() const noexcept
Definition: cow_string.h:906
iterator end()
Definition: cow_string.h:824
basic_string & operator=(const basic_string &__str)
Assign the value of str to this string.
Definition: cow_string.h:727
const_reference operator[](size_type __pos) const noexcept
Subscript access to the data contained in the string.
Definition: cow_string.h:1050
void clear() noexcept
Definition: cow_string.h:1013
bool empty() const noexcept
Definition: cow_string.h:1035
reference back()
Definition: cow_string.h:1150
static const size_type npos
Value returned by various member functions when they fail.
Definition: cow_string.h:330
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
Definition: cow_string.h:2249
const_iterator cbegin() const noexcept
Definition: cow_string.h:880
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
Definition: cow_string.h:1718
~basic_string() noexcept
Destroy the string instance.
Definition: cow_string.h:719
size_type capacity() const noexcept
Definition: cow_string.h:978
basic_string() noexcept
Default constructor creates an empty string.
Definition: cow_string.h:523
size_type max_size() const noexcept
Returns the size() of the largest possible string.
Definition: cow_string.h:933
Uniform interface to all pointer-like types.
Definition: ptr_traits.h:185
Marking input iterators.
Forward iterators support a superset of input iterator operations.
Uniform interface to C++98 and C++11 allocators.