libstdc++
basic_string.h
Go to the documentation of this file.
1 // Components for manipulating sequences of characters -*- C++ -*-
2 
3 // Copyright (C) 1997-2014 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 <debug/debug.h>
41 #if __cplusplus >= 201103L
42 #include <initializer_list>
43 #endif
44 
45 namespace std _GLIBCXX_VISIBILITY(default)
46 {
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 
49  /**
50  * @class basic_string basic_string.h <string>
51  * @brief Managing sequences of characters and character-like objects.
52  *
53  * @ingroup strings
54  * @ingroup sequences
55  *
56  * @tparam _CharT Type of character
57  * @tparam _Traits Traits for character type, defaults to
58  * char_traits<_CharT>.
59  * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
60  *
61  * Meets the requirements of a <a href="tables.html#65">container</a>, a
62  * <a href="tables.html#66">reversible container</a>, and a
63  * <a href="tables.html#67">sequence</a>. Of the
64  * <a href="tables.html#68">optional sequence requirements</a>, only
65  * @c push_back, @c at, and @c %array access are supported.
66  *
67  * @doctodo
68  *
69  *
70  * Documentation? What's that?
71  * Nathan Myers <ncm@cantrip.org>.
72  *
73  * A string looks like this:
74  *
75  * @code
76  * [_Rep]
77  * _M_length
78  * [basic_string<char_type>] _M_capacity
79  * _M_dataplus _M_refcount
80  * _M_p ----------------> unnamed array of char_type
81  * @endcode
82  *
83  * Where the _M_p points to the first character in the string, and
84  * you cast it to a pointer-to-_Rep and subtract 1 to get a
85  * pointer to the header.
86  *
87  * This approach has the enormous advantage that a string object
88  * requires only one allocation. All the ugliness is confined
89  * within a single %pair of inline functions, which each compile to
90  * a single @a add instruction: _Rep::_M_data(), and
91  * string::_M_rep(); and the allocation function which gets a
92  * block of raw bytes and with room enough and constructs a _Rep
93  * object at the front.
94  *
95  * The reason you want _M_data pointing to the character %array and
96  * not the _Rep is so that the debugger can see the string
97  * contents. (Probably we should add a non-inline member to get
98  * the _Rep for the debugger to use, so users can check the actual
99  * string length.)
100  *
101  * Note that the _Rep object is a POD so that you can have a
102  * static <em>empty string</em> _Rep object already @a constructed before
103  * static constructors have run. The reference-count encoding is
104  * chosen so that a 0 indicates one reference, so you never try to
105  * destroy the empty-string _Rep object.
106  *
107  * All but the last paragraph is considered pretty conventional
108  * for a C++ string implementation.
109  */
110  // 21.3 Template class basic_string
111  template<typename _CharT, typename _Traits, typename _Alloc>
113  {
114  typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
115 
116  // Types:
117  public:
118  typedef _Traits traits_type;
119  typedef typename _Traits::char_type value_type;
120  typedef _Alloc allocator_type;
121  typedef typename _CharT_alloc_type::size_type size_type;
122  typedef typename _CharT_alloc_type::difference_type difference_type;
123  typedef typename _CharT_alloc_type::reference reference;
124  typedef typename _CharT_alloc_type::const_reference const_reference;
125  typedef typename _CharT_alloc_type::pointer pointer;
126  typedef typename _CharT_alloc_type::const_pointer const_pointer;
127  typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
128  typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
129  const_iterator;
132 
133  private:
134  // _Rep: string representation
135  // Invariants:
136  // 1. String really contains _M_length + 1 characters: due to 21.3.4
137  // must be kept null-terminated.
138  // 2. _M_capacity >= _M_length
139  // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
140  // 3. _M_refcount has three states:
141  // -1: leaked, one reference, no ref-copies allowed, non-const.
142  // 0: one reference, non-const.
143  // n>0: n + 1 references, operations require a lock, const.
144  // 4. All fields==0 is an empty string, given the extra storage
145  // beyond-the-end for a null terminator; thus, the shared
146  // empty string representation needs no constructor.
147 
148  struct _Rep_base
149  {
150  size_type _M_length;
151  size_type _M_capacity;
152  _Atomic_word _M_refcount;
153  };
154 
155  struct _Rep : _Rep_base
156  {
157  // Types:
158  typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
159 
160  // (Public) Data members:
161 
162  // The maximum number of individual char_type elements of an
163  // individual string is determined by _S_max_size. This is the
164  // value that will be returned by max_size(). (Whereas npos
165  // is the maximum number of bytes the allocator can allocate.)
166  // If one was to divvy up the theoretical largest size string,
167  // with a terminating character and m _CharT elements, it'd
168  // look like this:
169  // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
170  // Solving for m:
171  // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
172  // In addition, this implementation quarters this amount.
173  static const size_type _S_max_size;
174  static const _CharT _S_terminal;
175 
176  // The following storage is init'd to 0 by the linker, resulting
177  // (carefully) in an empty string with one reference.
178  static size_type _S_empty_rep_storage[];
179 
180  static _Rep&
181  _S_empty_rep() _GLIBCXX_NOEXCEPT
182  {
183  // NB: Mild hack to avoid strict-aliasing warnings. Note that
184  // _S_empty_rep_storage is never modified and the punning should
185  // be reasonably safe in this case.
186  void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
187  return *reinterpret_cast<_Rep*>(__p);
188  }
189 
190  bool
191  _M_is_leaked() const _GLIBCXX_NOEXCEPT
192  { return this->_M_refcount < 0; }
193 
194  bool
195  _M_is_shared() const _GLIBCXX_NOEXCEPT
196  { return this->_M_refcount > 0; }
197 
198  void
199  _M_set_leaked() _GLIBCXX_NOEXCEPT
200  { this->_M_refcount = -1; }
201 
202  void
203  _M_set_sharable() _GLIBCXX_NOEXCEPT
204  { this->_M_refcount = 0; }
205 
206  void
207  _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
208  {
209 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
210  if (__builtin_expect(this != &_S_empty_rep(), false))
211 #endif
212  {
213  this->_M_set_sharable(); // One reference.
214  this->_M_length = __n;
215  traits_type::assign(this->_M_refdata()[__n], _S_terminal);
216  // grrr. (per 21.3.4)
217  // You cannot leave those LWG people alone for a second.
218  }
219  }
220 
221  _CharT*
222  _M_refdata() throw()
223  { return reinterpret_cast<_CharT*>(this + 1); }
224 
225  _CharT*
226  _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
227  {
228  return (!_M_is_leaked() && __alloc1 == __alloc2)
229  ? _M_refcopy() : _M_clone(__alloc1);
230  }
231 
232  // Create & Destroy
233  static _Rep*
234  _S_create(size_type, size_type, const _Alloc&);
235 
236  void
237  _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
238  {
239 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
240  if (__builtin_expect(this != &_S_empty_rep(), false))
241 #endif
242  {
243  // Be race-detector-friendly. For more info see bits/c++config.
244  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
245  if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
246  -1) <= 0)
247  {
248  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
249  _M_destroy(__a);
250  }
251  }
252  } // XXX MT
253 
254  void
255  _M_destroy(const _Alloc&) throw();
256 
257  _CharT*
258  _M_refcopy() throw()
259  {
260 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
261  if (__builtin_expect(this != &_S_empty_rep(), false))
262 #endif
263  __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
264  return _M_refdata();
265  } // XXX MT
266 
267  _CharT*
268  _M_clone(const _Alloc&, size_type __res = 0);
269  };
270 
271  // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
272  struct _Alloc_hider : _Alloc
273  {
274  _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
275  : _Alloc(__a), _M_p(__dat) { }
276 
277  _CharT* _M_p; // The actual data.
278  };
279 
280  public:
281  // Data Members (public):
282  // NB: This is an unsigned type, and thus represents the maximum
283  // size that the allocator can hold.
284  /// Value returned by various member functions when they fail.
285  static const size_type npos = static_cast<size_type>(-1);
286 
287  private:
288  // Data Members (private):
289  mutable _Alloc_hider _M_dataplus;
290 
291  _CharT*
292  _M_data() const _GLIBCXX_NOEXCEPT
293  { return _M_dataplus._M_p; }
294 
295  _CharT*
296  _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
297  { return (_M_dataplus._M_p = __p); }
298 
299  _Rep*
300  _M_rep() const _GLIBCXX_NOEXCEPT
301  { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
302 
303  // For the internal use we have functions similar to `begin'/`end'
304  // but they do not call _M_leak.
305  iterator
306  _M_ibegin() const _GLIBCXX_NOEXCEPT
307  { return iterator(_M_data()); }
308 
309  iterator
310  _M_iend() const _GLIBCXX_NOEXCEPT
311  { return iterator(_M_data() + this->size()); }
312 
313  void
314  _M_leak() // for use in begin() & non-const op[]
315  {
316  if (!_M_rep()->_M_is_leaked())
317  _M_leak_hard();
318  }
319 
320  size_type
321  _M_check(size_type __pos, const char* __s) const
322  {
323  if (__pos > this->size())
324  __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
325  "this->size() (which is %zu)"),
326  __s, __pos, this->size());
327  return __pos;
328  }
329 
330  void
331  _M_check_length(size_type __n1, size_type __n2, const char* __s) const
332  {
333  if (this->max_size() - (this->size() - __n1) < __n2)
334  __throw_length_error(__N(__s));
335  }
336 
337  // NB: _M_limit doesn't check for a bad __pos value.
338  size_type
339  _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
340  {
341  const bool __testoff = __off < this->size() - __pos;
342  return __testoff ? __off : this->size() - __pos;
343  }
344 
345  // True if _Rep and source do not overlap.
346  bool
347  _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
348  {
349  return (less<const _CharT*>()(__s, _M_data())
350  || less<const _CharT*>()(_M_data() + this->size(), __s));
351  }
352 
353  // When __n = 1 way faster than the general multichar
354  // traits_type::copy/move/assign.
355  static void
356  _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
357  {
358  if (__n == 1)
359  traits_type::assign(*__d, *__s);
360  else
361  traits_type::copy(__d, __s, __n);
362  }
363 
364  static void
365  _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
366  {
367  if (__n == 1)
368  traits_type::assign(*__d, *__s);
369  else
370  traits_type::move(__d, __s, __n);
371  }
372 
373  static void
374  _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
375  {
376  if (__n == 1)
377  traits_type::assign(*__d, __c);
378  else
379  traits_type::assign(__d, __n, __c);
380  }
381 
382  // _S_copy_chars is a separate template to permit specialization
383  // to optimize for the common case of pointers as iterators.
384  template<class _Iterator>
385  static void
386  _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
387  {
388  for (; __k1 != __k2; ++__k1, ++__p)
389  traits_type::assign(*__p, *__k1); // These types are off.
390  }
391 
392  static void
393  _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
394  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
395 
396  static void
397  _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
398  _GLIBCXX_NOEXCEPT
399  { _S_copy_chars(__p, __k1.base(), __k2.base()); }
400 
401  static void
402  _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
403  { _M_copy(__p, __k1, __k2 - __k1); }
404 
405  static void
406  _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
407  _GLIBCXX_NOEXCEPT
408  { _M_copy(__p, __k1, __k2 - __k1); }
409 
410  static int
411  _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
412  {
413  const difference_type __d = difference_type(__n1 - __n2);
414 
415  if (__d > __gnu_cxx::__numeric_traits<int>::__max)
416  return __gnu_cxx::__numeric_traits<int>::__max;
417  else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
418  return __gnu_cxx::__numeric_traits<int>::__min;
419  else
420  return int(__d);
421  }
422 
423  void
424  _M_mutate(size_type __pos, size_type __len1, size_type __len2);
425 
426  void
427  _M_leak_hard();
428 
429  static _Rep&
430  _S_empty_rep() _GLIBCXX_NOEXCEPT
431  { return _Rep::_S_empty_rep(); }
432 
433  public:
434  // Construct/copy/destroy:
435  // NB: We overload ctors in some cases instead of using default
436  // arguments, per 17.4.4.4 para. 2 item 2.
437 
438  /**
439  * @brief Default constructor creates an empty string.
440  */
442 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
443  : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
444 #else
445  : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
446 #endif
447 
448  /**
449  * @brief Construct an empty string using allocator @a a.
450  */
451  explicit
452  basic_string(const _Alloc& __a);
453 
454  // NB: per LWG issue 42, semantics different from IS:
455  /**
456  * @brief Construct string with copy of value of @a str.
457  * @param __str Source string.
458  */
459  basic_string(const basic_string& __str);
460  /**
461  * @brief Construct string as copy of a substring.
462  * @param __str Source string.
463  * @param __pos Index of first character to copy from.
464  * @param __n Number of characters to copy (default remainder).
465  */
466  basic_string(const basic_string& __str, size_type __pos,
467  size_type __n = npos);
468  /**
469  * @brief Construct string as copy of a substring.
470  * @param __str Source string.
471  * @param __pos Index of first character to copy from.
472  * @param __n Number of characters to copy.
473  * @param __a Allocator to use.
474  */
475  basic_string(const basic_string& __str, size_type __pos,
476  size_type __n, const _Alloc& __a);
477 
478  /**
479  * @brief Construct string initialized by a character %array.
480  * @param __s Source character %array.
481  * @param __n Number of characters to copy.
482  * @param __a Allocator to use (default is default allocator).
483  *
484  * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
485  * has no special meaning.
486  */
487  basic_string(const _CharT* __s, size_type __n,
488  const _Alloc& __a = _Alloc());
489  /**
490  * @brief Construct string as copy of a C string.
491  * @param __s Source C string.
492  * @param __a Allocator to use (default is default allocator).
493  */
494  basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
495  /**
496  * @brief Construct string as multiple characters.
497  * @param __n Number of characters.
498  * @param __c Character to use.
499  * @param __a Allocator to use (default is default allocator).
500  */
501  basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
502 
503 #if __cplusplus >= 201103L
504  /**
505  * @brief Move construct string.
506  * @param __str Source string.
507  *
508  * The newly-created string contains the exact contents of @a __str.
509  * @a __str is a valid, but unspecified string.
510  **/
512 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
513  noexcept // FIXME C++11: should always be noexcept.
514 #endif
515  : _M_dataplus(__str._M_dataplus)
516  {
517 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
518  __str._M_data(_S_empty_rep()._M_refdata());
519 #else
520  __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
521 #endif
522  }
523 
524  /**
525  * @brief Construct string from an initializer %list.
526  * @param __l std::initializer_list of characters.
527  * @param __a Allocator to use (default is default allocator).
528  */
529  basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
530 #endif // C++11
531 
532  /**
533  * @brief Construct string as copy of a range.
534  * @param __beg Start of range.
535  * @param __end End of range.
536  * @param __a Allocator to use (default is default allocator).
537  */
538  template<class _InputIterator>
539  basic_string(_InputIterator __beg, _InputIterator __end,
540  const _Alloc& __a = _Alloc());
541 
542  /**
543  * @brief Destroy the string instance.
544  */
545  ~basic_string() _GLIBCXX_NOEXCEPT
546  { _M_rep()->_M_dispose(this->get_allocator()); }
547 
548  /**
549  * @brief Assign the value of @a str to this string.
550  * @param __str Source string.
551  */
552  basic_string&
553  operator=(const basic_string& __str)
554  { return this->assign(__str); }
555 
556  /**
557  * @brief Copy contents of @a s into this string.
558  * @param __s Source null-terminated string.
559  */
560  basic_string&
561  operator=(const _CharT* __s)
562  { return this->assign(__s); }
563 
564  /**
565  * @brief Set value to string of length 1.
566  * @param __c Source character.
567  *
568  * Assigning to a character makes this string length 1 and
569  * (*this)[0] == @a c.
570  */
571  basic_string&
572  operator=(_CharT __c)
573  {
574  this->assign(1, __c);
575  return *this;
576  }
577 
578 #if __cplusplus >= 201103L
579  /**
580  * @brief Move assign the value of @a str to this string.
581  * @param __str Source string.
582  *
583  * The contents of @a str are moved into this string (without copying).
584  * @a str is a valid, but unspecified string.
585  **/
586  // PR 58265, this should be noexcept.
587  basic_string&
589  {
590  // NB: DR 1204.
591  this->swap(__str);
592  return *this;
593  }
594 
595  /**
596  * @brief Set value to string constructed from initializer %list.
597  * @param __l std::initializer_list.
598  */
599  basic_string&
600  operator=(initializer_list<_CharT> __l)
601  {
602  this->assign(__l.begin(), __l.size());
603  return *this;
604  }
605 #endif // C++11
606 
607  // Iterators:
608  /**
609  * Returns a read/write iterator that points to the first character in
610  * the %string. Unshares the string.
611  */
612  iterator
613  begin() // FIXME C++11: should be noexcept.
614  {
615  _M_leak();
616  return iterator(_M_data());
617  }
618 
619  /**
620  * Returns a read-only (constant) iterator that points to the first
621  * character in the %string.
622  */
623  const_iterator
624  begin() const _GLIBCXX_NOEXCEPT
625  { return const_iterator(_M_data()); }
626 
627  /**
628  * Returns a read/write iterator that points one past the last
629  * character in the %string. Unshares the string.
630  */
631  iterator
632  end() // FIXME C++11: should be noexcept.
633  {
634  _M_leak();
635  return iterator(_M_data() + this->size());
636  }
637 
638  /**
639  * Returns a read-only (constant) iterator that points one past the
640  * last character in the %string.
641  */
642  const_iterator
643  end() const _GLIBCXX_NOEXCEPT
644  { return const_iterator(_M_data() + this->size()); }
645 
646  /**
647  * Returns a read/write reverse iterator that points to the last
648  * character in the %string. Iteration is done in reverse element
649  * order. Unshares the string.
650  */
651  reverse_iterator
652  rbegin() // FIXME C++11: should be noexcept.
653  { return reverse_iterator(this->end()); }
654 
655  /**
656  * Returns a read-only (constant) reverse iterator that points
657  * to the last character in the %string. Iteration is done in
658  * reverse element order.
659  */
660  const_reverse_iterator
661  rbegin() const _GLIBCXX_NOEXCEPT
662  { return const_reverse_iterator(this->end()); }
663 
664  /**
665  * Returns a read/write reverse iterator that points to one before the
666  * first character in the %string. Iteration is done in reverse
667  * element order. Unshares the string.
668  */
669  reverse_iterator
670  rend() // FIXME C++11: should be noexcept.
671  { return reverse_iterator(this->begin()); }
672 
673  /**
674  * Returns a read-only (constant) reverse iterator that points
675  * to one before the first character in the %string. Iteration
676  * is done in reverse element order.
677  */
678  const_reverse_iterator
679  rend() const _GLIBCXX_NOEXCEPT
680  { return const_reverse_iterator(this->begin()); }
681 
682 #if __cplusplus >= 201103L
683  /**
684  * Returns a read-only (constant) iterator that points to the first
685  * character in the %string.
686  */
687  const_iterator
688  cbegin() const noexcept
689  { return const_iterator(this->_M_data()); }
690 
691  /**
692  * Returns a read-only (constant) iterator that points one past the
693  * last character in the %string.
694  */
695  const_iterator
696  cend() const noexcept
697  { return const_iterator(this->_M_data() + this->size()); }
698 
699  /**
700  * Returns a read-only (constant) reverse iterator that points
701  * to the last character in the %string. Iteration is done in
702  * reverse element order.
703  */
704  const_reverse_iterator
705  crbegin() const noexcept
706  { return const_reverse_iterator(this->end()); }
707 
708  /**
709  * Returns a read-only (constant) reverse iterator that points
710  * to one before the first character in the %string. Iteration
711  * is done in reverse element order.
712  */
713  const_reverse_iterator
714  crend() const noexcept
715  { return const_reverse_iterator(this->begin()); }
716 #endif
717 
718  public:
719  // Capacity:
720  /// Returns the number of characters in the string, not including any
721  /// null-termination.
722  size_type
723  size() const _GLIBCXX_NOEXCEPT
724  { return _M_rep()->_M_length; }
725 
726  /// Returns the number of characters in the string, not including any
727  /// null-termination.
728  size_type
729  length() const _GLIBCXX_NOEXCEPT
730  { return _M_rep()->_M_length; }
731 
732  /// Returns the size() of the largest possible %string.
733  size_type
734  max_size() const _GLIBCXX_NOEXCEPT
735  { return _Rep::_S_max_size; }
736 
737  /**
738  * @brief Resizes the %string to the specified number of characters.
739  * @param __n Number of characters the %string should contain.
740  * @param __c Character to fill any new elements.
741  *
742  * This function will %resize the %string to the specified
743  * number of characters. If the number is smaller than the
744  * %string's current size the %string is truncated, otherwise
745  * the %string is extended and new elements are %set to @a __c.
746  */
747  void
748  resize(size_type __n, _CharT __c);
749 
750  /**
751  * @brief Resizes the %string to the specified number of characters.
752  * @param __n Number of characters the %string should contain.
753  *
754  * This function will resize the %string to the specified length. If
755  * the new size is smaller than the %string's current size the %string
756  * is truncated, otherwise the %string is extended and new characters
757  * are default-constructed. For basic types such as char, this means
758  * setting them to 0.
759  */
760  void
761  resize(size_type __n)
762  { this->resize(__n, _CharT()); }
763 
764 #if __cplusplus >= 201103L
765  /// A non-binding request to reduce capacity() to size().
766  void
767  shrink_to_fit() _GLIBCXX_NOEXCEPT
768  {
769  if (capacity() > size())
770  {
771  __try
772  { reserve(0); }
773  __catch(...)
774  { }
775  }
776  }
777 #endif
778 
779  /**
780  * Returns the total number of characters that the %string can hold
781  * before needing to allocate more memory.
782  */
783  size_type
784  capacity() const _GLIBCXX_NOEXCEPT
785  { return _M_rep()->_M_capacity; }
786 
787  /**
788  * @brief Attempt to preallocate enough memory for specified number of
789  * characters.
790  * @param __res_arg Number of characters required.
791  * @throw std::length_error If @a __res_arg exceeds @c max_size().
792  *
793  * This function attempts to reserve enough memory for the
794  * %string to hold the specified number of characters. If the
795  * number requested is more than max_size(), length_error is
796  * thrown.
797  *
798  * The advantage of this function is that if optimal code is a
799  * necessity and the user can determine the string length that will be
800  * required, the user can reserve the memory in %advance, and thus
801  * prevent a possible reallocation of memory and copying of %string
802  * data.
803  */
804  void
805  reserve(size_type __res_arg = 0);
806 
807  /**
808  * Erases the string, making it empty.
809  */
810  // PR 56166: this should not throw.
811  void
813  { _M_mutate(0, this->size(), 0); }
814 
815  /**
816  * Returns true if the %string is empty. Equivalent to
817  * <code>*this == ""</code>.
818  */
819  bool
820  empty() const _GLIBCXX_NOEXCEPT
821  { return this->size() == 0; }
822 
823  // Element access:
824  /**
825  * @brief Subscript access to the data contained in the %string.
826  * @param __pos The index of the character to access.
827  * @return Read-only (constant) reference to the character.
828  *
829  * This operator allows for easy, array-style, data access.
830  * Note that data access with this operator is unchecked and
831  * out_of_range lookups are not defined. (For checked lookups
832  * see at().)
833  */
834  const_reference
835  operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
836  {
837  _GLIBCXX_DEBUG_ASSERT(__pos <= size());
838  return _M_data()[__pos];
839  }
840 
841  /**
842  * @brief Subscript access to the data contained in the %string.
843  * @param __pos The index of the character to access.
844  * @return Read/write reference to the character.
845  *
846  * This operator allows for easy, array-style, data access.
847  * Note that data access with this operator is unchecked and
848  * out_of_range lookups are not defined. (For checked lookups
849  * see at().) Unshares the string.
850  */
851  reference
852  operator[](size_type __pos)
853  {
854  // Allow pos == size() both in C++98 mode, as v3 extension,
855  // and in C++11 mode.
856  _GLIBCXX_DEBUG_ASSERT(__pos <= size());
857  // In pedantic mode be strict in C++98 mode.
858  _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
859  _M_leak();
860  return _M_data()[__pos];
861  }
862 
863  /**
864  * @brief Provides access to the data contained in the %string.
865  * @param __n The index of the character to access.
866  * @return Read-only (const) reference to the character.
867  * @throw std::out_of_range If @a n is an invalid index.
868  *
869  * This function provides for safer data access. The parameter is
870  * first checked that it is in the range of the string. The function
871  * throws out_of_range if the check fails.
872  */
873  const_reference
874  at(size_type __n) const
875  {
876  if (__n >= this->size())
877  __throw_out_of_range_fmt(__N("basic_string::at: __n "
878  "(which is %zu) >= this->size() "
879  "(which is %zu)"),
880  __n, this->size());
881  return _M_data()[__n];
882  }
883 
884  /**
885  * @brief Provides access to the data contained in the %string.
886  * @param __n The index of the character to access.
887  * @return Read/write reference to the character.
888  * @throw std::out_of_range If @a n is an invalid index.
889  *
890  * This function provides for safer data access. The parameter is
891  * first checked that it is in the range of the string. The function
892  * throws out_of_range if the check fails. Success results in
893  * unsharing the string.
894  */
895  reference
896  at(size_type __n)
897  {
898  if (__n >= size())
899  __throw_out_of_range_fmt(__N("basic_string::at: __n "
900  "(which is %zu) >= this->size() "
901  "(which is %zu)"),
902  __n, this->size());
903  _M_leak();
904  return _M_data()[__n];
905  }
906 
907 #if __cplusplus >= 201103L
908  /**
909  * Returns a read/write reference to the data at the first
910  * element of the %string.
911  */
912  reference
914  { return operator[](0); }
915 
916  /**
917  * Returns a read-only (constant) reference to the data at the first
918  * element of the %string.
919  */
920  const_reference
921  front() const _GLIBCXX_NOEXCEPT
922  { return operator[](0); }
923 
924  /**
925  * Returns a read/write reference to the data at the last
926  * element of the %string.
927  */
928  reference
930  { return operator[](this->size() - 1); }
931 
932  /**
933  * Returns a read-only (constant) reference to the data at the
934  * last element of the %string.
935  */
936  const_reference
937  back() const _GLIBCXX_NOEXCEPT
938  { return operator[](this->size() - 1); }
939 #endif
940 
941  // Modifiers:
942  /**
943  * @brief Append a string to this string.
944  * @param __str The string to append.
945  * @return Reference to this string.
946  */
947  basic_string&
948  operator+=(const basic_string& __str)
949  { return this->append(__str); }
950 
951  /**
952  * @brief Append a C string.
953  * @param __s The C string to append.
954  * @return Reference to this string.
955  */
956  basic_string&
957  operator+=(const _CharT* __s)
958  { return this->append(__s); }
959 
960  /**
961  * @brief Append a character.
962  * @param __c The character to append.
963  * @return Reference to this string.
964  */
965  basic_string&
966  operator+=(_CharT __c)
967  {
968  this->push_back(__c);
969  return *this;
970  }
971 
972 #if __cplusplus >= 201103L
973  /**
974  * @brief Append an initializer_list of characters.
975  * @param __l The initializer_list of characters to be appended.
976  * @return Reference to this string.
977  */
978  basic_string&
979  operator+=(initializer_list<_CharT> __l)
980  { return this->append(__l.begin(), __l.size()); }
981 #endif // C++11
982 
983  /**
984  * @brief Append a string to this string.
985  * @param __str The string to append.
986  * @return Reference to this string.
987  */
988  basic_string&
989  append(const basic_string& __str);
990 
991  /**
992  * @brief Append a substring.
993  * @param __str The string to append.
994  * @param __pos Index of the first character of str to append.
995  * @param __n The number of characters to append.
996  * @return Reference to this string.
997  * @throw std::out_of_range if @a __pos is not a valid index.
998  *
999  * This function appends @a __n characters from @a __str
1000  * starting at @a __pos to this string. If @a __n is is larger
1001  * than the number of available characters in @a __str, the
1002  * remainder of @a __str is appended.
1003  */
1004  basic_string&
1005  append(const basic_string& __str, size_type __pos, size_type __n);
1006 
1007  /**
1008  * @brief Append a C substring.
1009  * @param __s The C string to append.
1010  * @param __n The number of characters to append.
1011  * @return Reference to this string.
1012  */
1013  basic_string&
1014  append(const _CharT* __s, size_type __n);
1015 
1016  /**
1017  * @brief Append a C string.
1018  * @param __s The C string to append.
1019  * @return Reference to this string.
1020  */
1021  basic_string&
1022  append(const _CharT* __s)
1023  {
1024  __glibcxx_requires_string(__s);
1025  return this->append(__s, traits_type::length(__s));
1026  }
1027 
1028  /**
1029  * @brief Append multiple characters.
1030  * @param __n The number of characters to append.
1031  * @param __c The character to use.
1032  * @return Reference to this string.
1033  *
1034  * Appends __n copies of __c to this string.
1035  */
1036  basic_string&
1037  append(size_type __n, _CharT __c);
1038 
1039 #if __cplusplus >= 201103L
1040  /**
1041  * @brief Append an initializer_list of characters.
1042  * @param __l The initializer_list of characters to append.
1043  * @return Reference to this string.
1044  */
1045  basic_string&
1046  append(initializer_list<_CharT> __l)
1047  { return this->append(__l.begin(), __l.size()); }
1048 #endif // C++11
1049 
1050  /**
1051  * @brief Append a range of characters.
1052  * @param __first Iterator referencing the first character to append.
1053  * @param __last Iterator marking the end of the range.
1054  * @return Reference to this string.
1055  *
1056  * Appends characters in the range [__first,__last) to this string.
1057  */
1058  template<class _InputIterator>
1059  basic_string&
1060  append(_InputIterator __first, _InputIterator __last)
1061  { return this->replace(_M_iend(), _M_iend(), __first, __last); }
1062 
1063  /**
1064  * @brief Append a single character.
1065  * @param __c Character to append.
1066  */
1067  void
1068  push_back(_CharT __c)
1069  {
1070  const size_type __len = 1 + this->size();
1071  if (__len > this->capacity() || _M_rep()->_M_is_shared())
1072  this->reserve(__len);
1073  traits_type::assign(_M_data()[this->size()], __c);
1074  _M_rep()->_M_set_length_and_sharable(__len);
1075  }
1076 
1077  /**
1078  * @brief Set value to contents of another string.
1079  * @param __str Source string to use.
1080  * @return Reference to this string.
1081  */
1082  basic_string&
1083  assign(const basic_string& __str);
1084 
1085 #if __cplusplus >= 201103L
1086  /**
1087  * @brief Set value to contents of another string.
1088  * @param __str Source string to use.
1089  * @return Reference to this string.
1090  *
1091  * This function sets this string to the exact contents of @a __str.
1092  * @a __str is a valid, but unspecified string.
1093  */
1094  // PR 58265, this should be noexcept.
1095  basic_string&
1097  {
1098  this->swap(__str);
1099  return *this;
1100  }
1101 #endif // C++11
1102 
1103  /**
1104  * @brief Set value to a substring of a string.
1105  * @param __str The string to use.
1106  * @param __pos Index of the first character of str.
1107  * @param __n Number of characters to use.
1108  * @return Reference to this string.
1109  * @throw std::out_of_range if @a pos is not a valid index.
1110  *
1111  * This function sets this string to the substring of @a __str
1112  * consisting of @a __n characters at @a __pos. If @a __n is
1113  * is larger than the number of available characters in @a
1114  * __str, the remainder of @a __str is used.
1115  */
1116  basic_string&
1117  assign(const basic_string& __str, size_type __pos, size_type __n)
1118  { return this->assign(__str._M_data()
1119  + __str._M_check(__pos, "basic_string::assign"),
1120  __str._M_limit(__pos, __n)); }
1121 
1122  /**
1123  * @brief Set value to a C substring.
1124  * @param __s The C string to use.
1125  * @param __n Number of characters to use.
1126  * @return Reference to this string.
1127  *
1128  * This function sets the value of this string to the first @a __n
1129  * characters of @a __s. If @a __n is is larger than the number of
1130  * available characters in @a __s, the remainder of @a __s is used.
1131  */
1132  basic_string&
1133  assign(const _CharT* __s, size_type __n);
1134 
1135  /**
1136  * @brief Set value to contents of a C string.
1137  * @param __s The C string to use.
1138  * @return Reference to this string.
1139  *
1140  * This function sets the value of this string to the value of @a __s.
1141  * The data is copied, so there is no dependence on @a __s once the
1142  * function returns.
1143  */
1144  basic_string&
1145  assign(const _CharT* __s)
1146  {
1147  __glibcxx_requires_string(__s);
1148  return this->assign(__s, traits_type::length(__s));
1149  }
1150 
1151  /**
1152  * @brief Set value to multiple characters.
1153  * @param __n Length of the resulting string.
1154  * @param __c The character to use.
1155  * @return Reference to this string.
1156  *
1157  * This function sets the value of this string to @a __n copies of
1158  * character @a __c.
1159  */
1160  basic_string&
1161  assign(size_type __n, _CharT __c)
1162  { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1163 
1164  /**
1165  * @brief Set value to a range of characters.
1166  * @param __first Iterator referencing the first character to append.
1167  * @param __last Iterator marking the end of the range.
1168  * @return Reference to this string.
1169  *
1170  * Sets value of string to characters in the range [__first,__last).
1171  */
1172  template<class _InputIterator>
1173  basic_string&
1174  assign(_InputIterator __first, _InputIterator __last)
1175  { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
1176 
1177 #if __cplusplus >= 201103L
1178  /**
1179  * @brief Set value to an initializer_list of characters.
1180  * @param __l The initializer_list of characters to assign.
1181  * @return Reference to this string.
1182  */
1183  basic_string&
1184  assign(initializer_list<_CharT> __l)
1185  { return this->assign(__l.begin(), __l.size()); }
1186 #endif // C++11
1187 
1188  /**
1189  * @brief Insert multiple characters.
1190  * @param __p Iterator referencing location in string to insert at.
1191  * @param __n Number of characters to insert
1192  * @param __c The character to insert.
1193  * @throw std::length_error If new length exceeds @c max_size().
1194  *
1195  * Inserts @a __n copies of character @a __c starting at the
1196  * position referenced by iterator @a __p. If adding
1197  * characters causes the length to exceed max_size(),
1198  * length_error is thrown. The value of the string doesn't
1199  * change if an error is thrown.
1200  */
1201  void
1202  insert(iterator __p, size_type __n, _CharT __c)
1203  { this->replace(__p, __p, __n, __c); }
1204 
1205  /**
1206  * @brief Insert a range of characters.
1207  * @param __p Iterator referencing location in string to insert at.
1208  * @param __beg Start of range.
1209  * @param __end End of range.
1210  * @throw std::length_error If new length exceeds @c max_size().
1211  *
1212  * Inserts characters in range [__beg,__end). If adding
1213  * characters causes the length to exceed max_size(),
1214  * length_error is thrown. The value of the string doesn't
1215  * change if an error is thrown.
1216  */
1217  template<class _InputIterator>
1218  void
1219  insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1220  { this->replace(__p, __p, __beg, __end); }
1221 
1222 #if __cplusplus >= 201103L
1223  /**
1224  * @brief Insert an initializer_list of characters.
1225  * @param __p Iterator referencing location in string to insert at.
1226  * @param __l The initializer_list of characters to insert.
1227  * @throw std::length_error If new length exceeds @c max_size().
1228  */
1229  void
1230  insert(iterator __p, initializer_list<_CharT> __l)
1231  {
1232  _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1233  this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
1234  }
1235 #endif // C++11
1236 
1237  /**
1238  * @brief Insert value of a string.
1239  * @param __pos1 Iterator referencing location in string to insert at.
1240  * @param __str The string to insert.
1241  * @return Reference to this string.
1242  * @throw std::length_error If new length exceeds @c max_size().
1243  *
1244  * Inserts value of @a __str starting at @a __pos1. If adding
1245  * characters causes the length to exceed max_size(),
1246  * length_error is thrown. The value of the string doesn't
1247  * change if an error is thrown.
1248  */
1249  basic_string&
1250  insert(size_type __pos1, const basic_string& __str)
1251  { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1252 
1253  /**
1254  * @brief Insert a substring.
1255  * @param __pos1 Iterator referencing location in string to insert at.
1256  * @param __str The string to insert.
1257  * @param __pos2 Start of characters in str to insert.
1258  * @param __n Number of characters to insert.
1259  * @return Reference to this string.
1260  * @throw std::length_error If new length exceeds @c max_size().
1261  * @throw std::out_of_range If @a pos1 > size() or
1262  * @a __pos2 > @a str.size().
1263  *
1264  * Starting at @a pos1, insert @a __n character of @a __str
1265  * beginning with @a __pos2. If adding characters causes the
1266  * length to exceed max_size(), length_error is thrown. If @a
1267  * __pos1 is beyond the end of this string or @a __pos2 is
1268  * beyond the end of @a __str, out_of_range is thrown. The
1269  * value of the string doesn't change if an error is thrown.
1270  */
1271  basic_string&
1272  insert(size_type __pos1, const basic_string& __str,
1273  size_type __pos2, size_type __n)
1274  { return this->insert(__pos1, __str._M_data()
1275  + __str._M_check(__pos2, "basic_string::insert"),
1276  __str._M_limit(__pos2, __n)); }
1277 
1278  /**
1279  * @brief Insert a C substring.
1280  * @param __pos Iterator referencing location in string to insert at.
1281  * @param __s The C string to insert.
1282  * @param __n The number of characters to insert.
1283  * @return Reference to this string.
1284  * @throw std::length_error If new length exceeds @c max_size().
1285  * @throw std::out_of_range If @a __pos is beyond the end of this
1286  * string.
1287  *
1288  * Inserts the first @a __n characters of @a __s starting at @a
1289  * __pos. If adding characters causes the length to exceed
1290  * max_size(), length_error is thrown. If @a __pos is beyond
1291  * end(), out_of_range is thrown. The value of the string
1292  * doesn't change if an error is thrown.
1293  */
1294  basic_string&
1295  insert(size_type __pos, const _CharT* __s, size_type __n);
1296 
1297  /**
1298  * @brief Insert a C string.
1299  * @param __pos Iterator referencing location in string to insert at.
1300  * @param __s The C string to insert.
1301  * @return Reference to this string.
1302  * @throw std::length_error If new length exceeds @c max_size().
1303  * @throw std::out_of_range If @a pos is beyond the end of this
1304  * string.
1305  *
1306  * Inserts the first @a n characters of @a __s starting at @a __pos. If
1307  * adding characters causes the length to exceed max_size(),
1308  * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1309  * thrown. The value of the string doesn't change if an error is
1310  * thrown.
1311  */
1312  basic_string&
1313  insert(size_type __pos, const _CharT* __s)
1314  {
1315  __glibcxx_requires_string(__s);
1316  return this->insert(__pos, __s, traits_type::length(__s));
1317  }
1318 
1319  /**
1320  * @brief Insert multiple characters.
1321  * @param __pos Index in string to insert at.
1322  * @param __n Number of characters to insert
1323  * @param __c The character to insert.
1324  * @return Reference to this string.
1325  * @throw std::length_error If new length exceeds @c max_size().
1326  * @throw std::out_of_range If @a __pos is beyond the end of this
1327  * string.
1328  *
1329  * Inserts @a __n copies of character @a __c starting at index
1330  * @a __pos. If adding characters causes the length to exceed
1331  * max_size(), length_error is thrown. If @a __pos > length(),
1332  * out_of_range is thrown. The value of the string doesn't
1333  * change if an error is thrown.
1334  */
1335  basic_string&
1336  insert(size_type __pos, size_type __n, _CharT __c)
1337  { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1338  size_type(0), __n, __c); }
1339 
1340  /**
1341  * @brief Insert one character.
1342  * @param __p Iterator referencing position in string to insert at.
1343  * @param __c The character to insert.
1344  * @return Iterator referencing newly inserted char.
1345  * @throw std::length_error If new length exceeds @c max_size().
1346  *
1347  * Inserts character @a __c at position referenced by @a __p.
1348  * If adding character causes the length to exceed max_size(),
1349  * length_error is thrown. If @a __p is beyond end of string,
1350  * out_of_range is thrown. The value of the string doesn't
1351  * change if an error is thrown.
1352  */
1353  iterator
1354  insert(iterator __p, _CharT __c)
1355  {
1356  _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1357  const size_type __pos = __p - _M_ibegin();
1358  _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1359  _M_rep()->_M_set_leaked();
1360  return iterator(_M_data() + __pos);
1361  }
1362 
1363  /**
1364  * @brief Remove characters.
1365  * @param __pos Index of first character to remove (default 0).
1366  * @param __n Number of characters to remove (default remainder).
1367  * @return Reference to this string.
1368  * @throw std::out_of_range If @a pos is beyond the end of this
1369  * string.
1370  *
1371  * Removes @a __n characters from this string starting at @a
1372  * __pos. The length of the string is reduced by @a __n. If
1373  * there are < @a __n characters to remove, the remainder of
1374  * the string is truncated. If @a __p is beyond end of string,
1375  * out_of_range is thrown. The value of the string doesn't
1376  * change if an error is thrown.
1377  */
1378  basic_string&
1379  erase(size_type __pos = 0, size_type __n = npos)
1380  {
1381  _M_mutate(_M_check(__pos, "basic_string::erase"),
1382  _M_limit(__pos, __n), size_type(0));
1383  return *this;
1384  }
1385 
1386  /**
1387  * @brief Remove one character.
1388  * @param __position Iterator referencing the character to remove.
1389  * @return iterator referencing same location after removal.
1390  *
1391  * Removes the character at @a __position from this string. The value
1392  * of the string doesn't change if an error is thrown.
1393  */
1394  iterator
1395  erase(iterator __position)
1396  {
1397  _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1398  && __position < _M_iend());
1399  const size_type __pos = __position - _M_ibegin();
1400  _M_mutate(__pos, size_type(1), size_type(0));
1401  _M_rep()->_M_set_leaked();
1402  return iterator(_M_data() + __pos);
1403  }
1404 
1405  /**
1406  * @brief Remove a range of characters.
1407  * @param __first Iterator referencing the first character to remove.
1408  * @param __last Iterator referencing the end of the range.
1409  * @return Iterator referencing location of first after removal.
1410  *
1411  * Removes the characters in the range [first,last) from this string.
1412  * The value of the string doesn't change if an error is thrown.
1413  */
1414  iterator
1415  erase(iterator __first, iterator __last);
1416 
1417 #if __cplusplus >= 201103L
1418  /**
1419  * @brief Remove the last character.
1420  *
1421  * The string must be non-empty.
1422  */
1423  void
1424  pop_back() // FIXME C++11: should be noexcept.
1425  { erase(size()-1, 1); }
1426 #endif // C++11
1427 
1428  /**
1429  * @brief Replace characters with value from another string.
1430  * @param __pos Index of first character to replace.
1431  * @param __n Number of characters to be replaced.
1432  * @param __str String to insert.
1433  * @return Reference to this string.
1434  * @throw std::out_of_range If @a pos is beyond the end of this
1435  * string.
1436  * @throw std::length_error If new length exceeds @c max_size().
1437  *
1438  * Removes the characters in the range [__pos,__pos+__n) from
1439  * this string. In place, the value of @a __str is inserted.
1440  * If @a __pos is beyond end of string, out_of_range is thrown.
1441  * If the length of the result exceeds max_size(), length_error
1442  * is thrown. The value of the string doesn't change if an
1443  * error is thrown.
1444  */
1445  basic_string&
1446  replace(size_type __pos, size_type __n, const basic_string& __str)
1447  { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1448 
1449  /**
1450  * @brief Replace characters with value from another string.
1451  * @param __pos1 Index of first character to replace.
1452  * @param __n1 Number of characters to be replaced.
1453  * @param __str String to insert.
1454  * @param __pos2 Index of first character of str to use.
1455  * @param __n2 Number of characters from str to use.
1456  * @return Reference to this string.
1457  * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1458  * __str.size().
1459  * @throw std::length_error If new length exceeds @c max_size().
1460  *
1461  * Removes the characters in the range [__pos1,__pos1 + n) from this
1462  * string. In place, the value of @a __str is inserted. If @a __pos is
1463  * beyond end of string, out_of_range is thrown. If the length of the
1464  * result exceeds max_size(), length_error is thrown. The value of the
1465  * string doesn't change if an error is thrown.
1466  */
1467  basic_string&
1468  replace(size_type __pos1, size_type __n1, const basic_string& __str,
1469  size_type __pos2, size_type __n2)
1470  { return this->replace(__pos1, __n1, __str._M_data()
1471  + __str._M_check(__pos2, "basic_string::replace"),
1472  __str._M_limit(__pos2, __n2)); }
1473 
1474  /**
1475  * @brief Replace characters with value of a C substring.
1476  * @param __pos Index of first character to replace.
1477  * @param __n1 Number of characters to be replaced.
1478  * @param __s C string to insert.
1479  * @param __n2 Number of characters from @a s to use.
1480  * @return Reference to this string.
1481  * @throw std::out_of_range If @a pos1 > size().
1482  * @throw std::length_error If new length exceeds @c max_size().
1483  *
1484  * Removes the characters in the range [__pos,__pos + __n1)
1485  * from this string. In place, the first @a __n2 characters of
1486  * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1487  * @a __pos is beyond end of string, out_of_range is thrown. If
1488  * the length of result exceeds max_size(), length_error is
1489  * thrown. The value of the string doesn't change if an error
1490  * is thrown.
1491  */
1492  basic_string&
1493  replace(size_type __pos, size_type __n1, const _CharT* __s,
1494  size_type __n2);
1495 
1496  /**
1497  * @brief Replace characters with value of a C string.
1498  * @param __pos Index of first character to replace.
1499  * @param __n1 Number of characters to be replaced.
1500  * @param __s C string to insert.
1501  * @return Reference to this string.
1502  * @throw std::out_of_range If @a pos > size().
1503  * @throw std::length_error If new length exceeds @c max_size().
1504  *
1505  * Removes the characters in the range [__pos,__pos + __n1)
1506  * from this string. In place, the characters of @a __s are
1507  * inserted. If @a __pos is beyond end of string, out_of_range
1508  * is thrown. If the length of result exceeds max_size(),
1509  * length_error is thrown. The value of the string doesn't
1510  * change if an error is thrown.
1511  */
1512  basic_string&
1513  replace(size_type __pos, size_type __n1, const _CharT* __s)
1514  {
1515  __glibcxx_requires_string(__s);
1516  return this->replace(__pos, __n1, __s, traits_type::length(__s));
1517  }
1518 
1519  /**
1520  * @brief Replace characters with multiple characters.
1521  * @param __pos Index of first character to replace.
1522  * @param __n1 Number of characters to be replaced.
1523  * @param __n2 Number of characters to insert.
1524  * @param __c Character to insert.
1525  * @return Reference to this string.
1526  * @throw std::out_of_range If @a __pos > size().
1527  * @throw std::length_error If new length exceeds @c max_size().
1528  *
1529  * Removes the characters in the range [pos,pos + n1) from this
1530  * string. In place, @a __n2 copies of @a __c are inserted.
1531  * If @a __pos is beyond end of string, out_of_range is thrown.
1532  * If the length of result exceeds max_size(), length_error is
1533  * thrown. The value of the string doesn't change if an error
1534  * is thrown.
1535  */
1536  basic_string&
1537  replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1538  { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1539  _M_limit(__pos, __n1), __n2, __c); }
1540 
1541  /**
1542  * @brief Replace range of characters with string.
1543  * @param __i1 Iterator referencing start of range to replace.
1544  * @param __i2 Iterator referencing end of range to replace.
1545  * @param __str String value to insert.
1546  * @return Reference to this string.
1547  * @throw std::length_error If new length exceeds @c max_size().
1548  *
1549  * Removes the characters in the range [__i1,__i2). In place,
1550  * the value of @a __str is inserted. If the length of result
1551  * exceeds max_size(), length_error is thrown. The value of
1552  * the string doesn't change if an error is thrown.
1553  */
1554  basic_string&
1555  replace(iterator __i1, iterator __i2, const basic_string& __str)
1556  { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
1557 
1558  /**
1559  * @brief Replace range of characters with C substring.
1560  * @param __i1 Iterator referencing start of range to replace.
1561  * @param __i2 Iterator referencing end of range to replace.
1562  * @param __s C string value to insert.
1563  * @param __n Number of characters from s to insert.
1564  * @return Reference to this string.
1565  * @throw std::length_error If new length exceeds @c max_size().
1566  *
1567  * Removes the characters in the range [__i1,__i2). In place,
1568  * the first @a __n characters of @a __s are inserted. If the
1569  * length of result exceeds max_size(), length_error is thrown.
1570  * The value of the string doesn't change if an error is
1571  * thrown.
1572  */
1573  basic_string&
1574  replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
1575  {
1576  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1577  && __i2 <= _M_iend());
1578  return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
1579  }
1580 
1581  /**
1582  * @brief Replace range of characters with C string.
1583  * @param __i1 Iterator referencing start of range to replace.
1584  * @param __i2 Iterator referencing end of range to replace.
1585  * @param __s C string value to insert.
1586  * @return Reference to this string.
1587  * @throw std::length_error If new length exceeds @c max_size().
1588  *
1589  * Removes the characters in the range [__i1,__i2). In place,
1590  * the characters of @a __s are inserted. If the length of
1591  * result exceeds max_size(), length_error is thrown. The
1592  * value of the string doesn't change if an error is thrown.
1593  */
1594  basic_string&
1595  replace(iterator __i1, iterator __i2, const _CharT* __s)
1596  {
1597  __glibcxx_requires_string(__s);
1598  return this->replace(__i1, __i2, __s, traits_type::length(__s));
1599  }
1600 
1601  /**
1602  * @brief Replace range of characters with multiple characters
1603  * @param __i1 Iterator referencing start of range to replace.
1604  * @param __i2 Iterator referencing end of range to replace.
1605  * @param __n Number of characters to insert.
1606  * @param __c Character to insert.
1607  * @return Reference to this string.
1608  * @throw std::length_error If new length exceeds @c max_size().
1609  *
1610  * Removes the characters in the range [__i1,__i2). In place,
1611  * @a __n copies of @a __c are inserted. If the length of
1612  * result exceeds max_size(), length_error is thrown. The
1613  * value of the string doesn't change if an error is thrown.
1614  */
1615  basic_string&
1616  replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
1617  {
1618  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1619  && __i2 <= _M_iend());
1620  return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
1621  }
1622 
1623  /**
1624  * @brief Replace range of characters with range.
1625  * @param __i1 Iterator referencing start of range to replace.
1626  * @param __i2 Iterator referencing end of range to replace.
1627  * @param __k1 Iterator referencing start of range to insert.
1628  * @param __k2 Iterator referencing end of range to insert.
1629  * @return Reference to this string.
1630  * @throw std::length_error If new length exceeds @c max_size().
1631  *
1632  * Removes the characters in the range [__i1,__i2). In place,
1633  * characters in the range [__k1,__k2) are inserted. If the
1634  * length of result exceeds max_size(), length_error is thrown.
1635  * The value of the string doesn't change if an error is
1636  * thrown.
1637  */
1638  template<class _InputIterator>
1639  basic_string&
1640  replace(iterator __i1, iterator __i2,
1641  _InputIterator __k1, _InputIterator __k2)
1642  {
1643  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1644  && __i2 <= _M_iend());
1645  __glibcxx_requires_valid_range(__k1, __k2);
1646  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1647  return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
1648  }
1649 
1650  // Specializations for the common case of pointer and iterator:
1651  // useful to avoid the overhead of temporary buffering in _M_replace.
1652  basic_string&
1653  replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
1654  {
1655  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1656  && __i2 <= _M_iend());
1657  __glibcxx_requires_valid_range(__k1, __k2);
1658  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1659  __k1, __k2 - __k1);
1660  }
1661 
1662  basic_string&
1663  replace(iterator __i1, iterator __i2,
1664  const _CharT* __k1, const _CharT* __k2)
1665  {
1666  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1667  && __i2 <= _M_iend());
1668  __glibcxx_requires_valid_range(__k1, __k2);
1669  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1670  __k1, __k2 - __k1);
1671  }
1672 
1673  basic_string&
1674  replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
1675  {
1676  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1677  && __i2 <= _M_iend());
1678  __glibcxx_requires_valid_range(__k1, __k2);
1679  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1680  __k1.base(), __k2 - __k1);
1681  }
1682 
1683  basic_string&
1684  replace(iterator __i1, iterator __i2,
1685  const_iterator __k1, const_iterator __k2)
1686  {
1687  _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
1688  && __i2 <= _M_iend());
1689  __glibcxx_requires_valid_range(__k1, __k2);
1690  return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
1691  __k1.base(), __k2 - __k1);
1692  }
1693 
1694 #if __cplusplus >= 201103L
1695  /**
1696  * @brief Replace range of characters with initializer_list.
1697  * @param __i1 Iterator referencing start of range to replace.
1698  * @param __i2 Iterator referencing end of range to replace.
1699  * @param __l The initializer_list of characters to insert.
1700  * @return Reference to this string.
1701  * @throw std::length_error If new length exceeds @c max_size().
1702  *
1703  * Removes the characters in the range [__i1,__i2). In place,
1704  * characters in the range [__k1,__k2) are inserted. If the
1705  * length of result exceeds max_size(), length_error is thrown.
1706  * The value of the string doesn't change if an error is
1707  * thrown.
1708  */
1709  basic_string& replace(iterator __i1, iterator __i2,
1710  initializer_list<_CharT> __l)
1711  { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
1712 #endif // C++11
1713 
1714  private:
1715  template<class _Integer>
1716  basic_string&
1717  _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
1718  _Integer __val, __true_type)
1719  { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
1720 
1721  template<class _InputIterator>
1722  basic_string&
1723  _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
1724  _InputIterator __k2, __false_type);
1725 
1726  basic_string&
1727  _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
1728  _CharT __c);
1729 
1730  basic_string&
1731  _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
1732  size_type __n2);
1733 
1734  // _S_construct_aux is used to implement the 21.3.1 para 15 which
1735  // requires special behaviour if _InIter is an integral type
1736  template<class _InIterator>
1737  static _CharT*
1738  _S_construct_aux(_InIterator __beg, _InIterator __end,
1739  const _Alloc& __a, __false_type)
1740  {
1741  typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
1742  return _S_construct(__beg, __end, __a, _Tag());
1743  }
1744 
1745  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1746  // 438. Ambiguity in the "do the right thing" clause
1747  template<class _Integer>
1748  static _CharT*
1749  _S_construct_aux(_Integer __beg, _Integer __end,
1750  const _Alloc& __a, __true_type)
1751  { return _S_construct_aux_2(static_cast<size_type>(__beg),
1752  __end, __a); }
1753 
1754  static _CharT*
1755  _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
1756  { return _S_construct(__req, __c, __a); }
1757 
1758  template<class _InIterator>
1759  static _CharT*
1760  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
1761  {
1762  typedef typename std::__is_integer<_InIterator>::__type _Integral;
1763  return _S_construct_aux(__beg, __end, __a, _Integral());
1764  }
1765 
1766  // For Input Iterators, used in istreambuf_iterators, etc.
1767  template<class _InIterator>
1768  static _CharT*
1769  _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
1771 
1772  // For forward_iterators up to random_access_iterators, used for
1773  // string::iterator, _CharT*, etc.
1774  template<class _FwdIterator>
1775  static _CharT*
1776  _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
1778 
1779  static _CharT*
1780  _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
1781 
1782  public:
1783 
1784  /**
1785  * @brief Copy substring into C string.
1786  * @param __s C string to copy value into.
1787  * @param __n Number of characters to copy.
1788  * @param __pos Index of first character to copy.
1789  * @return Number of characters actually copied
1790  * @throw std::out_of_range If __pos > size().
1791  *
1792  * Copies up to @a __n characters starting at @a __pos into the
1793  * C string @a __s. If @a __pos is %greater than size(),
1794  * out_of_range is thrown.
1795  */
1796  size_type
1797  copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
1798 
1799  /**
1800  * @brief Swap contents with another string.
1801  * @param __s String to swap with.
1802  *
1803  * Exchanges the contents of this string with that of @a __s in constant
1804  * time.
1805  */
1806  // PR 58265, this should be noexcept.
1807  void
1808  swap(basic_string& __s);
1809 
1810  // String operations:
1811  /**
1812  * @brief Return const pointer to null-terminated contents.
1813  *
1814  * This is a handle to internal data. Do not modify or dire things may
1815  * happen.
1816  */
1817  const _CharT*
1818  c_str() const _GLIBCXX_NOEXCEPT
1819  { return _M_data(); }
1820 
1821  /**
1822  * @brief Return const pointer to contents.
1823  *
1824  * This is a handle to internal data. Do not modify or dire things may
1825  * happen.
1826  */
1827  const _CharT*
1828  data() const _GLIBCXX_NOEXCEPT
1829  { return _M_data(); }
1830 
1831  /**
1832  * @brief Return copy of allocator used to construct this string.
1833  */
1834  allocator_type
1835  get_allocator() const _GLIBCXX_NOEXCEPT
1836  { return _M_dataplus; }
1837 
1838  /**
1839  * @brief Find position of a C substring.
1840  * @param __s C string to locate.
1841  * @param __pos Index of character to search from.
1842  * @param __n Number of characters from @a s to search for.
1843  * @return Index of start of first occurrence.
1844  *
1845  * Starting from @a __pos, searches forward for the first @a
1846  * __n characters in @a __s within this string. If found,
1847  * returns the index where it begins. If not found, returns
1848  * npos.
1849  */
1850  size_type
1851  find(const _CharT* __s, size_type __pos, size_type __n) const;
1852 
1853  /**
1854  * @brief Find position of a string.
1855  * @param __str String to locate.
1856  * @param __pos Index of character to search from (default 0).
1857  * @return Index of start of first occurrence.
1858  *
1859  * Starting from @a __pos, searches forward for value of @a __str within
1860  * this string. If found, returns the index where it begins. If not
1861  * found, returns npos.
1862  */
1863  size_type
1864  find(const basic_string& __str, size_type __pos = 0) const
1865  _GLIBCXX_NOEXCEPT
1866  { return this->find(__str.data(), __pos, __str.size()); }
1867 
1868  /**
1869  * @brief Find position of a C string.
1870  * @param __s C string to locate.
1871  * @param __pos Index of character to search from (default 0).
1872  * @return Index of start of first occurrence.
1873  *
1874  * Starting from @a __pos, searches forward for the value of @a
1875  * __s within this string. If found, returns the index where
1876  * it begins. If not found, returns npos.
1877  */
1878  size_type
1879  find(const _CharT* __s, size_type __pos = 0) const
1880  {
1881  __glibcxx_requires_string(__s);
1882  return this->find(__s, __pos, traits_type::length(__s));
1883  }
1884 
1885  /**
1886  * @brief Find position of a character.
1887  * @param __c Character to locate.
1888  * @param __pos Index of character to search from (default 0).
1889  * @return Index of first occurrence.
1890  *
1891  * Starting from @a __pos, searches forward for @a __c within
1892  * this string. If found, returns the index where it was
1893  * found. If not found, returns npos.
1894  */
1895  size_type
1896  find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
1897 
1898  /**
1899  * @brief Find last position of a string.
1900  * @param __str String to locate.
1901  * @param __pos Index of character to search back from (default end).
1902  * @return Index of start of last occurrence.
1903  *
1904  * Starting from @a __pos, searches backward for value of @a
1905  * __str within this string. If found, returns the index where
1906  * it begins. If not found, returns npos.
1907  */
1908  size_type
1909  rfind(const basic_string& __str, size_type __pos = npos) const
1910  _GLIBCXX_NOEXCEPT
1911  { return this->rfind(__str.data(), __pos, __str.size()); }
1912 
1913  /**
1914  * @brief Find last position of a C substring.
1915  * @param __s C string to locate.
1916  * @param __pos Index of character to search back from.
1917  * @param __n Number of characters from s to search for.
1918  * @return Index of start of last occurrence.
1919  *
1920  * Starting from @a __pos, searches backward for the first @a
1921  * __n characters in @a __s within this string. If found,
1922  * returns the index where it begins. If not found, returns
1923  * npos.
1924  */
1925  size_type
1926  rfind(const _CharT* __s, size_type __pos, size_type __n) const;
1927 
1928  /**
1929  * @brief Find last position of a C string.
1930  * @param __s C string to locate.
1931  * @param __pos Index of character to start search at (default end).
1932  * @return Index of start of last occurrence.
1933  *
1934  * Starting from @a __pos, searches backward for the value of
1935  * @a __s within this string. If found, returns the index
1936  * where it begins. If not found, returns npos.
1937  */
1938  size_type
1939  rfind(const _CharT* __s, size_type __pos = npos) const
1940  {
1941  __glibcxx_requires_string(__s);
1942  return this->rfind(__s, __pos, traits_type::length(__s));
1943  }
1944 
1945  /**
1946  * @brief Find last position of a character.
1947  * @param __c Character to locate.
1948  * @param __pos Index of character to search back from (default end).
1949  * @return Index of last occurrence.
1950  *
1951  * Starting from @a __pos, searches backward for @a __c within
1952  * this string. If found, returns the index where it was
1953  * found. If not found, returns npos.
1954  */
1955  size_type
1956  rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
1957 
1958  /**
1959  * @brief Find position of a character of string.
1960  * @param __str String containing characters to locate.
1961  * @param __pos Index of character to search from (default 0).
1962  * @return Index of first occurrence.
1963  *
1964  * Starting from @a __pos, searches forward for one of the
1965  * characters of @a __str within this string. If found,
1966  * returns the index where it was found. If not found, returns
1967  * npos.
1968  */
1969  size_type
1970  find_first_of(const basic_string& __str, size_type __pos = 0) const
1971  _GLIBCXX_NOEXCEPT
1972  { return this->find_first_of(__str.data(), __pos, __str.size()); }
1973 
1974  /**
1975  * @brief Find position of a character of C substring.
1976  * @param __s String containing characters to locate.
1977  * @param __pos Index of character to search from.
1978  * @param __n Number of characters from s to search for.
1979  * @return Index of first occurrence.
1980  *
1981  * Starting from @a __pos, searches forward for one of the
1982  * first @a __n characters of @a __s within this string. If
1983  * found, returns the index where it was found. If not found,
1984  * returns npos.
1985  */
1986  size_type
1987  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
1988 
1989  /**
1990  * @brief Find position of a character of C string.
1991  * @param __s String containing characters to locate.
1992  * @param __pos Index of character to search from (default 0).
1993  * @return Index of first occurrence.
1994  *
1995  * Starting from @a __pos, searches forward for one of the
1996  * characters of @a __s within this string. If found, returns
1997  * the index where it was found. If not found, returns npos.
1998  */
1999  size_type
2000  find_first_of(const _CharT* __s, size_type __pos = 0) const
2001  {
2002  __glibcxx_requires_string(__s);
2003  return this->find_first_of(__s, __pos, traits_type::length(__s));
2004  }
2005 
2006  /**
2007  * @brief Find position of a character.
2008  * @param __c Character to locate.
2009  * @param __pos Index of character to search from (default 0).
2010  * @return Index of first occurrence.
2011  *
2012  * Starting from @a __pos, searches forward for the character
2013  * @a __c within this string. If found, returns the index
2014  * where it was found. If not found, returns npos.
2015  *
2016  * Note: equivalent to find(__c, __pos).
2017  */
2018  size_type
2019  find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2020  { return this->find(__c, __pos); }
2021 
2022  /**
2023  * @brief Find last position of a character of string.
2024  * @param __str String containing characters to locate.
2025  * @param __pos Index of character to search back from (default end).
2026  * @return Index of last occurrence.
2027  *
2028  * Starting from @a __pos, searches backward for one of the
2029  * characters of @a __str within this string. If found,
2030  * returns the index where it was found. If not found, returns
2031  * npos.
2032  */
2033  size_type
2034  find_last_of(const basic_string& __str, size_type __pos = npos) const
2035  _GLIBCXX_NOEXCEPT
2036  { return this->find_last_of(__str.data(), __pos, __str.size()); }
2037 
2038  /**
2039  * @brief Find last position of a character of C substring.
2040  * @param __s C string containing characters to locate.
2041  * @param __pos Index of character to search back from.
2042  * @param __n Number of characters from s to search for.
2043  * @return Index of last occurrence.
2044  *
2045  * Starting from @a __pos, searches backward for one of the
2046  * first @a __n characters of @a __s within this string. If
2047  * found, returns the index where it was found. If not found,
2048  * returns npos.
2049  */
2050  size_type
2051  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
2052 
2053  /**
2054  * @brief Find last position of a character of C string.
2055  * @param __s C string containing characters to locate.
2056  * @param __pos Index of character to search back from (default end).
2057  * @return Index of last occurrence.
2058  *
2059  * Starting from @a __pos, searches backward for one of the
2060  * characters of @a __s within this string. If found, returns
2061  * the index where it was found. If not found, returns npos.
2062  */
2063  size_type
2064  find_last_of(const _CharT* __s, size_type __pos = npos) const
2065  {
2066  __glibcxx_requires_string(__s);
2067  return this->find_last_of(__s, __pos, traits_type::length(__s));
2068  }
2069 
2070  /**
2071  * @brief Find last position of a character.
2072  * @param __c Character to locate.
2073  * @param __pos Index of character to search back from (default end).
2074  * @return Index of last occurrence.
2075  *
2076  * Starting from @a __pos, searches backward for @a __c within
2077  * this string. If found, returns the index where it was
2078  * found. If not found, returns npos.
2079  *
2080  * Note: equivalent to rfind(__c, __pos).
2081  */
2082  size_type
2083  find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2084  { return this->rfind(__c, __pos); }
2085 
2086  /**
2087  * @brief Find position of a character not in string.
2088  * @param __str String containing characters to avoid.
2089  * @param __pos Index of character to search from (default 0).
2090  * @return Index of first occurrence.
2091  *
2092  * Starting from @a __pos, searches forward for a character not contained
2093  * in @a __str within this string. If found, returns the index where it
2094  * was found. If not found, returns npos.
2095  */
2096  size_type
2097  find_first_not_of(const basic_string& __str, size_type __pos = 0) const
2098  _GLIBCXX_NOEXCEPT
2099  { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2100 
2101  /**
2102  * @brief Find position of a character not in C substring.
2103  * @param __s C string containing characters to avoid.
2104  * @param __pos Index of character to search from.
2105  * @param __n Number of characters from __s to consider.
2106  * @return Index of first occurrence.
2107  *
2108  * Starting from @a __pos, searches forward for a character not
2109  * contained in the first @a __n characters of @a __s within
2110  * this string. If found, returns the index where it was
2111  * found. If not found, returns npos.
2112  */
2113  size_type
2114  find_first_not_of(const _CharT* __s, size_type __pos,
2115  size_type __n) const;
2116 
2117  /**
2118  * @brief Find position of a character not in C string.
2119  * @param __s C string containing characters to avoid.
2120  * @param __pos Index of character to search from (default 0).
2121  * @return Index of first occurrence.
2122  *
2123  * Starting from @a __pos, searches forward for a character not
2124  * contained in @a __s within this string. If found, returns
2125  * the index where it was found. If not found, returns npos.
2126  */
2127  size_type
2128  find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2129  {
2130  __glibcxx_requires_string(__s);
2131  return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2132  }
2133 
2134  /**
2135  * @brief Find position of a different character.
2136  * @param __c Character to avoid.
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 a character
2141  * other than @a __c within this string. If found, returns the
2142  * index where it was found. If not found, returns npos.
2143  */
2144  size_type
2145  find_first_not_of(_CharT __c, size_type __pos = 0) const
2146  _GLIBCXX_NOEXCEPT;
2147 
2148  /**
2149  * @brief Find last position of a character not in string.
2150  * @param __str String containing characters to avoid.
2151  * @param __pos Index of character to search back from (default end).
2152  * @return Index of last occurrence.
2153  *
2154  * Starting from @a __pos, searches backward for a character
2155  * not contained in @a __str within this string. If found,
2156  * returns the index where it was found. If not found, returns
2157  * npos.
2158  */
2159  size_type
2160  find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2161  _GLIBCXX_NOEXCEPT
2162  { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2163 
2164  /**
2165  * @brief Find last position of a character not in C substring.
2166  * @param __s C string containing characters to avoid.
2167  * @param __pos Index of character to search back from.
2168  * @param __n Number of characters from s to consider.
2169  * @return Index of last occurrence.
2170  *
2171  * Starting from @a __pos, searches backward for a character not
2172  * contained in the first @a __n characters of @a __s within this string.
2173  * If found, returns the index where it was found. If not found,
2174  * returns npos.
2175  */
2176  size_type
2177  find_last_not_of(const _CharT* __s, size_type __pos,
2178  size_type __n) const;
2179  /**
2180  * @brief Find last position of a character not in C string.
2181  * @param __s C string containing characters to avoid.
2182  * @param __pos Index of character to search back from (default end).
2183  * @return Index of last occurrence.
2184  *
2185  * Starting from @a __pos, searches backward for a character
2186  * not contained in @a __s within this string. If found,
2187  * returns the index where it was found. If not found, returns
2188  * npos.
2189  */
2190  size_type
2191  find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2192  {
2193  __glibcxx_requires_string(__s);
2194  return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2195  }
2196 
2197  /**
2198  * @brief Find last position of a different character.
2199  * @param __c Character to avoid.
2200  * @param __pos Index of character to search back from (default end).
2201  * @return Index of last occurrence.
2202  *
2203  * Starting from @a __pos, searches backward for a character other than
2204  * @a __c within this string. If found, returns the index where it was
2205  * found. If not found, returns npos.
2206  */
2207  size_type
2208  find_last_not_of(_CharT __c, size_type __pos = npos) const
2209  _GLIBCXX_NOEXCEPT;
2210 
2211  /**
2212  * @brief Get a substring.
2213  * @param __pos Index of first character (default 0).
2214  * @param __n Number of characters in substring (default remainder).
2215  * @return The new string.
2216  * @throw std::out_of_range If __pos > size().
2217  *
2218  * Construct and return a new string using the @a __n
2219  * characters starting at @a __pos. If the string is too
2220  * short, use the remainder of the characters. If @a __pos is
2221  * beyond the end of the string, out_of_range is thrown.
2222  */
2223  basic_string
2224  substr(size_type __pos = 0, size_type __n = npos) const
2225  { return basic_string(*this,
2226  _M_check(__pos, "basic_string::substr"), __n); }
2227 
2228  /**
2229  * @brief Compare to a string.
2230  * @param __str String to compare against.
2231  * @return Integer < 0, 0, or > 0.
2232  *
2233  * Returns an integer < 0 if this string is ordered before @a
2234  * __str, 0 if their values are equivalent, or > 0 if this
2235  * string is ordered after @a __str. Determines the effective
2236  * length rlen of the strings to compare as the smallest of
2237  * size() and str.size(). The function then compares the two
2238  * strings by calling traits::compare(data(), str.data(),rlen).
2239  * If the result of the comparison is nonzero returns it,
2240  * otherwise the shorter one is ordered first.
2241  */
2242  int
2243  compare(const basic_string& __str) const
2244  {
2245  const size_type __size = this->size();
2246  const size_type __osize = __str.size();
2247  const size_type __len = std::min(__size, __osize);
2248 
2249  int __r = traits_type::compare(_M_data(), __str.data(), __len);
2250  if (!__r)
2251  __r = _S_compare(__size, __osize);
2252  return __r;
2253  }
2254 
2255  /**
2256  * @brief Compare substring to a string.
2257  * @param __pos Index of first character of substring.
2258  * @param __n Number of characters in substring.
2259  * @param __str String to compare against.
2260  * @return Integer < 0, 0, or > 0.
2261  *
2262  * Form the substring of this string from the @a __n characters
2263  * starting at @a __pos. Returns an integer < 0 if the
2264  * substring is ordered before @a __str, 0 if their values are
2265  * equivalent, or > 0 if the substring is ordered after @a
2266  * __str. Determines the effective length rlen of the strings
2267  * to compare as the smallest of the length of the substring
2268  * and @a __str.size(). The function then compares the two
2269  * strings by calling
2270  * traits::compare(substring.data(),str.data(),rlen). If the
2271  * result of the comparison is nonzero returns it, otherwise
2272  * the shorter one is ordered first.
2273  */
2274  int
2275  compare(size_type __pos, size_type __n, const basic_string& __str) const;
2276 
2277  /**
2278  * @brief Compare substring to a substring.
2279  * @param __pos1 Index of first character of substring.
2280  * @param __n1 Number of characters in substring.
2281  * @param __str String to compare against.
2282  * @param __pos2 Index of first character of substring of str.
2283  * @param __n2 Number of characters in substring of str.
2284  * @return Integer < 0, 0, or > 0.
2285  *
2286  * Form the substring of this string from the @a __n1
2287  * characters starting at @a __pos1. Form the substring of @a
2288  * __str from the @a __n2 characters starting at @a __pos2.
2289  * Returns an integer < 0 if this substring is ordered before
2290  * the substring of @a __str, 0 if their values are equivalent,
2291  * or > 0 if this substring is ordered after the substring of
2292  * @a __str. Determines the effective length rlen of the
2293  * strings to compare as the smallest of the lengths of the
2294  * substrings. The function then compares the two strings by
2295  * calling
2296  * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2297  * If the result of the comparison is nonzero returns it,
2298  * otherwise the shorter one is ordered first.
2299  */
2300  int
2301  compare(size_type __pos1, size_type __n1, const basic_string& __str,
2302  size_type __pos2, size_type __n2) const;
2303 
2304  /**
2305  * @brief Compare to a C string.
2306  * @param __s C string to compare against.
2307  * @return Integer < 0, 0, or > 0.
2308  *
2309  * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2310  * their values are equivalent, or > 0 if this string is ordered after
2311  * @a __s. Determines the effective length rlen of the strings to
2312  * compare as the smallest of size() and the length of a string
2313  * constructed from @a __s. The function then compares the two strings
2314  * by calling traits::compare(data(),s,rlen). If the result of the
2315  * comparison is nonzero returns it, otherwise the shorter one is
2316  * ordered first.
2317  */
2318  int
2319  compare(const _CharT* __s) const;
2320 
2321  // _GLIBCXX_RESOLVE_LIB_DEFECTS
2322  // 5 String::compare specification questionable
2323  /**
2324  * @brief Compare substring to a C string.
2325  * @param __pos Index of first character of substring.
2326  * @param __n1 Number of characters in substring.
2327  * @param __s C string to compare against.
2328  * @return Integer < 0, 0, or > 0.
2329  *
2330  * Form the substring of this string from the @a __n1
2331  * characters starting at @a pos. Returns an integer < 0 if
2332  * the substring is ordered before @a __s, 0 if their values
2333  * are equivalent, or > 0 if the substring is ordered after @a
2334  * __s. Determines the effective length rlen of the strings to
2335  * compare as the smallest of the length of the substring and
2336  * the length of a string constructed from @a __s. The
2337  * function then compares the two string by calling
2338  * traits::compare(substring.data(),__s,rlen). If the result of
2339  * the comparison is nonzero returns it, otherwise the shorter
2340  * one is ordered first.
2341  */
2342  int
2343  compare(size_type __pos, size_type __n1, const _CharT* __s) const;
2344 
2345  /**
2346  * @brief Compare substring against a character %array.
2347  * @param __pos Index of first character of substring.
2348  * @param __n1 Number of characters in substring.
2349  * @param __s character %array to compare against.
2350  * @param __n2 Number of characters of s.
2351  * @return Integer < 0, 0, or > 0.
2352  *
2353  * Form the substring of this string from the @a __n1
2354  * characters starting at @a __pos. Form a string from the
2355  * first @a __n2 characters of @a __s. Returns an integer < 0
2356  * if this substring is ordered before the string from @a __s,
2357  * 0 if their values are equivalent, or > 0 if this substring
2358  * is ordered after the string from @a __s. Determines the
2359  * effective length rlen of the strings to compare as the
2360  * smallest of the length of the substring and @a __n2. The
2361  * function then compares the two strings by calling
2362  * traits::compare(substring.data(),s,rlen). If the result of
2363  * the comparison is nonzero returns it, otherwise the shorter
2364  * one is ordered first.
2365  *
2366  * NB: s must have at least n2 characters, &apos;\\0&apos; has
2367  * no special meaning.
2368  */
2369  int
2370  compare(size_type __pos, size_type __n1, const _CharT* __s,
2371  size_type __n2) const;
2372  };
2373 
2374  // operator+
2375  /**
2376  * @brief Concatenate two strings.
2377  * @param __lhs First string.
2378  * @param __rhs Last string.
2379  * @return New string with value of @a __lhs followed by @a __rhs.
2380  */
2381  template<typename _CharT, typename _Traits, typename _Alloc>
2385  {
2387  __str.append(__rhs);
2388  return __str;
2389  }
2390 
2391  /**
2392  * @brief Concatenate C string and string.
2393  * @param __lhs First string.
2394  * @param __rhs Last string.
2395  * @return New string with value of @a __lhs followed by @a __rhs.
2396  */
2397  template<typename _CharT, typename _Traits, typename _Alloc>
2399  operator+(const _CharT* __lhs,
2401 
2402  /**
2403  * @brief Concatenate character and string.
2404  * @param __lhs First string.
2405  * @param __rhs Last string.
2406  * @return New string with @a __lhs followed by @a __rhs.
2407  */
2408  template<typename _CharT, typename _Traits, typename _Alloc>
2410  operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
2411 
2412  /**
2413  * @brief Concatenate string and C string.
2414  * @param __lhs First string.
2415  * @param __rhs Last string.
2416  * @return New string with @a __lhs followed by @a __rhs.
2417  */
2418  template<typename _CharT, typename _Traits, typename _Alloc>
2421  const _CharT* __rhs)
2422  {
2424  __str.append(__rhs);
2425  return __str;
2426  }
2427 
2428  /**
2429  * @brief Concatenate string and character.
2430  * @param __lhs First string.
2431  * @param __rhs Last string.
2432  * @return New string with @a __lhs followed by @a __rhs.
2433  */
2434  template<typename _CharT, typename _Traits, typename _Alloc>
2437  {
2438  typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
2439  typedef typename __string_type::size_type __size_type;
2440  __string_type __str(__lhs);
2441  __str.append(__size_type(1), __rhs);
2442  return __str;
2443  }
2444 
2445 #if __cplusplus >= 201103L
2446  template<typename _CharT, typename _Traits, typename _Alloc>
2450  { return std::move(__lhs.append(__rhs)); }
2451 
2452  template<typename _CharT, typename _Traits, typename _Alloc>
2456  { return std::move(__rhs.insert(0, __lhs)); }
2457 
2458  template<typename _CharT, typename _Traits, typename _Alloc>
2462  {
2463  const auto __size = __lhs.size() + __rhs.size();
2464  const bool __cond = (__size > __lhs.capacity()
2465  && __size <= __rhs.capacity());
2466  return __cond ? std::move(__rhs.insert(0, __lhs))
2467  : std::move(__lhs.append(__rhs));
2468  }
2469 
2470  template<typename _CharT, typename _Traits, typename _Alloc>
2472  operator+(const _CharT* __lhs,
2474  { return std::move(__rhs.insert(0, __lhs)); }
2475 
2476  template<typename _CharT, typename _Traits, typename _Alloc>
2478  operator+(_CharT __lhs,
2480  { return std::move(__rhs.insert(0, 1, __lhs)); }
2481 
2482  template<typename _CharT, typename _Traits, typename _Alloc>
2485  const _CharT* __rhs)
2486  { return std::move(__lhs.append(__rhs)); }
2487 
2488  template<typename _CharT, typename _Traits, typename _Alloc>
2491  _CharT __rhs)
2492  { return std::move(__lhs.append(1, __rhs)); }
2493 #endif
2494 
2495  // operator ==
2496  /**
2497  * @brief Test equivalence of two strings.
2498  * @param __lhs First string.
2499  * @param __rhs Second string.
2500  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
2501  */
2502  template<typename _CharT, typename _Traits, typename _Alloc>
2503  inline bool
2504  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2506  { return __lhs.compare(__rhs) == 0; }
2507 
2508  template<typename _CharT>
2509  inline
2510  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
2511  operator==(const basic_string<_CharT>& __lhs,
2512  const basic_string<_CharT>& __rhs)
2513  { return (__lhs.size() == __rhs.size()
2514  && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
2515  __lhs.size())); }
2516 
2517  /**
2518  * @brief Test equivalence of C string and string.
2519  * @param __lhs C string.
2520  * @param __rhs String.
2521  * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
2522  */
2523  template<typename _CharT, typename _Traits, typename _Alloc>
2524  inline bool
2525  operator==(const _CharT* __lhs,
2527  { return __rhs.compare(__lhs) == 0; }
2528 
2529  /**
2530  * @brief Test equivalence of string and C string.
2531  * @param __lhs String.
2532  * @param __rhs C string.
2533  * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
2534  */
2535  template<typename _CharT, typename _Traits, typename _Alloc>
2536  inline bool
2537  operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2538  const _CharT* __rhs)
2539  { return __lhs.compare(__rhs) == 0; }
2540 
2541  // operator !=
2542  /**
2543  * @brief Test difference of two strings.
2544  * @param __lhs First string.
2545  * @param __rhs Second string.
2546  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
2547  */
2548  template<typename _CharT, typename _Traits, typename _Alloc>
2549  inline bool
2550  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2552  { return !(__lhs == __rhs); }
2553 
2554  /**
2555  * @brief Test difference of C string and string.
2556  * @param __lhs C string.
2557  * @param __rhs String.
2558  * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
2559  */
2560  template<typename _CharT, typename _Traits, typename _Alloc>
2561  inline bool
2562  operator!=(const _CharT* __lhs,
2564  { return !(__lhs == __rhs); }
2565 
2566  /**
2567  * @brief Test difference of string and C string.
2568  * @param __lhs String.
2569  * @param __rhs C string.
2570  * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
2571  */
2572  template<typename _CharT, typename _Traits, typename _Alloc>
2573  inline bool
2574  operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2575  const _CharT* __rhs)
2576  { return !(__lhs == __rhs); }
2577 
2578  // operator <
2579  /**
2580  * @brief Test if string precedes string.
2581  * @param __lhs First string.
2582  * @param __rhs Second string.
2583  * @return True if @a __lhs precedes @a __rhs. False otherwise.
2584  */
2585  template<typename _CharT, typename _Traits, typename _Alloc>
2586  inline bool
2587  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2589  { return __lhs.compare(__rhs) < 0; }
2590 
2591  /**
2592  * @brief Test if string precedes C string.
2593  * @param __lhs String.
2594  * @param __rhs C string.
2595  * @return True if @a __lhs precedes @a __rhs. False otherwise.
2596  */
2597  template<typename _CharT, typename _Traits, typename _Alloc>
2598  inline bool
2599  operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2600  const _CharT* __rhs)
2601  { return __lhs.compare(__rhs) < 0; }
2602 
2603  /**
2604  * @brief Test if C string precedes string.
2605  * @param __lhs C string.
2606  * @param __rhs String.
2607  * @return True if @a __lhs precedes @a __rhs. False otherwise.
2608  */
2609  template<typename _CharT, typename _Traits, typename _Alloc>
2610  inline bool
2611  operator<(const _CharT* __lhs,
2613  { return __rhs.compare(__lhs) > 0; }
2614 
2615  // operator >
2616  /**
2617  * @brief Test if string follows string.
2618  * @param __lhs First string.
2619  * @param __rhs Second string.
2620  * @return True if @a __lhs follows @a __rhs. False otherwise.
2621  */
2622  template<typename _CharT, typename _Traits, typename _Alloc>
2623  inline bool
2626  { return __lhs.compare(__rhs) > 0; }
2627 
2628  /**
2629  * @brief Test if string follows C string.
2630  * @param __lhs String.
2631  * @param __rhs C string.
2632  * @return True if @a __lhs follows @a __rhs. False otherwise.
2633  */
2634  template<typename _CharT, typename _Traits, typename _Alloc>
2635  inline bool
2637  const _CharT* __rhs)
2638  { return __lhs.compare(__rhs) > 0; }
2639 
2640  /**
2641  * @brief Test if C string follows string.
2642  * @param __lhs C string.
2643  * @param __rhs String.
2644  * @return True if @a __lhs follows @a __rhs. False otherwise.
2645  */
2646  template<typename _CharT, typename _Traits, typename _Alloc>
2647  inline bool
2648  operator>(const _CharT* __lhs,
2650  { return __rhs.compare(__lhs) < 0; }
2651 
2652  // operator <=
2653  /**
2654  * @brief Test if string doesn't follow string.
2655  * @param __lhs First string.
2656  * @param __rhs Second string.
2657  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
2658  */
2659  template<typename _CharT, typename _Traits, typename _Alloc>
2660  inline bool
2661  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2663  { return __lhs.compare(__rhs) <= 0; }
2664 
2665  /**
2666  * @brief Test if string doesn't follow C string.
2667  * @param __lhs String.
2668  * @param __rhs C string.
2669  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
2670  */
2671  template<typename _CharT, typename _Traits, typename _Alloc>
2672  inline bool
2673  operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
2674  const _CharT* __rhs)
2675  { return __lhs.compare(__rhs) <= 0; }
2676 
2677  /**
2678  * @brief Test if C string doesn't follow string.
2679  * @param __lhs C string.
2680  * @param __rhs String.
2681  * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
2682  */
2683  template<typename _CharT, typename _Traits, typename _Alloc>
2684  inline bool
2685  operator<=(const _CharT* __lhs,
2687  { return __rhs.compare(__lhs) >= 0; }
2688 
2689  // operator >=
2690  /**
2691  * @brief Test if string doesn't precede string.
2692  * @param __lhs First string.
2693  * @param __rhs Second string.
2694  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
2695  */
2696  template<typename _CharT, typename _Traits, typename _Alloc>
2697  inline bool
2700  { return __lhs.compare(__rhs) >= 0; }
2701 
2702  /**
2703  * @brief Test if string doesn't precede C string.
2704  * @param __lhs String.
2705  * @param __rhs C string.
2706  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
2707  */
2708  template<typename _CharT, typename _Traits, typename _Alloc>
2709  inline bool
2711  const _CharT* __rhs)
2712  { return __lhs.compare(__rhs) >= 0; }
2713 
2714  /**
2715  * @brief Test if C string doesn't precede string.
2716  * @param __lhs C string.
2717  * @param __rhs String.
2718  * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
2719  */
2720  template<typename _CharT, typename _Traits, typename _Alloc>
2721  inline bool
2722  operator>=(const _CharT* __lhs,
2724  { return __rhs.compare(__lhs) <= 0; }
2725 
2726  /**
2727  * @brief Swap contents of two strings.
2728  * @param __lhs First string.
2729  * @param __rhs Second string.
2730  *
2731  * Exchanges the contents of @a __lhs and @a __rhs in constant time.
2732  */
2733  template<typename _CharT, typename _Traits, typename _Alloc>
2734  inline void
2737  { __lhs.swap(__rhs); }
2738 
2739  /**
2740  * @brief Read stream into a string.
2741  * @param __is Input stream.
2742  * @param __str Buffer to store into.
2743  * @return Reference to the input stream.
2744  *
2745  * Stores characters from @a __is into @a __str until whitespace is
2746  * found, the end of the stream is encountered, or str.max_size()
2747  * is reached. If is.width() is non-zero, that is the limit on the
2748  * number of characters stored into @a __str. Any previous
2749  * contents of @a __str are erased.
2750  */
2751  template<typename _CharT, typename _Traits, typename _Alloc>
2752  basic_istream<_CharT, _Traits>&
2753  operator>>(basic_istream<_CharT, _Traits>& __is,
2755 
2756  template<>
2757  basic_istream<char>&
2758  operator>>(basic_istream<char>& __is, basic_string<char>& __str);
2759 
2760  /**
2761  * @brief Write string to a stream.
2762  * @param __os Output stream.
2763  * @param __str String to write out.
2764  * @return Reference to the output stream.
2765  *
2766  * Output characters of @a __str into os following the same rules as for
2767  * writing a C string.
2768  */
2769  template<typename _CharT, typename _Traits, typename _Alloc>
2770  inline basic_ostream<_CharT, _Traits>&
2771  operator<<(basic_ostream<_CharT, _Traits>& __os,
2773  {
2774  // _GLIBCXX_RESOLVE_LIB_DEFECTS
2775  // 586. string inserter not a formatted function
2776  return __ostream_insert(__os, __str.data(), __str.size());
2777  }
2778 
2779  /**
2780  * @brief Read a line from stream into a string.
2781  * @param __is Input stream.
2782  * @param __str Buffer to store into.
2783  * @param __delim Character marking end of line.
2784  * @return Reference to the input stream.
2785  *
2786  * Stores characters from @a __is into @a __str until @a __delim is
2787  * found, the end of the stream is encountered, or str.max_size()
2788  * is reached. Any previous contents of @a __str are erased. If
2789  * @a __delim is encountered, it is extracted but not stored into
2790  * @a __str.
2791  */
2792  template<typename _CharT, typename _Traits, typename _Alloc>
2793  basic_istream<_CharT, _Traits>&
2794  getline(basic_istream<_CharT, _Traits>& __is,
2795  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
2796 
2797  /**
2798  * @brief Read a line from stream into a string.
2799  * @param __is Input stream.
2800  * @param __str Buffer to store into.
2801  * @return Reference to the input stream.
2802  *
2803  * Stores characters from is into @a __str until &apos;\n&apos; is
2804  * found, the end of the stream is encountered, or str.max_size()
2805  * is reached. Any previous contents of @a __str are erased. If
2806  * end of line is encountered, it is extracted but not stored into
2807  * @a __str.
2808  */
2809  template<typename _CharT, typename _Traits, typename _Alloc>
2810  inline basic_istream<_CharT, _Traits>&
2811  getline(basic_istream<_CharT, _Traits>& __is,
2813  { return std::getline(__is, __str, __is.widen('\n')); }
2814 
2815 #if __cplusplus >= 201103L
2816  /// Read a line from an rvalue stream into a string.
2817  template<typename _CharT, typename _Traits, typename _Alloc>
2818  inline basic_istream<_CharT, _Traits>&
2819  getline(basic_istream<_CharT, _Traits>&& __is,
2820  basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
2821  { return std::getline(__is, __str, __delim); }
2822 
2823  /// Read a line from an rvalue stream into a string.
2824  template<typename _CharT, typename _Traits, typename _Alloc>
2825  inline basic_istream<_CharT, _Traits>&
2826  getline(basic_istream<_CharT, _Traits>&& __is,
2828  { return std::getline(__is, __str); }
2829 #endif
2830 
2831  template<>
2832  basic_istream<char>&
2833  getline(basic_istream<char>& __in, basic_string<char>& __str,
2834  char __delim);
2835 
2836 #ifdef _GLIBCXX_USE_WCHAR_T
2837  template<>
2838  basic_istream<wchar_t>&
2839  getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
2840  wchar_t __delim);
2841 #endif
2842 
2843 _GLIBCXX_END_NAMESPACE_VERSION
2844 } // namespace
2845 
2846 #if __cplusplus >= 201103L && defined(_GLIBCXX_USE_C99)
2847 
2848 #include <ext/string_conversions.h>
2849 
2850 namespace std _GLIBCXX_VISIBILITY(default)
2851 {
2852 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2853 
2854  // 21.4 Numeric Conversions [string.conversions].
2855  inline int
2856  stoi(const string& __str, size_t* __idx = 0, int __base = 10)
2857  { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
2858  __idx, __base); }
2859 
2860  inline long
2861  stol(const string& __str, size_t* __idx = 0, int __base = 10)
2862  { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
2863  __idx, __base); }
2864 
2865  inline unsigned long
2866  stoul(const string& __str, size_t* __idx = 0, int __base = 10)
2867  { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
2868  __idx, __base); }
2869 
2870  inline long long
2871  stoll(const string& __str, size_t* __idx = 0, int __base = 10)
2872  { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
2873  __idx, __base); }
2874 
2875  inline unsigned long long
2876  stoull(const string& __str, size_t* __idx = 0, int __base = 10)
2877  { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
2878  __idx, __base); }
2879 
2880  // NB: strtof vs strtod.
2881  inline float
2882  stof(const string& __str, size_t* __idx = 0)
2883  { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
2884 
2885  inline double
2886  stod(const string& __str, size_t* __idx = 0)
2887  { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
2888 
2889  inline long double
2890  stold(const string& __str, size_t* __idx = 0)
2891  { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
2892 
2893  // NB: (v)snprintf vs sprintf.
2894 
2895  // DR 1261.
2896  inline string
2897  to_string(int __val)
2898  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
2899  "%d", __val); }
2900 
2901  inline string
2902  to_string(unsigned __val)
2903  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2904  4 * sizeof(unsigned),
2905  "%u", __val); }
2906 
2907  inline string
2908  to_string(long __val)
2909  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
2910  "%ld", __val); }
2911 
2912  inline string
2913  to_string(unsigned long __val)
2914  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2915  4 * sizeof(unsigned long),
2916  "%lu", __val); }
2917 
2918  inline string
2919  to_string(long long __val)
2920  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2921  4 * sizeof(long long),
2922  "%lld", __val); }
2923 
2924  inline string
2925  to_string(unsigned long long __val)
2926  { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
2927  4 * sizeof(unsigned long long),
2928  "%llu", __val); }
2929 
2930  inline string
2931  to_string(float __val)
2932  {
2933  const int __n =
2934  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
2935  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2936  "%f", __val);
2937  }
2938 
2939  inline string
2940  to_string(double __val)
2941  {
2942  const int __n =
2943  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
2944  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2945  "%f", __val);
2946  }
2947 
2948  inline string
2949  to_string(long double __val)
2950  {
2951  const int __n =
2952  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
2953  return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
2954  "%Lf", __val);
2955  }
2956 
2957 #ifdef _GLIBCXX_USE_WCHAR_T
2958  inline int
2959  stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
2960  { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
2961  __idx, __base); }
2962 
2963  inline long
2964  stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
2965  { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
2966  __idx, __base); }
2967 
2968  inline unsigned long
2969  stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
2970  { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
2971  __idx, __base); }
2972 
2973  inline long long
2974  stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
2975  { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
2976  __idx, __base); }
2977 
2978  inline unsigned long long
2979  stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
2980  { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
2981  __idx, __base); }
2982 
2983  // NB: wcstof vs wcstod.
2984  inline float
2985  stof(const wstring& __str, size_t* __idx = 0)
2986  { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
2987 
2988  inline double
2989  stod(const wstring& __str, size_t* __idx = 0)
2990  { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
2991 
2992  inline long double
2993  stold(const wstring& __str, size_t* __idx = 0)
2994  { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
2995 
2996 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
2997  // DR 1261.
2998  inline wstring
2999  to_wstring(int __val)
3000  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
3001  L"%d", __val); }
3002 
3003  inline wstring
3004  to_wstring(unsigned __val)
3005  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
3006  4 * sizeof(unsigned),
3007  L"%u", __val); }
3008 
3009  inline wstring
3010  to_wstring(long __val)
3011  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
3012  L"%ld", __val); }
3013 
3014  inline wstring
3015  to_wstring(unsigned long __val)
3016  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
3017  4 * sizeof(unsigned long),
3018  L"%lu", __val); }
3019 
3020  inline wstring
3021  to_wstring(long long __val)
3022  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
3023  4 * sizeof(long long),
3024  L"%lld", __val); }
3025 
3026  inline wstring
3027  to_wstring(unsigned long long __val)
3028  { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
3029  4 * sizeof(unsigned long long),
3030  L"%llu", __val); }
3031 
3032  inline wstring
3033  to_wstring(float __val)
3034  {
3035  const int __n =
3036  __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
3037  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
3038  L"%f", __val);
3039  }
3040 
3041  inline wstring
3042  to_wstring(double __val)
3043  {
3044  const int __n =
3045  __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
3046  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
3047  L"%f", __val);
3048  }
3049 
3050  inline wstring
3051  to_wstring(long double __val)
3052  {
3053  const int __n =
3054  __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
3055  return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
3056  L"%Lf", __val);
3057  }
3058 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
3059 #endif
3060 
3061 _GLIBCXX_END_NAMESPACE_VERSION
3062 } // namespace
3063 
3064 #endif /* C++11 && _GLIBCXX_USE_C99 ... */
3065 
3066 #if __cplusplus >= 201103L
3067 
3068 #include <bits/functional_hash.h>
3069 
3070 namespace std _GLIBCXX_VISIBILITY(default)
3071 {
3072 _GLIBCXX_BEGIN_NAMESPACE_VERSION
3073 
3074  // DR 1182.
3075 
3076 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
3077  /// std::hash specialization for string.
3078  template<>
3079  struct hash<string>
3080  : public __hash_base<size_t, string>
3081  {
3082  size_t
3083  operator()(const string& __s) const noexcept
3084  { return std::_Hash_impl::hash(__s.data(), __s.length()); }
3085  };
3086 
3087  template<>
3088  struct __is_fast_hash<hash<string>> : std::false_type
3089  { };
3090 
3091 #ifdef _GLIBCXX_USE_WCHAR_T
3092  /// std::hash specialization for wstring.
3093  template<>
3094  struct hash<wstring>
3095  : public __hash_base<size_t, wstring>
3096  {
3097  size_t
3098  operator()(const wstring& __s) const noexcept
3099  { return std::_Hash_impl::hash(__s.data(),
3100  __s.length() * sizeof(wchar_t)); }
3101  };
3102 
3103  template<>
3104  struct __is_fast_hash<hash<wstring>> : std::false_type
3105  { };
3106 #endif
3107 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
3108 
3109 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3110  /// std::hash specialization for u16string.
3111  template<>
3112  struct hash<u16string>
3113  : public __hash_base<size_t, u16string>
3114  {
3115  size_t
3116  operator()(const u16string& __s) const noexcept
3117  { return std::_Hash_impl::hash(__s.data(),
3118  __s.length() * sizeof(char16_t)); }
3119  };
3120 
3121  template<>
3122  struct __is_fast_hash<hash<u16string>> : std::false_type
3123  { };
3124 
3125  /// std::hash specialization for u32string.
3126  template<>
3127  struct hash<u32string>
3128  : public __hash_base<size_t, u32string>
3129  {
3130  size_t
3131  operator()(const u32string& __s) const noexcept
3132  { return std::_Hash_impl::hash(__s.data(),
3133  __s.length() * sizeof(char32_t)); }
3134  };
3135 
3136  template<>
3137  struct __is_fast_hash<hash<u32string>> : std::false_type
3138  { };
3139 #endif
3140 
3141 #if __cplusplus > 201103L
3142 
3143 #define __cpp_lib_string_udls 201304
3144 
3145  inline namespace literals
3146  {
3147  inline namespace string_literals
3148  {
3149 
3150  inline basic_string<char>
3151  operator""s(const char* __str, size_t __len)
3152  { return basic_string<char>{__str, __len}; }
3153 
3154 #ifdef _GLIBCXX_USE_WCHAR_T
3155  inline basic_string<wchar_t>
3156  operator""s(const wchar_t* __str, size_t __len)
3157  { return basic_string<wchar_t>{__str, __len}; }
3158 #endif
3159 
3160 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
3161  inline basic_string<char16_t>
3162  operator""s(const char16_t* __str, size_t __len)
3163  { return basic_string<char16_t>{__str, __len}; }
3164 
3165  inline basic_string<char32_t>
3166  operator""s(const char32_t* __str, size_t __len)
3167  { return basic_string<char32_t>{__str, __len}; }
3168 #endif
3169 
3170  } // inline namespace string_literals
3171  } // inline namespace literals
3172 
3173 #endif // __cplusplus > 201103L
3174 
3175 _GLIBCXX_END_NAMESPACE_VERSION
3176 } // namespace std
3177 
3178 #endif // C++11
3179 
3180 #endif /* _BASIC_STRING_H */
iterator begin()
Definition: basic_string.h:613
Marking input iterators.
basic_string & replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
Replace range of characters with multiple characters.
size_type find_first_not_of(const _CharT *__s, size_type __pos=0) const
Find position of a character not in C string.
basic_string & operator=(_CharT __c)
Set value to string of length 1.
Definition: basic_string.h:572
basic_string & operator=(const _CharT *__s)
Copy contents of s into this string.
Definition: basic_string.h:561
size_type find_last_not_of(const _CharT *__s, size_type __pos=npos) const
Find last position of a character not in C string.
basic_string & append(const _CharT *__s)
Append a C string.
const_reverse_iterator rbegin() const noexcept
Definition: basic_string.h:661
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
basic_string & operator+=(const _CharT *__s)
Append a C string.
Definition: basic_string.h:957
const_iterator cbegin() const noexcept
Definition: basic_string.h:688
const _CharT * c_str() const noexcept
Return const pointer to null-terminated contents.
basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
void insert(iterator __p, initializer_list< _CharT > __l)
Insert an initializer_list of characters.
basic_string & assign(const _CharT *__s)
Set value to contents of a C string.
basic_string(basic_string &&__str) noexcept
Move construct string.
Definition: basic_string.h:511
void insert(iterator __p, _InputIterator __beg, _InputIterator __end)
Insert a range of characters.
int compare(const basic_string &__str) const
Compare to a string.
void reserve(size_type __res_arg=0)
Attempt to preallocate enough memory for specified number of characters.
iterator erase(iterator __position)
Remove one character.
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition: basic_string.h:723
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.
Definition: basic_string.h:545
basic_string & replace(iterator __i1, iterator __i2, const _CharT *__s, size_type __n)
Replace range of characters with C substring.
basic_string & assign(const basic_string &__str)
Set value to contents of another string.
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
size_type max_size() const noexcept
Returns the size() of the largest possible string.
Definition: basic_string.h:734
basic_string & assign(initializer_list< _CharT > __l)
Set value to an initializer_list of characters.
size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
bool operator<=(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string doesn&#39;t follow string.
void pop_back()
Remove the last character.
basic_string & replace(iterator __i1, iterator __i2, initializer_list< _CharT > __l)
Replace range of characters with initializer_list.
Primary class template hash.
basic_string & operator=(const basic_string &__str)
Assign the value of str to this string.
Definition: basic_string.h:553
const_reverse_iterator crbegin() const noexcept
Definition: basic_string.h:705
const_reverse_iterator crend() const noexcept
Definition: basic_string.h:714
basic_string & replace(iterator __i1, iterator __i2, const basic_string &__str)
Replace range of characters with string.
basic_string & append(const basic_string &__str)
Append a string to this string.
const_reference at(size_type __n) const
Provides access to the data contained in the string.
Definition: basic_string.h:874
basic_string()
Default constructor creates an empty string.
Definition: basic_string.h:441
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
const_reverse_iterator rend() const noexcept
Definition: basic_string.h:679
_Siter_base< _Iterator >::iterator_type __base(_Iterator __it)
Definition: functions.h:558
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:101
basic_string & operator+=(_CharT __c)
Append a character.
Definition: basic_string.h:966
void resize(size_type __n)
Resizes the string to the specified number of characters.
Definition: basic_string.h:761
const_reference back() const noexcept
Definition: basic_string.h:937
basic_istream< _CharT, _Traits > & getline(basic_istream< _CharT, _Traits > &__is, basic_string< _CharT, _Traits, _Alloc > &__str, _CharT __delim)
Read a line from stream into a string.
size_type find(const _CharT *__s, size_type __pos, size_type __n) const
Find position of a C substring.
basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:194
ISO C++ entities toplevel namespace is std.
reference operator[](size_type __pos)
Subscript access to the data contained in the string.
Definition: basic_string.h:852
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 __n1, const _CharT *__s)
Replace characters with value of a C string.
void swap(basic_string &__s)
Swap contents with another string.
bool operator>=(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string doesn&#39;t precede string.
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
size_type find(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a string.
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
Definition: basic_string.h:643
const_reference front() const noexcept
Definition: basic_string.h:921
Managing sequences of characters and character-like objects.
Definition: basic_string.h:112
basic_string & append(_InputIterator __first, _InputIterator __last)
Append a range of characters.
basic_string & insert(size_type __pos, const _CharT *__s)
Insert a C string.
basic_string & assign(basic_string &&__str)
Set value to contents of another string.
bool empty() const noexcept
Definition: basic_string.h:820
size_type find_first_of(const _CharT *__s, size_type __pos=0) const
Find position of a character of C string.
reverse_iterator rend()
Definition: basic_string.h:670
basic_string & operator+=(initializer_list< _CharT > __l)
Append an initializer_list of characters.
Definition: basic_string.h:979
basic_string & operator=(basic_string &&__str)
Move assign the value of str to this string.
Definition: basic_string.h:588
One of the comparison functors.
Definition: stl_function.h:367
Forward iterators support a superset of input iterator operations.
reference back()
Definition: basic_string.h:929
basic_string & replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
Replace characters with multiple characters.
iterator insert(iterator __p, _CharT __c)
Insert one character.
size_type find_last_of(const _CharT *__s, size_type __pos=npos) const
Find last position of a character of C string.
void shrink_to_fit() noexcept
A non-binding request to reduce capacity() to size().
Definition: basic_string.h:767
size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
bool operator>(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string follows string.
size_type rfind(const _CharT *__s, size_type __pos=npos) const
Find last position of a C string.
basic_string & insert(size_type __pos, size_type __n, _CharT __c)
Insert multiple characters.
size_type find_last_not_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character not in string.
const _CharT * data() const noexcept
Return const pointer to contents.
Basis for explicit traits specializations.
Definition: char_traits.h:227
const_iterator begin() const noexcept
Definition: basic_string.h:624
bool operator<(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Test if string precedes string.
reference at(size_type __n)
Provides access to the data contained in the string.
Definition: basic_string.h:896
static const size_type npos
Value returned by various member functions when they fail.
Definition: basic_string.h:285
const_reference operator[](size_type __pos) const noexcept
Subscript access to the data contained in the string.
Definition: basic_string.h:835
const_iterator cend() const noexcept
Definition: basic_string.h:696
size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
size_type capacity() const noexcept
Definition: basic_string.h:784
basic_string & insert(size_type __pos1, const basic_string &__str)
Insert value of a string.
basic_string & operator+=(const basic_string &__str)
Append a string to this string.
Definition: basic_string.h:948
size_type find(const _CharT *__s, size_type __pos=0) const
Find position of a C string.
basic_string & replace(iterator __i1, iterator __i2, _InputIterator __k1, _InputIterator __k2)
Replace range of characters with range.
basic_string< _CharT, _Traits, _Alloc > operator+(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const basic_string< _CharT, _Traits, _Alloc > &__rhs)
Concatenate two strings.
size_type find_last_of(_CharT __c, size_type __pos=npos) const noexcept
Find last position of a character.
basic_istream< _CharT, _Traits > & operator>>(basic_istream< _CharT, _Traits > &__is, basic_string< _CharT, _Traits, _Alloc > &__str)
Read stream into a string.
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
void push_back(_CharT __c)
Append a single character.
basic_string & assign(_InputIterator __first, _InputIterator __last)
Set value to a range of characters.
size_type length() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition: basic_string.h:729
basic_string & append(initializer_list< _CharT > __l)
Append an initializer_list of characters.
basic_string & replace(iterator __i1, iterator __i2, const _CharT *__s)
Replace range of characters with C string.
basic_string & assign(size_type __n, _CharT __c)
Set value to multiple characters.
basic_string & operator=(initializer_list< _CharT > __l)
Set value to string constructed from initializer list.
Definition: basic_string.h:600
reverse_iterator rbegin()
Definition: basic_string.h:652
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 & insert(size_type __pos1, const basic_string &__str, size_type __pos2, size_type __n)
Insert a substring.
reference front()
Definition: basic_string.h:913