[PATCH] Assign clean up

Paolo Carlini pcarlini@unitus.it
Mon Dec 17 12:45:00 GMT 2001


Hi,

this is a nice clean up suggested by Nathan a couple of days ago: assign(__s,
__n) is more general than assign(__str, _pos, __n) and the latter can be
implemented in terms of the former (*). I took the occasion to add that missing
_Rep::_S_terminal at the end of the new string.

This patch should be quite harmless IMHO, and therefore I tested it with a
compiler a few days old. No regressions on my i686-pc-linux-gnu.

Ok to apply?

Thanks,
Paolo.

(*) The same strategy will probably work for insert too...

////////////////

2001-12-17  Paolo Carlini  <pcarlini@unitus.it>
            Nathan Myers  <ncm@cantrip.org>

        * include/bits/basic_string.h (assign(__str, __pos, __n)):
        Call assign(__s, __n).
        (assign(__s, __n)): Terminate the string with _S_terminal.

*** basic_string.h.orig Mon Dec 17 21:23:10 2001
--- basic_string.h Mon Dec 17 21:20:47 2001
*************** namespace std
*** 480,503 ****
        {
   if (__pos > __str.size())
     __throw_out_of_range("basic_string::assign");
!  if (_M_rep()->_M_is_shared() || _M_rep() != __str._M_rep())
!    return _M_replace_safe(_M_ibegin(), _M_iend(),
!      __str._M_check(__pos),
!      __str._M_fold(__pos, __n));
!  else
!    {
!      // Work in-place.
!      bool __testn = __n < __str.size() - __pos;
!      const size_type __newsize = __testn ? __n : __str.size() - __pos;
!      // Avoid move, if possible.
!      if (__pos >= __newsize)
!        traits_type::copy(_M_data(), __str._M_data() + __pos, __newsize);
!      else if (__pos)
!        traits_type::move(_M_data(), __str._M_data() + __pos, __newsize);
!      // else nothing (avoid calling move unnecessarily)
!      _M_rep()->_M_length = __newsize;
!      return *this;
!    }
        }

        basic_string&
--- 480,488 ----
        {
   if (__pos > __str.size())
     __throw_out_of_range("basic_string::assign");
!  const bool __testn = __n < __str.size() - __pos;
!  const size_type __newsize = __testn ? __n : __str.size() - __pos;
!  return this->assign(__str._M_data() + __pos, __newsize);
        }

        basic_string&
*************** namespace std
*** 517,522 ****
--- 502,508 ----
       else if (__pos)
         traits_type::move(_M_data(), __s, __n);
       _M_rep()->_M_length = __n;
+      _M_data()[__n] = _Rep::_S_terminal;
       return *this;
     }
        }






More information about the Libstdc++ mailing list