]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR libstdc++/13650 (string::compare should not (always) use traits_type::length())
authorPaolo Carlini <pcarlini@suse.de>
Tue, 13 Jan 2004 11:12:38 +0000 (11:12 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Tue, 13 Jan 2004 11:12:38 +0000 (11:12 +0000)
2004-01-13  Paolo Carlini  <pcarlini@suse.de>

PR libstdc++/13650
* include/bits/basic_string.tcc (compare(size_type, size_type,
const _CharT*, size_type)): Implement correctly the resolution
of DR 5: basically, s is a char array, -not- a C string.
* include/bits/basic_string.h: Tweak some comments.
* testsuite/21_strings/basic_string/compare/char/13650.cc: New.
* testsuite/21_strings/basic_string/compare/wchar_t/13650.cc: New.

From-SVN: r75798

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

index 7ffeb3b23b9328d34cbd3f33386cdf8629cbe9f4..d3f86f7be9a59420ed0c3653429f51302efcfbea 100644 (file)
@@ -1,3 +1,13 @@
+2004-01-13  Paolo Carlini  <pcarlini@suse.de>
+
+       PR libstdc++/13650
+       * include/bits/basic_string.tcc (compare(size_type, size_type,
+       const _CharT*, size_type)): Implement correctly the resolution
+       of DR 5: basically, s is a char array, -not- a C string.
+       * include/bits/basic_string.h: Tweak some comments.
+       * testsuite/21_strings/basic_string/compare/char/13650.cc: New.
+       * testsuite/21_strings/basic_string/compare/wchar_t/13650.cc: New.
+
 2004-01-12  Andreas Tobler  <a.tobler@schweiz.ch>
 
        * testsuite/lib/libstdc++.exp: Set LD_LIBRARY_PATH_32 for
index e3d19b295a96d1340ba948c13908c247a103df3c..0b679e0c081146db664e08ea40ff446b945155da 100644 (file)
@@ -379,10 +379,13 @@ namespace std
                   size_type __n, const _Alloc& __a);
 
       /**
-       *  @brief  Construct string as copy of a C substring.
-       *  @param  s  Source C string.
+       *  @brief  Construct string initialized by a character array.
+       *  @param  s  Source character array.
        *  @param  n  Number of characters to copy.
        *  @param  a  Allocator to use (default is default allocator).
+       *  
+       *  NB: s must have at least n characters, '\0' has no special
+       *  meaning.
        */
       basic_string(const _CharT* __s, size_type __n,
                   const _Alloc& __a = _Alloc());
@@ -1955,21 +1958,23 @@ namespace std
       compare(size_type __pos, size_type __n1, const _CharT* __s) const;
 
       /**
-       *  @brief  Compare substring against a C substring.
+       *  @brief  Compare substring against a character array.
        *  @param pos1  Index of first character of substring.
        *  @param n1  Number of characters in substring.
-       *  @param s  C string to compare against.
-       *  @param n2  Number of characters in substring of s.
+       *  @param s  character array to compare against.
+       *  @param n2  Number of characters of s.
        *  @return  Integer < 0, 0, or > 0.
        *
        *  Form the substring of this string from the @a n1 characters starting
-       *  at @a pos1.  Form the substring of @a s from the first @a n
-       *  characters of @a s.  Returns an integer < 0 if this substring is
-       *  ordered before the substring of @a s, 0 if their values are
-       *  equivalent, or > 0 if this substring is ordered after the substring
-       *  of @a s.  If the lengths of this substring and @a n are different,
-       *  the shorter one is ordered first.  If they are the same, returns the
-       *  result of traits::compare(substring.data(),s,size());
+       *  at @a pos1.  Form a string from the first @a n2 characters of @a s.
+       *  Returns an integer < 0 if this substring is ordered before the string
+       *  from @a s, 0 if their values are equivalent, or > 0 if this substring
+       *  is ordered after the string from @a s. If the lengths of this substring
+       *  and @a n2 are different, the shorter one is ordered first.  If they are
+       *  the same, returns the result of traits::compare(substring.data(),s,size());
+       *
+       *  NB: s must have at least n2 characters, '\0' has no special
+       *  meaning.
       */
       int
       compare(size_type __pos, size_type __n1, const _CharT* __s,
index 3a1e8468eb030f33e09da13c2380de7b072d2333..87eac7adda812f1c750d92545b0edf9e5be937a3 100644 (file)
@@ -1080,12 +1080,11 @@ namespace std
       if (__pos > __size)
        __throw_out_of_range("basic_string::compare");
       
-      const size_type __osize = std::min(traits_type::length(__s), __n2);
       const size_type __rsize = std::min(size_type(__size - __pos), __n1);
-      const size_type __len = std::min(__rsize, __osize);
+      const size_type __len = std::min(__rsize, __n2);
       int __r = traits_type::compare(_M_data() + __pos, __s, __len);
       if (!__r)
-       __r = __rsize - __osize;
+       __r = __rsize - __n2;
       return __r;
     }
 
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/compare/char/13650.cc b/libstdc++-v3/testsuite/21_strings/basic_string/compare/char/13650.cc
new file mode 100644 (file)
index 0000000..d1ed3d4
--- /dev/null
@@ -0,0 +1,48 @@
+// 2004-01-13  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2004 Free Software Foundation
+//
+// 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.6.8 basic_string::compare [lib.string::compare]
+
+#include <string>
+#include <testsuite_hooks.h>
+
+// libstdc++/13650
+void test01()
+{
+  using namespace std;
+  bool test __attribute__((unused)) = true;
+
+  const char lit_01[] = { 'w', 'e', '\0', 'r', 'd' };
+  const char lit_02[] = { 'w', 'e', 'i', '\0', 'd' };
+
+  const char lit_ref_a[] = { 'w', 'e', '\0', 'q', 'd' };
+  const string str_a(lit_ref_a, 5);
+  VERIFY( str_a.compare(0, 5, lit_01, 5) < 0 );
+
+  const char lit_ref_b[] = { 'w', 'e', 'i' };
+  const string str_b(lit_ref_b, 3);
+  VERIFY( str_b.compare(0, 3, lit_02, 5) < 0 );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/compare/wchar_t/13650.cc b/libstdc++-v3/testsuite/21_strings/basic_string/compare/wchar_t/13650.cc
new file mode 100644 (file)
index 0000000..13e3ca2
--- /dev/null
@@ -0,0 +1,48 @@
+// 2004-01-13  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2004 Free Software Foundation
+//
+// 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.6.8 basic_string::compare [lib.string::compare]
+
+#include <string>
+#include <testsuite_hooks.h>
+
+// libstdc++/13650
+void test01()
+{
+  using namespace std;
+  bool test __attribute__((unused)) = true;
+
+  const wchar_t lit_01[] = { L'w', L'e', L'\0', L'r', L'd' };
+  const wchar_t lit_02[] = { L'w', L'e', L'i', L'\0', L'd' };
+
+  const wchar_t lit_ref_a[] = { L'w', L'e', L'\0', L'q', L'd' };
+  const wstring str_a(lit_ref_a, 5);
+  VERIFY( str_a.compare(0, 5, lit_01, 5) < 0 );
+
+  const wchar_t lit_ref_b[] = { L'w', L'e', L'i' };
+  const wstring str_b(lit_ref_b, 3);
+  VERIFY( str_b.compare(0, 3, lit_02, 5) < 0 );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}