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  _GLIBCXX_NOEXCEPT;
2256 
2257  /**
2258  * @brief Find position of a string.
2259  * @param __str String to locate.
2260  * @param __pos Index of character to search from (default 0).
2261  * @return Index of start of first occurrence.
2262  *
2263  * Starting from @a __pos, searches forward for value of @a __str within
2264  * this string. If found, returns the index where it begins. If not
2265  * found, returns npos.
2266  */
2267  size_type
2268  find(const basic_string& __str, size_type __pos = 0) const
2269  _GLIBCXX_NOEXCEPT
2270  { return this->find(__str.data(), __pos, __str.size()); }
2271 
2272 #if __cplusplus > 201402L
2273  /**
2274  * @brief Find position of a string_view.
2275  * @param __sv The string_view to locate.
2276  * @param __pos Index of character to search from (default 0).
2277  * @return Index of start of first occurrence.
2278  */
2279  size_type
2280  find(__sv_type __sv, size_type __pos = 0) const noexcept
2281  { return this->find(__sv.data(), __pos, __sv.size()); }
2282 #endif // C++17
2283 
2284  /**
2285  * @brief Find position of a C string.
2286  * @param __s C string to locate.
2287  * @param __pos Index of character to search from (default 0).
2288  * @return Index of start of first occurrence.
2289  *
2290  * Starting from @a __pos, searches forward for the value of @a
2291  * __s within this string. If found, returns the index where
2292  * it begins. If not found, returns npos.
2293  */
2294  size_type
2295  find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2296  {
2297  __glibcxx_requires_string(__s);
2298  return this->find(__s, __pos, traits_type::length(__s));
2299  }
2300 
2301  /**
2302  * @brief Find position of a character.
2303  * @param __c Character to locate.
2304  * @param __pos Index of character to search from (default 0).
2305  * @return Index of first occurrence.
2306  *
2307  * Starting from @a __pos, searches forward for @a __c within
2308  * this string. If found, returns the index where it was
2309  * found. If not found, returns npos.
2310  */
2311  size_type
2312  find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
2313 
2314  /**
2315  * @brief Find last position of a string.
2316  * @param __str String to locate.
2317  * @param __pos Index of character to search back from (default end).
2318  * @return Index of start of last occurrence.
2319  *
2320  * Starting from @a __pos, searches backward for value of @a
2321  * __str within this string. If found, returns the index where
2322  * it begins. If not found, returns npos.
2323  */
2324  size_type
2325  rfind(const basic_string& __str, size_type __pos = npos) const
2326  _GLIBCXX_NOEXCEPT
2327  { return this->rfind(__str.data(), __pos, __str.size()); }
2328 
2329 #if __cplusplus > 201402L
2330  /**
2331  * @brief Find last position of a string_view.
2332  * @param __sv The string_view to locate.
2333  * @param __pos Index of character to search back from (default end).
2334  * @return Index of start of last occurrence.
2335  */
2336  size_type
2337  rfind(__sv_type __sv, size_type __pos = npos) const noexcept
2338  { return this->rfind(__sv.data(), __pos, __sv.size()); }
2339 #endif // C++17
2340 
2341  /**
2342  * @brief Find last position of a C substring.
2343  * @param __s C string to locate.
2344  * @param __pos Index of character to search back from.
2345  * @param __n Number of characters from s to search for.
2346  * @return Index of start of last occurrence.
2347  *
2348  * Starting from @a __pos, searches backward for the first @a
2349  * __n characters in @a __s within this string. If found,
2350  * returns the index where it begins. If not found, returns
2351  * npos.
2352  */
2353  size_type
2354  rfind(const _CharT* __s, size_type __pos, size_type __n) const
2355  _GLIBCXX_NOEXCEPT;
2356 
2357  /**
2358  * @brief Find last position of a C string.
2359  * @param __s C string to locate.
2360  * @param __pos Index of character to start search at (default end).
2361  * @return Index of start of last occurrence.
2362  *
2363  * Starting from @a __pos, searches backward for the value of
2364  * @a __s within this string. If found, returns the index
2365  * where it begins. If not found, returns npos.
2366  */
2367  size_type
2368  rfind(const _CharT* __s, size_type __pos = npos) const
2369  {
2370  __glibcxx_requires_string(__s);
2371  return this->rfind(__s, __pos, traits_type::length(__s));
2372  }
2373 
2374  /**
2375  * @brief Find last position of a character.
2376  * @param __c Character to locate.
2377  * @param __pos Index of character to search back from (default end).
2378  * @return Index of last occurrence.
2379  *
2380  * Starting from @a __pos, searches backward for @a __c within
2381  * this string. If found, returns the index where it was
2382  * found. If not found, returns npos.
2383  */
2384  size_type
2385  rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
2386 
2387  /**
2388  * @brief Find position of a character of string.
2389  * @param __str String containing characters to locate.
2390  * @param __pos Index of character to search from (default 0).
2391  * @return Index of first occurrence.
2392  *
2393  * Starting from @a __pos, searches forward for one of the
2394  * characters of @a __str within this string. If found,
2395  * returns the index where it was found. If not found, returns
2396  * npos.
2397  */
2398  size_type
2399  find_first_of(const basic_string& __str, size_type __pos = 0) const
2400  _GLIBCXX_NOEXCEPT
2401  { return this->find_first_of(__str.data(), __pos, __str.size()); }
2402 
2403 #if __cplusplus > 201402L
2404  /**
2405  * @brief Find position of a character of a string_view.
2406  * @param __sv A string_view containing characters to locate.
2407  * @param __pos Index of character to search from (default 0).
2408  * @return Index of first occurrence.
2409  */
2410  size_type
2411  find_first_of(__sv_type __sv, size_type __pos = 0) const noexcept
2412  { return this->find_first_of(__sv.data(), __pos, __sv.size()); }
2413 #endif // C++17
2414 
2415  /**
2416  * @brief Find position of a character of C substring.
2417  * @param __s String containing characters to locate.
2418  * @param __pos Index of character to search from.
2419  * @param __n Number of characters from s to search for.
2420  * @return Index of first occurrence.
2421  *
2422  * Starting from @a __pos, searches forward for one of the
2423  * first @a __n characters of @a __s within this string. If
2424  * found, returns the index where it was found. If not found,
2425  * returns npos.
2426  */
2427  size_type
2428  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
2429  _GLIBCXX_NOEXCEPT;
2430 
2431  /**
2432  * @brief Find position of a character of C string.
2433  * @param __s String containing characters to locate.
2434  * @param __pos Index of character to search from (default 0).
2435  * @return Index of first occurrence.
2436  *
2437  * Starting from @a __pos, searches forward for one of the
2438  * characters of @a __s within this string. If found, returns
2439  * the index where it was found. If not found, returns npos.
2440  */
2441  size_type
2442  find_first_of(const _CharT* __s, size_type __pos = 0) const
2443  _GLIBCXX_NOEXCEPT
2444  {
2445  __glibcxx_requires_string(__s);
2446  return this->find_first_of(__s, __pos, traits_type::length(__s));
2447  }
2448 
2449  /**
2450  * @brief Find position of a character.
2451  * @param __c Character to locate.
2452  * @param __pos Index of character to search from (default 0).
2453  * @return Index of first occurrence.
2454  *
2455  * Starting from @a __pos, searches forward for the character
2456  * @a __c within this string. If found, returns the index
2457  * where it was found. If not found, returns npos.
2458  *
2459  * Note: equivalent to find(__c, __pos).
2460  */
2461  size_type
2462  find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2463  { return this->find(__c, __pos); }
2464 
2465  /**
2466  * @brief Find last position of a character of string.
2467  * @param __str String containing characters to locate.
2468  * @param __pos Index of character to search back from (default end).
2469  * @return Index of last occurrence.
2470  *
2471  * Starting from @a __pos, searches backward for one of the
2472  * characters of @a __str within this string. If found,
2473  * returns the index where it was found. If not found, returns
2474  * npos.
2475  */
2476  size_type
2477  find_last_of(const basic_string& __str, size_type __pos = npos) const
2478  _GLIBCXX_NOEXCEPT
2479  { return this->find_last_of(__str.data(), __pos, __str.size()); }
2480 
2481 #if __cplusplus > 201402L
2482  /**
2483  * @brief Find last position of a character of string.
2484  * @param __sv A string_view containing characters to locate.
2485  * @param __pos Index of character to search back from (default end).
2486  * @return Index of last occurrence.
2487  */
2488  size_type
2489  find_last_of(__sv_type __sv, size_type __pos = npos) const noexcept
2490  { return this->find_last_of(__sv.data(), __pos, __sv.size()); }
2491 #endif // C++17
2492 
2493  /**
2494  * @brief Find last position of a character of C substring.
2495  * @param __s C string containing characters to locate.
2496  * @param __pos Index of character to search back from.
2497  * @param __n Number of characters from s to search for.
2498  * @return Index of last occurrence.
2499  *
2500  * Starting from @a __pos, searches backward for one of the
2501  * first @a __n characters of @a __s within this string. If
2502  * found, returns the index where it was found. If not found,
2503  * returns npos.
2504  */
2505  size_type
2506  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
2507  _GLIBCXX_NOEXCEPT;
2508 
2509  /**
2510  * @brief Find last position of a character of C string.
2511  * @param __s C string containing characters to locate.
2512  * @param __pos Index of character to search back from (default end).
2513  * @return Index of last occurrence.
2514  *
2515  * Starting from @a __pos, searches backward for one of the
2516  * characters of @a __s within this string. If found, returns
2517  * the index where it was found. If not found, returns npos.
2518  */
2519  size_type
2520  find_last_of(const _CharT* __s, size_type __pos = npos) const
2521  _GLIBCXX_NOEXCEPT
2522  {
2523  __glibcxx_requires_string(__s);
2524  return this->find_last_of(__s, __pos, traits_type::length(__s));
2525  }
2526 
2527  /**
2528  * @brief Find last position of a character.
2529  * @param __c Character to locate.
2530  * @param __pos Index of character to search back from (default end).
2531  * @return Index of last occurrence.
2532  *
2533  * Starting from @a __pos, searches backward for @a __c within
2534  * this string. If found, returns the index where it was
2535  * found. If not found, returns npos.
2536  *
2537  * Note: equivalent to rfind(__c, __pos).
2538  */
2539  size_type
2540  find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2541  { return this->rfind(__c, __pos); }
2542 
2543  /**
2544  * @brief Find position of a character not in string.
2545  * @param __str String containing characters to avoid.
2546  * @param __pos Index of character to search from (default 0).
2547  * @return Index of first occurrence.
2548  *
2549  * Starting from @a __pos, searches forward for a character not contained
2550  * in @a __str within this string. If found, returns the index where it
2551  * was found. If not found, returns npos.
2552  */
2553  size_type
2554  find_first_not_of(const basic_string& __str, size_type __pos = 0) const
2555  _GLIBCXX_NOEXCEPT
2556  { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2557 
2558 #if __cplusplus > 201402L
2559  /**
2560  * @brief Find position of a character not in a string_view.
2561  * @param __sv A string_view containing characters to avoid.
2562  * @param __pos Index of character to search from (default 0).
2563  * @return Index of first occurrence.
2564  */
2565  size_type
2566  find_first_not_of(__sv_type __sv, size_type __pos = 0) const noexcept
2567  { return this->find_first_not_of(__sv.data(), __pos, __sv.size()); }
2568 #endif // C++17
2569 
2570  /**
2571  * @brief Find position of a character not in C substring.
2572  * @param __s C string containing characters to avoid.
2573  * @param __pos Index of character to search from.
2574  * @param __n Number of characters from __s to consider.
2575  * @return Index of first occurrence.
2576  *
2577  * Starting from @a __pos, searches forward for a character not
2578  * contained in the first @a __n characters of @a __s within
2579  * this string. If found, returns the index where it was
2580  * found. If not found, returns npos.
2581  */
2582  size_type
2583  find_first_not_of(const _CharT* __s, size_type __pos,
2584  size_type __n) const _GLIBCXX_NOEXCEPT;
2585 
2586  /**
2587  * @brief Find position of a character not in C string.
2588  * @param __s C string containing characters to avoid.
2589  * @param __pos Index of character to search from (default 0).
2590  * @return Index of first occurrence.
2591  *
2592  * Starting from @a __pos, searches forward for a character not
2593  * contained in @a __s within this string. If found, returns
2594  * the index where it was found. If not found, returns npos.
2595  */
2596  size_type
2597  find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2598  _GLIBCXX_NOEXCEPT
2599  {
2600  __glibcxx_requires_string(__s);
2601  return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2602  }
2603 
2604  /**
2605  * @brief Find position of a different character.
2606  * @param __c Character to avoid.
2607  * @param __pos Index of character to search from (default 0).
2608  * @return Index of first occurrence.
2609  *
2610  * Starting from @a __pos, searches forward for a character
2611  * other than @a __c within this string. If found, returns the
2612  * index where it was found. If not found, returns npos.
2613  */
2614  size_type
2615  find_first_not_of(_CharT __c, size_type __pos = 0) const
2616  _GLIBCXX_NOEXCEPT;
2617 
2618  /**
2619  * @brief Find last position of a character not in string.
2620  * @param __str String containing characters to avoid.
2621  * @param __pos Index of character to search back from (default end).
2622  * @return Index of last occurrence.
2623  *
2624  * Starting from @a __pos, searches backward for a character
2625  * not contained in @a __str within this string. If found,
2626  * returns the index where it was found. If not found, returns
2627  * npos.
2628  */
2629  size_type
2630  find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2631  _GLIBCXX_NOEXCEPT
2632  { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2633 
2634 #if __cplusplus > 201402L
2635  /**
2636  * @brief Find last position of a character not in a string_view.
2637  * @param __sv A string_view containing characters to avoid.
2638  * @param __pos Index of character to search back from (default end).
2639  * @return Index of last occurrence.
2640  */
2641  size_type
2642  find_last_not_of(__sv_type __sv, size_type __pos = npos) const noexcept
2643  { return this->find_last_not_of(__sv.data(), __pos, __sv.size()); }
2644 #endif // C++17
2645 
2646  /**
2647  * @brief Find last position of a character not in C substring.
2648  * @param __s C string containing characters to avoid.
2649  * @param __pos Index of character to search back from.
2650  * @param __n Number of characters from s to consider.
2651  * @return Index of last occurrence.
2652  *
2653  * Starting from @a __pos, searches backward for a character not
2654  * contained in the first @a __n characters of @a __s within this string.
2655  * If found, returns the index where it was found. If not found,
2656  * returns npos.
2657  */
2658  size_type
2659  find_last_not_of(const _CharT* __s, size_type __pos,
2660  size_type __n) const _GLIBCXX_NOEXCEPT;
2661  /**
2662  * @brief Find last position of a character not in C string.
2663  * @param __s C string containing characters to avoid.
2664  * @param __pos Index of character to search back from (default end).
2665  * @return Index of last occurrence.
2666  *
2667  * Starting from @a __pos, searches backward for a character
2668  * not contained in @a __s within this string. If found,
2669  * returns the index where it was found. If not found, returns
2670  * npos.
2671  */
2672  size_type
2673  find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2674  _GLIBCXX_NOEXCEPT
2675  {
2676  __glibcxx_requires_string(__s);
2677  return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2678  }
2679 
2680  /**
2681  * @brief Find last position of a different character.
2682  * @param __c Character to avoid.
2683  * @param __pos Index of character to search back from (default end).
2684  * @return Index of last occurrence.
2685  *
2686  * Starting from @a __pos, searches backward for a character other than
2687  * @a __c within this string. If found, returns the index where it was
2688  * found. If not found, returns npos.
2689  */
2690  size_type
2691  find_last_not_of(_CharT __c, size_type __pos = npos) const
2692  _GLIBCXX_NOEXCEPT;
2693 
2694  /**
2695  * @brief Get a substring.
2696  * @param __pos Index of first character (default 0).
2697  * @param __n Number of characters in substring (default remainder).
2698  * @return The new string.
2699  * @throw std::out_of_range If __pos > size().
2700  *
2701  * Construct and return a new string using the @a __n
2702  * characters starting at @a __pos. If the string is too
2703  * short, use the remainder of the characters. If @a __pos is
2704  * beyond the end of the string, out_of_range is thrown.
2705  */
2706  basic_string
2707  substr(size_type __pos = 0, size_type __n = npos) const
2708  { return basic_string(*this,
2709  _M_check(__pos, "basic_string::substr"), __n); }
2710 
2711  /**
2712  * @brief Compare to a string.
2713  * @param __str String to compare against.
2714  * @return Integer < 0, 0, or > 0.
2715  *
2716  * Returns an integer < 0 if this string is ordered before @a
2717  * __str, 0 if their values are equivalent, or > 0 if this
2718  * string is ordered after @a __str. Determines the effective
2719  * length rlen of the strings to compare as the smallest of
2720  * size() and str.size(). The function then compares the two
2721  * strings by calling traits::compare(data(), str.data(),rlen).
2722  * If the result of the comparison is nonzero returns it,
2723  * otherwise the shorter one is ordered first.
2724  */
2725  int
2726  compare(const basic_string& __str) const
2727  {
2728  const size_type __size = this->size();
2729  const size_type __osize = __str.size();
2730  const size_type __len = std::min(__size, __osize);
2731 
2732  int __r = traits_type::compare(_M_data(), __str.data(), __len);
2733  if (!__r)
2734  __r = _S_compare(__size, __osize);
2735  return __r;
2736  }
2737 
2738 #if __cplusplus > 201402L
2739  /**
2740  * @brief Compare to a string_view.
2741  * @param __sv A string_view to compare against.
2742  * @return Integer < 0, 0, or > 0.
2743  */
2744  int
2745  compare(__sv_type __sv) const
2746  {
2747  const size_type __size = this->size();
2748  const size_type __osize = __sv.size();
2749  const size_type __len = std::min(__size, __osize);
2750 
2751  int __r = traits_type::compare(_M_data(), __sv.data(), __len);
2752  if (!__r)
2753  __r = _S_compare(__size, __osize);
2754  return __r;
2755  }
2756 
2757  /**
2758  * @brief Compare to a string_view.
2759  * @param __pos A position in the string to start comparing from.
2760  * @param __n The number of characters to compare.
2761  * @param __sv A string_view to compare against.
2762  * @return Integer < 0, 0, or > 0.
2763  */
2764  int
2765  compare(size_type __pos, size_type __n, __sv_type __sv) const
2766  { return __sv_type(*this).substr(__pos, __n).compare(__sv); }
2767 
2768  /**
2769  * @brief Compare to a string_view.
2770  * @param __pos1 A position in the string to start comparing from.
2771  * @param __n1 The number of characters to compare.
2772  * @param __sv A string_view to compare against.
2773  * @param __pos2 A position in the string_view to start comparing from.
2774  * @param __n2 The number of characters to compare.
2775  * @return Integer < 0, 0, or > 0.
2776  */
2777  template <typename _Tp>
2778  _If_sv<_Tp, int>
2779  compare(size_type __pos1, size_type __n1, const _Tp& __svt,
2780  size_type __pos2, size_type __n2 = npos) const
2781  {
2782  __sv_type __sv = __svt;
2783  return __sv_type(*this)
2784  .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
2785  }
2786 #endif // C++17
2787 
2788  /**
2789  * @brief Compare substring to a string.
2790  * @param __pos Index of first character of substring.
2791  * @param __n Number of characters in substring.
2792  * @param __str String to compare against.
2793  * @return Integer < 0, 0, or > 0.
2794  *
2795  * Form the substring of this string from the @a __n characters
2796  * starting at @a __pos. Returns an integer < 0 if the
2797  * substring is ordered before @a __str, 0 if their values are
2798  * equivalent, or > 0 if the substring is ordered after @a
2799  * __str. Determines the effective length rlen of the strings
2800  * to compare as the smallest of the length of the substring
2801  * and @a __str.size(). The function then compares the two
2802  * strings by calling
2803  * traits::compare(substring.data(),str.data(),rlen). If the
2804  * result of the comparison is nonzero returns it, otherwise
2805  * the shorter one is ordered first.
2806  */
2807  int
2808  compare(size_type __pos, size_type __n, const basic_string& __str) const;
2809 
2810  /**
2811  * @brief Compare substring to a substring.
2812  * @param __pos1 Index of first character of substring.
2813  * @param __n1 Number of characters in substring.
2814  * @param __str String to compare against.
2815  * @param __pos2 Index of first character of substring of str.
2816  * @param __n2 Number of characters in substring of str.
2817  * @return Integer < 0, 0, or > 0.
2818  *
2819  * Form the substring of this string from the @a __n1
2820  * characters starting at @a __pos1. Form the substring of @a
2821  * __str from the @a __n2 characters starting at @a __pos2.
2822  * Returns an integer < 0 if this substring is ordered before
2823  * the substring of @a __str, 0 if their values are equivalent,
2824  * or > 0 if this substring is ordered after the substring of
2825  * @a __str. Determines the effective length rlen of the
2826  * strings to compare as the smallest of the lengths of the
2827  * substrings. The function then compares the two strings by
2828  * calling
2829  * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2830  * If the result of the comparison is nonzero returns it,
2831  * otherwise the shorter one is ordered first.
2832  */
2833  int
2834  compare(size_type __pos1, size_type __n1, const basic_string& __str,
2835  size_type __pos2, size_type __n2) const;
2836 
2837  /**
2838  * @brief Compare to a C string.
2839  * @param __s C string to compare against.
2840  * @return Integer < 0, 0, or > 0.
2841  *
2842  * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2843  * their values are equivalent, or > 0 if this string is ordered after
2844  * @a __s. Determines the effective length rlen of the strings to
2845  * compare as the smallest of size() and the length of a string
2846  * constructed from @a __s. The function then compares the two strings
2847  * by calling traits::compare(data(),s,rlen). If the result of the
2848  * comparison is nonzero returns it, otherwise the shorter one is
2849  * ordered first.
2850  */
2851  int
2852  compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
2853 
2854  // _GLIBCXX_RESOLVE_LIB_DEFECTS
2855  // 5 String::compare specification questionable
2856  /**
2857  * @brief Compare substring to a C string.
2858  * @param __pos Index of first character of substring.
2859  * @param __n1 Number of characters in substring.
2860  * @param __s C string to compare against.
2861  * @return Integer < 0, 0, or > 0.
2862  *
2863  * Form the substring of this string from the @a __n1
2864  * characters starting at @a pos. Returns an integer < 0 if
2865  * the substring is ordered before @a __s, 0 if their values
2866  * are equivalent, or > 0 if the substring is ordered after @a
2867  * __s. Determines the effective length rlen of the strings to
2868  * compare as the smallest of the length of the substring and
2869  * the length of a string constructed from @a __s. The
2870  * function then compares the two string by calling
2871  * traits::compare(substring.data(),__s,rlen). If the result of
2872  * the comparison is nonzero returns it, otherwise the shorter
2873  * one is ordered first.
2874  */
2875  int
2876  compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2877 
2878  /**
2879  * @brief Compare substring against a character %array.
2880  * @param __pos Index of first character of substring.
2881  * @param __n1 Number of characters in substring.
2882  * @param __s character %array to compare against.
2883  * @param __n2 Number of characters of s.
2884  * @return Integer < 0, 0, or > 0.
2885  *
2886  * Form the substring of this string from the @a __n1
2887  * characters starting at @a __pos. Form a string from the
2888  * first @a __n2 characters of @a __s. Returns an integer < 0
2889  * if this substring is ordered before the string from @a __s,
2890  * 0 if their values are equivalent, or > 0 if this substring
2891  * is ordered after the string from @a __s. Determines the
2892  * effective length rlen of the strings to compare as the
2893  * smallest of the length of the substring and @a __n2. The
2894  * function then compares the two strings by calling
2895  * traits::compare(substring.data(),s,rlen). If the result of
2896  * the comparison is nonzero returns it, otherwise the shorter
2897  * one is ordered first.
2898  *
2899  * NB: s must have at least n2 characters, &apos;\\0&apos; has
2900  * no special meaning.
2901  */
2902  int
2903  compare(size_type __pos, size_type __n1, const _CharT* __s,
2904  size_type __n2) const;
2905  };
2906 _GLIBCXX_END_NAMESPACE_CXX11
2907 #else // !_GLIBCXX_USE_CXX11_ABI
2908  // Reference-counted COW string implentation
2909 
2910  /**
2911  * @class basic_string basic_string.h <string>
2912  * @brief Managing sequences of characters and character-like objects.
2913  *
2914  * @ingroup strings
2915  * @ingroup sequences
2916  *
2917  * @tparam _CharT Type of character
2918  * @tparam _Traits Traits for character type, defaults to
2919  * char_traits<_CharT>.
2920  * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
2921  *
2922  * Meets the requirements of a <a href="tables.html#65">container</a>, a
2923  * <a href="tables.html#66">reversible container</a>, and a
2924  * <a href="tables.html#67">sequence</a>. Of the
2925  * <a href="tables.html#68">optional sequence requirements</a>, only
2926  * @c push_back, @c at, and @c %array access are supported.
2927  *
2928  * @doctodo
2929  *
2930  *
2931  * Documentation? What's that?
2932  * Nathan Myers <ncm@cantrip.org>.
2933  *
2934  * A string looks like this:
2935  *
2936  * @code
2937  * [_Rep]
2938  * _M_length
2939  * [basic_string<char_type>] _M_capacity
2940  * _M_dataplus _M_refcount
2941  * _M_p ----------------> unnamed array of char_type
2942  * @endcode
2943  *
2944  * Where the _M_p points to the first character in the string, and
2945  * you cast it to a pointer-to-_Rep and subtract 1 to get a
2946  * pointer to the header.
2947  *
2948  * This approach has the enormous advantage that a string object
2949  * requires only one allocation. All the ugliness is confined
2950  * within a single %pair of inline functions, which each compile to
2951  * a single @a add instruction: _Rep::_M_data(), and
2952  * string::_M_rep(); and the allocation function which gets a
2953  * block of raw bytes and with room enough and constructs a _Rep
2954  * object at the front.
2955  *
2956  * The reason you want _M_data pointing to the character %array and
2957  * not the _Rep is so that the debugger can see the string
2958  * contents. (Probably we should add a non-inline member to get
2959  * the _Rep for the debugger to use, so users can check the actual
2960  * string length.)
2961  *
2962  * Note that the _Rep object is a POD so that you can have a
2963  * static <em>empty string</em> _Rep object already @a constructed before
2964  * static constructors have run. The reference-count encoding is
2965  * chosen so that a 0 indicates one reference, so you never try to
2966  * destroy the empty-string _Rep object.
2967  *
2968  * All but the last paragraph is considered pretty conventional
2969  * for a C++ string implementation.
2970  */
2971  // 21.3 Template class basic_string
2972  template<typename _CharT, typename _Traits, typename _Alloc>
2973  class basic_string
2974  {
2975  typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
2976 
2977  // Types:
2978  public:
2979  typedef _Traits traits_type;
2980  typedef typename _Traits::char_type value_type;
2981  typedef _Alloc allocator_type;
2982  typedef typename _CharT_alloc_type::size_type size_type;
2983  typedef typename _CharT_alloc_type::difference_type difference_type;
2984  typedef typename _CharT_alloc_type::reference reference;
2985  typedef typename _CharT_alloc_type::const_reference const_reference;
2986  typedef typename _CharT_alloc_type::pointer pointer;
2987  typedef typename _CharT_alloc_type::const_pointer const_pointer;
2988  typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
2989  typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
2990  const_iterator;
2991  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
2992  typedef std::reverse_iterator<iterator> reverse_iterator;
2993 
2994  private:
2995  // _Rep: string representation
2996  // Invariants:
2997  // 1. String really contains _M_length + 1 characters: due to 21.3.4
2998  // must be kept null-terminated.
2999  // 2. _M_capacity >= _M_length
3000  // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
3001  // 3. _M_refcount has three states:
3002  // -1: leaked, one reference, no ref-copies allowed, non-const.
3003  // 0: one reference, non-const.
3004  // n>0: n + 1 references, operations require a lock, const.
3005  // 4. All fields==0 is an empty string, given the extra storage
3006  // beyond-the-end for a null terminator; thus, the shared
3007  // empty string representation needs no constructor.
3008 
3009  struct _Rep_base
3010  {
3011  size_type _M_length;
3012  size_type _M_capacity;
3013  _Atomic_word _M_refcount;
3014  };
3015 
3016  struct _Rep : _Rep_base
3017  {
3018  // Types:
3019  typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
3020 
3021  // (Public) Data members:
3022 
3023  // The maximum number of individual char_type elements of an
3024  // individual string is determined by _S_max_size. This is the
3025  // value that will be returned by max_size(). (Whereas npos
3026  // is the maximum number of bytes the allocator can allocate.)
3027  // If one was to divvy up the theoretical largest size string,
3028  // with a terminating character and m _CharT elements, it'd
3029  // look like this:
3030  // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
3031  // Solving for m:
3032  // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
3033  // In addition, this implementation quarters this amount.
3034  static const size_type _S_max_size;
3035  static const _CharT _S_terminal;
3036 
3037  // The following storage is init'd to 0 by the linker, resulting
3038  // (carefully) in an empty string with one reference.
3039  static size_type _S_empty_rep_storage[];
3040 
3041  static _Rep&
3042  _S_empty_rep() _GLIBCXX_NOEXCEPT
3043  {
3044  // NB: Mild hack to avoid strict-aliasing warnings. Note that
3045  // _S_empty_rep_storage is never modified and the punning should
3046  // be reasonably safe in this case.
3047  void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
3048  return *reinterpret_cast<_Rep*>(__p);
3049  }
3050 
3051  bool
3052  _M_is_leaked() const _GLIBCXX_NOEXCEPT
3053  {
3054 #if defined(__GTHREADS)
3055  // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3056  // so we need to use an atomic load. However, _M_is_leaked
3057  // predicate does not change concurrently (i.e. the string is either
3058  // leaked or not), so a relaxed load is enough.
3059  return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
3060 #else
3061  return this->_M_refcount < 0;
3062 #endif
3063  }
3064 
3065  bool
3066  _M_is_shared() const _GLIBCXX_NOEXCEPT
3067  {
3068 #if defined(__GTHREADS)
3069  // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3070  // so we need to use an atomic load. Another thread can drop last
3071  // but one reference concurrently with this check, so we need this
3072  // load to be acquire to synchronize with release fetch_and_add in
3073  // _M_dispose.
3074  return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
3075 #else
3076  return this->_M_refcount > 0;
3077 #endif
3078  }
3079 
3080  void
3081  _M_set_leaked() _GLIBCXX_NOEXCEPT
3082  { this->_M_refcount = -1; }
3083 
3084  void
3085  _M_set_sharable() _GLIBCXX_NOEXCEPT
3086  { this->_M_refcount = 0; }
3087 
3088  void
3089  _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
3090  {
3091 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3092  if (__builtin_expect(this != &_S_empty_rep(), false))
3093 #endif
3094  {
3095  this->_M_set_sharable(); // One reference.
3096  this->_M_length = __n;
3097  traits_type::assign(this->_M_refdata()[__n], _S_terminal);
3098  // grrr. (per 21.3.4)
3099  // You cannot leave those LWG people alone for a second.
3100  }
3101  }
3102 
3103  _CharT*
3104  _M_refdata() throw()
3105  { return reinterpret_cast<_CharT*>(this + 1); }
3106 
3107  _CharT*
3108  _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
3109  {
3110  return (!_M_is_leaked() && __alloc1 == __alloc2)
3111  ? _M_refcopy() : _M_clone(__alloc1);
3112  }
3113 
3114  // Create & Destroy
3115  static _Rep*
3116  _S_create(size_type, size_type, const _Alloc&);
3117 
3118  void
3119  _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
3120  {
3121 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3122  if (__builtin_expect(this != &_S_empty_rep(), false))
3123 #endif
3124  {
3125  // Be race-detector-friendly. For more info see bits/c++config.
3126  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
3127  // Decrement of _M_refcount is acq_rel, because:
3128  // - all but last decrements need to release to synchronize with
3129  // the last decrement that will delete the object.
3130  // - the last decrement needs to acquire to synchronize with
3131  // all the previous decrements.
3132  // - last but one decrement needs to release to synchronize with
3133  // the acquire load in _M_is_shared that will conclude that
3134  // the object is not shared anymore.
3135  if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
3136  -1) <= 0)
3137  {
3138  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
3139  _M_destroy(__a);
3140  }
3141  }
3142  } // XXX MT
3143 
3144  void
3145  _M_destroy(const _Alloc&) throw();
3146 
3147  _CharT*
3148  _M_refcopy() throw()
3149  {
3150 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3151  if (__builtin_expect(this != &_S_empty_rep(), false))
3152 #endif
3153  __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
3154  return _M_refdata();
3155  } // XXX MT
3156 
3157  _CharT*
3158  _M_clone(const _Alloc&, size_type __res = 0);
3159  };
3160 
3161  // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
3162  struct _Alloc_hider : _Alloc
3163  {
3164  _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
3165  : _Alloc(__a), _M_p(__dat) { }
3166 
3167  _CharT* _M_p; // The actual data.
3168  };
3169 
3170  public:
3171  // Data Members (public):
3172  // NB: This is an unsigned type, and thus represents the maximum
3173  // size that the allocator can hold.
3174  /// Value returned by various member functions when they fail.
3175  static const size_type npos = static_cast<size_type>(-1);
3176 
3177  private:
3178  // Data Members (private):
3179  mutable _Alloc_hider _M_dataplus;
3180 
3181  _CharT*
3182  _M_data() const _GLIBCXX_NOEXCEPT
3183  { return _M_dataplus._M_p; }
3184 
3185  _CharT*
3186  _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
3187  { return (_M_dataplus._M_p = __p); }
3188 
3189  _Rep*
3190  _M_rep() const _GLIBCXX_NOEXCEPT
3191  { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
3192 
3193  // For the internal use we have functions similar to `begin'/`end'
3194  // but they do not call _M_leak.
3195  iterator
3196  _M_ibegin() const _GLIBCXX_NOEXCEPT
3197  { return iterator(_M_data()); }
3198 
3199  iterator
3200  _M_iend() const _GLIBCXX_NOEXCEPT
3201  { return iterator(_M_data() + this->size()); }
3202 
3203  void
3204  _M_leak() // for use in begin() & non-const op[]
3205  {
3206  if (!_M_rep()->_M_is_leaked())
3207  _M_leak_hard();
3208  }
3209 
3210  size_type
3211  _M_check(size_type __pos, const char* __s) const
3212  {
3213  if (__pos > this->size())
3214  __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
3215  "this->size() (which is %zu)"),
3216  __s, __pos, this->size());
3217  return __pos;
3218  }
3219 
3220  void
3221  _M_check_length(size_type __n1, size_type __n2, const char* __s) const
3222  {
3223  if (this->max_size() - (this->size() - __n1) < __n2)
3224  __throw_length_error(__N(__s));
3225  }
3226 
3227  // NB: _M_limit doesn't check for a bad __pos value.
3228  size_type
3229  _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
3230  {
3231  const bool __testoff = __off < this->size() - __pos;
3232  return __testoff ? __off : this->size() - __pos;
3233  }
3234 
3235  // True if _Rep and source do not overlap.
3236  bool
3237  _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3238  {
3239  return (less<const _CharT*>()(__s, _M_data())
3240  || less<const _CharT*>()(_M_data() + this->size(), __s));
3241  }
3242 
3243  // When __n = 1 way faster than the general multichar
3244  // traits_type::copy/move/assign.
3245  static void
3246  _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
3247  {
3248  if (__n == 1)
3249  traits_type::assign(*__d, *__s);
3250  else
3251  traits_type::copy(__d, __s, __n);
3252  }
3253 
3254  static void
3255  _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
3256  {
3257  if (__n == 1)
3258  traits_type::assign(*__d, *__s);
3259  else
3260  traits_type::move(__d, __s, __n);
3261  }
3262 
3263  static void
3264  _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
3265  {
3266  if (__n == 1)
3267  traits_type::assign(*__d, __c);
3268  else
3269  traits_type::assign(__d, __n, __c);
3270  }
3271 
3272  // _S_copy_chars is a separate template to permit specialization
3273  // to optimize for the common case of pointers as iterators.
3274  template<class _Iterator>
3275  static void
3276  _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
3277  {
3278  for (; __k1 != __k2; ++__k1, (void)++__p)
3279  traits_type::assign(*__p, *__k1); // These types are off.
3280  }
3281 
3282  static void
3283  _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
3284  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3285 
3286  static void
3287  _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
3288  _GLIBCXX_NOEXCEPT
3289  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3290 
3291  static void
3292  _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
3293  { _M_copy(__p, __k1, __k2 - __k1); }
3294 
3295  static void
3296  _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
3297  _GLIBCXX_NOEXCEPT
3298  { _M_copy(__p, __k1, __k2 - __k1); }
3299 
3300  static int
3301  _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
3302  {
3303  const difference_type __d = difference_type(__n1 - __n2);
3304 
3305  if (__d > __gnu_cxx::__numeric_traits<int>::__max)
3306  return __gnu_cxx::__numeric_traits<int>::__max;
3307  else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
3308  return __gnu_cxx::__numeric_traits<int>::__min;
3309  else
3310  return int(__d);
3311  }
3312 
3313  void
3314  _M_mutate(size_type __pos, size_type __len1, size_type __len2);
3315 
3316  void
3317  _M_leak_hard();
3318 
3319  static _Rep&
3320  _S_empty_rep() _GLIBCXX_NOEXCEPT
3321  { return _Rep::_S_empty_rep(); }
3322 
3323  public:
3324  // Construct/copy/destroy:
3325  // NB: We overload ctors in some cases instead of using default
3326  // arguments, per 17.4.4.4 para. 2 item 2.
3327 
3328  /**
3329  * @brief Default constructor creates an empty string.
3330  */
3332 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3333  : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
3334 #else
3335  : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
3336 #endif
3337 
3338  /**
3339  * @brief Construct an empty string using allocator @a a.
3340  */
3341  explicit
3342  basic_string(const _Alloc& __a);
3343 
3344  // NB: per LWG issue 42, semantics different from IS:
3345  /**
3346  * @brief Construct string with copy of value of @a str.
3347  * @param __str Source string.
3348  */
3349  basic_string(const basic_string& __str);
3350 
3351  // _GLIBCXX_RESOLVE_LIB_DEFECTS
3352  // 2583. no way to supply an allocator for basic_string(str, pos)
3353  /**
3354  * @brief Construct string as copy of a substring.
3355  * @param __str Source string.
3356  * @param __pos Index of first character to copy from.
3357  * @param __a Allocator to use.
3358  */
3359  basic_string(const basic_string& __str, size_type __pos,
3360  const _Alloc& __a = _Alloc());
3361 
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  */
3368  basic_string(const basic_string& __str, size_type __pos,
3369  size_type __n);
3370  /**
3371  * @brief Construct string as copy of a substring.
3372  * @param __str Source string.
3373  * @param __pos Index of first character to copy from.
3374  * @param __n Number of characters to copy.
3375  * @param __a Allocator to use.
3376  */
3377  basic_string(const basic_string& __str, size_type __pos,
3378  size_type __n, const _Alloc& __a);
3379 
3380  /**
3381  * @brief Construct string initialized by a character %array.
3382  * @param __s Source character %array.
3383  * @param __n Number of characters to copy.
3384  * @param __a Allocator to use (default is default allocator).
3385  *
3386  * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
3387  * has no special meaning.
3388  */
3389  basic_string(const _CharT* __s, size_type __n,
3390  const _Alloc& __a = _Alloc());
3391  /**
3392  * @brief Construct string as copy of a C string.
3393  * @param __s Source C string.
3394  * @param __a Allocator to use (default is default allocator).
3395  */
3396  basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
3397  /**
3398  * @brief Construct string as multiple characters.
3399  * @param __n Number of characters.
3400  * @param __c Character to use.
3401  * @param __a Allocator to use (default is default allocator).
3402  */
3403  basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
3404 
3405 #if __cplusplus >= 201103L
3406  /**
3407  * @brief Move construct string.
3408  * @param __str Source string.
3409  *
3410  * The newly-created string contains the exact contents of @a __str.
3411  * @a __str is a valid, but unspecified string.
3412  **/
3413  basic_string(basic_string&& __str)
3414 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3415  noexcept // FIXME C++11: should always be noexcept.
3416 #endif
3417  : _M_dataplus(__str._M_dataplus)
3418  {
3419 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3420  __str._M_data(_S_empty_rep()._M_refdata());
3421 #else
3422  __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3423 #endif
3424  }
3425 
3426  /**
3427  * @brief Construct string from an initializer %list.
3428  * @param __l std::initializer_list of characters.
3429  * @param __a Allocator to use (default is default allocator).
3430  */
3431  basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
3432 #endif // C++11
3433 
3434  /**
3435  * @brief Construct string as copy of a range.
3436  * @param __beg Start of range.
3437  * @param __end End of range.
3438  * @param __a Allocator to use (default is default allocator).
3439  */
3440  template<class _InputIterator>
3441  basic_string(_InputIterator __beg, _InputIterator __end,
3442  const _Alloc& __a = _Alloc());
3443 
3444  /**
3445  * @brief Destroy the string instance.
3446  */
3447  ~basic_string() _GLIBCXX_NOEXCEPT
3448  { _M_rep()->_M_dispose(this->get_allocator()); }
3449 
3450  /**
3451  * @brief Assign the value of @a str to this string.
3452  * @param __str Source string.
3453  */
3454  basic_string&
3455  operator=(const basic_string& __str)
3456  { return this->assign(__str); }
3457 
3458  /**
3459  * @brief Copy contents of @a s into this string.
3460  * @param __s Source null-terminated string.
3461  */
3462  basic_string&
3463  operator=(const _CharT* __s)
3464  { return this->assign(__s); }
3465 
3466  /**
3467  * @brief Set value to string of length 1.
3468  * @param __c Source character.
3469  *
3470  * Assigning to a character makes this string length 1 and
3471  * (*this)[0] == @a c.
3472  */
3473  basic_string&
3474  operator=(_CharT __c)
3475  {
3476  this->assign(1, __c);
3477  return *this;
3478  }
3479 
3480 #if __cplusplus >= 201103L
3481  /**
3482  * @brief Move assign the value of @a str to this string.
3483  * @param __str Source string.
3484  *
3485  * The contents of @a str are moved into this string (without copying).
3486  * @a str is a valid, but unspecified string.
3487  **/
3488  // PR 58265, this should be noexcept.
3489  basic_string&
3490  operator=(basic_string&& __str)
3491  {
3492  // NB: DR 1204.
3493  this->swap(__str);
3494  return *this;
3495  }
3496 
3497  /**
3498  * @brief Set value to string constructed from initializer %list.
3499  * @param __l std::initializer_list.
3500  */
3501  basic_string&
3503  {
3504  this->assign(__l.begin(), __l.size());
3505  return *this;
3506  }
3507 #endif // C++11
3508 
3509  // Iterators:
3510  /**
3511  * Returns a read/write iterator that points to the first character in
3512  * the %string. Unshares the string.
3513  */
3514  iterator
3515  begin() // FIXME C++11: should be noexcept.
3516  {
3517  _M_leak();
3518  return iterator(_M_data());
3519  }
3520 
3521  /**
3522  * Returns a read-only (constant) iterator that points to the first
3523  * character in the %string.
3524  */
3525  const_iterator
3526  begin() const _GLIBCXX_NOEXCEPT
3527  { return const_iterator(_M_data()); }
3528 
3529  /**
3530  * Returns a read/write iterator that points one past the last
3531  * character in the %string. Unshares the string.
3532  */
3533  iterator
3534  end() // FIXME C++11: should be noexcept.
3535  {
3536  _M_leak();
3537  return iterator(_M_data() + this->size());
3538  }
3539 
3540  /**
3541  * Returns a read-only (constant) iterator that points one past the
3542  * last character in the %string.
3543  */
3544  const_iterator
3545  end() const _GLIBCXX_NOEXCEPT
3546  { return const_iterator(_M_data() + this->size()); }
3547 
3548  /**
3549  * Returns a read/write reverse iterator that points to the last
3550  * character in the %string. Iteration is done in reverse element
3551  * order. Unshares the string.
3552  */
3553  reverse_iterator
3554  rbegin() // FIXME C++11: should be noexcept.
3555  { return reverse_iterator(this->end()); }
3556 
3557  /**
3558  * Returns a read-only (constant) reverse iterator that points
3559  * to the last character in the %string. Iteration is done in
3560  * reverse element order.
3561  */
3562  const_reverse_iterator
3563  rbegin() const _GLIBCXX_NOEXCEPT
3564  { return const_reverse_iterator(this->end()); }
3565 
3566  /**
3567  * Returns a read/write reverse iterator that points to one before the
3568  * first character in the %string. Iteration is done in reverse
3569  * element order. Unshares the string.
3570  */
3571  reverse_iterator
3572  rend() // FIXME C++11: should be noexcept.
3573  { return reverse_iterator(this->begin()); }
3574 
3575  /**
3576  * Returns a read-only (constant) reverse iterator that points
3577  * to one before the first character in the %string. Iteration
3578  * is done in reverse element order.
3579  */
3580  const_reverse_iterator
3581  rend() const _GLIBCXX_NOEXCEPT
3582  { return const_reverse_iterator(this->begin()); }
3583 
3584 #if __cplusplus >= 201103L
3585  /**
3586  * Returns a read-only (constant) iterator that points to the first
3587  * character in the %string.
3588  */
3589  const_iterator
3590  cbegin() const noexcept
3591  { return const_iterator(this->_M_data()); }
3592 
3593  /**
3594  * Returns a read-only (constant) iterator that points one past the
3595  * last character in the %string.
3596  */
3597  const_iterator
3598  cend() const noexcept
3599  { return const_iterator(this->_M_data() + this->size()); }
3600 
3601  /**
3602  * Returns a read-only (constant) reverse iterator that points
3603  * to the last character in the %string. Iteration is done in
3604  * reverse element order.
3605  */
3606  const_reverse_iterator
3607  crbegin() const noexcept
3608  { return const_reverse_iterator(this->end()); }
3609 
3610  /**
3611  * Returns a read-only (constant) reverse iterator that points
3612  * to one before the first character in the %string. Iteration
3613  * is done in reverse element order.
3614  */
3615  const_reverse_iterator
3616  crend() const noexcept
3617  { return const_reverse_iterator(this->begin()); }
3618 #endif
3619 
3620  public:
3621  // Capacity:
3622  /// Returns the number of characters in the string, not including any
3623  /// null-termination.
3624  size_type
3625  size() const _GLIBCXX_NOEXCEPT
3626  { return _M_rep()->_M_length; }
3627 
3628  /// Returns the number of characters in the string, not including any
3629  /// null-termination.
3630  size_type
3631  length() const _GLIBCXX_NOEXCEPT
3632  { return _M_rep()->_M_length; }
3633 
3634  /// Returns the size() of the largest possible %string.
3635  size_type
3636  max_size() const _GLIBCXX_NOEXCEPT
3637  { return _Rep::_S_max_size; }
3638 
3639  /**
3640  * @brief Resizes the %string to the specified number of characters.
3641  * @param __n Number of characters the %string should contain.
3642  * @param __c Character to fill any new elements.
3643  *
3644  * This function will %resize the %string to the specified
3645  * number of characters. If the number is smaller than the
3646  * %string's current size the %string is truncated, otherwise
3647  * the %string is extended and new elements are %set to @a __c.
3648  */
3649  void
3650  resize(size_type __n, _CharT __c);
3651 
3652  /**
3653  * @brief Resizes the %string to the specified number of characters.
3654  * @param __n Number of characters the %string should contain.
3655  *
3656  * This function will resize the %string to the specified length. If
3657  * the new size is smaller than the %string's current size the %string
3658  * is truncated, otherwise the %string is extended and new characters
3659  * are default-constructed. For basic types such as char, this means
3660  * setting them to 0.
3661  */
3662  void
3663  resize(size_type __n)
3664  { this->resize(__n, _CharT()); }
3665 
3666 #if __cplusplus >= 201103L
3667  /// A non-binding request to reduce capacity() to size().
3668  void
3669  shrink_to_fit() _GLIBCXX_NOEXCEPT
3670  {
3671 #if __cpp_exceptions
3672  if (capacity() > size())
3673  {
3674  try
3675  { reserve(0); }
3676  catch(...)
3677  { }
3678  }
3679 #endif
3680  }
3681 #endif
3682 
3683  /**
3684  * Returns the total number of characters that the %string can hold
3685  * before needing to allocate more memory.
3686  */
3687  size_type
3688  capacity() const _GLIBCXX_NOEXCEPT
3689  { return _M_rep()->_M_capacity; }
3690 
3691  /**
3692  * @brief Attempt to preallocate enough memory for specified number of
3693  * characters.
3694  * @param __res_arg Number of characters required.
3695  * @throw std::length_error If @a __res_arg exceeds @c max_size().
3696  *
3697  * This function attempts to reserve enough memory for the
3698  * %string to hold the specified number of characters. If the
3699  * number requested is more than max_size(), length_error is
3700  * thrown.
3701  *
3702  * The advantage of this function is that if optimal code is a
3703  * necessity and the user can determine the string length that will be
3704  * required, the user can reserve the memory in %advance, and thus
3705  * prevent a possible reallocation of memory and copying of %string
3706  * data.
3707  */
3708  void
3709  reserve(size_type __res_arg = 0);
3710 
3711  /**
3712  * Erases the string, making it empty.
3713  */
3714 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3715  void
3716  clear() _GLIBCXX_NOEXCEPT
3717  {
3718  if (_M_rep()->_M_is_shared())
3719  {
3720  _M_rep()->_M_dispose(this->get_allocator());
3721  _M_data(_S_empty_rep()._M_refdata());
3722  }
3723  else
3724  _M_rep()->_M_set_length_and_sharable(0);
3725  }
3726 #else
3727  // PR 56166: this should not throw.
3728  void
3729  clear()
3730  { _M_mutate(0, this->size(), 0); }
3731 #endif
3732 
3733  /**
3734  * Returns true if the %string is empty. Equivalent to
3735  * <code>*this == ""</code>.
3736  */
3737  bool
3738  empty() const _GLIBCXX_NOEXCEPT
3739  { return this->size() == 0; }
3740 
3741  // Element access:
3742  /**
3743  * @brief Subscript access to the data contained in the %string.
3744  * @param __pos The index of the character to access.
3745  * @return Read-only (constant) reference to the character.
3746  *
3747  * This operator allows for easy, array-style, data access.
3748  * Note that data access with this operator is unchecked and
3749  * out_of_range lookups are not defined. (For checked lookups
3750  * see at().)
3751  */
3752  const_reference
3753  operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
3754  {
3755  __glibcxx_assert(__pos <= size());
3756  return _M_data()[__pos];
3757  }
3758 
3759  /**
3760  * @brief Subscript access to the data contained in the %string.
3761  * @param __pos The index of the character to access.
3762  * @return Read/write reference to the character.
3763  *
3764  * This operator allows for easy, array-style, data access.
3765  * Note that data access with this operator is unchecked and
3766  * out_of_range lookups are not defined. (For checked lookups
3767  * see at().) Unshares the string.
3768  */
3769  reference
3770  operator[](size_type __pos)
3771  {
3772  // Allow pos == size() both in C++98 mode, as v3 extension,
3773  // and in C++11 mode.
3774  __glibcxx_assert(__pos <= size());
3775  // In pedantic mode be strict in C++98 mode.
3776  _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
3777  _M_leak();
3778  return _M_data()[__pos];
3779  }
3780 
3781  /**
3782  * @brief Provides access to the data contained in the %string.
3783  * @param __n The index of the character to access.
3784  * @return Read-only (const) reference to the character.
3785  * @throw std::out_of_range If @a n is an invalid index.
3786  *
3787  * This function provides for safer data access. The parameter is
3788  * first checked that it is in the range of the string. The function
3789  * throws out_of_range if the check fails.
3790  */
3791  const_reference
3792  at(size_type __n) const
3793  {
3794  if (__n >= this->size())
3795  __throw_out_of_range_fmt(__N("basic_string::at: __n "
3796  "(which is %zu) >= this->size() "
3797  "(which is %zu)"),
3798  __n, this->size());
3799  return _M_data()[__n];
3800  }
3801 
3802  /**
3803  * @brief Provides access to the data contained in the %string.
3804  * @param __n The index of the character to access.
3805  * @return Read/write reference to the character.
3806  * @throw std::out_of_range If @a n is an invalid index.
3807  *
3808  * This function provides for safer data access. The parameter is
3809  * first checked that it is in the range of the string. The function
3810  * throws out_of_range if the check fails. Success results in
3811  * unsharing the string.
3812  */
3813  reference
3814  at(size_type __n)
3815  {
3816  if (__n >= size())
3817  __throw_out_of_range_fmt(__N("basic_string::at: __n "
3818  "(which is %zu) >= this->size() "
3819  "(which is %zu)"),
3820  __n, this->size());
3821  _M_leak();
3822  return _M_data()[__n];
3823  }
3824 
3825 #if __cplusplus >= 201103L
3826  /**
3827  * Returns a read/write reference to the data at the first
3828  * element of the %string.
3829  */
3830  reference
3832  {
3833  __glibcxx_assert(!empty());
3834  return operator[](0);
3835  }
3836 
3837  /**
3838  * Returns a read-only (constant) reference to the data at the first
3839  * element of the %string.
3840  */
3841  const_reference
3842  front() const noexcept
3843  {
3844  __glibcxx_assert(!empty());
3845  return operator[](0);
3846  }
3847 
3848  /**
3849  * Returns a read/write reference to the data at the last
3850  * element of the %string.
3851  */
3852  reference
3854  {
3855  __glibcxx_assert(!empty());
3856  return operator[](this->size() - 1);
3857  }
3858 
3859  /**
3860  * Returns a read-only (constant) reference to the data at the
3861  * last element of the %string.
3862  */
3863  const_reference
3864  back() const noexcept
3865  {
3866  __glibcxx_assert(!empty());
3867  return operator[](this->size() - 1);
3868  }
3869 #endif
3870 
3871  // Modifiers:
3872  /**
3873  * @brief Append a string to this string.
3874  * @param __str The string to append.
3875  * @return Reference to this string.
3876  */
3877  basic_string&
3878  operator+=(const basic_string& __str)
3879  { return this->append(__str); }
3880 
3881  /**
3882  * @brief Append a C string.
3883  * @param __s The C string to append.
3884  * @return Reference to this string.
3885  */
3886  basic_string&
3887  operator+=(const _CharT* __s)
3888  { return this->append(__s); }
3889 
3890  /**
3891  * @brief Append a character.
3892  * @param __c The character to append.
3893  * @return Reference to this string.
3894  */
3895  basic_string&
3896  operator+=(_CharT __c)
3897  {
3898  this->push_back(__c);
3899  return *this;
3900  }
3901 
3902 #if __cplusplus >= 201103L
3903  /**
3904  * @brief Append an initializer_list of characters.
3905  * @param __l The initializer_list of characters to be appended.
3906  * @return Reference to this string.
3907  */
3908  basic_string&
3910  { return this->append(__l.begin(), __l.size()); }
3911 #endif // C++11
3912 
3913  /**
3914  * @brief Append a string to this string.
3915  * @param __str The string to append.
3916  * @return Reference to this string.
3917  */
3918  basic_string&
3919  append(const basic_string& __str);
3920 
3921  /**
3922  * @brief Append a substring.
3923  * @param __str The string to append.
3924  * @param __pos Index of the first character of str to append.
3925  * @param __n The number of characters to append.
3926  * @return Reference to this string.
3927  * @throw std::out_of_range if @a __pos is not a valid index.
3928  *
3929  * This function appends @a __n characters from @a __str
3930  * starting at @a __pos to this string. If @a __n is is larger
3931  * than the number of available characters in @a __str, the
3932  * remainder of @a __str is appended.
3933  */
3934  basic_string&
3935  append(const basic_string& __str, size_type __pos, size_type __n);
3936 
3937  /**
3938  * @brief Append a C substring.
3939  * @param __s The C string to append.
3940  * @param __n The number of characters to append.
3941  * @return Reference to this string.
3942  */
3943  basic_string&
3944  append(const _CharT* __s, size_type __n);
3945 
3946  /**
3947  * @brief Append a C string.
3948  * @param __s The C string to append.
3949  * @return Reference to this string.
3950  */
3951  basic_string&
3952  append(const _CharT* __s)
3953  {
3954  __glibcxx_requires_string(__s);
3955  return this->append(__s, traits_type::length(__s));
3956  }
3957 
3958  /**
3959  * @brief Append multiple characters.
3960  * @param __n The number of characters to append.
3961  * @param __c The character to use.
3962  * @return Reference to this string.
3963  *
3964  * Appends __n copies of __c to this string.
3965  */
3966  basic_string&
3967  append(size_type __n, _CharT __c);
3968 
3969 #if __cplusplus >= 201103L
3970  /**
3971  * @brief Append an initializer_list of characters.
3972  * @param __l The initializer_list of characters to append.
3973  * @return Reference to this string.
3974  */
3975  basic_string&
3977  { return this->append(__l.begin(), __l.size()); }
3978 #endif // C++11
3979 
3980  /**
3981  * @brief Append a range of characters.
3982  * @param __first Iterator referencing the first character to append.
3983  * @param __last Iterator marking the end of the range.
3984  * @return Reference to this string.
3985  *
3986  * Appends characters in the range [__first,__last) to this string.
3987  */
3988  template<class _InputIterator>
3989  basic_string&
3990  append(_InputIterator __first, _InputIterator __last)
3991  { return this->replace(_M_iend(), _M_iend(), __first, __last); }
3992 
3993  /**
3994  * @brief Append a single character.
3995  * @param __c Character to append.
3996  */
3997  void
3998  push_back(_CharT __c)
3999  {
4000  const size_type __len = 1 + this->size();
4001  if (__len > this->capacity() || _M_rep()->_M_is_shared())
4002  this->reserve(__len);
4003  traits_type::assign(_M_data()[this->size()], __c);
4004  _M_rep()->_M_set_length_and_sharable(__len);
4005  }
4006 
4007  /**
4008  * @brief Set value to contents of another string.
4009  * @param __str Source string to use.
4010  * @return Reference to this string.
4011  */
4012  basic_string&
4013  assign(const basic_string& __str);
4014 
4015 #if __cplusplus >= 201103L
4016  /**
4017  * @brief Set value to contents of another string.
4018  * @param __str Source string to use.
4019  * @return Reference to this string.
4020  *
4021  * This function sets this string to the exact contents of @a __str.
4022  * @a __str is a valid, but unspecified string.
4023  */
4024  // PR 58265, this should be noexcept.
4025  basic_string&
4026  assign(basic_string&& __str)
4027  {
4028  this->swap(__str);
4029  return *this;
4030  }
4031 #endif // C++11
4032 
4033  /**
4034  * @brief Set value to a substring of a string.
4035  * @param __str The string to use.
4036  * @param __pos Index of the first character of str.
4037  * @param __n Number of characters to use.
4038  * @return Reference to this string.
4039  * @throw std::out_of_range if @a pos is not a valid index.
4040  *
4041  * This function sets this string to the substring of @a __str
4042  * consisting of @a __n characters at @a __pos. If @a __n is
4043  * is larger than the number of available characters in @a
4044  * __str, the remainder of @a __str is used.
4045  */
4046  basic_string&
4047  assign(const basic_string& __str, size_type __pos, size_type __n)
4048  { return this->assign(__str._M_data()
4049  + __str._M_check(__pos, "basic_string::assign"),
4050  __str._M_limit(__pos, __n)); }
4051 
4052  /**
4053  * @brief Set value to a C substring.
4054  * @param __s The C string to use.
4055  * @param __n Number of characters to use.
4056  * @return Reference to this string.
4057  *
4058  * This function sets the value of this string to the first @a __n
4059  * characters of @a __s. If @a __n is is larger than the number of
4060  * available characters in @a __s, the remainder of @a __s is used.
4061  */
4062  basic_string&
4063  assign(const _CharT* __s, size_type __n);
4064 
4065  /**
4066  * @brief Set value to contents of a C string.
4067  * @param __s The C string to use.
4068  * @return Reference to this string.
4069  *
4070  * This function sets the value of this string to the value of @a __s.
4071  * The data is copied, so there is no dependence on @a __s once the
4072  * function returns.
4073  */
4074  basic_string&
4075  assign(const _CharT* __s)
4076  {
4077  __glibcxx_requires_string(__s);
4078  return this->assign(__s, traits_type::length(__s));
4079  }
4080 
4081  /**
4082  * @brief Set value to multiple characters.
4083  * @param __n Length of the resulting string.
4084  * @param __c The character to use.
4085  * @return Reference to this string.
4086  *
4087  * This function sets the value of this string to @a __n copies of
4088  * character @a __c.
4089  */
4090  basic_string&
4091  assign(size_type __n, _CharT __c)
4092  { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
4093 
4094  /**
4095  * @brief Set value to a range of characters.
4096  * @param __first Iterator referencing the first character to append.
4097  * @param __last Iterator marking the end of the range.
4098  * @return Reference to this string.
4099  *
4100  * Sets value of string to characters in the range [__first,__last).
4101  */
4102  template<class _InputIterator>
4103  basic_string&
4104  assign(_InputIterator __first, _InputIterator __last)
4105  { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
4106 
4107 #if __cplusplus >= 201103L
4108  /**
4109  * @brief Set value to an initializer_list of characters.
4110  * @param __l The initializer_list of characters to assign.
4111  * @return Reference to this string.
4112  */
4113  basic_string&
4115  { return this->assign(__l.begin(), __l.size()); }
4116 #endif // C++11
4117 
4118  /**
4119  * @brief Insert multiple characters.
4120  * @param __p Iterator referencing location in string to insert at.
4121  * @param __n Number of characters to insert
4122  * @param __c The character to insert.
4123  * @throw std::length_error If new length exceeds @c max_size().
4124  *
4125  * Inserts @a __n copies of character @a __c starting at the
4126  * position referenced by iterator @a __p. If adding
4127  * characters causes the length to exceed max_size(),
4128  * length_error is thrown. The value of the string doesn't
4129  * change if an error is thrown.
4130  */
4131  void
4132  insert(iterator __p, size_type __n, _CharT __c)
4133  { this->replace(__p, __p, __n, __c); }
4134 
4135  /**
4136  * @brief Insert a range of characters.
4137  * @param __p Iterator referencing location in string to insert at.
4138  * @param __beg Start of range.
4139  * @param __end End of range.
4140  * @throw std::length_error If new length exceeds @c max_size().
4141  *
4142  * Inserts characters in range [__beg,__end). If adding
4143  * characters causes the length to exceed max_size(),
4144  * length_error is thrown. The value of the string doesn't
4145  * change if an error is thrown.
4146  */
4147  template<class _InputIterator>
4148  void
4149  insert(iterator __p, _InputIterator __beg, _InputIterator __end)
4150  { this->replace(__p, __p, __beg, __end); }
4151 
4152 #if __cplusplus >= 201103L
4153  /**
4154  * @brief Insert an initializer_list of characters.
4155  * @param __p Iterator referencing location in string to insert at.
4156  * @param __l The initializer_list of characters to insert.
4157  * @throw std::length_error If new length exceeds @c max_size().
4158  */
4159  void
4161  {
4162  _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4163  this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
4164  }
4165 #endif // C++11
4166 
4167  /**
4168  * @brief Insert value of a string.
4169  * @param __pos1 Iterator referencing location in string to insert at.
4170  * @param __str The string to insert.
4171  * @return Reference to this string.
4172  * @throw std::length_error If new length exceeds @c max_size().
4173  *
4174  * Inserts value of @a __str starting at @a __pos1. If adding
4175  * characters causes the length to exceed max_size(),
4176  * length_error is thrown. The value of the string doesn't
4177  * change if an error is thrown.
4178  */
4179  basic_string&
4180  insert(size_type __pos1, const basic_string& __str)
4181  { return this->insert(__pos1, __str, size_type(0), __str.size()); }
4182 
4183  /**
4184  * @brief Insert a substring.
4185  * @param __pos1 Iterator referencing location in string to insert at.
4186  * @param __str The string to insert.
4187  * @param __pos2 Start of characters in str to insert.
4188  * @param __n Number of characters to insert.
4189  * @return Reference to this string.
4190  * @throw std::length_error If new length exceeds @c max_size().
4191  * @throw std::out_of_range If @a pos1 > size() or
4192  * @a __pos2 > @a str.size().
4193  *
4194  * Starting at @a pos1, insert @a __n character of @a __str
4195  * beginning with @a __pos2. If adding characters causes the
4196  * length to exceed max_size(), length_error is thrown. If @a
4197  * __pos1 is beyond the end of this string or @a __pos2 is
4198  * beyond the end of @a __str, out_of_range is thrown. The
4199  * value of the string doesn't change if an error is thrown.
4200  */
4201  basic_string&
4202  insert(size_type __pos1, const basic_string& __str,
4203  size_type __pos2, size_type __n)
4204  { return this->insert(__pos1, __str._M_data()
4205  + __str._M_check(__pos2, "basic_string::insert"),
4206  __str._M_limit(__pos2, __n)); }
4207 
4208  /**
4209  * @brief Insert a C substring.
4210  * @param __pos Iterator referencing location in string to insert at.
4211  * @param __s The C string to insert.
4212  * @param __n The number of characters to insert.
4213  * @return Reference to this string.
4214  * @throw std::length_error If new length exceeds @c max_size().
4215  * @throw std::out_of_range If @a __pos is beyond the end of this
4216  * string.
4217  *
4218  * Inserts the first @a __n characters of @a __s starting at @a
4219  * __pos. If adding characters causes the length to exceed
4220  * max_size(), length_error is thrown. If @a __pos is beyond
4221  * end(), out_of_range is thrown. The value of the string
4222  * doesn't change if an error is thrown.
4223  */
4224  basic_string&
4225  insert(size_type __pos, const _CharT* __s, size_type __n);
4226 
4227  /**
4228  * @brief Insert a C string.
4229  * @param __pos Iterator referencing location in string to insert at.
4230  * @param __s The C string to insert.
4231  * @return Reference to this string.
4232  * @throw std::length_error If new length exceeds @c max_size().
4233  * @throw std::out_of_range If @a pos is beyond the end of this
4234  * string.
4235  *
4236  * Inserts the first @a n characters of @a __s starting at @a __pos. If
4237  * adding characters causes the length to exceed max_size(),
4238  * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4239  * thrown. The value of the string doesn't change if an error is
4240  * thrown.
4241  */
4242  basic_string&
4243  insert(size_type __pos, const _CharT* __s)
4244  {
4245  __glibcxx_requires_string(__s);
4246  return this->insert(__pos, __s, traits_type::length(__s));
4247  }
4248 
4249  /**
4250  * @brief Insert multiple characters.
4251  * @param __pos Index in string to insert at.
4252  * @param __n Number of characters to insert
4253  * @param __c The character to insert.
4254  * @return Reference to this string.
4255  * @throw std::length_error If new length exceeds @c max_size().
4256  * @throw std::out_of_range If @a __pos is beyond the end of this
4257  * string.
4258  *
4259  * Inserts @a __n copies of character @a __c starting at index
4260  * @a __pos. If adding characters causes the length to exceed
4261  * max_size(), length_error is thrown. If @a __pos > length(),
4262  * out_of_range is thrown. The value of the string doesn't
4263  * change if an error is thrown.
4264  */
4265  basic_string&
4266  insert(size_type __pos, size_type __n, _CharT __c)
4267  { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
4268  size_type(0), __n, __c); }
4269 
4270  /**
4271  * @brief Insert one character.
4272  * @param __p Iterator referencing position in string to insert at.
4273  * @param __c The character to insert.
4274  * @return Iterator referencing newly inserted char.
4275  * @throw std::length_error If new length exceeds @c max_size().
4276  *
4277  * Inserts character @a __c at position referenced by @a __p.
4278  * If adding character causes the length to exceed max_size(),
4279  * length_error is thrown. If @a __p is beyond end of string,
4280  * out_of_range is thrown. The value of the string doesn't
4281  * change if an error is thrown.
4282  */
4283  iterator
4284  insert(iterator __p, _CharT __c)
4285  {
4286  _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4287  const size_type __pos = __p - _M_ibegin();
4288  _M_replace_aux(__pos, size_type(0), size_type(1), __c);
4289  _M_rep()->_M_set_leaked();
4290  return iterator(_M_data() + __pos);
4291  }
4292 
4293  /**
4294  * @brief Remove characters.
4295  * @param __pos Index of first character to remove (default 0).
4296  * @param __n Number of characters to remove (default remainder).
4297  * @return Reference to this string.
4298  * @throw std::out_of_range If @a pos is beyond the end of this
4299  * string.
4300  *
4301  * Removes @a __n characters from this string starting at @a
4302  * __pos. The length of the string is reduced by @a __n. If
4303  * there are < @a __n characters to remove, the remainder of
4304  * the string is truncated. If @a __p is beyond end of string,
4305  * out_of_range is thrown. The value of the string doesn't
4306  * change if an error is thrown.
4307  */
4308  basic_string&
4309  erase(size_type __pos = 0, size_type __n = npos)
4310  {
4311  _M_mutate(_M_check(__pos, "basic_string::erase"),
4312  _M_limit(__pos, __n), size_type(0));
4313  return *this;
4314  }
4315 
4316  /**
4317  * @brief Remove one character.
4318  * @param __position Iterator referencing the character to remove.
4319  * @return iterator referencing same location after removal.
4320  *
4321  * Removes the character at @a __position from this string. The value
4322  * of the string doesn't change if an error is thrown.
4323  */
4324  iterator
4325  erase(iterator __position)
4326  {
4327  _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
4328  && __position < _M_iend());
4329  const size_type __pos = __position - _M_ibegin();
4330  _M_mutate(__pos, size_type(1), size_type(0));
4331  _M_rep()->_M_set_leaked();
4332  return iterator(_M_data() + __pos);
4333  }
4334 
4335  /**
4336  * @brief Remove a range of characters.
4337  * @param __first Iterator referencing the first character to remove.
4338  * @param __last Iterator referencing the end of the range.
4339  * @return Iterator referencing location of first after removal.
4340  *
4341  * Removes the characters in the range [first,last) from this string.
4342  * The value of the string doesn't change if an error is thrown.
4343  */
4344  iterator
4345  erase(iterator __first, iterator __last);
4346 
4347 #if __cplusplus >= 201103L
4348  /**
4349  * @brief Remove the last character.
4350  *
4351  * The string must be non-empty.
4352  */
4353  void
4354  pop_back() // FIXME C++11: should be noexcept.
4355  {
4356  __glibcxx_assert(!empty());
4357  erase(size() - 1, 1);
4358  }
4359 #endif // C++11
4360 
4361  /**
4362  * @brief Replace characters with value from another string.
4363  * @param __pos Index of first character to replace.
4364  * @param __n Number of characters to be replaced.
4365  * @param __str String to insert.
4366  * @return Reference to this string.
4367  * @throw std::out_of_range If @a pos is beyond the end of this
4368  * string.
4369  * @throw std::length_error If new length exceeds @c max_size().
4370  *
4371  * Removes the characters in the range [__pos,__pos+__n) from
4372  * this string. In place, the value of @a __str is inserted.
4373  * If @a __pos is beyond end of string, out_of_range is thrown.
4374  * If the length of the result exceeds max_size(), length_error
4375  * is thrown. The value of the string doesn't change if an
4376  * error is thrown.
4377  */
4378  basic_string&
4379  replace(size_type __pos, size_type __n, const basic_string& __str)
4380  { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
4381 
4382  /**
4383  * @brief Replace characters with value from another string.
4384  * @param __pos1 Index of first character to replace.
4385  * @param __n1 Number of characters to be replaced.
4386  * @param __str String to insert.
4387  * @param __pos2 Index of first character of str to use.
4388  * @param __n2 Number of characters from str to use.
4389  * @return Reference to this string.
4390  * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4391  * __str.size().
4392  * @throw std::length_error If new length exceeds @c max_size().
4393  *
4394  * Removes the characters in the range [__pos1,__pos1 + n) from this
4395  * string. In place, the value of @a __str is inserted. If @a __pos is
4396  * beyond end of string, out_of_range is thrown. If the length of the
4397  * result exceeds max_size(), length_error is thrown. The value of the
4398  * string doesn't change if an error is thrown.
4399  */
4400  basic_string&
4401  replace(size_type __pos1, size_type __n1, const basic_string& __str,
4402  size_type __pos2, size_type __n2)
4403  { return this->replace(__pos1, __n1, __str._M_data()
4404  + __str._M_check(__pos2, "basic_string::replace"),
4405  __str._M_limit(__pos2, __n2)); }
4406 
4407  /**
4408  * @brief Replace characters with value of a C substring.
4409  * @param __pos Index of first character to replace.
4410  * @param __n1 Number of characters to be replaced.
4411  * @param __s C string to insert.
4412  * @param __n2 Number of characters from @a s to use.
4413  * @return Reference to this string.
4414  * @throw std::out_of_range If @a pos1 > size().
4415  * @throw std::length_error If new length exceeds @c max_size().
4416  *
4417  * Removes the characters in the range [__pos,__pos + __n1)
4418  * from this string. In place, the first @a __n2 characters of
4419  * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4420  * @a __pos is beyond end of string, out_of_range is thrown. If
4421  * the length of result exceeds max_size(), length_error is
4422  * thrown. The value of the string doesn't change if an error
4423  * is thrown.
4424  */
4425  basic_string&
4426  replace(size_type __pos, size_type __n1, const _CharT* __s,
4427  size_type __n2);
4428 
4429  /**
4430  * @brief Replace characters with value of a C string.
4431  * @param __pos Index of first character to replace.
4432  * @param __n1 Number of characters to be replaced.
4433  * @param __s C string to insert.
4434  * @return Reference to this string.
4435  * @throw std::out_of_range If @a pos > size().
4436  * @throw std::length_error If new length exceeds @c max_size().
4437  *
4438  * Removes the characters in the range [__pos,__pos + __n1)
4439  * from this string. In place, the characters of @a __s are
4440  * inserted. If @a __pos is beyond end of string, out_of_range
4441  * is thrown. If the length of result exceeds max_size(),
4442  * length_error is thrown. The value of the string doesn't
4443  * change if an error is thrown.
4444  */
4445  basic_string&
4446  replace(size_type __pos, size_type __n1, const _CharT* __s)
4447  {
4448  __glibcxx_requires_string(__s);
4449  return this->replace(__pos, __n1, __s, traits_type::length(__s));
4450  }
4451 
4452  /**
4453  * @brief Replace characters with multiple characters.
4454  * @param __pos Index of first character to replace.
4455  * @param __n1 Number of characters to be replaced.
4456  * @param __n2 Number of characters to insert.
4457  * @param __c Character to insert.
4458  * @return Reference to this string.
4459  * @throw std::out_of_range If @a __pos > size().
4460  * @throw std::length_error If new length exceeds @c max_size().
4461  *
4462  * Removes the characters in the range [pos,pos + n1) from this
4463  * string. In place, @a __n2 copies of @a __c are inserted.
4464  * If @a __pos is beyond end of string, out_of_range is thrown.
4465  * If the length of result exceeds max_size(), length_error is
4466  * thrown. The value of the string doesn't change if an error
4467  * is thrown.
4468  */
4469  basic_string&
4470  replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
4471  { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
4472  _M_limit(__pos, __n1), __n2, __c); }
4473 
4474  /**
4475  * @brief Replace range of characters with string.
4476  * @param __i1 Iterator referencing start of range to replace.
4477  * @param __i2 Iterator referencing end of range to replace.
4478  * @param __str String value to insert.
4479  * @return Reference to this string.
4480  * @throw std::length_error If new length exceeds @c max_size().
4481  *
4482  * Removes the characters in the range [__i1,__i2). In place,
4483  * the value of @a __str is inserted. If the length of result
4484  * exceeds max_size(), length_error is thrown. The value of
4485  * the string doesn't change if an error is thrown.
4486  */
4487  basic_string&
4488  replace(iterator __i1, iterator __i2, const basic_string& __str)
4489  { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
4490 
4491  /**
4492  * @brief Replace range of characters with C substring.
4493  * @param __i1 Iterator referencing start of range to replace.
4494  * @param __i2 Iterator referencing end of range to replace.
4495  * @param __s C string value to insert.
4496  * @param __n Number of characters from s to insert.
4497  * @return Reference to this string.
4498  * @throw std::length_error If new length exceeds @c max_size().
4499  *
4500  * Removes the characters in the range [__i1,__i2). In place,
4501  * the first @a __n characters of @a __s are inserted. If the
4502  * length of result exceeds max_size(), length_error is thrown.
4503  * The value of the string doesn't change if an error is
4504  * thrown.
4505  */
4506  basic_string&
4507  replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
4508  {
4509  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4510  && __i2 <= _M_iend());
4511  return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
4512  }
4513 
4514  /**
4515  * @brief Replace range of characters with C string.
4516  * @param __i1 Iterator referencing start of range to replace.
4517  * @param __i2 Iterator referencing end of range to replace.
4518  * @param __s C string value to insert.
4519  * @return Reference to this string.
4520  * @throw std::length_error If new length exceeds @c max_size().
4521  *
4522  * Removes the characters in the range [__i1,__i2). In place,
4523  * the characters of @a __s are inserted. If the length of
4524  * result exceeds max_size(), length_error is thrown. The
4525  * value of the string doesn't change if an error is thrown.
4526  */
4527  basic_string&
4528  replace(iterator __i1, iterator __i2, const _CharT* __s)
4529  {
4530  __glibcxx_requires_string(__s);
4531  return this->replace(__i1, __i2, __s, traits_type::length(__s));
4532  }
4533 
4534  /**
4535  * @brief Replace range of characters with multiple characters
4536  * @param __i1 Iterator referencing start of range to replace.
4537  * @param __i2 Iterator referencing end of range to replace.
4538  * @param __n Number of characters to insert.
4539  * @param __c Character to insert.
4540  * @return Reference to this string.
4541  * @throw std::length_error If new length exceeds @c max_size().
4542  *
4543  * Removes the characters in the range [__i1,__i2). In place,
4544  * @a __n copies of @a __c are inserted. If the length of
4545  * result exceeds max_size(), length_error is thrown. The
4546  * value of the string doesn't change if an error is thrown.
4547  */
4548  basic_string&
4549  replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
4550  {
4551  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4552  && __i2 <= _M_iend());
4553  return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
4554  }
4555 
4556  /**
4557  * @brief Replace range of characters with range.
4558  * @param __i1 Iterator referencing start of range to replace.
4559  * @param __i2 Iterator referencing end of range to replace.
4560  * @param __k1 Iterator referencing start of range to insert.
4561  * @param __k2 Iterator referencing end of range to insert.
4562  * @return Reference to this string.
4563  * @throw std::length_error If new length exceeds @c max_size().
4564  *
4565  * Removes the characters in the range [__i1,__i2). In place,
4566  * characters in the range [__k1,__k2) are inserted. If the
4567  * length of result exceeds max_size(), length_error is thrown.
4568  * The value of the string doesn't change if an error is
4569  * thrown.
4570  */
4571  template<class _InputIterator>
4572  basic_string&
4573  replace(iterator __i1, iterator __i2,
4574  _InputIterator __k1, _InputIterator __k2)
4575  {
4576  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4577  && __i2 <= _M_iend());
4578  __glibcxx_requires_valid_range(__k1, __k2);
4579  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
4580  return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
4581  }
4582 
4583  // Specializations for the common case of pointer and iterator:
4584  // useful to avoid the overhead of temporary buffering in _M_replace.
4585  basic_string&
4586  replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
4587  {
4588  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4589  && __i2 <= _M_iend());
4590  __glibcxx_requires_valid_range(__k1, __k2);
4591  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4592  __k1, __k2 - __k1);
4593  }
4594 
4595  basic_string&
4596  replace(iterator __i1, iterator __i2,
4597  const _CharT* __k1, const _CharT* __k2)
4598  {
4599  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4600  && __i2 <= _M_iend());
4601  __glibcxx_requires_valid_range(__k1, __k2);
4602  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4603  __k1, __k2 - __k1);
4604  }
4605 
4606  basic_string&
4607  replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
4608  {
4609  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4610  && __i2 <= _M_iend());
4611  __glibcxx_requires_valid_range(__k1, __k2);
4612  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4613  __k1.base(), __k2 - __k1);
4614  }
4615 
4616  basic_string&
4617  replace(iterator __i1, iterator __i2,
4618  const_iterator __k1, const_iterator __k2)
4619  {
4620  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4621  && __i2 <= _M_iend());
4622  __glibcxx_requires_valid_range(__k1, __k2);
4623  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4624  __k1.base(), __k2 - __k1);
4625  }
4626 
4627 #if __cplusplus >= 201103L
4628  /**
4629  * @brief Replace range of characters with initializer_list.
4630  * @param __i1 Iterator referencing start of range to replace.
4631  * @param __i2 Iterator referencing end of range to replace.
4632  * @param __l The initializer_list of characters to insert.
4633  * @return Reference to this string.
4634  * @throw std::length_error If new length exceeds @c max_size().
4635  *
4636  * Removes the characters in the range [__i1,__i2). In place,
4637  * characters in the range [__k1,__k2) are inserted. If the
4638  * length of result exceeds max_size(), length_error is thrown.
4639  * The value of the string doesn't change if an error is
4640  * thrown.
4641  */
4642  basic_string& replace(iterator __i1, iterator __i2,
4644  { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
4645 #endif // C++11
4646 
4647  private:
4648  template<class _Integer>
4649  basic_string&
4650  _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
4651  _Integer __val, __true_type)
4652  { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
4653 
4654  template<class _InputIterator>
4655  basic_string&
4656  _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
4657  _InputIterator __k2, __false_type);
4658 
4659  basic_string&
4660  _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
4661  _CharT __c);
4662 
4663  basic_string&
4664  _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
4665  size_type __n2);
4666 
4667  // _S_construct_aux is used to implement the 21.3.1 para 15 which
4668  // requires special behaviour if _InIter is an integral type
4669  template<class _InIterator>
4670  static _CharT*
4671  _S_construct_aux(_InIterator __beg, _InIterator __end,
4672  const _Alloc& __a, __false_type)
4673  {
4674  typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
4675  return _S_construct(__beg, __end, __a, _Tag());
4676  }
4677 
4678  // _GLIBCXX_RESOLVE_LIB_DEFECTS
4679  // 438. Ambiguity in the "do the right thing" clause
4680  template<class _Integer>
4681  static _CharT*
4682  _S_construct_aux(_Integer __beg, _Integer __end,
4683  const _Alloc& __a, __true_type)
4684  { return _S_construct_aux_2(static_cast<size_type>(__beg),
4685  __end, __a); }
4686 
4687  static _CharT*
4688  _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
4689  { return _S_construct(__req, __c, __a); }
4690 
4691  template<class _InIterator>
4692  static _CharT*
4693  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
4694  {
4695  typedef typename std::__is_integer<_InIterator>::__type _Integral;
4696  return _S_construct_aux(__beg, __end, __a, _Integral());
4697  }
4698 
4699  // For Input Iterators, used in istreambuf_iterators, etc.
4700  template<class _InIterator>
4701  static _CharT*
4702  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
4704 
4705  // For forward_iterators up to random_access_iterators, used for
4706  // string::iterator, _CharT*, etc.
4707  template<class _FwdIterator>
4708  static _CharT*
4709  _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
4711 
4712  static _CharT*
4713  _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
4714 
4715  public:
4716 
4717  /**
4718  * @brief Copy substring into C string.
4719  * @param __s C string to copy value into.
4720  * @param __n Number of characters to copy.
4721  * @param __pos Index of first character to copy.
4722  * @return Number of characters actually copied
4723  * @throw std::out_of_range If __pos > size().
4724  *
4725  * Copies up to @a __n characters starting at @a __pos into the
4726  * C string @a __s. If @a __pos is %greater than size(),
4727  * out_of_range is thrown.
4728  */
4729  size_type
4730  copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
4731 
4732  /**
4733  * @brief Swap contents with another string.
4734  * @param __s String to swap with.
4735  *
4736  * Exchanges the contents of this string with that of @a __s in constant
4737  * time.
4738  */
4739  // PR 58265, this should be noexcept.
4740  void
4741  swap(basic_string& __s);
4742 
4743  // String operations:
4744  /**
4745  * @brief Return const pointer to null-terminated contents.
4746  *
4747  * This is a handle to internal data. Do not modify or dire things may
4748  * happen.
4749  */
4750  const _CharT*
4751  c_str() const _GLIBCXX_NOEXCEPT
4752  { return _M_data(); }
4753 
4754  /**
4755  * @brief Return const pointer to contents.
4756  *
4757  * This is a pointer to internal data. It is undefined to modify
4758  * the contents through the returned pointer. To get a pointer that
4759  * allows modifying the contents use @c &str[0] instead,
4760  * (or in C++17 the non-const @c str.data() overload).
4761  */
4762  const _CharT*
4763  data() const _GLIBCXX_NOEXCEPT
4764  { return _M_data(); }
4765 
4766 #if __cplusplus > 201402L
4767  /**
4768  * @brief Return non-const pointer to contents.
4769  *
4770  * This is a pointer to the character sequence held by the string.
4771  * Modifying the characters in the sequence is allowed.
4772  */
4773  _CharT*
4774  data() noexcept
4775  { return _M_data(); }
4776 #endif
4777 
4778  /**
4779  * @brief Return copy of allocator used to construct this string.
4780  */
4781  allocator_type
4782  get_allocator() const _GLIBCXX_NOEXCEPT
4783  { return _M_dataplus; }
4784 
4785  /**
4786  * @brief Find position of a C substring.
4787  * @param __s C string to locate.
4788  * @param __pos Index of character to search from.
4789  * @param __n Number of characters from @a s to search for.
4790  * @return Index of start of first occurrence.
4791  *
4792  * Starting from @a __pos, searches forward for the first @a
4793  * __n characters in @a __s within this string. If found,
4794  * returns the index where it begins. If not found, returns
4795  * npos.
4796  */
4797  size_type
4798  find(const _CharT* __s, size_type __pos, size_type __n) const
4799  _GLIBCXX_NOEXCEPT;
4800 
4801  /**
4802  * @brief Find position of a string.
4803  * @param __str String to locate.
4804  * @param __pos Index of character to search from (default 0).
4805  * @return Index of start of first occurrence.
4806  *
4807  * Starting from @a __pos, searches forward for value of @a __str within
4808  * this string. If found, returns the index where it begins. If not
4809  * found, returns npos.
4810  */
4811  size_type
4812  find(const basic_string& __str, size_type __pos = 0) const
4813  _GLIBCXX_NOEXCEPT
4814  { return this->find(__str.data(), __pos, __str.size()); }
4815 
4816  /**
4817  * @brief Find position of a C string.
4818  * @param __s C string to locate.
4819  * @param __pos Index of character to search from (default 0).
4820  * @return Index of start of first occurrence.
4821  *
4822  * Starting from @a __pos, searches forward for the value of @a
4823  * __s within this string. If found, returns the index where
4824  * it begins. If not found, returns npos.
4825  */
4826  size_type
4827  find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
4828  {
4829  __glibcxx_requires_string(__s);
4830  return this->find(__s, __pos, traits_type::length(__s));
4831  }
4832 
4833  /**
4834  * @brief Find position of a character.
4835  * @param __c Character to locate.
4836  * @param __pos Index of character to search from (default 0).
4837  * @return Index of first occurrence.
4838  *
4839  * Starting from @a __pos, searches forward for @a __c within
4840  * this string. If found, returns the index where it was
4841  * found. If not found, returns npos.
4842  */
4843  size_type
4844  find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
4845 
4846  /**
4847  * @brief Find last position of a string.
4848  * @param __str String to locate.
4849  * @param __pos Index of character to search back from (default end).
4850  * @return Index of start of last occurrence.
4851  *
4852  * Starting from @a __pos, searches backward for value of @a
4853  * __str within this string. If found, returns the index where
4854  * it begins. If not found, returns npos.
4855  */
4856  size_type
4857  rfind(const basic_string& __str, size_type __pos = npos) const
4858  _GLIBCXX_NOEXCEPT
4859  { return this->rfind(__str.data(), __pos, __str.size()); }
4860 
4861  /**
4862  * @brief Find last position of a C substring.
4863  * @param __s C string to locate.
4864  * @param __pos Index of character to search back from.
4865  * @param __n Number of characters from s to search for.
4866  * @return Index of start of last occurrence.
4867  *
4868  * Starting from @a __pos, searches backward for the first @a
4869  * __n characters in @a __s within this string. If found,
4870  * returns the index where it begins. If not found, returns
4871  * npos.
4872  */
4873  size_type
4874  rfind(const _CharT* __s, size_type __pos, size_type __n) const
4875  _GLIBCXX_NOEXCEPT;
4876 
4877  /**
4878  * @brief Find last position of a C string.
4879  * @param __s C string to locate.
4880  * @param __pos Index of character to start search at (default end).
4881  * @return Index of start of last occurrence.
4882  *
4883  * Starting from @a __pos, searches backward for the value of
4884  * @a __s within this string. If found, returns the index
4885  * where it begins. If not found, returns npos.
4886  */
4887  size_type
4888  rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
4889  {
4890  __glibcxx_requires_string(__s);
4891  return this->rfind(__s, __pos, traits_type::length(__s));
4892  }
4893 
4894  /**
4895  * @brief Find last position of a character.
4896  * @param __c Character to locate.
4897  * @param __pos Index of character to search back from (default end).
4898  * @return Index of last occurrence.
4899  *
4900  * Starting from @a __pos, searches backward for @a __c within
4901  * this string. If found, returns the index where it was
4902  * found. If not found, returns npos.
4903  */
4904  size_type
4905  rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
4906 
4907  /**
4908  * @brief Find position of a character of string.
4909  * @param __str String containing characters to locate.
4910  * @param __pos Index of character to search from (default 0).
4911  * @return Index of first occurrence.
4912  *
4913  * Starting from @a __pos, searches forward for one of the
4914  * characters of @a __str within this string. If found,
4915  * returns the index where it was found. If not found, returns
4916  * npos.
4917  */
4918  size_type
4919  find_first_of(const basic_string& __str, size_type __pos = 0) const
4920  _GLIBCXX_NOEXCEPT
4921  { return this->find_first_of(__str.data(), __pos, __str.size()); }
4922 
4923  /**
4924  * @brief Find position of a character of C substring.
4925  * @param __s String containing characters to locate.
4926  * @param __pos Index of character to search from.
4927  * @param __n Number of characters from s to search for.
4928  * @return Index of first occurrence.
4929  *
4930  * Starting from @a __pos, searches forward for one of the
4931  * first @a __n characters of @a __s within this string. If
4932  * found, returns the index where it was found. If not found,
4933  * returns npos.
4934  */
4935  size_type
4936  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
4937  _GLIBCXX_NOEXCEPT;
4938 
4939  /**
4940  * @brief Find position of a character of C string.
4941  * @param __s String containing characters to locate.
4942  * @param __pos Index of character to search from (default 0).
4943  * @return Index of first occurrence.
4944  *
4945  * Starting from @a __pos, searches forward for one of the
4946  * characters of @a __s within this string. If found, returns
4947  * the index where it was found. If not found, returns npos.
4948  */
4949  size_type
4950  find_first_of(const _CharT* __s, size_type __pos = 0) const
4951  _GLIBCXX_NOEXCEPT
4952  {
4953  __glibcxx_requires_string(__s);
4954  return this->find_first_of(__s, __pos, traits_type::length(__s));
4955  }
4956 
4957  /**
4958  * @brief Find position of a character.
4959  * @param __c Character to locate.
4960  * @param __pos Index of character to search from (default 0).
4961  * @return Index of first occurrence.
4962  *
4963  * Starting from @a __pos, searches forward for the character
4964  * @a __c within this string. If found, returns the index
4965  * where it was found. If not found, returns npos.
4966  *
4967  * Note: equivalent to find(__c, __pos).
4968  */
4969  size_type
4970  find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
4971  { return this->find(__c, __pos); }
4972 
4973  /**
4974  * @brief Find last position of a character of string.
4975  * @param __str String containing characters to locate.
4976  * @param __pos Index of character to search back from (default end).
4977  * @return Index of last occurrence.
4978  *
4979  * Starting from @a __pos, searches backward for one of the
4980  * characters of @a __str within this string. If found,
4981  * returns the index where it was found. If not found, returns
4982  * npos.
4983  */
4984  size_type
4985  find_last_of(const basic_string& __str, size_type __pos = npos) const
4986  _GLIBCXX_NOEXCEPT
4987  { return this->find_last_of(__str.data(), __pos, __str.size()); }
4988 
4989  /**
4990  * @brief Find last position of a character of C substring.
4991  * @param __s C string containing characters to locate.
4992  * @param __pos Index of character to search back from.
4993  * @param __n Number of characters from s to search for.
4994  * @return Index of last occurrence.
4995  *
4996  * Starting from @a __pos, searches backward for one of the
4997  * first @a __n characters of @a __s within this string. If
4998  * found, returns the index where it was found. If not found,
4999  * returns npos.
5000  */
5001  size_type
5002  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
5003  _GLIBCXX_NOEXCEPT;
5004 
5005  /**
5006  * @brief Find last position of a character of C string.
5007  * @param __s C string containing characters to locate.
5008  * @param __pos Index of character to search back from (default end).
5009  * @return Index of last occurrence.
5010  *
5011  * Starting from @a __pos, searches backward for one of the
5012  * characters of @a __s within this string. If found, returns
5013  * the index where it was found. If not found, returns npos.
5014  */
5015  size_type
5016  find_last_of(const _CharT* __s, size_type __pos = npos) const
5017  _GLIBCXX_NOEXCEPT
5018  {
5019  __glibcxx_requires_string(__s);
5020  return this->find_last_of(__s, __pos, traits_type::length(__s));
5021  }
5022 
5023  /**
5024  * @brief Find last position of a character.
5025  * @param __c Character to locate.
5026  * @param __pos Index of character to search back from (default end).
5027  * @return Index of last occurrence.
5028  *
5029  * Starting from @a __pos, searches backward for @a __c within
5030  * this string. If found, returns the index where it was
5031  * found. If not found, returns npos.
5032  *
5033  * Note: equivalent to rfind(__c, __pos).
5034  */
5035  size_type
5036  find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
5037  { return this->rfind(__c, __pos); }
5038 
5039  /**
5040  * @brief Find position of a character not in string.
5041  * @param __str String containing characters to avoid.
5042  * @param __pos Index of character to search from (default 0).
5043  * @return Index of first occurrence.
5044  *
5045  * Starting from @a __pos, searches forward for a character not contained
5046  * in @a __str within this string. If found, returns the index where it
5047  * was found. If not found, returns npos.
5048  */
5049  size_type
5050  find_first_not_of(const basic_string& __str, size_type __pos = 0) const
5051  _GLIBCXX_NOEXCEPT
5052  { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
5053 
5054  /**
5055  * @brief Find position of a character not in C substring.
5056  * @param __s C string containing characters to avoid.
5057  * @param __pos Index of character to search from.
5058  * @param __n Number of characters from __s to consider.
5059  * @return Index of first occurrence.
5060  *
5061  * Starting from @a __pos, searches forward for a character not
5062  * contained in the first @a __n characters of @a __s within
5063  * this string. If found, returns the index where it was
5064  * found. If not found, returns npos.
5065  */
5066  size_type
5067  find_first_not_of(const _CharT* __s, size_type __pos,
5068  size_type __n) const _GLIBCXX_NOEXCEPT;
5069 
5070  /**
5071  * @brief Find position of a character not in C string.
5072  * @param __s C string containing characters to avoid.
5073  * @param __pos Index of character to search from (default 0).
5074  * @return Index of first occurrence.
5075  *
5076  * Starting from @a __pos, searches forward for a character not
5077  * contained in @a __s within this string. If found, returns
5078  * the index where it was found. If not found, returns npos.
5079  */
5080  size_type
5081  find_first_not_of(const _CharT* __s, size_type __pos = 0) const
5082  _GLIBCXX_NOEXCEPT
5083  {
5084  __glibcxx_requires_string(__s);
5085  return this->find_first_not_of(__s, __pos, traits_type::length(__s));
5086  }
5087 
5088  /**
5089  * @brief Find position of a different character.
5090  * @param __c Character to avoid.
5091  * @param __pos Index of character to search from (default 0).
5092  * @return Index of first occurrence.
5093  *
5094  * Starting from @a __pos, searches forward for a character
5095  * other than @a __c within this string. If found, returns the
5096  * index where it was found. If not found, returns npos.
5097  */
5098  size_type
5099  find_first_not_of(_CharT __c, size_type __pos = 0) const
5100  _GLIBCXX_NOEXCEPT;
5101 
5102  /**
5103  * @brief Find last position of a character not in string.
5104  * @param __str String containing characters to avoid.
5105  * @param __pos Index of character to search back from (default end).
5106  * @return Index of last occurrence.
5107  *
5108  * Starting from @a __pos, searches backward for a character
5109  * not contained in @a __str within this string. If found,
5110  * returns the index where it was found. If not found, returns
5111  * npos.
5112  */
5113  size_type
5114  find_last_not_of(const basic_string& __str, size_type __pos = npos) const
5115  _GLIBCXX_NOEXCEPT
5116  { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
5117 
5118  /**
5119  * @brief Find last position of a character not in C substring.
5120  * @param __s C string containing characters to avoid.
5121  * @param __pos Index of character to search back from.
5122  * @param __n Number of characters from s to consider.
5123  * @return Index of last occurrence.
5124  *
5125  * Starting from @a __pos, searches backward for a character not
5126  * contained in the first @a __n characters of @a __s within this string.
5127  * If found, returns the index where it was found. If not found,
5128  * returns npos.
5129  */
5130  size_type
5131  find_last_not_of(const _CharT* __s, size_type __pos,
5132  size_type __n) const _GLIBCXX_NOEXCEPT;
5133  /**
5134  * @brief Find last position of a character not in C string.
5135  * @param __s C string containing characters to avoid.
5136  * @param __pos Index of character to search back from (default end).
5137  * @return Index of last occurrence.
5138  *
5139  * Starting from @a __pos, searches backward for a character
5140  * not contained in @a __s within this string. If found,
5141  * returns the index where it was found. If not found, returns
5142  * npos.
5143  */
5144  size_type
5145  find_last_not_of(const _CharT* __s, size_type __pos = npos) const
5146  _GLIBCXX_NOEXCEPT
5147  {
5148  __glibcxx_requires_string(__s);
5149  return this->find_last_not_of(__s, __pos, traits_type::length(__s));
5150  }
5151 
5152  /**
5153  * @brief Find last position of a different character.
5154  * @param __c Character to avoid.
5155  * @param __pos Index of character to search back from (default end).
5156  * @return Index of last occurrence.
5157  *
5158  * Starting from @a __pos, searches backward for a character other than
5159  * @a __c within this string. If found, returns the index where it was
5160  * found. If not found, returns npos.
5161  */
5162  size_type
5163  find_last_not_of(_CharT __c, size_type __pos = npos) const
5164  _GLIBCXX_NOEXCEPT;
5165 
5166  /**
5167  * @brief Get a substring.
5168  * @param __pos Index of first character (default 0).
5169  * @param __n Number of characters in substring (default remainder).
5170  * @return The new string.
5171  * @throw std::out_of_range If __pos > size().
5172  *
5173  * Construct and return a new string using the @a __n
5174  * characters starting at @a __pos. If the string is too
5175  * short, use the remainder of the characters. If @a __pos is
5176  * beyond the end of the string, out_of_range is thrown.
5177  */
5178  basic_string
5179  substr(size_type __pos = 0, size_type __n = npos) const
5180  { return basic_string(*this,
5181  _M_check(__pos, "basic_string::substr"), __n); }
5182 
5183  /**
5184  * @brief Compare to a string.
5185  * @param __str String to compare against.
5186  * @return Integer < 0, 0, or > 0.
5187  *
5188  * Returns an integer < 0 if this string is ordered before @a
5189  * __str, 0 if their values are equivalent, or > 0 if this
5190  * string is ordered after @a __str. Determines the effective
5191  * length rlen of the strings to compare as the smallest of
5192  * size() and str.size(). The function then compares the two
5193  * strings by calling traits::compare(data(), str.data(),rlen).
5194  * If the result of the comparison is nonzero returns it,
5195  * otherwise the shorter one is ordered first.
5196  */
5197  int
5198  compare(const basic_string& __str) const
5199  {
5200  const size_type __size = this->size();
5201  const size_type __osize = __str.size();
5202  const size_type __len = std::min(__size, __osize);
5203 
5204  int __r = traits_type::compare(_M_data(), __str.data(), __len);
5205  if (!__r)
5206  __r = _S_compare(__size, __osize);
5207  return __r;
5208  }
5209 
5210  /**
5211  * @brief Compare substring to a string.
5212  * @param __pos Index of first character of substring.
5213  * @param __n Number of characters in substring.
5214  * @param __str String to compare against.
5215  * @return Integer < 0, 0, or > 0.
5216  *
5217  * Form the substring of this string from the @a __n characters
5218  * starting at @a __pos. Returns an integer < 0 if the
5219  * substring is ordered before @a __str, 0 if their values are
5220  * equivalent, or > 0 if the substring is ordered after @a
5221  * __str. Determines the effective length rlen of the strings
5222  * to compare as the smallest of the length of the substring
5223  * and @a __str.size(). The function then compares the two
5224  * strings by calling
5225  * traits::compare(substring.data(),str.data(),rlen). If the
5226  * result of the comparison is nonzero returns it, otherwise
5227  * the shorter one is ordered first.
5228  */
5229  int
5230  compare(size_type __pos, size_type __n, const basic_string& __str) const;
5231 
5232  /**
5233  * @brief Compare substring to a substring.
5234  * @param __pos1 Index of first character of substring.
5235  * @param __n1 Number of characters in substring.
5236  * @param __str String to compare against.
5237  * @param __pos2 Index of first character of substring of str.
5238  * @param __n2 Number of characters in substring of str.
5239  * @return Integer < 0, 0, or > 0.
5240  *
5241  * Form the substring of this string from the @a __n1
5242  * characters starting at @a __pos1. Form the substring of @a
5243  * __str from the @a __n2 characters starting at @a __pos2.
5244  * Returns an integer < 0 if this substring is ordered before
5245  * the substring of @a __str, 0 if their values are equivalent,
5246  * or > 0 if this substring is ordered after the substring of
5247  * @a __str. Determines the effective length rlen of the
5248  * strings to compare as the smallest of the lengths of the
5249  * substrings. The function then compares the two strings by
5250  * calling
5251  * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5252  * If the result of the comparison is nonzero returns it,
5253  * otherwise the shorter one is ordered first.
5254  */
5255  int
5256  compare(size_type __pos1, size_type __n1, const basic_string& __str,
5257  size_type __pos2, size_type __n2) const;
5258 
5259  /**
5260  * @brief Compare to a C string.
5261  * @param __s C string to compare against.
5262  * @return Integer < 0, 0, or > 0.
5263  *
5264  * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5265  * their values are equivalent, or > 0 if this string is ordered after
5266  * @a __s. Determines the effective length rlen of the strings to
5267  * compare as the smallest of size() and the length of a string
5268  * constructed from @a __s. The function then compares the two strings
5269  * by calling traits::compare(data(),s,rlen). If the result of the
5270  * comparison is nonzero returns it, otherwise the shorter one is
5271  * ordered first.
5272  */
5273  int
5274  compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
5275 
5276  // _GLIBCXX_RESOLVE_LIB_DEFECTS
5277  // 5 String::compare specification questionable
5278  /**
5279  * @brief Compare substring to a C string.
5280  * @param __pos Index of first character of substring.
5281  * @param __n1 Number of characters in substring.
5282  * @param __s C string to compare against.
5283  * @return Integer < 0, 0, or > 0.
5284  *
5285  * Form the substring of this string from the @a __n1
5286  * characters starting at @a pos. Returns an integer < 0 if
5287  * the substring is ordered before @a __s, 0 if their values
5288  * are equivalent, or > 0 if the substring is ordered after @a
5289  * __s. Determines the effective length rlen of the strings to
5290  * compare as the smallest of the length of the substring and
5291  * the length of a string constructed from @a __s. The
5292  * function then compares the two string by calling
5293  * traits::compare(substring.data(),__s,rlen). If the result of
5294  * the comparison is nonzero returns it, otherwise the shorter
5295  * one is ordered first.
5296  */
5297  int
5298  compare(size_type __pos, size_type __n1, const _CharT* __s) const;
5299 
5300  /**
5301  * @brief Compare substring against a character %array.
5302  * @param __pos Index of first character of substring.
5303  * @param __n1 Number of characters in substring.
5304  * @param __s character %array to compare against.
5305  * @param __n2 Number of characters of s.
5306  * @return Integer < 0, 0, or > 0.
5307  *
5308  * Form the substring of this string from the @a __n1
5309  * characters starting at @a __pos. Form a string from the
5310  * first @a __n2 characters of @a __s. Returns an integer < 0
5311  * if this substring is ordered before the string from @a __s,
5312  * 0 if their values are equivalent, or > 0 if this substring
5313  * is ordered after the string from @a __s. Determines the
5314  * effective length rlen of the strings to compare as the
5315  * smallest of the length of the substring and @a __n2. The
5316  * function then compares the two strings by calling
5317  * traits::compare(substring.data(),s,rlen). If the result of
5318  * the comparison is nonzero returns it, otherwise the shorter
5319  * one is ordered first.
5320  *
5321  * NB: s must have at least n2 characters, &apos;\\0&apos; has
5322  * no special meaning.
5323  */
5324  int
5325  compare(size_type __pos, size_type __n1, const _CharT* __s,
5326  size_type __n2) const;
5327 
5328 # ifdef _GLIBCXX_TM_TS_INTERNAL
5329  friend void
5330  ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
5331  void* exc);
5332  friend const char*
5333  ::_txnal_cow_string_c_str(const void *that);
5334  friend void
5335  ::_txnal_cow_string_D1(void *that);
5336  friend void
5337  ::_txnal_cow_string_D1_commit(void *that);
5338 # endif
5339  };
5340 #endif // !_GLIBCXX_USE_CXX11_ABI
5341 
5342  // operator+
5343  /**
5344  * @brief Concatenate two strings.
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>
5353  {
5355  __str.append(__rhs);
5356  return __str;
5357  }
5358 
5359  /**
5360  * @brief Concatenate C string and string.
5361  * @param __lhs First string.
5362  * @param __rhs Last string.
5363  * @return New string with value of @a __lhs followed by @a __rhs.
5364  */
5365  template<typename _CharT, typename _Traits, typename _Alloc>
5367  operator+(const _CharT* __lhs,
5369 
5370  /**
5371  * @brief Concatenate character and string.
5372  * @param __lhs First string.
5373  * @param __rhs Last string.
5374  * @return New string with @a __lhs followed by @a __rhs.
5375  */
5376  template<typename _CharT, typename _Traits, typename _Alloc>
5378  operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
5379 
5380  /**
5381  * @brief Concatenate string and C string.
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  const _CharT* __rhs)
5390  {
5392  __str.append(__rhs);
5393  return __str;
5394  }
5395 
5396  /**
5397  * @brief Concatenate string and character.
5398  * @param __lhs First string.
5399  * @param __rhs Last string.
5400  * @return New string with @a __lhs followed by @a __rhs.
5401  */
5402  template<typename _CharT, typename _Traits, typename _Alloc>
5405  {
5406  typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
5407  typedef typename __string_type::size_type __size_type;
5408  __string_type __str(__lhs);
5409  __str.append(__size_type(1), __rhs);
5410  return __str;
5411  }
5412 
5413 #if __cplusplus >= 201103L
5414  template<typename _CharT, typename _Traits, typename _Alloc>
5418  { return std::move(__lhs.append(__rhs)); }
5419 
5420  template<typename _CharT, typename _Traits, typename _Alloc>
5424  { return std::move(__rhs.insert(0, __lhs)); }
5425 
5426  template<typename _CharT, typename _Traits, typename _Alloc>
5430  {
5431  const auto __size = __lhs.size() + __rhs.size();
5432  const bool __cond = (__size > __lhs.capacity()
5433  && __size <= __rhs.capacity());
5434  return __cond ? std::move(__rhs.insert(0, __lhs))
5435  : std::move(__lhs.append(__rhs));
5436  }
5437 
5438  template<typename _CharT, typename _Traits, typename _Alloc>
5440  operator+(const _CharT* __lhs,
5442  { return std::move(__rhs.insert(0, __lhs)); }
5443 
5444  template<typename _CharT, typename _Traits, typename _Alloc>
5446  operator+(_CharT __lhs,
5448  { return std::move(__rhs.insert(0, 1, __lhs)); }
5449 
5450  template<typename _CharT, typename _Traits, typename _Alloc>
5453  const _CharT* __rhs)
5454  { return std::move(__lhs.append(__rhs)); }
5455 
5456  template<typename _CharT, typename _Traits, typename _Alloc>
5459  _CharT __rhs)
5460  { return std::move(__lhs.append(1, __rhs)); }
5461 #endif
5462 
5463  // operator ==
5464  /**
5465  * @brief Test equivalence of two strings.
5466  * @param __lhs First string.
5467  * @param __rhs Second string.
5468  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
5469  */
5470  template<typename _CharT, typename _Traits, typename _Alloc>
5471  inline bool
5472  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5474  _GLIBCXX_NOEXCEPT
5475  { return __lhs.compare(__rhs) == 0; }
5476 
5477  template<typename _CharT>
5478  inline
5479  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
5480  operator==(const basic_string<_CharT>& __lhs,
5481  const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT
5482  { return (__lhs.size() == __rhs.size()
5483  && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
5484  __lhs.size())); }
5485 
5486  /**
5487  * @brief Test equivalence of C string and string.
5488  * @param __lhs C string.
5489  * @param __rhs String.
5490  * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
5491  */
5492  template<typename _CharT, typename _Traits, typename _Alloc>
5493  inline bool
5494  operator==(const _CharT* __lhs,
5496  { return __rhs.compare(__lhs) == 0; }
5497 
5498  /**
5499  * @brief Test equivalence of string and C string.
5500  * @param __lhs String.
5501  * @param __rhs C string.
5502  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
5503  */
5504  template<typename _CharT, typename _Traits, typename _Alloc>
5505  inline bool
5506  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5507  const _CharT* __rhs)
5508  { return __lhs.compare(__rhs) == 0; }
5509 
5510  // operator !=
5511  /**
5512  * @brief Test difference of two strings.
5513  * @param __lhs First string.
5514  * @param __rhs Second string.
5515  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5516  */
5517  template<typename _CharT, typename _Traits, typename _Alloc>
5518  inline bool
5519  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5521  _GLIBCXX_NOEXCEPT
5522  { return !(__lhs == __rhs); }
5523 
5524  /**
5525  * @brief Test difference of C string and string.
5526  * @param __lhs C string.
5527  * @param __rhs String.
5528  * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
5529  */
5530  template<typename _CharT, typename _Traits, typename _Alloc>
5531  inline bool
5532  operator!=(const _CharT* __lhs,
5534  { return !(__lhs == __rhs); }
5535 
5536  /**
5537  * @brief Test difference of string and C string.
5538  * @param __lhs String.
5539  * @param __rhs C string.
5540  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5541  */
5542  template<typename _CharT, typename _Traits, typename _Alloc>
5543  inline bool
5544  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5545  const _CharT* __rhs)
5546  { return !(__lhs == __rhs); }
5547 
5548  // operator <
5549  /**
5550  * @brief Test if string precedes string.
5551  * @param __lhs First string.
5552  * @param __rhs Second string.
5553  * @return True if @a __lhs precedes @a __rhs. False otherwise.
5554  */
5555  template<typename _CharT, typename _Traits, typename _Alloc>
5556  inline bool
5557  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5559  _GLIBCXX_NOEXCEPT
5560  { return __lhs.compare(__rhs) < 0; }
5561 
5562  /**
5563  * @brief Test if string precedes C string.
5564  * @param __lhs String.
5565  * @param __rhs C string.
5566  * @return True if @a __lhs precedes @a __rhs. False otherwise.
5567  */
5568  template<typename _CharT, typename _Traits, typename _Alloc>
5569  inline bool
5570  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5571  const _CharT* __rhs)
5572  { return __lhs.compare(__rhs) < 0; }
5573 
5574  /**
5575  * @brief Test if C string precedes string.
5576  * @param __lhs C string.
5577  * @param __rhs String.
5578  * @return True if @a __lhs precedes @a __rhs. False otherwise.
5579  */
5580  template<typename _CharT, typename _Traits, typename _Alloc>
5581  inline bool
5582  operator<(const _CharT* __lhs,
5584  { return __rhs.compare(__lhs) > 0; }
5585 
5586  // operator >
5587  /**
5588  * @brief Test if string follows string.
5589  * @param __lhs First string.
5590  * @param __rhs Second string.
5591  * @return True if @a __lhs follows @a __rhs. False otherwise.
5592  */
5593  template<typename _CharT, typename _Traits, typename _Alloc>
5594  inline bool
5597  _GLIBCXX_NOEXCEPT
5598  { return __lhs.compare(__rhs) > 0; }
5599 
5600  /**
5601  * @brief Test if string follows C string.
5602  * @param __lhs String.
5603  * @param __rhs C string.
5604  * @return True if @a __lhs follows @a __rhs. False otherwise.
5605  */
5606  template<typename _CharT, typename _Traits, typename _Alloc>
5607  inline bool
5609  const _CharT* __rhs)
5610  { return __lhs.compare(__rhs) > 0; }
5611 
5612  /**
5613  * @brief Test if C string follows string.
5614  * @param __lhs C string.
5615  * @param __rhs String.
5616  * @return True if @a __lhs follows @a __rhs. False otherwise.
5617  */
5618  template<typename _CharT, typename _Traits, typename _Alloc>
5619  inline bool
5620  operator>(const _CharT* __lhs,
5622  { return __rhs.compare(__lhs) < 0; }
5623 
5624  // operator <=
5625  /**
5626  * @brief Test if string doesn't follow string.
5627  * @param __lhs First string.
5628  * @param __rhs Second string.
5629  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5630  */
5631  template<typename _CharT, typename _Traits, typename _Alloc>
5632  inline bool
5633  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5635  _GLIBCXX_NOEXCEPT
5636  { return __lhs.compare(__rhs) <= 0; }
5637 
5638  /**
5639  * @brief Test if string doesn't follow C string.
5640  * @param __lhs String.
5641  * @param __rhs C string.
5642  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5643  */
5644  template<typename _CharT, typename _Traits, typename _Alloc>
5645  inline bool
5646  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5647  const _CharT* __rhs)
5648  { return __lhs.compare(__rhs) <= 0; }
5649 
5650  /**
5651  * @brief Test if C string doesn't follow string.
5652  * @param __lhs C string.
5653  * @param __rhs String.
5654  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5655  */
5656  template<typename _CharT, typename _Traits, typename _Alloc>
5657  inline bool
5658  operator<=(const _CharT* __lhs,
5660  { return __rhs.compare(__lhs) >= 0; }
5661 
5662  // operator >=
5663  /**
5664  * @brief Test if string doesn't precede string.
5665  * @param __lhs First string.
5666  * @param __rhs Second string.
5667  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5668  */
5669  template<typename _CharT, typename _Traits, typename _Alloc>
5670  inline bool
5671  operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5673  _GLIBCXX_NOEXCEPT
5674  { return __lhs.compare(__rhs) >= 0; }
5675 
5676  /**
5677  * @brief Test if string doesn't precede C string.
5678  * @param __lhs String.
5679  * @param __rhs C string.
5680  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5681  */
5682  template<typename _CharT, typename _Traits, typename _Alloc>
5683  inline bool
5684  operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
5685  const _CharT* __rhs)
5686  { return __lhs.compare(__rhs) >= 0; }
5687 
5688  /**
5689  * @brief Test if C string doesn't precede string.
5690  * @param __lhs C string.
5691  * @param __rhs String.
5692  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5693  */
5694  template<typename _CharT, typename _Traits, typename _Alloc>
5695  inline bool
5696  operator>=(const _CharT* __lhs,
5698  { return __rhs.compare(__lhs) <= 0; }
5699 
5700  /**
5701  * @brief Swap contents of two strings.
5702  * @param __lhs First string.
5703  * @param __rhs Second string.
5704  *
5705  * Exchanges the contents of @a __lhs and @a __rhs in constant time.
5706  */
5707  template<typename _CharT, typename _Traits, typename _Alloc>
5708  inline void
5711  _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
5712  { __lhs.swap(__rhs); }
5713 
5714 
5715  /**
5716  * @brief Read stream into a string.
5717  * @param __is Input stream.
5718  * @param __str Buffer to store into.
5719  * @return Reference to the input stream.
5720  *
5721  * Stores characters from @a __is into @a __str until whitespace is
5722  * found, the end of the stream is encountered, or str.max_size()
5723  * is reached. If is.width() is non-zero, that is the limit on the
5724  * number of characters stored into @a __str. Any previous
5725  * contents of @a __str are erased.
5726  */
5727  template<typename _CharT, typename _Traits, typename _Alloc>
5731 
5732  template<>
5735 
5736  /**
5737  * @brief Write string to a stream.
5738  * @param __os Output stream.
5739  * @param __str String to write out.
5740  * @return Reference to the output stream.
5741  *
5742  * Output characters of @a __str into os following the same rules as for
5743  * writing a C string.
5744  */
5745  template<typename _CharT, typename _Traits, typename _Alloc>
5747  operator<<(basic_ostream<_CharT, _Traits>& __os,
5749  {
5750  // _GLIBCXX_RESOLVE_LIB_DEFECTS
5751  // 586. string inserter not a formatted function
5752  return __ostream_insert(__os, __str.data(), __str.size());
5753  }
5754 
5755  /**
5756  * @brief Read a line from stream into a string.
5757  * @param __is Input stream.
5758  * @param __str Buffer to store into.
5759  * @param __delim Character marking end of line.
5760  * @return Reference to the input stream.
5761  *
5762  * Stores characters from @a __is into @a __str until @a __delim is
5763  * found, the end of the stream is encountered, or str.max_size()
5764  * is reached. Any previous contents of @a __str are erased. If
5765  * @a __delim is encountered, it is extracted but not stored into
5766  * @a __str.
5767  */
5768  template<typename _CharT, typename _Traits, typename _Alloc>
5771  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
5772 
5773  /**
5774  * @brief Read a line from stream into a string.
5775  * @param __is Input stream.
5776  * @param __str Buffer to store into.
5777  * @return Reference to the input stream.
5778  *
5779  * Stores characters from is into @a __str until &apos;\n&apos; is
5780  * found, the end of the stream is encountered, or str.max_size()
5781  * is reached. Any previous contents of @a __str are erased. If
5782  * end of line is encountered, it is extracted but not stored into
5783  * @a __str.
5784  */
5785  template<typename _CharT, typename _Traits, typename _Alloc>
5789  { return std::getline(__is, __str, __is.widen('\n')); }
5790 
5791 #if __cplusplus >= 201103L
5792  /// Read a line from an rvalue stream into a string.
5793  template<typename _CharT, typename _Traits, typename _Alloc>
5796  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
5797  { return std::getline(__is, __str, __delim); }
5798 
5799  /// Read a line from an rvalue stream into a string.
5800  template<typename _CharT, typename _Traits, typename _Alloc>
5804  { return std::getline(__is, __str); }
5805 #endif
5806 
5807  template<>
5810  char __delim);
5811 
5812 #ifdef _GLIBCXX_USE_WCHAR_T
5813  template<>
5816  wchar_t __delim);
5817 #endif
5818 
5819 _GLIBCXX_END_NAMESPACE_VERSION
5820 } // namespace
5821 
5822 #if __cplusplus >= 201103L
5823 
5824 #include <ext/string_conversions.h>
5825 
5826 namespace std _GLIBCXX_VISIBILITY(default)
5827 {
5828 _GLIBCXX_BEGIN_NAMESPACE_VERSION
5829 _GLIBCXX_BEGIN_NAMESPACE_CXX11
5830 
5831 #if _GLIBCXX_USE_C99_STDLIB
5832  // 21.4 Numeric Conversions [string.conversions].
5833  inline int
5834  stoi(const string& __str, size_t* __idx = 0, int __base = 10)
5835  { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
5836  __idx, __base); }
5837 
5838  inline long
5839  stol(const string& __str, size_t* __idx = 0, int __base = 10)
5840  { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
5841  __idx, __base); }
5842 
5843  inline unsigned long
5844  stoul(const string& __str, size_t* __idx = 0, int __base = 10)
5845  { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
5846  __idx, __base); }
5847 
5848  inline long long
5849  stoll(const string& __str, size_t* __idx = 0, int __base = 10)
5850  { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
5851  __idx, __base); }
5852 
5853  inline unsigned long long
5854  stoull(const string& __str, size_t* __idx = 0, int __base = 10)
5855  { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
5856  __idx, __base); }
5857 
5858  // NB: strtof vs strtod.
5859  inline float
5860  stof(const string& __str, size_t* __idx = 0)
5861  { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
5862 
5863  inline double
5864  stod(const string& __str, size_t* __idx = 0)
5865  { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
5866 
5867  inline long double
5868  stold(const string& __str, size_t* __idx = 0)
5869  { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
5870 #endif // _GLIBCXX_USE_C99_STDLIB
5871 
5872 #if _GLIBCXX_USE_C99_STDIO
5873  // NB: (v)snprintf vs sprintf.
5874 
5875  // DR 1261.
5876  inline string
5877  to_string(int __val)
5878  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
5879  "%d", __val); }
5880 
5881  inline string
5882  to_string(unsigned __val)
5883  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
5884  4 * sizeof(unsigned),
5885  "%u", __val); }
5886 
5887  inline string
5888  to_string(long __val)
5889  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
5890  "%ld", __val); }
5891 
5892  inline string
5893  to_string(unsigned long __val)
5894  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
5895  4 * sizeof(unsigned long),
5896  "%lu", __val); }
5897 
5898  inline string
5899  to_string(long long __val)
5900  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
5901  4 * sizeof(long long),
5902  "%lld", __val); }
5903 
5904  inline string
5905  to_string(unsigned long long __val)
5906  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
5907  4 * sizeof(unsigned long long),
5908  "%llu", __val); }
5909 
5910  inline string
5911  to_string(float __val)
5912  {
5913  const int __n =
5914  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
5915  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
5916  "%f", __val);
5917  }
5918 
5919  inline string
5920  to_string(double __val)
5921  {
5922  const int __n =
5923  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
5924  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
5925  "%f", __val);
5926  }
5927 
5928  inline string
5929  to_string(long double __val)
5930  {
5931  const int __n =
5932  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
5933  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
5934  "%Lf", __val);
5935  }
5936 #endif // _GLIBCXX_USE_C99_STDIO
5937 
5938 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
5939  inline int
5940  stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
5941  { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
5942  __idx, __base); }
5943 
5944  inline long
5945  stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
5946  { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
5947  __idx, __base); }
5948 
5949  inline unsigned long
5950  stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
5951  { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
5952  __idx, __base); }
5953 
5954  inline long long
5955  stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
5956  { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
5957  __idx, __base); }
5958 
5959  inline unsigned long long
5960  stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
5961  { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
5962  __idx, __base); }
5963 
5964  // NB: wcstof vs wcstod.
5965  inline float
5966  stof(const wstring& __str, size_t* __idx = 0)
5967  { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
5968 
5969  inline double
5970  stod(const wstring& __str, size_t* __idx = 0)
5971  { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
5972 
5973  inline long double
5974  stold(const wstring& __str, size_t* __idx = 0)
5975  { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
5976 
5977 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
5978  // DR 1261.
5979  inline wstring
5980  to_wstring(int __val)
5981  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
5982  L"%d", __val); }
5983 
5984  inline wstring
5985  to_wstring(unsigned __val)
5986  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
5987  4 * sizeof(unsigned),
5988  L"%u", __val); }
5989 
5990  inline wstring
5991  to_wstring(long __val)
5992  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
5993  L"%ld", __val); }
5994 
5995  inline wstring
5996  to_wstring(unsigned long __val)
5997  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
5998  4 * sizeof(unsigned long),
5999  L"%lu", __val); }
6000 
6001  inline wstring
6002  to_wstring(long long __val)
6003  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6004  4 * sizeof(long long),
6005  L"%lld", __val); }
6006 
6007  inline wstring
6008  to_wstring(unsigned long long __val)
6009  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6010  4 * sizeof(unsigned long long),
6011  L"%llu", __val); }
6012 
6013  inline wstring
6014  to_wstring(float __val)
6015  {
6016  const int __n =
6017  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6018  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6019  L"%f", __val);
6020  }
6021 
6022  inline wstring
6023  to_wstring(double __val)
6024  {
6025  const int __n =
6026  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6027  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6028  L"%f", __val);
6029  }
6030 
6031  inline wstring
6032  to_wstring(long double __val)
6033  {
6034  const int __n =
6035  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6036  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6037  L"%Lf", __val);
6038  }
6039 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6040 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6041 
6042 _GLIBCXX_END_NAMESPACE_CXX11
6043 _GLIBCXX_END_NAMESPACE_VERSION
6044 } // namespace
6045 
6046 #endif /* C++11 */
6047 
6048 #if __cplusplus >= 201103L
6049 
6050 #include <bits/functional_hash.h>
6051 
6052 namespace std _GLIBCXX_VISIBILITY(default)
6053 {
6054 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6055 
6056  // DR 1182.
6057 
6058 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6059  /// std::hash specialization for string.
6060  template<>
6061  struct hash<string>
6062  : public __hash_base<size_t, string>
6063  {
6064  size_t
6065  operator()(const string& __s) const noexcept
6066  { return std::_Hash_impl::hash(__s.data(), __s.length()); }
6067  };
6068 
6069  template<>
6070  struct __is_fast_hash<hash<string>> : std::false_type
6071  { };
6072 
6073 #ifdef _GLIBCXX_USE_WCHAR_T
6074  /// std::hash specialization for wstring.
6075  template<>
6076  struct hash<wstring>
6077  : public __hash_base<size_t, wstring>
6078  {
6079  size_t
6080  operator()(const wstring& __s) const noexcept
6081  { return std::_Hash_impl::hash(__s.data(),
6082  __s.length() * sizeof(wchar_t)); }
6083  };
6084 
6085  template<>
6086  struct __is_fast_hash<hash<wstring>> : std::false_type
6087  { };
6088 #endif
6089 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6090 
6091 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6092  /// std::hash specialization for u16string.
6093  template<>
6094  struct hash<u16string>
6095  : public __hash_base<size_t, u16string>
6096  {
6097  size_t
6098  operator()(const u16string& __s) const noexcept
6099  { return std::_Hash_impl::hash(__s.data(),
6100  __s.length() * sizeof(char16_t)); }
6101  };
6102 
6103  template<>
6104  struct __is_fast_hash<hash<u16string>> : std::false_type
6105  { };
6106 
6107  /// std::hash specialization for u32string.
6108  template<>
6109  struct hash<u32string>
6110  : public __hash_base<size_t, u32string>
6111  {
6112  size_t
6113  operator()(const u32string& __s) const noexcept
6114  { return std::_Hash_impl::hash(__s.data(),
6115  __s.length() * sizeof(char32_t)); }
6116  };
6117 
6118  template<>
6119  struct __is_fast_hash<hash<u32string>> : std::false_type
6120  { };
6121 #endif
6122 
6123 _GLIBCXX_END_NAMESPACE_VERSION
6124 
6125 #if __cplusplus > 201103L
6126 
6127 #define __cpp_lib_string_udls 201304
6128 
6129  inline namespace literals
6130  {
6131  inline namespace string_literals
6132  {
6133 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6134 
6135  _GLIBCXX_DEFAULT_ABI_TAG
6136  inline basic_string<char>
6137  operator""s(const char* __str, size_t __len)
6138  { return basic_string<char>{__str, __len}; }
6139 
6140 #ifdef _GLIBCXX_USE_WCHAR_T
6141  _GLIBCXX_DEFAULT_ABI_TAG
6142  inline basic_string<wchar_t>
6143  operator""s(const wchar_t* __str, size_t __len)
6144  { return basic_string<wchar_t>{__str, __len}; }
6145 #endif
6146 
6147 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6148  _GLIBCXX_DEFAULT_ABI_TAG
6149  inline basic_string<char16_t>
6150  operator""s(const char16_t* __str, size_t __len)
6151  { return basic_string<char16_t>{__str, __len}; }
6152 
6153  _GLIBCXX_DEFAULT_ABI_TAG
6154  inline basic_string<char32_t>
6155  operator""s(const char32_t* __str, size_t __len)
6156  { return basic_string<char32_t>{__str, __len}; }
6157 #endif
6158 
6159 _GLIBCXX_END_NAMESPACE_VERSION
6160  } // inline namespace string_literals
6161  } // inline namespace literals
6162 
6163 #endif // __cplusplus > 201103L
6164 
6165 } // namespace std
6166 
6167 #endif // C++11
6168 
6169 #endif /* _BASIC_STRING_H */
bool empty() const noexcept
int compare(const basic_string &__str) const
Compare to a string.
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.
const_reverse_iterator crend() const noexcept
reference back()
Primary class template hash.
Definition: system_error:141
const_reverse_iterator crbegin() const noexcept
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
size_type find(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a string.
size_type find_last_of(_CharT __c, size_type __pos=npos) const noexcept
Find last position of a character.
static const size_type npos
Value returned by various member functions when they fail.
basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
const_reference back() const noexcept
const_iterator begin() const noexcept
size_type find(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a C substring.
basic_string & assign(_InputIterator __first, _InputIterator __last)
Set value to a range of characters.
char_type widen(char __c) const
Widens characters.
Definition: basic_ios.h:449
const _CharT * data() const noexcept
Return const pointer to contents.
basic_string & replace(iterator __i1, iterator __i2, const _CharT *__s, size_type __n)
Replace range of characters with C substring.
Marking input iterators.
const_reference at(size_type __n) const
Provides access to the data contained in the string.
basic_string & append(const basic_string &__str)
Append a string to this string.
basic_string & operator=(initializer_list< _CharT > __l)
Set value to string constructed from initializer list.
basic_string & replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
Replace characters with multiple characters.
basic_string & replace(size_type __pos1, size_type __n1, const basic_string &__str, size_type __pos2, size_type __n2)
Replace characters with value from another string.
const_iterator end() const noexcept
Managing sequences of characters and character-like objects.
basic_string & append(initializer_list< _CharT > __l)
Append an initializer_list of characters.
size_type find_last_of(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a character of C string.
const _CharT * c_str() const noexcept
Return const pointer to null-terminated contents.
basic_string & append(const _CharT *__s)
Append a C string.
basic_string & insert(size_type __pos, const _CharT *__s)
Insert a C string.
initializer_list
ISO C++ entities toplevel namespace is std.
const_reference front() const noexcept
const_iterator cbegin() const noexcept
size_type max_size() const noexcept
Returns the size() of the largest possible string.
void push_back(_CharT __c)
Append a single character.
size_type find(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a C string.
reverse_iterator rend()
basic_string & replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
Replace range of characters with multiple characters.
void shrink_to_fit() noexcept
A non-binding request to reduce capacity() to size().
size_type find_first_of(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a character of C string.
reference at(size_type __n)
Provides access to the data contained in the string.
basic_string & insert(size_type __pos1, const basic_string &__str)
Insert value of a string.
Template class basic_ostream.
Definition: iosfwd:86
size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
~basic_string() noexcept
Destroy the string instance.
basic_string & operator=(_CharT __c)
Set value to string of length 1.
basic_string & operator+=(initializer_list< _CharT > __l)
Append an initializer_list of characters.
basic_string & operator+=(_CharT __c)
Append a character.
basic_string & replace(iterator __i1, iterator __i2, _InputIterator __k1, _InputIterator __k2)
Replace range of characters with range.
size_type find_first_of(_CharT __c, size_type __pos=0) const noexcept
Find position of a character.
void insert(iterator __p, _InputIterator __beg, _InputIterator __end)
Insert a range of characters.
basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
Basis for explicit traits specializations.
Definition: char_traits.h:227
const_reverse_iterator rend() const noexcept
Uniform interface to all pointer-like types.
Definition: ptr_traits.h:78
const_iterator cend() const noexcept
size_type find_first_not_of(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a character not in C string.
iterator erase(iterator __position)
Remove one character.
basic_string & assign(initializer_list< _CharT > __l)
Set value to an initializer_list of characters.
size_type find_last_not_of(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a character not in C string.
reference front()
basic_string & assign(const basic_string &__str, size_type __pos, size_type __n)
Set value to a substring of a string.
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1462
basic_string & insert(size_type __pos1, const basic_string &__str, size_type __pos2, size_type __n)
Insert a substring.
basic_string & append(_InputIterator __first, _InputIterator __last)
Append a range of characters.
size_type length() const noexcept
Returns the number of characters in the string, not including any null-termination.
const_reference operator[](size_type __pos) const noexcept
Subscript access to the data contained in the string.
basic_string & insert(size_type __pos, size_type __n, _CharT __c)
Insert multiple characters.
integral_constant
Definition: type_traits:69
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
reverse_iterator rbegin()
void clear() noexcept
size_type capacity() const noexcept
basic_string & replace(iterator __i1, iterator __i2, initializer_list< _CharT > __l)
Replace range of characters with initializer_list.
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
basic_string & assign(basic_string &&__str)
Set value to contents of another string.
size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
One of the comparison functors.
Definition: stl_function.h:340
reference operator[](size_type __pos)
Subscript access to the data contained in the string.
Forward iterators support a superset of input iterator operations.
basic_string & replace(size_type __pos, size_type __n1, const _CharT *__s)
Replace characters with value of a C string.
Uniform interface to C++98 and C++11 allocators.
basic_string & operator+=(const _CharT *__s)
Append a C string.
const_reverse_iterator rbegin() const noexcept
basic_string & operator=(basic_string &&__str)
Move assign the value of str to this string.
void reserve(size_type __res_arg=0)
Attempt to preallocate enough memory for specified number of characters.
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
basic_string & replace(iterator __i1, iterator __i2, const basic_string &__str)
Replace range of characters with string.
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
void insert(iterator __p, initializer_list< _CharT > __l)
Insert an initializer_list of characters.
basic_string & operator=(const _CharT *__s)
Copy contents of s into this string.
basic_string & assign(const _CharT *__s)
Set value to contents of a C string.
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
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.
size_type rfind(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a C string.
basic_string & operator+=(const basic_string &__str)
Append a string to this string.
size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition: complex:326
void pop_back()
Remove the last character.
Template class basic_istream.
Definition: iosfwd:83
basic_string & operator=(const basic_string &__str)
Assign the value of str to this string.
_GLIBCXX14_CONSTEXPR const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:195
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(basic_string &&__str) noexcept
Move construct string.
iterator insert(iterator __p, _CharT __c)
Insert one character.
basic_string()
Default constructor creates an empty string.
void resize(size_type __n)
Resizes the string to the specified number of characters.
basic_string & assign(size_type __n, _CharT __c)
Set value to multiple characters.
size_type find_last_not_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character not in string.
basic_string & assign(const basic_string &__str)
Set value to contents of another string.