[ 
https://issues.apache.org/jira/browse/TEXT-241?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Maksym Korshun closed TEXT-241.
-------------------------------

> 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
>             Fix For: 1.15.1
>
>
> {{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