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