You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2015/12/11 14:41:27 UTC

[lang] LANG-1193 ordinalIndexOf("abc", "ab", 1) gives incorrect answer of -1 (correct answer should be 0) Revert LANG-1077

Repository: commons-lang
Updated Branches:
  refs/heads/master 15e1ea2f4 -> d75fe46b8


LANG-1193 ordinalIndexOf("abc", "ab", 1) gives incorrect answer of -1
(correct answer should be 0)
Revert LANG-1077

Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/d75fe46b
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/d75fe46b
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/d75fe46b

Branch: refs/heads/master
Commit: d75fe46b8f1b0d5c27887052ee4714d6a9c7ea4b
Parents: 15e1ea2
Author: Sebb <se...@apache.org>
Authored: Fri Dec 11 13:41:22 2015 +0000
Committer: Sebb <se...@apache.org>
Committed: Fri Dec 11 13:41:22 2015 +0000

----------------------------------------------------------------------
 src/changes/changes.xml                            |  2 +-
 .../java/org/apache/commons/lang3/StringUtils.java | 15 ++++++++++++---
 .../lang3/StringUtilsEqualsIndexOfTest.java        | 17 +++++++++++++++--
 3 files changed, 28 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/d75fe46b/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ca3408c..7c197c4 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.5" date="tba" description="tba">
+    <action issue="LANG-1193" type="fix" dev="sebb" due-to="Qin Li">ordinalIndexOf("abc", "ab", 1) gives incorrect answer of -1 (correct answer should be 0); revert fix for LANG-1077</action>
     <action issue="LANG-1182" type="update" dev="britter" due-to="Larry West, Pascal Schumacher">Clarify JavaDoc of StringUtils.containsAny()</action>
     <action issue="LANG-1169" type="add" dev="lguibert" due-to="Rafal Glowinski, Robert Parr, Arman Sharif">Add StringUtils methods to compare a string to multiple strings</action>
     <action issue="LANG-1185" type="add" dev="lguibert">Add remove by regular expression methods in StringUtils</action>
@@ -104,7 +105,6 @@
     <action issue="LANG-1071" type="update" dev="britter" due-to="Arno Noordover">Fix wrong examples in JavaDoc of StringUtils.replaceEachRepeatedly(...), StringUtils.replaceEach(...)</action>
     <action issue="LANG-883" type="add" dev="britter" due-to="Daniel Stewart">Add StringUtils.containsAny(CharSequence, CharSequence...) method</action>
     <action issue="LANG-1073" type="fix" dev="kinow" due-to="haiyang li">Read wrong component type of array in add in ArrayUtils</action>
-    <action issue="LANG-1077" type="fix" dev="kinow" due-to="haiyang li">StringUtils.ordinalIndexOf("aaaaaa", "aa", 2) != 3 in StringUtils</action>
     <action issue="LANG-1072" type="fix" dev="sebb" due-to="haiyang li">Duplicated "0x" check in createBigInteger in NumberUtils</action>
     <action issue="LANG-1064" type="fix" dev="djones" due-to="B.J. Herbison">StringUtils.abbreviate description doesn't agree with the examples</action>
     <action issue="LANG-1052" type="add" dev="britter" due-to="Jan Matèrne">Multiline recursive to string style</action>

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/d75fe46b/src/main/java/org/apache/commons/lang3/StringUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 1b58b95..c7583c9 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -1222,6 +1222,13 @@ public class StringUtils {
      * StringUtils.ordinalIndexOf("aabaabaa", "", 2)   = 0
      * </pre>
      *
+     * <p>Matches may overlap:</p>
+     * <pre>
+     * StringUtils.ordinalIndexOf("ababab","aba", 1)          = 0
+     * StringUtils.ordinalIndexOf("ababab","aba", 2)          = 2
+     * StringUtils.ordinalIndexOf("ababab","aba", 3)          = -1
+     * </pre>
+     *
      * <p>Note that 'head(CharSequence str, int n)' may be implemented as: </p>
      *
      * <pre>
@@ -1248,7 +1255,7 @@ public class StringUtils {
      *
      * @param str  the CharSequence to check, may be null
      * @param searchStr  the CharSequence to find, may be null
-     * @param ordinal  the n-th {@code searchStr} to find
+     * @param ordinal  the n-th {@code searchStr} to find, overlapping matches are allowed.
      * @param lastIndex true if lastOrdinalIndexOf() otherwise false if ordinalIndexOf()
      * @return the n-th index of the search CharSequence,
      *  {@code -1} ({@code INDEX_NOT_FOUND}) if no match or {@code null} string input
@@ -1262,12 +1269,14 @@ public class StringUtils {
             return lastIndex ? str.length() : 0;
         }
         int found = 0;
+        // set the initial index beyond the end of the string
+        // this is to allow for the initial index decrement/increment
         int index = lastIndex ? str.length() : INDEX_NOT_FOUND;
         do {
             if (lastIndex) {
-                index = CharSequenceUtils.lastIndexOf(str, searchStr, index - searchStr.length());
+                index = CharSequenceUtils.lastIndexOf(str, searchStr, index - 1); // step backwards thru string
             } else {
-                index = CharSequenceUtils.indexOf(str, searchStr, index + searchStr.length());
+                index = CharSequenceUtils.indexOf(str, searchStr, index + 1); // step forwards through string
             }
             if (index < 0) {
                 return index;

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/d75fe46b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
index 8750de3..f509290 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
@@ -1148,8 +1148,21 @@ public class StringUtilsEqualsIndexOfTest  {
         assertEquals(8, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 9));
         assertEquals(-1, StringUtils.ordinalIndexOf("aaaaaaaaa", "a", 10));
 
-        assertEquals(3, StringUtils.ordinalIndexOf("aaaaaa", "aa", 2));
-        assertEquals(-1, StringUtils.ordinalIndexOf("aaaaaa", "aa", 3));
+        assertEquals(0, StringUtils.ordinalIndexOf("aaaaaa", "aa", 1));
+        assertEquals(1, StringUtils.ordinalIndexOf("aaaaaa", "aa", 2));
+        assertEquals(2, StringUtils.ordinalIndexOf("aaaaaa", "aa", 3));
+        assertEquals(3, StringUtils.ordinalIndexOf("aaaaaa", "aa", 4));
+        assertEquals(4, StringUtils.ordinalIndexOf("aaaaaa", "aa", 5));
+        assertEquals(-1, StringUtils.ordinalIndexOf("aaaaaa", "aa", 6));
+
+        assertEquals(0, StringUtils.ordinalIndexOf("ababab", "aba", 1));
+        assertEquals(2, StringUtils.ordinalIndexOf("ababab", "aba", 2));
+        assertEquals(-1, StringUtils.ordinalIndexOf("ababab", "aba", 3));
+    }
+    
+    @Test
+    public void testLANG1193() {
+        assertEquals(0, StringUtils.ordinalIndexOf("abc", "ab", 1));        
     }
 
 }