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

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

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

Sebb resolved LANG-1241.
------------------------
    Resolution: Not A Problem

No longer a problem, now that LANG-1077 has been reverted.

Repository: commons-lang
Updated Branches:
  refs/heads/master f08c4f6ae -> 7fd021d82


LANG-1241 StringUtils.ordinalIndexOf broken

Show that the method is no longer broken, now that LANG-1077 has been
reverted

Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/7fd021d8
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/7fd021d8
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/7fd021d8

> 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)