Commit ec61e852 by Paolo Carlini Committed by Paolo Carlini

basic_string.h (_Rep::_M_is_safe, [...]): New, use througout.

2004-10-25  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/basic_string.h (_Rep::_M_is_safe, _M_check_length,
	_M_move, _M_copy, _M_assign): New, use througout.
	(operator+=(_CharT)): Define in terms of push_back.
	(append(const basic_string&)): Define here, inline, and simplify,
	don't use the full _M_replace_safe.
	(append(size_type, _CharT)): Likewise, don't use _M_replace_aux.
	(push_back): Likewise.
	(assign(const basic_string&)): Define here, inline.
	* include/bits/basic_string.tcc (append(const _CharT* s, size_type):
	Fix: when s points inside the _Rep, upon reallocation (reserve) we
	were copying from deallocated memory.
	(append(const basic_string&, size_type, size_type)): Simplify,
	don't use _M_replace_safe.
	(replace(size_type, size_type, const _CharT*, size_type)): Slightly
	tweak.
	(reserve): Likewise.
	* testsuite/21_strings/basic_string/append/char/2.cc: New.
	* testsuite/21_strings/basic_string/append/char/3.cc: Likewise.
	* testsuite/21_strings/basic_string/append/wchar_t/2.cc: Likewise.
	* testsuite/21_strings/basic_string/append/wchar_t/3.cc: Likewise.

	* testsuite/21_strings/basic_string/assign/char/3.cc: Remove junk.
	* testsuite/21_strings/basic_string/assign/wchar_t/3.cc: Likewise.

From-SVN: r89526
parent 4318d4a1
2004-10-25 Paolo Carlini <pcarlini@suse.de>
* include/bits/basic_string.h (_Rep::_M_is_safe, _M_check_length,
_M_move, _M_copy, _M_assign): New, use througout.
(operator+=(_CharT)): Define in terms of push_back.
(append(const basic_string&)): Define here, inline, and simplify,
don't use the full _M_replace_safe.
(append(size_type, _CharT)): Likewise, don't use _M_replace_aux.
(push_back): Likewise.
(assign(const basic_string&)): Define here, inline.
* include/bits/basic_string.tcc (append(const _CharT* s, size_type):
Fix: when s points inside the _Rep, upon reallocation (reserve) we
were copying from deallocated memory.
(append(const basic_string&, size_type, size_type)): Simplify,
don't use _M_replace_safe.
(replace(size_type, size_type, const _CharT*, size_type)): Slightly
tweak.
(reserve): Likewise.
* testsuite/21_strings/basic_string/append/char/2.cc: New.
* testsuite/21_strings/basic_string/append/char/3.cc: Likewise.
* testsuite/21_strings/basic_string/append/wchar_t/2.cc: Likewise.
* testsuite/21_strings/basic_string/append/wchar_t/3.cc: Likewise.
* testsuite/21_strings/basic_string/assign/char/3.cc: Remove junk.
* testsuite/21_strings/basic_string/assign/wchar_t/3.cc: Likewise.
2004-10-23 Andrew Pinski <pinskia@physics.uc.edu>
* testsuite/ext/mt_allocator/deallocate_global-2.c:
......
......@@ -185,6 +185,14 @@ namespace std
_M_is_shared() const
{ return this->_M_refcount > 0; }
// True if source and destination do not overlap.
bool
_M_is_safe(const _CharT* __data, const _CharT* __s) const
{
return (less<const _CharT*>()(__s, __data)
|| less<const _CharT*>()(__data + this->_M_length, __s));
}
void
_M_set_leaked()
{ this->_M_refcount = -1; }
......@@ -302,6 +310,13 @@ namespace std
return __pos;
}
void
_M_check_length(size_type __n1, size_type __n2, const char* __s) const
{
if (this->max_size() - (this->size() - __n1) < __n2)
__throw_length_error(__N(__s));
}
// NB: _M_limit doesn't check for a bad __pos value.
size_type
_M_limit(size_type __pos, size_type __off) const
......@@ -310,6 +325,35 @@ namespace std
return __testoff ? __off : this->size() - __pos;
}
// When __n = 1 way faster than the general multichar
// traits_type::copy/move/assign.
static void
_M_copy(_CharT* __d, const _CharT* __s, size_type __n)
{
if (__n == 1)
traits_type::assign(*__d, *__s);
else
traits_type::copy(__d, __s, __n);
}
static void
_M_move(_CharT* __d, const _CharT* __s, size_type __n)
{
if (__n == 1)
traits_type::assign(*__d, *__s);
else
traits_type::move(__d, __s, __n);
}
static void
_M_assign(_CharT* __d, size_type __n, _CharT __c)
{
if (__n == 1)
traits_type::assign(*__d, __c);
else
traits_type::assign(__d, __n, __c);
}
// _S_copy_chars is a separate template to permit specialization
// to optimize for the common case of pointers as iterators.
template<class _Iterator>
......@@ -330,11 +374,11 @@ namespace std
static void
_S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
{ traits_type::copy(__p, __k1, __k2 - __k1); }
{ _M_copy(__p, __k1, __k2 - __k1); }
static void
_S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
{ traits_type::copy(__p, __k1, __k2 - __k1); }
{ _M_copy(__p, __k1, __k2 - __k1); }
void
_M_mutate(size_type __pos, size_type __len1, size_type __len2);
......@@ -721,7 +765,10 @@ namespace std
*/
basic_string&
operator+=(_CharT __c)
{ return this->append(size_type(1), __c); }
{
this->push_back(__c);
return *this;
}
/**
* @brief Append a string to this string.
......@@ -729,7 +776,19 @@ namespace std
* @return Reference to this string.
*/
basic_string&
append(const basic_string& __str);
append(const basic_string& __str)
{
const size_type __size = __str.size();
if (__size)
{
const size_type __len = __size + this->size();
if (__len > this->capacity() || _M_rep()->_M_is_shared())
this->reserve(__len);
_M_copy(_M_data() + this->size(), __str._M_data(), __size);
_M_rep()->_M_set_length_and_sharable(__len);
}
return *this;
}
/**
* @brief Append a substring.
......@@ -777,7 +836,18 @@ namespace std
*/
basic_string&
append(size_type __n, _CharT __c)
{ return _M_replace_aux(this->size(), size_type(0), __n, __c); }
{
if (__n)
{
_M_check_length(size_type(0), __n, "basic_string::append");
const size_type __len = __n + this->size();
if (__len > this->capacity() || _M_rep()->_M_is_shared())
this->reserve(__len);
_M_assign(_M_data() + this->size(), __n, __c);
_M_rep()->_M_set_length_and_sharable(__len);
}
return *this;
}
/**
* @brief Append a range of characters.
......@@ -798,7 +868,13 @@ namespace std
*/
void
push_back(_CharT __c)
{ _M_replace_aux(this->size(), size_type(0), size_type(1), __c); }
{
const size_type __len = 1 + this->size();
if (__len > this->capacity() || _M_rep()->_M_is_shared())
this->reserve(__len);
traits_type::assign(_M_data()[this->size()], __c);
_M_rep()->_M_set_length_and_sharable(__len);
}
/**
* @brief Set value to contents of another string.
......@@ -806,7 +882,18 @@ namespace std
* @return Reference to this string.
*/
basic_string&
assign(const basic_string& __str);
assign(const basic_string& __str)
{
if (_M_rep() != __str._M_rep())
{
// XXX MT
const allocator_type __a = this->get_allocator();
_CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
_M_rep()->_M_dispose(__a);
_M_data(__tmp);
}
return *this;
}
/**
* @brief Set value to a substring of a string.
......@@ -909,7 +996,8 @@ namespace std
* of the string doesn't change if an error is thrown.
*/
template<class _InputIterator>
void insert(iterator __p, _InputIterator __beg, _InputIterator __end)
void
insert(iterator __p, _InputIterator __beg, _InputIterator __end)
{ this->replace(__p, __p, __beg, __end); }
/**
......@@ -1370,13 +1458,10 @@ namespace std
_M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
_CharT __c)
{
if (this->max_size() - (this->size() - __n1) < __n2)
__throw_length_error(__N("basic_string::_M_replace_aux"));
_M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
_M_mutate(__pos1, __n1, __n2);
if (__n2 == 1)
_M_data()[__pos1] = __c;
else if (__n2)
traits_type::assign(_M_data() + __pos1, __n2, __c);
if (__n2)
_M_assign(_M_data() + __pos1, __n2, __c);
return *this;
}
......@@ -1385,10 +1470,8 @@ namespace std
size_type __n2)
{
_M_mutate(__pos1, __n1, __n2);
if (__n2 == 1)
_M_data()[__pos1] = *__s;
else if (__n2)
traits_type::copy(_M_data() + __pos1, __s, __n2);
if (__n2)
_M_copy(_M_data() + __pos1, __s, __n2);
return *this;
}
......
......@@ -18,7 +18,7 @@
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// 21.3.5.3 basic_string::assign
// 21.3.5.2 basic_string::append
#include <string>
#include <stdexcept>
......
// 2004-25-10 Paolo Carlini <pcarlini@suse.de>
// Copyright (C) 2004 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// 21.3.5 string modifiers
#include <string>
#include <testsuite_hooks.h>
// append(const _CharT* __s, size_type __n)
// append(const _CharT* __s)
void
test02()
{
bool test __attribute__((unused)) = true;
using namespace std;
string one;
string two;
string three;
const char * source = "Written in your eyes";
one.append(source);
VERIFY( one == "Written in your eyes" );
two.append(source, 20);
VERIFY( two == "Written in your eyes" );
three.append(source, 7);
VERIFY( three == "Written" );
three.clear();
three.append(source + 8, 2);
VERIFY( three == "in" );
one.append(one.c_str(), 20);
VERIFY( one == "Written in your eyesWritten in your eyes" );
two.append(two.c_str() + 16, 4);
VERIFY( two == "Written in your eyeseyes" );
two.append(two.c_str(), 3);
VERIFY( two == "Written in your eyeseyesWri" );
}
int main()
{
test02();
return 0;
}
// 2004-25-10 Paolo Carlini <pcarlini@suse.de>
// Copyright (C) 2004 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// 21.3.5 string modifiers
#include <string>
#include <testsuite_hooks.h>
// Upon reallocation (basic_string::reserve) we were copying from
// deallocated memory.
void
test03()
{
bool test __attribute__((unused)) = true;
using namespace std;
const char * source = "Kesto";
for (unsigned i = 0; i < 10; ++i)
{
string one(source);
string two(source);
for (unsigned j = 0; j < 18; ++j)
{
VERIFY( one == two );
one.append(one);
one += 'x';
two.append(two.c_str(), two.size());
two += 'x';
}
}
}
int main()
{
test03();
return 0;
}
......@@ -18,7 +18,7 @@
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// 21.3.5.3 basic_string::assign
// 21.3.5.2 basic_string::append
#include <string>
#include <stdexcept>
......
// 2004-25-10 Paolo Carlini <pcarlini@suse.de>
// Copyright (C) 2004 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// 21.3.5 string modifiers
#include <string>
#include <testsuite_hooks.h>
// append(const _CharT* __s, size_type __n)
// append(const _CharT* __s)
void
test02()
{
bool test __attribute__((unused)) = true;
using namespace std;
wstring one;
wstring two;
wstring three;
const wchar_t * source = L"Written in your eyes";
one.append(source);
VERIFY( one == L"Written in your eyes" );
two.append(source, 20);
VERIFY( two == L"Written in your eyes" );
three.append(source, 7);
VERIFY( three == L"Written" );
three.clear();
three.append(source + 8, 2);
VERIFY( three == L"in" );
one.append(one.c_str(), 20);
VERIFY( one == L"Written in your eyesWritten in your eyes" );
two.append(two.c_str() + 16, 4);
VERIFY( two == L"Written in your eyeseyes" );
two.append(two.c_str(), 3);
VERIFY( two == L"Written in your eyeseyesWri" );
}
int main()
{
test02();
return 0;
}
// 2004-25-10 Paolo Carlini <pcarlini@suse.de>
// Copyright (C) 2004 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// 21.3.5 string modifiers
#include <string>
#include <testsuite_hooks.h>
// Upon reallocation (basic_string::reserve) we were copying from
// deallocated memory.
void
test03()
{
bool test __attribute__((unused)) = true;
using namespace std;
const wchar_t * source = L"Kesto";
for (unsigned i = 0; i < 10; ++i)
{
wstring one(source);
wstring two(source);
for (unsigned j = 0; j < 18; ++j)
{
VERIFY( one == two );
one.append(one);
one += L'x';
two.append(two.c_str(), two.size());
two += L'x';
}
}
}
int main()
{
test03();
return 0;
}
// 2001-10-30 Benjamin Kosnik <bkoz@redhat.com>
// Copyright (C) 2001, 2003 Free Software Foundation, Inc.
// Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
......@@ -21,7 +21,6 @@
// 21.3.5 string modifiers
#include <string>
#include <cstdio>
#include <testsuite_hooks.h>
// assign(const _CharT* __s, size_type __n)
......@@ -35,7 +34,6 @@ test03()
string one;
string two;
string three = two;
const char * source = "Selling England by the pound";
one.assign(source);
......
// 2001-10-30 Benjamin Kosnik <bkoz@redhat.com>
// Copyright (C) 2001, 2003 Free Software Foundation, Inc.
// Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
......@@ -21,7 +21,6 @@
// 21.3.5 string modifiers
#include <string>
#include <cstdio>
#include <testsuite_hooks.h>
// assign(const _CharT* __s, size_type __n)
......@@ -35,7 +34,6 @@ test03()
wstring one;
wstring two;
wstring three = two;
const wchar_t* source = L"Selling England by the pound";
one.assign(source);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment