You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2004/08/22 05:40:27 UTC

cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang StringUtilsTest.java

bayard      2004/08/21 20:40:27

  Modified:    lang/src/java/org/apache/commons/lang StringUtils.java
               lang/src/test/org/apache/commons/lang StringUtilsTest.java
  Log:
  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
  
  Revision  Changes    Path
  1.134     +114 -1    jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
  retrieving revision 1.133
  retrieving revision 1.134
  diff -u -r1.133 -r1.134
  --- StringUtils.java	15 Aug 2004 23:47:05 -0000	1.133
  +++ StringUtils.java	22 Aug 2004 03:40:27 -0000	1.134
  @@ -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
  
  
  
  1.62      +52 -1     jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java
  
  Index: StringUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java,v
  retrieving revision 1.61
  retrieving revision 1.62
  diff -u -r1.61 -r1.62
  --- StringUtilsTest.java	15 Aug 2004 23:47:05 -0000	1.61
  +++ StringUtilsTest.java	22 Aug 2004 03:40:27 -0000	1.62
  @@ -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);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org