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 E7C3C18F8A for ; Fri, 11 Dec 2015 18:32:11 +0000 (UTC) Received: (qmail 51300 invoked by uid 500); 11 Dec 2015 18:32:11 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 51059 invoked by uid 500); 11 Dec 2015 18:32:11 -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 50492 invoked by uid 99); 11 Dec 2015 18:32:11 -0000 Received: from arcas.apache.org (HELO arcas) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Dec 2015 18:32:11 +0000 Received: from arcas.apache.org (localhost [127.0.0.1]) by arcas (Postfix) with ESMTP id 35C842C14F9 for ; Fri, 11 Dec 2015 18:32:11 +0000 (UTC) Date: Fri, 11 Dec 2015 18:32:11 +0000 (UTC) From: "Sebb (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Closed] (LANG-1077) [PATCH] StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) != 3 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 ] Sebb closed LANG-1077. ---------------------- > [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)