commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bruno P. Kinoshita (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (LANG-1077) [PATCH] StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) != 2 in StringUtils
Date Fri, 12 Dec 2014 18:58:13 GMT

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

Bruno P. Kinoshita commented on LANG-1077:
------------------------------------------

Hello Haiyang!

No, I thank you for filing the issue and taking your time to provide some code. I never used
the orginalIndexOf, so I thought it was a good chance to learn it too :-)

I *think* your previous code was right. Looking at the current implementation, it sems to
always return the first occurrence of the needle (searchStr), *after* the given offset (ordinal)
in the haystack (str).

I updated my local copy with your updated code, and it broke existing tests. 

What do you think?

> [PATCH] StringUtils.ordinalIndexOf("aaaaaa", "aa", 2)  != 2 in StringUtils
> --------------------------------------------------------------------------
>
>                 Key: LANG-1077
>                 URL: https://issues.apache.org/jira/browse/LANG-1077
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.*
>    Affects Versions: 3.3.2
>            Reporter: haiyang li
>              Labels: patch
>             Fix For: Discussion
>
>         Attachments: LANG-1077.patch
>
>
> {code:title= org.apache.commons.lang3.StringUtils.java|borderStyle=solid}
>         int found = 0;
>         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);
>             }
>             if (index < 0) {
>                 return index;
>             }
>             found++;
>         } while (found < ordinal);
> {code}
> Should it be:
> {code:title= org.apache.commons.lang3.StringUtils.java|borderStyle=solid}
>     private static int ordinalIndexOf(final CharSequence str, final CharSequence searchStr,
final int ordinal, final boolean lastIndex) {
>         if (str == null || searchStr == null || ordinal <= 0) {
>             return INDEX_NOT_FOUND;
>         }
>         if (searchStr.length() == 0) {
>             return lastIndex ? str.length() : 0;
>         }
>         final int searchStrLen = searchStr.length();
>         int found = 0;
>         int index = lastIndex ? str.length() : 0;
>         do {
>             if (lastIndex) {
>                 index = CharSequenceUtils.lastIndexOf(str, searchStr, index);
>             } else {
>                 index = CharSequenceUtils.indexOf(str, searchStr, index);
>             }
>             if (index < 0) {
>                 return INDEX_NOT_FOUND;
>             }
>             index = lastIndex ? index - searchStrLen : index + searchStrLen;
>             found++;
>         } while (found < ordinal);
>         return index;
>     }
> {code}



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

Mime
View raw message