This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[v3] Basic_string::assign simplification
- From: Paolo Carlini <pcarlini at unitus dot it>
- To: gcc-patches at gcc dot gnu dot org
- Date: Tue, 18 Dec 2001 13:18:56 +0100
- Subject: [v3] Basic_string::assign simplification
Hi,
I have just committed the following patch which simplifies the new code and adds
the termination symbol at the end of the new string. Approved by Nathan Myers.
Cheers,
Paolo.
///////
2001-12-18 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.
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/basic_string.h,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- gcc/libstdc++-v3/include/bits/basic_string.h 2001/12/16 01:02:17
1.15
+++ gcc/libstdc++-v3/include/bits/basic_string.h 2001/12/18 12:06:00
1.16
@@ -478,26 +478,12 @@
basic_string&
assign(const basic_string& __str, size_type __pos, size_type __n)
{
- if (__pos > __str.size())
+ const size_type __strsize = __str.size();
+ if (__pos > __strsize)
__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;
- }
+ const bool __testn = __n < __strsize - __pos;
+ const size_type __newsize = __testn ? __n : __strsize - __pos;
+ return this->assign(__str._M_data() + __pos, __newsize);
}
basic_string&
@@ -517,6 +503,7 @@
else if (__pos)
traits_type::move(_M_data(), __s, __n);
_M_rep()->_M_length = __n;
+ _M_data()[__n] = _Rep::_S_terminal;
return *this;
}
}