You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2010/01/28 18:20:01 UTC

svn commit: r904169 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/StringUtils.java test/java/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java

Author: niallp
Date: Thu Jan 28 17:20:01 2010
New Revision: 904169

URL: http://svn.apache.org/viewvc?rev=904169&view=rev
Log:
Port LANG-569 to 2.x branch - adding indexOfIgnoreCase() and lastIndexOfIgnoreCase() methods to StringUtils

Modified:
    commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
    commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java

Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java?rev=904169&r1=904168&r2=904169&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java (original)
+++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java Thu Jan 28 17:20:01 2010
@@ -873,6 +873,87 @@
         return str.indexOf(searchStr, startPos);
     }
 
+    /**
+     * <p>Case in-sensitive find of the first index within a String.</p>
+     *
+     * <p>A <code>null</code> String will return <code>-1</code>.
+     * A negative start position is treated as zero.
+     * An empty ("") search String always matches.
+     * A start position greater than the string length only matches
+     * an empty search String.</p>
+     *
+     * <pre>
+     * StringUtils.indexOfIgnoreCase(null, *)          = -1
+     * StringUtils.indexOfIgnoreCase(*, null)          = -1
+     * StringUtils.indexOfIgnoreCase("", "")           = 0
+     * StringUtils.indexOfIgnoreCase("aabaabaa", "a")  = 0
+     * StringUtils.indexOfIgnoreCase("aabaabaa", "b")  = 2
+     * StringUtils.indexOfIgnoreCase("aabaabaa", "ab") = 1
+     * </pre>
+     *
+     * @param str  the String to check, may be null
+     * @param searchStr  the String to find, may be null
+     * @return the first index of the search String,
+     *  -1 if no match or <code>null</code> string input
+     * @since 3.0
+     */
+    public static int indexOfIgnoreCase(String str, String searchStr) {
+        return indexOfIgnoreCase(str, searchStr, 0);
+    }
+
+    /**
+     * <p>Case in-sensitive find of the first index within a String
+     * from the specified position.</p>
+     *
+     * <p>A <code>null</code> String will return <code>-1</code>.
+     * A negative start position is treated as zero.
+     * An empty ("") search String always matches.
+     * A start position greater than the string length only matches
+     * an empty search String.</p>
+     *
+     * <pre>
+     * StringUtils.indexOfIgnoreCase(null, *, *)          = -1
+     * StringUtils.indexOfIgnoreCase(*, null, *)          = -1
+     * StringUtils.indexOfIgnoreCase("", "", 0)           = 0
+     * StringUtils.indexOfIgnoreCase("aabaabaa", "A", 0)  = 0
+     * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 0)  = 2
+     * StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
+     * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 3)  = 5
+     * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 9)  = -1
+     * StringUtils.indexOfIgnoreCase("aabaabaa", "B", -1) = 2
+     * StringUtils.indexOfIgnoreCase("aabaabaa", "", 2)   = 2
+     * StringUtils.indexOfIgnoreCase("abc", "", 9)        = 3
+     * </pre>
+     *
+     * @param str  the String to check, may be null
+     * @param searchStr  the String to find, may be null
+     * @param startPos  the start position, negative treated as zero
+     * @return the first index of the search String,
+     *  -1 if no match or <code>null</code> string input
+     * @since 3.0
+     */
+    public static int indexOfIgnoreCase(String str, String searchStr, int startPos) {
+        if (str == null || searchStr == null) {
+            return -1;
+        }
+        if (startPos < 0) {
+            startPos = 0;
+        }
+        int endLimit = (str.length() - searchStr.length()) + 1;
+        if (startPos > endLimit) {
+            return -1;
+        }
+        if (searchStr.length() == 0) {
+            return startPos;
+        }
+        for (int i = startPos; i < endLimit; i++) {
+            if (str.regionMatches(true, i, searchStr, 0, searchStr.length())) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
     // LastIndexOf
     //-----------------------------------------------------------------------
     /**
@@ -1036,6 +1117,85 @@
         return str.lastIndexOf(searchStr, startPos);
     }
 
+    /**
+     * <p>Case in-sensitive find of the last index within a String.</p>
+     *
+     * <p>A <code>null</code> String will return <code>-1</code>.
+     * A negative start position returns <code>-1</code>.
+     * An empty ("") search String always matches unless the start position is negative.
+     * A start position greater than the string length searches the whole string.</p>
+     *
+     * <pre>
+     * StringUtils.lastIndexOfIgnoreCase(null, *)          = -1
+     * StringUtils.lastIndexOfIgnoreCase(*, null)          = -1
+     * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A")  = 7
+     * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B")  = 5
+     * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB") = 4
+     * </pre>
+     *
+     * @param str  the String to check, may be null
+     * @param searchStr  the String to find, may be null
+     * @return the first index of the search String,
+     *  -1 if no match or <code>null</code> string input
+     * @since 3.0
+     */
+    public static int lastIndexOfIgnoreCase(String str, String searchStr) {
+        if (str == null || searchStr == null) {
+            return -1;
+        }
+        return lastIndexOfIgnoreCase(str, searchStr, str.length());
+    }
+
+    /**
+     * <p>Case in-sensitive find of the last index within a String
+     * from the specified position.</p>
+     *
+     * <p>A <code>null</code> String will return <code>-1</code>.
+     * A negative start position returns <code>-1</code>.
+     * An empty ("") search String always matches unless the start position is negative.
+     * A start position greater than the string length searches the whole string.</p>
+     *
+     * <pre>
+     * StringUtils.lastIndexOfIgnoreCase(null, *, *)          = -1
+     * StringUtils.lastIndexOfIgnoreCase(*, null, *)          = -1
+     * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 8)  = 7
+     * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 8)  = 5
+     * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB", 8) = 4
+     * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 9)  = 5
+     * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", -1) = -1
+     * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 0)  = 0
+     * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 0)  = -1
+     * </pre>
+     *
+     * @param str  the String to check, may be null
+     * @param searchStr  the String to find, may be null
+     * @param startPos  the start position
+     * @return the first index of the search String,
+     *  -1 if no match or <code>null</code> string input
+     * @since 3.0
+     */
+    public static int lastIndexOfIgnoreCase(String str, String searchStr, int startPos) {
+        if (str == null || searchStr == null) {
+            return -1;
+        }
+        if (startPos > (str.length() - searchStr.length())) {
+            startPos = str.length() - searchStr.length();
+        }
+        if (startPos < 0) {
+            return -1;
+        }
+        if (searchStr.length() == 0) {
+            return startPos;
+        }
+
+        for (int i = startPos; i >= 0; i--) {
+            if (str.regionMatches(true, i, searchStr, 0, searchStr.length())) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
     // Contains
     //-----------------------------------------------------------------------
     /**

Modified: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java?rev=904169&r1=904168&r2=904169&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java (original)
+++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java Thu Jan 28 17:20:01 2010
@@ -109,6 +109,37 @@
         assertEquals(0, StringUtils.indexOf("aabaabaa", ""));
     }
 
+    public void testIndexOfIgnoreCase_String() {
+        assertEquals(-1, StringUtils.indexOfIgnoreCase(null, null));
+        assertEquals(-1, StringUtils.indexOfIgnoreCase(null, ""));
+        assertEquals(-1, StringUtils.indexOfIgnoreCase("", null));
+        assertEquals(0, StringUtils.indexOfIgnoreCase("", ""));
+        assertEquals(0, StringUtils.indexOfIgnoreCase("aabaabaa", "a"));
+        assertEquals(0, StringUtils.indexOfIgnoreCase("aabaabaa", "A"));
+        assertEquals(2, StringUtils.indexOfIgnoreCase("aabaabaa", "b"));
+        assertEquals(2, StringUtils.indexOfIgnoreCase("aabaabaa", "B"));
+        assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "ab"));
+        assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB"));
+        assertEquals(0, StringUtils.indexOfIgnoreCase("aabaabaa", ""));
+    }
+
+    public void testIndexOfIgnoreCase_StringInt() {
+        assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", -1));
+        assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 0));
+        assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 1));
+        assertEquals(4, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 2));
+        assertEquals(4, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 3));
+        assertEquals(4, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 4));
+        assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 5));
+        assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 6));
+        assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 7));
+        assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 8));
+        assertEquals(1, StringUtils.indexOfIgnoreCase("aab", "AB", 1));
+        assertEquals(5, StringUtils.indexOfIgnoreCase("aabaabaa", "", 5));
+        assertEquals(-1, StringUtils.indexOfIgnoreCase("ab", "AAB", 0));
+        assertEquals(-1, StringUtils.indexOfIgnoreCase("aab", "AAB", 1));
+    }
+
     public void testOrdinalIndexOf() {
         assertEquals(-1, StringUtils.ordinalIndexOf(null, null, Integer.MIN_VALUE));
         assertEquals(-1, StringUtils.ordinalIndexOf("", null, Integer.MIN_VALUE));
@@ -263,6 +294,47 @@
         assertEquals(0, StringUtils.lastIndexOf("aabaabaa", "a", 0));
     }
 
+    public void testLastIndexOfIgnoreCase_String() {
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, null));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", null));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, ""));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", "a"));
+        assertEquals(0, StringUtils.lastIndexOfIgnoreCase("", ""));
+        assertEquals(8, StringUtils.lastIndexOfIgnoreCase("aabaabaa", ""));
+        assertEquals(7, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "a"));
+        assertEquals(7, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A"));
+        assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "b"));
+        assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B"));
+        assertEquals(4, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "ab"));
+        assertEquals(4, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB"));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("ab", "AAB"));
+        assertEquals(0, StringUtils.lastIndexOfIgnoreCase("aab", "AAB"));
+    }
+
+    public void testLastIndexOfIgnoreCase_StringInt() {
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, null, 0));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, null, -1));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, "", 0));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, "", -1));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", null, 0));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", null, -1));
+        assertEquals(0, StringUtils.lastIndexOfIgnoreCase("", "", 0));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", "", -1));
+        assertEquals(0, StringUtils.lastIndexOfIgnoreCase("", "", 9));
+        assertEquals(0, StringUtils.lastIndexOfIgnoreCase("abc", "", 0));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("abc", "", -1));
+        assertEquals(3, StringUtils.lastIndexOfIgnoreCase("abc", "", 9));
+        assertEquals(7, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 8));
+        assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 8));
+        assertEquals(4, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB", 8));
+        assertEquals(2, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 3));
+        assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 9));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", -1));
+        assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 0));
+        assertEquals(0, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 0));
+        assertEquals(1, StringUtils.lastIndexOfIgnoreCase("aab", "AB", 1));
+    }
+
     //-----------------------------------------------------------------------
     public void testContainsChar() {
         assertEquals(false, StringUtils.contains(null, ' '));