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

Generated at Tue May 1 16:28:37 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001