Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id DB51310340 for ; Fri, 12 Dec 2014 19:35:18 +0000 (UTC) Received: (qmail 85722 invoked by uid 500); 12 Dec 2014 19:35:13 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 85632 invoked by uid 500); 12 Dec 2014 19:35:13 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 85620 invoked by uid 99); 12 Dec 2014 19:35:13 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Dec 2014 19:35:13 +0000 Date: Fri, 12 Dec 2014 19:35:13 +0000 (UTC) From: "haiyang li (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Updated] (LANG-1077) [PATCH] StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) != 2 in StringUtils MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/LANG-1077?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] haiyang li updated LANG-1077: ----------------------------- Description: {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} was: {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} > [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; > // } > // 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)