[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


From: Patrick Doyle
Subject: String.indexOf
Date: Wed, 8 Aug 2001 14:15:14 -0400 (EDT)

I have made some modificatinos to String.indexOf and String.lastIndexOf so
they better handle empty strings.  (Does Mauve test such cases?)

I don't have a great deal of confidence in the correctness of these
changes, but as far as I can tell, they do bring the code closer to the
wording of the String spec by fixing a few off-by-one errors.  Does
someone else want to put their brain on this to see if it makes sense?  
(Consider empty needle and haystack strings.)

Here's the patch.  It may not be 100% kosher because I have trimmed out
some other modifications I made (which you don't want), but the changes
are simple enough that they could be applied by hand if necessary:

+++ Wed Aug  8 14:04:28 2001
@@ -688,7 +689,7 @@
   public int indexOf(String str, int fromIndex) throws
NullPointerException {
     if (fromIndex < 0) fromIndex = 0;
-    for (int i = fromIndex; i < count; i++)
+    for (int i = fromIndex; i <= count; i++)
       if (regionMatches(i, str, 0, str.count))
        return i;
     return -1;
@@ -735,7 +736,7 @@
    * @exception NullPointerException if `str' is null
   public int lastIndexOf(String str) throws NullPointerException {
-    return lastIndexOf(str, count-1);
+    return lastIndexOf(str, count);

Patrick Doyle

reply via email to

[Prev in Thread] Current Thread [Next in Thread]