Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

basic_string.tcc

Go to the documentation of this file.
00001 // Components for manipulating sequences of characters -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 //
00031 // ISO C++ 14882: 21  Strings library
00032 //
00033 
00034 // This file is included by <string>.  It is not meant to be included
00035 // separately.
00036 
00037 // Written by Jason Merrill based upon the specification by Takanori Adachi
00038 // in ANSI X3J16/94-0013R2.  Rewritten by Nathan Myers to ISO-14882.
00039 
00040 #ifndef _CPP_BITS_STRING_TCC
00041 #define _CPP_BITS_STRING_TCC 1
00042 
00043 namespace std
00044 {
00045   template<typename _CharT, typename _Traits, typename _Alloc>
00046     const _CharT 
00047     basic_string<_CharT, _Traits, _Alloc>::
00048     _Rep::_S_terminal = _CharT();
00049 
00050   template<typename _CharT, typename _Traits, typename _Alloc>
00051     const typename basic_string<_CharT, _Traits, _Alloc>::size_type 
00052     basic_string<_CharT, _Traits, _Alloc>::
00053     _Rep::_S_max_size = (((npos - sizeof(_Rep))/sizeof(_CharT)) - 1) / 4;
00054 
00055   template<typename _CharT, typename _Traits, typename _Alloc>
00056     const typename basic_string<_CharT, _Traits, _Alloc>::size_type
00057     basic_string<_CharT, _Traits, _Alloc>::npos;
00058 
00059   // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
00060   // at static init time (before static ctors are run).
00061   template<typename _CharT, typename _Traits, typename _Alloc>
00062     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00063     basic_string<_CharT, _Traits, _Alloc>::_S_empty_rep_storage[
00064     (sizeof(_Rep) + sizeof(_CharT) + sizeof(size_type) - 1)/sizeof(size_type)]
00065     __attribute__ ((__aligned__ (16)))
00066 #ifdef _S_EMPTY_REP_INIT
00067     _S_EMPTY_REP_INIT
00068 #endif
00069   ;
00070 
00071   // NB: This is the special case for Input Iterators, used in
00072   // istreambuf_iterators, etc.
00073   // Input Iterators have a cost structure very different from
00074   // pointers, calling for a different coding style.
00075   template<typename _CharT, typename _Traits, typename _Alloc>
00076     template<typename _InIter>
00077       _CharT*
00078       basic_string<_CharT, _Traits, _Alloc>::
00079       _S_construct(_InIter __beg, _InIter __end, const _Alloc& __a,
00080            input_iterator_tag)
00081       {
00082     if (__beg == __end && __a == _Alloc())
00083       return _S_empty_rep()._M_refcopy();
00084     // Avoid reallocation for common case.
00085     _CharT __buf[100];
00086     size_type __i = 0;
00087     while (__beg != __end && __i < sizeof(__buf) / sizeof(_CharT))
00088       { 
00089         __buf[__i++] = *__beg; 
00090         ++__beg; 
00091       }
00092     _Rep* __r = _Rep::_S_create(__i, __a);
00093     traits_type::copy(__r->_M_refdata(), __buf, __i);
00094     __r->_M_length = __i;
00095     try 
00096       {
00097         // NB: this loop looks precisely this way because
00098         // it avoids comparing __beg != __end any more
00099         // than strictly necessary; != might be expensive!
00100         for (;;)
00101           {
00102         _CharT* __p = __r->_M_refdata() + __r->_M_length;
00103         _CharT* __last = __r->_M_refdata() + __r->_M_capacity;
00104         for (;;)
00105           {
00106             if (__beg == __end)
00107               {
00108             __r->_M_length = __p - __r->_M_refdata();
00109             *__p = _Rep::_S_terminal;       // grrr.
00110             return __r->_M_refdata();
00111               }
00112             if (__p == __last)
00113               break;
00114             *__p++ = *__beg; 
00115             ++__beg;
00116           }
00117         // Allocate more space.
00118         size_type __len = __p - __r->_M_refdata();
00119         _Rep* __another = _Rep::_S_create(__len + 1, __a);
00120         traits_type::copy(__another->_M_refdata(), 
00121                   __r->_M_refdata(), __len);
00122         __r->_M_destroy(__a);
00123         __r = __another;
00124         __r->_M_length = __len;
00125           }
00126       }
00127     catch(...) 
00128       {
00129         __r->_M_destroy(__a); 
00130         __throw_exception_again;
00131       }
00132     return 0;
00133       }
00134   
00135   template<typename _CharT, typename _Traits, typename _Alloc>
00136     template <class _InIter>
00137       _CharT*
00138       basic_string<_CharT,_Traits,_Alloc>::
00139       _S_construct(_InIter __beg, _InIter __end, const _Alloc& __a, 
00140            forward_iterator_tag)
00141       {
00142     size_type __dnew = static_cast<size_type>(distance(__beg, __end));
00143 
00144     if (__beg == __end && __a == _Alloc())
00145       return _S_empty_rep()._M_refcopy();
00146 
00147     // Check for out_of_range and length_error exceptions.
00148     _Rep* __r = _Rep::_S_create(__dnew, __a);
00149     try 
00150       { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
00151     catch(...) 
00152       { 
00153         __r->_M_destroy(__a); 
00154         __throw_exception_again;
00155       }
00156     __r->_M_length = __dnew;
00157 
00158     __r->_M_refdata()[__dnew] = _Rep::_S_terminal;  // grrr.
00159     return __r->_M_refdata();
00160       }
00161 
00162   template<typename _CharT, typename _Traits, typename _Alloc>
00163     _CharT*
00164     basic_string<_CharT,_Traits, _Alloc>::
00165     _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
00166     {
00167       if (__n == 0 && __a == _Alloc())
00168     return _S_empty_rep()._M_refcopy();
00169 
00170       // Check for out_of_range and length_error exceptions.
00171       _Rep* __r = _Rep::_S_create(__n, __a);
00172       try 
00173     { 
00174       if (__n) 
00175         traits_type::assign(__r->_M_refdata(), __n, __c); 
00176     }
00177       catch(...) 
00178     { 
00179       __r->_M_destroy(__a); 
00180       __throw_exception_again;
00181     }
00182       __r->_M_length = __n;
00183       __r->_M_refdata()[__n] = _Rep::_S_terminal;  // grrr
00184       return __r->_M_refdata();
00185     }
00186 
00187   template<typename _CharT, typename _Traits, typename _Alloc>
00188     basic_string<_CharT, _Traits, _Alloc>::
00189     basic_string(const basic_string& __str)
00190     : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(), __str.get_allocator()),
00191          __str.get_allocator())
00192     { }
00193 
00194   template<typename _CharT, typename _Traits, typename _Alloc>
00195     basic_string<_CharT, _Traits, _Alloc>::
00196     basic_string(const _Alloc& __a)
00197     : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
00198     { }
00199  
00200   template<typename _CharT, typename _Traits, typename _Alloc>
00201     basic_string<_CharT, _Traits, _Alloc>::
00202     basic_string(const basic_string& __str, size_type __pos, size_type __n)
00203     : _M_dataplus(_S_construct(__str._M_check(__pos), 
00204                    __str._M_fold(__pos, __n), _Alloc()), _Alloc())
00205     { }
00206 
00207   template<typename _CharT, typename _Traits, typename _Alloc>
00208     basic_string<_CharT, _Traits, _Alloc>::
00209     basic_string(const basic_string& __str, size_type __pos,
00210          size_type __n, const _Alloc& __a)
00211     : _M_dataplus(_S_construct(__str._M_check(__pos), 
00212                    __str._M_fold(__pos, __n), __a), __a)
00213     { }
00214 
00215   template<typename _CharT, typename _Traits, typename _Alloc>
00216     basic_string<_CharT, _Traits, _Alloc>::
00217     basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
00218     : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
00219     { }
00220 
00221   template<typename _CharT, typename _Traits, typename _Alloc>
00222     basic_string<_CharT, _Traits, _Alloc>::
00223     basic_string(const _CharT* __s, const _Alloc& __a)
00224     : _M_dataplus(_S_construct(__s, __s + traits_type::length(__s), __a), __a)
00225     { }
00226 
00227   template<typename _CharT, typename _Traits, typename _Alloc>
00228     basic_string<_CharT, _Traits, _Alloc>::
00229     basic_string(size_type __n, _CharT __c, const _Alloc& __a)
00230     : _M_dataplus(_S_construct(__n, __c, __a), __a)
00231     { }
00232  
00233   template<typename _CharT, typename _Traits, typename _Alloc>
00234     template<typename _InputIter>
00235     basic_string<_CharT, _Traits, _Alloc>::
00236     basic_string(_InputIter __beg, _InputIter __end, const _Alloc& __a)
00237     : _M_dataplus(_S_construct(__beg, __end, __a), __a)
00238     { }
00239 
00240   template<typename _CharT, typename _Traits, typename _Alloc>
00241     basic_string<_CharT, _Traits, _Alloc>&
00242     basic_string<_CharT, _Traits, _Alloc>::assign(const basic_string& __str)
00243     {
00244       if (_M_rep() != __str._M_rep())
00245     {
00246       // XXX MT
00247       allocator_type __a = this->get_allocator();
00248       _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
00249       _M_rep()->_M_dispose(__a);
00250       _M_data(__tmp);
00251     }
00252       return *this;
00253     }
00254   
00255   template<typename _CharT, typename _Traits, typename _Alloc>
00256     void
00257     basic_string<_CharT, _Traits, _Alloc>::_Rep::
00258     _M_destroy(const _Alloc& __a) throw ()
00259     {
00260       size_type __size = sizeof(_Rep) + (_M_capacity + 1) * sizeof(_CharT);
00261       _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
00262     }
00263 
00264   template<typename _CharT, typename _Traits, typename _Alloc>
00265     void
00266     basic_string<_CharT, _Traits, _Alloc>::_M_leak_hard()
00267     {
00268       if (_M_rep()->_M_is_shared()) 
00269     _M_mutate(0, 0, 0);
00270       _M_rep()->_M_set_leaked();
00271     }
00272 
00273   template<typename _CharT, typename _Traits, typename _Alloc>
00274     void
00275     basic_string<_CharT, _Traits, _Alloc>::
00276     _M_mutate(size_type __pos, size_type __len1, size_type __len2)
00277     {
00278       size_type       __old_size = this->size();
00279       const size_type __new_size = __old_size + __len2 - __len1;
00280       const _CharT*        __src = _M_data()  + __pos + __len1;
00281       const size_type __how_much = __old_size - __pos - __len1;
00282       
00283       if (_M_rep()->_M_is_shared() || __new_size > capacity())
00284     {
00285       // Must reallocate.
00286       allocator_type __a = get_allocator();
00287       _Rep* __r = _Rep::_S_create(__new_size, __a);
00288       try 
00289         {
00290           if (__pos)
00291         traits_type::copy(__r->_M_refdata(), _M_data(), __pos);
00292           if (__how_much)
00293         traits_type::copy(__r->_M_refdata() + __pos + __len2, 
00294                   __src, __how_much);
00295         }
00296       catch(...) 
00297         { 
00298           __r->_M_dispose(get_allocator()); 
00299           __throw_exception_again;
00300         }
00301       _M_rep()->_M_dispose(__a);
00302       _M_data(__r->_M_refdata());
00303       }
00304       else if (__how_much && __len1 != __len2)
00305     {
00306       // Work in-place
00307       traits_type::move(_M_data() + __pos + __len2, __src, __how_much);
00308     }
00309       _M_rep()->_M_set_sharable();
00310       _M_rep()->_M_length = __new_size;
00311       _M_data()[__new_size] = _Rep::_S_terminal; // grrr. (per 21.3.4)
00312     // You cannot leave those LWG people alone for a second.
00313     }
00314   
00315   template<typename _CharT, typename _Traits, typename _Alloc>
00316     void
00317     basic_string<_CharT, _Traits, _Alloc>::reserve(size_type __res)
00318     {
00319       if (__res > this->capacity() || _M_rep()->_M_is_shared())
00320         {
00321       if (__res > this->max_size())
00322         __throw_length_error("basic_string::reserve");
00323       // Make sure we don't shrink below the current size
00324       if (__res < this->size())
00325         __res = this->size();
00326       allocator_type __a = get_allocator();
00327       _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
00328       _M_rep()->_M_dispose(__a);
00329       _M_data(__tmp);
00330         }
00331     }
00332   
00333   template<typename _CharT, typename _Traits, typename _Alloc>
00334     void basic_string<_CharT, _Traits, _Alloc>::swap(basic_string& __s)
00335     {
00336       if (_M_rep()->_M_is_leaked()) 
00337     _M_rep()->_M_set_sharable();
00338       if (__s._M_rep()->_M_is_leaked()) 
00339     __s._M_rep()->_M_set_sharable();
00340       if (this->get_allocator() == __s.get_allocator())
00341     {
00342       _CharT* __tmp = _M_data();
00343       _M_data(__s._M_data());
00344       __s._M_data(__tmp);
00345     }
00346       // The code below can usually be optimized away.
00347       else 
00348     {
00349       basic_string __tmp1(_M_ibegin(), _M_iend(), __s.get_allocator());
00350       basic_string __tmp2(__s._M_ibegin(), __s._M_iend(), 
00351                   this->get_allocator());
00352       *this = __tmp2;
00353       __s = __tmp1;
00354     }
00355     }
00356 
00357 #ifdef _GLIBCPP_ALLOC_CONTROL
00358   template<typename _CharT, typename _Traits, typename _Alloc>
00359     bool (*basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_excess_slop) 
00360     (size_t, size_t) = 
00361     basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_default_excess;
00362 #endif
00363 
00364   template<typename _CharT, typename _Traits, typename _Alloc>
00365     typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
00366     basic_string<_CharT, _Traits, _Alloc>::_Rep::
00367     _S_create(size_t __capacity, const _Alloc& __alloc)
00368     {
00369       typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
00370 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00371       // 83.  String::npos vs. string::max_size()
00372       if (__capacity > _S_max_size)
00373 #else
00374       if (__capacity == npos)
00375 #endif
00376     __throw_length_error("basic_string::_S_create");
00377 
00378       // NB: Need an array of char_type[__capacity], plus a
00379       // terminating null char_type() element, plus enough for the
00380       // _Rep data structure. Whew. Seemingly so needy, yet so elemental.
00381       size_t __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
00382       // NB: Might throw, but no worries about a leak, mate: _Rep()
00383       // does not throw.
00384       void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
00385       _Rep *__p = new (__place) _Rep;
00386       __p->_M_capacity = __capacity;
00387       __p->_M_set_sharable();  // one reference
00388       __p->_M_length = 0;
00389       return __p;
00390     }
00391 
00392   template<typename _CharT, typename _Traits, typename _Alloc>
00393     _CharT*
00394     basic_string<_CharT, _Traits, _Alloc>::_Rep::
00395     _M_clone(const _Alloc& __alloc, size_type __res)
00396     {
00397       _Rep* __r = _Rep::_S_create(_M_length + __res, __alloc);
00398       if (_M_length)
00399     {
00400       try 
00401         { traits_type::copy(__r->_M_refdata(), _M_refdata(), _M_length); }
00402       catch(...)  
00403         { 
00404           __r->_M_destroy(__alloc); 
00405           __throw_exception_again;
00406         }
00407     }
00408       __r->_M_length = _M_length;
00409       return __r->_M_refdata();
00410     }
00411   
00412   template<typename _CharT, typename _Traits, typename _Alloc>
00413   inline bool
00414 #ifdef _GLIBCPP_ALLOC_CONTROL
00415     basic_string<_CharT, _Traits, _Alloc>::_Rep::
00416     _S_default_excess(size_t __s, size_t __r)
00417 #else
00418     basic_string<_CharT, _Traits, _Alloc>::_Rep::
00419     _S_excess_slop(size_t __s, size_t __r)
00420 #endif
00421     {
00422       return 2 * (__s <= 16 ? 16 : __s) < __r;
00423     }
00424   
00425   template<typename _CharT, typename _Traits, typename _Alloc>
00426     void
00427     basic_string<_CharT, _Traits, _Alloc>::resize(size_type __n, _CharT __c)
00428     {
00429       if (__n > max_size())
00430     __throw_length_error("basic_string::resize");
00431       size_type __size = this->size();
00432       if (__size < __n)
00433     this->append(__n - __size, __c);
00434       else if (__n < __size)
00435     this->erase(__n);
00436       // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
00437     }
00438   
00439   template<typename _CharT, typename _Traits, typename _Alloc>
00440     template<typename _InputIter>
00441       basic_string<_CharT, _Traits, _Alloc>&
00442       basic_string<_CharT, _Traits, _Alloc>::
00443       _M_replace(iterator __i1, iterator __i2, _InputIter __k1, 
00444          _InputIter __k2, input_iterator_tag)
00445       {
00446     basic_string __s(__k1, __k2);
00447     return this->replace(__i1, __i2, __s._M_ibegin(), __s._M_iend());
00448       }
00449 
00450   template<typename _CharT, typename _Traits, typename _Alloc>
00451     template<typename _ForwardIter>
00452       basic_string<_CharT, _Traits, _Alloc>&
00453       basic_string<_CharT, _Traits, _Alloc>::
00454       _M_replace(iterator __i1, iterator __i2, _ForwardIter __k1, 
00455          _ForwardIter __k2, forward_iterator_tag)
00456       {
00457     size_type __dold = __i2 - __i1;
00458     size_type __dmax = this->max_size();
00459     size_type __dnew = static_cast<size_type>(distance(__k1, __k2));
00460 
00461     if (__dmax <= __dnew)
00462       __throw_length_error("basic_string::_M_replace");
00463     size_type __off = __i1 - _M_ibegin();
00464     _M_mutate(__off, __dold, __dnew);
00465     // Invalidated __i1, __i2
00466     if (__dnew)
00467       _S_copy_chars(_M_data() + __off, __k1, __k2);
00468     
00469     return *this;
00470       }
00471 
00472   template<typename _CharT, typename _Traits, typename _Alloc>
00473     basic_string<_CharT, _Traits, _Alloc>&
00474     basic_string<_CharT, _Traits, _Alloc>::
00475     replace(size_type __pos1, size_type __n1, const basic_string& __str,
00476         size_type __pos2, size_type __n2)
00477     {
00478       return this->replace(_M_check(__pos1), _M_fold(__pos1, __n1),
00479                __str._M_check(__pos2), 
00480                __str._M_fold(__pos2, __n2));
00481     }
00482 
00483   template<typename _CharT, typename _Traits, typename _Alloc>
00484     basic_string<_CharT,_Traits,_Alloc>&
00485     basic_string<_CharT,_Traits,_Alloc>::
00486     append(const basic_string& __str)
00487     {
00488       // Iff appending itself, string needs to pre-reserve the
00489       // correct size so that _M_mutate does not clobber the
00490       // iterators formed here.
00491       size_type __size = __str.size();
00492       size_type __len = __size + this->size();
00493       if (__len > this->capacity())
00494     this->reserve(__len);
00495       return this->replace(_M_iend(), _M_iend(), __str._M_ibegin(),
00496                __str._M_iend());
00497     }
00498 
00499   template<typename _CharT, typename _Traits, typename _Alloc>
00500     basic_string<_CharT,_Traits,_Alloc>&
00501     basic_string<_CharT,_Traits,_Alloc>::
00502     append(const basic_string& __str, size_type __pos, size_type __n)
00503     {
00504       // Iff appending itself, string needs to pre-reserve the
00505       // correct size so that _M_mutate does not clobber the
00506       // iterators formed here.
00507       size_type __len = min(__str.size() - __pos, __n) + this->size();
00508       if (__len > this->capacity())
00509     this->reserve(__len);
00510       return this->replace(_M_iend(), _M_iend(), __str._M_check(__pos),
00511                __str._M_fold(__pos, __n));
00512     }
00513 
00514   template<typename _CharT, typename _Traits, typename _Alloc>
00515     basic_string<_CharT,_Traits,_Alloc>&
00516     basic_string<_CharT,_Traits,_Alloc>::
00517     append(const _CharT* __s, size_type __n)
00518     {
00519       size_type __len = __n + this->size();
00520       if (__len > this->capacity())
00521     this->reserve(__len);
00522       return this->replace(_M_iend(), _M_iend(), __s, __s + __n);
00523     }
00524 
00525   template<typename _CharT, typename _Traits, typename _Alloc>
00526     basic_string<_CharT,_Traits,_Alloc>&
00527     basic_string<_CharT,_Traits,_Alloc>::
00528     append(size_type __n, _CharT __c)
00529     {
00530       size_type __len = __n + this->size();
00531       if (__len > this->capacity())
00532     this->reserve(__len);
00533        return this->replace(_M_iend(), _M_iend(), __n, __c);
00534     }
00535 
00536   template<typename _CharT, typename _Traits, typename _Alloc>
00537     basic_string<_CharT,_Traits,_Alloc>
00538     operator+(const _CharT* __lhs,
00539              const basic_string<_CharT,_Traits,_Alloc>& __rhs)
00540     {
00541       typedef basic_string<_CharT,_Traits,_Alloc> __string_type;
00542       typedef typename __string_type::size_type   __size_type;
00543       __size_type __len = _Traits::length(__lhs);
00544       __string_type __str;
00545       __str.reserve(__len + __rhs.size());
00546       __str.append(__lhs, __lhs + __len);
00547       __str.append(__rhs);
00548       return __str;
00549     }
00550 
00551   template<typename _CharT, typename _Traits, typename _Alloc>
00552     basic_string<_CharT,_Traits,_Alloc>
00553     operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs)
00554     {
00555       typedef basic_string<_CharT,_Traits,_Alloc> __string_type;
00556       typedef typename __string_type::size_type   __size_type;
00557       __string_type __str;
00558       __size_type __len = __rhs.size();
00559       __str.reserve(__len + 1);
00560       __str.append(__size_type(1), __lhs);
00561       __str.append(__rhs);
00562       return __str;
00563     }
00564 
00565   template<typename _CharT, typename _Traits, typename _Alloc>
00566     basic_string<_CharT, _Traits, _Alloc>&
00567     basic_string<_CharT, _Traits, _Alloc>::
00568     replace(iterator __i1, iterator __i2, size_type __n2, _CharT __c)
00569     {
00570       size_type __n1 = __i2 - __i1;
00571       size_type __off1 = __i1 - _M_ibegin();
00572       if (max_size() - (this->size() - __n1) <= __n2)
00573     __throw_length_error("basic_string::replace");
00574       _M_mutate (__off1, __n1, __n2);
00575       // Invalidated __i1, __i2
00576       if (__n2)
00577     traits_type::assign(_M_data() + __off1, __n2, __c);
00578       return *this;
00579     }
00580   
00581   template<typename _CharT, typename _Traits, typename _Alloc>
00582     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00583     basic_string<_CharT, _Traits, _Alloc>::
00584     copy(_CharT* __s, size_type __n, size_type __pos) const
00585     {
00586       if (__pos > this->size())
00587     __throw_out_of_range("basic_string::copy");
00588       
00589       if (__n > this->size() - __pos)
00590     __n = this->size() - __pos;
00591       
00592       traits_type::copy(__s, _M_data() + __pos, __n);
00593       // 21.3.5.7 par 3: do not append null.  (good.)
00594       return __n;
00595     }
00596 
00597   template<typename _CharT, typename _Traits, typename _Alloc>
00598     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00599     basic_string<_CharT, _Traits, _Alloc>::
00600     find(const _CharT* __s, size_type __pos, size_type __n) const
00601     {
00602       size_type __size = this->size();
00603       size_t __xpos = __pos;
00604       const _CharT* __data = _M_data();
00605       for (; __xpos + __n <= __size; ++__xpos)
00606     if (traits_type::compare(__data + __xpos, __s, __n) == 0)
00607       return __xpos;
00608       return npos;
00609     }
00610 
00611   template<typename _CharT, typename _Traits, typename _Alloc>
00612     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00613     basic_string<_CharT, _Traits, _Alloc>::
00614     find(_CharT __c, size_type __pos) const
00615     {
00616       size_type __size = this->size();
00617       size_type __ret = npos;
00618       if (__pos < __size)
00619     {
00620       const _CharT* __data = _M_data();
00621       size_type __n = __size - __pos;
00622       const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
00623       if (__p)
00624         __ret = __p - __data;
00625     }
00626       return __ret;
00627     }
00628 
00629 
00630   template<typename _CharT, typename _Traits, typename _Alloc>
00631     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00632     basic_string<_CharT, _Traits, _Alloc>::
00633     rfind(const _CharT* __s, size_type __pos, size_type __n) const
00634     {
00635       size_type __size = this->size();
00636       if (__n <= __size)
00637     {
00638       __pos = std::min(__size - __n ,__pos);
00639       const _CharT* __data = _M_data();
00640       do 
00641         {
00642           if (traits_type::compare(__data + __pos, __s, __n) == 0)
00643         return __pos;
00644         } 
00645       while (__pos-- > 0);
00646     }
00647       return npos;
00648     }
00649   
00650   template<typename _CharT, typename _Traits, typename _Alloc>
00651     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00652     basic_string<_CharT, _Traits, _Alloc>::
00653     rfind(_CharT __c, size_type __pos) const
00654     {
00655       size_type __size = this->size();
00656       if (__size)
00657     {
00658       size_t __xpos = __size - 1;
00659       if (__xpos > __pos)
00660         __xpos = __pos;
00661       
00662       for (++__xpos; __xpos-- > 0; )
00663         if (traits_type::eq(_M_data()[__xpos], __c))
00664           return __xpos;
00665     }
00666       return npos;
00667     }
00668   
00669   template<typename _CharT, typename _Traits, typename _Alloc>
00670     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00671     basic_string<_CharT, _Traits, _Alloc>::
00672     find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
00673     {
00674       for (; __n && __pos < this->size(); ++__pos)
00675     {
00676       const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
00677       if (__p)
00678         return __pos;
00679     }
00680       return npos;
00681     }
00682  
00683   template<typename _CharT, typename _Traits, typename _Alloc>
00684     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00685     basic_string<_CharT, _Traits, _Alloc>::
00686     find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
00687     {
00688       size_type __size = this->size();
00689       if (__size && __n)
00690     { 
00691       if (--__size > __pos) 
00692         __size = __pos;
00693       do
00694         {
00695           if (traits_type::find(__s, __n, _M_data()[__size]))
00696         return __size;
00697         } 
00698       while (__size-- != 0);
00699     }
00700       return npos;
00701     }
00702   
00703   template<typename _CharT, typename _Traits, typename _Alloc>
00704     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00705     basic_string<_CharT, _Traits, _Alloc>::
00706     find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
00707     {
00708       size_t __xpos = __pos;
00709       for (; __n && __xpos < this->size(); ++__xpos)
00710     if (!traits_type::find(__s, __n, _M_data()[__xpos]))
00711       return __xpos;
00712       return npos;
00713     }
00714 
00715   template<typename _CharT, typename _Traits, typename _Alloc>
00716     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00717     basic_string<_CharT, _Traits, _Alloc>::
00718     find_first_not_of(_CharT __c, size_type __pos) const
00719     {
00720       size_t __xpos = __pos;
00721       for (; __xpos < this->size(); ++__xpos)
00722     if (!traits_type::eq(_M_data()[__xpos], __c))
00723       return __xpos;
00724       return npos;
00725     }
00726 
00727   template<typename _CharT, typename _Traits, typename _Alloc>
00728     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00729     basic_string<_CharT, _Traits, _Alloc>::
00730     find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
00731     {
00732       size_type __size = this->size();
00733       if (__size && __n)
00734     { 
00735       if (--__size > __pos) 
00736         __size = __pos;
00737       do
00738         {
00739           if (!traits_type::find(__s, __n, _M_data()[__size]))
00740         return __size;
00741         } 
00742       while (__size--);
00743     }
00744       return npos;
00745     }
00746 
00747   template<typename _CharT, typename _Traits, typename _Alloc>
00748     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00749     basic_string<_CharT, _Traits, _Alloc>::
00750     find_last_not_of(_CharT __c, size_type __pos) const
00751     {
00752       size_type __size = this->size();
00753       if (__size)
00754     { 
00755       if (--__size > __pos) 
00756         __size = __pos;
00757       do
00758         {
00759           if (!traits_type::eq(_M_data()[__size], __c))
00760         return __size;
00761         } 
00762       while (__size--);
00763     }
00764       return npos;
00765     }
00766   
00767   template<typename _CharT, typename _Traits, typename _Alloc>
00768     int
00769     basic_string<_CharT, _Traits, _Alloc>::
00770     compare(size_type __pos, size_type __n, const basic_string& __str) const
00771     {
00772       size_type __size = this->size();
00773       size_type __osize = __str.size();
00774       if (__pos > __size)
00775     __throw_out_of_range("basic_string::compare");
00776       
00777       size_type __rsize= min(__size - __pos, __n);
00778       size_type __len = min(__rsize, __osize);
00779       int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
00780       if (!__r)
00781     __r = __rsize - __osize;
00782       return __r;
00783     }
00784 
00785   template<typename _CharT, typename _Traits, typename _Alloc>
00786     int
00787     basic_string<_CharT, _Traits, _Alloc>::
00788     compare(size_type __pos1, size_type __n1, const basic_string& __str,
00789         size_type __pos2, size_type __n2) const
00790     {
00791       size_type __size = this->size();
00792       size_type __osize = __str.size();
00793       if (__pos1 > __size || __pos2 > __osize)
00794     __throw_out_of_range("basic_string::compare");
00795       
00796       size_type __rsize = min(__size - __pos1, __n1);
00797       size_type __rosize = min(__osize - __pos2, __n2);
00798       size_type __len = min(__rsize, __rosize);
00799       int __r = traits_type::compare(_M_data() + __pos1, 
00800                      __str.data() + __pos2, __len);
00801       if (!__r)
00802     __r = __rsize - __rosize;
00803       return __r;
00804     }
00805 
00806 
00807   template<typename _CharT, typename _Traits, typename _Alloc>
00808     int
00809     basic_string<_CharT, _Traits, _Alloc>::
00810     compare(const _CharT* __s) const
00811     {
00812       size_type __size = this->size();
00813       int __r = traits_type::compare(_M_data(), __s, __size);
00814       if (!__r)
00815     __r = __size - traits_type::length(__s);
00816       return __r;
00817     }
00818 
00819 
00820   template<typename _CharT, typename _Traits, typename _Alloc>
00821     int
00822     basic_string <_CharT,_Traits,_Alloc>::
00823     compare(size_type __pos, size_type __n1, const _CharT* __s) const
00824     {
00825       size_type __size = this->size();
00826       if (__pos > __size)
00827     __throw_out_of_range("basic_string::compare");
00828       
00829       size_type __osize = traits_type::length(__s);
00830       size_type __rsize = min(__size - __pos, __n1);
00831       size_type __len = min(__rsize, __osize);
00832       int __r = traits_type::compare(_M_data() + __pos, __s, __len);
00833       if (!__r)
00834     __r = __rsize - __osize;
00835       return __r;
00836     }
00837 
00838   template<typename _CharT, typename _Traits, typename _Alloc>
00839     int
00840     basic_string <_CharT,_Traits,_Alloc>::
00841     compare(size_type __pos, size_type __n1, const _CharT* __s, 
00842         size_type __n2) const
00843     {
00844       size_type __size = this->size();
00845       if (__pos > __size)
00846     __throw_out_of_range("basic_string::compare");
00847       
00848       size_type __osize = min(traits_type::length(__s), __n2);
00849       size_type __rsize = min(__size - __pos, __n1);
00850       size_type __len = min(__rsize, __osize);
00851       int __r = traits_type::compare(_M_data() + __pos, __s, __len);
00852       if (!__r)
00853     __r = __rsize - __osize;
00854       return __r;
00855     }
00856 
00857   template <class _CharT, class _Traits, class _Alloc>
00858     void
00859     _S_string_copy(const basic_string<_CharT, _Traits, _Alloc>& __str,
00860            _CharT* __buf, typename _Alloc::size_type __bufsiz)
00861     {
00862       typedef typename _Alloc::size_type size_type;
00863       size_type __strsize = __str.size();
00864       size_type __bytes = min(__strsize, __bufsiz - 1);
00865       _Traits::copy(__buf, __str.data(), __bytes);
00866       __buf[__bytes] = _CharT();
00867     }
00868 } // namespace std
00869 
00870 #endif

Generated on Sat Apr 19 07:14:19 2003 for libstdc++-v3 Source by doxygen1.2.15