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