LANG-1206: Improve CharSetUtils.squeeze() performance (closes #147)

patch by Mohammed Alfallaj
This commit is contained in:
pascalschumacher 2016-05-20 20:04:30 +02:00
parent f26f04dc6e
commit afedbae8ac
2 changed files with 19 additions and 5 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.5" date="tba" description="tba">
<action issue="LANG-1206" type="update" dev="pschumacher" due-to="Mohammed Alfallaj">Improve CharSetUtils.squeeze() performance</action>
<action issue="LANG-1225" type="add" dev="pschumacher" due-to="Caleb Cushing">Add RandomStringUtils#randomGraph and #randomPrint which match corresponding regular expression class</action>
<action issue="LANG-901" type="fix" dev="pschumacher" due-to="Matthew Bartenschlag">StringUtils#startsWithAny/endsWithAny is case sensitive - documented as case insensitive</action>
<action issue="LANG-1223" type="add" dev="pschumacher" due-to="Nick Manley">Add StopWatch#getTime(TimeUnit)</action>

View File

@ -68,13 +68,26 @@ public class CharSetUtils {
final StringBuilder buffer = new StringBuilder(str.length());
final char[] chrs = str.toCharArray();
final int sz = chrs.length;
char lastChar = ' ';
char lastChar = chrs[0];
char ch = ' ';
for (int i = 0; i < sz; i++) {
Character inChars = null;
Character notInChars = null;
buffer.append(lastChar);
for (int i = 1; i < sz; i++) {
ch = chrs[i];
// Compare with contains() last for performance.
if (ch == lastChar && i != 0 && chars.contains(ch)) {
continue;
if (ch == lastChar) {
if ((inChars != null) && (ch == inChars)) {
continue;
} else {
if ((notInChars == null) || (ch != notInChars)) {
if (chars.contains(ch)) {
inChars = ch;
continue;
} else {
notInChars = ch;
}
}
}
}
buffer.append(ch);
lastChar = ch;