[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-714] StringUtils doc/comment spelling fixes.
[LANG-715] CharSetUtils.squeeze() speedup.
[LANG-716] swapCase and *capitalize speedups.

View File

@ -5094,25 +5094,23 @@ public class StringUtils {
* @return the changed String, {@code null} if null String input
*/
public static String swapCase(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
if (str == null || str.length() == 0) {
return str;
}
StringBuilder buffer = new StringBuilder(strLen);
char ch = 0;
for (int i = 0; i < strLen; i++) {
ch = str.charAt(i);
char[] buffer = str.toCharArray();
for (int i = 0; i < buffer.length; i++) {
char ch = buffer[i];
if (Character.isUpperCase(ch)) {
ch = Character.toLowerCase(ch);
buffer[i] = Character.toLowerCase(ch);
} else if (Character.isTitleCase(ch)) {
ch = Character.toLowerCase(ch);
buffer[i] = Character.toLowerCase(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

View File

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