atharv1205 opened a new pull request, #794:
URL: https://github.com/apache/commons-text/pull/794
`TextStringBuilder.lastIndexOf(String, int)` and the deprecated
`StrBuilder.lastIndexOf(String, int)` treat `startIndex` as the last index a
match may end at. `java.lang.String` and `StringBuilder` treat it as the last
index a match may start at. Single-character searches already follow the JDK,
so the two code paths disagree.
| Builder content | Call | JDK `String.lastIndexOf` | `TextStringBuilder` /
`StrBuilder` (current) |
|---|---|---|---|
| `"abab"` | `lastIndexOf("ab", 2)` | 2 | 0 |
| `"bbbaaaaa"` | `lastIndexOf("aa", 4)` | 4 | 3 |
| `"a,b,c"` | `lastIndexOf("b,c", 2)` | 2 | -1 |
| `"key=value=x"` | `lastIndexOf("=x", 9)` | 9 | -1 |
### Root cause
After clamping `startIndex` to `size - 1`, the search loop starts at
`startIndex - strLen + 1`, so a match that starts after that position (but
still at or before `startIndex`) is never checked. #763 (TEXT-241) fixed only
the empty search string case and kept this loop.
### Precedent
Commons Lang removed the same loop from
`org.apache.commons.lang3.text.StrBuilder.lastIndexOf(String, int)` and
delegated to `StringUtils.lastIndexOf(this, str, startIndex)` in
apache/commons-lang@b6739ab929018ab574946eb9f6f75db6ad30a3c2 (released in
3.13.0). That call later became `Strings.CS.lastIndexOf(this, str, startIndex)`
in apache/commons-lang@6941f81cd321a61217a687c0077fcd0d0a3044a2.
### Fix
Both methods now delegate to `Strings.CS.lastIndexOf(this, str,
startIndex)`, matching Commons Lang. `Strings` is already used elsewhere in
Commons Text (`WordUtils`, `CsvTranslators`). The behavior for a `null` search
string (-1) and an empty search string (TEXT-241) is unchanged.
### Tests
Added to `TextStringBuilderTest` and `StrBuilderTest`:
- `testLastIndexOf_String_int_MatchStartingAtStartIndex`: the four cases
above; `startIndex` greater than, equal to the length, 0 and negative; empty
search string; search string longer than the builder; `null` search string.
- `testLastIndexOf_String_int_MatchesJdk`: for several sample strings and
search strings, asserts `lastIndexOf(x, i) == s.lastIndexOf(x, i)` for every
`i` in `[-1, length + 1]`.
Both new tests fail without the change to the main code. No existing test
was changed.
### Not changed
`lastIndexOf(StringMatcher, int)` (and `lastIndexOf(StrMatcher, int)`) is
unchanged. Should it follow the same semantics in a later change?
---
- [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
- [x] Read the [ASF Generative Tooling
Guidance](https://www.apache.org/legal/generative-tooling.html) if you use
Artificial Intelligence (AI).
- [x] I used AI to create any part of, or all of, this pull request. Which
AI tool was used to create this pull request, and to what extent did it
contribute? I used AI for assistance; I reviewed and verified every line.
- [x] Run a successful build using the default
[Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command
line by itself.
- [x] Write unit tests that match behavioral changes, where the tests fail
if the changes to the runtime are not applied. This may not always be possible,
but it is a best practice.
- [x] Write a pull request description that is detailed enough to understand
what the pull request does, how, and why.
- [x] Each commit in the pull request should have a meaningful subject line
and body. Note that a maintainer may squash commits during the merge process.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]