From 95c9624ff55c571983f7278edeceff8a90042d84 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Tue, 13 Jan 2004 11:12:38 +0000 Subject: [PATCH] re PR libstdc++/13650 (string::compare should not (always) use traits_type::length()) 2004-01-13 Paolo Carlini 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 | 10 ++++ libstdc++-v3/include/bits/basic_string.h | 29 ++++++----- libstdc++-v3/include/bits/basic_string.tcc | 5 +- .../basic_string/compare/char/13650.cc | 48 +++++++++++++++++++ .../basic_string/compare/wchar_t/13650.cc | 48 +++++++++++++++++++ 5 files changed, 125 insertions(+), 15 deletions(-) create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/compare/char/13650.cc create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/compare/wchar_t/13650.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 7ffeb3b23b9..d3f86f7be9a 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,13 @@ +2004-01-13 Paolo Carlini + + 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 * testsuite/lib/libstdc++.exp: Set LD_LIBRARY_PATH_32 for diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index e3d19b295a9..0b679e0c081 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -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, diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 3a1e8468eb0..87eac7adda8 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -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 index 00000000000..d1ed3d4fa45 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/compare/char/13650.cc @@ -0,0 +1,48 @@ +// 2004-01-13 Paolo Carlini + +// 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 +#include + +// 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 index 00000000000..13e3ca26019 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/compare/wchar_t/13650.cc @@ -0,0 +1,48 @@ +// 2004-01-13 Paolo Carlini + +// 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 +#include + +// 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; +}