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