[ https://issues.apache.org/jira/browse/LANG-1241?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15324839#comment-15324839 ]
Pascal Schumacher commented on LANG-1241: ----------------------------------------- [~nmanley], [~s...@apache.org] I was not aware that LANG-1077 has been reverted, else I would not have created this issue. Thanks for clarifying. > 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)