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