Move capitalizeAllWords, uncapitalizeAllWords, swapCase to WordUtils

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137620 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-08-17 22:59:28 +00:00
parent f253575ee7
commit d1d8827200
5 changed files with 221 additions and 138 deletions

View File

@ -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);

View File

@ -145,7 +145,7 @@ import java.util.List;
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @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 {
* <p>Capitalizes a String changing the first letter to title case as
* per {@link Character#toTitleCase(char)}. No other letters are changed.</p>
*
* <p>A <code>null</code> input String returns <code>null</code>.</p>
* <p>For a word based alorithm, see {@link WordUtils#capitalize(String)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.capitalize(null) = null
@ -3425,7 +3426,7 @@ public class StringUtils {
*
* @param str the String to capitalize, may be null
* @return the capitalized String, <code>null</code> 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 {
* <p>Uncapitalizes a String changing the first letter to title case as
* per {@link Character#toLowerCase(char)}. No other letters are changed.</p>
*
* <p>A <code>null</code> input String returns <code>null</code>.</p>
* <p>For a word based alorithm, see {@link WordUtils#uncapitalize(String)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.uncapitalize(null) = null
@ -3467,7 +3469,7 @@ public class StringUtils {
*
* @param str the String to uncapitalize, may be null
* @return the uncapitalized String, <code>null</code> 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 {
}
/**
* <p>Swaps the case of a String using a word based algorithm.</p>
* <p>Swaps the case of a String changing upper and title case to
* lower case, and lower case to upper case.</p>
*
* <ul>
* <li>Upper case character converts to Lower case</li>
* <li>Title case character converts to Lower case</li>
* <li>Lower case character after Whitespace or at start converts to Title case</li>
* <li>Other Lower case character converts to Upper case</li>
* <li>Lower case character converts to Upper case</li>
* </ul>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
* <p>For a word based alorithm, see {@link WordUtils#swapCase(String)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
@ -3513,6 +3515,11 @@ public class StringUtils {
* StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
* </pre>
*
* <p>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.</p>
*
* @param str the String to swap case, may be null
* @return the changed String, <code>null</code> 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);
ch = Character.toUpperCase(ch);
}
} else {
tmp = ch;
}
buffer.append(tmp);
whitespace = Character.isWhitespace(ch);
}
return buffer.toString();
}
/**
* <p>Capitalizes all the whitespace separated words in a String.
* Only the first letter of each word is changed.</p>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.capitalizeAllWords(null) = null
* StringUtils.capitalizeAllWords("") = ""
* StringUtils.capitalizeAllWords("i am FINE") = "I Am FINE"
* </pre>
*
* @param str the String to capitalize, may be null
* @return capitalized String, <code>null</code> 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);
}
}
return buffer.toString();
}
@ -3597,51 +3554,11 @@ public class StringUtils {
*
* @param str the String to capitalize, may be null
* @return capitalized String, <code>null</code> 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);
}
/**
* <p>Uncapitalizes all the whitespace separated words in a String.
* Only the first letter of each word is changed.</p>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.uncapitalizeAllWords(null) = null
* StringUtils.uncapitalizeAllWords("") = ""
* StringUtils.uncapitalizeAllWords("I Am FINE") = "i am fINE"
* </pre>
*
* @param str the String to uncapitalize, may be null
* @return uncapitalized String, <code>null</code> 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

View File

@ -66,7 +66,7 @@ package org.apache.commons.lang;
* @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @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
//-----------------------------------------------------------------------
/**
* <p>Wraps a single line of text, identifying words by <code>' '</code>.</p>
*
@ -250,4 +252,140 @@ public class WordUtils {
return wrappedLine.toString();
}
// Capitalizing
//-----------------------------------------------------------------------
/**
* <p>Capitalizes all the whitespace separated words in a String.
* Only the first letter of each word is changed.</p>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
* A <code>null</code> input String returns <code>null</code>.
* Capitalization uses the unicode title case, normally equivalent to
* upper case.</p>
*
* <pre>
* WordUtils.capitalize(null) = null
* WordUtils.capitalize("") = ""
* WordUtils.capitalize("i am FINE") = "I Am FINE"
* </pre>
*
* @param str the String to capitalize, may be null
* @return capitalized String, <code>null</code> 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();
}
/**
* <p>Uncapitalizes all the whitespace separated words in a String.
* Only the first letter of each word is changed.</p>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* WordUtils.uncapitalize(null) = null
* WordUtils.uncapitalize("") = ""
* WordUtils.uncapitalize("I Am FINE") = "i am fINE"
* </pre>
*
* @param str the String to uncapitalize, may be null
* @return uncapitalized String, <code>null</code> 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();
}
/**
* <p>Swaps the case of a String using a word based algorithm.</p>
*
* <ul>
* <li>Upper case character converts to Lower case</li>
* <li>Title case character converts to Lower case</li>
* <li>Lower case character after Whitespace or at start converts to Title case</li>
* <li>Other Lower case character converts to Upper case</li>
* </ul>
*
* <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
* A <code>null</code> input String returns <code>null</code>.</p>
*
* <pre>
* StringUtils.swapCase(null) = null
* StringUtils.swapCase("") = ""
* StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
* </pre>
*
* @param str the String to swap case, may be null
* @return the changed String, <code>null</code> 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();
}
}

View File

@ -75,7 +75,7 @@ import junit.textui.TestRunner;
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author Phil Steitz
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @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));

View File

@ -66,7 +66,7 @@ import junit.framework.TestSuite;
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @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") );
}
}