This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
patch for bugs in bastring.cc
- To: egcs-patches at cygnus dot com
- Subject: patch for bugs in bastring.cc
- From: Bob Sidebotham <rns at fore dot com>
- Date: Thu, 25 Jun 1998 17:00:24 -0400
- Cc: jason at cygnus dot com
This patch fixes bugs in the find_last* functions in bastring.cc. In
each function there are two bugs. If the length of the string is zero,
then xpos = length() - 1 produces npos, and the function iterates over
non-existent elements of the string. Secondly, the loop for (; xpos;
--xpos) doesn't look at xpos == 0, and hence will never return a
successful result for that position.
The recoding of the loop assumes that npos == size_type(-1).
I've also enclosed an update to the test program tstring.cc to
check for these cases.
Bob Sidebotham
rns@fore.com
*** bastring.cc Thu Jun 25 16:38:35 1998
--- bastring.cc Thu Jun 25 16:42:51 1998
***************
*** 325,334 ****
basic_string <charT, traits, Allocator>::
find_last_of (const charT* s, size_type pos, size_type n) const
{
size_t xpos = length () - 1;
if (xpos > pos)
xpos = pos;
! for (; xpos; --xpos)
if (_find (s, data () [xpos], 0, n) != npos)
return xpos;
return npos;
--- 325,336 ----
basic_string <charT, traits, Allocator>::
find_last_of (const charT* s, size_type pos, size_type n) const
{
+ if (length() == 0)
+ return npos;
size_t xpos = length () - 1;
if (xpos > pos)
xpos = pos;
! for (; xpos != npos; --xpos)
if (_find (s, data () [xpos], 0, n) != npos)
return xpos;
return npos;
***************
*** 363,372 ****
basic_string <charT, traits, Allocator>::
find_last_not_of (const charT* s, size_type pos, size_type n) const
{
size_t xpos = length () - 1;
if (xpos > pos)
xpos = pos;
! for (; xpos; --xpos)
if (_find (s, data () [xpos], 0, n) == npos)
return xpos;
return npos;
--- 365,376 ----
basic_string <charT, traits, Allocator>::
find_last_not_of (const charT* s, size_type pos, size_type n) const
{
+ if (length() == 0)
+ return npos;
size_t xpos = length () - 1;
if (xpos > pos)
xpos = pos;
! for (; xpos != npos; --xpos)
if (_find (s, data () [xpos], 0, n) == npos)
return xpos;
return npos;
***************
*** 377,386 ****
basic_string <charT, traits, Allocator>::
find_last_not_of (charT c, size_type pos) const
{
size_t xpos = length () - 1;
if (xpos > pos)
xpos = pos;
! for (; xpos; --xpos)
if (traits::ne (data () [xpos], c))
return xpos;
return npos;
--- 381,392 ----
basic_string <charT, traits, Allocator>::
find_last_not_of (charT c, size_type pos) const
{
+ if (length() == 0)
+ return npos;
size_t xpos = length () - 1;
if (xpos > pos)
xpos = pos;
! for (; xpos != npos; --xpos)
if (traits::ne (data () [xpos], c))
return xpos;
return npos;
*** tstring.cc Thu Jun 25 16:48:24 1998
--- tstring.cc Thu Jun 25 16:49:05 1998
***************
*** 109,114 ****
--- 105,155 ----
assert(z == "Hello, world.");
}
+ void findtest() {
+ string x;
+ string::size_type pos;
+ pos = x.find_last_not_of('X');
+ assert(pos == string::npos);
+ pos = x.find_last_not_of("XYZ");
+ assert(pos == string::npos);
+
+ string y("a");
+ pos = y.find_last_not_of('X');
+ assert(pos == 0);
+ pos = y.find_last_not_of('a');
+ assert(pos == string::npos);
+ pos = y.find_last_not_of("XYZ");
+ assert(pos == 0);
+ pos = y.find_last_not_of("a");
+ assert(pos == string::npos);
+
+ string z("ab");
+ pos = z.find_last_not_of('X');
+ assert(pos == 1);
+ pos = z.find_last_not_of("XYZ");
+ assert(pos == 1);
+ pos = z.find_last_not_of('b');
+ assert(pos == 0);
+ pos = z.find_last_not_of("Xb");
+ assert(pos == 0);
+ pos = z.find_last_not_of("Xa");
+ assert(pos == 1);
+ pos = z.find_last_of("ab");
+ assert(pos == 1);
+ pos = z.find_last_of("Xa");
+ assert(pos == 0);
+ pos = z.find_last_of("Xb");
+ assert(pos == 1);
+ pos = z.find_last_of("XYZ");
+ assert(pos == string::npos);
+ pos = z.find_last_of('a');
+ assert(pos == 0);
+ pos = z.find_last_of('b');
+ assert(pos == 1);
+ pos = z.find_last_of('X');
+ assert(pos == string::npos);
+ }
+
void comparetest()
{
string x = X;
***************
*** 191,196 ****
--- 229,235 ----
decltest();
cattest();
comparetest();
+ findtest();
substrtest();
identitytest(X, X);
identitytest(X, Y);