Rolling capitalize and uncapitalize back from (CharSequence) to (String)

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1088505 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2011-04-04 08:42:38 +00:00
parent 9946140913
commit 325648a185
2 changed files with 15 additions and 35 deletions

View File

@ -5004,29 +5004,25 @@ public class StringUtils {
* StringUtils.capitalize("cAt") = "CAt" * StringUtils.capitalize("cAt") = "CAt"
* </pre> * </pre>
* *
* @param cs the String to capitalize, may be null * @param str the String to capitalize, may be null
* @return the capitalized String, {@code null} if null String input * @return the capitalized String, {@code null} if null String input
* @see org.apache.commons.lang3.text.WordUtils#capitalize(String) * @see org.apache.commons.lang3.text.WordUtils#capitalize(String)
* @see #uncapitalize(CharSequence) * @see #uncapitalize(String)
* @since 2.0 * @since 2.0
* @since 3.0 Changed signature from capitalize(String) to capitalize(CharSequence)
*/ */
public static String capitalize(CharSequence cs) { public static String capitalize(String str) {
if (cs == null ) {
return null;
}
int strLen; int strLen;
if ((strLen = cs.length()) == 0) { if (str == null || (strLen = str.length()) == 0) {
return cs.toString(); return str;
} }
return new StringBuilder(strLen) return new StringBuilder(strLen)
.append(Character.toTitleCase(cs.charAt(0))) .append(Character.toTitleCase(str.charAt(0)))
.append(StringUtils.subSequence(cs, 1)) .append(str.substring(1))
.toString(); .toString();
} }
/** /**
* <p>Uncapitalizes a CharSequence changing the first letter to title case as * <p>Uncapitalizes a String changing the first letter to title case as
* per {@link Character#toLowerCase(char)}. No other letters are changed.</p> * per {@link Character#toLowerCase(char)}. No other letters are changed.</p>
* *
* <p>For a word based algorithm, see {@link org.apache.commons.lang3.text.WordUtils#uncapitalize(String)}. * <p>For a word based algorithm, see {@link org.apache.commons.lang3.text.WordUtils#uncapitalize(String)}.
@ -5039,24 +5035,20 @@ public class StringUtils {
* StringUtils.uncapitalize("CAT") = "cAT" * StringUtils.uncapitalize("CAT") = "cAT"
* </pre> * </pre>
* *
* @param cs the String to uncapitalize, may be null * @param str the String to uncapitalize, may be null
* @return the uncapitalized String, {@code null} if null String input * @return the uncapitalized String, {@code null} if null String input
* @see org.apache.commons.lang3.text.WordUtils#uncapitalize(String) * @see org.apache.commons.lang3.text.WordUtils#uncapitalize(String)
* @see #capitalize(CharSequence) * @see #capitalize(String)
* @since 2.0 * @since 2.0
* @since 3.0 Changed signature from uncapitalize(String) to uncapitalize(CharSequence)
*/ */
public static String uncapitalize(CharSequence cs) { public static String uncapitalize(String str) {
if (cs == null ) {
return null;
}
int strLen; int strLen;
if ((strLen = cs.length()) == 0) { if (str == null || (strLen = str.length()) == 0) {
return cs.toString(); return str;
} }
return new StringBuilder(strLen) return new StringBuilder(strLen)
.append(Character.toLowerCase(cs.charAt(0))) .append(Character.toLowerCase(str.charAt(0)))
.append(StringUtils.subSequence(cs, 1)) .append(str.substring(1))
.toString(); .toString();
} }

View File

@ -119,12 +119,6 @@ public class StringUtilsTest extends TestCase {
"", StringUtils.capitalize("") ); "", StringUtils.capitalize("") );
assertEquals("capitalize(single-char-string) failed", assertEquals("capitalize(single-char-string) failed",
"X", StringUtils.capitalize("x") ); "X", StringUtils.capitalize("x") );
assertEquals("capitalize(single-char-string) failed",
"X", StringUtils.capitalize(new StringBuilder("x")) );
assertEquals("capitalize(single-char-string) failed",
"X", StringUtils.capitalize(new StringBuffer("x")) );
assertEquals("capitalize(single-char-string) failed",
"X", StringUtils.capitalize(CharBuffer.wrap("x")) );
assertEquals("uncapitalize(String) failed", assertEquals("uncapitalize(String) failed",
FOO_UNCAP, StringUtils.uncapitalize(FOO_CAP) ); FOO_UNCAP, StringUtils.uncapitalize(FOO_CAP) );
@ -132,12 +126,6 @@ public class StringUtilsTest extends TestCase {
"", StringUtils.uncapitalize("") ); "", StringUtils.uncapitalize("") );
assertEquals("uncapitalize(single-char-string) failed", assertEquals("uncapitalize(single-char-string) failed",
"x", StringUtils.uncapitalize("X") ); "x", StringUtils.uncapitalize("X") );
assertEquals("uncapitalize(single-char-string) failed",
"x", StringUtils.uncapitalize(new StringBuilder("X")) );
assertEquals("uncapitalize(single-char-string) failed",
"x", StringUtils.uncapitalize(new StringBuffer("X")) );
assertEquals("uncapitalize(single-char-string) failed",
"x", StringUtils.uncapitalize(CharBuffer.wrap("X")) );
// reflection type of tests: Sentences. // reflection type of tests: Sentences.
assertEquals("uncapitalize(capitalize(String)) failed", assertEquals("uncapitalize(capitalize(String)) failed",