Added the split variant from #24910 in which the separator may be a full String rather than a String of characters. Rather than the issues suggested boolean parameter, the name has been changed to splitByWholeSeparator


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137897 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java
index 81f5c6a..3a41dbe 100644
--- a/src/java/org/apache/commons/lang/StringUtils.java
+++ b/src/java/org/apache/commons/lang/StringUtils.java
@@ -111,7 +111,7 @@
  * @author Al Chou
  * @author Michael Davey
  * @since 1.0
- * @version $Id: StringUtils.java,v 1.133 2004/08/15 23:47:05 scolebourne Exp $
+ * @version $Id: StringUtils.java,v 1.134 2004/08/22 03:40:27 bayard Exp $
  */
 public class StringUtils {
     // Performance testing notes (JDK 1.4, Jul03, scolebourne)
@@ -2060,6 +2060,119 @@
         return splitWorker(str, separatorChars, max, false);
     }
 
+    /**
+     * <p>Splits the provided text into an array, separator string specified.</p>
+     *
+     * <p>The separator(s) will not be included in the returned String array.
+     * Adjacent separators are treated as one separator.</p>
+     *
+     * <p>A <code>null</code> input String returns <code>null</code>.
+     * A <code>null</code> separator splits on whitespace.</p>
+     *
+     * <pre>
+     * StringUtils.split(null, *)            = null
+     * StringUtils.split("", *)              = []
+     * StringUtils.split("ab de fg", null)   = ["ab", "de", "fg"]
+     * StringUtils.split("ab   de fg", null) = ["ab", "de", "fg"]
+     * StringUtils.split("ab:cd:ef", ":")    = ["ab", "cd", "ef"]
+     * StringUtils.split("abstemiouslyaeiouyabstemiously", "aeiouy")  = ["bst", "m", "sl", "bst", "m", "sl"]
+     * StringUtils.split("abstemiouslyaeiouyabstemiously", "aeiouy")  = ["abstemiously", "abstemiously"]
+     * </pre>
+     *
+     * @param str  the String to parse, may be null
+     * @param separator  String containing the String to be used as a delimiter,
+     *  <code>null</code> splits on whitespace
+     * @return an array of parsed Strings, <code>null</code> if null String was input
+     */
+    public static String[] splitByWholeSeparator(String str, String separator) {
+        return splitByWholeSeparator( str, separator, -1 ) ;
+    }
+
+    /**
+     * <p>Splits the provided text into an array, separator string specified.
+     * Returns a maximum of <code>max</code> substrings.</p>
+     *
+     * <p>The separator(s) will not be included in the returned String array.
+     * Adjacent separators are treated as one separator.</p>
+     *
+     * <p>A <code>null</code> input String returns <code>null</code>.
+     * A <code>null</code> separator splits on whitespace.</p>
+     *
+     * <pre>
+     * StringUtils.splitByWholeSeparator(null, *, *)               = null
+     * StringUtils.splitByWholeSeparator("", *, *)                 = []
+     * StringUtils.splitByWholeSeparator("ab de fg", null, 0)      = ["ab", "de", "fg"]
+     * StringUtils.splitByWholeSeparator("ab   de fg", null, 0)    = ["ab", "de", "fg"]
+     * StringUtils.splitByWholeSeparator("ab:cd:ef", ":", 2)       = ["ab", "cd"]
+     * StringUtils.splitByWholeSeparator("abstemiouslyaeiouyabstemiously", "aeiouy", 2) = ["bst", "m"]
+     * StringUtils.splitByWholeSeparator("abstemiouslyaeiouyabstemiously", "aeiouy", 2)  = ["abstemiously", "abstemiously"]
+     * </pre>
+     *
+     * @param str  the String to parse, may be null
+     * @param separator  String containing the String to be used as a delimiter,
+     *  <code>null</code> splits on whitespace
+     * @param max  the maximum number of elements to include in the returned
+     *  array. A zero or negative value implies no limit.
+     * @return an array of parsed Strings, <code>null</code> if null String was input
+     */
+    public static String[] splitByWholeSeparator( String str, String separator, int max ) {
+        if (str == null) {
+            return null;
+        }
+
+        int len = str.length() ;
+
+        if (len == 0) {
+            return ArrayUtils.EMPTY_STRING_ARRAY;
+        }
+
+        if ( ( separator == null ) || ( "".equals( separator ) ) ) {
+            // Split on whitespace.
+            return split( str, null, max ) ;
+        }
+
+
+        int separatorLength = separator.length() ;
+
+        ArrayList substrings = new ArrayList() ;
+        int numberOfSubstrings = 0 ;
+        int beg = 0 ;
+        int end = 0 ;
+        while ( end < len ) {
+            end = str.indexOf( separator, beg ) ;
+
+            if ( end > -1 ) {
+                if ( end > beg ) {
+                    numberOfSubstrings += 1 ;
+
+                    if ( numberOfSubstrings == max ) {
+                        end = len ;
+                        substrings.add( str.substring( beg ) ) ;
+                    } else {
+                        // The following is OK, because String.substring( beg, end ) excludes
+                        // the character at the position 'end'.
+                        substrings.add( str.substring( beg, end ) ) ;
+
+                        // Set the starting point for the next search.
+                        // The following is equivalent to beg = end + (separatorLength - 1) + 1,
+                        // which is the right calculation:
+                        beg = end + separatorLength ;
+                    }
+                } else {
+                    // We found a consecutive occurrence of the separator, so skip it.
+                    beg = end + separatorLength ;
+                }
+            } else {
+                // String.substring( beg ) goes from 'beg' to the end of the String.
+                substrings.add( str.substring( beg ) ) ;
+                end = len ;
+            }
+        }
+
+        return (String[]) substrings.toArray( new String[substrings.size()] ) ;
+    }
+
+
     //-----------------------------------------------------------------------
     /**
      * <p>Splits the provided text into an array, using whitespace as the
diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java
index 1797ab2..f641183 100644
--- a/src/test/org/apache/commons/lang/StringUtilsTest.java
+++ b/src/test/org/apache/commons/lang/StringUtilsTest.java
@@ -38,7 +38,7 @@
  * @author Phil Steitz
  * @author Gary D. Gregory
  * @author Al Chou
- * @version $Id: StringUtilsTest.java,v 1.61 2004/08/15 23:47:05 scolebourne Exp $
+ * @version $Id: StringUtilsTest.java,v 1.62 2004/08/22 03:40:27 bayard Exp $
  */
 public class StringUtilsTest extends TestCase {
     
@@ -360,6 +360,57 @@
         assertEquals(msg, str.substring(2), res[1]);
     }
 
+    public void testSplitByWholeString_StringStringBoolean() {
+        assertEquals( null, StringUtils.splitByWholeSeparator( null, "." ) ) ;
+
+        assertEquals( 0, StringUtils.splitByWholeSeparator( "", "." ).length ) ;
+
+        String stringToSplitOnNulls = "ab   de fg" ;
+        String[] splitOnNullExpectedResults = { "ab", "de", "fg" } ;
+
+        String[] splitOnNullResults = StringUtils.splitByWholeSeparator( "ab   de fg", null ) ;
+        assertEquals( splitOnNullExpectedResults.length, splitOnNullResults.length ) ;
+        for ( int i = 0 ; i < splitOnNullExpectedResults.length ; i+= 1 ) {
+            assertEquals( splitOnNullExpectedResults[i], splitOnNullResults[i] ) ;
+        }
+
+        String stringToSplitOnCharactersAndString = "abstemiouslyaeiouyabstemiously" ;
+
+        String[] splitOnStringExpectedResults = { "abstemiously", "abstemiously" } ;
+        String[] splitOnStringResults = StringUtils.splitByWholeSeparator( stringToSplitOnCharactersAndString, "aeiouy" ) ;
+        assertEquals( splitOnStringExpectedResults.length, splitOnStringResults.length ) ;
+        for ( int i = 0 ; i < splitOnStringExpectedResults.length ; i+= 1 ) {
+            assertEquals( splitOnStringExpectedResults[i], splitOnStringResults[i] ) ;
+        }
+    }
+
+    public void testSplitByWholeString_StringStringBooleanInt() {
+        assertEquals( null, StringUtils.splitByWholeSeparator( null, ".", 3 ) ) ;
+
+        assertEquals( 0, StringUtils.splitByWholeSeparator( "", ".", 3 ).length ) ;
+
+        String stringToSplitOnNulls = "ab   de fg" ;
+        String[] splitOnNullExpectedResults = { "ab", "de fg" } ;
+        //String[] splitOnNullExpectedResults = { "ab", "de" } ;
+
+        String[] splitOnNullResults = StringUtils.splitByWholeSeparator( stringToSplitOnNulls, null, 2 ) ;
+        assertEquals( splitOnNullExpectedResults.length, splitOnNullResults.length ) ;
+        for ( int i = 0 ; i < splitOnNullExpectedResults.length ; i+= 1 ) {
+            assertEquals( splitOnNullExpectedResults[i], splitOnNullResults[i] ) ;
+        }
+
+        String stringToSplitOnCharactersAndString = "abstemiouslyaeiouyabstemiouslyaeiouyabstemiously" ;
+
+        String[] splitOnStringExpectedResults = { "abstemiously", "abstemiouslyaeiouyabstemiously" } ;
+        //String[] splitOnStringExpectedResults = { "abstemiously", "abstemiously" } ;
+        String[] splitOnStringResults = StringUtils.splitByWholeSeparator( stringToSplitOnCharactersAndString, "aeiouy", 2 ) ;
+        assertEquals( splitOnStringExpectedResults.length, splitOnStringResults.length ) ;
+        for ( int i = 0 ; i < splitOnStringExpectedResults.length ; i++ ) {
+            assertEquals( splitOnStringExpectedResults[i], splitOnStringResults[i] ) ;
+        }
+    }
+
+    
     public void testSplitPreserveAllTokens_String() {
         assertEquals(null, StringUtils.splitPreserveAllTokens(null));
         assertEquals(0, StringUtils.splitPreserveAllTokens("").length);