[LANG-716] swapCase and *capitalize speedups.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1144921 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2011-07-10 18:01:56 +00:00
parent 0502919490
commit fa4f5d8019
4 changed files with 35 additions and 47 deletions

View File

@ -151,3 +151,4 @@ BUG FIXES IN 3.0
[LANG-710] StringIndexOutOfBoundsException when calling unescapeHtml4("&#03") [LANG-710] StringIndexOutOfBoundsException when calling unescapeHtml4("&#03")
[LANG-714] StringUtils doc/comment spelling fixes. [LANG-714] StringUtils doc/comment spelling fixes.
[LANG-715] CharSetUtils.squeeze() speedup. [LANG-715] CharSetUtils.squeeze() speedup.
[LANG-716] swapCase and *capitalize speedups.

View File

@ -5094,25 +5094,23 @@ public static String uncapitalize(String str) {
* @return the changed String, {@code null} if null String input * @return the changed String, {@code null} if null String input
*/ */
public static String swapCase(String str) { public static String swapCase(String str) {
int strLen; if (str == null || str.length() == 0) {
if (str == null || (strLen = str.length()) == 0) {
return str; return str;
} }
StringBuilder buffer = new StringBuilder(strLen);
char ch = 0; char[] buffer = str.toCharArray();
for (int i = 0; i < strLen; i++) {
ch = str.charAt(i); for (int i = 0; i < buffer.length; i++) {
char ch = buffer[i];
if (Character.isUpperCase(ch)) { if (Character.isUpperCase(ch)) {
ch = Character.toLowerCase(ch); buffer[i] = Character.toLowerCase(ch);
} else if (Character.isTitleCase(ch)) { } else if (Character.isTitleCase(ch)) {
ch = Character.toLowerCase(ch); buffer[i] = Character.toLowerCase(ch);
} else if (Character.isLowerCase(ch)) { } else if (Character.isLowerCase(ch)) {
ch = Character.toUpperCase(ch); buffer[i] = Character.toUpperCase(ch);
} }
buffer.append(ch);
} }
return buffer.toString(); return new String(buffer);
} }
// Count matches // Count matches

View File

@ -201,23 +201,18 @@ public static String capitalize(String str, char... delimiters) {
if (str == null || str.length() == 0 || delimLen == 0) { if (str == null || str.length() == 0 || delimLen == 0) {
return str; return str;
} }
int strLen = str.length(); char[] buffer = str.toCharArray();
StringBuilder buffer = new StringBuilder(strLen);
boolean capitalizeNext = true; boolean capitalizeNext = true;
for (int i = 0; i < strLen; i++) { for (int i = 0; i < buffer.length; i++) {
char ch = str.charAt(i); char ch = buffer[i];
if (isDelimiter(ch, delimiters)) { if (isDelimiter(ch, delimiters)) {
buffer.append(ch);
capitalizeNext = true; capitalizeNext = true;
} else if (capitalizeNext) { } else if (capitalizeNext) {
buffer.append(Character.toTitleCase(ch)); buffer[i] = Character.toTitleCase(ch);
capitalizeNext = false; capitalizeNext = false;
} else {
buffer.append(ch);
} }
} }
return buffer.toString(); return new String(buffer);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -331,23 +326,18 @@ public static String uncapitalize(String str, char... delimiters) {
if (str == null || str.length() == 0 || delimLen == 0) { if (str == null || str.length() == 0 || delimLen == 0) {
return str; return str;
} }
int strLen = str.length(); char[] buffer = str.toCharArray();
StringBuilder buffer = new StringBuilder(strLen);
boolean uncapitalizeNext = true; boolean uncapitalizeNext = true;
for (int i = 0; i < strLen; i++) { for (int i = 0; i < buffer.length; i++) {
char ch = str.charAt(i); char ch = buffer[i];
if (isDelimiter(ch, delimiters)) { if (isDelimiter(ch, delimiters)) {
buffer.append(ch);
uncapitalizeNext = true; uncapitalizeNext = true;
} else if (uncapitalizeNext) { } else if (uncapitalizeNext) {
buffer.append(Character.toLowerCase(ch)); buffer[i] = Character.toLowerCase(ch);
uncapitalizeNext = false; uncapitalizeNext = false;
} else {
buffer.append(ch);
} }
} }
return buffer.toString(); return new String(buffer);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -374,35 +364,33 @@ public static String uncapitalize(String str, char... delimiters) {
* @return the changed String, <code>null</code> if null String input * @return the changed String, <code>null</code> if null String input
*/ */
public static String swapCase(String str) { public static String swapCase(String str) {
int strLen; if (str == null || str.length() == 0) {
if (str == null || (strLen = str.length()) == 0) {
return str; return str;
} }
StringBuilder buffer = new StringBuilder(strLen); char[] buffer = str.toCharArray();
boolean whitespace = true; boolean whitespace = true;
char ch = 0;
char tmp = 0;
for (int i = 0; i < strLen; i++) { for (int i = 0; i < buffer.length; i++) {
ch = str.charAt(i); char ch = buffer[i];
if (Character.isUpperCase(ch)) { if (Character.isUpperCase(ch)) {
tmp = Character.toLowerCase(ch); buffer[i] = Character.toLowerCase(ch);
whitespace = false;
} else if (Character.isTitleCase(ch)) { } else if (Character.isTitleCase(ch)) {
tmp = Character.toLowerCase(ch); buffer[i] = Character.toLowerCase(ch);
whitespace = false;
} else if (Character.isLowerCase(ch)) { } else if (Character.isLowerCase(ch)) {
if (whitespace) { if (whitespace) {
tmp = Character.toTitleCase(ch); buffer[i] = Character.toTitleCase(ch);
whitespace = false;
} else { } else {
tmp = Character.toUpperCase(ch); buffer[i] = Character.toUpperCase(ch);
} }
} else { } else {
tmp = ch; whitespace = Character.isWhitespace(ch);
} }
buffer.append(tmp);
whitespace = Character.isWhitespace(ch);
} }
return buffer.toString(); return new String(buffer);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -22,9 +22,10 @@
<body> <body>
<release version="3.0" date="Unreleased" description="Backwards incompatible update of Commons Lang to Java 5"> <release version="3.0" date="Unreleased" description="Backwards incompatible update of Commons Lang to Java 5">
<action type="update" issue="LANG-713">Increase test coverage of FieldUtils read methods and tweak javadoc</action> <action type="fix" issue="LANG-716">swapCase and *capitalize speedups.</action>
<action type="fix" issue="LANG-715">CharSetUtils.squeeze() speedup.</action> <action type="fix" issue="LANG-715">CharSetUtils.squeeze() speedup.</action>
<action type="fix" issue="LANG-714">StringUtils doc/comment spelling fixes.</action> <action type="fix" issue="LANG-714">StringUtils doc/comment spelling fixes.</action>
<action type="update" issue="LANG-713">Increase test coverage of FieldUtils read methods and tweak javadoc</action>
<action type="fix" issue="LANG-711">Add includeantruntime=false to javac targets to quell warnings in ant 1.8.1 and better (and modest performance gain).</action> <action type="fix" issue="LANG-711">Add includeantruntime=false to javac targets to quell warnings in ant 1.8.1 and better (and modest performance gain).</action>
<action type="fix" issue="LANG-710">StringIndexOutOfBoundsException when calling unescapeHtml4("&amp;#03")</action> <action type="fix" issue="LANG-710">StringIndexOutOfBoundsException when calling unescapeHtml4("&amp;#03")</action>
<action type="fix" issue="LANG-703">StringUtils.join throws NPE when toString returns null for one of objects in collection</action> <action type="fix" issue="LANG-703">StringUtils.join throws NPE when toString returns null for one of objects in collection</action>