RFR: 8302590: Add String.indexOf(int ch, int fromIndex, int toIndex) [v2]

Raffaello Giulietti rgiulietti at openjdk.org
Mon Feb 27 18:25:08 UTC 2023


On Mon, 27 Feb 2023 15:05:05 GMT, Alan Bateman <alanb at openjdk.org> wrote:

>> @AlanBateman Both from an `@apiNote` and from an implementation perspective, the checked _some-prefix_`IndexOf()`variants would better make use of the public 3 params `indexOf()` API point proposed here, rather than relying on `StringLatin1` and `StringUTF16` internals.
>> 
>> Thus, parking this PR would mean that the other PR could not refer to the API point from here until this one is integrated. 
>> 
>> Perhaps I'm missing something.
>> Is it perhaps better to add the checked variants directly in this PR?
>
> The suggestion is to write down some alternatives to the 3-arg indexOf method and work through some examples to see if they are less error prone. It might be that you decide to move forward with the current proposal or it might be that you decide a different method would help avoid some common mistakes.

An obvious alternative that comes to mind is an additional `checkedIndexOf(int ch, int fromIndex, int toIndex)` that behaves like `substring()` in checking the indices, and like the 3 params `indexOf()` when not throwing.

Depending on the invocation context, this might help in some cases, in particular when the arguments have not been validated before: a programmer can then rely on the "battery-included" check.

-------------

PR: https://git.openjdk.org/jdk/pull/12600


More information about the core-libs-dev mailing list