[PATCH] Assign(_str, _pos, _n) (take2) + benchmarks
Nathan Myers
ncm-nospam@cantrip.org
Sat Dec 15 02:09:00 GMT 2001
On Fri, Dec 14, 2001 at 12:43:02PM +0100, Paolo Carlini wrote:
> This is the next attempt, much simpler indeed:
>
> basic_string&
> assign(const basic_string& __str, size_type __pos, size_type __n)
> {
> 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 memmove, if possible.
> if (__pos >= __newsize)
> traits_type::copy(_M_data(), __str._M_data() + __pos, __newsize);
> else
> traits_type::move(_M_data(), __str._M_data() + __pos, __newsize);
> _M_rep()->_M_length = __newsize;
> return *this;
> }
> }
What if pos is zero? The code as written will work, and will just
call move() unnecessarily, which on x86 appears to run through the
loop copying all the characters in place. I don't know whether it's
worth optimizing for.
Nathan Myers
ncm at cantrip dot org
More information about the Libstdc++
mailing list