[ https://issues.apache.org/jira/browse/LANG-1077?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15053470#comment-15053470 ]
Sebb commented on LANG-1077: ---------------------------- I agree that had the patch been 'better' it would not have caused JIRA-1193 to be raised. In fact I was originally going to try to fix it accordingly. However, I looked further into it, and noticed that fixing the behaviour according to your original issue was not a good idea, as it would have changed the existing behaviour. The code was added to LANG in version 2.1 about 12 years ago, and the behaviour did not change until 3.4 which broke it. Although the behaviour is not what you expect, it is consistent with the original Javadoc. Furthermore the behaviour has been the same for multiple releases, so changing the behaviour now would potentially break existing applications. > [PATCH] StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) != 3 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 > 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; > // } > // 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); > // return index; > 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 index = lastIndex ? str.length() : 0; > for (int found = 0; index >= 0;) { > if (lastIndex) { > index = CharSequenceUtils.lastIndexOf(str, searchStr, index); > } else { > index = CharSequenceUtils.indexOf(str, searchStr, index); > } > if (index < 0) { > return INDEX_NOT_FOUND; > } > if (++found >= ordinal) { > break; > } > index = lastIndex ? index - searchStrLen : index + searchStrLen; > } > return index; > } > {code} -- This message was sent by Atlassian JIRA (v6.3.4#6332)