Thanks Roger.

Let's say you have a `String` that is structured in lines, and for each line 
you need to count the occurrences (0 or more) of some character.
A way to process a line is to first search for the next '\n' from the current 
position, then to seach for that character starting from the same position but 
limiting the search up to the position of the '\n', update the current position 
after the character, iterate.
You then advance to the next line and repeat the above.

There are other ways to achieve the same result, but they are probably either 
more complicated or less efficient.

I’m proposing this addition because very similar, non-public methods already 
exist in the codebase, and which are already used to implement `indexOf()`.



As for `lastIndexOf()`, in the forward search it's natural to have `toIndex` 
excluded from the search. There are many instance of right-open ranges 
[`fromIndex`, `toIndex`) in the codebase, so the proposal is to adopt this 
convention for `indexOf(ch, fromIndex, toIndex)`.

For a backward search, however, this is less natural. The non-obvious part is 
to assign natural semantics to `toIndex` for `lastIndexOf(ch, fromIndex, 
toIndex)`. Currently, `lastIndexOf(ch, fromIndex)` operates in the closed range 
of indices [`0`, `fromIndex`].
Should the range be closed [`toIndex`, `fromIndex`] or the left-open 
(`toIndex`, `fromIndex`]? Should `lastIndexOf(ch, fromIndex)` be seen as an 
abbreviation for `lastIndexOf(ch, fromIndex, 0)` or for `lastIndexOf(ch, 
fromIndex, -1)`?

Once there's consensus about this aspect, I can of course add `lastIndexOf(ch, 
fromIndex, toIndex)` as well.



From: core-libs-dev <core-libs-dev-r...@openjdk.org> on behalf of Roger Riggs 
<roger.ri...@oracle.com>
Date: Wednesday, 15 February 2023 at 18:03
To: core-libs-dev@openjdk.org <core-libs-dev@openjdk.org>
Subject: Re: 8302590: Add String.indexOf(int ch, int fromIndex, int toIndex)
Hi Raffaello,

What's the use case, can you give an example?

Seems reasonable; would you also add `lastIndexOf(int ch, int fromIndex, int 
toIndex)`?
Not intrinsified, but for symmetry.

Regards, Roger
On 2/15/23 10:47 AM, Raffaello Giulietti wrote:
Hello,

Currently `String` does not expose a method `indexOf(int ch, int fromIndex, int 
toIndex)`, where the 3rd parameter limits the search to positions up to it 
(exclusive). JBS issue [1] has been filed as an “Enhancement” to add it to the 
codebase.

Before preparing a PR and a CSR, I would like to gather opinions and 
suggestions.

Greetings
Raffaello

----

[1] https://bugs.openjdk.org/browse/JDK-8302590


Reply via email to