This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
basic_string<> - operator+
- To: Benjamin Kosnik <bkoz@cygnus.com>
- Subject: basic_string<> - operator+
- From: Ryszard Kabatek <rysio@rumcajs.chemie.uni-halle.de>
- Date: Wed, 23 Jun 1999 13:38:28 +0200 (CEST)
- cc: libstdc++@sourceware.cygnus.com
- Reply-To: Ryszard Kabatek <kabatek@chemie.uni-halle.de>
The operator+ for basic_string<> is curently implemented in the way:
basic_string<> __str(__lhs);
__str.append(__rhs);
return __str;
In the cases where __lhs is a string it is OK (except if __lhs is unsharable).
In other cases (_CharT, _CharT*) it leads to a reallocation.
My proposal:
create an empty string -> call reserve -> append __lhs -> append __rhs.
1999-06-23 Ryszard Kabatek <kabatek@chemie.uni-halle.de>
* bits/basic_string.h:
Call reserve in operator+ to avoid reallocation.
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.37
diff -c -2 -p -r1.37 basic_string.h
*** basic_string.h 1999/06/16 01:46:03 1.37
--- basic_string.h 1999/06/23 11:23:14
*************** namespace std {
*** 796,800 ****
const basic_string<_CharT, _Traits, _Alloc>& __rhs)
{
! basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
__str.append(__rhs);
return __str;
--- 796,804 ----
const basic_string<_CharT, _Traits, _Alloc>& __rhs)
{
! typedef basic_string<_CharT, _Traits, _Alloc> __str_type;
! __str_type::size_type __len = _Traits::length(__lhs);
! __str_type __str;
! __str.reserve(__len + __rhs.size());
! __str.append(__lhs, __lhs + __len);
__str.append(__rhs);
return __str;
*************** namespace std {
*** 805,809 ****
operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
{
! basic_string<_CharT, _Traits, _Alloc> __str(1, __lhs);
__str.append(__rhs);
return __str;
--- 809,815 ----
operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
{
! basic_string<_CharT, _Traits, _Alloc> __str;
! __str.reserve(1 + __rhs.size());
! __str.append(1, __lhs);
__str.append(__rhs);
return __str;