basic_string.h (c_str()): Simplify, due to 21.3.4 the internal representation is always kept null-terminated.

2004-01-18  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/basic_string.h (c_str()): Simplify, due to
	21.3.4 the internal representation is always kept null-terminated.
	* include/bits/basic_string.tcc (_M_clone): Null-terminate.
	* testsuite/21_strings/basic_string/element_access/char/4.cc: New.
	* testsuite/21_strings/basic_string/element_access/wchar_t/4.cc: Ditto.

From-SVN: r76092
This commit is contained in:
Paolo Carlini 2004-01-18 10:51:13 +00:00 committed by Paolo Carlini
parent 9c96a6896b
commit acbab5bf1c
5 changed files with 113 additions and 9 deletions

View file

@ -1,3 +1,12 @@
2004-01-18 Paolo Carlini <pcarlini@suse.de>
* include/bits/basic_string.h (c_str()): Simplify, due to
21.3.4 the internal representation is always kept null-terminated.
* include/bits/basic_string.tcc (_M_clone): Null-terminate.
* testsuite/21_strings/basic_string/element_access/char/4.cc: New.
* testsuite/21_strings/basic_string/element_access/wchar_t/4.cc:
Ditto.
2004-01-18 Paolo Carlini <pcarlini@suse.de>
* include/bits/basic_string.h (append(size_type, _CharT)):

View file

@ -129,9 +129,8 @@ namespace std
private:
// _Rep: string representation
// Invariants:
// 1. String really contains _M_length + 1 characters; last is set
// to 0 only on call to c_str(). We avoid instantiating
// _CharT() where the interface does not require it.
// 1. String really contains _M_length + 1 characters: due to 21.3.4
// must be kept null-terminated.
// 2. _M_capacity >= _M_length
// Allocated memory is always _M_capacity + (1 * sizeof(_CharT)).
// 3. _M_refcount has three states:
@ -1457,12 +1456,7 @@ namespace std
*/
const _CharT*
c_str() const
{
// MT: This assumes concurrent writes are OK.
const size_type __n = this->size();
traits_type::assign(_M_data()[__n], _Rep::_S_terminal);
return _M_data();
}
{ return _M_data(); }
/**
* @brief Return const pointer to contents.

View file

@ -616,6 +616,7 @@ namespace std
}
}
__r->_M_length = this->_M_length;
__r->_M_refdata()[this->_M_length] = _Rep::_S_terminal;
return __r->_M_refdata();
}

View file

@ -0,0 +1,50 @@
// 2004-01-18 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.4 basic_string element access
#include <string>
#include <testsuite_hooks.h>
// http://gcc.gnu.org/ml/libstdc++/2004-01/msg00184.html
void test01()
{
bool test __attribute__((unused)) = true;
using namespace std;
for (int i = 0; i < 2000; ++i)
{
string str_01;
for (int j = 0; j < i; ++j)
str_01 += 'a';
str_01.reserve(i + 10);
const string str_02(str_01);
VERIFY( str_02[i] == '\0' );
}
}
int main()
{
test01();
return 0;
}

View file

@ -0,0 +1,50 @@
// 2004-01-18 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.4 basic_string element access
#include <string>
#include <testsuite_hooks.h>
// http://gcc.gnu.org/ml/libstdc++/2004-01/msg00184.html
void test01()
{
bool test __attribute__((unused)) = true;
using namespace std;
for (int i = 0; i < 2000; ++i)
{
wstring str_01;
for (int j = 0; j < i; ++j)
str_01 += L'a';
str_01.reserve(i + 10);
const wstring str_02(str_01);
VERIFY( str_02[i] == L'\0' );
}
}
int main()
{
test01();
return 0;
}