]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
basic_string.h (c_str()): Simplify, due to 21.3.4 the internal representation is...
authorPaolo Carlini <pcarlini@suse.de>
Sun, 18 Jan 2004 10:51:13 +0000 (10:51 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Sun, 18 Jan 2004 10:51:13 +0000 (10:51 +0000)
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

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/basic_string.h
libstdc++-v3/include/bits/basic_string.tcc
libstdc++-v3/testsuite/21_strings/basic_string/element_access/char/4.cc [new file with mode: 0644]
libstdc++-v3/testsuite/21_strings/basic_string/element_access/wchar_t/4.cc [new file with mode: 0644]

index 5590ce494e45f33d32b9288b135b40af440a5326..6470dbb802c077f7e4594116fc04a9d2d8b7fef5 100644 (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)):
index d9847a4b32b15809801d9602b23130135fd111b7..69a9f2dc86edd8555b5ee5547e7f42ce47a55ead 100644 (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.
index 5f50d3694a33031848cd3d37fbfdc08f8dfb9664..bb39df3f370ffb1f7ea176f1ff3b1f2fb63203d4 100644 (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();
     }
   
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 (file)
index 0000000..3edc543
--- /dev/null
@@ -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 (file)
index 0000000..18b72f0
--- /dev/null
@@ -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;
+}