You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by rousej <gi...@git.apache.org> on 2015/08/15 03:00:44 UTC

[GitHub] commons-lang pull request: fix StringUtils.ordinalIndexOf("aaaaaa"...

Github user rousej commented on the pull request:

    https://github.com/apache/commons-lang/pull/93#issuecomment-131275348
  
    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.
    
    ........
    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);  
       }  
    ........
    
    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. 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.



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---