OSDN Git Service

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