[ 
https://issues.apache.org/jira/browse/TEXT-241?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18100210#comment-18100210
 ] 

Maksym Korshun commented on TEXT-241:
-------------------------------------

[~ggregory] Hi! Apologies for the empty ticket. I originally created it as a 
placeholder while gathering the details and logic fix.

I've updated the ticket description above with the summary. I am currently 
working on a patch and unit tests for this and plan to submit a Pull Request 
shortly!

> TextStringBuilder.lastIndexOf("") (and deprecated StrBuilder.lastIndexOf("")) 
> returns incorrect index (size - 1 instead of size) when searching for an 
> empty string.
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: TEXT-241
>                 URL: https://issues.apache.org/jira/browse/TEXT-241
>             Project: Commons Text
>          Issue Type: Bug
>            Reporter: Maksym Korshun
>            Priority: Minor
>
> {{StrBuilder.lastIndexOf(String)}} and 
> {{TextStringBuilder.lastIndexOf(String)}}
> do not match {{java.lang.String}} / {{StringBuilder}} for the empty string.
>  
> For a builder of length n, {{lastIndexOf("")}} should return n (empty match
> at the end). Both implementations currently return n - 1.
>  
> Cause:
> {{lastIndexOf(String, int)}} clamps {{startIndex}} with:
> {{startIndex = startIndex >= size ? size - 1 : startIndex}}
> before the empty-string case, which returns that clamped index. So
> {{lastIndexOf("")}} can never return size.
>  
> Example:
>  
> {code:java}
> new StrBuilder("abab").lastIndexOf("") // actual 3, expected 4
> "abab".lastIndexOf("") // 4
> new StringBuilder("abab").lastIndexOf("") // 4
> {code}
> Same issue in {{TextStringBuilder}}.
>  
> Fix:
> In {{lastIndexOf(String, int)}}, for an empty search string allow the start
> index up to size and return that, keep the existing size - 1 clamp for 
> non-empty strings.
>  
> Affects:
> - org.apache.commons.text.StrBuilder
> - org.apache.commons.text.TextStringBuilder
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to