This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] basic_string::_M_clone/c_str changes


Hi,

the _M_clone change is a bug fix, and goes into 3.4 too.

Tested x86-linux, committing to mainline.

Paolo.

////////////
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.
diff -urN libstdc++-v3-orig/include/bits/basic_string.h libstdc++-v3/include/bits/basic_string.h
--- libstdc++-v3-orig/include/bits/basic_string.h	2004-01-17 18:53:52.000000000 +0100
+++ libstdc++-v3/include/bits/basic_string.h	2004-01-18 11:12:09.000000000 +0100
@@ -129,9 +129,8 @@
     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 @@
       */
       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 -urN libstdc++-v3-orig/include/bits/basic_string.tcc libstdc++-v3/include/bits/basic_string.tcc
--- libstdc++-v3-orig/include/bits/basic_string.tcc	2004-01-17 18:55:18.000000000 +0100
+++ libstdc++-v3/include/bits/basic_string.tcc	2004-01-18 11:09:59.000000000 +0100
@@ -616,6 +616,7 @@
 	    }
 	}
       __r->_M_length = this->_M_length;
+      __r->_M_refdata()[this->_M_length] = _Rep::_S_terminal;
       return __r->_M_refdata();
     }
   
diff -urN libstdc++-v3-orig/testsuite/21_strings/basic_string/element_access/char/4.cc libstdc++-v3/testsuite/21_strings/basic_string/element_access/char/4.cc
--- libstdc++-v3-orig/testsuite/21_strings/basic_string/element_access/char/4.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/21_strings/basic_string/element_access/char/4.cc	2004-01-18 11:08:00.000000000 +0100
@@ -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 -urN libstdc++-v3-orig/testsuite/21_strings/basic_string/element_access/wchar_t/4.cc libstdc++-v3/testsuite/21_strings/basic_string/element_access/wchar_t/4.cc
--- libstdc++-v3-orig/testsuite/21_strings/basic_string/element_access/wchar_t/4.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/21_strings/basic_string/element_access/wchar_t/4.cc	2004-01-18 11:07:22.000000000 +0100
@@ -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;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]