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