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

Generated on Tue Apr 21 13:13:25 2009 for libstdc++ by  doxygen 1.5.8