This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

basic_string<>::reserve - patch


As suggested by Nathan Myers I modified the _M_clone member function.
I added a second parameter:
       _CharT*
       _M_clone(const _Alloc&, size_type __res = 0);

Now it can be used in basic_string<>::reserve too. 

In reserve I added a check for _M_state too. Otherwise it would not have
the expected behaviour if the string has enough storage reserved but
it has reference copies.

BTW. In reserve we not check the __res_arg vlaue for a length_error.
     If max_size() is much less than npos we should.
     But I do not know if the standard allows reserve to throw
     a length_error. If not it means that max_size() must return a value
     very close to npos.


The attachment contains the patch.

1999-06-30  Ryszard Kabatek <kabatek@chemie.uni-halle.de>

         * bits/basic_string.h: Add a size_type parameter to _M_clone
           with a default value set to 0.
         * bits/string.tcc: In _M_clone by the call of _S_create
	   add the value of the new parameter to _M_length.
           In reserve check _M_state and use _M_clone instead of _M_mutate.


Ryszard Kabatek
Martin-Luther University Halle-Wittenberg, Department of Physical Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany
Tel. +49 3461 46 2487 (2466) Fax. +49 3461 46 2129
Index: bits/basic_string.h
===================================================================
RCS file: /cvs/libstdc++/libstdc++/bits/basic_string.h,v
retrieving revision 1.40
diff -c -2 -p -r1.40 basic_string.h
*** basic_string.h	1999/06/29 15:41:35	1.40
--- basic_string.h	1999/06/30 09:00:56
*************** namespace std {
*** 186,190 ****
  
  	_CharT* 
! 	_M_clone(const _Alloc&);
  
  #if _GLIBCPP_ALLOC_CONTROL
--- 186,190 ----
  
  	_CharT* 
! 	_M_clone(const _Alloc&, size_type __res = 0);
  
  #if _GLIBCPP_ALLOC_CONTROL
Index: bits/string.tcc
===================================================================
RCS file: /cvs/libstdc++/libstdc++/bits/string.tcc,v
retrieving revision 1.45
diff -c -2 -p -r1.45 string.tcc
*** string.tcc	1999/06/29 18:55:17	1.45
--- string.tcc	1999/06/30 09:00:57
*************** namespace std
*** 292,300 ****
      basic_string<_CharT, _Traits, _Alloc>::reserve(size_type __res_arg)
      {
!       if (__res_arg > this->capacity())
  	{
! 	  size_type __old_size = this->size();
! 	  _M_mutate(__old_size, 0, __res_arg - __old_size);
! 	  _M_rep()->_M_length = __old_size;
  	}
      }
--- 292,301 ----
      basic_string<_CharT, _Traits, _Alloc>::reserve(size_type __res_arg)
      {
!       if (__res_arg > this->capacity() || _M_rep()->_M_state > 0)
  	{
!           allocator_type __a = get_allocator();
!           _CharT* __tmp = _M_rep()->_M_clone(__a, __res_arg - this->size());
!           _M_rep()->_M_dispose(__a);
!           _M_data(__tmp);
  	}
      }
*************** namespace std
*** 361,367 ****
      _CharT*
      basic_string<_CharT, _Traits, _Alloc>::_Rep::
!     _M_clone(const _Alloc& __alloc)
      {
!       _Rep* __r = _Rep::_S_create(_M_length, __alloc);
        if (_M_length)
  	{
--- 362,368 ----
      _CharT*
      basic_string<_CharT, _Traits, _Alloc>::_Rep::
!     _M_clone(const _Alloc& __alloc, size_type __res)
      {
!       _Rep* __r = _Rep::_S_create(_M_length + __res, __alloc);
        if (_M_length)
  	{

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]