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