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