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 2002/06/21 07:20:36 UTC
cvs commit: jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang StringsTest.java
bayard 2002/06/20 22:20:36
Modified: lang/src/java/org/apache/commons/lang Strings.java
lang/src/test/org/apache/commons/lang StringsTest.java
Log:
1. Brings split() in line with the default behavior of
java.util.StringTokenizer and both Perl and Python's implementation of split().
2. Replaces the 'no limit' magic number of -1 with any negative number. Need
to decide if this is desired.
Submitted by: Christopher Elkins
Revision Changes Path
1.7 +19 -7 jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/Strings.java
Index: Strings.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/Strings.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Strings.java 20 Jun 2002 03:20:28 -0000 1.6
+++ Strings.java 21 Jun 2002 05:20:36 -0000 1.7
@@ -239,7 +239,7 @@
*/
public static String[] split(String text)
{
- return split(text, " ", -1);
+ return split(text, null, -1);
}
/**
@@ -255,16 +255,28 @@
* separator.
*
* @param text Textual list to parse.
- * @param separator The separator character.
+ * @param separator The separator character. If <code>null</code>, splits
+ * on whitespace.
* @param max The maximum number of elements to include in the
- * list. A value of <code>-1</code> implies no limit.
+ * list. A zero or negative value implies no limit.
* @return The list of values.
*/
public static String[] split(String text, String separator, int max)
{
- StringTokenizer tok = new StringTokenizer(text, separator);
+ StringTokenizer tok = null;
+ if (separator == null)
+ {
+ // Null separator means we're using StringTokenizer's default
+ // delimiter, which comprises all whitespace characters.
+ tok = new StringTokenizer(text);
+ }
+ else
+ {
+ tok = new StringTokenizer(text, separator);
+ }
+
int listSize = tok.countTokens();
- if (max != -1 && listSize > max)
+ if (max > 0 && listSize > max)
{
listSize = max;
}
@@ -273,7 +285,7 @@
int i = 0;
while (tok.hasMoreTokens())
{
- if (max != -1 && i == listSize - 1)
+ if (max > 0 && i == listSize - 1)
{
// In the situation where we hit the max yet have
// tokens left over in our input, the last list
1.3 +20 -0 jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang/StringsTest.java
Index: StringsTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang/StringsTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- StringsTest.java 9 Mar 2002 17:16:50 -0000 1.2
+++ StringsTest.java 21 Jun 2002 05:20:36 -0000 1.3
@@ -151,6 +151,26 @@
assertEquals("split(Object[], String, int) failed", expected[i],
result[i]);
}
+
+ result = Strings.split(TEXT_LIST, SEPARATOR, 0);
+ expected = ARRAY_LIST;
+ assertEquals("split(Object[], String, int) yielded unexpected length",
+ expected.length, result.length);
+ for (int i = 0; i < result.length; i++)
+ {
+ assertEquals("split(Object[], String, int) failed", expected[i],
+ result[i]);
+ }
+
+ result = Strings.split(TEXT_LIST, SEPARATOR, -1);
+ expected = ARRAY_LIST;
+ assertEquals("split(Object[], String, int) yielded unexpected length",
+ expected.length, result.length);
+ for (int i = 0; i < result.length; i++)
+ {
+ assertEquals("split(Object[], String, int) failed", expected[i],
+ result[i]);
+ }
}
public void testReplaceFunctions()
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>