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

Generated on Tue Jan 30 17:31:47 2007 for GNU C++ STL by doxygen 1.3.6