[TEXT-241] TextStringBuilder.lastIndexOf("") and StrBuilder.lastIndexOf("") return incorrect index for empty string (size - 1 instead of size) - #763
Merged
garydgregory merged 3 commits intoJul 30, 2026
Conversation
Contributor
Author
|
Jira ticket TEXT-241 |
Contributor
Author
|
Additionally, I have question on what code is preferred |
Member
|
@kzhunmax |
There was a problem hiding this comment.
Pull request overview
Fixes lastIndexOf("") behavior in StrBuilder and TextStringBuilder to match java.lang.String / StringBuilder semantics (for a builder of length n, lastIndexOf("") returns n).
Changes:
- Update
lastIndexOf(String)to delegate usingstartIndex = sizeso empty-search can returnsize. - Adjust
lastIndexOf(String, int)clamping so empty search strings allowstartIndexup tosize, while non-empty searches still clamp tosize - 1. - Add regression tests for
lastIndexOf("")on a non-empty builder.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/main/java/org/apache/commons/text/TextStringBuilder.java | Fixes empty-string lastIndexOf clamping to allow returning size. |
| src/main/java/org/apache/commons/text/StrBuilder.java | Same fix as TextStringBuilder for parity with JDK behavior. |
| src/test/java/org/apache/commons/text/TextStringBuilderTest.java | Adds regression assertion for lastIndexOf("") returning the builder length. |
| src/test/java/org/apache/commons/text/StrBuilderTest.java | Adds regression assertion for lastIndexOf("") and adds @Deprecated annotation to match the existing Javadoc deprecation tag. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
|
Merged 🚀 , thank you @kzhunmax |
garydgregory
added a commit
that referenced
this pull request
Jul 30, 2026
StrBuilder.lastIndexOf("") return incorrect index for empty string (size
- 1 instead of size) (#763).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
StrBuilder.lastIndexOf(String)andextStringBuilder.lastIndexOf(String)do not match
java.lang.String/StringBuilderfor the empty string.For a builder of length n,
lastIndexOf("")should return n (empty matchat the end). Both implementations currently return
n - 1.Cause:
lastIndexOf(String, int)clamps startIndex with:startIndex = startIndex >= size ? size - 1 : startIndexbefore the empty-string case, which returns that clamped index. So
lastIndexOf("")can never return size.Example:
Fix:
In
lastIndexOf(String, int), for an empty search string allow the startindex up to size and return that, keep the existing
size - 1clamp for non-empty strings.Affects: