OSDN Git Service

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