commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [lang] LANG-1241 StringUtils.ordinalIndexOf broken
Date Fri, 10 Jun 2016 10:53:29 GMT
Repository: commons-lang
Updated Branches:
  refs/heads/master f08c4f6ae -> 7fd021d82


LANG-1241 StringUtils.ordinalIndexOf broken

Show that the method is no longer broken, now that LANG-1077 has been
reverted

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

Branch: refs/heads/master
Commit: 7fd021d82ff431fb31f42bc6c5c44a3b979cb426
Parents: f08c4f6
Author: Sebb <sebb@apache.org>
Authored: Fri Jun 10 11:53:24 2016 +0100
Committer: Sebb <sebb@apache.org>
Committed: Fri Jun 10 11:53:24 2016 +0100

----------------------------------------------------------------------
 .../org/apache/commons/lang3/StringUtils.java   |  4 ++++
 .../lang3/StringUtilsEqualsIndexOfTest.java     | 20 ++++++++++++++++++++
 2 files changed, 24 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/7fd021d8/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 f2b8554..9bc6396 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -1334,6 +1334,9 @@ public class StringUtils {
      * This method uses {@link String#indexOf(String)} if possible.</p>
      * <p><b>Note:</b> The code starts looking for a match at the start
of the target,
      * incrementing the starting index by one after each successful match.</p>
+     * <p>The code increments the starting index by one,
+     * rather than by the length of the match string,
+     * so matches may overlap.</p>
      * <p>A {@code null} CharSequence will return {@code -1}.</p>
      *
      * <pre>
@@ -1383,6 +1386,7 @@ public class StringUtils {
     /**
      * <p>Finds the n-th index within a String, handling {@code null}.
      * This method uses {@link String#indexOf(String)} if possible.</p>
+     * <p>Note that matches may overlap<p>
      *
      * <p>A {@code null} CharSequence will return {@code -1}.</p>
      *

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/7fd021d8/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 a4079c9..93a1dfc 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
@@ -1177,4 +1177,24 @@ public class StringUtilsEqualsIndexOfTest  {
         assertEquals(0, StringUtils.ordinalIndexOf("abc", "ab", 1));        
     }
 
+    @Test
+    // Non-overlapping test
+    public void testLANG1241_1() {
+        //                                          0  3  6
+        assertEquals(0, StringUtils.ordinalIndexOf("abaabaab", "ab", 1));
+        assertEquals(3, StringUtils.ordinalIndexOf("abaabaab", "ab", 2));
+        assertEquals(6, StringUtils.ordinalIndexOf("abaabaab", "ab", 3));
+    }
+
+    @Test
+    // Overlapping matching test
+    public void testLANG1241_2() {
+        //                                          0 2 4
+        assertEquals(0, StringUtils.ordinalIndexOf("abababa", "aba", 1));
+        assertEquals(2, StringUtils.ordinalIndexOf("abababa", "aba", 2));
+        assertEquals(4, StringUtils.ordinalIndexOf("abababa", "aba", 3));
+        assertEquals(0, StringUtils.ordinalIndexOf("abababab", "abab", 1));
+        assertEquals(2, StringUtils.ordinalIndexOf("abababab", "abab", 2));
+        assertEquals(4, StringUtils.ordinalIndexOf("abababab", "abab", 3));
+    }
 }


Mime
View raw message