scolebourne 2003/07/24 17:50:01
Modified: lang/src/test/org/apache/commons/lang StringUtilsTest.java
lang/src/java/org/apache/commons/lang StringUtils.java
Log:
Update slice methods to be more consistent
from Phil Steitz
Revision Changes Path
1.38 +5 -7
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.37
retrieving revision 1.38
diff -u -r1.37 -r1.38
--- StringUtilsTest.java 22 Jul 2003 23:36:39 -0000 1.37
+++ StringUtilsTest.java 25 Jul 2003 00:50:00 -0000 1.38
@@ -649,13 +649,12 @@
assertEquals("", StringUtils.sliceFirst("", "XX"));
assertEquals("foo", StringUtils.sliceFirst("foo", null));
- assertEquals("", StringUtils.sliceFirst("foo", "b"));
+ assertEquals("foo", StringUtils.sliceFirst("foo", "b"));
assertEquals("f", StringUtils.sliceFirst("foot", "o"));
assertEquals("", StringUtils.sliceFirst("abc", "a"));
assertEquals("a", StringUtils.sliceFirst("abcba", "b"));
assertEquals("ab", StringUtils.sliceFirst("abc", "c"));
- assertEquals("abc", StringUtils.sliceFirst("abc", ""));
- assertEquals("", StringUtils.sliceFirst("abc", "d"));
+ assertEquals("", StringUtils.sliceFirst("abc", ""));
}
public void testSliceFirstRemainder_StringString() {
@@ -669,13 +668,12 @@
assertEquals("", StringUtils.sliceFirstRemainder("", "XX"));
assertEquals("", StringUtils.sliceFirstRemainder("foo", null));
- assertEquals("foo", StringUtils.sliceFirstRemainder("foo", "b"));
assertEquals("ot", StringUtils.sliceFirstRemainder("foot", "o"));
assertEquals("bc", StringUtils.sliceFirstRemainder("abc", "a"));
assertEquals("cba", StringUtils.sliceFirstRemainder("abcba", "b"));
assertEquals("", StringUtils.sliceFirstRemainder("abc", "c"));
- assertEquals("", StringUtils.sliceFirstRemainder("abc", ""));
- assertEquals("abc", StringUtils.sliceFirstRemainder("abc", "d"));
+ assertEquals("abc", StringUtils.sliceFirstRemainder("abc", ""));
+ assertEquals("", StringUtils.sliceFirstRemainder("abc", "d"));
}
//-----------------------------------------------------------------------
1.78 +34 -43
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.77
retrieving revision 1.78
diff -u -r1.77 -r1.78
--- StringUtils.java 22 Jul 2003 23:36:40 -0000 1.77
+++ StringUtils.java 25 Jul 2003 00:50:00 -0000 1.78
@@ -2438,8 +2438,6 @@
* <p>A <code>null</code> string input will return <code>null</code>.
* An empty ("") string input will return the empty string.
* An empty or <code>null</code> separator will return the input string.</p>
- *
- * <p>This method is the opposite of [EMAIL PROTECTED] #sliceRemainder(String,
String)}.</p>
*
* <pre>
* StringUtils.slice(null, *) = null
@@ -2462,12 +2460,11 @@
if (str == null || separator == null || str.length() == 0 ||
separator.length() == 0) {
return str;
}
- int idx = str.lastIndexOf(separator);
- if (idx != -1) {
- return str.substring(0, idx);
- } else {
+ int pos = str.lastIndexOf(separator);
+ if (pos == -1) {
return str;
}
+ return str.substring(0, pos);
}
/**
@@ -2476,9 +2473,8 @@
*
* <p>A <code>null</code> string input will return <code>null</code>.
* An empty ("") string input will return the empty string.
- * An empty or <code>null</code> separator will return the empty string.</p>
- *
- * <p>This method is the opposite of [EMAIL PROTECTED] #slice(String,
String)}.</p>
+ * An empty or <code>null</code> separator will return the empty string if
+ * the input string is not <code>null</code>.</p>
*
* <pre>
* StringUtils.sliceRemainder(null, *) = null
@@ -2506,14 +2502,11 @@
if (separator == null || separator.length() == 0) {
return "";
}
- int idx = str.lastIndexOf(separator);
- if (idx == str.length() - separator.length()) {
- return "";
- } else if (idx != -1) {
- return str.substring(idx + separator.length());
- } else {
+ int pos = str.lastIndexOf(separator);
+ if (pos == -1 || pos == (str.length() - separator.length())) {
return "";
}
+ return str.substring(pos + separator.length());
}
/**
@@ -2522,9 +2515,7 @@
*
* <p>A <code>null</code> string input will return <code>null</code>.
* An empty ("") string input will return the empty string.
- * An empty or <code>null</code> separator will return the input string.</p>
- *
- * <p>This method is the opposite of [EMAIL PROTECTED] #sliceFirst(String,
String)}.</p>
+ * A <code>null</code> separator will return the input string.</p>
*
* <pre>
* StringUtils.sliceFirst(null, *) = null
@@ -2532,8 +2523,8 @@
* StringUtils.sliceFirst("abc", "a") = ""
* StringUtils.sliceFirst("abcba", "b") = "a"
* StringUtils.sliceFirst("abc", "c") = "ab"
- * StringUtils.sliceFirst("abc", "d") = ""
- * StringUtils.sliceFirst("abc", "") = "abc"
+ * StringUtils.sliceFirst("abc", "d") = "abc"
+ * StringUtils.sliceFirst("abc", "") = ""
* StringUtils.sliceFirst("abc", null) = "abc"
* </pre>
*
@@ -2545,15 +2536,17 @@
* @return sliced String, <code>null</code> if null String input
*/
public static String sliceFirst(String str, String separator) {
- if (str == null || separator == null || str.length() == 0 ||
separator.length() == 0) {
+ if (str == null || separator == null || str.length() == 0) {
return str;
}
- int idx = str.indexOf(separator);
- if (idx != -1) {
- return str.substring(0, idx);
- } else {
+ if (separator.length() == 0) {
return "";
}
+ int pos = str.indexOf(separator);
+ if (pos == -1) {
+ return str;
+ }
+ return str.substring(0, pos);
}
/**
@@ -2562,19 +2555,18 @@
*
* <p>A <code>null</code> string input will return <code>null</code>.
* An empty ("") string input will return the empty string.
- * An empty or <code>null</code> separator will return the empty string.</p>
+ * A <code>null</code> separator will return the empty string if the
+ * input string is not <code>null</code>.</p>
*
- * <p>This method is the opposite of [EMAIL PROTECTED] #sliceFirst(String,
String)}.</p>
- *
* <pre>
- * StringUtils.sliceFirstRemainder(null, *) = null
- * StringUtils.sliceFirstRemainder("", *) = ""
- * StringUtils.sliceFirstRemainder(*, "") = ""
- * StringUtils.sliceFirstRemainder(*, null) = ""
- * StringUtils.sliceFirstRemainder("abc", "a") = "bc"
- * StringUtils.sliceFirstRemainder("abcba", "b") = "cba"
- * StringUtils.sliceFirstRemainder("abc", "c") = ""
- * StringUtils.sliceFirstRemainder("abc", "d") = "abc"
+ * StringUtils.sliceFirstRemainder(null, *) = null
+ * StringUtils.sliceFirstRemainder("", *) = ""
+ * StringUtils.sliceFirstRemainder(*, null) = ""
+ * StringUtils.sliceFirstRemainder("abc", "a") = "bc"
+ * StringUtils.sliceFirstRemainder("abcba", "b") = "cba"
+ * StringUtils.sliceFirstRemainder("abc", "c") = ""
+ * StringUtils.sliceFirstRemainder("abc", "d") = ""
+ * StringUtils.sliceFirstRemainder("abc", "") = "abc"
* </pre>
*
* <p><em>(This method was formerly named prechomp. Also, previously
@@ -2588,15 +2580,14 @@
if (str == null || str.length() == 0) {
return str;
}
- if (separator == null || separator.length() == 0) {
+ if (separator == null) {
return "";
}
- int idx = str.indexOf(separator);
- if (idx != -1) {
- return str.substring(idx + separator.length());
- } else {
- return str;
+ int pos = str.indexOf(separator);
+ if (pos == -1) {
+ return "";
}
+ return str.substring(pos + separator.length());
}
// Conversion
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]