diff options
author | paolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4> | 2004-01-18 10:51:13 +0000 |
---|---|---|
committer | paolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4> | 2004-01-18 10:51:13 +0000 |
commit | f019e348b742f068e8f10626d89f6e465d2fb01f (patch) | |
tree | 458444ccb1b5f0e88dd4126c4113a0638daa4107 | |
parent | 513767461f801f6f8a64156098d5ac4d52b8c9c2 (diff) | |
download | ppe42-gcc-f019e348b742f068e8f10626d89f6e465d2fb01f.tar.gz ppe42-gcc-f019e348b742f068e8f10626d89f6e465d2fb01f.zip |
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.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@76092 138bc75d-0d04-0410-961f-82ee72b054a4
5 files changed, 113 insertions, 9 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 5590ce494e4..6470dbb802c 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,14 @@ 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)): Moved inline, just call _M_replace_aux, no source iterators at risk of being clobbered. diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index d9847a4b32b..69a9f2dc86e 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -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. diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 5f50d3694a3..bb39df3f370 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -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(); } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/element_access/char/4.cc b/libstdc++-v3/testsuite/21_strings/basic_string/element_access/char/4.cc new file mode 100644 index 00000000000..3edc543b32b --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/element_access/char/4.cc @@ -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; +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/element_access/wchar_t/4.cc b/libstdc++-v3/testsuite/21_strings/basic_string/element_access/wchar_t/4.cc new file mode 100644 index 00000000000..18b72f0a15f --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/element_access/wchar_t/4.cc @@ -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; +} |