[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: String.indexOf
From: |
John Keiser |
Subject: |
Re: String.indexOf |
Date: |
18 Aug 2001 13:41:15 -0400 |
On 08 Aug 2001 14:15:14 -0400, Patrick Doyle wrote:
>
> 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:
>
>
> +++ String.java 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);
> }
>
I assume the sole purpose of this change is to make it so that a person
can call lastIndexOf("") and have it return count?
I can't think of any other effect the code could possibly have.
--John
- String.indexOf, Patrick Doyle, 2001/08/08
- Re: String.indexOf,
John Keiser <=