From 325648a1859a5aa31007ec6990a7ac8dceba33c8 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Mon, 4 Apr 2011 08:42:38 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/lang3/StringUtils.java | 38 ++++++++----------- .../apache/commons/lang3/StringUtilsTest.java | 12 ------ 2 files changed, 15 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 263b9b318..156f1ed4c 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -5004,29 +5004,25 @@ public class StringUtils { * StringUtils.capitalize("cAt") = "CAt" * * - * @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 * @see org.apache.commons.lang3.text.WordUtils#capitalize(String) - * @see #uncapitalize(CharSequence) + * @see #uncapitalize(String) * @since 2.0 - * @since 3.0 Changed signature from capitalize(String) to capitalize(CharSequence) */ - public static String capitalize(CharSequence cs) { - if (cs == null ) { - return null; - } + public static String capitalize(String str) { int strLen; - if ((strLen = cs.length()) == 0) { - return cs.toString(); + if (str == null || (strLen = str.length()) == 0) { + return str; } return new StringBuilder(strLen) - .append(Character.toTitleCase(cs.charAt(0))) - .append(StringUtils.subSequence(cs, 1)) + .append(Character.toTitleCase(str.charAt(0))) + .append(str.substring(1)) .toString(); } /** - *

Uncapitalizes a CharSequence changing the first letter to title case as + *

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

* *

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" * * - * @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 * @see org.apache.commons.lang3.text.WordUtils#uncapitalize(String) - * @see #capitalize(CharSequence) + * @see #capitalize(String) * @since 2.0 - * @since 3.0 Changed signature from uncapitalize(String) to uncapitalize(CharSequence) */ - public static String uncapitalize(CharSequence cs) { - if (cs == null ) { - return null; - } + public static String uncapitalize(String str) { int strLen; - if ((strLen = cs.length()) == 0) { - return cs.toString(); + if (str == null || (strLen = str.length()) == 0) { + return str; } return new StringBuilder(strLen) - .append(Character.toLowerCase(cs.charAt(0))) - .append(StringUtils.subSequence(cs, 1)) + .append(Character.toLowerCase(str.charAt(0))) + .append(str.substring(1)) .toString(); } diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index be814c0e0..cd1fdcb00 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -119,12 +119,6 @@ public class StringUtilsTest extends TestCase { "", StringUtils.capitalize("") ); assertEquals("capitalize(single-char-string) failed", "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", FOO_UNCAP, StringUtils.uncapitalize(FOO_CAP) ); @@ -132,12 +126,6 @@ public class StringUtilsTest extends TestCase { "", StringUtils.uncapitalize("") ); assertEquals("uncapitalize(single-char-string) failed", "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. assertEquals("uncapitalize(capitalize(String)) failed",