From 30ddfad67fed59e5c61d039f0b4c5be697d8ddae Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sun, 20 Jul 2003 00:17:29 +0000 Subject: [PATCH] Deprecate deleteSpaces() Move delete methods next to replace git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137463 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/StringUtils.java | 130 +++++++++--------- .../apache/commons/lang/StringUtilsTest.java | 92 ++++++++++--- .../lang/StringUtilsTrimEmptyTest.java | 22 +-- 3 files changed, 139 insertions(+), 105 deletions(-) diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index 469a37ef7..699afa70d 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -98,7 +98,7 @@ import org.apache.commons.lang.math.NumberUtils; * @author Arun Mammen Thomas * @author Gary Gregory * @since 1.0 - * @version $Id: StringUtils.java,v 1.68 2003/07/20 00:04:12 scolebourne Exp $ + * @version $Id: StringUtils.java,v 1.69 2003/07/20 00:17:29 scolebourne Exp $ */ public class StringUtils { // Performance testing notes (JDK 1.4, Jul03, scolebourne) @@ -250,66 +250,6 @@ public class StringUtils { return (str == null ? "" : str.trim()); } - // Delete - //----------------------------------------------------------------------- - - /** - *

Deletes all 'space' characters from a String as defined by - * {@link Character#isSpace(char)}.

- * - *

This is the only StringUtils method that uses the - * isSpace definition. You are advised to use - * {@link #deleteWhitespace(String)} instead as whitespace is much - * better localized.

- * - *
-     * StringUtils.deleteSpaces(null)           = null
-     * StringUtils.deleteSpaces("abc")          = "abc"
-     * StringUtils.deleteSpaces(" \t  abc \n ") = "abc"
-     * StringUtils.deleteSpaces("ab  c")        = "abc"
-     * StringUtils.deleteSpaces("a\nb\tc     ") = "abc"
-     * 
- * - *

Spaces are defined as {' ', '\t', '\r', '\n', '\b'} - * in line with the deprecated isSpace method.

- * - * @param str the String to delete spaces from, may be null - * @return the String without 'spaces', null if null String input - */ - public static String deleteSpaces(String str) { - if (str == null) { - return null; - } - return CharSetUtils.delete(str, " \t\r\n\b"); - } - - /** - *

Deletes all whitespaces from a String as defined by - * {@link Character#isWhitespace(char)}.

- * - *
-     * StringUtils.deleteWhitespace(null)        = null
-     * StringUtils.deleteWhitespace("abc")       = "abc"
-     * StringUtils.deleteWhitespace("   abc  ")  = "abc"
-     * 
- * - * @param str the String to delete whitespace from, may be null - * @return the String without whitespaces, null if null String input - */ - public static String deleteWhitespace(String str) { - if (str == null) { - return null; - } - int sz = str.length(); - StringBuffer buffer = new StringBuffer(sz); - for (int i = 0; i < sz; i++) { - if (!Character.isWhitespace(str.charAt(i))) { - buffer.append(str.charAt(i)); - } - } - return buffer.toString(); - } - // Empty checks //----------------------------------------------------------------------- @@ -1577,11 +1517,73 @@ public class StringUtils { return buf.toString(); } + // Delete + //----------------------------------------------------------------------- + + /** + *

Deletes all 'space' characters from a String as defined by + * {@link Character#isSpace(char)}.

+ * + *

This is the only StringUtils method that uses the + * isSpace definition. You are advised to use + * {@link #deleteWhitespace(String)} instead as whitespace is much + * better localized.

+ * + *
+     * StringUtils.deleteSpaces(null)           = null
+     * StringUtils.deleteSpaces("abc")          = "abc"
+     * StringUtils.deleteSpaces(" \t  abc \n ") = "abc"
+     * StringUtils.deleteSpaces("ab  c")        = "abc"
+     * StringUtils.deleteSpaces("a\nb\tc     ") = "abc"
+     * 
+ * + *

Spaces are defined as {' ', '\t', '\r', '\n', '\b'} + * in line with the deprecated isSpace method.

+ * + * @param str the String to delete spaces from, may be null + * @return the String without 'spaces', null if null String input + * @deprecated Use the better localized {@link #deleteWhitespace(String)}. + * Method will be removed in Commons Lang 3.0. + */ + public static String deleteSpaces(String str) { + if (str == null) { + return null; + } + return CharSetUtils.delete(str, " \t\r\n\b"); + } + + /** + *

Deletes all whitespaces from a String as defined by + * {@link Character#isWhitespace(char)}.

+ * + *
+     * StringUtils.deleteWhitespace(null)        = null
+     * StringUtils.deleteWhitespace("abc")       = "abc"
+     * StringUtils.deleteWhitespace("   abc  ")  = "abc"
+     * 
+ * + * @param str the String to delete whitespace from, may be null + * @return the String without whitespaces, null if null String input + */ + public static String deleteWhitespace(String str) { + if (str == null) { + return null; + } + int sz = str.length(); + StringBuffer buffer = new StringBuffer(sz); + for (int i = 0; i < sz; i++) { + if (!Character.isWhitespace(str.charAt(i))) { + buffer.append(str.charAt(i)); + } + } + return buffer.toString(); + } + // Replacing //----------------------------------------------------------------------- /** - *

Replace a String with another String inside a larger String, once.

+ *

Replaces a String with another String inside a larger String, once.

* *

A null reference passed to this method is a no-op.

* @@ -1606,7 +1608,7 @@ public class StringUtils { } /** - *

Replace all occurances of a String within another String.

+ *

Replaces all occurances of a String within another String.

* *

A null reference passed to this method is a no-op.

* @@ -1631,7 +1633,7 @@ public class StringUtils { } /** - *

Replace a String with another String inside a larger String, + *

Replaces a String with another String inside a larger String, * for the first max values of the search String.

* *

A null reference passed to this method is a no-op.

diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java index eddfc73e9..8e8acb55f 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTest.java @@ -72,7 +72,7 @@ import junit.textui.TestRunner; * @author Henning P. Schmiedehausen - * @version $Id: StringUtilsTest.java,v 1.30 2003/07/19 23:28:23 scolebourne Exp $ + * @version $Id: StringUtilsTest.java,v 1.31 2003/07/20 00:17:29 scolebourne Exp $ */ public class StringUtilsTest extends TestCase { @@ -341,26 +341,78 @@ public class StringUtilsTest extends TestCase { } } - public void testReplaceFunctions() { - assertEquals("replace(String, String, String, int) failed", - FOO, StringUtils.replace("oo" + FOO, "o", "", 2)); - assertEquals("replace(String, String, String) failed", - "", StringUtils.replace(FOO + FOO + FOO, FOO, "")); - assertEquals("replaceOnce(String, String, String) failed", - FOO, StringUtils.replaceOnce(FOO + FOO, FOO, "")); - assertEquals("carriage-return replace(String,String,String) failed", - "test123", StringUtils.replace("test\r1\r2\r3", "\r", "")); + public void testDeleteSpace_String() { + assertEquals(null, StringUtils.deleteSpaces(null)); + assertEquals("", StringUtils.deleteSpaces("")); + assertEquals("", StringUtils.deleteSpaces(" \t\t\n\n ")); + assertEquals("test", StringUtils.deleteSpaces("t \t\ne\rs\n\n \tt")); + } + + public void testDeleteWhitespace_String() { + assertEquals(null, StringUtils.deleteWhitespace(null)); + assertEquals("", StringUtils.deleteWhitespace("")); + assertEquals("", StringUtils.deleteWhitespace(" \u000C \t\t\u001F\n\n \u000B ")); + assertEquals("", StringUtils.deleteWhitespace(StringUtilsTest.WHITESPACE)); + assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils.deleteWhitespace(StringUtilsTest.NON_WHITESPACE)); + // Note: u-2007 and u-000A both cause problems in the source code + // it should ignore 2007 but delete 000A + assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace(" \u00A0 \t\t\n\n \u202F ")); + assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace("\u00A0\u202F")); + assertEquals("test", StringUtils.deleteWhitespace("\u000Bt \t\n\u0009e\rs\n\n \tt")); + } - assertEquals("replace(String, String, String) failed", - "FOO", StringUtils.replace("FOO", "", "any")); - assertEquals("replace(String, String, String) failed", - "FOO", StringUtils.replace("FOO", null, "any")); - assertEquals("replace(String, String, String) failed", - "FOO", StringUtils.replace("FOO", "F", null)); - assertEquals("replace(String, String, String) failed", - "FOO", StringUtils.replace("FOO", null, null)); - assertEquals("replace(String, String, String) failed", - null, StringUtils.replace(null, "", "any")); + public void testReplace_StringStringString() { + assertEquals(null, StringUtils.replace(null, null, null)); + assertEquals(null, StringUtils.replace(null, null, "any")); + assertEquals(null, StringUtils.replace(null, "any", null)); + assertEquals(null, StringUtils.replace(null, "any", "any")); + + assertEquals("", StringUtils.replace("", null, null)); + assertEquals("", StringUtils.replace("", null, "any")); + assertEquals("", StringUtils.replace("", "any", null)); + assertEquals("", StringUtils.replace("", "any", "any")); + + assertEquals("FOO", StringUtils.replace("FOO", "", "any")); + assertEquals("FOO", StringUtils.replace("FOO", null, "any")); + assertEquals("FOO", StringUtils.replace("FOO", "F", null)); + assertEquals("FOO", StringUtils.replace("FOO", null, null)); + + assertEquals("", StringUtils.replace("foofoofoo", "foo", "")); + assertEquals("barbarbar", StringUtils.replace("foofoofoo", "foo", "bar")); + assertEquals("farfarfar", StringUtils.replace("foofoofoo", "oo", "ar")); + } + + public void testReplace_StringStringStringInt() { + assertEquals(null, StringUtils.replace(null, null, null, 2)); + assertEquals(null, StringUtils.replace(null, null, "any", 2)); + assertEquals(null, StringUtils.replace(null, "any", null, 2)); + assertEquals(null, StringUtils.replace(null, "any", "any", 2)); + + assertEquals("f", StringUtils.replace("oofoo", "o", "", -1)); + assertEquals("oofoo", StringUtils.replace("oofoo", "o", "", 0)); + assertEquals("ofoo", StringUtils.replace("oofoo", "o", "", 1)); + assertEquals("foo", StringUtils.replace("oofoo", "o", "", 2)); + assertEquals("fo", StringUtils.replace("oofoo", "o", "", 3)); + assertEquals("f", StringUtils.replace("oofoo", "o", "", 4)); + } + + public void testReplaceOnce_StringStringString() { + assertEquals(null, StringUtils.replaceOnce(null, null, null)); + assertEquals(null, StringUtils.replaceOnce(null, null, "any")); + assertEquals(null, StringUtils.replaceOnce(null, "any", null)); + assertEquals(null, StringUtils.replaceOnce(null, "any", "any")); + + assertEquals("", StringUtils.replaceOnce("", null, null)); + assertEquals("", StringUtils.replaceOnce("", null, "any")); + assertEquals("", StringUtils.replaceOnce("", "any", null)); + assertEquals("", StringUtils.replaceOnce("", "any", "any")); + + assertEquals("FOO", StringUtils.replaceOnce("FOO", "", "any")); + assertEquals("FOO", StringUtils.replaceOnce("FOO", null, "any")); + assertEquals("FOO", StringUtils.replaceOnce("FOO", "F", null)); + assertEquals("FOO", StringUtils.replaceOnce("FOO", null, null)); + + assertEquals("foofoo", StringUtils.replaceOnce("foofoofoo", "foo", "")); } public void testOverlayString() { diff --git a/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java b/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java index b600ae5e3..07d421dfa 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTrimEmptyTest.java @@ -63,7 +63,7 @@ import junit.textui.TestRunner; * * @author Stephen Colebourne * @author Ringo De Smet - * @version $Id: StringUtilsTrimEmptyTest.java,v 1.13 2003/07/20 00:04:12 scolebourne Exp $ + * @version $Id: StringUtilsTrimEmptyTest.java,v 1.14 2003/07/20 00:17:29 scolebourne Exp $ */ public class StringUtilsTrimEmptyTest extends TestCase { private static final String FOO = "foo"; @@ -170,26 +170,6 @@ public class StringUtilsTrimEmptyTest extends TestCase { assertEquals(true, StringUtils.isNotBlank(" foo ")); } - public void testDeleteSpace() { - assertEquals(null, StringUtils.deleteSpaces(null)); - assertEquals("", StringUtils.deleteSpaces("")); - assertEquals("", StringUtils.deleteSpaces(" \t\t\n\n ")); - assertEquals("test", StringUtils.deleteSpaces("t \t\ne\rs\n\n \tt")); - } - - public void testDeleteWhitespace() { - assertEquals(null, StringUtils.deleteWhitespace(null)); - assertEquals("", StringUtils.deleteWhitespace("")); - assertEquals("", StringUtils.deleteWhitespace(" \u000C \t\t\u001F\n\n \u000B ")); - assertEquals("", StringUtils.deleteWhitespace(StringUtilsTest.WHITESPACE)); - assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils.deleteWhitespace(StringUtilsTest.NON_WHITESPACE)); - // Note: u-2007 and u-000A both cause problems in the source code - // it should ignore 2007 but delete 000A - assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace(" \u00A0 \t\t\n\n \u202F ")); - assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace("\u00A0\u202F")); - assertEquals("test", StringUtils.deleteWhitespace("\u000Bt \t\n\u0009e\rs\n\n \tt")); - } - public void testStrip_String() { assertEquals(null, StringUtils.strip(null)); assertEquals("", StringUtils.strip(""));