commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r891142 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang3/StringUtils.java test/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
Date Wed, 16 Dec 2009 07:32:31 GMT
Author: bayard
Date: Wed Dec 16 07:32:30 2009
New Revision: 891142

URL: http://svn.apache.org/viewvc?rev=891142&view=rev
Log:
Applying my patch from LANG-469 - adding a lastOrdinalIndexOf method to complement the existing
ordinalIndexOf method

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang3/StringUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang3/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang3/StringUtils.java?rev=891142&r1=891141&r2=891142&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang3/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang3/StringUtils.java Wed Dec 16
07:32:30 2009
@@ -831,6 +831,12 @@
      * StringUtils.ordinalIndexOf("aabaabaa", "", 2)   = 0
      * </pre>
      *
+     * <p>Note that 'head(String str, int n)' may be implemented as: </p>
+     *
+     * <pre>
+     *   str.substring(0, lastOrdinalIndexOf(str, "\n", n))
+     * </pre>
+     *
      * @param str  the String to check, may be null
      * @param searchStr  the String to find, may be null
      * @param ordinal  the n-th <code>searchStr</code> to find
@@ -839,16 +845,25 @@
      * @since 2.1
      */
     public static int ordinalIndexOf(String str, String searchStr, int ordinal) {
+        return ordinalIndexOf(str, searchStr, ordinal, false);
+    }
+
+    // Shared code between ordinalIndexOf(String,String,int) and lastOrdinalIndexOf(String,String,int)
+    private static int ordinalIndexOf(String str, String searchStr, int ordinal, boolean
lastIndex) {
         if (str == null || searchStr == null || ordinal <= 0) {
             return INDEX_NOT_FOUND;
         }
         if (searchStr.length() == 0) {
-            return 0;
+            return lastIndex ? str.length() : 0;
         }
         int found = 0;
-        int index = INDEX_NOT_FOUND;
+        int index = lastIndex ? str.length() : INDEX_NOT_FOUND;
         do {
-            index = str.indexOf(searchStr, index + 1);
+            if(lastIndex) {
+                index = str.lastIndexOf(searchStr, index - 1);
+            } else {
+                index = str.indexOf(searchStr, index + 1);
+            }
             if (index < 0) {
                 return index;
             }
@@ -991,6 +1006,43 @@
     }
 
     /**
+     * <p>Finds the n-th last index within a String, handling <code>null</code>.
+     * This method uses {@link String#lastIndexOf(String)}.</p>
+     *
+     * <p>A <code>null</code> String will return <code>-1</code>.</p>
+     *
+     * <pre>
+     * StringUtils.lastOrdinalIndexOf(null, *, *)          = -1
+     * StringUtils.lastOrdinalIndexOf(*, null, *)          = -1
+     * StringUtils.lastOrdinalIndexOf("", "", *)           = 0
+     * StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 1)  = 7
+     * StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 2)  = 6
+     * StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 1)  = 5
+     * StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 2)  = 2
+     * StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 1) = 4
+     * StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2) = 1
+     * StringUtils.lastOrdinalIndexOf("aabaabaa", "", 1)   = 8
+     * StringUtils.lastOrdinalIndexOf("aabaabaa", "", 2)   = 8
+     * </pre>
+     *
+     * <p>Note that 'tail(String str, int n)' may be implemented as: </p>
+     *
+     * <pre>
+     *   str.substring(lastOrdinalIndexOf(str, "\n", n) + 1)
+     * </pre>
+     *
+     * @param str  the String to check, may be null
+     * @param searchStr  the String to find, may be null
+     * @param ordinal  the n-th last <code>searchStr</code> to find
+     * @return the n-th last index of the search String,
+     *  <code>-1</code> (<code>INDEX_NOT_FOUND</code>) if no match
or <code>null</code> string input
+     * @since 3.0
+     */
+    public static int lastOrdinalIndexOf(String str, String searchStr, int ordinal) {
+        return ordinalIndexOf(str, searchStr, ordinal, true);
+    }
+
+    /**
      * <p>Finds the first index within a String, handling <code>null</code>.
      * This method uses {@link String#lastIndexOf(String, int)}.</p>
      *
@@ -5836,5 +5888,4 @@
         int strOffset = str.length() - suffix.length();
         return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length());
     }
-
 }

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java?rev=891142&r1=891141&r2=891142&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
(original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
Wed Dec 16 07:32:30 2009
@@ -230,6 +230,20 @@
         assertEquals(4, StringUtils.lastIndexOf("aabaabaa", "ab"));
     }
 
+    public void testLastOrdinalIndexOf() {
+        assertEquals(-1, StringUtils.lastOrdinalIndexOf(null, "*", 42) );
+        assertEquals(-1, StringUtils.lastOrdinalIndexOf("*", null, 42) );
+        assertEquals(0, StringUtils.lastOrdinalIndexOf("", "", 42) );
+        assertEquals(7, StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 1) );
+        assertEquals(6, StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 2) );
+        assertEquals(5, StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 1) );
+        assertEquals(2, StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 2) );
+        assertEquals(4, StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 1) );
+        assertEquals(1, StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2) );
+        assertEquals(8, StringUtils.lastOrdinalIndexOf("aabaabaa", "", 1) );
+        assertEquals(8, StringUtils.lastOrdinalIndexOf("aabaabaa", "", 2) );
+    }
+
     public void testLastIndexOf_StringInt() {
         assertEquals(-1, StringUtils.lastIndexOf(null, null, 0));
         assertEquals(-1, StringUtils.lastIndexOf(null, null, -1));



Mime
View raw message