libstdc++
basic_string.h
Go to the documentation of this file.
00001 // Components for manipulating sequences of characters -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
00004 // 2006, 2007, 2008, 2009, 2010, 2011
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /** @file bits/basic_string.h
00028  *  This is an internal header file, included by other library headers.
00029  *  Do not attempt to use it directly. @headername{string}
00030  */
00031 
00032 //
00033 // ISO C++ 14882: 21 Strings library
00034 //
00035 
00036 #ifndef _BASIC_STRING_H
00037 #define _BASIC_STRING_H 1
00038 
00039 #pragma GCC system_header
00040 
00041 #include <ext/atomicity.h>
00042 #include <debug/debug.h>
00043 #include <initializer_list>
00044 
00045 namespace std _GLIBCXX_VISIBILITY(default)
00046 {
00047 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00048 
00049   /**
00050    *  @class basic_string basic_string.h <string>
00051    *  @brief  Managing sequences of characters and character-like objects.
00052    *
00053    *  @ingroup strings
00054    *  @ingroup sequences
00055    *
00056    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
00057    *  <a href="tables.html#66">reversible container</a>, and a
00058    *  <a href="tables.html#67">sequence</a>.  Of the
00059    *  <a href="tables.html#68">optional sequence requirements</a>, only
00060    *  @c push_back, @c at, and @c %array access are supported.
00061    *
00062    *  @doctodo
00063    *
00064    *
00065    *  Documentation?  What's that?
00066    *  Nathan Myers <ncm@cantrip.org>.
00067    *
00068    *  A string looks like this:
00069    *
00070    *  @code
00071    *                                        [_Rep]
00072    *                                        _M_length
00073    *   [basic_string<char_type>]            _M_capacity
00074    *   _M_dataplus                          _M_refcount
00075    *   _M_p ---------------->               unnamed array of char_type
00076    *  @endcode
00077    *
00078    *  Where the _M_p points to the first character in the string, and
00079    *  you cast it to a pointer-to-_Rep and subtract 1 to get a
00080    *  pointer to the header.
00081    *
00082    *  This approach has the enormous advantage that a string object
00083    *  requires only one allocation.  All the ugliness is confined
00084    *  within a single %pair of inline functions, which each compile to
00085    *  a single @a add instruction: _Rep::_M_data(), and
00086    *  string::_M_rep(); and the allocation function which gets a
00087    *  block of raw bytes and with room enough and constructs a _Rep
00088    *  object at the front.
00089    *
00090    *  The reason you want _M_data pointing to the character %array and
00091    *  not the _Rep is so that the debugger can see the string
00092    *  contents. (Probably we should add a non-inline member to get
00093    *  the _Rep for the debugger to use, so users can check the actual
00094    *  string length.)
00095    *
00096    *  Note that the _Rep object is a POD so that you can have a
00097    *  static <em>empty string</em> _Rep object already @a constructed before
00098    *  static constructors have run.  The reference-count encoding is
00099    *  chosen so that a 0 indicates one reference, so you never try to
00100    *  destroy the empty-string _Rep object.
00101    *
00102    *  All but the last paragraph is considered pretty conventional
00103    *  for a C++ string implementation.
00104   */
00105   // 21.3  Template class basic_string
00106   template<typename _CharT, typename _Traits, typename _Alloc>
00107     class basic_string
00108     {
00109       typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
00110 
00111       // Types:
00112     public:
00113       typedef _Traits                       traits_type;
00114       typedef typename _Traits::char_type           value_type;
00115       typedef _Alloc                        allocator_type;
00116       typedef typename _CharT_alloc_type::size_type     size_type;
00117       typedef typename _CharT_alloc_type::difference_type   difference_type;
00118       typedef typename _CharT_alloc_type::reference     reference;
00119       typedef typename _CharT_alloc_type::const_reference   const_reference;
00120       typedef typename _CharT_alloc_type::pointer       pointer;
00121       typedef typename _CharT_alloc_type::const_pointer     const_pointer;
00122       typedef __gnu_cxx::__normal_iterator<pointer, basic_string>  iterator;
00123       typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
00124                                                             const_iterator;
00125       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00126       typedef std::reverse_iterator<iterator>           reverse_iterator;
00127 
00128     private:
00129       // _Rep: string representation
00130       //   Invariants:
00131       //   1. String really contains _M_length + 1 characters: due to 21.3.4
00132       //      must be kept null-terminated.
00133       //   2. _M_capacity >= _M_length
00134       //      Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
00135       //   3. _M_refcount has three states:
00136       //      -1: leaked, one reference, no ref-copies allowed, non-const.
00137       //       0: one reference, non-const.
00138       //     n>0: n + 1 references, operations require a lock, const.
00139       //   4. All fields==0 is an empty string, given the extra storage
00140       //      beyond-the-end for a null terminator; thus, the shared
00141       //      empty string representation needs no constructor.
00142 
00143       struct _Rep_base
00144       {
00145     size_type       _M_length;
00146     size_type       _M_capacity;
00147     _Atomic_word        _M_refcount;
00148       };
00149 
00150       struct _Rep : _Rep_base
00151       {
00152     // Types:
00153     typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
00154 
00155     // (Public) Data members:
00156 
00157     // The maximum number of individual char_type elements of an
00158     // individual string is determined by _S_max_size. This is the
00159     // value that will be returned by max_size().  (Whereas npos
00160     // is the maximum number of bytes the allocator can allocate.)
00161     // If one was to divvy up the theoretical largest size string,
00162     // with a terminating character and m _CharT elements, it'd
00163     // look like this:
00164     // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
00165     // Solving for m:
00166     // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
00167     // In addition, this implementation quarters this amount.
00168     static const size_type  _S_max_size;
00169     static const _CharT _S_terminal;
00170 
00171     // The following storage is init'd to 0 by the linker, resulting
00172         // (carefully) in an empty string with one reference.
00173         static size_type _S_empty_rep_storage[];
00174 
00175         static _Rep&
00176         _S_empty_rep()
00177         { 
00178       // NB: Mild hack to avoid strict-aliasing warnings.  Note that
00179       // _S_empty_rep_storage is never modified and the punning should
00180       // be reasonably safe in this case.
00181       void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
00182       return *reinterpret_cast<_Rep*>(__p);
00183     }
00184 
00185         bool
00186     _M_is_leaked() const
00187         { return this->_M_refcount < 0; }
00188 
00189         bool
00190     _M_is_shared() const
00191         { return this->_M_refcount > 0; }
00192 
00193         void
00194     _M_set_leaked()
00195         { this->_M_refcount = -1; }
00196 
00197         void
00198     _M_set_sharable()
00199         { this->_M_refcount = 0; }
00200 
00201     void
00202     _M_set_length_and_sharable(size_type __n)
00203     {
00204 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
00205       if (__builtin_expect(this != &_S_empty_rep(), false))
00206 #endif
00207         {
00208           this->_M_set_sharable();  // One reference.
00209           this->_M_length = __n;
00210           traits_type::assign(this->_M_refdata()[__n], _S_terminal);
00211           // grrr. (per 21.3.4)
00212           // You cannot leave those LWG people alone for a second.
00213         }
00214     }
00215 
00216     _CharT*
00217     _M_refdata() throw()
00218     { return reinterpret_cast<_CharT*>(this + 1); }
00219 
00220     _CharT*
00221     _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
00222     {
00223       return (!_M_is_leaked() && __alloc1 == __alloc2)
00224               ? _M_refcopy() : _M_clone(__alloc1);
00225     }
00226 
00227     // Create & Destroy
00228     static _Rep*
00229     _S_create(size_type, size_type, const _Alloc&);
00230 
00231     void
00232     _M_dispose(const _Alloc& __a)
00233     {
00234 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
00235       if (__builtin_expect(this != &_S_empty_rep(), false))
00236 #endif
00237         {
00238           // Be race-detector-friendly.  For more info see bits/c++config.
00239           _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
00240           if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
00241                              -1) <= 0)
00242         {
00243           _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
00244           _M_destroy(__a);
00245         }
00246         }
00247     }  // XXX MT
00248 
00249     void
00250     _M_destroy(const _Alloc&) throw();
00251 
00252     _CharT*
00253     _M_refcopy() throw()
00254     {
00255 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
00256       if (__builtin_expect(this != &_S_empty_rep(), false))
00257 #endif
00258             __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
00259       return _M_refdata();
00260     }  // XXX MT
00261 
00262     _CharT*
00263     _M_clone(const _Alloc&, size_type __res = 0);
00264       };
00265 
00266       // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
00267       struct _Alloc_hider : _Alloc
00268       {
00269     _Alloc_hider(_CharT* __dat, const _Alloc& __a)
00270     : _Alloc(__a), _M_p(__dat) { }
00271 
00272     _CharT* _M_p; // The actual data.
00273       };
00274 
00275     public:
00276       // Data Members (public):
00277       // NB: This is an unsigned type, and thus represents the maximum
00278       // size that the allocator can hold.
00279       ///  Value returned by various member functions when they fail.
00280       static const size_type    npos = static_cast<size_type>(-1);
00281 
00282     private:
00283       // Data Members (private):
00284       mutable _Alloc_hider  _M_dataplus;
00285 
00286       _CharT*
00287       _M_data() const
00288       { return  _M_dataplus._M_p; }
00289 
00290       _CharT*
00291       _M_data(_CharT* __p)
00292       { return (_M_dataplus._M_p = __p); }
00293 
00294       _Rep*
00295       _M_rep() const
00296       { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
00297 
00298       // For the internal use we have functions similar to `begin'/`end'
00299       // but they do not call _M_leak.
00300       iterator
00301       _M_ibegin() const
00302       { return iterator(_M_data()); }
00303 
00304       iterator
00305       _M_iend() const
00306       { return iterator(_M_data() + this->size()); }
00307 
00308       void
00309       _M_leak()    // for use in begin() & non-const op[]
00310       {
00311     if (!_M_rep()->_M_is_leaked())
00312       _M_leak_hard();
00313       }
00314 
00315       size_type
00316       _M_check(size_type __pos, const char* __s) const
00317       {
00318     if (__pos > this->size())
00319       __throw_out_of_range(__N(__s));
00320     return __pos;
00321       }
00322 
00323       void
00324       _M_check_length(size_type __n1, size_type __n2, const char* __s) const
00325       {
00326     if (this->max_size() - (this->size() - __n1) < __n2)
00327       __throw_length_error(__N(__s));
00328       }
00329 
00330       // NB: _M_limit doesn't check for a bad __pos value.
00331       size_type
00332       _M_limit(size_type __pos, size_type __off) const
00333       {
00334     const bool __testoff =  __off < this->size() - __pos;
00335     return __testoff ? __off : this->size() - __pos;
00336       }
00337 
00338       // True if _Rep and source do not overlap.
00339       bool
00340       _M_disjunct(const _CharT* __s) const
00341       {
00342     return (less<const _CharT*>()(__s, _M_data())
00343         || less<const _CharT*>()(_M_data() + this->size(), __s));
00344       }
00345 
00346       // When __n = 1 way faster than the general multichar
00347       // traits_type::copy/move/assign.
00348       static void
00349       _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
00350       {
00351     if (__n == 1)
00352       traits_type::assign(*__d, *__s);
00353     else
00354       traits_type::copy(__d, __s, __n);
00355       }
00356 
00357       static void
00358       _M_move(_CharT* __d, const _CharT* __s, size_type __n)
00359       {
00360     if (__n == 1)
00361       traits_type::assign(*__d, *__s);
00362     else
00363       traits_type::move(__d, __s, __n);   
00364       }
00365 
00366       static void
00367       _M_assign(_CharT* __d, size_type __n, _CharT __c)
00368       {
00369     if (__n == 1)
00370       traits_type::assign(*__d, __c);
00371     else
00372       traits_type::assign(__d, __n, __c);     
00373       }
00374 
00375       // _S_copy_chars is a separate template to permit specialization
00376       // to optimize for the common case of pointers as iterators.
00377       template<class _Iterator>
00378         static void
00379         _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
00380         {
00381       for (; __k1 != __k2; ++__k1, ++__p)
00382         traits_type::assign(*__p, *__k1); // These types are off.
00383     }
00384 
00385       static void
00386       _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
00387       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
00388 
00389       static void
00390       _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
00391       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
00392 
00393       static void
00394       _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
00395       { _M_copy(__p, __k1, __k2 - __k1); }
00396 
00397       static void
00398       _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
00399       { _M_copy(__p, __k1, __k2 - __k1); }
00400 
00401       static int
00402       _S_compare(size_type __n1, size_type __n2)
00403       {
00404     const difference_type __d = difference_type(__n1 - __n2);
00405 
00406     if (__d > __gnu_cxx::__numeric_traits<int>::__max)
00407       return __gnu_cxx::__numeric_traits<int>::__max;
00408     else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
00409       return __gnu_cxx::__numeric_traits<int>::__min;
00410     else
00411       return int(__d);
00412       }
00413 
00414       void
00415       _M_mutate(size_type __pos, size_type __len1, size_type __len2);
00416 
00417       void
00418       _M_leak_hard();
00419 
00420       static _Rep&
00421       _S_empty_rep()
00422       { return _Rep::_S_empty_rep(); }
00423 
00424     public:
00425       // Construct/copy/destroy:
00426       // NB: We overload ctors in some cases instead of using default
00427       // arguments, per 17.4.4.4 para. 2 item 2.
00428 
00429       /**
00430        *  @brief  Default constructor creates an empty string.
00431        */
00432       basic_string()
00433 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
00434       : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
00435 #else
00436       : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
00437 #endif
00438 
00439       /**
00440        *  @brief  Construct an empty string using allocator @a a.
00441        */
00442       explicit
00443       basic_string(const _Alloc& __a);
00444 
00445       // NB: per LWG issue 42, semantics different from IS:
00446       /**
00447        *  @brief  Construct string with copy of value of @a str.
00448        *  @param  str  Source string.
00449        */
00450       basic_string(const basic_string& __str);
00451       /**
00452        *  @brief  Construct string as copy of a substring.
00453        *  @param  str  Source string.
00454        *  @param  pos  Index of first character to copy from.
00455        *  @param  n  Number of characters to copy (default remainder).
00456        */
00457       basic_string(const basic_string& __str, size_type __pos,
00458            size_type __n = npos);
00459       /**
00460        *  @brief  Construct string as copy of a substring.
00461        *  @param  str  Source string.
00462        *  @param  pos  Index of first character to copy from.
00463        *  @param  n  Number of characters to copy.
00464        *  @param  a  Allocator to use.
00465        */
00466       basic_string(const basic_string& __str, size_type __pos,
00467            size_type __n, const _Alloc& __a);
00468 
00469       /**
00470        *  @brief  Construct string initialized by a character %array.
00471        *  @param  s  Source character %array.
00472        *  @param  n  Number of characters to copy.
00473        *  @param  a  Allocator to use (default is default allocator).
00474        *
00475        *  NB: @a s must have at least @a n characters, &apos;\\0&apos;
00476        *  has no special meaning.
00477        */
00478       basic_string(const _CharT* __s, size_type __n,
00479            const _Alloc& __a = _Alloc());
00480       /**
00481        *  @brief  Construct string as copy of a C string.
00482        *  @param  s  Source C string.
00483        *  @param  a  Allocator to use (default is default allocator).
00484        */
00485       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
00486       /**
00487        *  @brief  Construct string as multiple characters.
00488        *  @param  n  Number of characters.
00489        *  @param  c  Character to use.
00490        *  @param  a  Allocator to use (default is default allocator).
00491        */
00492       basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
00493 
00494 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00495       /**
00496        *  @brief  Move construct string.
00497        *  @param  str  Source string.
00498        *
00499        *  The newly-created string contains the exact contents of @a str.
00500        *  @a str is a valid, but unspecified string.
00501        **/
00502       basic_string(basic_string&& __str)
00503       : _M_dataplus(__str._M_dataplus)
00504       {
00505 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING   
00506     __str._M_data(_S_empty_rep()._M_refdata());
00507 #else
00508     __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
00509 #endif
00510       }
00511 
00512       /**
00513        *  @brief  Construct string from an initializer %list.
00514        *  @param  l  std::initializer_list of characters.
00515        *  @param  a  Allocator to use (default is default allocator).
00516        */
00517       basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
00518 #endif // __GXX_EXPERIMENTAL_CXX0X__
00519 
00520       /**
00521        *  @brief  Construct string as copy of a range.
00522        *  @param  beg  Start of range.
00523        *  @param  end  End of range.
00524        *  @param  a  Allocator to use (default is default allocator).
00525        */
00526       template<class _InputIterator>
00527         basic_string(_InputIterator __beg, _InputIterator __end,
00528              const _Alloc& __a = _Alloc());
00529 
00530       /**
00531        *  @brief  Destroy the string instance.
00532        */
00533       ~basic_string()
00534       { _M_rep()->_M_dispose(this->get_allocator()); }
00535 
00536       /**
00537        *  @brief  Assign the value of @a str to this string.
00538        *  @param  str  Source string.
00539        */
00540       basic_string&
00541       operator=(const basic_string& __str) 
00542       { return this->assign(__str); }
00543 
00544       /**
00545        *  @brief  Copy contents of @a s into this string.
00546        *  @param  s  Source null-terminated string.
00547        */
00548       basic_string&
00549       operator=(const _CharT* __s) 
00550       { return this->assign(__s); }
00551 
00552       /**
00553        *  @brief  Set value to string of length 1.
00554        *  @param  c  Source character.
00555        *
00556        *  Assigning to a character makes this string length 1 and
00557        *  (*this)[0] == @a c.
00558        */
00559       basic_string&
00560       operator=(_CharT __c) 
00561       { 
00562     this->assign(1, __c); 
00563     return *this;
00564       }
00565 
00566 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00567       /**
00568        *  @brief  Move assign the value of @a str to this string.
00569        *  @param  str  Source string.
00570        *
00571        *  The contents of @a str are moved into this string (without copying).
00572        *  @a str is a valid, but unspecified string.
00573        **/
00574       basic_string&
00575       operator=(basic_string&& __str)
00576       {
00577     // NB: DR 1204.
00578     this->swap(__str);
00579     return *this;
00580       }
00581 
00582       /**
00583        *  @brief  Set value to string constructed from initializer %list.
00584        *  @param  l  std::initializer_list.
00585        */
00586       basic_string&
00587       operator=(initializer_list<_CharT> __l)
00588       {
00589     this->assign(__l.begin(), __l.size());
00590     return *this;
00591       }
00592 #endif // __GXX_EXPERIMENTAL_CXX0X__
00593 
00594       // Iterators:
00595       /**
00596        *  Returns a read/write iterator that points to the first character in
00597        *  the %string.  Unshares the string.
00598        */
00599       iterator
00600       begin()
00601       {
00602     _M_leak();
00603     return iterator(_M_data());
00604       }
00605 
00606       /**
00607        *  Returns a read-only (constant) iterator that points to the first
00608        *  character in the %string.
00609        */
00610       const_iterator
00611       begin() const
00612       { return const_iterator(_M_data()); }
00613 
00614       /**
00615        *  Returns a read/write iterator that points one past the last
00616        *  character in the %string.  Unshares the string.
00617        */
00618       iterator
00619       end()
00620       {
00621     _M_leak();
00622     return iterator(_M_data() + this->size());
00623       }
00624 
00625       /**
00626        *  Returns a read-only (constant) iterator that points one past the
00627        *  last character in the %string.
00628        */
00629       const_iterator
00630       end() const
00631       { return const_iterator(_M_data() + this->size()); }
00632 
00633       /**
00634        *  Returns a read/write reverse iterator that points to the last
00635        *  character in the %string.  Iteration is done in reverse element
00636        *  order.  Unshares the string.
00637        */
00638       reverse_iterator
00639       rbegin()
00640       { return reverse_iterator(this->end()); }
00641 
00642       /**
00643        *  Returns a read-only (constant) reverse iterator that points
00644        *  to the last character in the %string.  Iteration is done in
00645        *  reverse element order.
00646        */
00647       const_reverse_iterator
00648       rbegin() const
00649       { return const_reverse_iterator(this->end()); }
00650 
00651       /**
00652        *  Returns a read/write reverse iterator that points to one before the
00653        *  first character in the %string.  Iteration is done in reverse
00654        *  element order.  Unshares the string.
00655        */
00656       reverse_iterator
00657       rend()
00658       { return reverse_iterator(this->begin()); }
00659 
00660       /**
00661        *  Returns a read-only (constant) reverse iterator that points
00662        *  to one before the first character in the %string.  Iteration
00663        *  is done in reverse element order.
00664        */
00665       const_reverse_iterator
00666       rend() const
00667       { return const_reverse_iterator(this->begin()); }
00668 
00669 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00670       /**
00671        *  Returns a read-only (constant) iterator that points to the first
00672        *  character in the %string.
00673        */
00674       const_iterator
00675       cbegin() const
00676       { return const_iterator(this->_M_data()); }
00677 
00678       /**
00679        *  Returns a read-only (constant) iterator that points one past the
00680        *  last character in the %string.
00681        */
00682       const_iterator
00683       cend() const
00684       { return const_iterator(this->_M_data() + this->size()); }
00685 
00686       /**
00687        *  Returns a read-only (constant) reverse iterator that points
00688        *  to the last character in the %string.  Iteration is done in
00689        *  reverse element order.
00690        */
00691       const_reverse_iterator
00692       crbegin() const
00693       { return const_reverse_iterator(this->end()); }
00694 
00695       /**
00696        *  Returns a read-only (constant) reverse iterator that points
00697        *  to one before the first character in the %string.  Iteration
00698        *  is done in reverse element order.
00699        */
00700       const_reverse_iterator
00701       crend() const
00702       { return const_reverse_iterator(this->begin()); }
00703 #endif
00704 
00705     public:
00706       // Capacity:
00707       ///  Returns the number of characters in the string, not including any
00708       ///  null-termination.
00709       size_type
00710       size() const
00711       { return _M_rep()->_M_length; }
00712 
00713       ///  Returns the number of characters in the string, not including any
00714       ///  null-termination.
00715       size_type
00716       length() const
00717       { return _M_rep()->_M_length; }
00718 
00719       ///  Returns the size() of the largest possible %string.
00720       size_type
00721       max_size() const
00722       { return _Rep::_S_max_size; }
00723 
00724       /**
00725        *  @brief  Resizes the %string to the specified number of characters.
00726        *  @param  n  Number of characters the %string should contain.
00727        *  @param  c  Character to fill any new elements.
00728        *
00729        *  This function will %resize the %string to the specified
00730        *  number of characters.  If the number is smaller than the
00731        *  %string's current size the %string is truncated, otherwise
00732        *  the %string is extended and new elements are %set to @a c.
00733        */
00734       void
00735       resize(size_type __n, _CharT __c);
00736 
00737       /**
00738        *  @brief  Resizes the %string to the specified number of characters.
00739        *  @param  n  Number of characters the %string should contain.
00740        *
00741        *  This function will resize the %string to the specified length.  If
00742        *  the new size is smaller than the %string's current size the %string
00743        *  is truncated, otherwise the %string is extended and new characters
00744        *  are default-constructed.  For basic types such as char, this means
00745        *  setting them to 0.
00746        */
00747       void
00748       resize(size_type __n)
00749       { this->resize(__n, _CharT()); }
00750 
00751 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00752       ///  A non-binding request to reduce capacity() to size().
00753       void
00754       shrink_to_fit()
00755       {
00756     __try
00757       { reserve(0); }
00758     __catch(...)
00759       { }
00760       }
00761 #endif
00762 
00763       /**
00764        *  Returns the total number of characters that the %string can hold
00765        *  before needing to allocate more memory.
00766        */
00767       size_type
00768       capacity() const
00769       { return _M_rep()->_M_capacity; }
00770 
00771       /**
00772        *  @brief  Attempt to preallocate enough memory for specified number of
00773        *          characters.
00774        *  @param  res_arg  Number of characters required.
00775        *  @throw  std::length_error  If @a res_arg exceeds @c max_size().
00776        *
00777        *  This function attempts to reserve enough memory for the
00778        *  %string to hold the specified number of characters.  If the
00779        *  number requested is more than max_size(), length_error is
00780        *  thrown.
00781        *
00782        *  The advantage of this function is that if optimal code is a
00783        *  necessity and the user can determine the string length that will be
00784        *  required, the user can reserve the memory in %advance, and thus
00785        *  prevent a possible reallocation of memory and copying of %string
00786        *  data.
00787        */
00788       void
00789       reserve(size_type __res_arg = 0);
00790 
00791       /**
00792        *  Erases the string, making it empty.
00793        */
00794       void
00795       clear()
00796       { _M_mutate(0, this->size(), 0); }
00797 
00798       /**
00799        *  Returns true if the %string is empty.  Equivalent to 
00800        *  <code>*this == ""</code>.
00801        */
00802       bool
00803       empty() const
00804       { return this->size() == 0; }
00805 
00806       // Element access:
00807       /**
00808        *  @brief  Subscript access to the data contained in the %string.
00809        *  @param  pos  The index of the character to access.
00810        *  @return  Read-only (constant) reference to the character.
00811        *
00812        *  This operator allows for easy, array-style, data access.
00813        *  Note that data access with this operator is unchecked and
00814        *  out_of_range lookups are not defined. (For checked lookups
00815        *  see at().)
00816        */
00817       const_reference
00818       operator[] (size_type __pos) const
00819       {
00820     _GLIBCXX_DEBUG_ASSERT(__pos <= size());
00821     return _M_data()[__pos];
00822       }
00823 
00824       /**
00825        *  @brief  Subscript access to the data contained in the %string.
00826        *  @param  pos  The index of the character to access.
00827        *  @return  Read/write reference to the character.
00828        *
00829        *  This operator allows for easy, array-style, data access.
00830        *  Note that data access with this operator is unchecked and
00831        *  out_of_range lookups are not defined. (For checked lookups
00832        *  see at().)  Unshares the string.
00833        */
00834       reference
00835       operator[](size_type __pos)
00836       {
00837         // allow pos == size() as v3 extension:
00838     _GLIBCXX_DEBUG_ASSERT(__pos <= size());
00839         // but be strict in pedantic mode:
00840     _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
00841     _M_leak();
00842     return _M_data()[__pos];
00843       }
00844 
00845       /**
00846        *  @brief  Provides access to the data contained in the %string.
00847        *  @param n The index of the character to access.
00848        *  @return  Read-only (const) reference to the character.
00849        *  @throw  std::out_of_range  If @a n is an invalid index.
00850        *
00851        *  This function provides for safer data access.  The parameter is
00852        *  first checked that it is in the range of the string.  The function
00853        *  throws out_of_range if the check fails.
00854        */
00855       const_reference
00856       at(size_type __n) const
00857       {
00858     if (__n >= this->size())
00859       __throw_out_of_range(__N("basic_string::at"));
00860     return _M_data()[__n];
00861       }
00862 
00863 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00864       /**
00865        *  Returns a read/write reference to the data at the first
00866        *  element of the %string.
00867        */
00868       reference
00869       front()
00870       { return operator[](0); }
00871 
00872       /**
00873        *  Returns a read-only (constant) reference to the data at the first
00874        *  element of the %string.
00875        */
00876       const_reference
00877       front() const
00878       { return operator[](0); }
00879 
00880       /**
00881        *  Returns a read/write reference to the data at the last
00882        *  element of the %string.
00883        */
00884       reference
00885       back()
00886       { return operator[](this->size() - 1); }
00887 
00888       /**
00889        *  Returns a read-only (constant) reference to the data at the
00890        *  last element of the %string.
00891        */
00892       const_reference
00893       back() const
00894       { return operator[](this->size() - 1); }
00895 #endif
00896 
00897       /**
00898        *  @brief  Provides access to the data contained in the %string.
00899        *  @param n The index of the character to access.
00900        *  @return  Read/write reference to the character.
00901        *  @throw  std::out_of_range  If @a n is an invalid index.
00902        *
00903        *  This function provides for safer data access.  The parameter is
00904        *  first checked that it is in the range of the string.  The function
00905        *  throws out_of_range if the check fails.  Success results in
00906        *  unsharing the string.
00907        */
00908       reference
00909       at(size_type __n)
00910       {
00911     if (__n >= size())
00912       __throw_out_of_range(__N("basic_string::at"));
00913     _M_leak();
00914     return _M_data()[__n];
00915       }
00916 
00917       // Modifiers:
00918       /**
00919        *  @brief  Append a string to this string.
00920        *  @param str  The string to append.
00921        *  @return  Reference to this string.
00922        */
00923       basic_string&
00924       operator+=(const basic_string& __str)
00925       { return this->append(__str); }
00926 
00927       /**
00928        *  @brief  Append a C string.
00929        *  @param s  The C string to append.
00930        *  @return  Reference to this string.
00931        */
00932       basic_string&
00933       operator+=(const _CharT* __s)
00934       { return this->append(__s); }
00935 
00936       /**
00937        *  @brief  Append a character.
00938        *  @param c  The character to append.
00939        *  @return  Reference to this string.
00940        */
00941       basic_string&
00942       operator+=(_CharT __c)
00943       { 
00944     this->push_back(__c);
00945     return *this;
00946       }
00947 
00948 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00949       /**
00950        *  @brief  Append an initializer_list of characters.
00951        *  @param l  The initializer_list of characters to be appended.
00952        *  @return  Reference to this string.
00953        */
00954       basic_string&
00955       operator+=(initializer_list<_CharT> __l)
00956       { return this->append(__l.begin(), __l.size()); }
00957 #endif // __GXX_EXPERIMENTAL_CXX0X__
00958 
00959       /**
00960        *  @brief  Append a string to this string.
00961        *  @param str  The string to append.
00962        *  @return  Reference to this string.
00963        */
00964       basic_string&
00965       append(const basic_string& __str);
00966 
00967       /**
00968        *  @brief  Append a substring.
00969        *  @param str  The string to append.
00970        *  @param pos  Index of the first character of str to append.
00971        *  @param n  The number of characters to append.
00972        *  @return  Reference to this string.
00973        *  @throw  std::out_of_range if @a pos is not a valid index.
00974        *
00975        *  This function appends @a n characters from @a str starting at @a pos
00976        *  to this string.  If @a n is is larger than the number of available
00977        *  characters in @a str, the remainder of @a str is appended.
00978        */
00979       basic_string&
00980       append(const basic_string& __str, size_type __pos, size_type __n);
00981 
00982       /**
00983        *  @brief  Append a C substring.
00984        *  @param s  The C string to append.
00985        *  @param n  The number of characters to append.
00986        *  @return  Reference to this string.
00987        */
00988       basic_string&
00989       append(const _CharT* __s, size_type __n);
00990 
00991       /**
00992        *  @brief  Append a C string.
00993        *  @param s  The C string to append.
00994        *  @return  Reference to this string.
00995        */
00996       basic_string&
00997       append(const _CharT* __s)
00998       {
00999     __glibcxx_requires_string(__s);
01000     return this->append(__s, traits_type::length(__s));
01001       }
01002 
01003       /**
01004        *  @brief  Append multiple characters.
01005        *  @param n  The number of characters to append.
01006        *  @param c  The character to use.
01007        *  @return  Reference to this string.
01008        *
01009        *  Appends n copies of c to this string.
01010        */
01011       basic_string&
01012       append(size_type __n, _CharT __c);
01013 
01014 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01015       /**
01016        *  @brief  Append an initializer_list of characters.
01017        *  @param l  The initializer_list of characters to append.
01018        *  @return  Reference to this string.
01019        */
01020       basic_string&
01021       append(initializer_list<_CharT> __l)
01022       { return this->append(__l.begin(), __l.size()); }
01023 #endif // __GXX_EXPERIMENTAL_CXX0X__
01024 
01025       /**
01026        *  @brief  Append a range of characters.
01027        *  @param first  Iterator referencing the first character to append.
01028        *  @param last  Iterator marking the end of the range.
01029        *  @return  Reference to this string.
01030        *
01031        *  Appends characters in the range [first,last) to this string.
01032        */
01033       template<class _InputIterator>
01034         basic_string&
01035         append(_InputIterator __first, _InputIterator __last)
01036         { return this->replace(_M_iend(), _M_iend(), __first, __last); }
01037 
01038       /**
01039        *  @brief  Append a single character.
01040        *  @param c  Character to append.
01041        */
01042       void
01043       push_back(_CharT __c)
01044       { 
01045     const size_type __len = 1 + this->size();
01046     if (__len > this->capacity() || _M_rep()->_M_is_shared())
01047       this->reserve(__len);
01048     traits_type::assign(_M_data()[this->size()], __c);
01049     _M_rep()->_M_set_length_and_sharable(__len);
01050       }
01051 
01052       /**
01053        *  @brief  Set value to contents of another string.
01054        *  @param  str  Source string to use.
01055        *  @return  Reference to this string.
01056        */
01057       basic_string&
01058       assign(const basic_string& __str);
01059 
01060 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01061       /**
01062        *  @brief  Set value to contents of another string.
01063        *  @param  str  Source string to use.
01064        *  @return  Reference to this string.
01065        *
01066        *  This function sets this string to the exact contents of @a str.
01067        *  @a str is a valid, but unspecified string.
01068        */
01069       basic_string&
01070       assign(basic_string&& __str)
01071       {
01072     this->swap(__str);
01073     return *this;
01074       }
01075 #endif // __GXX_EXPERIMENTAL_CXX0X__
01076 
01077       /**
01078        *  @brief  Set value to a substring of a string.
01079        *  @param str  The string to use.
01080        *  @param pos  Index of the first character of str.
01081        *  @param n  Number of characters to use.
01082        *  @return  Reference to this string.
01083        *  @throw  std::out_of_range if @a pos is not a valid index.
01084        *
01085        *  This function sets this string to the substring of @a str consisting
01086        *  of @a n characters at @a pos.  If @a n is is larger than the number
01087        *  of available characters in @a str, the remainder of @a str is used.
01088        */
01089       basic_string&
01090       assign(const basic_string& __str, size_type __pos, size_type __n)
01091       { return this->assign(__str._M_data()
01092                 + __str._M_check(__pos, "basic_string::assign"),
01093                 __str._M_limit(__pos, __n)); }
01094 
01095       /**
01096        *  @brief  Set value to a C substring.
01097        *  @param s  The C string to use.
01098        *  @param n  Number of characters to use.
01099        *  @return  Reference to this string.
01100        *
01101        *  This function sets the value of this string to the first @a n
01102        *  characters of @a s.  If @a n is is larger than the number of
01103        *  available characters in @a s, the remainder of @a s is used.
01104        */
01105       basic_string&
01106       assign(const _CharT* __s, size_type __n);
01107 
01108       /**
01109        *  @brief  Set value to contents of a C string.
01110        *  @param s  The C string to use.
01111        *  @return  Reference to this string.
01112        *
01113        *  This function sets the value of this string to the value of @a s.
01114        *  The data is copied, so there is no dependence on @a s once the
01115        *  function returns.
01116        */
01117       basic_string&
01118       assign(const _CharT* __s)
01119       {
01120     __glibcxx_requires_string(__s);
01121     return this->assign(__s, traits_type::length(__s));
01122       }
01123 
01124       /**
01125        *  @brief  Set value to multiple characters.
01126        *  @param n  Length of the resulting string.
01127        *  @param c  The character to use.
01128        *  @return  Reference to this string.
01129        *
01130        *  This function sets the value of this string to @a n copies of
01131        *  character @a c.
01132        */
01133       basic_string&
01134       assign(size_type __n, _CharT __c)
01135       { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
01136 
01137       /**
01138        *  @brief  Set value to a range of characters.
01139        *  @param first  Iterator referencing the first character to append.
01140        *  @param last  Iterator marking the end of the range.
01141        *  @return  Reference to this string.
01142        *
01143        *  Sets value of string to characters in the range [first,last).
01144       */
01145       template<class _InputIterator>
01146         basic_string&
01147         assign(_InputIterator __first, _InputIterator __last)
01148         { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
01149 
01150 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01151       /**
01152        *  @brief  Set value to an initializer_list of characters.
01153        *  @param l  The initializer_list of characters to assign.
01154        *  @return  Reference to this string.
01155        */
01156       basic_string&
01157       assign(initializer_list<_CharT> __l)
01158       { return this->assign(__l.begin(), __l.size()); }
01159 #endif // __GXX_EXPERIMENTAL_CXX0X__
01160 
01161       /**
01162        *  @brief  Insert multiple characters.
01163        *  @param p  Iterator referencing location in string to insert at.
01164        *  @param n  Number of characters to insert
01165        *  @param c  The character to insert.
01166        *  @throw  std::length_error  If new length exceeds @c max_size().
01167        *
01168        *  Inserts @a n copies of character @a c starting at the position
01169        *  referenced by iterator @a p.  If adding characters causes the length
01170        *  to exceed max_size(), length_error is thrown.  The value of the
01171        *  string doesn't change if an error is thrown.
01172       */
01173       void
01174       insert(iterator __p, size_type __n, _CharT __c)
01175       { this->replace(__p, __p, __n, __c);  }
01176 
01177       /**
01178        *  @brief  Insert a range of characters.
01179        *  @param p  Iterator referencing location in string to insert at.
01180        *  @param beg  Start of range.
01181        *  @param end  End of range.
01182        *  @throw  std::length_error  If new length exceeds @c max_size().
01183        *
01184        *  Inserts characters in range [beg,end).  If adding characters causes
01185        *  the length to exceed max_size(), length_error is thrown.  The value
01186        *  of the string doesn't change if an error is thrown.
01187       */
01188       template<class _InputIterator>
01189         void
01190         insert(iterator __p, _InputIterator __beg, _InputIterator __end)
01191         { this->replace(__p, __p, __beg, __end); }
01192 
01193 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01194       /**
01195        *  @brief  Insert an initializer_list of characters.
01196        *  @param p  Iterator referencing location in string to insert at.
01197        *  @param l  The initializer_list of characters to insert.
01198        *  @throw  std::length_error  If new length exceeds @c max_size().
01199        */
01200       void
01201       insert(iterator __p, initializer_list<_CharT> __l)
01202       {
01203     _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
01204     this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
01205       }
01206 #endif // __GXX_EXPERIMENTAL_CXX0X__
01207 
01208       /**
01209        *  @brief  Insert value of a string.
01210        *  @param pos1  Iterator referencing location in string to insert at.
01211        *  @param str  The string to insert.
01212        *  @return  Reference to this string.
01213        *  @throw  std::length_error  If new length exceeds @c max_size().
01214        *
01215        *  Inserts value of @a str starting at @a pos1.  If adding characters
01216        *  causes the length to exceed max_size(), length_error is thrown.  The
01217        *  value of the string doesn't change if an error is thrown.
01218       */
01219       basic_string&
01220       insert(size_type __pos1, const basic_string& __str)
01221       { return this->insert(__pos1, __str, size_type(0), __str.size()); }
01222 
01223       /**
01224        *  @brief  Insert a substring.
01225        *  @param pos1  Iterator referencing location in string to insert at.
01226        *  @param str  The string to insert.
01227        *  @param pos2  Start of characters in str to insert.
01228        *  @param n  Number of characters to insert.
01229        *  @return  Reference to this string.
01230        *  @throw  std::length_error  If new length exceeds @c max_size().
01231        *  @throw  std::out_of_range  If @a pos1 > size() or
01232        *  @a pos2 > @a str.size().
01233        *
01234        *  Starting at @a pos1, insert @a n character of @a str beginning with
01235        *  @a pos2.  If adding characters causes the length to exceed
01236        *  max_size(), length_error is thrown.  If @a pos1 is beyond the end of
01237        *  this string or @a pos2 is beyond the end of @a str, out_of_range is
01238        *  thrown.  The value of the string doesn't change if an error is
01239        *  thrown.
01240       */
01241       basic_string&
01242       insert(size_type __pos1, const basic_string& __str,
01243          size_type __pos2, size_type __n)
01244       { return this->insert(__pos1, __str._M_data()
01245                 + __str._M_check(__pos2, "basic_string::insert"),
01246                 __str._M_limit(__pos2, __n)); }
01247 
01248       /**
01249        *  @brief  Insert a C substring.
01250        *  @param pos  Iterator referencing location in string to insert at.
01251        *  @param s  The C string to insert.
01252        *  @param n  The number of characters to insert.
01253        *  @return  Reference to this string.
01254        *  @throw  std::length_error  If new length exceeds @c max_size().
01255        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01256        *  string.
01257        *
01258        *  Inserts the first @a n characters of @a s starting at @a pos.  If
01259        *  adding characters causes the length to exceed max_size(),
01260        *  length_error is thrown.  If @a pos is beyond end(), out_of_range is
01261        *  thrown.  The value of the string doesn't change if an error is
01262        *  thrown.
01263       */
01264       basic_string&
01265       insert(size_type __pos, const _CharT* __s, size_type __n);
01266 
01267       /**
01268        *  @brief  Insert a C string.
01269        *  @param pos  Iterator referencing location in string to insert at.
01270        *  @param s  The C string to insert.
01271        *  @return  Reference to this string.
01272        *  @throw  std::length_error  If new length exceeds @c max_size().
01273        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01274        *  string.
01275        *
01276        *  Inserts the first @a n characters of @a s starting at @a pos.  If
01277        *  adding characters causes the length to exceed max_size(),
01278        *  length_error is thrown.  If @a pos is beyond end(), out_of_range is
01279        *  thrown.  The value of the string doesn't change if an error is
01280        *  thrown.
01281       */
01282       basic_string&
01283       insert(size_type __pos, const _CharT* __s)
01284       {
01285     __glibcxx_requires_string(__s);
01286     return this->insert(__pos, __s, traits_type::length(__s));
01287       }
01288 
01289       /**
01290        *  @brief  Insert multiple characters.
01291        *  @param pos  Index in string to insert at.
01292        *  @param n  Number of characters to insert
01293        *  @param c  The character to insert.
01294        *  @return  Reference to this string.
01295        *  @throw  std::length_error  If new length exceeds @c max_size().
01296        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01297        *  string.
01298        *
01299        *  Inserts @a n copies of character @a c starting at index @a pos.  If
01300        *  adding characters causes the length to exceed max_size(),
01301        *  length_error is thrown.  If @a pos > length(), out_of_range is
01302        *  thrown.  The value of the string doesn't change if an error is
01303        *  thrown.
01304       */
01305       basic_string&
01306       insert(size_type __pos, size_type __n, _CharT __c)
01307       { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
01308                   size_type(0), __n, __c); }
01309 
01310       /**
01311        *  @brief  Insert one character.
01312        *  @param p  Iterator referencing position in string to insert at.
01313        *  @param c  The character to insert.
01314        *  @return  Iterator referencing newly inserted char.
01315        *  @throw  std::length_error  If new length exceeds @c max_size().
01316        *
01317        *  Inserts character @a c at position referenced by @a p.  If adding
01318        *  character causes the length to exceed max_size(), length_error is
01319        *  thrown.  If @a p is beyond end of string, out_of_range is thrown.
01320        *  The value of the string doesn't change if an error is thrown.
01321       */
01322       iterator
01323       insert(iterator __p, _CharT __c)
01324       {
01325     _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
01326     const size_type __pos = __p - _M_ibegin();
01327     _M_replace_aux(__pos, size_type(0), size_type(1), __c);
01328     _M_rep()->_M_set_leaked();
01329     return iterator(_M_data() + __pos);
01330       }
01331 
01332       /**
01333        *  @brief  Remove characters.
01334        *  @param pos  Index of first character to remove (default 0).
01335        *  @param n  Number of characters to remove (default remainder).
01336        *  @return  Reference to this string.
01337        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01338        *  string.
01339        *
01340        *  Removes @a n characters from this string starting at @a pos.  The
01341        *  length of the string is reduced by @a n.  If there are < @a n
01342        *  characters to remove, the remainder of the string is truncated.  If
01343        *  @a p is beyond end of string, out_of_range is thrown.  The value of
01344        *  the string doesn't change if an error is thrown.
01345       */
01346       basic_string&
01347       erase(size_type __pos = 0, size_type __n = npos)
01348       { 
01349     _M_mutate(_M_check(__pos, "basic_string::erase"),
01350           _M_limit(__pos, __n), size_type(0));
01351     return *this;
01352       }
01353 
01354       /**
01355        *  @brief  Remove one character.
01356        *  @param position  Iterator referencing the character to remove.
01357        *  @return  iterator referencing same location after removal.
01358        *
01359        *  Removes the character at @a position from this string. The value
01360        *  of the string doesn't change if an error is thrown.
01361       */
01362       iterator
01363       erase(iterator __position)
01364       {
01365     _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
01366                  && __position < _M_iend());
01367     const size_type __pos = __position - _M_ibegin();
01368     _M_mutate(__pos, size_type(1), size_type(0));
01369     _M_rep()->_M_set_leaked();
01370     return iterator(_M_data() + __pos);
01371       }
01372 
01373       /**
01374        *  @brief  Remove a range of characters.
01375        *  @param first  Iterator referencing the first character to remove.
01376        *  @param last  Iterator referencing the end of the range.
01377        *  @return  Iterator referencing location of first after removal.
01378        *
01379        *  Removes the characters in the range [first,last) from this string.
01380        *  The value of the string doesn't change if an error is thrown.
01381       */
01382       iterator
01383       erase(iterator __first, iterator __last);
01384  
01385       /**
01386        *  @brief  Replace characters with value from another string.
01387        *  @param pos  Index of first character to replace.
01388        *  @param n  Number of characters to be replaced.
01389        *  @param str  String to insert.
01390        *  @return  Reference to this string.
01391        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01392        *  string.
01393        *  @throw  std::length_error  If new length exceeds @c max_size().
01394        *
01395        *  Removes the characters in the range [pos,pos+n) from this string.
01396        *  In place, the value of @a str is inserted.  If @a pos is beyond end
01397        *  of string, out_of_range is thrown.  If the length of the result
01398        *  exceeds max_size(), length_error is thrown.  The value of the string
01399        *  doesn't change if an error is thrown.
01400       */
01401       basic_string&
01402       replace(size_type __pos, size_type __n, const basic_string& __str)
01403       { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
01404 
01405       /**
01406        *  @brief  Replace characters with value from another string.
01407        *  @param pos1  Index of first character to replace.
01408        *  @param n1  Number of characters to be replaced.
01409        *  @param str  String to insert.
01410        *  @param pos2  Index of first character of str to use.
01411        *  @param n2  Number of characters from str to use.
01412        *  @return  Reference to this string.
01413        *  @throw  std::out_of_range  If @a pos1 > size() or @a pos2 >
01414        *  str.size().
01415        *  @throw  std::length_error  If new length exceeds @c max_size().
01416        *
01417        *  Removes the characters in the range [pos1,pos1 + n) from this
01418        *  string.  In place, the value of @a str is inserted.  If @a pos is
01419        *  beyond end of string, out_of_range is thrown.  If the length of the
01420        *  result exceeds max_size(), length_error is thrown.  The value of the
01421        *  string doesn't change if an error is thrown.
01422       */
01423       basic_string&
01424       replace(size_type __pos1, size_type __n1, const basic_string& __str,
01425           size_type __pos2, size_type __n2)
01426       { return this->replace(__pos1, __n1, __str._M_data()
01427                  + __str._M_check(__pos2, "basic_string::replace"),
01428                  __str._M_limit(__pos2, __n2)); }
01429 
01430       /**
01431        *  @brief  Replace characters with value of a C substring.
01432        *  @param pos  Index of first character to replace.
01433        *  @param n1  Number of characters to be replaced.
01434        *  @param s  C string to insert.
01435        *  @param n2  Number of characters from @a s to use.
01436        *  @return  Reference to this string.
01437        *  @throw  std::out_of_range  If @a pos1 > size().
01438        *  @throw  std::length_error  If new length exceeds @c max_size().
01439        *
01440        *  Removes the characters in the range [pos,pos + n1) from this string.
01441        *  In place, the first @a n2 characters of @a s are inserted, or all
01442        *  of @a s if @a n2 is too large.  If @a pos is beyond end of string,
01443        *  out_of_range is thrown.  If the length of result exceeds max_size(),
01444        *  length_error is thrown.  The value of the string doesn't change if
01445        *  an error is thrown.
01446       */
01447       basic_string&
01448       replace(size_type __pos, size_type __n1, const _CharT* __s,
01449           size_type __n2);
01450 
01451       /**
01452        *  @brief  Replace characters with value of a C string.
01453        *  @param pos  Index of first character to replace.
01454        *  @param n1  Number of characters to be replaced.
01455        *  @param s  C string to insert.
01456        *  @return  Reference to this string.
01457        *  @throw  std::out_of_range  If @a pos > size().
01458        *  @throw  std::length_error  If new length exceeds @c max_size().
01459        *
01460        *  Removes the characters in the range [pos,pos + n1) from this string.
01461        *  In place, the characters of @a s are inserted.  If @a pos is beyond
01462        *  end of string, out_of_range is thrown.  If the length of result
01463        *  exceeds max_size(), length_error is thrown.  The value of the string
01464        *  doesn't change if an error is thrown.
01465       */
01466       basic_string&
01467       replace(size_type __pos, size_type __n1, const _CharT* __s)
01468       {
01469     __glibcxx_requires_string(__s);
01470     return this->replace(__pos, __n1, __s, traits_type::length(__s));
01471       }
01472 
01473       /**
01474        *  @brief  Replace characters with multiple characters.
01475        *  @param pos  Index of first character to replace.
01476        *  @param n1  Number of characters to be replaced.
01477        *  @param n2  Number of characters to insert.
01478        *  @param c  Character to insert.
01479        *  @return  Reference to this string.
01480        *  @throw  std::out_of_range  If @a pos > size().
01481        *  @throw  std::length_error  If new length exceeds @c max_size().
01482        *
01483        *  Removes the characters in the range [pos,pos + n1) from this string.
01484        *  In place, @a n2 copies of @a c are inserted.  If @a pos is beyond
01485        *  end of string, out_of_range is thrown.  If the length of result
01486        *  exceeds max_size(), length_error is thrown.  The value of the string
01487        *  doesn't change if an error is thrown.
01488       */
01489       basic_string&
01490       replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
01491       { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
01492                   _M_limit(__pos, __n1), __n2, __c); }
01493 
01494       /**
01495        *  @brief  Replace range of characters with string.
01496        *  @param i1  Iterator referencing start of range to replace.
01497        *  @param i2  Iterator referencing end of range to replace.
01498        *  @param str  String value to insert.
01499        *  @return  Reference to this string.
01500        *  @throw  std::length_error  If new length exceeds @c max_size().
01501        *
01502        *  Removes the characters in the range [i1,i2).  In place, the value of
01503        *  @a str is inserted.  If the length of result exceeds max_size(),
01504        *  length_error is thrown.  The value of the string doesn't change if
01505        *  an error is thrown.
01506       */
01507       basic_string&
01508       replace(iterator __i1, iterator __i2, const basic_string& __str)
01509       { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
01510 
01511       /**
01512        *  @brief  Replace range of characters with C substring.
01513        *  @param i1  Iterator referencing start of range to replace.
01514        *  @param i2  Iterator referencing end of range to replace.
01515        *  @param s  C string value to insert.
01516        *  @param n  Number of characters from s to insert.
01517        *  @return  Reference to this string.
01518        *  @throw  std::length_error  If new length exceeds @c max_size().
01519        *
01520        *  Removes the characters in the range [i1,i2).  In place, the first @a
01521        *  n characters of @a s are inserted.  If the length of result exceeds
01522        *  max_size(), length_error is thrown.  The value of the string doesn't
01523        *  change if an error is thrown.
01524       */
01525       basic_string&
01526       replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
01527       {
01528     _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
01529                  && __i2 <= _M_iend());
01530     return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
01531       }
01532 
01533       /**
01534        *  @brief  Replace range of characters with C string.
01535        *  @param i1  Iterator referencing start of range to replace.
01536        *  @param i2  Iterator referencing end of range to replace.
01537        *  @param s  C string value to insert.
01538        *  @return  Reference to this string.
01539        *  @throw  std::length_error  If new length exceeds @c max_size().
01540        *
01541        *  Removes the characters in the range [i1,i2).  In place, the
01542        *  characters of @a s are inserted.  If the length of result exceeds
01543        *  max_size(), length_error is thrown.  The value of the string doesn't
01544        *  change if an error is thrown.
01545       */
01546       basic_string&
01547       replace(iterator __i1, iterator __i2, const _CharT* __s)
01548       {
01549     __glibcxx_requires_string(__s);
01550     return this->replace(__i1, __i2, __s, traits_type::length(__s));
01551       }
01552 
01553       /**
01554        *  @brief  Replace range of characters with multiple characters
01555        *  @param i1  Iterator referencing start of range to replace.
01556        *  @param i2  Iterator referencing end of range to replace.
01557        *  @param n  Number of characters to insert.
01558        *  @param c  Character to insert.
01559        *  @return  Reference to this string.
01560        *  @throw  std::length_error  If new length exceeds @c max_size().
01561        *
01562        *  Removes the characters in the range [i1,i2).  In place, @a n copies
01563        *  of @a c are inserted.  If the length of result exceeds max_size(),
01564        *  length_error is thrown.  The value of the string doesn't change if
01565        *  an error is thrown.
01566       */
01567       basic_string&
01568       replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
01569       {
01570     _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
01571                  && __i2 <= _M_iend());
01572     return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
01573       }
01574 
01575       /**
01576        *  @brief  Replace range of characters with range.
01577        *  @param i1  Iterator referencing start of range to replace.
01578        *  @param i2  Iterator referencing end of range to replace.
01579        *  @param k1  Iterator referencing start of range to insert.
01580        *  @param k2  Iterator referencing end of range to insert.
01581        *  @return  Reference to this string.
01582        *  @throw  std::length_error  If new length exceeds @c max_size().
01583        *
01584        *  Removes the characters in the range [i1,i2).  In place, characters
01585        *  in the range [k1,k2) are inserted.  If the length of result exceeds
01586        *  max_size(), length_error is thrown.  The value of the string doesn't
01587        *  change if an error is thrown.
01588       */
01589       template<class _InputIterator>
01590         basic_string&
01591         replace(iterator __i1, iterator __i2,
01592         _InputIterator __k1, _InputIterator __k2)
01593         {
01594       _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
01595                    && __i2 <= _M_iend());
01596       __glibcxx_requires_valid_range(__k1, __k2);
01597       typedef typename std::__is_integer<_InputIterator>::__type _Integral;
01598       return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
01599     }
01600 
01601       // Specializations for the common case of pointer and iterator:
01602       // useful to avoid the overhead of temporary buffering in _M_replace.
01603       basic_string&
01604       replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
01605       {
01606     _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
01607                  && __i2 <= _M_iend());
01608     __glibcxx_requires_valid_range(__k1, __k2);
01609     return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
01610                  __k1, __k2 - __k1);
01611       }
01612 
01613       basic_string&
01614       replace(iterator __i1, iterator __i2,
01615           const _CharT* __k1, const _CharT* __k2)
01616       {
01617     _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
01618                  && __i2 <= _M_iend());
01619     __glibcxx_requires_valid_range(__k1, __k2);
01620     return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
01621                  __k1, __k2 - __k1);
01622       }
01623 
01624       basic_string&
01625       replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
01626       {
01627     _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
01628                  && __i2 <= _M_iend());
01629     __glibcxx_requires_valid_range(__k1, __k2);
01630     return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
01631                  __k1.base(), __k2 - __k1);
01632       }
01633 
01634       basic_string&
01635       replace(iterator __i1, iterator __i2,
01636           const_iterator __k1, const_iterator __k2)
01637       {
01638     _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
01639                  && __i2 <= _M_iend());
01640     __glibcxx_requires_valid_range(__k1, __k2);
01641     return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
01642                  __k1.base(), __k2 - __k1);
01643       }
01644       
01645 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01646       /**
01647        *  @brief  Replace range of characters with initializer_list.
01648        *  @param i1  Iterator referencing start of range to replace.
01649        *  @param i2  Iterator referencing end of range to replace.
01650        *  @param l  The initializer_list of characters to insert.
01651        *  @return  Reference to this string.
01652        *  @throw  std::length_error  If new length exceeds @c max_size().
01653        *
01654        *  Removes the characters in the range [i1,i2).  In place, characters
01655        *  in the range [k1,k2) are inserted.  If the length of result exceeds
01656        *  max_size(), length_error is thrown.  The value of the string doesn't
01657        *  change if an error is thrown.
01658       */
01659       basic_string& replace(iterator __i1, iterator __i2,
01660                 initializer_list<_CharT> __l)
01661       { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
01662 #endif // __GXX_EXPERIMENTAL_CXX0X__
01663 
01664     private:
01665       template<class _Integer>
01666     basic_string&
01667     _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
01668                 _Integer __val, __true_type)
01669         { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
01670 
01671       template<class _InputIterator>
01672     basic_string&
01673     _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
01674                 _InputIterator __k2, __false_type);
01675 
01676       basic_string&
01677       _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
01678              _CharT __c);
01679 
01680       basic_string&
01681       _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
01682               size_type __n2);
01683 
01684       // _S_construct_aux is used to implement the 21.3.1 para 15 which
01685       // requires special behaviour if _InIter is an integral type
01686       template<class _InIterator>
01687         static _CharT*
01688         _S_construct_aux(_InIterator __beg, _InIterator __end,
01689              const _Alloc& __a, __false_type)
01690     {
01691           typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
01692           return _S_construct(__beg, __end, __a, _Tag());
01693     }
01694 
01695       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01696       // 438. Ambiguity in the "do the right thing" clause
01697       template<class _Integer>
01698         static _CharT*
01699         _S_construct_aux(_Integer __beg, _Integer __end,
01700              const _Alloc& __a, __true_type)
01701         { return _S_construct_aux_2(static_cast<size_type>(__beg),
01702                     __end, __a); }
01703 
01704       static _CharT*
01705       _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
01706       { return _S_construct(__req, __c, __a); }
01707 
01708       template<class _InIterator>
01709         static _CharT*
01710         _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
01711     {
01712       typedef typename std::__is_integer<_InIterator>::__type _Integral;
01713       return _S_construct_aux(__beg, __end, __a, _Integral());
01714         }
01715 
01716       // For Input Iterators, used in istreambuf_iterators, etc.
01717       template<class _InIterator>
01718         static _CharT*
01719          _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
01720               input_iterator_tag);
01721 
01722       // For forward_iterators up to random_access_iterators, used for
01723       // string::iterator, _CharT*, etc.
01724       template<class _FwdIterator>
01725         static _CharT*
01726         _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
01727              forward_iterator_tag);
01728 
01729       static _CharT*
01730       _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
01731 
01732     public:
01733 
01734       /**
01735        *  @brief  Copy substring into C string.
01736        *  @param s  C string to copy value into.
01737        *  @param n  Number of characters to copy.
01738        *  @param pos  Index of first character to copy.
01739        *  @return  Number of characters actually copied
01740        *  @throw  std::out_of_range  If pos > size().
01741        *
01742        *  Copies up to @a n characters starting at @a pos into the C string @a
01743        *  s.  If @a pos is %greater than size(), out_of_range is thrown.
01744       */
01745       size_type
01746       copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
01747 
01748       /**
01749        *  @brief  Swap contents with another string.
01750        *  @param s  String to swap with.
01751        *
01752        *  Exchanges the contents of this string with that of @a s in constant
01753        *  time.
01754       */
01755       void
01756       swap(basic_string& __s);
01757 
01758       // String operations:
01759       /**
01760        *  @brief  Return const pointer to null-terminated contents.
01761        *
01762        *  This is a handle to internal data.  Do not modify or dire things may
01763        *  happen.
01764       */
01765       const _CharT*
01766       c_str() const
01767       { return _M_data(); }
01768 
01769       /**
01770        *  @brief  Return const pointer to contents.
01771        *
01772        *  This is a handle to internal data.  Do not modify or dire things may
01773        *  happen.
01774       */
01775       const _CharT*
01776       data() const
01777       { return _M_data(); }
01778 
01779       /**
01780        *  @brief  Return copy of allocator used to construct this string.
01781       */
01782       allocator_type
01783       get_allocator() const
01784       { return _M_dataplus; }
01785 
01786       /**
01787        *  @brief  Find position of a C substring.
01788        *  @param s  C string to locate.
01789        *  @param pos  Index of character to search from.
01790        *  @param n  Number of characters from @a s to search for.
01791        *  @return  Index of start of first occurrence.
01792        *
01793        *  Starting from @a pos, searches forward for the first @a n characters
01794        *  in @a s within this string.  If found, returns the index where it
01795        *  begins.  If not found, returns npos.
01796       */
01797       size_type
01798       find(const _CharT* __s, size_type __pos, size_type __n) const;
01799 
01800       /**
01801        *  @brief  Find position of a string.
01802        *  @param str  String to locate.
01803        *  @param pos  Index of character to search from (default 0).
01804        *  @return  Index of start of first occurrence.
01805        *
01806        *  Starting from @a pos, searches forward for value of @a str within
01807        *  this string.  If found, returns the index where it begins.  If not
01808        *  found, returns npos.
01809       */
01810       size_type
01811       find(const basic_string& __str, size_type __pos = 0) const
01812       { return this->find(__str.data(), __pos, __str.size()); }
01813 
01814       /**
01815        *  @brief  Find position of a C string.
01816        *  @param s  C string to locate.
01817        *  @param pos  Index of character to search from (default 0).
01818        *  @return  Index of start of first occurrence.
01819        *
01820        *  Starting from @a pos, searches forward for the value of @a s within
01821        *  this string.  If found, returns the index where it begins.  If not
01822        *  found, returns npos.
01823       */
01824       size_type
01825       find(const _CharT* __s, size_type __pos = 0) const
01826       {
01827     __glibcxx_requires_string(__s);
01828     return this->find(__s, __pos, traits_type::length(__s));
01829       }
01830 
01831       /**
01832        *  @brief  Find position of a character.
01833        *  @param c  Character to locate.
01834        *  @param pos  Index of character to search from (default 0).
01835        *  @return  Index of first occurrence.
01836        *
01837        *  Starting from @a pos, searches forward for @a c within this string.
01838        *  If found, returns the index where it was found.  If not found,
01839        *  returns npos.
01840       */
01841       size_type
01842       find(_CharT __c, size_type __pos = 0) const;
01843 
01844       /**
01845        *  @brief  Find last position of a string.
01846        *  @param str  String to locate.
01847        *  @param pos  Index of character to search back from (default end).
01848        *  @return  Index of start of last occurrence.
01849        *
01850        *  Starting from @a pos, searches backward for value of @a str within
01851        *  this string.  If found, returns the index where it begins.  If not
01852        *  found, returns npos.
01853       */
01854       size_type
01855       rfind(const basic_string& __str, size_type __pos = npos) const
01856       { return this->rfind(__str.data(), __pos, __str.size()); }
01857 
01858       /**
01859        *  @brief  Find last position of a C substring.
01860        *  @param s  C string to locate.
01861        *  @param pos  Index of character to search back from.
01862        *  @param n  Number of characters from s to search for.
01863        *  @return  Index of start of last occurrence.
01864        *
01865        *  Starting from @a pos, searches backward for the first @a n
01866        *  characters in @a s within this string.  If found, returns the index
01867        *  where it begins.  If not found, returns npos.
01868       */
01869       size_type
01870       rfind(const _CharT* __s, size_type __pos, size_type __n) const;
01871 
01872       /**
01873        *  @brief  Find last position of a C string.
01874        *  @param s  C string to locate.
01875        *  @param pos  Index of character to start search at (default end).
01876        *  @return  Index of start of  last occurrence.
01877        *
01878        *  Starting from @a pos, searches backward for the value of @a s within
01879        *  this string.  If found, returns the index where it begins.  If not
01880        *  found, returns npos.
01881       */
01882       size_type
01883       rfind(const _CharT* __s, size_type __pos = npos) const
01884       {
01885     __glibcxx_requires_string(__s);
01886     return this->rfind(__s, __pos, traits_type::length(__s));
01887       }
01888 
01889       /**
01890        *  @brief  Find last position of a character.
01891        *  @param c  Character to locate.
01892        *  @param pos  Index of character to search back from (default end).
01893        *  @return  Index of last occurrence.
01894        *
01895        *  Starting from @a pos, searches backward for @a c within this string.
01896        *  If found, returns the index where it was found.  If not found,
01897        *  returns npos.
01898       */
01899       size_type
01900       rfind(_CharT __c, size_type __pos = npos) const;
01901 
01902       /**
01903        *  @brief  Find position of a character of string.
01904        *  @param str  String containing characters to locate.
01905        *  @param pos  Index of character to search from (default 0).
01906        *  @return  Index of first occurrence.
01907        *
01908        *  Starting from @a pos, searches forward for one of the characters of
01909        *  @a str within this string.  If found, returns the index where it was
01910        *  found.  If not found, returns npos.
01911       */
01912       size_type
01913       find_first_of(const basic_string& __str, size_type __pos = 0) const
01914       { return this->find_first_of(__str.data(), __pos, __str.size()); }
01915 
01916       /**
01917        *  @brief  Find position of a character of C substring.
01918        *  @param s  String containing characters to locate.
01919        *  @param pos  Index of character to search from.
01920        *  @param n  Number of characters from s to search for.
01921        *  @return  Index of first occurrence.
01922        *
01923        *  Starting from @a pos, searches forward for one of the first @a n
01924        *  characters of @a s within this string.  If found, returns the index
01925        *  where it was found.  If not found, returns npos.
01926       */
01927       size_type
01928       find_first_of(const _CharT* __s, size_type __pos, size_type __n) const;
01929 
01930       /**
01931        *  @brief  Find position of a character of C string.
01932        *  @param s  String containing characters to locate.
01933        *  @param pos  Index of character to search from (default 0).
01934        *  @return  Index of first occurrence.
01935        *
01936        *  Starting from @a pos, searches forward for one of the characters of
01937        *  @a s within this string.  If found, returns the index where it was
01938        *  found.  If not found, returns npos.
01939       */
01940       size_type
01941       find_first_of(const _CharT* __s, size_type __pos = 0) const
01942       {
01943     __glibcxx_requires_string(__s);
01944     return this->find_first_of(__s, __pos, traits_type::length(__s));
01945       }
01946 
01947       /**
01948        *  @brief  Find position of a character.
01949        *  @param c  Character to locate.
01950        *  @param pos  Index of character to search from (default 0).
01951        *  @return  Index of first occurrence.
01952        *
01953        *  Starting from @a pos, searches forward for the character @a c within
01954        *  this string.  If found, returns the index where it was found.  If
01955        *  not found, returns npos.
01956        *
01957        *  Note: equivalent to find(c, pos).
01958       */
01959       size_type
01960       find_first_of(_CharT __c, size_type __pos = 0) const
01961       { return this->find(__c, __pos); }
01962 
01963       /**
01964        *  @brief  Find last position of a character of string.
01965        *  @param str  String containing characters to locate.
01966        *  @param pos  Index of character to search back from (default end).
01967        *  @return  Index of last occurrence.
01968        *
01969        *  Starting from @a pos, searches backward for one of the characters of
01970        *  @a str within this string.  If found, returns the index where it was
01971        *  found.  If not found, returns npos.
01972       */
01973       size_type
01974       find_last_of(const basic_string& __str, size_type __pos = npos) const
01975       { return this->find_last_of(__str.data(), __pos, __str.size()); }
01976 
01977       /**
01978        *  @brief  Find last position of a character of C substring.
01979        *  @param s  C string containing characters to locate.
01980        *  @param pos  Index of character to search back from.
01981        *  @param n  Number of characters from s to search for.
01982        *  @return  Index of last occurrence.
01983        *
01984        *  Starting from @a pos, searches backward for one of the first @a n
01985        *  characters of @a s within this string.  If found, returns the index
01986        *  where it was found.  If not found, returns npos.
01987       */
01988       size_type
01989       find_last_of(const _CharT* __s, size_type __pos, size_type __n) const;
01990 
01991       /**
01992        *  @brief  Find last position of a character of C string.
01993        *  @param s  C string containing characters to locate.
01994        *  @param pos  Index of character to search back from (default end).
01995        *  @return  Index of last occurrence.
01996        *
01997        *  Starting from @a pos, searches backward for one of the characters of
01998        *  @a s within this string.  If found, returns the index where it was
01999        *  found.  If not found, returns npos.
02000       */
02001       size_type
02002       find_last_of(const _CharT* __s, size_type __pos = npos) const
02003       {
02004     __glibcxx_requires_string(__s);
02005     return this->find_last_of(__s, __pos, traits_type::length(__s));
02006       }
02007 
02008       /**
02009        *  @brief  Find last position of a character.
02010        *  @param c  Character to locate.
02011        *  @param pos  Index of character to search back from (default end).
02012        *  @return  Index of last occurrence.
02013        *
02014        *  Starting from @a pos, searches backward for @a c within this string.
02015        *  If found, returns the index where it was found.  If not found,
02016        *  returns npos.
02017        *
02018        *  Note: equivalent to rfind(c, pos).
02019       */
02020       size_type
02021       find_last_of(_CharT __c, size_type __pos = npos) const
02022       { return this->rfind(__c, __pos); }
02023 
02024       /**
02025        *  @brief  Find position of a character not in string.
02026        *  @param str  String containing characters to avoid.
02027        *  @param pos  Index of character to search from (default 0).
02028        *  @return  Index of first occurrence.
02029        *
02030        *  Starting from @a pos, searches forward for a character not contained
02031        *  in @a str within this string.  If found, returns the index where it
02032        *  was found.  If not found, returns npos.
02033       */
02034       size_type
02035       find_first_not_of(const basic_string& __str, size_type __pos = 0) const
02036       { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
02037 
02038       /**
02039        *  @brief  Find position of a character not in C substring.
02040        *  @param s  C string containing characters to avoid.
02041        *  @param pos  Index of character to search from.
02042        *  @param n  Number of characters from s to consider.
02043        *  @return  Index of first occurrence.
02044        *
02045        *  Starting from @a pos, searches forward for a character not contained
02046        *  in the first @a n characters of @a s within this string.  If found,
02047        *  returns the index where it was found.  If not found, returns npos.
02048       */
02049       size_type
02050       find_first_not_of(const _CharT* __s, size_type __pos,
02051             size_type __n) const;
02052 
02053       /**
02054        *  @brief  Find position of a character not in C string.
02055        *  @param s  C string containing characters to avoid.
02056        *  @param pos  Index of character to search from (default 0).
02057        *  @return  Index of first occurrence.
02058        *
02059        *  Starting from @a pos, searches forward for a character not contained
02060        *  in @a s within this string.  If found, returns the index where it
02061        *  was found.  If not found, returns npos.
02062       */
02063       size_type
02064       find_first_not_of(const _CharT* __s, size_type __pos = 0) const
02065       {
02066     __glibcxx_requires_string(__s);
02067     return this->find_first_not_of(__s, __pos, traits_type::length(__s));
02068       }
02069 
02070       /**
02071        *  @brief  Find position of a different character.
02072        *  @param c  Character to avoid.
02073        *  @param pos  Index of character to search from (default 0).
02074        *  @return  Index of first occurrence.
02075        *
02076        *  Starting from @a pos, searches forward for a character other than @a c
02077        *  within this string.  If found, returns the index where it was found.
02078        *  If not found, returns npos.
02079       */
02080       size_type
02081       find_first_not_of(_CharT __c, size_type __pos = 0) const;
02082 
02083       /**
02084        *  @brief  Find last position of a character not in string.
02085        *  @param str  String containing characters to avoid.
02086        *  @param pos  Index of character to search back from (default end).
02087        *  @return  Index of last occurrence.
02088        *
02089        *  Starting from @a pos, searches backward for a character not
02090        *  contained in @a str within this string.  If found, returns the index
02091        *  where it was found.  If not found, returns npos.
02092       */
02093       size_type
02094       find_last_not_of(const basic_string& __str, size_type __pos = npos) const
02095       { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
02096 
02097       /**
02098        *  @brief  Find last position of a character not in C substring.
02099        *  @param s  C string containing characters to avoid.
02100        *  @param pos  Index of character to search back from.
02101        *  @param n  Number of characters from s to consider.
02102        *  @return  Index of last occurrence.
02103        *
02104        *  Starting from @a pos, searches backward for a character not
02105        *  contained in the first @a n characters of @a s within this string.
02106        *  If found, returns the index where it was found.  If not found,
02107        *  returns npos.
02108       */
02109       size_type
02110       find_last_not_of(const _CharT* __s, size_type __pos,
02111                size_type __n) const;
02112       /**
02113        *  @brief  Find last position of a character not in C string.
02114        *  @param s  C string containing characters to avoid.
02115        *  @param pos  Index of character to search back from (default end).
02116        *  @return  Index of last occurrence.
02117        *
02118        *  Starting from @a pos, searches backward for a character not
02119        *  contained in @a s within this string.  If found, returns the index
02120        *  where it was found.  If not found, returns npos.
02121       */
02122       size_type
02123       find_last_not_of(const _CharT* __s, size_type __pos = npos) const
02124       {
02125     __glibcxx_requires_string(__s);
02126     return this->find_last_not_of(__s, __pos, traits_type::length(__s));
02127       }
02128 
02129       /**
02130        *  @brief  Find last position of a different character.
02131        *  @param c  Character to avoid.
02132        *  @param pos  Index of character to search back from (default end).
02133        *  @return  Index of last occurrence.
02134        *
02135        *  Starting from @a pos, searches backward for a character other than
02136        *  @a c within this string.  If found, returns the index where it was
02137        *  found.  If not found, returns npos.
02138       */
02139       size_type
02140       find_last_not_of(_CharT __c, size_type __pos = npos) const;
02141 
02142       /**
02143        *  @brief  Get a substring.
02144        *  @param pos  Index of first character (default 0).
02145        *  @param n  Number of characters in substring (default remainder).
02146        *  @return  The new string.
02147        *  @throw  std::out_of_range  If pos > size().
02148        *
02149        *  Construct and return a new string using the @a n characters starting
02150        *  at @a pos.  If the string is too short, use the remainder of the
02151        *  characters.  If @a pos is beyond the end of the string, out_of_range
02152        *  is thrown.
02153       */
02154       basic_string
02155       substr(size_type __pos = 0, size_type __n = npos) const
02156       { return basic_string(*this,
02157                 _M_check(__pos, "basic_string::substr"), __n); }
02158 
02159       /**
02160        *  @brief  Compare to a string.
02161        *  @param str  String to compare against.
02162        *  @return  Integer < 0, 0, or > 0.
02163        *
02164        *  Returns an integer < 0 if this string is ordered before @a str, 0 if
02165        *  their values are equivalent, or > 0 if this string is ordered after
02166        *  @a str.  Determines the effective length rlen of the strings to
02167        *  compare as the smallest of size() and str.size().  The function
02168        *  then compares the two strings by calling traits::compare(data(),
02169        *  str.data(),rlen).  If the result of the comparison is nonzero returns
02170        *  it, otherwise the shorter one is ordered first.
02171       */
02172       int
02173       compare(const basic_string& __str) const
02174       {
02175     const size_type __size = this->size();
02176     const size_type __osize = __str.size();
02177     const size_type __len = std::min(__size, __osize);
02178 
02179     int __r = traits_type::compare(_M_data(), __str.data(), __len);
02180     if (!__r)
02181       __r = _S_compare(__size, __osize);
02182     return __r;
02183       }
02184 
02185       /**
02186        *  @brief  Compare substring to a string.
02187        *  @param pos  Index of first character of substring.
02188        *  @param n  Number of characters in substring.
02189        *  @param str  String to compare against.
02190        *  @return  Integer < 0, 0, or > 0.
02191        *
02192        *  Form the substring of this string from the @a n characters starting
02193        *  at @a pos.  Returns an integer < 0 if the substring is ordered
02194        *  before @a str, 0 if their values are equivalent, or > 0 if the
02195        *  substring is ordered after @a str.  Determines the effective length
02196        *  rlen of the strings to compare as the smallest of the length of the
02197        *  substring and @a str.size().  The function then compares the two
02198        *  strings by calling traits::compare(substring.data(),str.data(),rlen).
02199        *  If the result of the comparison is nonzero returns it, otherwise the
02200        *  shorter one is ordered first.
02201       */
02202       int
02203       compare(size_type __pos, size_type __n, const basic_string& __str) const;
02204 
02205       /**
02206        *  @brief  Compare substring to a substring.
02207        *  @param pos1  Index of first character of substring.
02208        *  @param n1  Number of characters in substring.
02209        *  @param str  String to compare against.
02210        *  @param pos2  Index of first character of substring of str.
02211        *  @param n2  Number of characters in substring of str.
02212        *  @return  Integer < 0, 0, or > 0.
02213        *
02214        *  Form the substring of this string from the @a n1 characters starting
02215        *  at @a pos1.  Form the substring of @a str from the @a n2 characters
02216        *  starting at @a pos2.  Returns an integer < 0 if this substring is
02217        *  ordered before the substring of @a str, 0 if their values are
02218        *  equivalent, or > 0 if this substring is ordered after the substring
02219        *  of @a str.  Determines the effective length rlen of the strings
02220        *  to compare as the smallest of the lengths of the substrings.  The
02221        *  function then compares the two strings by calling
02222        *  traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
02223        *  If the result of the comparison is nonzero returns it, otherwise the
02224        *  shorter one is ordered first.
02225       */
02226       int
02227       compare(size_type __pos1, size_type __n1, const basic_string& __str,
02228           size_type __pos2, size_type __n2) const;
02229 
02230       /**
02231        *  @brief  Compare to a C string.
02232        *  @param s  C string to compare against.
02233        *  @return  Integer < 0, 0, or > 0.
02234        *
02235        *  Returns an integer < 0 if this string is ordered before @a s, 0 if
02236        *  their values are equivalent, or > 0 if this string is ordered after
02237        *  @a s.  Determines the effective length rlen of the strings to
02238        *  compare as the smallest of size() and the length of a string
02239        *  constructed from @a s.  The function then compares the two strings
02240        *  by calling traits::compare(data(),s,rlen).  If the result of the
02241        *  comparison is nonzero returns it, otherwise the shorter one is
02242        *  ordered first.
02243       */
02244       int
02245       compare(const _CharT* __s) const;
02246 
02247       // _GLIBCXX_RESOLVE_LIB_DEFECTS
02248       // 5 String::compare specification questionable
02249       /**
02250        *  @brief  Compare substring to a C string.
02251        *  @param pos  Index of first character of substring.
02252        *  @param n1  Number of characters in substring.
02253        *  @param s  C string to compare against.
02254        *  @return  Integer < 0, 0, or > 0.
02255        *
02256        *  Form the substring of this string from the @a n1 characters starting
02257        *  at @a pos.  Returns an integer < 0 if the substring is ordered
02258        *  before @a s, 0 if their values are equivalent, or > 0 if the
02259        *  substring is ordered after @a s.  Determines the effective length
02260        *  rlen of the strings to compare as the smallest of the length of the 
02261        *  substring and the length of a string constructed from @a s.  The
02262        *  function then compares the two string by calling
02263        *  traits::compare(substring.data(),s,rlen).  If the result of the
02264        *  comparison is nonzero returns it, otherwise the shorter one is
02265        *  ordered first.
02266       */
02267       int
02268       compare(size_type __pos, size_type __n1, const _CharT* __s) const;
02269 
02270       /**
02271        *  @brief  Compare substring against a character %array.
02272        *  @param pos1  Index of first character of substring.
02273        *  @param n1  Number of characters in substring.
02274        *  @param s  character %array to compare against.
02275        *  @param n2  Number of characters of s.
02276        *  @return  Integer < 0, 0, or > 0.
02277        *
02278        *  Form the substring of this string from the @a n1 characters starting
02279        *  at @a pos1.  Form a string from the first @a n2 characters of @a s.
02280        *  Returns an integer < 0 if this substring is ordered before the string
02281        *  from @a s, 0 if their values are equivalent, or > 0 if this substring
02282        *  is ordered after the string from @a s.   Determines the effective
02283        *  length rlen of the strings to compare as the smallest of the length
02284        *  of the substring and @a n2.  The function then compares the two
02285        *  strings by calling traits::compare(substring.data(),s,rlen).  If the
02286        *  result of the comparison is nonzero returns it, otherwise the shorter
02287        *  one is ordered first.
02288        *
02289        *  NB: s must have at least n2 characters, &apos;\\0&apos; has
02290        *  no special meaning.
02291       */
02292       int
02293       compare(size_type __pos, size_type __n1, const _CharT* __s,
02294           size_type __n2) const;
02295   };
02296 
02297   // operator+
02298   /**
02299    *  @brief  Concatenate two strings.
02300    *  @param lhs  First string.
02301    *  @param rhs  Last string.
02302    *  @return  New string with value of @a lhs followed by @a rhs.
02303    */
02304   template<typename _CharT, typename _Traits, typename _Alloc>
02305     basic_string<_CharT, _Traits, _Alloc>
02306     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02307           const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02308     {
02309       basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
02310       __str.append(__rhs);
02311       return __str;
02312     }
02313 
02314   /**
02315    *  @brief  Concatenate C string and string.
02316    *  @param lhs  First string.
02317    *  @param rhs  Last string.
02318    *  @return  New string with value of @a lhs followed by @a rhs.
02319    */
02320   template<typename _CharT, typename _Traits, typename _Alloc>
02321     basic_string<_CharT,_Traits,_Alloc>
02322     operator+(const _CharT* __lhs,
02323           const basic_string<_CharT,_Traits,_Alloc>& __rhs);
02324 
02325   /**
02326    *  @brief  Concatenate character and string.
02327    *  @param lhs  First string.
02328    *  @param rhs  Last string.
02329    *  @return  New string with @a lhs followed by @a rhs.
02330    */
02331   template<typename _CharT, typename _Traits, typename _Alloc>
02332     basic_string<_CharT,_Traits,_Alloc>
02333     operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
02334 
02335   /**
02336    *  @brief  Concatenate string and C string.
02337    *  @param lhs  First string.
02338    *  @param rhs  Last string.
02339    *  @return  New string with @a lhs followed by @a rhs.
02340    */
02341   template<typename _CharT, typename _Traits, typename _Alloc>
02342     inline basic_string<_CharT, _Traits, _Alloc>
02343     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02344          const _CharT* __rhs)
02345     {
02346       basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
02347       __str.append(__rhs);
02348       return __str;
02349     }
02350 
02351   /**
02352    *  @brief  Concatenate string and character.
02353    *  @param lhs  First string.
02354    *  @param rhs  Last string.
02355    *  @return  New string with @a lhs followed by @a rhs.
02356    */
02357   template<typename _CharT, typename _Traits, typename _Alloc>
02358     inline basic_string<_CharT, _Traits, _Alloc>
02359     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
02360     {
02361       typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
02362       typedef typename __string_type::size_type     __size_type;
02363       __string_type __str(__lhs);
02364       __str.append(__size_type(1), __rhs);
02365       return __str;
02366     }
02367 
02368 #ifdef __GXX_EXPERIMENTAL_CXX0X__
02369   template<typename _CharT, typename _Traits, typename _Alloc>
02370     inline basic_string<_CharT, _Traits, _Alloc>
02371     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
02372           const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02373     { return std::move(__lhs.append(__rhs)); }
02374 
02375   template<typename _CharT, typename _Traits, typename _Alloc>
02376     inline basic_string<_CharT, _Traits, _Alloc>
02377     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02378           basic_string<_CharT, _Traits, _Alloc>&& __rhs)
02379     { return std::move(__rhs.insert(0, __lhs)); }
02380 
02381   template<typename _CharT, typename _Traits, typename _Alloc>
02382     inline basic_string<_CharT, _Traits, _Alloc>
02383     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
02384           basic_string<_CharT, _Traits, _Alloc>&& __rhs)
02385     {
02386       const auto __size = __lhs.size() + __rhs.size();
02387       const bool __cond = (__size > __lhs.capacity()
02388                && __size <= __rhs.capacity());
02389       return __cond ? std::move(__rhs.insert(0, __lhs))
02390                 : std::move(__lhs.append(__rhs));
02391     }
02392 
02393   template<typename _CharT, typename _Traits, typename _Alloc>
02394     inline basic_string<_CharT, _Traits, _Alloc>
02395     operator+(const _CharT* __lhs,
02396           basic_string<_CharT, _Traits, _Alloc>&& __rhs)
02397     { return std::move(__rhs.insert(0, __lhs)); }
02398 
02399   template<typename _CharT, typename _Traits, typename _Alloc>
02400     inline basic_string<_CharT, _Traits, _Alloc>
02401     operator+(_CharT __lhs,
02402           basic_string<_CharT, _Traits, _Alloc>&& __rhs)
02403     { return std::move(__rhs.insert(0, 1, __lhs)); }
02404 
02405   template<typename _CharT, typename _Traits, typename _Alloc>
02406     inline basic_string<_CharT, _Traits, _Alloc>
02407     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
02408           const _CharT* __rhs)
02409     { return std::move(__lhs.append(__rhs)); }
02410 
02411   template<typename _CharT, typename _Traits, typename _Alloc>
02412     inline basic_string<_CharT, _Traits, _Alloc>
02413     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
02414           _CharT __rhs)
02415     { return std::move(__lhs.append(1, __rhs)); }
02416 #endif
02417 
02418   // operator ==
02419   /**
02420    *  @brief  Test equivalence of two strings.
02421    *  @param lhs  First string.
02422    *  @param rhs  Second string.
02423    *  @return  True if @a lhs.compare(@a rhs) == 0.  False otherwise.
02424    */
02425   template<typename _CharT, typename _Traits, typename _Alloc>
02426     inline bool
02427     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02428            const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02429     { return __lhs.compare(__rhs) == 0; }
02430 
02431   template<typename _CharT>
02432     inline
02433     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
02434     operator==(const basic_string<_CharT>& __lhs,
02435            const basic_string<_CharT>& __rhs)
02436     { return (__lhs.size() == __rhs.size()
02437           && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
02438                             __lhs.size())); }
02439 
02440   /**
02441    *  @brief  Test equivalence of C string and string.
02442    *  @param lhs  C string.
02443    *  @param rhs  String.
02444    *  @return  True if @a rhs.compare(@a lhs) == 0.  False otherwise.
02445    */
02446   template<typename _CharT, typename _Traits, typename _Alloc>
02447     inline bool
02448     operator==(const _CharT* __lhs,
02449            const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02450     { return __rhs.compare(__lhs) == 0; }
02451 
02452   /**
02453    *  @brief  Test equivalence of string and C string.
02454    *  @param lhs  String.
02455    *  @param rhs  C string.
02456    *  @return  True if @a lhs.compare(@a rhs) == 0.  False otherwise.
02457    */
02458   template<typename _CharT, typename _Traits, typename _Alloc>
02459     inline bool
02460     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02461            const _CharT* __rhs)
02462     { return __lhs.compare(__rhs) == 0; }
02463 
02464   // operator !=
02465   /**
02466    *  @brief  Test difference of two strings.
02467    *  @param lhs  First string.
02468    *  @param rhs  Second string.
02469    *  @return  True if @a lhs.compare(@a rhs) != 0.  False otherwise.
02470    */
02471   template<typename _CharT, typename _Traits, typename _Alloc>
02472     inline bool
02473     operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02474            const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02475     { return !(__lhs == __rhs); }
02476 
02477   /**
02478    *  @brief  Test difference of C string and string.
02479    *  @param lhs  C string.
02480    *  @param rhs  String.
02481    *  @return  True if @a rhs.compare(@a lhs) != 0.  False otherwise.
02482    */
02483   template<typename _CharT, typename _Traits, typename _Alloc>
02484     inline bool
02485     operator!=(const _CharT* __lhs,
02486            const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02487     { return !(__lhs == __rhs); }
02488 
02489   /**
02490    *  @brief  Test difference of string and C string.
02491    *  @param lhs  String.
02492    *  @param rhs  C string.
02493    *  @return  True if @a lhs.compare(@a rhs) != 0.  False otherwise.
02494    */
02495   template<typename _CharT, typename _Traits, typename _Alloc>
02496     inline bool
02497     operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02498            const _CharT* __rhs)
02499     { return !(__lhs == __rhs); }
02500 
02501   // operator <
02502   /**
02503    *  @brief  Test if string precedes string.
02504    *  @param lhs  First string.
02505    *  @param rhs  Second string.
02506    *  @return  True if @a lhs precedes @a rhs.  False otherwise.
02507    */
02508   template<typename _CharT, typename _Traits, typename _Alloc>
02509     inline bool
02510     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02511           const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02512     { return __lhs.compare(__rhs) < 0; }
02513 
02514   /**
02515    *  @brief  Test if string precedes C string.
02516    *  @param lhs  String.
02517    *  @param rhs  C string.
02518    *  @return  True if @a lhs precedes @a rhs.  False otherwise.
02519    */
02520   template<typename _CharT, typename _Traits, typename _Alloc>
02521     inline bool
02522     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02523           const _CharT* __rhs)
02524     { return __lhs.compare(__rhs) < 0; }
02525 
02526   /**
02527    *  @brief  Test if C string precedes string.
02528    *  @param lhs  C string.
02529    *  @param rhs  String.
02530    *  @return  True if @a lhs precedes @a rhs.  False otherwise.
02531    */
02532   template<typename _CharT, typename _Traits, typename _Alloc>
02533     inline bool
02534     operator<(const _CharT* __lhs,
02535           const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02536     { return __rhs.compare(__lhs) > 0; }
02537 
02538   // operator >
02539   /**
02540    *  @brief  Test if string follows string.
02541    *  @param lhs  First string.
02542    *  @param rhs  Second string.
02543    *  @return  True if @a lhs follows @a rhs.  False otherwise.
02544    */
02545   template<typename _CharT, typename _Traits, typename _Alloc>
02546     inline bool
02547     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02548           const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02549     { return __lhs.compare(__rhs) > 0; }
02550 
02551   /**
02552    *  @brief  Test if string follows C string.
02553    *  @param lhs  String.
02554    *  @param rhs  C string.
02555    *  @return  True if @a lhs follows @a rhs.  False otherwise.
02556    */
02557   template<typename _CharT, typename _Traits, typename _Alloc>
02558     inline bool
02559     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02560           const _CharT* __rhs)
02561     { return __lhs.compare(__rhs) > 0; }
02562 
02563   /**
02564    *  @brief  Test if C string follows string.
02565    *  @param lhs  C string.
02566    *  @param rhs  String.
02567    *  @return  True if @a lhs follows @a rhs.  False otherwise.
02568    */
02569   template<typename _CharT, typename _Traits, typename _Alloc>
02570     inline bool
02571     operator>(const _CharT* __lhs,
02572           const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02573     { return __rhs.compare(__lhs) < 0; }
02574 
02575   // operator <=
02576   /**
02577    *  @brief  Test if string doesn't follow string.
02578    *  @param lhs  First string.
02579    *  @param rhs  Second string.
02580    *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
02581    */
02582   template<typename _CharT, typename _Traits, typename _Alloc>
02583     inline bool
02584     operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02585            const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02586     { return __lhs.compare(__rhs) <= 0; }
02587 
02588   /**
02589    *  @brief  Test if string doesn't follow C string.
02590    *  @param lhs  String.
02591    *  @param rhs  C string.
02592    *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
02593    */
02594   template<typename _CharT, typename _Traits, typename _Alloc>
02595     inline bool
02596     operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02597            const _CharT* __rhs)
02598     { return __lhs.compare(__rhs) <= 0; }
02599 
02600   /**
02601    *  @brief  Test if C string doesn't follow string.
02602    *  @param lhs  C string.
02603    *  @param rhs  String.
02604    *  @return  True if @a lhs doesn't follow @a rhs.  False otherwise.
02605    */
02606   template<typename _CharT, typename _Traits, typename _Alloc>
02607     inline bool
02608     operator<=(const _CharT* __lhs,
02609            const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02610     { return __rhs.compare(__lhs) >= 0; }
02611 
02612   // operator >=
02613   /**
02614    *  @brief  Test if string doesn't precede string.
02615    *  @param lhs  First string.
02616    *  @param rhs  Second string.
02617    *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
02618    */
02619   template<typename _CharT, typename _Traits, typename _Alloc>
02620     inline bool
02621     operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02622            const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02623     { return __lhs.compare(__rhs) >= 0; }
02624 
02625   /**
02626    *  @brief  Test if string doesn't precede C string.
02627    *  @param lhs  String.
02628    *  @param rhs  C string.
02629    *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
02630    */
02631   template<typename _CharT, typename _Traits, typename _Alloc>
02632     inline bool
02633     operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
02634            const _CharT* __rhs)
02635     { return __lhs.compare(__rhs) >= 0; }
02636 
02637   /**
02638    *  @brief  Test if C string doesn't precede string.
02639    *  @param lhs  C string.
02640    *  @param rhs  String.
02641    *  @return  True if @a lhs doesn't precede @a rhs.  False otherwise.
02642    */
02643   template<typename _CharT, typename _Traits, typename _Alloc>
02644     inline bool
02645     operator>=(const _CharT* __lhs,
02646          const basic_string<_CharT, _Traits, _Alloc>& __rhs)
02647     { return __rhs.compare(__lhs) <= 0; }
02648 
02649   /**
02650    *  @brief  Swap contents of two strings.
02651    *  @param lhs  First string.
02652    *  @param rhs  Second string.
02653    *
02654    *  Exchanges the contents of @a lhs and @a rhs in constant time.
02655    */
02656   template<typename _CharT, typename _Traits, typename _Alloc>
02657     inline void
02658     swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
02659      basic_string<_CharT, _Traits, _Alloc>& __rhs)
02660     { __lhs.swap(__rhs); }
02661 
02662   /**
02663    *  @brief  Read stream into a string.
02664    *  @param is  Input stream.
02665    *  @param str  Buffer to store into.
02666    *  @return  Reference to the input stream.
02667    *
02668    *  Stores characters from @a is into @a str until whitespace is found, the
02669    *  end of the stream is encountered, or str.max_size() is reached.  If
02670    *  is.width() is non-zero, that is the limit on the number of characters
02671    *  stored into @a str.  Any previous contents of @a str are erased.
02672    */
02673   template<typename _CharT, typename _Traits, typename _Alloc>
02674     basic_istream<_CharT, _Traits>&
02675     operator>>(basic_istream<_CharT, _Traits>& __is,
02676            basic_string<_CharT, _Traits, _Alloc>& __str);
02677 
02678   template<>
02679     basic_istream<char>&
02680     operator>>(basic_istream<char>& __is, basic_string<char>& __str);
02681 
02682   /**
02683    *  @brief  Write string to a stream.
02684    *  @param os  Output stream.
02685    *  @param str  String to write out.
02686    *  @return  Reference to the output stream.
02687    *
02688    *  Output characters of @a str into os following the same rules as for
02689    *  writing a C string.
02690    */
02691   template<typename _CharT, typename _Traits, typename _Alloc>
02692     inline basic_ostream<_CharT, _Traits>&
02693     operator<<(basic_ostream<_CharT, _Traits>& __os,
02694            const basic_string<_CharT, _Traits, _Alloc>& __str)
02695     {
02696       // _GLIBCXX_RESOLVE_LIB_DEFECTS
02697       // 586. string inserter not a formatted function
02698       return __ostream_insert(__os, __str.data(), __str.size());
02699     }
02700 
02701   /**
02702    *  @brief  Read a line from stream into a string.
02703    *  @param is  Input stream.
02704    *  @param str  Buffer to store into.
02705    *  @param delim  Character marking end of line.
02706    *  @return  Reference to the input stream.
02707    *
02708    *  Stores characters from @a is into @a str until @a delim is found, the
02709    *  end of the stream is encountered, or str.max_size() is reached.  If
02710    *  is.width() is non-zero, that is the limit on the number of characters
02711    *  stored into @a str.  Any previous contents of @a str are erased.  If @a
02712    *  delim was encountered, it is extracted but not stored into @a str.
02713    */
02714   template<typename _CharT, typename _Traits, typename _Alloc>
02715     basic_istream<_CharT, _Traits>&
02716     getline(basic_istream<_CharT, _Traits>& __is,
02717         basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
02718 
02719   /**
02720    *  @brief  Read a line from stream into a string.
02721    *  @param is  Input stream.
02722    *  @param str  Buffer to store into.
02723    *  @return  Reference to the input stream.
02724    *
02725    *  Stores characters from is into @a str until &apos;\n&apos; is
02726    *  found, the end of the stream is encountered, or str.max_size()
02727    *  is reached.  If is.width() is non-zero, that is the limit on the
02728    *  number of characters stored into @a str.  Any previous contents
02729    *  of @a str are erased.  If end of line was encountered, it is
02730    *  extracted but not stored into @a str.
02731    */
02732   template<typename _CharT, typename _Traits, typename _Alloc>
02733     inline basic_istream<_CharT, _Traits>&
02734     getline(basic_istream<_CharT, _Traits>& __is,
02735         basic_string<_CharT, _Traits, _Alloc>& __str)
02736     { return getline(__is, __str, __is.widen('\n')); }
02737 
02738   template<>
02739     basic_istream<char>&
02740     getline(basic_istream<char>& __in, basic_string<char>& __str,
02741         char __delim);
02742 
02743 #ifdef _GLIBCXX_USE_WCHAR_T
02744   template<>
02745     basic_istream<wchar_t>&
02746     getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
02747         wchar_t __delim);
02748 #endif  
02749 
02750 _GLIBCXX_END_NAMESPACE_VERSION
02751 } // namespace
02752 
02753 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && defined(_GLIBCXX_USE_C99) \
02754      && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
02755 
02756 #include <ext/string_conversions.h>
02757 
02758 namespace std _GLIBCXX_VISIBILITY(default)
02759 {
02760 _GLIBCXX_BEGIN_NAMESPACE_VERSION
02761 
02762   // 21.4 Numeric Conversions [string.conversions].
02763   inline int
02764   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
02765   { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
02766                     __idx, __base); }
02767 
02768   inline long
02769   stol(const string& __str, size_t* __idx = 0, int __base = 10)
02770   { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
02771                  __idx, __base); }
02772 
02773   inline unsigned long
02774   stoul(const string& __str, size_t* __idx = 0, int __base = 10)
02775   { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
02776                  __idx, __base); }
02777 
02778   inline long long
02779   stoll(const string& __str, size_t* __idx = 0, int __base = 10)
02780   { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
02781                  __idx, __base); }
02782 
02783   inline unsigned long long
02784   stoull(const string& __str, size_t* __idx = 0, int __base = 10)
02785   { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
02786                  __idx, __base); }
02787 
02788   // NB: strtof vs strtod.
02789   inline float
02790   stof(const string& __str, size_t* __idx = 0)
02791   { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
02792 
02793   inline double
02794   stod(const string& __str, size_t* __idx = 0)
02795   { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
02796 
02797   inline long double
02798   stold(const string& __str, size_t* __idx = 0)
02799   { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
02800 
02801   // NB: (v)snprintf vs sprintf.
02802 
02803   // DR 1261.
02804   inline string
02805   to_string(int __val)
02806   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
02807                        "%d", __val); }
02808 
02809   inline string
02810   to_string(unsigned __val)
02811   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
02812                        4 * sizeof(unsigned),
02813                        "%u", __val); }
02814 
02815   inline string
02816   to_string(long __val)
02817   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
02818                        "%ld", __val); }
02819 
02820   inline string
02821   to_string(unsigned long __val)
02822   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
02823                        4 * sizeof(unsigned long),
02824                        "%lu", __val); }
02825 
02826   inline string
02827   to_string(long long __val)
02828   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
02829                        4 * sizeof(long long),
02830                        "%lld", __val); }
02831 
02832   inline string
02833   to_string(unsigned long long __val)
02834   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
02835                        4 * sizeof(unsigned long long),
02836                        "%llu", __val); }
02837 
02838   inline string
02839   to_string(float __val)
02840   {
02841     const int __n = 
02842       __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
02843     return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
02844                        "%f", __val);
02845   }
02846 
02847   inline string
02848   to_string(double __val)
02849   {
02850     const int __n = 
02851       __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
02852     return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
02853                        "%f", __val);
02854   }
02855 
02856   inline string
02857   to_string(long double __val)
02858   {
02859     const int __n = 
02860       __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
02861     return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
02862                        "%Lf", __val);
02863   }
02864 
02865 #ifdef _GLIBCXX_USE_WCHAR_T
02866   inline int 
02867   stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
02868   { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
02869                     __idx, __base); }
02870 
02871   inline long 
02872   stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
02873   { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
02874                  __idx, __base); }
02875 
02876   inline unsigned long
02877   stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
02878   { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
02879                  __idx, __base); }
02880 
02881   inline long long
02882   stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
02883   { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
02884                  __idx, __base); }
02885 
02886   inline unsigned long long
02887   stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
02888   { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
02889                  __idx, __base); }
02890 
02891   // NB: wcstof vs wcstod.
02892   inline float
02893   stof(const wstring& __str, size_t* __idx = 0)
02894   { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
02895 
02896   inline double
02897   stod(const wstring& __str, size_t* __idx = 0)
02898   { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
02899 
02900   inline long double
02901   stold(const wstring& __str, size_t* __idx = 0)
02902   { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
02903 
02904   // DR 1261.
02905   inline wstring
02906   to_wstring(int __val)
02907   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
02908                         L"%d", __val); }
02909 
02910   inline wstring
02911   to_wstring(unsigned __val)
02912   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
02913                         4 * sizeof(unsigned),
02914                         L"%u", __val); }
02915 
02916   inline wstring
02917   to_wstring(long __val)
02918   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
02919                         L"%ld", __val); }
02920 
02921   inline wstring
02922   to_wstring(unsigned long __val)
02923   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
02924                         4 * sizeof(unsigned long),
02925                         L"%lu", __val); }
02926 
02927   inline wstring
02928   to_wstring(long long __val)
02929   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
02930                         4 * sizeof(long long),
02931                         L"%lld", __val); }
02932 
02933   inline wstring
02934   to_wstring(unsigned long long __val)
02935   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
02936                         4 * sizeof(unsigned long long),
02937                         L"%llu", __val); }
02938 
02939   inline wstring
02940   to_wstring(float __val)
02941   {
02942     const int __n =
02943       __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
02944     return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
02945                         L"%f", __val);
02946   }
02947 
02948   inline wstring
02949   to_wstring(double __val)
02950   {
02951     const int __n =
02952       __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
02953     return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
02954                         L"%f", __val);
02955   }
02956 
02957   inline wstring
02958   to_wstring(long double __val)
02959   {
02960     const int __n =
02961       __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
02962     return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
02963                         L"%Lf", __val);
02964   }
02965 #endif
02966 
02967 _GLIBCXX_END_NAMESPACE_VERSION
02968 } // namespace
02969 
02970 #endif /* __GXX_EXPERIMENTAL_CXX0X__ && _GLIBCXX_USE_C99 ... */
02971 
02972 #ifdef __GXX_EXPERIMENTAL_CXX0X__
02973 
02974 #include <bits/functional_hash.h>
02975 
02976 namespace std _GLIBCXX_VISIBILITY(default)
02977 {
02978 _GLIBCXX_BEGIN_NAMESPACE_VERSION
02979 
02980   // DR 1182.
02981 
02982 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
02983   /// std::hash specialization for string.
02984   template<>
02985     struct hash<string>
02986     : public __hash_base<size_t, string>
02987     {
02988       size_t
02989       operator()(const string& __s) const
02990       { return std::_Hash_impl::hash(__s.data(), __s.length()); }
02991     };
02992 
02993 #ifdef _GLIBCXX_USE_WCHAR_T
02994   /// std::hash specialization for wstring.
02995   template<>
02996     struct hash<wstring>
02997     : public __hash_base<size_t, wstring>
02998     {
02999       size_t
03000       operator()(const wstring& __s) const
03001       { return std::_Hash_impl::hash(__s.data(),
03002                                      __s.length() * sizeof(wchar_t)); }
03003     };
03004 #endif
03005 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
03006 
03007 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
03008   /// std::hash specialization for u16string.
03009   template<>
03010     struct hash<u16string>
03011     : public __hash_base<size_t, u16string>
03012     {
03013       size_t
03014       operator()(const u16string& __s) const
03015       { return std::_Hash_impl::hash(__s.data(),
03016                                      __s.length() * sizeof(char16_t)); }
03017     };
03018 
03019   /// std::hash specialization for u32string.
03020   template<>
03021     struct hash<u32string>
03022     : public __hash_base<size_t, u32string>
03023     {
03024       size_t
03025       operator()(const u32string& __s) const
03026       { return std::_Hash_impl::hash(__s.data(),
03027                                      __s.length() * sizeof(char32_t)); }
03028     };
03029 #endif
03030 
03031 _GLIBCXX_END_NAMESPACE_VERSION
03032 } // namespace
03033 
03034 #endif /* __GXX_EXPERIMENTAL_CXX0X__ */
03035 
03036 #endif /* _BASIC_STRING_H */