diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 557d6d51b..0df6fdb25 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -1,4 +1,4 @@ -$Id: RELEASE-NOTES.txt,v 1.22 2003/08/17 22:01:04 scolebourne Exp $ +$Id: RELEASE-NOTES.txt,v 1.23 2003/08/17 22:59:28 scolebourne Exp $ Commons Lang Package Version 2.0 @@ -21,6 +21,7 @@ Some StringUtils methods have changed functionality from 1.0: isEmpty() chomp(String) chomp(String,String) + swapCase(String) Numerous other methods have changed null handling to accept nulls gracefully. As with all major version releases, check your code for incompatibilities. @@ -82,6 +83,7 @@ lang: StringUtils: isEmpty() changed to not trim chomp() changed to be more like Perl. + swapCase() no longer word based, but no difference if you pass in ASCII Various methods changed in the handling of null (less exceptions). Many new methods. Various methods deprecated. @@ -362,12 +364,9 @@ org.apache.commons.lang.StringUtils > public static java.lang.String capitalize(java.lang.String); > public static java.lang.String uncapitalize(java.lang.String); > public static java.lang.String uncapitalise(java.lang.String); -> public static java.lang.String capitalizeAllWords(java.lang.String); < public static java.lang.String getNestedString(java.lang.String, java.lang.String); < public static java.lang.String getNestedString(java.lang.String, java.lang.String, java.lang.String); --- -> public static java.lang.String uncapitalizeAllWords(java.lang.String); -> public static java.lang.String uncapitaliseAllWords(java.lang.String); > public static boolean isWhitespace(java.lang.String); > public static java.lang.String reverseDelimited(java.lang.String, char); > public static java.lang.String abbreviate(java.lang.String, int); diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index 917e9896e..8bbe05bb0 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -145,7 +145,7 @@ import java.util.List; * @author Gary Gregory * @author Phil Steitz * @since 1.0 - * @version $Id: StringUtils.java,v 1.100 2003/08/16 10:36:00 scolebourne Exp $ + * @version $Id: StringUtils.java,v 1.101 2003/08/17 22:56:11 scolebourne Exp $ */ public class StringUtils { // Performance testing notes (JDK 1.4, Jul03, scolebourne) @@ -3414,7 +3414,8 @@ public class StringUtils { *

Capitalizes a String changing the first letter to title case as * per {@link Character#toTitleCase(char)}. No other letters are changed.

* - *

A null input String returns null.

+ *

For a word based alorithm, see {@link WordUtils#capitalize(String)}. + * A null input String returns null.

* *
      * StringUtils.capitalize(null)  = null
@@ -3425,7 +3426,7 @@ public class StringUtils {
      * 
      * @param str  the String to capitalize, may be null
      * @return the capitalized String, null if null String input
-     * @see #capitalizeAllWords(String)
+     * @see WordUtils#capitalize(String)
      * @see #uncapitalize(String)
      */
     public static String capitalize(String str) {
@@ -3456,7 +3457,8 @@ public class StringUtils {
      * 

Uncapitalizes a String changing the first letter to title case as * per {@link Character#toLowerCase(char)}. No other letters are changed.

* - *

A null input String returns null.

+ *

For a word based alorithm, see {@link WordUtils#uncapitalize(String)}. + * A null input String returns null.

* *
      * StringUtils.uncapitalize(null)  = null
@@ -3467,7 +3469,7 @@ public class StringUtils {
      * 
      * @param str  the String to uncapitalize, may be null
      * @return the uncapitalized String, null if null String input
-     * @see #uncapitalizeAllWords(String)
+     * @see WordUtils#uncapitalize(String)
      * @see #capitalize(String)
      */
     public static String uncapitalize(String str) {
@@ -3495,16 +3497,16 @@ public class StringUtils {
     }
 
     /**
-     * 

Swaps the case of a String using a word based algorithm.

+ *

Swaps the case of a String changing upper and title case to + * lower case, and lower case to upper case.

* * * - *

Whitespace is defined by {@link Character#isWhitespace(char)}. + *

For a word based alorithm, see {@link WordUtils#swapCase(String)}. * A null input String returns null.

* *
@@ -3513,6 +3515,11 @@ public class StringUtils {
      * StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
      * 
* + *

NOTE: This method changed in Lang version 2.0. + * It no longer performs a word based alorithm. + * If you only use ASCII, you will notice no change. + * That functionality is available in WordUtils.

+ * * @param str the String to swap case, may be null * @return the changed String, null if null String input */ @@ -3523,67 +3530,17 @@ public class StringUtils { } StringBuffer buffer = new StringBuffer(strLen); - boolean whitespace = true; char ch = 0; - char tmp = 0; - for (int i = 0; i < strLen; i++) { ch = str.charAt(i); if (Character.isUpperCase(ch)) { - tmp = Character.toLowerCase(ch); + ch = Character.toLowerCase(ch); } else if (Character.isTitleCase(ch)) { - tmp = Character.toLowerCase(ch); + ch = Character.toLowerCase(ch); } else if (Character.isLowerCase(ch)) { - if (whitespace) { - tmp = Character.toTitleCase(ch); - } else { - tmp = Character.toUpperCase(ch); - } - } else { - tmp = ch; - } - buffer.append(tmp); - whitespace = Character.isWhitespace(ch); - } - return buffer.toString(); - } - - /** - *

Capitalizes all the whitespace separated words in a String. - * Only the first letter of each word is changed.

- * - *

Whitespace is defined by {@link Character#isWhitespace(char)}. - * A null input String returns null.

- * - *
-     * StringUtils.capitalizeAllWords(null)        = null
-     * StringUtils.capitalizeAllWords("")          = ""
-     * StringUtils.capitalizeAllWords("i am FINE") = "I Am FINE"
-     * 
- * - * @param str the String to capitalize, may be null - * @return capitalized String, null if null String input - * @see #capitalize(String) - * @see #uncapitalizeAllWords(String) - */ - public static String capitalizeAllWords(String str) { - int strLen; - if (str == null || (strLen = str.length()) == 0) { - return str; - } - StringBuffer buffer = new StringBuffer(strLen); - boolean whitespace = true; - for (int i = 0; i < strLen; i++) { - char ch = str.charAt(i); - if (Character.isWhitespace(ch)) { - buffer.append(ch); - whitespace = true; - } else if (whitespace) { - buffer.append(Character.toTitleCase(ch)); - whitespace = false; - } else { - buffer.append(ch); + ch = Character.toUpperCase(ch); } + buffer.append(ch); } return buffer.toString(); } @@ -3597,51 +3554,11 @@ public class StringUtils { * * @param str the String to capitalize, may be null * @return capitalized String, null if null String input - * @deprecated Use the standardly named {@link #capitalizeAllWords(String)}. + * @deprecated Use the relocated {@link WordUtils#capitalize(String)}. * Method will be removed in Commons Lang 3.0. */ public static String capitaliseAllWords(String str) { - return capitalizeAllWords(str); - } - - /** - *

Uncapitalizes all the whitespace separated words in a String. - * Only the first letter of each word is changed.

- * - *

Whitespace is defined by {@link Character#isWhitespace(char)}. - * A null input String returns null.

- * - *
-     * StringUtils.uncapitalizeAllWords(null)        = null
-     * StringUtils.uncapitalizeAllWords("")          = ""
-     * StringUtils.uncapitalizeAllWords("I Am FINE") = "i am fINE"
-     * 
- * - * @param str the String to uncapitalize, may be null - * @return uncapitalized String, null if null String input - * @see #uncapitalize(String) - * @see #capitalizeAllWords(String) - */ - public static String uncapitalizeAllWords(String str) { - int strLen; - if (str == null || (strLen = str.length()) == 0) { - return str; - } - StringBuffer buffer = new StringBuffer(strLen); - boolean whitespace = true; - for (int i = 0; i < strLen; i++) { - char ch = str.charAt(i); - if (Character.isWhitespace(ch)) { - buffer.append(ch); - whitespace = true; - } else if (whitespace) { - buffer.append(Character.toLowerCase(ch)); - whitespace = false; - } else { - buffer.append(ch); - } - } - return buffer.toString(); + return WordUtils.capitalize(str); } // Count matches diff --git a/src/java/org/apache/commons/lang/WordUtils.java b/src/java/org/apache/commons/lang/WordUtils.java index bdd6eb7f5..9beee0b3d 100644 --- a/src/java/org/apache/commons/lang/WordUtils.java +++ b/src/java/org/apache/commons/lang/WordUtils.java @@ -66,7 +66,7 @@ package org.apache.commons.lang; * @author Henning P. Schmiedehausen * @author Gary Gregory * @since 2.0 - * @version $Id: WordUtils.java,v 1.1 2003/08/17 21:57:37 scolebourne Exp $ + * @version $Id: WordUtils.java,v 1.2 2003/08/17 22:56:11 scolebourne Exp $ */ public class WordUtils { @@ -155,6 +155,8 @@ public class WordUtils { // return (stringBuffer.toString()); // } + // Wrapping + //----------------------------------------------------------------------- /** *

Wraps a single line of text, identifying words by ' '.

* @@ -250,4 +252,140 @@ public class WordUtils { return wrappedLine.toString(); } + // Capitalizing + //----------------------------------------------------------------------- + /** + *

Capitalizes all the whitespace separated words in a String. + * Only the first letter of each word is changed.

+ * + *

Whitespace is defined by {@link Character#isWhitespace(char)}. + * A null input String returns null. + * Capitalization uses the unicode title case, normally equivalent to + * upper case.

+ * + *
+     * WordUtils.capitalize(null)        = null
+     * WordUtils.capitalize("")          = ""
+     * WordUtils.capitalize("i am FINE") = "I Am FINE"
+     * 
+ * + * @param str the String to capitalize, may be null + * @return capitalized String, null if null String input + */ + public static String capitalize(String str) { + int strLen; + if (str == null || (strLen = str.length()) == 0) { + return str; + } + StringBuffer buffer = new StringBuffer(strLen); + boolean whitespace = true; + for (int i = 0; i < strLen; i++) { + char ch = str.charAt(i); + if (Character.isWhitespace(ch)) { + buffer.append(ch); + whitespace = true; + } else if (whitespace) { + buffer.append(Character.toTitleCase(ch)); + whitespace = false; + } else { + buffer.append(ch); + } + } + return buffer.toString(); + } + + /** + *

Uncapitalizes all the whitespace separated words in a String. + * Only the first letter of each word is changed.

+ * + *

Whitespace is defined by {@link Character#isWhitespace(char)}. + * A null input String returns null.

+ * + *
+     * WordUtils.uncapitalize(null)        = null
+     * WordUtils.uncapitalize("")          = ""
+     * WordUtils.uncapitalize("I Am FINE") = "i am fINE"
+     * 
+ * + * @param str the String to uncapitalize, may be null + * @return uncapitalized String, null if null String input + * @see #uncapitalize(String) + * @see #capitalizeAllWords(String) + */ + public static String uncapitalize(String str) { + int strLen; + if (str == null || (strLen = str.length()) == 0) { + return str; + } + StringBuffer buffer = new StringBuffer(strLen); + boolean whitespace = true; + for (int i = 0; i < strLen; i++) { + char ch = str.charAt(i); + if (Character.isWhitespace(ch)) { + buffer.append(ch); + whitespace = true; + } else if (whitespace) { + buffer.append(Character.toLowerCase(ch)); + whitespace = false; + } else { + buffer.append(ch); + } + } + return buffer.toString(); + } + + /** + *

Swaps the case of a String using a word based algorithm.

+ * + * + * + *

Whitespace is defined by {@link Character#isWhitespace(char)}. + * A null input String returns null.

+ * + *
+     * StringUtils.swapCase(null)                 = null
+     * StringUtils.swapCase("")                   = ""
+     * StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
+     * 
+ * + * @param str the String to swap case, may be null + * @return the changed String, null if null String input + */ + public static String swapCase(String str) { + int strLen; + if (str == null || (strLen = str.length()) == 0) { + return str; + } + StringBuffer buffer = new StringBuffer(strLen); + + boolean whitespace = true; + char ch = 0; + char tmp = 0; + + for (int i = 0; i < strLen; i++) { + ch = str.charAt(i); + if (Character.isUpperCase(ch)) { + tmp = Character.toLowerCase(ch); + } else if (Character.isTitleCase(ch)) { + tmp = Character.toLowerCase(ch); + } else if (Character.isLowerCase(ch)) { + if (whitespace) { + tmp = Character.toTitleCase(ch); + } else { + tmp = Character.toUpperCase(ch); + } + } else { + tmp = ch; + } + buffer.append(tmp); + whitespace = Character.isWhitespace(ch); + } + return buffer.toString(); + } + } diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java index 7dacafd2b..b00617c27 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTest.java @@ -75,7 +75,7 @@ import junit.textui.TestRunner; * @author Henning P. Schmiedehausen * @author Phil Steitz * @author Gary Gregory - * @version $Id: StringUtilsTest.java,v 1.49 2003/08/14 01:16:30 ggregory Exp $ + * @version $Id: StringUtilsTest.java,v 1.50 2003/08/17 22:56:11 scolebourne Exp $ */ public class StringUtilsTest extends TestCase { @@ -162,11 +162,8 @@ public class StringUtilsTest extends TestCase { public void testCaseFunctions() { assertEquals(null, StringUtils.upperCase(null)); assertEquals(null, StringUtils.lowerCase(null)); - assertEquals(null, StringUtils.swapCase(null)); assertEquals(null, StringUtils.capitalize(null)); assertEquals(null, StringUtils.uncapitalize(null)); - assertEquals(null, StringUtils.capitalizeAllWords(null)); - assertEquals(null, StringUtils.uncapitalizeAllWords(null)); assertEquals("capitalize(String) failed", FOO_CAP, StringUtils.capitalize(FOO_UNCAP) ); @@ -174,36 +171,20 @@ public class StringUtilsTest extends TestCase { "", StringUtils.capitalize("") ); assertEquals("capitalize(single-char-string) failed", "X", StringUtils.capitalize("x") ); - assertEquals("capitalizeAllWords(String) failed", - "Foo Bar Baz", StringUtils.capitalizeAllWords(SENTENCE_UNCAP) ); - assertEquals("capitalizeAllWords(empty-string) failed", - "", StringUtils.capitalizeAllWords("") ); assertEquals("uncapitalize(String) failed", FOO_UNCAP, StringUtils.uncapitalize(FOO_CAP) ); assertEquals("uncapitalize(empty-string) failed", "", StringUtils.uncapitalize("") ); assertEquals("uncapitalize(single-char-string) failed", "x", StringUtils.uncapitalize("X") ); - assertEquals("uncapitalizeAllWords(String) failed", - SENTENCE_UNCAP, StringUtils.uncapitalizeAllWords("Foo Bar Baz") ); - assertEquals("uncapitalizeAllWords(empty-string) failed", - "", StringUtils.uncapitalizeAllWords("") ); // reflection type of tests: Sentences. - assertEquals("uncapitalizeAllWords(capitalizeAllWords(String)) failed", - SENTENCE_UNCAP, StringUtils.uncapitalizeAllWords(StringUtils.capitalizeAllWords(SENTENCE_UNCAP)) ); - assertEquals("capitalizeAllWords(uncapitalizeAllWords(String)) failed", - SENTENCE_CAP, StringUtils.capitalizeAllWords(StringUtils.uncapitalizeAllWords(SENTENCE_CAP)) ); assertEquals("uncapitalize(capitalize(String)) failed", SENTENCE_UNCAP, StringUtils.uncapitalize(StringUtils.capitalize(SENTENCE_UNCAP)) ); assertEquals("capitalize(uncapitalize(String)) failed", SENTENCE_CAP, StringUtils.capitalize(StringUtils.uncapitalize(SENTENCE_CAP)) ); // reflection type of tests: One word. - assertEquals("uncapitalizeAllWords(capitalizeAllWords(String)) failed", - FOO_UNCAP, StringUtils.uncapitalizeAllWords(StringUtils.capitalizeAllWords(FOO_UNCAP)) ); - assertEquals("capitalizeAllWords(uncapitalizeAllWords(String)) failed", - FOO_CAP, StringUtils.capitalizeAllWords(StringUtils.uncapitalizeAllWords(FOO_CAP)) ); assertEquals("uncapitalize(capitalize(String)) failed", FOO_UNCAP, StringUtils.uncapitalize(StringUtils.capitalize(FOO_UNCAP)) ); assertEquals("capitalize(uncapitalize(String)) failed", @@ -217,15 +198,22 @@ public class StringUtilsTest extends TestCase { "foo test thing", StringUtils.lowerCase("fOo test THING") ); assertEquals("lowerCase(empty-string) failed", "", StringUtils.lowerCase("") ); - - assertEquals("swapCase(empty-string) failed", - "", StringUtils.swapCase("") ); - assertEquals("swapCase(String-with-numbers) failed", - "a123RgYu", StringUtils.swapCase("A123rGyU") ); - assertEquals("swapCase(String) failed", - "Hello aPACHE", StringUtils.swapCase("hELLO Apache") ); } + public void testSwapCase_String() { + assertEquals(null, StringUtils.swapCase(null)); + assertEquals("", StringUtils.swapCase("")); + assertEquals(" ", StringUtils.swapCase(" ")); + + assertEquals("i", WordUtils.swapCase("I") ); + assertEquals("I", WordUtils.swapCase("i") ); + assertEquals("I AM HERE 123", StringUtils.swapCase("i am here 123") ); + assertEquals("i aM hERE 123", StringUtils.swapCase("I Am Here 123") ); + assertEquals("I AM here 123", StringUtils.swapCase("i am HERE 123") ); + assertEquals("i am here 123", StringUtils.swapCase("I AM HERE 123") ); + } + + //----------------------------------------------------------------------- public void testJoin_Objectarray() { assertEquals(null, StringUtils.join(null)); assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST)); diff --git a/src/test/org/apache/commons/lang/WordUtilsTest.java b/src/test/org/apache/commons/lang/WordUtilsTest.java index ee5ebef90..a46de4b38 100644 --- a/src/test/org/apache/commons/lang/WordUtilsTest.java +++ b/src/test/org/apache/commons/lang/WordUtilsTest.java @@ -66,7 +66,7 @@ import junit.framework.TestSuite; * @author Ringo De Smet * @author Henri Yandell * @author Stephen Colebourne - * @version $Id: WordUtilsTest.java,v 1.1 2003/08/17 21:57:37 scolebourne Exp $ + * @version $Id: WordUtilsTest.java,v 1.2 2003/08/17 22:56:11 scolebourne Exp $ */ public class WordUtilsTest extends TestCase { @@ -195,4 +195,45 @@ public class WordUtilsTest extends TestCase { // System.err.println(expected); // System.err.println(WordUtils.wrap(input, 20, "\n", false)); } + + //----------------------------------------------------------------------- + public void testCapitalize_String() { + assertEquals(null, WordUtils.capitalize(null)); + assertEquals("", WordUtils.capitalize("")); + assertEquals(" ", WordUtils.capitalize(" ")); + + assertEquals("I", WordUtils.capitalize("I") ); + assertEquals("I", WordUtils.capitalize("i") ); + assertEquals("I Am Here 123", WordUtils.capitalize("i am here 123") ); + assertEquals("I Am Here 123", WordUtils.capitalize("I Am Here 123") ); + assertEquals("I Am HERE 123", WordUtils.capitalize("i am HERE 123") ); + assertEquals("I AM HERE 123", WordUtils.capitalize("I AM HERE 123") ); + } + + public void testUncapitalize_String() { + assertEquals(null, WordUtils.uncapitalize(null)); + assertEquals("", WordUtils.uncapitalize("")); + assertEquals(" ", WordUtils.uncapitalize(" ")); + + assertEquals("i", WordUtils.uncapitalize("I") ); + assertEquals("i", WordUtils.uncapitalize("i") ); + assertEquals("i am here 123", WordUtils.uncapitalize("i am here 123") ); + assertEquals("i am here 123", WordUtils.uncapitalize("I Am Here 123") ); + assertEquals("i am hERE 123", WordUtils.uncapitalize("i am HERE 123") ); + assertEquals("i aM hERE 123", WordUtils.uncapitalize("I AM HERE 123") ); + } + + public void testSwapCase_String() { + assertEquals(null, WordUtils.swapCase(null)); + assertEquals("", WordUtils.swapCase("")); + assertEquals(" ", WordUtils.swapCase(" ")); + + assertEquals("i", WordUtils.swapCase("I") ); + assertEquals("I", WordUtils.swapCase("i") ); + assertEquals("I AM HERE 123", WordUtils.swapCase("i am here 123") ); + assertEquals("i aM hERE 123", WordUtils.swapCase("I Am Here 123") ); + assertEquals("I AM here 123", WordUtils.swapCase("i am HERE 123") ); + assertEquals("i am here 123", WordUtils.swapCase("I AM HERE 123") ); + } + }