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
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 2, 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 // You should have received a copy of the GNU General Public License along
00019 // with this library; see the file COPYING.  If not, write to the Free
00020 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00021 // USA.
00022 
00023 // As a special exception, you may use this file as part of a free software
00024 // library without restriction.  Specifically, if other files instantiate
00025 // templates or use macros or inline functions from this file, or you compile
00026 // this file and link it with other files to produce an executable, this
00027 // file does not by itself cause the resulting executable to be covered by
00028 // the GNU General Public License.  This exception does not however
00029 // invalidate any other reasons why the executable file might be covered by
00030 // the GNU General Public License.
00031 
00032 /** @file basic_string.h
00033  *  This is an internal header file, included by other library headers.
00034  *  You should not attempt to use it directly.
00035  */
00036 
00037 //
00038 // ISO C++ 14882: 21 Strings library
00039 //
00040 
00041 #ifndef _BASIC_STRING_H
00042 #define _BASIC_STRING_H 1
00043 
00044 #pragma GCC system_header
00045 
00046 #include <ext/atomicity.h>
00047 #include <debug/debug.h>
00048 #include <initializer_list>
00049 
00050 _GLIBCXX_BEGIN_NAMESPACE(std)
00051 
00052   /**
00053    *  @class basic_string basic_string.h <string>
00054    *  @brief  Managing sequences of characters and character-like objects.
00055    *
00056    *  @ingroup Containers
00057    *  @ingroup Sequences
00058    *
00059    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
00060    *  <a href="tables.html#66">reversible container</a>, and a
00061    *  <a href="tables.html#67">sequence</a>.  Of the
00062    *  <a href="tables.html#68">optional sequence requirements</a>, only
00063    *  @c push_back, @c at, and array access are supported.
00064    *
00065    *  @doctodo
00066    *
00067    *
00068    *  Documentation?  What's that?
00069    *  Nathan Myers <ncm@cantrip.org>.
00070    *
00071    *  A string looks like this:
00072    *
00073    *  @code
00074    *                                        [_Rep]
00075    *                                        _M_length
00076    *   [basic_string<char_type>]            _M_capacity
00077    *   _M_dataplus                          _M_refcount
00078    *   _M_p ---------------->               unnamed array of char_type
00079    *  @endcode
00080    *
00081    *  Where the _M_p points to the first character in the string, and
00082    *  you cast it to a pointer-to-_Rep and subtract 1 to get a
00083    *  pointer to the header.
00084    *
00085    *  This approach has the enormous advantage that a string object
00086    *  requires only one allocation.  All the ugliness is confined
00087    *  within a single pair of inline functions, which each compile to
00088    *  a single "add" instruction: _Rep::_M_data(), and
00089    *  string::_M_rep(); and the allocation function which gets a
00090    *  block of raw bytes and with room enough and constructs a _Rep
00091    *  object at the front.
00092    *
00093    *  The reason you want _M_data pointing to the character array and
00094    *  not the _Rep is so that the debugger can see the string
00095    *  contents. (Probably we should add a non-inline member to get
00096    *  the _Rep for the debugger to use, so users can check the actual
00097    *  string length.)
00098    *
00099    *  Note that the _Rep object is a POD so that you can have a
00100    *  static "empty string" _Rep object already "constructed" before
00101    *  static constructors have run.  The reference-count encoding is
00102    *  chosen so that a 0 indicates one reference, so you never try to
00103    *  destroy the empty-string _Rep object.
00104    *
00105    *  All but the last paragraph is considered pretty conventional
00106    *  for a C++ string implementation.
00107   */
00108   // 21.3  Template class basic_string
00109   template<typename _CharT, typename _Traits, typename _Alloc>
00110     class basic_string
00111     {
00112       typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
00113 
00114       // Types:
00115     public:
00116       typedef _Traits                       traits_type;
00117       typedef typename _Traits::char_type           value_type;
00118       typedef _Alloc                        allocator_type;
00119       typedef typename _CharT_alloc_type::size_type     size_type;
00120       typedef typename _CharT_alloc_type::difference_type   difference_type;
00121       typedef typename _CharT_alloc_type::reference     reference;
00122       typedef typename _CharT_alloc_type::const_reference   const_reference;
00123       typedef typename _CharT_alloc_type::pointer       pointer;
00124       typedef typename _CharT_alloc_type::const_pointer     const_pointer;
00125       typedef __gnu_cxx::__normal_iterator<pointer, basic_string>  iterator;
00126       typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
00127                                                             const_iterator;
00128       typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00129       typedef std::reverse_iterator<iterator>           reverse_iterator;
00130 
00131     private:
00132       // _Rep: string representation
00133       //   Invariants:
00134       //   1. String really contains _M_length + 1 characters: due to 21.3.4
00135       //      must be kept null-terminated.
00136       //   2. _M_capacity >= _M_length
00137       //      Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
00138       //   3. _M_refcount has three states:
00139       //      -1: leaked, one reference, no ref-copies allowed, non-const.
00140       //       0: one reference, non-const.
00141       //     n>0: n + 1 references, operations require a lock, const.
00142       //   4. All fields==0 is an empty string, given the extra storage
00143       //      beyond-the-end for a null terminator; thus, the shared
00144       //      empty string representation needs no constructor.
00145 
00146       struct _Rep_base
00147       {
00148     size_type       _M_length;
00149     size_type       _M_capacity;
00150     _Atomic_word        _M_refcount;
00151       };
00152 
00153       struct _Rep : _Rep_base
00154       {
00155     // Types:
00156     typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
00157 
00158     // (Public) Data members:
00159 
00160     // The maximum number of individual char_type elements of an
00161     // individual string is determined by _S_max_size. This is the
00162     // value that will be returned by max_size().  (Whereas npos
00163     // is the maximum number of bytes the allocator can allocate.)
00164     // If one was to divvy up the theoretical largest size string,
00165     // with a terminating character and m _CharT elements, it'd
00166     // look like this:
00167     // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
00168     // Solving for m:
00169     // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
00170     // In addition, this implementation quarters this amount.
00171     static const size_type  _S_max_size;
00172     static const _CharT _S_terminal;
00173 
00174     // The following storage is init'd to 0 by the linker, resulting
00175         // (carefully) in an empty string with one reference.
00176         static size_type _S_empty_rep_storage[];
00177 
00178         static _Rep&
00179         _S_empty_rep()
00180         { 
00181       // NB: Mild hack to avoid strict-aliasing warnings.  Note that
00182       // _S_empty_rep_storage is never modified and the punning should
00183       // be reasonably safe in this case.
00184       void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
00185       return *reinterpret_cast<_Rep*>(__p);
00186     }
00187 
00188         bool
00189     _M_is_leaked() const
00190         { return this->_M_refcount < 0; }
00191 
00192         bool
00193     _M_is_shared() const
00194         { return this->_M_refcount > 0; }
00195 
00196         void
00197     _M_set_leaked()
00198         { this->_M_refcount = -1; }
00199 
00200         void
00201     _M_set_sharable()
00202         { this->_M_refcount = 0; }
00203 
00204     void
00205     _M_set_length_and_sharable(size_type __n)
00206     { 
00207       this->_M_set_sharable();  // One reference.
00208       this->_M_length = __n;
00209       traits_type::assign(this->_M_refdata()[__n], _S_terminal);
00210       // grrr. (per 21.3.4)
00211       // You cannot leave those LWG people alone for a second.
00212     }
00213 
00214     _CharT*
00215     _M_refdata() throw()
00216     { return reinterpret_cast<_CharT*>(this + 1); }
00217 
00218     _CharT*
00219     _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
00220     {
00221       return (!_M_is_leaked() && __alloc1 == __alloc2)
00222               ? _M_refcopy() : _M_clone(__alloc1);
00223     }
00224 
00225     // Create & Destroy
00226     static _Rep*
00227     _S_create(size_type, size_type, const _Alloc&);
00228 
00229     void
00230     _M_dispose(const _Alloc& __a)
00231     {
00232 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
00233       if (__builtin_expect(this != &_S_empty_rep(), false))
00234 #endif
00235         if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
00236                                -1) <= 0)
00237           _M_destroy(__a);
00238     }  // XXX MT
00239 
00240     void
00241     _M_destroy(const _Alloc&) throw();
00242 
00243     _CharT*
00244     _M_refcopy() throw()
00245     {
00246 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
00247       if (__builtin_expect(this != &_S_empty_rep(), false))
00248 #endif
00249             __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
00250       return _M_refdata();
00251     }  // XXX MT
00252 
00253     _CharT*
00254     _M_clone(const _Alloc&, size_type __res = 0);
00255       };
00256 
00257       // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
00258       struct _Alloc_hider : _Alloc
00259       {
00260     _Alloc_hider(_CharT* __dat, const _Alloc& __a)
00261     : _Alloc(__a), _M_p(__dat) { }
00262 
00263     _CharT* _M_p; // The actual data.
00264       };
00265 
00266     public:
00267       // Data Members (public):
00268       // NB: This is an unsigned type, and thus represents the maximum
00269       // size that the allocator can hold.
00270       ///  Value returned by various member functions when they fail.
00271       static const size_type    npos = static_cast<size_type>(-1);
00272 
00273     private:
00274       // Data Members (private):
00275       mutable _Alloc_hider  _M_dataplus;
00276 
00277       _CharT*
00278       _M_data() const
00279       { return  _M_dataplus._M_p; }
00280 
00281       _CharT*
00282       _M_data(_CharT* __p)
00283       { return (_M_dataplus._M_p = __p); }
00284 
00285       _Rep*
00286       _M_rep() const
00287       { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
00288 
00289       // For the internal use we have functions similar to `begin'/`end'
00290       // but they do not call _M_leak.
00291       iterator
00292       _M_ibegin() const
00293       { return iterator(_M_data()); }
00294 
00295       iterator
00296       _M_iend() const
00297       { return iterator(_M_data() + this->size()); }
00298 
00299       void
00300       _M_leak()    // for use in begin() & non-const op[]
00301       {
00302     if (!_M_rep()->_M_is_leaked())
00303       _M_leak_hard();
00304       }
00305 
00306       size_type
00307       _M_check(size_type __pos, const char* __s) const
00308       {
00309     if (__pos > this->size())
00310       __throw_out_of_range(__N(__s));
00311     return __pos;
00312       }
00313 
00314       void
00315       _M_check_length(size_type __n1, size_type __n2, const char* __s) const
00316       {
00317     if (this->max_size() - (this->size() - __n1) < __n2)
00318       __throw_length_error(__N(__s));
00319       }
00320 
00321       // NB: _M_limit doesn't check for a bad __pos value.
00322       size_type
00323       _M_limit(size_type __pos, size_type __off) const
00324       {
00325     const bool __testoff =  __off < this->size() - __pos;
00326     return __testoff ? __off : this->size() - __pos;
00327       }
00328 
00329       // True if _Rep and source do not overlap.
00330       bool
00331       _M_disjunct(const _CharT* __s) const
00332       {
00333     return (less<const _CharT*>()(__s, _M_data())
00334         || less<const _CharT*>()(_M_data() + this->size(), __s));
00335       }
00336 
00337       // When __n = 1 way faster than the general multichar
00338       // traits_type::copy/move/assign.
00339       static void
00340       _M_copy(_CharT* __d, const _CharT* __s, size_type __n)
00341       {
00342     if (__n == 1)
00343       traits_type::assign(*__d, *__s);
00344     else
00345       traits_type::copy(__d, __s, __n);
00346       }
00347 
00348       static void
00349       _M_move(_CharT* __d, const _CharT* __s, size_type __n)
00350       {
00351     if (__n == 1)
00352       traits_type::assign(*__d, *__s);
00353     else
00354       traits_type::move(__d, __s, __n);   
00355       }
00356 
00357       static void
00358       _M_assign(_CharT* __d, size_type __n, _CharT __c)
00359       {
00360     if (__n == 1)
00361       traits_type::assign(*__d, __c);
00362     else
00363       traits_type::assign(__d, __n, __c);     
00364       }
00365 
00366       // _S_copy_chars is a separate template to permit specialization
00367       // to optimize for the common case of pointers as iterators.
00368       template<class _Iterator>
00369         static void
00370         _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
00371         {
00372       for (; __k1 != __k2; ++__k1, ++__p)
00373         traits_type::assign(*__p, *__k1); // These types are off.
00374     }
00375 
00376       static void
00377       _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
00378       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
00379 
00380       static void
00381       _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
00382       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
00383 
00384       static void
00385       _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
00386       { _M_copy(__p, __k1, __k2 - __k1); }
00387 
00388       static void
00389       _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
00390       { _M_copy(__p, __k1, __k2 - __k1); }
00391 
00392       static int
00393       _S_compare(size_type __n1, size_type __n2)
00394       {
00395     const difference_type __d = difference_type(__n1 - __n2);
00396 
00397     if (__d > __gnu_cxx::__numeric_traits<int>::__max)
00398       return __gnu_cxx::__numeric_traits<int>::__max;
00399     else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
00400       return __gnu_cxx::__numeric_traits<int>::__min;
00401     else
00402       return int(__d);
00403       }
00404 
00405       void
00406       _M_mutate(size_type __pos, size_type __len1, size_type __len2);
00407 
00408       void
00409       _M_leak_hard();
00410 
00411       static _Rep&
00412       _S_empty_rep()
00413       { return _Rep::_S_empty_rep(); }
00414 
00415     public:
00416       // Construct/copy/destroy:
00417       // NB: We overload ctors in some cases instead of using default
00418       // arguments, per 17.4.4.4 para. 2 item 2.
00419 
00420       /**
00421        *  @brief  Default constructor creates an empty string.
00422        */
00423       inline
00424       basic_string();
00425 
00426       /**
00427        *  @brief  Construct an empty string using allocator @a a.
00428        */
00429       explicit
00430       basic_string(const _Alloc& __a);
00431 
00432       // NB: per LWG issue 42, semantics different from IS:
00433       /**
00434        *  @brief  Construct string with copy of value of @a str.
00435        *  @param  str  Source string.
00436        */
00437       basic_string(const basic_string& __str);
00438       /**
00439        *  @brief  Construct string as copy of a substring.
00440        *  @param  str  Source string.
00441        *  @param  pos  Index of first character to copy from.
00442        *  @param  n  Number of characters to copy (default remainder).
00443        */
00444       basic_string(const basic_string& __str, size_type __pos,
00445            size_type __n = npos);
00446       /**
00447        *  @brief  Construct string as copy of a substring.
00448        *  @param  str  Source string.
00449        *  @param  pos  Index of first character to copy from.
00450        *  @param  n  Number of characters to copy.
00451        *  @param  a  Allocator to use.
00452        */
00453       basic_string(const basic_string& __str, size_type __pos,
00454            size_type __n, const _Alloc& __a);
00455 
00456       /**
00457        *  @brief  Construct string initialized by a character array.
00458        *  @param  s  Source character array.
00459        *  @param  n  Number of characters to copy.
00460        *  @param  a  Allocator to use (default is default allocator).
00461        *
00462        *  NB: @a s must have at least @a n characters, '\0' has no special
00463        *  meaning.
00464        */
00465       basic_string(const _CharT* __s, size_type __n,
00466            const _Alloc& __a = _Alloc());
00467       /**
00468        *  @brief  Construct string as copy of a C string.
00469        *  @param  s  Source C string.
00470        *  @param  a  Allocator to use (default is default allocator).
00471        */
00472       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
00473       /**
00474        *  @brief  Construct string as multiple characters.
00475        *  @param  n  Number of characters.
00476        *  @param  c  Character to use.
00477        *  @param  a  Allocator to use (default is default allocator).
00478        */
00479       basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
00480 
00481 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00482       /**
00483        *  @brief  Construct string from an initializer list.
00484        *  @param  l  std::initializer_list of characters.
00485        *  @param  a  Allocator to use (default is default allocator).
00486        */
00487       basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
00488 #endif // __GXX_EXPERIMENTAL_CXX0X__
00489 
00490       /**
00491        *  @brief  Construct string as copy of a range.
00492        *  @param  beg  Start of range.
00493        *  @param  end  End of range.
00494        *  @param  a  Allocator to use (default is default allocator).
00495        */
00496       template<class _InputIterator>
00497         basic_string(_InputIterator __beg, _InputIterator __end,
00498              const _Alloc& __a = _Alloc());
00499 
00500       /**
00501        *  @brief  Destroy the string instance.
00502        */
00503       ~basic_string()
00504       { _M_rep()->_M_dispose(this->get_allocator()); }
00505 
00506       /**
00507        *  @brief  Assign the value of @a str to this string.
00508        *  @param  str  Source string.
00509        */
00510       basic_string&
00511       operator=(const basic_string& __str) 
00512       { return this->assign(__str); }
00513 
00514       /**
00515        *  @brief  Copy contents of @a s into this string.
00516        *  @param  s  Source null-terminated string.
00517        */
00518       basic_string&
00519       operator=(const _CharT* __s) 
00520       { return this->assign(__s); }
00521 
00522       /**
00523        *  @brief  Set value to string of length 1.
00524        *  @param  c  Source character.
00525        *
00526        *  Assigning to a character makes this string length 1 and
00527        *  (*this)[0] == @a c.
00528        */
00529       basic_string&
00530       operator=(_CharT __c) 
00531       { 
00532     this->assign(1, __c); 
00533     return *this;
00534       }
00535 
00536 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00537       /**
00538        *  @brief  Set value to string constructed from initializer list.
00539        *  @param  l  std::initializer_list.
00540        */
00541       basic_string&
00542       operator=(initializer_list<_CharT> __l)
00543       {
00544     this->assign (__l.begin(), __l.end());
00545     return *this;
00546       }
00547 #endif // __GXX_EXPERIMENTAL_CXX0X__
00548 
00549       // Iterators:
00550       /**
00551        *  Returns a read/write iterator that points to the first character in
00552        *  the %string.  Unshares the string.
00553        */
00554       iterator
00555       begin()
00556       {
00557     _M_leak();
00558     return iterator(_M_data());
00559       }
00560 
00561       /**
00562        *  Returns a read-only (constant) iterator that points to the first
00563        *  character in the %string.
00564        */
00565       const_iterator
00566       begin() const
00567       { return const_iterator(_M_data()); }
00568 
00569       /**
00570        *  Returns a read/write iterator that points one past the last
00571        *  character in the %string.  Unshares the string.
00572        */
00573       iterator
00574       end()
00575       {
00576     _M_leak();
00577     return iterator(_M_data() + this->size());
00578       }
00579 
00580       /**
00581        *  Returns a read-only (constant) iterator that points one past the
00582        *  last character in the %string.
00583        */
00584       const_iterator
00585       end() const
00586       { return const_iterator(_M_data() + this->size()); }
00587 
00588       /**
00589        *  Returns a read/write reverse iterator that points to the last
00590        *  character in the %string.  Iteration is done in reverse element
00591        *  order.  Unshares the string.
00592        */
00593       reverse_iterator
00594       rbegin()
00595       { return reverse_iterator(this->end()); }
00596 
00597       /**
00598        *  Returns a read-only (constant) reverse iterator that points
00599        *  to the last character in the %string.  Iteration is done in
00600        *  reverse element order.
00601        */
00602       const_reverse_iterator
00603       rbegin() const
00604       { return const_reverse_iterator(this->end()); }
00605 
00606       /**
00607        *  Returns a read/write reverse iterator that points to one before the
00608        *  first character in the %string.  Iteration is done in reverse
00609        *  element order.  Unshares the string.
00610        */
00611       reverse_iterator
00612       rend()
00613       { return reverse_iterator(this->begin()); }
00614 
00615       /**
00616        *  Returns a read-only (constant) reverse iterator that points
00617        *  to one before the first character in the %string.  Iteration
00618        *  is done in reverse element order.
00619        */
00620       const_reverse_iterator
00621       rend() const
00622       { return const_reverse_iterator(this->begin()); }
00623 
00624     public:
00625       // Capacity:
00626       ///  Returns the number of characters in the string, not including any
00627       ///  null-termination.
00628       size_type
00629       size() const
00630       { return _M_rep()->_M_length; }
00631 
00632       ///  Returns the number of characters in the string, not including any
00633       ///  null-termination.
00634       size_type
00635       length() const
00636       { return _M_rep()->_M_length; }
00637 
00638       /// Returns the size() of the largest possible %string.
00639       size_type
00640       max_size() const
00641       { return _Rep::_S_max_size; }
00642 
00643       /**
00644        *  @brief  Resizes the %string to the specified number of characters.
00645        *  @param  n  Number of characters the %string should contain.
00646        *  @param  c  Character to fill any new elements.
00647        *
00648        *  This function will %resize the %string to the specified
00649        *  number of characters.  If the number is smaller than the
00650        *  %string's current size the %string is truncated, otherwise
00651        *  the %string is extended and new elements are set to @a c.
00652        */
00653       void
00654       resize(size_type __n, _CharT __c);
00655 
00656       /**
00657        *  @brief  Resizes the %string to the specified number of characters.
00658        *  @param  n  Number of characters the %string should contain.
00659        *
00660        *  This function will resize the %string to the specified length.  If
00661        *  the new size is smaller than the %string's current size the %string
00662        *  is truncated, otherwise the %string is extended and new characters
00663        *  are default-constructed.  For basic types such as char, this means
00664        *  setting them to 0.
00665        */
00666       void
00667       resize(size_type __n)
00668       { this->resize(__n, _CharT()); }
00669 
00670       /**
00671        *  Returns the total number of characters that the %string can hold
00672        *  before needing to allocate more memory.
00673        */
00674       size_type
00675       capacity() const
00676       { return _M_rep()->_M_capacity; }
00677 
00678       /**
00679        *  @brief  Attempt to preallocate enough memory for specified number of
00680        *          characters.
00681        *  @param  res_arg  Number of characters required.
00682        *  @throw  std::length_error  If @a res_arg exceeds @c max_size().
00683        *
00684        *  This function attempts to reserve enough memory for the
00685        *  %string to hold the specified number of characters.  If the
00686        *  number requested is more than max_size(), length_error is
00687        *  thrown.
00688        *
00689        *  The advantage of this function is that if optimal code is a
00690        *  necessity and the user can determine the string length that will be
00691        *  required, the user can reserve the memory in %advance, and thus
00692        *  prevent a possible reallocation of memory and copying of %string
00693        *  data.
00694        */
00695       void
00696       reserve(size_type __res_arg = 0);
00697 
00698       /**
00699        *  Erases the string, making it empty.
00700        */
00701       void
00702       clear()
00703       { _M_mutate(0, this->size(), 0); }
00704 
00705       /**
00706        *  Returns true if the %string is empty.  Equivalent to *this == "".
00707        */
00708       bool
00709       empty() const
00710       { return this->size() == 0; }
00711 
00712       // Element access:
00713       /**
00714        *  @brief  Subscript access to the data contained in the %string.
00715        *  @param  pos  The index of the character to access.
00716        *  @return  Read-only (constant) reference to the character.
00717        *
00718        *  This operator allows for easy, array-style, data access.
00719        *  Note that data access with this operator is unchecked and
00720        *  out_of_range lookups are not defined. (For checked lookups
00721        *  see at().)
00722        */
00723       const_reference
00724       operator[] (size_type __pos) const
00725       {
00726     _GLIBCXX_DEBUG_ASSERT(__pos <= size());
00727     return _M_data()[__pos];
00728       }
00729 
00730       /**
00731        *  @brief  Subscript access to the data contained in the %string.
00732        *  @param  pos  The index of the character to access.
00733        *  @return  Read/write reference to the character.
00734        *
00735        *  This operator allows for easy, array-style, data access.
00736        *  Note that data access with this operator is unchecked and
00737        *  out_of_range lookups are not defined. (For checked lookups
00738        *  see at().)  Unshares the string.
00739        */
00740       reference
00741       operator[](size_type __pos)
00742       {
00743         // allow pos == size() as v3 extension:
00744     _GLIBCXX_DEBUG_ASSERT(__pos <= size());
00745         // but be strict in pedantic mode:
00746     _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
00747     _M_leak();
00748     return _M_data()[__pos];
00749       }
00750 
00751       /**
00752        *  @brief  Provides access to the data contained in the %string.
00753        *  @param n The index of the character to access.
00754        *  @return  Read-only (const) reference to the character.
00755        *  @throw  std::out_of_range  If @a n is an invalid index.
00756        *
00757        *  This function provides for safer data access.  The parameter is
00758        *  first checked that it is in the range of the string.  The function
00759        *  throws out_of_range if the check fails.
00760        */
00761       const_reference
00762       at(size_type __n) const
00763       {
00764     if (__n >= this->size())
00765       __throw_out_of_range(__N("basic_string::at"));
00766     return _M_data()[__n];
00767       }
00768 
00769       /**
00770        *  @brief  Provides access to the data contained in the %string.
00771        *  @param n The index of the character to access.
00772        *  @return  Read/write reference to the character.
00773        *  @throw  std::out_of_range  If @a n is an invalid index.
00774        *
00775        *  This function provides for safer data access.  The parameter is
00776        *  first checked that it is in the range of the string.  The function
00777        *  throws out_of_range if the check fails.  Success results in
00778        *  unsharing the string.
00779        */
00780       reference
00781       at(size_type __n)
00782       {
00783     if (__n >= size())
00784       __throw_out_of_range(__N("basic_string::at"));
00785     _M_leak();
00786     return _M_data()[__n];
00787       }
00788 
00789       // Modifiers:
00790       /**
00791        *  @brief  Append a string to this string.
00792        *  @param str  The string to append.
00793        *  @return  Reference to this string.
00794        */
00795       basic_string&
00796       operator+=(const basic_string& __str)
00797       { return this->append(__str); }
00798 
00799       /**
00800        *  @brief  Append a C string.
00801        *  @param s  The C string to append.
00802        *  @return  Reference to this string.
00803        */
00804       basic_string&
00805       operator+=(const _CharT* __s)
00806       { return this->append(__s); }
00807 
00808       /**
00809        *  @brief  Append a character.
00810        *  @param c  The character to append.
00811        *  @return  Reference to this string.
00812        */
00813       basic_string&
00814       operator+=(_CharT __c)
00815       { 
00816     this->push_back(__c);
00817     return *this;
00818       }
00819 
00820 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00821       /**
00822        *  @brief  Append an initializer_list of characters.
00823        *  @param l  The initializer_list of characters to be appended.
00824        *  @return  Reference to this string.
00825        */
00826       basic_string&
00827       operator+=(initializer_list<_CharT> __l)
00828       { return this->append(__l.begin(), __l.end()); }
00829 #endif // __GXX_EXPERIMENTAL_CXX0X__
00830 
00831       /**
00832        *  @brief  Append a string to this string.
00833        *  @param str  The string to append.
00834        *  @return  Reference to this string.
00835        */
00836       basic_string&
00837       append(const basic_string& __str);
00838 
00839       /**
00840        *  @brief  Append a substring.
00841        *  @param str  The string to append.
00842        *  @param pos  Index of the first character of str to append.
00843        *  @param n  The number of characters to append.
00844        *  @return  Reference to this string.
00845        *  @throw  std::out_of_range if @a pos is not a valid index.
00846        *
00847        *  This function appends @a n characters from @a str starting at @a pos
00848        *  to this string.  If @a n is is larger than the number of available
00849        *  characters in @a str, the remainder of @a str is appended.
00850        */
00851       basic_string&
00852       append(const basic_string& __str, size_type __pos, size_type __n);
00853 
00854       /**
00855        *  @brief  Append a C substring.
00856        *  @param s  The C string to append.
00857        *  @param n  The number of characters to append.
00858        *  @return  Reference to this string.
00859        */
00860       basic_string&
00861       append(const _CharT* __s, size_type __n);
00862 
00863       /**
00864        *  @brief  Append a C string.
00865        *  @param s  The C string to append.
00866        *  @return  Reference to this string.
00867        */
00868       basic_string&
00869       append(const _CharT* __s)
00870       {
00871     __glibcxx_requires_string(__s);
00872     return this->append(__s, traits_type::length(__s));
00873       }
00874 
00875       /**
00876        *  @brief  Append multiple characters.
00877        *  @param n  The number of characters to append.
00878        *  @param c  The character to use.
00879        *  @return  Reference to this string.
00880        *
00881        *  Appends n copies of c to this string.
00882        */
00883       basic_string&
00884       append(size_type __n, _CharT __c);
00885 
00886 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00887       /**
00888        *  @brief  Append an initializer_list of characters.
00889        *  @param l  The initializer_list of characters to append.
00890        *  @return  Reference to this string.
00891        */
00892       basic_string&
00893       append(initializer_list<_CharT> __l)
00894       { return this->append(__l.begin(), __l.end()); }
00895 #endif // __GXX_EXPERIMENTAL_CXX0X__
00896 
00897       /**
00898        *  @brief  Append a range of characters.
00899        *  @param first  Iterator referencing the first character to append.
00900        *  @param last  Iterator marking the end of the range.
00901        *  @return  Reference to this string.
00902        *
00903        *  Appends characters in the range [first,last) to this string.
00904        */
00905       template<class _InputIterator>
00906         basic_string&
00907         append(_InputIterator __first, _InputIterator __last)
00908         { return this->replace(_M_iend(), _M_iend(), __first, __last); }
00909 
00910       /**
00911        *  @brief  Append a single character.
00912        *  @param c  Character to append.
00913        */
00914       void
00915       push_back(_CharT __c)
00916       { 
00917     const size_type __len = 1 + this->size();
00918     if (__len > this->capacity() || _M_rep()->_M_is_shared())
00919       this->reserve(__len);
00920     traits_type::assign(_M_data()[this->size()], __c);
00921     _M_rep()->_M_set_length_and_sharable(__len);
00922       }
00923 
00924       /**
00925        *  @brief  Set value to contents of another string.
00926        *  @param  str  Source string to use.
00927        *  @return  Reference to this string.
00928        */
00929       basic_string&
00930       assign(const basic_string& __str);
00931 
00932       /**
00933        *  @brief  Set value to a substring of a string.
00934        *  @param str  The string to use.
00935        *  @param pos  Index of the first character of str.
00936        *  @param n  Number of characters to use.
00937        *  @return  Reference to this string.
00938        *  @throw  std::out_of_range if @a pos is not a valid index.
00939        *
00940        *  This function sets this string to the substring of @a str consisting
00941        *  of @a n characters at @a pos.  If @a n is is larger than the number
00942        *  of available characters in @a str, the remainder of @a str is used.
00943        */
00944       basic_string&
00945       assign(const basic_string& __str, size_type __pos, size_type __n)
00946       { return this->assign(__str._M_data()
00947                 + __str._M_check(__pos, "basic_string::assign"),
00948                 __str._M_limit(__pos, __n)); }
00949 
00950       /**
00951        *  @brief  Set value to a C substring.
00952        *  @param s  The C string to use.
00953        *  @param n  Number of characters to use.
00954        *  @return  Reference to this string.
00955        *
00956        *  This function sets the value of this string to the first @a n
00957        *  characters of @a s.  If @a n is is larger than the number of
00958        *  available characters in @a s, the remainder of @a s is used.
00959        */
00960       basic_string&
00961       assign(const _CharT* __s, size_type __n);
00962 
00963       /**
00964        *  @brief  Set value to contents of a C string.
00965        *  @param s  The C string to use.
00966        *  @return  Reference to this string.
00967        *
00968        *  This function sets the value of this string to the value of @a s.
00969        *  The data is copied, so there is no dependence on @a s once the
00970        *  function returns.
00971        */
00972       basic_string&
00973       assign(const _CharT* __s)
00974       {
00975     __glibcxx_requires_string(__s);
00976     return this->assign(__s, traits_type::length(__s));
00977       }
00978 
00979       /**
00980        *  @brief  Set value to multiple characters.
00981        *  @param n  Length of the resulting string.
00982        *  @param c  The character to use.
00983        *  @return  Reference to this string.
00984        *
00985        *  This function sets the value of this string to @a n copies of
00986        *  character @a c.
00987        */
00988       basic_string&
00989       assign(size_type __n, _CharT __c)
00990       { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
00991 
00992       /**
00993        *  @brief  Set value to a range of characters.
00994        *  @param first  Iterator referencing the first character to append.
00995        *  @param last  Iterator marking the end of the range.
00996        *  @return  Reference to this string.
00997        *
00998        *  Sets value of string to characters in the range [first,last).
00999       */
01000       template<class _InputIterator>
01001         basic_string&
01002         assign(_InputIterator __first, _InputIterator __last)
01003         { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
01004 
01005 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01006       /**
01007        *  @brief  Set value to an initializer_list of characters.
01008        *  @param l  The initializer_list of characters to assign.
01009        *  @return  Reference to this string.
01010        */
01011       basic_string&
01012       assign(initializer_list<_CharT> __l)
01013       { return this->assign(__l.begin(), __l.end()); }
01014 #endif // __GXX_EXPERIMENTAL_CXX0X__
01015 
01016       /**
01017        *  @brief  Insert multiple characters.
01018        *  @param p  Iterator referencing location in string to insert at.
01019        *  @param n  Number of characters to insert
01020        *  @param c  The character to insert.
01021        *  @throw  std::length_error  If new length exceeds @c max_size().
01022        *
01023        *  Inserts @a n copies of character @a c starting at the position
01024        *  referenced by iterator @a p.  If adding characters causes the length
01025        *  to exceed max_size(), length_error is thrown.  The value of the
01026        *  string doesn't change if an error is thrown.
01027       */
01028       void
01029       insert(iterator __p, size_type __n, _CharT __c)
01030       { this->replace(__p, __p, __n, __c);  }
01031 
01032       /**
01033        *  @brief  Insert a range of characters.
01034        *  @param p  Iterator referencing location in string to insert at.
01035        *  @param beg  Start of range.
01036        *  @param end  End of range.
01037        *  @throw  std::length_error  If new length exceeds @c max_size().
01038        *
01039        *  Inserts characters in range [beg,end).  If adding characters causes
01040        *  the length to exceed max_size(), length_error is thrown.  The value
01041        *  of the string doesn't change if an error is thrown.
01042       */
01043       template<class _InputIterator>
01044         void
01045         insert(iterator __p, _InputIterator __beg, _InputIterator __end)
01046         { this->replace(__p, __p, __beg, __end); }
01047 
01048 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01049       /**
01050        *  @brief  Insert an initializer_list of characters.
01051        *  @param p  Iterator referencing location in string to insert at.
01052        *  @param l  The initializer_list of characters to insert.
01053        *  @throw  std::length_error  If new length exceeds @c max_size().
01054        */
01055       void
01056       insert(iterator __p, initializer_list<_CharT> __l)
01057       { this->insert(__p, __l.begin(), __l.end()); }
01058 #endif // __GXX_EXPERIMENTAL_CXX0X__
01059 
01060       /**
01061        *  @brief  Insert value of a string.
01062        *  @param pos1  Iterator referencing location in string to insert at.
01063        *  @param str  The string to insert.
01064        *  @return  Reference to this string.
01065        *  @throw  std::length_error  If new length exceeds @c max_size().
01066        *
01067        *  Inserts value of @a str starting at @a pos1.  If adding characters
01068        *  causes the length to exceed max_size(), length_error is thrown.  The
01069        *  value of the string doesn't change if an error is thrown.
01070       */
01071       basic_string&
01072       insert(size_type __pos1, const basic_string& __str)
01073       { return this->insert(__pos1, __str, size_type(0), __str.size()); }
01074 
01075       /**
01076        *  @brief  Insert a substring.
01077        *  @param pos1  Iterator referencing location in string to insert at.
01078        *  @param str  The string to insert.
01079        *  @param pos2  Start of characters in str to insert.
01080        *  @param n  Number of characters to insert.
01081        *  @return  Reference to this string.
01082        *  @throw  std::length_error  If new length exceeds @c max_size().
01083        *  @throw  std::out_of_range  If @a pos1 > size() or
01084        *  @a pos2 > @a str.size().
01085        *
01086        *  Starting at @a pos1, insert @a n character of @a str beginning with
01087        *  @a pos2.  If adding characters causes the length to exceed
01088        *  max_size(), length_error is thrown.  If @a pos1 is beyond the end of
01089        *  this string or @a pos2 is beyond the end of @a str, out_of_range is
01090        *  thrown.  The value of the string doesn't change if an error is
01091        *  thrown.
01092       */
01093       basic_string&
01094       insert(size_type __pos1, const basic_string& __str,
01095          size_type __pos2, size_type __n)
01096       { return this->insert(__pos1, __str._M_data()
01097                 + __str._M_check(__pos2, "basic_string::insert"),
01098                 __str._M_limit(__pos2, __n)); }
01099 
01100       /**
01101        *  @brief  Insert a C substring.
01102        *  @param pos  Iterator referencing location in string to insert at.
01103        *  @param s  The C string to insert.
01104        *  @param n  The number of characters to insert.
01105        *  @return  Reference to this string.
01106        *  @throw  std::length_error  If new length exceeds @c max_size().
01107        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01108        *  string.
01109        *
01110        *  Inserts the first @a n characters of @a s starting at @a pos.  If
01111        *  adding characters causes the length to exceed max_size(),
01112        *  length_error is thrown.  If @a pos is beyond end(), out_of_range is
01113        *  thrown.  The value of the string doesn't change if an error is
01114        *  thrown.
01115       */
01116       basic_string&
01117       insert(size_type __pos, const _CharT* __s, size_type __n);
01118 
01119       /**
01120        *  @brief  Insert a C string.
01121        *  @param pos  Iterator referencing location in string to insert at.
01122        *  @param s  The C string to insert.
01123        *  @return  Reference to this string.
01124        *  @throw  std::length_error  If new length exceeds @c max_size().
01125        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01126        *  string.
01127        *
01128        *  Inserts the first @a n characters of @a s starting at @a pos.  If
01129        *  adding characters causes the length to exceed max_size(),
01130        *  length_error is thrown.  If @a pos is beyond end(), out_of_range is
01131        *  thrown.  The value of the string doesn't change if an error is
01132        *  thrown.
01133       */
01134       basic_string&
01135       insert(size_type __pos, const _CharT* __s)
01136       {
01137     __glibcxx_requires_string(__s);
01138     return this->insert(__pos, __s, traits_type::length(__s));
01139       }
01140 
01141       /**
01142        *  @brief  Insert multiple characters.
01143        *  @param pos  Index in string to insert at.
01144        *  @param n  Number of characters to insert
01145        *  @param c  The character to insert.
01146        *  @return  Reference to this string.
01147        *  @throw  std::length_error  If new length exceeds @c max_size().
01148        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01149        *  string.
01150        *
01151        *  Inserts @a n copies of character @a c starting at index @a pos.  If
01152        *  adding characters causes the length to exceed max_size(),
01153        *  length_error is thrown.  If @a pos > length(), out_of_range is
01154        *  thrown.  The value of the string doesn't change if an error is
01155        *  thrown.
01156       */
01157       basic_string&
01158       insert(size_type __pos, size_type __n, _CharT __c)
01159       { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
01160                   size_type(0), __n, __c); }
01161 
01162       /**
01163        *  @brief  Insert one character.
01164        *  @param p  Iterator referencing position in string to insert at.
01165        *  @param c  The character to insert.
01166        *  @return  Iterator referencing newly inserted char.
01167        *  @throw  std::length_error  If new length exceeds @c max_size().
01168        *
01169        *  Inserts character @a c at position referenced by @a p.  If adding
01170        *  character causes the length to exceed max_size(), length_error is
01171        *  thrown.  If @a p is beyond end of string, out_of_range is thrown.
01172        *  The value of the string doesn't change if an error is thrown.
01173       */
01174       iterator
01175       insert(iterator __p, _CharT __c)
01176       {
01177     _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
01178     const size_type __pos = __p - _M_ibegin();
01179     _M_replace_aux(__pos, size_type(0), size_type(1), __c);
01180     _M_rep()->_M_set_leaked();
01181     return iterator(_M_data() + __pos);
01182       }
01183 
01184       /**
01185        *  @brief  Remove characters.
01186        *  @param pos  Index of first character to remove (default 0).
01187        *  @param n  Number of characters to remove (default remainder).
01188        *  @return  Reference to this string.
01189        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01190        *  string.
01191        *
01192        *  Removes @a n characters from this string starting at @a pos.  The
01193        *  length of the string is reduced by @a n.  If there are < @a n
01194        *  characters to remove, the remainder of the string is truncated.  If
01195        *  @a p is beyond end of string, out_of_range is thrown.  The value of
01196        *  the string doesn't change if an error is thrown.
01197       */
01198       basic_string&
01199       erase(size_type __pos = 0, size_type __n = npos)
01200       { 
01201     _M_mutate(_M_check(__pos, "basic_string::erase"),
01202           _M_limit(__pos, __n), size_type(0));
01203     return *this;
01204       }
01205 
01206       /**
01207        *  @brief  Remove one character.
01208        *  @param position  Iterator referencing the character to remove.
01209        *  @return  iterator referencing same location after removal.
01210        *
01211        *  Removes the character at @a position from this string. The value
01212        *  of the string doesn't change if an error is thrown.
01213       */
01214       iterator
01215       erase(iterator __position)
01216       {
01217     _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
01218                  && __position < _M_iend());
01219     const size_type __pos = __position - _M_ibegin();
01220     _M_mutate(__pos, size_type(1), size_type(0));
01221     _M_rep()->_M_set_leaked();
01222     return iterator(_M_data() + __pos);
01223       }
01224 
01225       /**
01226        *  @brief  Remove a range of characters.
01227        *  @param first  Iterator referencing the first character to remove.
01228        *  @param last  Iterator referencing the end of the range.
01229        *  @return  Iterator referencing location of first after removal.
01230        *
01231        *  Removes the characters in the range [first,last) from this string.
01232        *  The value of the string doesn't change if an error is thrown.
01233       */
01234       iterator
01235       erase(iterator __first, iterator __last)
01236       {
01237     _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
01238                  && __last <= _M_iend());
01239         const size_type __pos = __first - _M_ibegin();
01240     _M_mutate(__pos, __last - __first, size_type(0));
01241     _M_rep()->_M_set_leaked();
01242     return iterator(_M_data() + __pos);
01243       }
01244 
01245       /**
01246        *  @brief  Replace characters with value from another string.
01247        *  @param pos  Index of first character to replace.
01248        *  @param n  Number of characters to be replaced.
01249        *  @param str  String to insert.
01250        *  @return  Reference to this string.
01251        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01252        *  string.
01253        *  @throw  std::length_error  If new length exceeds @c max_size().
01254        *
01255        *  Removes the characters in the range [pos,pos+n) from this string.
01256        *  In place, the value of @a str is inserted.  If @a pos is beyond end
01257        *  of string, out_of_range is thrown.  If the length of the result
01258        *  exceeds max_size(), length_error is thrown.  The value of the string
01259        *  doesn't change if an error is thrown.
01260       */
01261       basic_string&
01262       replace(size_type __pos, size_type __n, const basic_string& __str)
01263       { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
01264 
01265       /**
01266        *  @brief  Replace characters with value from another string.
01267        *  @param pos1  Index of first character to replace.
01268        *  @param n1  Number of characters to be replaced.
01269        *  @param str  String to insert.
01270        *  @param pos2  Index of first character of str to use.
01271        *  @param n2  Number of characters from str to use.
01272        *  @return  Reference to this string.
01273        *  @throw  std::out_of_range  If @a pos1 > size() or @a pos2 >
01274        *  str.size().
01275        *  @throw  std::length_error  If new length exceeds @c max_size().
01276        *
01277        *  Removes the characters in the range [pos1,pos1 + n) from this
01278        *  string.  In place, the value of @a str is inserted.  If @a pos is
01279        *  beyond end of string, out_of_range is thrown.  If the length of the
01280        *  result exceeds max_size(), length_error is thrown.  The value of the
01281        *  string doesn't change if an error is thrown.
01282       */
01283       basic_string&
01284       replace(size_type __pos1, size_type __n1, const basic_string& __str,
01285           size_type __pos2, size_type __n2)
01286       { return this->replace(__pos1, __n1, __str._M_data()
01287                  + __str._M_check(__pos2, "basic_string::replace"),
01288                  __str._M_limit(__pos2, __n2)); }
01289 
01290       /**
01291        *  @brief  Replace characters with value of a C substring.
01292        *  @param pos  Index of first character to replace.
01293        *  @param n1  Number of characters to be replaced.
01294        *  @param s  C string to insert.
01295        *  @param n2  Number of characters from @a s to use.
01296        *  @return  Reference to this string.
01297        *  @throw  std::out_of_range  If @a pos1 > size().
01298        *  @throw  std::length_error  If new length exceeds @c max_size().
01299        *
01300        *  Removes the characters in the range [pos,pos + n1) from this string.
01301        *  In place, the first @a n2 characters of @a s are inserted, or all
01302        *  of @a s if @a n2 is too large.  If @a pos is beyond end of string,
01303        *  out_of_range is thrown.  If the length of result exceeds max_size(),
01304        *  length_error is thrown.  The value of the string doesn't change if
01305        *  an error is thrown.
01306       */
01307       basic_string&
01308       replace(size_type __pos, size_type __n1, const _CharT* __s,
01309           size_type __n2);
01310 
01311       /**
01312        *  @brief  Replace characters with value of a C string.
01313        *  @param pos  Index of first character to replace.
01314        *  @param n1  Number of characters to be replaced.
01315        *  @param s  C string to insert.
01316        *  @return  Reference to this string.
01317        *  @throw  std::out_of_range  If @a pos > size().
01318        *  @throw  std::length_error  If new length exceeds @c max_size().
01319        *
01320        *  Removes the characters in the range [pos,pos + n1) from this string.
01321        *  In place, the first @a n characters of @a s are inserted.  If @a
01322        *  pos is beyond end of string, out_of_range is thrown.  If the length
01323        *  of result exceeds max_size(), length_error is thrown.  The value of
01324        *  the string doesn't change if an error is thrown.
01325       */
01326       basic_string&
01327       replace(size_type __pos, size_type __n1, const _CharT* __s)
01328       {
01329     __glibcxx_requires_string(__s);
01330     return this->replace(__pos, __n1, __s, traits_type::length(__s));
01331       }
01332 
01333       /**
01334        *  @brief  Replace characters with multiple characters.
01335        *  @param pos  Index of first character to replace.
01336        *  @param n1  Number of characters to be replaced.
01337        *  @param n2  Number of characters to insert.
01338        *  @param c  Character to insert.
01339        *  @return  Reference to this string.
01340        *  @throw  std::out_of_range  If @a pos > size().
01341        *  @throw  std::length_error  If new length exceeds @c max_size().
01342        *
01343        *  Removes the characters in the range [pos,pos + n1) from this string.
01344        *  In place, @a n2 copies of @a c are inserted.  If @a pos is beyond
01345        *  end of string, out_of_range is thrown.  If the length of result
01346        *  exceeds max_size(), length_error is thrown.  The value of the string
01347        *  doesn't change if an error is thrown.
01348       */
01349       basic_string&
01350       replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
01351       { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
01352                   _M_limit(__pos, __n1), __n2, __c); }
01353 
01354       /**
01355        *  @brief  Replace range of characters with string.
01356        *  @param i1  Iterator referencing start of range to replace.
01357        *  @param i2  Iterator referencing end of range to replace.
01358        *  @param str  String value to insert.
01359        *  @return  Reference to this string.
01360        *  @throw  std::length_error  If new length exceeds @c max_size().
01361        *
01362        *  Removes the characters in the range [i1,i2).  In place, the value of
01363        *  @a str is inserted.  If the length of result exceeds max_size(),
01364        *  length_error is thrown.  The value of the string doesn't change if
01365        *  an error is thrown.
01366       */
01367       basic_string&