basic_string.tcc

00001 // Components for manipulating sequences of characters -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 //
00032 // ISO C++ 14882: 21  Strings library
00033 //
00034 
00035 // This file is included by <string>.  It is not meant to be included
00036 // separately.
00037 
00038 // Written by Jason Merrill based upon the specification by Takanori Adachi
00039 // in ANSI X3J16/94-0013R2.  Rewritten by Nathan Myers to ISO-14882.
00040 
00041 #ifndef _CPP_BITS_STRING_TCC
00042 #define _CPP_BITS_STRING_TCC 1
00043 
00044 #pragma GCC system_header
00045 
00046 namespace std
00047 {
00048   template<typename _CharT, typename _Traits, typename _Alloc>
00049     const typename basic_string<_CharT, _Traits, _Alloc>::size_type 
00050     basic_string<_CharT, _Traits, _Alloc>::
00051     _Rep::_S_max_size = (((npos - sizeof(_Rep))/sizeof(_CharT)) - 1) / 4;
00052 
00053   template<typename _CharT, typename _Traits, typename _Alloc>
00054     const _CharT 
00055     basic_string<_CharT, _Traits, _Alloc>::
00056     _Rep::_S_terminal = _CharT();
00057 
00058   template<typename _CharT, typename _Traits, typename _Alloc>
00059     const typename basic_string<_CharT, _Traits, _Alloc>::size_type
00060     basic_string<_CharT, _Traits, _Alloc>::npos;
00061 
00062   // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
00063   // at static init time (before static ctors are run).
00064   template<typename _CharT, typename _Traits, typename _Alloc>
00065     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00066     basic_string<_CharT, _Traits, _Alloc>::_S_empty_rep_storage[
00067     (sizeof(_Rep) + sizeof(_CharT) + sizeof(size_type) - 1)/sizeof(size_type)];
00068 
00069   // NB: This is the special case for Input Iterators, used in
00070   // istreambuf_iterators, etc.
00071   // Input Iterators have a cost structure very different from
00072   // pointers, calling for a different coding style.
00073   template<typename _CharT, typename _Traits, typename _Alloc>
00074     template<typename _InIter>
00075       _CharT*
00076       basic_string<_CharT, _Traits, _Alloc>::
00077       _S_construct(_InIter __beg, _InIter __end, const _Alloc& __a,
00078            input_iterator_tag)
00079       {
00080     if (__beg == __end && __a == _Alloc())
00081       return _S_empty_rep()._M_refcopy();
00082     // Avoid reallocation for common case.
00083     _CharT __buf[100];
00084     size_type __i = 0;
00085     while (__beg != __end && __i < sizeof(__buf) / sizeof(_CharT))
00086       { 
00087         __buf[__i++] = *__beg; 
00088         ++__beg; 
00089       }
00090     _Rep* __r = _Rep::_S_create(__i, __a);
00091     traits_type::copy(__r->_M_refdata(), __buf, __i);
00092     __r->_M_length = __i;
00093     try 
00094       {
00095         // NB: this loop looks precisely this way because
00096         // it avoids comparing __beg != __end any more
00097         // than strictly necessary; != might be expensive!
00098         for (;;)
00099           {
00100         _CharT* __p = __r->_M_refdata() + __r->_M_length;
00101         _CharT* __last = __r->_M_refdata() + __r->_M_capacity;
00102         for (;;)
00103           {
00104             if (__beg == __end)
00105               {
00106             __r->_M_length = __p - __r->_M_refdata();
00107             *__p = _Rep::_S_terminal;       // grrr.
00108             return __r->_M_refdata();
00109               }
00110             if (__p == __last)
00111               break;
00112             *__p++ = *__beg; 
00113             ++__beg;
00114           }
00115         // Allocate more space.
00116         size_type __len = __p - __r->_M_refdata();
00117         _Rep* __another = _Rep::_S_create(__len + 1, __a);
00118         traits_type::copy(__another->_M_refdata(), 
00119                   __r->_M_refdata(), __len);
00120         __r->_M_destroy(__a);
00121         __r = __another;
00122         __r->_M_length = __len;
00123           }
00124       }
00125     catch(...) 
00126       {
00127         __r->_M_destroy(__a); 
00128         __throw_exception_again;
00129       }
00130     return 0;
00131       }
00132   
00133   template<typename _CharT, typename _Traits, typename _Alloc>
00134     template <class _InIter>
00135       _CharT*
00136       basic_string<_CharT, _Traits, _Alloc>::
00137       _S_construct(_InIter __beg, _InIter __end, const _Alloc& __a, 
00138            forward_iterator_tag)
00139       {
00140     if (__beg == __end && __a == _Alloc())
00141       return _S_empty_rep()._M_refcopy();
00142 
00143     // NB: Not required, but considered best practice.
00144     if (__builtin_expect(__beg == _InIter(), 0))
00145       __throw_logic_error("attempt to create string with null pointer");
00146 
00147     size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));
00148     
00149     // Check for out_of_range and length_error exceptions.
00150     _Rep* __r = _Rep::_S_create(__dnew, __a);
00151     try 
00152       { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
00153     catch(...) 
00154       { 
00155         __r->_M_destroy(__a); 
00156         __throw_exception_again;
00157       }
00158     __r->_M_length = __dnew;
00159 
00160     __r->_M_refdata()[__dnew] = _Rep::_S_terminal;  // grrr.
00161     return __r->_M_refdata();
00162       }
00163 
00164   template<typename _CharT, typename _Traits, typename _Alloc>
00165     _CharT*
00166     basic_string<_CharT, _Traits, _Alloc>::
00167     _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
00168     {
00169       if (__n == 0 && __a == _Alloc())
00170     return _S_empty_rep()._M_refcopy();
00171 
00172       // Check for out_of_range and length_error exceptions.
00173       _Rep* __r = _Rep::_S_create(__n, __a);
00174       if (__n) 
00175     traits_type::assign(__r->_M_refdata(), __n, __c);
00176 
00177       __r->_M_length = __n;
00178       __r->_M_refdata()[__n] = _Rep::_S_terminal;  // grrr
00179       return __r->_M_refdata();
00180     }
00181 
00182   template<typename _CharT, typename _Traits, typename _Alloc>
00183     basic_string<_CharT, _Traits, _Alloc>::
00184     basic_string(const basic_string& __str)
00185     : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
00186                       __str.get_allocator()),
00187           __str.get_allocator())
00188     { }
00189 
00190   template<typename _CharT, typename _Traits, typename _Alloc>
00191     basic_string<_CharT, _Traits, _Alloc>::
00192     basic_string(const _Alloc& __a)
00193     : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
00194     { }
00195  
00196   template<typename _CharT, typename _Traits, typename _Alloc>
00197     basic_string<_CharT, _Traits, _Alloc>::
00198     basic_string(const basic_string& __str, size_type __pos, size_type __n)
00199     : _M_dataplus(_S_construct(__str._M_check(__pos), 
00200                    __str._M_fold(__pos, __n), _Alloc()), _Alloc())
00201     { }
00202 
00203   template<typename _CharT, typename _Traits, typename _Alloc>
00204     basic_string<_CharT, _Traits, _Alloc>::
00205     basic_string(const basic_string& __str, size_type __pos,
00206          size_type __n, const _Alloc& __a)
00207     : _M_dataplus(_S_construct(__str._M_check(__pos), 
00208                    __str._M_fold(__pos, __n), __a), __a)
00209     { }
00210 
00211   template<typename _CharT, typename _Traits, typename _Alloc>
00212     basic_string<_CharT, _Traits, _Alloc>::
00213     basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
00214     : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
00215     { }
00216 
00217   template<typename _CharT, typename _Traits, typename _Alloc>
00218     basic_string<_CharT, _Traits, _Alloc>::
00219     basic_string(const _CharT* __s, const _Alloc& __a)
00220     : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
00221                    __s + npos, __a), __a)
00222     { }
00223 
00224   template<typename _CharT, typename _Traits, typename _Alloc>
00225     basic_string<_CharT, _Traits, _Alloc>::
00226     basic_string(size_type __n, _CharT __c, const _Alloc& __a)
00227     : _M_dataplus(_S_construct(__n, __c, __a), __a)
00228     { }
00229  
00230   template<typename _CharT, typename _Traits, typename _Alloc>
00231     template<typename _InputIter>
00232     basic_string<_CharT, _Traits, _Alloc>::
00233     basic_string(_InputIter __beg, _InputIter __end, const _Alloc& __a)
00234     : _M_dataplus(_S_construct(__beg, __end, __a), __a)
00235     { }
00236 
00237   template<typename _CharT, typename _Traits, typename _Alloc>
00238     basic_string<_CharT, _Traits, _Alloc>&
00239     basic_string<_CharT, _Traits, _Alloc>::assign(const basic_string& __str)
00240     {
00241       if (_M_rep() != __str._M_rep())
00242     {
00243       // XXX MT
00244       allocator_type __a = this->get_allocator();
00245       _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
00246       _M_rep()->_M_dispose(__a);
00247       _M_data(__tmp);
00248     }
00249       return *this;
00250     }
00251 
00252    template<typename _CharT, typename _Traits, typename _Alloc>
00253      basic_string<_CharT, _Traits, _Alloc>&
00254      basic_string<_CharT, _Traits, _Alloc>::
00255      assign(const basic_string& __str, size_type __pos, size_type __n)
00256      {
00257        const size_type __strsize = __str.size();
00258        if (__pos > __strsize)
00259      __throw_out_of_range("basic_string::assign");
00260        const bool __testn = __n < __strsize - __pos;
00261        const size_type __newsize = __testn ? __n : __strsize - __pos;
00262        return this->assign(__str._M_data() + __pos, __newsize);
00263      }
00264 
00265    template<typename _CharT, typename _Traits, typename _Alloc>
00266      basic_string<_CharT, _Traits, _Alloc>&
00267      basic_string<_CharT, _Traits, _Alloc>::
00268      assign(const _CharT* __s, size_type __n)
00269      {
00270        if (__n > this->max_size())
00271      __throw_length_error("basic_string::assign");
00272        if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
00273        || less<const _CharT*>()(_M_data() + this->size(), __s))
00274      return _M_replace_safe(_M_ibegin(), _M_iend(), __s, __s + __n);
00275        else
00276      {
00277        // Work in-place
00278        const size_type __pos = __s - _M_data();
00279        if (__pos >= __n)
00280          traits_type::copy(_M_data(), __s, __n);
00281        else if (__pos)
00282          traits_type::move(_M_data(), __s, __n);
00283        _M_rep()->_M_set_sharable();
00284        _M_rep()->_M_length = __n;
00285        _M_data()[__n] = _Rep::_S_terminal;  // grr.
00286        return *this;
00287      }
00288      }
00289 
00290    template<typename _CharT, typename _Traits, typename _Alloc>
00291      basic_string<_CharT, _Traits, _Alloc>&
00292      basic_string<_CharT, _Traits, _Alloc>::
00293      insert(size_type __pos1, const basic_string& __str,
00294             size_type __pos2, size_type __n)
00295      {
00296        const size_type __strsize = __str.size();
00297        if (__pos2 > __strsize)
00298      __throw_out_of_range("basic_string::insert");
00299        const bool __testn = __n < __strsize - __pos2;
00300        const size_type __newsize = __testn ? __n : __strsize - __pos2;
00301        return this->insert(__pos1, __str._M_data() + __pos2, __newsize);
00302      }
00303 
00304    template<typename _CharT, typename _Traits, typename _Alloc>
00305      basic_string<_CharT, _Traits, _Alloc>&
00306      basic_string<_CharT, _Traits, _Alloc>::
00307      insert(size_type __pos, const _CharT* __s, size_type __n)
00308      {
00309        const size_type __size = this->size();
00310        if (__pos > __size)
00311          __throw_out_of_range("basic_string::insert");
00312        if (__size > this->max_size() - __n)
00313          __throw_length_error("basic_string::insert");
00314        if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
00315            || less<const _CharT*>()(_M_data() + __size, __s))
00316          return _M_replace_safe(_M_ibegin() + __pos, _M_ibegin() + __pos,
00317                                 __s, __s + __n);
00318        else
00319          {
00320            // Work in-place. If _M_mutate reallocates the string, __s
00321            // does not point anymore to valid data, therefore we save its
00322            // offset, then we restore it.
00323            const size_type __off = __s - _M_data();
00324            _M_mutate(__pos, 0, __n);
00325            __s = _M_data() + __off;
00326            _CharT* __p = _M_data() + __pos;
00327            if (__s  + __n <= __p)
00328              traits_type::copy(__p, __s, __n);
00329            else if (__s >= __p)
00330              traits_type::copy(__p, __s + __n, __n);
00331            else
00332              {
00333                traits_type::copy(__p, __s, __p - __s);
00334                traits_type::copy(__p + (__p-__s), __p + __n, __n - (__p-__s));
00335              }
00336            return *this;
00337          }
00338      }
00339  
00340    template<typename _CharT, typename _Traits, typename _Alloc>
00341      basic_string<_CharT, _Traits, _Alloc>&
00342      basic_string<_CharT, _Traits, _Alloc>::
00343      replace(size_type __pos, size_type __n1, const _CharT* __s,
00344          size_type __n2)
00345      {
00346        const size_type __size = this->size();
00347        if (__pos > __size)
00348          __throw_out_of_range("basic_string::replace");
00349        const bool __testn1 = __n1 < __size - __pos;
00350        const size_type __foldn1 = __testn1 ? __n1 : __size - __pos;
00351        if (__size - __foldn1 > this->max_size() - __n2)
00352          __throw_length_error("basic_string::replace");
00353        if (_M_rep()->_M_is_shared() || less<const _CharT*>()(__s, _M_data())
00354            || less<const _CharT*>()(_M_data() + __size, __s))
00355          return _M_replace_safe(_M_ibegin() + __pos,
00356                 _M_ibegin() + __pos + __foldn1, __s, __s + __n2);
00357        // Todo: optimized in-place replace.
00358        else
00359      return _M_replace(_M_ibegin() + __pos, _M_ibegin() + __pos + __foldn1,
00360                __s, __s + __n2,
00361                typename iterator_traits<const _CharT*>::iterator_category());
00362      }
00363   
00364   template<typename _CharT, typename _Traits, typename _Alloc>
00365     void
00366     basic_string<_CharT, _Traits, _Alloc>::_Rep::
00367     _M_destroy(const _Alloc& __a) throw ()
00368     {
00369       size_type __size = sizeof(_Rep) + (_M_capacity + 1) * sizeof(_CharT);
00370       _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
00371     }
00372 
00373   template<typename _CharT, typename _Traits, typename _Alloc>
00374     void
00375     basic_string<_CharT, _Traits, _Alloc>::_M_leak_hard()
00376     {
00377       if (_M_rep()->_M_is_shared()) 
00378     _M_mutate(0, 0, 0);
00379       _M_rep()->_M_set_leaked();
00380     }
00381 
00382   // _M_mutate and, below, _M_clone, include, in the same form, an exponential
00383   // growth policy, necessary to meet amortized linear time requirements of
00384   // the library: see http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
00385   // The policy is active for allocations requiring an amount of memory above
00386   // system pagesize. This is consistent with the requirements of the standard:
00387   // see, f.i., http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
00388   template<typename _CharT, typename _Traits, typename _Alloc>
00389     void
00390     basic_string<_CharT, _Traits, _Alloc>::
00391     _M_mutate(size_type __pos, size_type __len1, size_type __len2)
00392     {
00393       const size_type __old_size = this->size();
00394       const size_type __new_size = __old_size + __len2 - __len1;
00395       const size_type __how_much = __old_size - __pos - __len1;
00396       
00397       if (__new_size > capacity() || _M_rep()->_M_is_shared())
00398     {
00399       // Must reallocate.
00400       allocator_type __a = get_allocator();
00401       _Rep* __r;
00402       if (__new_size > capacity())
00403         // Growing exponentially.
00404         __r = _Rep::_S_create(__new_size > 2 * capacity() ?
00405                   __new_size : 2 * capacity(), __a);
00406       else
00407         __r = _Rep::_S_create(__new_size, __a);
00408 
00409       if (__pos)
00410         traits_type::copy(__r->_M_refdata(), _M_data(), __pos);
00411       if (__how_much)
00412         traits_type::copy(__r->_M_refdata() + __pos + __len2, 
00413                   _M_data() + __pos + __len1, __how_much);
00414 
00415       _M_rep()->_M_dispose(__a);
00416       _M_data(__r->_M_refdata());
00417     }
00418       else if (__how_much && __len1 != __len2)
00419     {
00420       // Work in-place
00421       traits_type::move(_M_data() + __pos + __len2,
00422                 _M_data() + __pos + __len1, __how_much);
00423     }
00424       _M_rep()->_M_set_sharable();
00425       _M_rep()->_M_length = __new_size;
00426       _M_data()[__new_size] = _Rep::_S_terminal; // grrr. (per 21.3.4)
00427     // You cannot leave those LWG people alone for a second.
00428     }
00429   
00430   template<typename _CharT, typename _Traits, typename _Alloc>
00431     void
00432     basic_string<_CharT, _Traits, _Alloc>::reserve(size_type __res)
00433     {
00434       if (__res != this->capacity() || _M_rep()->_M_is_shared())
00435         {
00436       if (__res > this->max_size())
00437         __throw_length_error("basic_string::reserve");
00438       // Make sure we don't shrink below the current size
00439       if (__res < this->size())
00440         __res = this->size();
00441       allocator_type __a = get_allocator();
00442       _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
00443       _M_rep()->_M_dispose(__a);
00444       _M_data(__tmp);
00445         }
00446     }
00447   
00448   template<typename _CharT, typename _Traits, typename _Alloc>
00449     void basic_string<_CharT, _Traits, _Alloc>::swap(basic_string& __s)
00450     {
00451       if (_M_rep()->_M_is_leaked()) 
00452     _M_rep()->_M_set_sharable();
00453       if (__s._M_rep()->_M_is_leaked()) 
00454     __s._M_rep()->_M_set_sharable();
00455       if (this->get_allocator() == __s.get_allocator())
00456     {
00457       _CharT* __tmp = _M_data();
00458       _M_data(__s._M_data());
00459       __s._M_data(__tmp);
00460     }
00461       // The code below can usually be optimized away.
00462       else 
00463     {
00464       basic_string __tmp1(_M_ibegin(), _M_iend(), __s.get_allocator());
00465       basic_string __tmp2(__s._M_ibegin(), __s._M_iend(), 
00466                   this->get_allocator());
00467       *this = __tmp2;
00468       __s = __tmp1;
00469     }
00470     }
00471 
00472   template<typename _CharT, typename _Traits, typename _Alloc>
00473     typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
00474     basic_string<_CharT, _Traits, _Alloc>::_Rep::
00475     _S_create(size_t __capacity, const _Alloc& __alloc)
00476     {
00477 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00478       // 83.  String::npos vs. string::max_size()
00479       if (__capacity > _S_max_size)
00480 #else
00481       if (__capacity == npos)
00482 #endif
00483     __throw_length_error("basic_string::_S_create");
00484 
00485       // NB: Need an array of char_type[__capacity], plus a
00486       // terminating null char_type() element, plus enough for the
00487       // _Rep data structure. Whew. Seemingly so needy, yet so elemental.
00488       size_t __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
00489 
00490       // The standard places no restriction on allocating more memory
00491       // than is strictly needed within this layer at the moment or as
00492       // requested by an explicit application call to reserve().  Many
00493       // malloc implementations perform quite poorly when an
00494       // application attempts to allocate memory in a stepwise fashion
00495       // growing each allocation size by only 1 char.  Additionally,
00496       // it makes little sense to allocate less linear memory than the
00497       // natural blocking size of the malloc implementation.
00498       // Unfortunately, we would need a somewhat low-level calculation
00499       // with tuned parameters to get this perfect for any particular
00500       // malloc implementation.  Fortunately, generalizations about
00501       // common features seen among implementations seems to suffice.
00502 
00503       // __pagesize need not match the actual VM page size for good
00504       // results in practice, thus we pick a common value on the low
00505       // side.  __malloc_header_size is an estimate of the amount of
00506       // overhead per memory allocation (in practice seen N * sizeof
00507       // (void*) where N is 0, 2 or 4).  According to folklore,
00508       // picking this value on the high side is better than
00509       // low-balling it (especially when this algorithm is used with
00510       // malloc implementations that allocate memory blocks rounded up
00511       // to a size which is a power of 2).
00512       const size_t __pagesize = 4096; // must be 2^i * __subpagesize
00513       const size_t __malloc_header_size = 4 * sizeof (void*);
00514       if ((__size + __malloc_header_size) > __pagesize)
00515     {
00516       const size_t __extra =
00517         (__pagesize - ((__size + __malloc_header_size) % __pagesize))
00518         % __pagesize;
00519       __capacity += __extra / sizeof(_CharT);
00520       // Never allocate a string bigger than _S_max_size.
00521       if (__capacity > _S_max_size)
00522         __capacity = _S_max_size;
00523       __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
00524     }
00525 
00526       // NB: Might throw, but no worries about a leak, mate: _Rep()
00527       // does not throw.
00528       void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
00529       _Rep *__p = new (__place) _Rep;
00530       __p->_M_capacity = __capacity;
00531       __p->_M_set_sharable();  // One reference.
00532       __p->_M_length = 0;
00533       return __p;
00534     }
00535 
00536   template<typename _CharT, typename _Traits, typename _Alloc>
00537     _CharT*
00538     basic_string<_CharT, _Traits, _Alloc>::_Rep::
00539     _M_clone(const _Alloc& __alloc, size_type __res)
00540     {
00541       // Requested capacity of the clone.
00542       const size_type __requested_cap = _M_length + __res;
00543       _Rep* __r;
00544       if (__requested_cap > _M_capacity)
00545         // Growing exponentially.
00546         __r = _Rep::_S_create(__requested_cap > 2 * _M_capacity ?
00547                               __requested_cap : 2 * _M_capacity, __alloc);
00548       else
00549         __r = _Rep::_S_create(__requested_cap, __alloc);
00550       
00551       if (_M_length)
00552     traits_type::copy(__r->_M_refdata(), _M_refdata(), _M_length);
00553       
00554       __r->_M_length = _M_length;
00555       __r->_M_refdata()[_M_length] = _Rep::_S_terminal;
00556       return __r->_M_refdata();
00557     }
00558   
00559   template<typename _CharT, typename _Traits, typename _Alloc>
00560     void
00561     basic_string<_CharT, _Traits, _Alloc>::resize(size_type __n, _CharT __c)
00562     {
00563       if (__n > max_size())
00564     __throw_length_error("basic_string::resize");
00565       size_type __size = this->size();
00566       if (__size < __n)
00567     this->append(__n - __size, __c);
00568       else if (__n < __size)
00569     this->erase(__n);
00570       // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
00571     }
00572 
00573   // This is the general replace helper, which currently gets instantiated both
00574   // for input iterators and reverse iterators. It buffers internally and then
00575   // calls _M_replace_safe.
00576   template<typename _CharT, typename _Traits, typename _Alloc>
00577     template<typename _InputIter>
00578       basic_string<_CharT, _Traits, _Alloc>&
00579       basic_string<_CharT, _Traits, _Alloc>::
00580       _M_replace(iterator __i1, iterator __i2, _InputIter __k1, 
00581          _InputIter __k2, input_iterator_tag)
00582       {
00583     // Save concerned source string data in a temporary.
00584     basic_string __s(__k1, __k2);
00585     return _M_replace_safe(__i1, __i2, __s._M_ibegin(), __s._M_iend());
00586       }
00587 
00588   // This is a special replace helper, which does not buffer internally
00589   // and can be used in "safe" situations involving forward iterators,
00590   // i.e., when source and destination ranges are known to not overlap.
00591   template<typename _CharT, typename _Traits, typename _Alloc>
00592     template<typename _ForwardIter>
00593       basic_string<_CharT, _Traits, _Alloc>&
00594       basic_string<_CharT, _Traits, _Alloc>::
00595       _M_replace_safe(iterator __i1, iterator __i2, _ForwardIter __k1, 
00596               _ForwardIter __k2)
00597       {
00598     size_type __dnew = static_cast<size_type>(std::distance(__k1, __k2));
00599     size_type __dold = __i2 - __i1;
00600     size_type __dmax = this->max_size();
00601 
00602     if (__dmax <= __dnew)
00603       __throw_length_error("basic_string::_M_replace");
00604     size_type __off = __i1 - _M_ibegin();
00605     _M_mutate(__off, __dold, __dnew);
00606 
00607     // Invalidated __i1, __i2
00608     if (__dnew == 1)
00609       _M_data()[__off] = *__k1;
00610         else if (__dnew)
00611       _S_copy_chars(_M_data() + __off, __k1, __k2);
00612 
00613     return *this;
00614       }
00615 
00616   template<typename _CharT, typename _Traits, typename _Alloc>
00617     basic_string<_CharT, _Traits, _Alloc>&
00618     basic_string<_CharT, _Traits, _Alloc>::
00619     replace(size_type __pos1, size_type __n1, const basic_string& __str,
00620         size_type __pos2, size_type __n2)
00621     {
00622       const size_type __strsize = __str.size();
00623       if (__pos2 > __strsize)
00624     __throw_out_of_range("basic_string::replace");
00625       const bool __testn2 = __n2 < __strsize - __pos2;
00626       const size_type __foldn2 = __testn2 ? __n2 : __strsize - __pos2;
00627       return this->replace(__pos1, __n1,
00628                __str._M_data() + __pos2, __foldn2);      
00629     }
00630 
00631   template<typename _CharT, typename _Traits, typename _Alloc>
00632     basic_string<_CharT, _Traits, _Alloc>&
00633     basic_string<_CharT, _Traits, _Alloc>::
00634     append(const basic_string& __str)
00635     {
00636       // Iff appending itself, string needs to pre-reserve the
00637       // correct size so that _M_mutate does not clobber the
00638       // iterators formed here.
00639       size_type __size = __str.size();
00640       size_type __len = __size + this->size();
00641       if (__len > this->capacity())
00642     this->reserve(__len);
00643       return _M_replace_safe(_M_iend(), _M_iend(), __str._M_ibegin(),
00644                  __str._M_iend());
00645     }
00646 
00647   template<typename _CharT, typename _Traits, typename _Alloc>
00648     basic_string<_CharT, _Traits, _Alloc>&
00649     basic_string<_CharT, _Traits, _Alloc>::
00650     append(const basic_string& __str, size_type __pos, size_type __n)
00651     {
00652       // Iff appending itself, string needs to pre-reserve the
00653       // correct size so that _M_mutate does not clobber the
00654       // iterators formed here.
00655       size_type __len = std::min(size_type(__str.size() - __pos),
00656                  __n) + this->size();
00657       if (__len > this->capacity())
00658     this->reserve(__len);
00659       return _M_replace_safe(_M_iend(), _M_iend(), __str._M_check(__pos),
00660                  __str._M_fold(__pos, __n));
00661     }
00662 
00663   template<typename _CharT, typename _Traits, typename _Alloc>
00664     basic_string<_CharT, _Traits, _Alloc>&
00665     basic_string<_CharT, _Traits, _Alloc>::
00666     append(const _CharT* __s, size_type __n)
00667     {
00668       size_type __len = __n + this->size();
00669       if (__len > this->capacity())
00670     this->reserve(__len);
00671       return _M_replace_safe(_M_iend(), _M_iend(), __s, __s + __n);
00672     }
00673 
00674   template<typename _CharT, typename _Traits, typename _Alloc>
00675     basic_string<_CharT, _Traits, _Alloc>&
00676     basic_string<_CharT, _Traits, _Alloc>::
00677     append(size_type __n, _CharT __c)
00678     {
00679       size_type __len = __n + this->size();
00680       if (__len > this->capacity())
00681     this->reserve(__len);
00682        return this->replace(_M_iend(), _M_iend(), __n, __c);
00683     }
00684 
00685   template<typename _CharT, typename _Traits, typename _Alloc>
00686     basic_string<_CharT, _Traits, _Alloc>
00687     operator+(const _CharT* __lhs,
00688           const basic_string<_CharT, _Traits, _Alloc>& __rhs)
00689     {
00690       typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
00691       typedef typename __string_type::size_type   __size_type;
00692       __size_type __len = _Traits::length(__lhs);
00693       __string_type __str;
00694       __str.reserve(__len + __rhs.size());
00695       __str.append(__lhs, __lhs + __len);
00696       __str.append(__rhs);
00697       return __str;
00698     }
00699 
00700   template<typename _CharT, typename _Traits, typename _Alloc>
00701     basic_string<_CharT, _Traits, _Alloc>
00702     operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
00703     {
00704       typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
00705       typedef typename __string_type::size_type   __size_type;
00706       __string_type __str;
00707       __size_type __len = __rhs.size();
00708       __str.reserve(__len + 1);
00709       __str.append(__size_type(1), __lhs);
00710       __str.append(__rhs);
00711       return __str;
00712     }
00713 
00714   template<typename _CharT, typename _Traits, typename _Alloc>
00715     basic_string<_CharT, _Traits, _Alloc>&
00716     basic_string<_CharT, _Traits, _Alloc>::
00717     replace(iterator __i1, iterator __i2, size_type __n2, _CharT __c)
00718     {
00719       size_type __n1 = __i2 - __i1;
00720       size_type __off1 = __i1 - _M_ibegin();
00721       if (max_size() - (this->size() - __n1) <= __n2)
00722     __throw_length_error("basic_string::replace");
00723       _M_mutate (__off1, __n1, __n2);
00724       // Invalidated __i1, __i2
00725       if (__n2 == 1)
00726     _M_data()[__off1] = __c;
00727       else if (__n2)
00728     traits_type::assign(_M_data() + __off1, __n2, __c);
00729       return *this;
00730     }
00731   
00732   template<typename _CharT, typename _Traits, typename _Alloc>
00733     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00734     basic_string<_CharT, _Traits, _Alloc>::
00735     copy(_CharT* __s, size_type __n, size_type __pos) const
00736     {
00737       if (__pos > this->size())
00738     __throw_out_of_range("basic_string::copy");
00739       
00740       if (__n > this->size() - __pos)
00741     __n = this->size() - __pos;
00742       
00743       traits_type::copy(__s, _M_data() + __pos, __n);
00744       // 21.3.5.7 par 3: do not append null.  (good.)
00745       return __n;
00746     }
00747 
00748   template<typename _CharT, typename _Traits, typename _Alloc>
00749     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00750     basic_string<_CharT, _Traits, _Alloc>::
00751     find(const _CharT* __s, size_type __pos, size_type __n) const
00752     {
00753       size_type __size = this->size();
00754       size_t __xpos = __pos;
00755       const _CharT* __data = _M_data();
00756       for (; __xpos + __n <= __size; ++__xpos)
00757     if (traits_type::compare(__data + __xpos, __s, __n) == 0)
00758       return __xpos;
00759       return npos;
00760     }
00761 
00762   template<typename _CharT, typename _Traits, typename _Alloc>
00763     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00764     basic_string<_CharT, _Traits, _Alloc>::
00765     find(_CharT __c, size_type __pos) const
00766     {
00767       size_type __size = this->size();
00768       size_type __ret = npos;
00769       if (__pos < __size)
00770     {
00771       const _CharT* __data = _M_data();
00772       size_type __n = __size - __pos;
00773       const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
00774       if (__p)
00775         __ret = __p - __data;
00776     }
00777       return __ret;
00778     }
00779 
00780 
00781   template<typename _CharT, typename _Traits, typename _Alloc>
00782     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00783     basic_string<_CharT, _Traits, _Alloc>::
00784     rfind(const _CharT* __s, size_type __pos, size_type __n) const
00785     {
00786       size_type __size = this->size();
00787       if (__n <= __size)
00788     {
00789       __pos = std::min(size_type(__size - __n), __pos);
00790       const _CharT* __data = _M_data();
00791       do 
00792         {
00793           if (traits_type::compare(__data + __pos, __s, __n) == 0)
00794         return __pos;
00795         } 
00796       while (__pos-- > 0);
00797     }
00798       return npos;
00799     }
00800   
00801   template<typename _CharT, typename _Traits, typename _Alloc>
00802     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00803     basic_string<_CharT, _Traits, _Alloc>::
00804     rfind(_CharT __c, size_type __pos) const
00805     {
00806       size_type __size = this->size();
00807       if (__size)
00808     {
00809       size_t __xpos = __size - 1;
00810       if (__xpos > __pos)
00811         __xpos = __pos;
00812       
00813       for (++__xpos; __xpos-- > 0; )
00814         if (traits_type::eq(_M_data()[__xpos], __c))
00815           return __xpos;
00816     }
00817       return npos;
00818     }
00819   
00820   template<typename _CharT, typename _Traits, typename _Alloc>
00821     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00822     basic_string<_CharT, _Traits, _Alloc>::
00823     find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
00824     {
00825       for (; __n && __pos < this->size(); ++__pos)
00826     {
00827       const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
00828       if (__p)
00829         return __pos;
00830     }
00831       return npos;
00832     }
00833  
00834   template<typename _CharT, typename _Traits, typename _Alloc>
00835     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00836     basic_string<_CharT, _Traits, _Alloc>::
00837     find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
00838     {
00839       size_type __size = this->size();
00840       if (__size && __n)
00841     { 
00842       if (--__size > __pos) 
00843         __size = __pos;
00844       do
00845         {
00846           if (traits_type::find(__s, __n, _M_data()[__size]))
00847         return __size;
00848         } 
00849       while (__size-- != 0);
00850     }
00851       return npos;
00852     }
00853   
00854   template<typename _CharT, typename _Traits, typename _Alloc>
00855     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00856     basic_string<_CharT, _Traits, _Alloc>::
00857     find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
00858     {
00859       size_t __xpos = __pos;
00860       for (; __xpos < this->size(); ++__xpos)
00861     if (!traits_type::find(__s, __n, _M_data()[__xpos]))
00862       return __xpos;
00863       return npos;
00864     }
00865 
00866   template<typename _CharT, typename _Traits, typename _Alloc>
00867     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00868     basic_string<_CharT, _Traits, _Alloc>::
00869     find_first_not_of(_CharT __c, size_type __pos) const
00870     {
00871       size_t __xpos = __pos;
00872       for (; __xpos < this->size(); ++__xpos)
00873     if (!traits_type::eq(_M_data()[__xpos], __c))
00874       return __xpos;
00875       return npos;
00876     }
00877 
00878   template<typename _CharT, typename _Traits, typename _Alloc>
00879     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00880     basic_string<_CharT, _Traits, _Alloc>::
00881     find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
00882     {
00883       size_type __size = this->size();
00884       if (__size)
00885     { 
00886       if (--__size > __pos) 
00887         __size = __pos;
00888       do
00889         {
00890           if (!traits_type::find(__s, __n, _M_data()[__size]))
00891         return __size;
00892         } 
00893       while (__size--);
00894     }
00895       return npos;
00896     }
00897 
00898   template<typename _CharT, typename _Traits, typename _Alloc>
00899     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00900     basic_string<_CharT, _Traits, _Alloc>::
00901     find_last_not_of(_CharT __c, size_type __pos) const
00902     {
00903       size_type __size = this->size();
00904       if (__size)
00905     { 
00906       if (--__size > __pos) 
00907         __size = __pos;
00908       do
00909         {
00910           if (!traits_type::eq(_M_data()[__size], __c))
00911         return __size;
00912         } 
00913       while (__size--);
00914     }
00915       return npos;
00916     }
00917   
00918   template<typename _CharT, typename _Traits, typename _Alloc>
00919     int
00920     basic_string<_CharT, _Traits, _Alloc>::
00921     compare(size_type __pos, size_type __n, const basic_string& __str) const
00922     {
00923       size_type __size = this->size();
00924       size_type __osize = __str.size();
00925       if (__pos > __size)
00926     __throw_out_of_range("basic_string::compare");
00927       
00928       size_type __rsize= std::min(size_type(__size - __pos), __n);
00929       size_type __len = std::min(__rsize, __osize);
00930       int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
00931       if (!__r)
00932     __r = __rsize - __osize;
00933       return __r;
00934     }
00935 
00936   template<typename _CharT, typename _Traits, typename _Alloc>
00937     int
00938     basic_string<_CharT, _Traits, _Alloc>::
00939     compare(size_type __pos1, size_type __n1, const basic_string& __str,
00940         size_type __pos2, size_type __n2) const
00941     {
00942       size_type __size = this->size();
00943       size_type __osize = __str.size();
00944       if (__pos1 > __size || __pos2 > __osize)
00945     __throw_out_of_range("basic_string::compare");
00946       
00947       size_type __rsize = std::min(size_type(__size - __pos1), __n1);
00948       size_type __rosize = std::min(size_type(__osize - __pos2), __n2);
00949       size_type __len = std::min(__rsize, __rosize);
00950       int __r = traits_type::compare(_M_data() + __pos1, 
00951                      __str.data() + __pos2, __len);
00952       if (!__r)
00953     __r = __rsize - __rosize;
00954       return __r;
00955     }
00956 
00957 
00958   template<typename _CharT, typename _Traits, typename _Alloc>
00959     int
00960     basic_string<_CharT, _Traits, _Alloc>::
00961     compare(const _CharT* __s) const
00962     {
00963       size_type __size = this->size();
00964       size_type __osize = traits_type::length(__s);
00965       size_type __len = std::min(__size, __osize);
00966       int __r = traits_type::compare(_M_data(), __s, __len);
00967       if (!__r)
00968     __r = __size - __osize;
00969       return __r;
00970     }
00971 
00972 
00973   template<typename _CharT, typename _Traits, typename _Alloc>
00974     int
00975     basic_string <_CharT, _Traits, _Alloc>::
00976     compare(size_type __pos, size_type __n1, const _CharT* __s) const
00977     {
00978       size_type __size = this->size();
00979       if (__pos > __size)
00980     __throw_out_of_range("basic_string::compare");
00981       
00982       size_type __osize = traits_type::length(__s);
00983       size_type __rsize = std::min(size_type(__size - __pos), __n1);
00984       size_type __len = std::min(__rsize, __osize);
00985       int __r = traits_type::compare(_M_data() + __pos, __s, __len);
00986       if (!__r)
00987     __r = __rsize - __osize;
00988       return __r;
00989     }
00990 
00991   template<typename _CharT, typename _Traits, typename _Alloc>
00992     int
00993     basic_string <_CharT, _Traits, _Alloc>::
00994     compare(size_type __pos, size_type __n1, const _CharT* __s, 
00995         size_type __n2) const
00996     {
00997       size_type __size = this->size();
00998       if (__pos > __size)
00999     __throw_out_of_range("basic_string::compare");
01000       
01001       size_type __rsize = std::min(size_type(__size - __pos), __n1);
01002       size_type __len = std::min(__rsize, __n2);
01003       int __r = traits_type::compare(_M_data() + __pos, __s, __len);
01004       if (!__r)
01005     __r = __rsize - __n2;
01006       return __r;
01007     }
01008 
01009   template <class _CharT, class _Traits, class _Alloc>
01010     void
01011     _S_string_copy(const basic_string<_CharT, _Traits, _Alloc>& __str,
01012            _CharT* __buf, typename _Alloc::size_type __bufsiz)
01013     {
01014       typedef typename _Alloc::size_type size_type;
01015       size_type __strsize = __str.size();
01016       size_type __bytes = std::min(__strsize, __bufsiz - 1);
01017       _Traits::copy(__buf, __str.data(), __bytes);
01018       __buf[__bytes] = _CharT();
01019     }
01020 
01021   // Inhibit implicit instantiations for required instantiations,
01022   // which are defined via explicit instantiations elsewhere.  
01023   // NB: This syntax is a GNU extension.
01024 #if _GLIBCPP_EXTERN_TEMPLATE
01025   extern template class basic_string<char>;
01026   extern template 
01027     basic_istream<char>& 
01028     operator>>(basic_istream<char>&, string&);
01029   extern template 
01030     basic_ostream<char>& 
01031     operator<<(basic_ostream<char>&, const string&);
01032   extern template 
01033     basic_istream<char>& 
01034     getline(basic_istream<char>&, string&, char);
01035   extern template 
01036     basic_istream<char>& 
01037     getline(basic_istream<char>&, string&);
01038 
01039 #ifdef _GLIBCPP_USE_WCHAR_T
01040   extern template class basic_string<wchar_t>;
01041   extern template 
01042     basic_istream<wchar_t>& 
01043     operator>>(basic_istream<wchar_t>&, wstring&);
01044   extern template 
01045     basic_ostream<wchar_t>& 
01046     operator<<(basic_ostream<wchar_t>&, const wstring&);
01047   extern template 
01048     basic_istream<wchar_t>& 
01049     getline(basic_istream<wchar_t>&, wstring&, wchar_t);
01050   extern template 
01051     basic_istream<wchar_t>& 
01052     getline(basic_istream<wchar_t>&, wstring&);
01053 #endif
01054 #endif
01055 } // namespace std
01056 
01057 #endif

Generated on Sat Mar 19 18:32:13 2005 for libstdc++-v3 Source by  doxygen 1.4.1