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, 2002, 2003, 2004, 2005
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 /** @file basic_string.tcc
00032  *  This is an internal header file, included by other library headers.
00033  *  You should not attempt to use it directly.
00034  */
00035 
00036 //
00037 // ISO C++ 14882: 21  Strings library
00038 //
00039 
00040 // Written by Jason Merrill based upon the specification by Takanori Adachi
00041 // in ANSI X3J16/94-0013R2.  Rewritten by Nathan Myers to ISO-14882.
00042 
00043 #ifndef _BASIC_STRING_TCC
00044 #define _BASIC_STRING_TCC 1
00045 
00046 #pragma GCC system_header
00047 
00048 namespace std
00049 {
00050   template<typename _Type>
00051     inline bool
00052     __is_null_pointer(_Type* __ptr)
00053     { return __ptr == 0; }
00054 
00055   template<typename _Type>
00056     inline bool
00057     __is_null_pointer(_Type)
00058     { return false; }
00059 
00060   template<typename _CharT, typename _Traits, typename _Alloc>
00061     const typename basic_string<_CharT, _Traits, _Alloc>::size_type
00062     basic_string<_CharT, _Traits, _Alloc>::
00063     _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
00064 
00065   template<typename _CharT, typename _Traits, typename _Alloc>
00066     const _CharT
00067     basic_string<_CharT, _Traits, _Alloc>::
00068     _Rep::_S_terminal = _CharT();
00069 
00070   template<typename _CharT, typename _Traits, typename _Alloc>
00071     const typename basic_string<_CharT, _Traits, _Alloc>::size_type
00072     basic_string<_CharT, _Traits, _Alloc>::npos;
00073 
00074   // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
00075   // at static init time (before static ctors are run).
00076   template<typename _CharT, typename _Traits, typename _Alloc>
00077     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00078     basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
00079     (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
00080       sizeof(size_type)];
00081 
00082   // NB: This is the special case for Input Iterators, used in
00083   // istreambuf_iterators, etc.
00084   // Input Iterators have a cost structure very different from
00085   // pointers, calling for a different coding style.
00086   template<typename _CharT, typename _Traits, typename _Alloc>
00087     template<typename _InIterator>
00088       _CharT*
00089       basic_string<_CharT, _Traits, _Alloc>::
00090       _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
00091            input_iterator_tag)
00092       {
00093 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
00094     if (__beg == __end && __a == _Alloc())
00095       return _S_empty_rep()._M_refdata();
00096 #endif
00097     // Avoid reallocation for common case.
00098     _CharT __buf[128];
00099     size_type __len = 0;
00100     while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
00101       {
00102         __buf[__len++] = *__beg;
00103         ++__beg;
00104       }
00105     _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
00106     _M_copy(__r->_M_refdata(), __buf, __len);
00107     try
00108       {
00109         while (__beg != __end)
00110           {
00111         if (__len == __r->_M_capacity)
00112           {
00113             // Allocate more space.
00114             _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
00115             _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
00116             __r->_M_destroy(__a);
00117             __r = __another;
00118           }
00119         __r->_M_refdata()[__len++] = *__beg;
00120         ++__beg;
00121           }
00122       }
00123     catch(...)
00124       {
00125         __r->_M_destroy(__a);
00126         __throw_exception_again;
00127       }
00128     __r->_M_set_length_and_sharable(__len);
00129     return __r->_M_refdata();
00130       }
00131 
00132   template<typename _CharT, typename _Traits, typename _Alloc>
00133     template <typename _InIterator>
00134       _CharT*
00135       basic_string<_CharT, _Traits, _Alloc>::
00136       _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
00137            forward_iterator_tag)
00138       {
00139 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
00140     if (__beg == __end && __a == _Alloc())
00141       return _S_empty_rep()._M_refdata();
00142 #endif
00143     // NB: Not required, but considered best practice.
00144     if (__builtin_expect(__is_null_pointer(__beg) && __beg != __end, 0))
00145       __throw_logic_error(__N("basic_string::_S_construct NULL not valid"));
00146 
00147     const size_type __dnew = static_cast<size_type>(std::distance(__beg,
00148                                       __end));
00149     // Check for out_of_range and length_error exceptions.
00150     _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __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_set_length_and_sharable(__dnew);
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 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
00168       if (__n == 0 && __a == _Alloc())
00169     return _S_empty_rep()._M_refdata();
00170 #endif
00171       // Check for out_of_range and length_error exceptions.
00172       _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
00173       if (__n)
00174     _M_assign(__r->_M_refdata(), __n, __c);
00175 
00176       __r->_M_set_length_and_sharable(__n);
00177       return __r->_M_refdata();
00178     }
00179 
00180   template<typename _CharT, typename _Traits, typename _Alloc>
00181     basic_string<_CharT, _Traits, _Alloc>::
00182     basic_string(const basic_string& __str)
00183     : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
00184                       __str.get_allocator()),
00185           __str.get_allocator())
00186     { }
00187 
00188   template<typename _CharT, typename _Traits, typename _Alloc>
00189     basic_string<_CharT, _Traits, _Alloc>::
00190     basic_string(const _Alloc& __a)
00191     : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
00192     { }
00193 
00194   template<typename _CharT, typename _Traits, typename _Alloc>
00195     basic_string<_CharT, _Traits, _Alloc>::
00196     basic_string(const basic_string& __str, size_type __pos, size_type __n)
00197     : _M_dataplus(_S_construct(__str._M_data()
00198                    + __str._M_check(__pos,
00199                         "basic_string::basic_string"),
00200                    __str._M_data() + __str._M_limit(__pos, __n)
00201                    + __pos, _Alloc()), _Alloc())
00202     { }
00203 
00204   template<typename _CharT, typename _Traits, typename _Alloc>
00205     basic_string<_CharT, _Traits, _Alloc>::
00206     basic_string(const basic_string& __str, size_type __pos,
00207          size_type __n, const _Alloc& __a)
00208     : _M_dataplus(_S_construct(__str._M_data()
00209                    + __str._M_check(__pos,
00210                         "basic_string::basic_string"),
00211                    __str._M_data() + __str._M_limit(__pos, __n)
00212                    + __pos, __a), __a)
00213     { }
00214 
00215   // TBD: DPG annotate
00216   template<typename _CharT, typename _Traits, typename _Alloc>
00217     basic_string<_CharT, _Traits, _Alloc>::
00218     basic_string(const _CharT* __s, size_type __n, const _Alloc& __a)
00219     : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
00220     { }
00221 
00222   // TBD: DPG annotate
00223   template<typename _CharT, typename _Traits, typename _Alloc>
00224     basic_string<_CharT, _Traits, _Alloc>::
00225     basic_string(const _CharT* __s, const _Alloc& __a)
00226     : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
00227                    __s + npos, __a), __a)
00228     { }
00229 
00230   template<typename _CharT, typename _Traits, typename _Alloc>
00231     basic_string<_CharT, _Traits, _Alloc>::
00232     basic_string(size_type __n, _CharT __c, const _Alloc& __a)
00233     : _M_dataplus(_S_construct(__n, __c, __a), __a)
00234     { }
00235 
00236   // TBD: DPG annotate
00237   template<typename _CharT, typename _Traits, typename _Alloc>
00238     template<typename _InputIterator>
00239     basic_string<_CharT, _Traits, _Alloc>::
00240     basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a)
00241     : _M_dataplus(_S_construct(__beg, __end, __a), __a)
00242     { }
00243 
00244   template<typename _CharT, typename _Traits, typename _Alloc>
00245     basic_string<_CharT, _Traits, _Alloc>&
00246     basic_string<_CharT, _Traits, _Alloc>::
00247     assign(const basic_string& __str)
00248     {
00249       if (_M_rep() != __str._M_rep())
00250     {
00251       // XXX MT
00252       const allocator_type __a = this->get_allocator();
00253       _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
00254       _M_rep()->_M_dispose(__a);
00255       _M_data(__tmp);
00256     }
00257       return *this;
00258     }
00259 
00260   template<typename _CharT, typename _Traits, typename _Alloc>
00261     basic_string<_CharT, _Traits, _Alloc>&
00262     basic_string<_CharT, _Traits, _Alloc>::
00263     assign(const _CharT* __s, size_type __n)
00264     {
00265       __glibcxx_requires_string_len(__s, __n);
00266       _M_check_length(this->size(), __n, "basic_string::assign");
00267       if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
00268     return _M_replace_safe(size_type(0), this->size(), __s, __n);
00269       else
00270     {
00271       // Work in-place.
00272       const size_type __pos = __s - _M_data();
00273       if (__pos >= __n)
00274         _M_copy(_M_data(), __s, __n);
00275       else if (__pos)
00276         _M_move(_M_data(), __s, __n);
00277       _M_rep()->_M_set_length_and_sharable(__n);
00278       return *this;
00279     }
00280      }
00281 
00282   template<typename _CharT, typename _Traits, typename _Alloc>
00283     basic_string<_CharT, _Traits, _Alloc>&
00284     basic_string<_CharT, _Traits, _Alloc>::
00285     append(size_type __n, _CharT __c)
00286     {
00287       if (__n)
00288     {
00289       _M_check_length(size_type(0), __n, "basic_string::append");     
00290       const size_type __len = __n + this->size();
00291       if (__len > this->capacity() || _M_rep()->_M_is_shared())
00292         this->reserve(__len);
00293       _M_assign(_M_data() + this->size(), __n, __c);
00294       _M_rep()->_M_set_length_and_sharable(__len);
00295     }
00296       return *this;
00297     }
00298 
00299   template<typename _CharT, typename _Traits, typename _Alloc>
00300     basic_string<_CharT, _Traits, _Alloc>&
00301     basic_string<_CharT, _Traits, _Alloc>::
00302     append(const _CharT* __s, size_type __n)
00303     {
00304       __glibcxx_requires_string_len(__s, __n);
00305       if (__n)
00306     {
00307       _M_check_length(size_type(0), __n, "basic_string::append");
00308       const size_type __len = __n + this->size();
00309       if (__len > this->capacity() || _M_rep()->_M_is_shared())
00310         {
00311           if (_M_disjunct(__s))
00312         this->reserve(__len);
00313           else
00314         {
00315           const size_type __off = __s - _M_data();
00316           this->reserve(__len);
00317           __s = _M_data() + __off;
00318         }
00319         }
00320       _M_copy(_M_data() + this->size(), __s, __n);
00321       _M_rep()->_M_set_length_and_sharable(__len);
00322     }
00323       return *this;
00324     }
00325 
00326   template<typename _CharT, typename _Traits, typename _Alloc>
00327     basic_string<_CharT, _Traits, _Alloc>&
00328     basic_string<_CharT, _Traits, _Alloc>::
00329     append(const basic_string& __str)
00330     {
00331       const size_type __size = __str.size();
00332       if (__size)
00333     {
00334       const size_type __len = __size + this->size();
00335       if (__len > this->capacity() || _M_rep()->_M_is_shared())
00336         this->reserve(__len);
00337       _M_copy(_M_data() + this->size(), __str._M_data(), __size);
00338       _M_rep()->_M_set_length_and_sharable(__len);
00339     }
00340       return *this;
00341     }    
00342 
00343   template<typename _CharT, typename _Traits, typename _Alloc>
00344     basic_string<_CharT, _Traits, _Alloc>&
00345     basic_string<_CharT, _Traits, _Alloc>::
00346     append(const basic_string& __str, size_type __pos, size_type __n)
00347     {
00348       __str._M_check(__pos, "basic_string::append");
00349       __n = __str._M_limit(__pos, __n);
00350       if (__n)
00351     {
00352       const size_type __len = __n + this->size();
00353       if (__len > this->capacity() || _M_rep()->_M_is_shared())
00354         this->reserve(__len);
00355       _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
00356       _M_rep()->_M_set_length_and_sharable(__len);    
00357     }
00358       return *this;
00359     }
00360 
00361    template<typename _CharT, typename _Traits, typename _Alloc>
00362      basic_string<_CharT, _Traits, _Alloc>&
00363      basic_string<_CharT, _Traits, _Alloc>::
00364      insert(size_type __pos, const _CharT* __s, size_type __n)
00365      {
00366        __glibcxx_requires_string_len(__s, __n);
00367        _M_check(__pos, "basic_string::insert");
00368        _M_check_length(size_type(0), __n, "basic_string::insert");
00369        if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
00370          return _M_replace_safe(__pos, size_type(0), __s, __n);
00371        else
00372          {
00373            // Work in-place.
00374            const size_type __off = __s - _M_data();
00375            _M_mutate(__pos, 0, __n);
00376            __s = _M_data() + __off;
00377            _CharT* __p = _M_data() + __pos;
00378            if (__s  + __n <= __p)
00379              _M_copy(__p, __s, __n);
00380            else if (__s >= __p)
00381              _M_copy(__p, __s + __n, __n);
00382            else
00383              {
00384            const size_type __nleft = __p - __s;
00385                _M_copy(__p, __s, __nleft);
00386                _M_copy(__p + __nleft, __p + __n, __n - __nleft);
00387              }
00388            return *this;
00389          }
00390      }
00391 
00392    template<typename _CharT, typename _Traits, typename _Alloc>
00393      basic_string<_CharT, _Traits, _Alloc>&
00394      basic_string<_CharT, _Traits, _Alloc>::
00395      replace(size_type __pos, size_type __n1, const _CharT* __s,
00396          size_type __n2)
00397      {
00398        __glibcxx_requires_string_len(__s, __n2);
00399        _M_check(__pos, "basic_string::replace");
00400        __n1 = _M_limit(__pos, __n1);
00401        _M_check_length(__n1, __n2, "basic_string::replace");
00402        bool __left;
00403        if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
00404          return _M_replace_safe(__pos, __n1, __s, __n2);
00405        else if ((__left = __s + __n2 <= _M_data() + __pos)
00406         || _M_data() + __pos + __n1 <= __s)
00407      {
00408        // Work in-place: non-overlapping case.
00409        size_type __off = __s - _M_data();
00410        __left ? __off : (__off += __n2 - __n1);
00411        _M_mutate(__pos, __n1, __n2);
00412        _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
00413        return *this;
00414      }
00415        else
00416      {
00417        // Todo: overlapping case.
00418        const basic_string __tmp(__s, __n2);
00419        return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
00420      }
00421      }
00422 
00423   template<typename _CharT, typename _Traits, typename _Alloc>
00424     void
00425     basic_string<_CharT, _Traits, _Alloc>::_Rep::
00426     _M_destroy(const _Alloc& __a) throw ()
00427     {
00428 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
00429       if (this == &_S_empty_rep())
00430     return;
00431 #endif
00432       const size_type __size = sizeof(_Rep_base) +
00433                            (this->_M_capacity + 1) * sizeof(_CharT);
00434       _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
00435     }
00436 
00437   template<typename _CharT, typename _Traits, typename _Alloc>
00438     void
00439     basic_string<_CharT, _Traits, _Alloc>::
00440     _M_leak_hard()
00441     {
00442 #ifndef _GLIBCXX_FULLY_DYNAMIC_STRING
00443       if (_M_rep() == &_S_empty_rep())
00444     return;
00445 #endif
00446       if (_M_rep()->_M_is_shared())
00447     _M_mutate(0, 0, 0);
00448       _M_rep()->_M_set_leaked();
00449     }
00450 
00451   template<typename _CharT, typename _Traits, typename _Alloc>
00452     void
00453     basic_string<_CharT, _Traits, _Alloc>::
00454     _M_mutate(size_type __pos, size_type __len1, size_type __len2)
00455     {
00456       const size_type __old_size = this->size();
00457       const size_type __new_size = __old_size + __len2 - __len1;
00458       const size_type __how_much = __old_size - __pos - __len1;
00459 
00460       if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
00461     {
00462       // Must reallocate.
00463       const allocator_type __a = get_allocator();
00464       _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
00465 
00466       if (__pos)
00467         _M_copy(__r->_M_refdata(), _M_data(), __pos);
00468       if (__how_much)
00469         _M_copy(__r->_M_refdata() + __pos + __len2,
00470             _M_data() + __pos + __len1, __how_much);
00471 
00472       _M_rep()->_M_dispose(__a);
00473       _M_data(__r->_M_refdata());
00474     }
00475       else if (__how_much && __len1 != __len2)
00476     {
00477       // Work in-place.
00478       _M_move(_M_data() + __pos + __len2,
00479           _M_data() + __pos + __len1, __how_much);
00480     }
00481       _M_rep()->_M_set_length_and_sharable(__new_size);
00482     }
00483 
00484   template<typename _CharT, typename _Traits, typename _Alloc>
00485     void
00486     basic_string<_CharT, _Traits, _Alloc>::
00487     reserve(size_type __res)
00488     {
00489       if (__res != this->capacity() || _M_rep()->_M_is_shared())
00490         {
00491       // Make sure we don't shrink below the current size
00492       if (__res < this->size())
00493         __res = this->size();
00494       const allocator_type __a = get_allocator();
00495       _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
00496       _M_rep()->_M_dispose(__a);
00497       _M_data(__tmp);
00498         }
00499     }
00500 
00501   template<typename _CharT, typename _Traits, typename _Alloc>
00502     void
00503     basic_string<_CharT, _Traits, _Alloc>::
00504     swap(basic_string& __s)
00505     {
00506       if (_M_rep()->_M_is_leaked())
00507     _M_rep()->_M_set_sharable();
00508       if (__s._M_rep()->_M_is_leaked())
00509     __s._M_rep()->_M_set_sharable();
00510       if (this->get_allocator() == __s.get_allocator())
00511     {
00512       _CharT* __tmp = _M_data();
00513       _M_data(__s._M_data());
00514       __s._M_data(__tmp);
00515     }
00516       // The code below can usually be optimized away.
00517       else
00518     {
00519       const basic_string __tmp1(_M_ibegin(), _M_iend(),
00520                     __s.get_allocator());
00521       const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
00522                     this->get_allocator());
00523       *this = __tmp2;
00524       __s = __tmp1;
00525     }
00526     }
00527 
00528   template<typename _CharT, typename _Traits, typename _Alloc>
00529     typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
00530     basic_string<_CharT, _Traits, _Alloc>::_Rep::
00531     _S_create(size_type __capacity, size_type __old_capacity,
00532           const _Alloc& __alloc)
00533     {
00534       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00535       // 83.  String::npos vs. string::max_size()
00536       if (__capacity > _S_max_size)
00537     __throw_length_error(__N("basic_string::_S_create"));
00538 
00539       // The standard places no restriction on allocating more memory
00540       // than is strictly needed within this layer at the moment or as
00541       // requested by an explicit application call to reserve().
00542 
00543       // Many malloc implementations perform quite poorly when an
00544       // application attempts to allocate memory in a stepwise fashion
00545       // growing each allocation size by only 1 char.  Additionally,
00546       // it makes little sense to allocate less linear memory than the
00547       // natural blocking size of the malloc implementation.
00548       // Unfortunately, we would need a somewhat low-level calculation
00549       // with tuned parameters to get this perfect for any particular
00550       // malloc implementation.  Fortunately, generalizations about
00551       // common features seen among implementations seems to suffice.
00552 
00553       // __pagesize need not match the actual VM page size for good
00554       // results in practice, thus we pick a common value on the low
00555       // side.  __malloc_header_size is an estimate of the amount of
00556       // overhead per memory allocation (in practice seen N * sizeof
00557       // (void*) where N is 0, 2 or 4).  According to folklore,
00558       // picking this value on the high side is better than
00559       // low-balling it (especially when this algorithm is used with
00560       // malloc implementations that allocate memory blocks rounded up
00561       // to a size which is a power of 2).
00562       const size_type __pagesize = 4096;
00563       const size_type __malloc_header_size = 4 * sizeof(void*);
00564 
00565       // The below implements an exponential growth policy, necessary to
00566       // meet amortized linear time requirements of the library: see
00567       // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
00568       // It's active for allocations requiring an amount of memory above
00569       // system pagesize. This is consistent with the requirements of the
00570       // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
00571       if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
00572     __capacity = 2 * __old_capacity;
00573 
00574       // NB: Need an array of char_type[__capacity], plus a terminating
00575       // null char_type() element, plus enough for the _Rep data structure.
00576       // Whew. Seemingly so needy, yet so elemental.
00577       size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
00578 
00579       const size_type __adj_size = __size + __malloc_header_size;
00580       if (__adj_size > __pagesize && __capacity > __old_capacity)
00581     {
00582       const size_type __extra = __pagesize - __adj_size % __pagesize;
00583       __capacity += __extra / sizeof(_CharT);
00584       // Never allocate a string bigger than _S_max_size.
00585       if (__capacity > _S_max_size)
00586         __capacity = _S_max_size;
00587       __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
00588     }
00589 
00590       // NB: Might throw, but no worries about a leak, mate: _Rep()
00591       // does not throw.
00592       void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
00593       _Rep *__p = new (__place) _Rep;
00594       __p->_M_capacity = __capacity;
00595       // ABI compatibility - 3.4.x set in _S_create both
00596       // _M_refcount and _M_length.  All callers of _S_create
00597       // in basic_string.tcc then set just _M_length.
00598       // In 4.0.x and later both _M_refcount and _M_length
00599       // are initialized in the callers, unfortunately we can
00600       // have 3.4.x compiled code with _S_create callers inlined
00601       // calling 4.0.x+ _S_create.
00602       __p->_M_set_sharable();
00603       return __p;
00604     }
00605 
00606   template<typename _CharT, typename _Traits, typename _Alloc>
00607     _CharT*
00608     basic_string<_CharT, _Traits, _Alloc>::_Rep::
00609     _M_clone(const _Alloc& __alloc, size_type __res)
00610     {
00611       // Requested capacity of the clone.
00612       const size_type __requested_cap = this->_M_length + __res;
00613       _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
00614                   __alloc);
00615       if (this->_M_length)
00616     _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
00617 
00618       __r->_M_set_length_and_sharable(this->_M_length);
00619       return __r->_M_refdata();
00620     }
00621 
00622   template<typename _CharT, typename _Traits, typename _Alloc>
00623     void
00624     basic_string<_CharT, _Traits, _Alloc>::
00625     resize(size_type __n, _CharT __c)
00626     {
00627       const size_type __size = this->size();
00628       _M_check_length(__size, __n, "basic_string::resize");
00629       if (__size < __n)
00630     this->append(__n - __size, __c);
00631       else if (__n < __size)
00632     this->erase(__n);
00633       // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
00634     }
00635 
00636   template<typename _CharT, typename _Traits, typename _Alloc>
00637     template<typename _InputIterator>
00638       basic_string<_CharT, _Traits, _Alloc>&
00639       basic_string<_CharT, _Traits, _Alloc>::
00640       _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
00641               _InputIterator __k2, __false_type)
00642       {
00643     const basic_string __s(__k1, __k2);
00644     const size_type __n1 = __i2 - __i1;
00645     _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
00646     return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
00647                    __s.size());
00648       }
00649 
00650   template<typename _CharT, typename _Traits, typename _Alloc>
00651     basic_string<_CharT, _Traits, _Alloc>&
00652     basic_string<_CharT, _Traits, _Alloc>::
00653     _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
00654            _CharT __c)
00655     {
00656       _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
00657       _M_mutate(__pos1, __n1, __n2);
00658       if (__n2)
00659     _M_assign(_M_data() + __pos1, __n2, __c);
00660       return *this;
00661     }
00662 
00663   template<typename _CharT, typename _Traits, typename _Alloc>
00664     basic_string<_CharT, _Traits, _Alloc>&
00665     basic_string<_CharT, _Traits, _Alloc>::
00666     _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
00667             size_type __n2)
00668     {
00669       _M_mutate(__pos1, __n1, __n2);
00670       if (__n2)
00671     _M_copy(_M_data() + __pos1, __s, __n2);
00672       return *this;
00673     }
00674    
00675   template<typename _CharT, typename _Traits, typename _Alloc>
00676     basic_string<_CharT, _Traits, _Alloc>
00677     operator+(const _CharT* __lhs,
00678           const basic_string<_CharT, _Traits, _Alloc>& __rhs)
00679     {
00680       __glibcxx_requires_string(__lhs);
00681       typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
00682       typedef typename __string_type::size_type   __size_type;
00683       const __size_type __len = _Traits::length(__lhs);
00684       __string_type __str;
00685       __str.reserve(__len + __rhs.size());
00686       __str.append(__lhs, __len);
00687       __str.append(__rhs);
00688       return __str;
00689     }
00690 
00691   template<typename _CharT, typename _Traits, typename _Alloc>
00692     basic_string<_CharT, _Traits, _Alloc>
00693     operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
00694     {
00695       typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
00696       typedef typename __string_type::size_type   __size_type;
00697       __string_type __str;
00698       const __size_type __len = __rhs.size();
00699       __str.reserve(__len + 1);
00700       __str.append(__size_type(1), __lhs);
00701       __str.append(__rhs);
00702       return __str;
00703     }
00704 
00705   template<typename _CharT, typename _Traits, typename _Alloc>
00706     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00707     basic_string<_CharT, _Traits, _Alloc>::
00708     copy(_CharT* __s, size_type __n, size_type __pos) const
00709     {
00710       _M_check(__pos, "basic_string::copy");
00711       __n = _M_limit(__pos, __n);
00712       __glibcxx_requires_string_len(__s, __n);
00713       if (__n)
00714     _M_copy(__s, _M_data() + __pos, __n);
00715       // 21.3.5.7 par 3: do not append null.  (good.)
00716       return __n;
00717     }
00718 
00719   template<typename _CharT, typename _Traits, typename _Alloc>
00720     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00721     basic_string<_CharT, _Traits, _Alloc>::
00722     find(const _CharT* __s, size_type __pos, size_type __n) const
00723     {
00724       __glibcxx_requires_string_len(__s, __n);
00725       size_type __ret = npos;
00726       const size_type __size = this->size();
00727       if (__pos + __n <= __size)
00728     {
00729       const _CharT* __data = _M_data();
00730       const _CharT* __p = std::search(__data + __pos, __data + __size,
00731                       __s, __s + __n, traits_type::eq);
00732       if (__p != __data + __size || __n == 0)
00733         __ret = __p - __data;
00734     }
00735       return __ret;
00736     }
00737 
00738   template<typename _CharT, typename _Traits, typename _Alloc>
00739     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00740     basic_string<_CharT, _Traits, _Alloc>::
00741     find(_CharT __c, size_type __pos) const
00742     {
00743       size_type __ret = npos;
00744       const size_type __size = this->size();
00745       if (__pos < __size)
00746     {
00747       const _CharT* __data = _M_data();
00748       const size_type __n = __size - __pos;
00749       const _CharT* __p = traits_type::find(__data + __pos, __n, __c);
00750       if (__p)
00751         __ret = __p - __data;
00752     }
00753       return __ret;
00754     }
00755 
00756   template<typename _CharT, typename _Traits, typename _Alloc>
00757     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00758     basic_string<_CharT, _Traits, _Alloc>::
00759     rfind(const _CharT* __s, size_type __pos, size_type __n) const
00760     {
00761       __glibcxx_requires_string_len(__s, __n);
00762       const size_type __size = this->size();
00763       if (__n <= __size)
00764     {
00765       __pos = std::min(size_type(__size - __n), __pos);
00766       const _CharT* __data = _M_data();
00767       do
00768         {
00769           if (traits_type::compare(__data + __pos, __s, __n) == 0)
00770         return __pos;
00771         }
00772       while (__pos-- > 0);
00773     }
00774       return npos;
00775     }
00776 
00777   template<typename _CharT, typename _Traits, typename _Alloc>
00778     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00779     basic_string<_CharT, _Traits, _Alloc>::
00780     rfind(_CharT __c, size_type __pos) const
00781     {
00782       size_type __size = this->size();
00783       if (__size)
00784     {
00785       if (--__size > __pos)
00786         __size = __pos;
00787       for (++__size; __size-- > 0; )
00788         if (traits_type::eq(_M_data()[__size], __c))
00789           return __size;
00790     }
00791       return npos;
00792     }
00793 
00794   template<typename _CharT, typename _Traits, typename _Alloc>
00795     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00796     basic_string<_CharT, _Traits, _Alloc>::
00797     find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
00798     {
00799       __glibcxx_requires_string_len(__s, __n);
00800       for (; __n && __pos < this->size(); ++__pos)
00801     {
00802       const _CharT* __p = traits_type::find(__s, __n, _M_data()[__pos]);
00803       if (__p)
00804         return __pos;
00805     }
00806       return npos;
00807     }
00808 
00809   template<typename _CharT, typename _Traits, typename _Alloc>
00810     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00811     basic_string<_CharT, _Traits, _Alloc>::
00812     find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
00813     {
00814       __glibcxx_requires_string_len(__s, __n);
00815       size_type __size = this->size();
00816       if (__size && __n)
00817     {
00818       if (--__size > __pos)
00819         __size = __pos;
00820       do
00821         {
00822           if (traits_type::find(__s, __n, _M_data()[__size]))
00823         return __size;
00824         }
00825       while (__size-- != 0);
00826     }
00827       return npos;
00828     }
00829 
00830   template<typename _CharT, typename _Traits, typename _Alloc>
00831     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00832     basic_string<_CharT, _Traits, _Alloc>::
00833     find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
00834     {
00835       __glibcxx_requires_string_len(__s, __n);
00836       for (; __pos < this->size(); ++__pos)
00837     if (!traits_type::find(__s, __n, _M_data()[__pos]))
00838       return __pos;
00839       return npos;
00840     }
00841 
00842   template<typename _CharT, typename _Traits, typename _Alloc>
00843     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00844     basic_string<_CharT, _Traits, _Alloc>::
00845     find_first_not_of(_CharT __c, size_type __pos) const
00846     {
00847       for (; __pos < this->size(); ++__pos)
00848     if (!traits_type::eq(_M_data()[__pos], __c))
00849       return __pos;
00850       return npos;
00851     }
00852 
00853   template<typename _CharT, typename _Traits, typename _Alloc>
00854     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00855     basic_string<_CharT, _Traits, _Alloc>::
00856     find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
00857     {
00858       __glibcxx_requires_string_len(__s, __n);
00859       size_type __size = this->size();
00860       if (__size)
00861     {
00862       if (--__size > __pos)
00863         __size = __pos;
00864       do
00865         {
00866           if (!traits_type::find(__s, __n, _M_data()[__size]))
00867         return __size;
00868         }
00869       while (__size--);
00870     }
00871       return npos;
00872     }
00873 
00874   template<typename _CharT, typename _Traits, typename _Alloc>
00875     typename basic_string<_CharT, _Traits, _Alloc>::size_type
00876     basic_string<_CharT, _Traits, _Alloc>::
00877     find_last_not_of(_CharT __c, size_type __pos) const
00878     {
00879       size_type __size = this->size();
00880       if (__size)
00881     {
00882       if (--__size > __pos)
00883         __size = __pos;
00884       do
00885         {
00886           if (!traits_type::eq(_M_data()[__size], __c))
00887         return __size;
00888         }
00889       while (__size--);
00890     }
00891       return npos;
00892     }
00893 
00894   template<typename _CharT, typename _Traits, typename _Alloc>
00895     int
00896     basic_string<_CharT, _Traits, _Alloc>::
00897     compare(size_type __pos, size_type __n, const basic_string& __str) const
00898     {
00899       _M_check(__pos, "basic_string::compare");
00900       __n = _M_limit(__pos, __n);
00901       const size_type __osize = __str.size();
00902       const size_type __len = std::min(__n, __osize);
00903       int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
00904       if (!__r)
00905     __r = __n - __osize;
00906       return __r;
00907     }
00908 
00909   template<typename _CharT, typename _Traits, typename _Alloc>
00910     int
00911     basic_string<_CharT, _Traits, _Alloc>::
00912     compare(size_type __pos1, size_type __n1, const basic_string& __str,
00913         size_type __pos2, size_type __n2) const
00914     {
00915       _M_check(__pos1, "basic_string::compare");
00916       __str._M_check(__pos2, "basic_string::compare");
00917       __n1 = _M_limit(__pos1, __n1);
00918       __n2 = __str._M_limit(__pos2, __n2);
00919       const size_type __len = std::min(__n1, __n2);
00920       int __r = traits_type::compare(_M_data() + __pos1,
00921                      __str.data() + __pos2, __len);
00922       if (!__r)
00923     __r = __n1 - __n2;
00924       return __r;
00925     }
00926 
00927   template<typename _CharT, typename _Traits, typename _Alloc>
00928     int
00929     basic_string<_CharT, _Traits, _Alloc>::
00930     compare(const _CharT* __s) const
00931     {
00932       __glibcxx_requires_string(__s);
00933       const size_type __size = this->size();
00934       const size_type __osize = traits_type::length(__s);
00935       const size_type __len = std::min(__size, __osize);
00936       int __r = traits_type::compare(_M_data(), __s, __len);
00937       if (!__r)
00938     __r = __size - __osize;
00939       return __r;
00940     }
00941 
00942   template<typename _CharT, typename _Traits, typename _Alloc>
00943     int
00944     basic_string <_CharT, _Traits, _Alloc>::
00945     compare(size_type __pos, size_type __n1, const _CharT* __s) const
00946     {
00947       __glibcxx_requires_string(__s);
00948       _M_check(__pos, "basic_string::compare");
00949       __n1 = _M_limit(__pos, __n1);
00950       const size_type __osize = traits_type::length(__s);
00951       const size_type __len = std::min(__n1, __osize);
00952       int __r = traits_type::compare(_M_data() + __pos, __s, __len);
00953       if (!__r)
00954     __r = __n1 - __osize;
00955       return __r;
00956     }
00957 
00958   template<typename _CharT, typename _Traits, typename _Alloc>
00959     int
00960     basic_string <_CharT, _Traits, _Alloc>::
00961     compare(size_type __pos, size_type __n1, const _CharT* __s,
00962         size_type __n2) const
00963     {
00964       __glibcxx_requires_string_len(__s, __n2);
00965       _M_check(__pos, "basic_string::compare");
00966       __n1 = _M_limit(__pos, __n1);
00967       const size_type __len = std::min(__n1, __n2);
00968       int __r = traits_type::compare(_M_data() + __pos, __s, __len);
00969       if (!__r)
00970     __r = __n1 - __n2;
00971       return __r;
00972     }
00973 
00974   // Inhibit implicit instantiations for required instantiations,
00975   // which are defined via explicit instantiations elsewhere.
00976   // NB: This syntax is a GNU extension.
00977 #if _GLIBCXX_EXTERN_TEMPLATE
00978   extern template class basic_string<char>;
00979   extern template
00980     basic_istream<char>&
00981     operator>>(basic_istream<char>&, string&);
00982   extern template
00983     basic_ostream<char>&
00984     operator<<(basic_ostream<char>&, const string&);
00985   extern template
00986     basic_istream<char>&
00987     getline(basic_istream<char>&, string&, char);
00988   extern template
00989     basic_istream<char>&
00990     getline(basic_istream<char>&, string&);
00991 
00992 #ifdef _GLIBCXX_USE_WCHAR_T
00993   extern template class basic_string<wchar_t>;
00994   extern template
00995     basic_istream<wchar_t>&
00996     operator>>(basic_istream<wchar_t>&, wstring&);
00997   extern template
00998     basic_ostream<wchar_t>&
00999     operator<<(basic_ostream<wchar_t>&, const wstring&);
01000   extern template
01001     basic_istream<wchar_t>&
01002     getline(basic_istream<wchar_t>&, wstring&, wchar_t);
01003   extern template
01004     basic_istream<wchar_t>&
01005     getline(basic_istream<wchar_t>&, wstring&);
01006 #endif
01007 #endif
01008 } // namespace std
01009 
01010 #endif

Generated on Thu Nov 1 17:35:56 2007 for libstdc++ by  doxygen 1.5.1