Update slice methods to be more consistent

from Phil Steitz


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137492 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-25 00:50:00 +00:00
parent e758deb5e8
commit d33605dfc9
2 changed files with 39 additions and 50 deletions

View File

@ -146,7 +146,7 @@
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a> * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author Phil Steitz * @author Phil Steitz
* @since 1.0 * @since 1.0
* @version $Id: StringUtils.java,v 1.77 2003/07/22 23:36:40 scolebourne Exp $ * @version $Id: StringUtils.java,v 1.78 2003/07/25 00:50:00 scolebourne Exp $
*/ */
public class StringUtils { public class StringUtils {
// Performance testing notes (JDK 1.4, Jul03, scolebourne) // Performance testing notes (JDK 1.4, Jul03, scolebourne)
@ -2438,8 +2438,6 @@ public static String slice(String str) {
* <p>A <code>null</code> string input will return <code>null</code>. * <p>A <code>null</code> string input will return <code>null</code>.
* An empty ("") string input will return the empty string. * An empty ("") string input will return the empty string.
* An empty or <code>null</code> separator will return the input string.</p> * An empty or <code>null</code> separator will return the input string.</p>
*
* <p>This method is the opposite of {@link #sliceRemainder(String, String)}.</p>
* *
* <pre> * <pre>
* StringUtils.slice(null, *) = null * StringUtils.slice(null, *) = null
@ -2462,12 +2460,11 @@ public static String slice(String str, String separator) {
if (str == null || separator == null || str.length() == 0 || separator.length() == 0) { if (str == null || separator == null || str.length() == 0 || separator.length() == 0) {
return str; return str;
} }
int idx = str.lastIndexOf(separator); int pos = str.lastIndexOf(separator);
if (idx != -1) { if (pos == -1) {
return str.substring(0, idx);
} else {
return str; return str;
} }
return str.substring(0, pos);
} }
/** /**
@ -2476,9 +2473,8 @@ public static String slice(String str, String separator) {
* *
* <p>A <code>null</code> string input will return <code>null</code>. * <p>A <code>null</code> string input will return <code>null</code>.
* An empty ("") string input will return the empty string. * An empty ("") string input will return the empty string.
* An empty or <code>null</code> separator will return the empty string.</p> * An empty or <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 {@link #slice(String, String)}.</p>
* *
* <pre> * <pre>
* StringUtils.sliceRemainder(null, *) = null * StringUtils.sliceRemainder(null, *) = null
@ -2506,14 +2502,11 @@ public static String sliceRemainder(String str, String separator) {
if (separator == null || separator.length() == 0) { if (separator == null || separator.length() == 0) {
return ""; return "";
} }
int idx = str.lastIndexOf(separator); int pos = str.lastIndexOf(separator);
if (idx == str.length() - separator.length()) { if (pos == -1 || pos == (str.length() - separator.length())) {
return "";
} else if (idx != -1) {
return str.substring(idx + separator.length());
} else {
return ""; return "";
} }
return str.substring(pos + separator.length());
} }
/** /**
@ -2522,9 +2515,7 @@ public static String sliceRemainder(String str, String separator) {
* *
* <p>A <code>null</code> string input will return <code>null</code>. * <p>A <code>null</code> string input will return <code>null</code>.
* An empty ("") string input will return the empty string. * An empty ("") string input will return the empty string.
* An empty or <code>null</code> separator will return the input string.</p> * A <code>null</code> separator will return the input string.</p>
*
* <p>This method is the opposite of {@link #sliceFirst(String, String)}.</p>
* *
* <pre> * <pre>
* StringUtils.sliceFirst(null, *) = null * StringUtils.sliceFirst(null, *) = null
@ -2532,8 +2523,8 @@ public static String sliceRemainder(String str, String separator) {
* StringUtils.sliceFirst("abc", "a") = "" * StringUtils.sliceFirst("abc", "a") = ""
* StringUtils.sliceFirst("abcba", "b") = "a" * StringUtils.sliceFirst("abcba", "b") = "a"
* StringUtils.sliceFirst("abc", "c") = "ab" * StringUtils.sliceFirst("abc", "c") = "ab"
* StringUtils.sliceFirst("abc", "d") = "" * StringUtils.sliceFirst("abc", "d") = "abc"
* StringUtils.sliceFirst("abc", "") = "abc" * StringUtils.sliceFirst("abc", "") = ""
* StringUtils.sliceFirst("abc", null) = "abc" * StringUtils.sliceFirst("abc", null) = "abc"
* </pre> * </pre>
* *
@ -2545,15 +2536,17 @@ public static String sliceRemainder(String str, String separator) {
* @return sliced String, <code>null</code> if null String input * @return sliced String, <code>null</code> if null String input
*/ */
public static String sliceFirst(String str, String separator) { 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; return str;
} }
int idx = str.indexOf(separator); if (separator.length() == 0) {
if (idx != -1) {
return str.substring(0, idx);
} else {
return ""; return "";
} }
int pos = str.indexOf(separator);
if (pos == -1) {
return str;
}
return str.substring(0, pos);
} }
/** /**
@ -2562,19 +2555,18 @@ public static String sliceFirst(String str, String separator) {
* *
* <p>A <code>null</code> string input will return <code>null</code>. * <p>A <code>null</code> string input will return <code>null</code>.
* An empty ("") string input will return the empty string. * 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 {@link #sliceFirst(String, String)}.</p>
*
* <pre> * <pre>
* StringUtils.sliceFirstRemainder(null, *) = null * StringUtils.sliceFirstRemainder(null, *) = null
* StringUtils.sliceFirstRemainder("", *) = "" * StringUtils.sliceFirstRemainder("", *) = ""
* StringUtils.sliceFirstRemainder(*, "") = "" * StringUtils.sliceFirstRemainder(*, null) = ""
* StringUtils.sliceFirstRemainder(*, null) = "" * StringUtils.sliceFirstRemainder("abc", "a") = "bc"
* StringUtils.sliceFirstRemainder("abc", "a") = "bc" * StringUtils.sliceFirstRemainder("abcba", "b") = "cba"
* StringUtils.sliceFirstRemainder("abcba", "b") = "cba" * StringUtils.sliceFirstRemainder("abc", "c") = ""
* StringUtils.sliceFirstRemainder("abc", "c") = "" * StringUtils.sliceFirstRemainder("abc", "d") = ""
* StringUtils.sliceFirstRemainder("abc", "d") = "abc" * StringUtils.sliceFirstRemainder("abc", "") = "abc"
* </pre> * </pre>
* *
* <p><em>(This method was formerly named prechomp. Also, previously * <p><em>(This method was formerly named prechomp. Also, previously
@ -2588,15 +2580,14 @@ public static String sliceFirstRemainder(String str, String separator) {
if (str == null || str.length() == 0) { if (str == null || str.length() == 0) {
return str; return str;
} }
if (separator == null || separator.length() == 0) { if (separator == null) {
return ""; return "";
} }
int idx = str.indexOf(separator); int pos = str.indexOf(separator);
if (idx != -1) { if (pos == -1) {
return str.substring(idx + separator.length()); return "";
} else {
return str;
} }
return str.substring(pos + separator.length());
} }
// Conversion // Conversion

View File

@ -72,7 +72,7 @@
* @author Holger Krauth * @author Holger Krauth
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a> * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author Phil Steitz * @author Phil Steitz
* @version $Id: StringUtilsTest.java,v 1.37 2003/07/22 23:36:39 scolebourne Exp $ * @version $Id: StringUtilsTest.java,v 1.38 2003/07/25 00:50:00 scolebourne Exp $
*/ */
public class StringUtilsTest extends TestCase { public class StringUtilsTest extends TestCase {
@ -649,13 +649,12 @@ public void testSliceFirst_StringString() {
assertEquals("", StringUtils.sliceFirst("", "XX")); assertEquals("", StringUtils.sliceFirst("", "XX"));
assertEquals("foo", StringUtils.sliceFirst("foo", null)); assertEquals("foo", StringUtils.sliceFirst("foo", null));
assertEquals("", StringUtils.sliceFirst("foo", "b")); assertEquals("foo", StringUtils.sliceFirst("foo", "b"));
assertEquals("f", StringUtils.sliceFirst("foot", "o")); assertEquals("f", StringUtils.sliceFirst("foot", "o"));
assertEquals("", StringUtils.sliceFirst("abc", "a")); assertEquals("", StringUtils.sliceFirst("abc", "a"));
assertEquals("a", StringUtils.sliceFirst("abcba", "b")); assertEquals("a", StringUtils.sliceFirst("abcba", "b"));
assertEquals("ab", StringUtils.sliceFirst("abc", "c")); assertEquals("ab", StringUtils.sliceFirst("abc", "c"));
assertEquals("abc", StringUtils.sliceFirst("abc", "")); assertEquals("", StringUtils.sliceFirst("abc", ""));
assertEquals("", StringUtils.sliceFirst("abc", "d"));
} }
public void testSliceFirstRemainder_StringString() { public void testSliceFirstRemainder_StringString() {
@ -669,13 +668,12 @@ public void testSliceFirstRemainder_StringString() {
assertEquals("", StringUtils.sliceFirstRemainder("", "XX")); assertEquals("", StringUtils.sliceFirstRemainder("", "XX"));
assertEquals("", StringUtils.sliceFirstRemainder("foo", null)); assertEquals("", StringUtils.sliceFirstRemainder("foo", null));
assertEquals("foo", StringUtils.sliceFirstRemainder("foo", "b"));
assertEquals("ot", StringUtils.sliceFirstRemainder("foot", "o")); assertEquals("ot", StringUtils.sliceFirstRemainder("foot", "o"));
assertEquals("bc", StringUtils.sliceFirstRemainder("abc", "a")); assertEquals("bc", StringUtils.sliceFirstRemainder("abc", "a"));
assertEquals("cba", StringUtils.sliceFirstRemainder("abcba", "b")); assertEquals("cba", StringUtils.sliceFirstRemainder("abcba", "b"));
assertEquals("", StringUtils.sliceFirstRemainder("abc", "c")); assertEquals("", StringUtils.sliceFirstRemainder("abc", "c"));
assertEquals("", StringUtils.sliceFirstRemainder("abc", "")); assertEquals("abc", StringUtils.sliceFirstRemainder("abc", ""));
assertEquals("abc", StringUtils.sliceFirstRemainder("abc", "d")); assertEquals("", StringUtils.sliceFirstRemainder("abc", "d"));
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------