You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Nick Manley (JIRA)" <ji...@apache.org> on 2016/06/10 05:57:20 UTC

[jira] [Commented] (LANG-1241) StringUtils.ordinalIndexOf broken

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

Nick Manley commented on LANG-1241:
-----------------------------------

[~sebb@apache.org]'s commit [d75fe46|https://github.com/apache/commons-lang/commit/d75fe46b8f1b0d5c27887052ee4714d6a9c7ea4b] reverts LANG-1077 which was closed as invalid. So what's considered broken here?

> StringUtils.ordinalIndexOf broken
> ---------------------------------
>
>                 Key: LANG-1241
>                 URL: https://issues.apache.org/jira/browse/LANG-1241
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.*
>    Affects Versions: 3.4
>            Reporter: Pascal Schumacher
>
> Quoting rousej from the discussion of [https://github.com/apache/commons-lang/pull/93]
> I agree with britter. StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) == 1 would be true because the sequence 'aa' is at every index. There seems to be confusion around this method, but it seems to me the original code had it correct.
> {code:java}
> int index = lastIndex ? str.length() : INDEX_NOT_FOUND;
> do {
> if (lastIndex) {
> index = CharSequenceUtils.lastIndexOf(str, searchStr, index - 1);
> } else {
> index = CharSequenceUtils.indexOf(str, searchStr, index + 1);
> }
> {code}
> I'm not sure why StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) == 3 would ever be true.
> StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) == 2 is easier to see where it's coming from, but StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) == 1 is still the correct answer, and it also works for other tests. I found my way here because I wanted to use this method in a project, but found that the current release is broken by commit [e5a3039|https://github.com/apache/commons-lang/commit/e5a3039f7a1e727fca40db7357a9191b6a7cf41d] . With the current release if the first index is between 0 and searchStr.length() -1 the method will return the index for ordinal + 1.....in other words the wrong index.
> This fact is missed in a test like
> assertEquals(3, StringUtils.ordinalIndexOf("aaaaaa", "aa", 2));
> /* Note the above test from commit [e5a3039|https://github.com/apache/commons-lang/commit/e5a3039f7a1e727fca40db7357a9191b6a7cf41d] is incorrect since the array of chars begins at 0.*/
> The following test should pass, but will fail in the current release due to the broken method:
> {code:java}
> assertEquals(0, StringUtils.ordinalIndexOf("abaabaab", "ab", 1);
> assertEquals(3, StringUtils.ordinalIndexOf("abaabaab", "ab", 2);
> assertEquals(6, StringUtils.ordinalIndexOf("abaabaab", "ab", 3);
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)