Bug in String.lastIndexOf

olivier olivier at envoii.com
Thu Mar 8 14:35:25 PST 2001


If this bug is not yet corrected, here is the fix:
In String.lastIndexOf, replace
     int it = offset+eIdx+ic;
by
     int it = offset+eIdx+str.count-1;
Actually, ic contains str.offset, this explains why lastIndexOf does not
work if the searched substring has an offset !=0 (this is the case when one
uses the substring method).

Here is another regression test:
 String s1="toto le heros",s2="xxtoto le heros".substring(2);
 String sub1="le",sub2="xxxle".substring(3); // test with substring so
offset inside String object is >0
 for(int pos=-2;pos<s1.length()+3;pos++) {
  int found1=s1.lastIndexOf(sub1,pos);
  int found2=s2.lastIndexOf(sub2,pos);
  dbg.assert(found1==found2,"li1");
  if(found1!=-1) {
   dbg.assert(s1.startsWith(sub1,found1) && found1<=pos,"li2"); // as said
in Java spec
   dbg.assert(s2.startsWith(sub2,found2) && found2<=pos,"li3");
  }
 }

----- Original Message -----
From: "Peter Graves" <peter at armedbear.org>
To: "Archie Cobbs" <archie at dellroad.org>; "Edouard G. Parmelan"
<egp at free.fr>; "Tim Wilkinson" <tim at transvirtual.com>
Cc: <kaffe at rufus.w3.org>
Sent: Saturday, October 28, 2000 7:39 PM
Subject: Bug in String.lastIndexOf


>
> There's a bug in java.lang.String lastIndexOf(String, int).
>
> Here's a short test program that illustrates the problem:
>
> class t
> {
>     public static void main(String[] args)
>     {
>         String s1 = "this is a test yes it is";
>         String s2 = "yet another test";
>         String s3 = s2.substring(12); // "test"
>         for (int i = 0; i < s1.length(); i++)
>             System.err.println("i = " + i + " index = " +
s1.lastIndexOf(s3, i));
>     }
> }
>
> If you run this program with kaffe (current CVS), the
> lastIndexOf call returns 10 for -all- values of i.
>
> It should return -1 for i < 10 (and does so with IBM 1.3, for
> example).
>
> -Peter
>
>



More information about the kaffe mailing list