Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of StringUtils.deleteAny() #24870

Merged
merged 1 commit into from Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -453,17 +453,17 @@ public static String deleteAny(String inString, @Nullable String charsToDelete)
return inString;
}

StringBuilder sb = new StringBuilder(inString.length());
int lastCharIndex = 0;
char[] result = new char[inString.length()];
for (int i = 0; i < inString.length(); i++) {
char c = inString.charAt(i);
if (charsToDelete.indexOf(c) == -1) {
sb.append(c);
result[lastCharIndex++] = c;
}
}
return sb.toString();
return new String(result, 0, lastCharIndex);
}


//---------------------------------------------------------------------
// Convenience methods for working with formatted Strings
//---------------------------------------------------------------------
Expand Down
Expand Up @@ -306,7 +306,7 @@ void quote() {
void quoteIfString() {
assertThat(StringUtils.quoteIfString("myString")).isEqualTo("'myString'");
assertThat(StringUtils.quoteIfString("")).isEqualTo("''");
assertThat(StringUtils.quoteIfString(5)).isEqualTo(Integer.valueOf(5));
assertThat(StringUtils.quoteIfString(5)).isEqualTo(5);
assertThat(StringUtils.quoteIfString(null)).isNull();
}

Expand Down Expand Up @@ -498,7 +498,7 @@ void tokenizeToStringArray() {
void tokenizeToStringArrayWithNotIgnoreEmptyTokens() {
String[] sa = StringUtils.tokenizeToStringArray("a,b , ,c", ",", true, false);
assertThat(sa.length).isEqualTo(4);
assertThat(sa[0].equals("a") && sa[1].equals("b") && sa[2].equals("") && sa[3].equals("c")).as("components are correct").isTrue();
assertThat(sa[0].equals("a") && sa[1].equals("b") && sa[2].isEmpty() && sa[3].equals("c")).as("components are correct").isTrue();
}

@Test
Expand Down Expand Up @@ -539,7 +539,7 @@ void delimitedListToStringArrayWithSemicolon() {
}

@Test
void delimitedListToStringArrayWithEmptyString() {
void delimitedListToStringArrayWithEmptyDelimiter() {
String[] sa = StringUtils.delimitedListToStringArray("a,b", "");
assertThat(sa.length).isEqualTo(3);
assertThat(sa[0]).isEqualTo("a");
Expand Down Expand Up @@ -601,7 +601,7 @@ void commaDelimitedListToStringArrayEmptyStrings() {
// Could read these from files
String[] sa = StringUtils.commaDelimitedListToStringArray("a,,b");
assertThat(sa.length).as("a,,b produces array length 3").isEqualTo(3);
assertThat(sa[0].equals("a") && sa[1].equals("") && sa[2].equals("b")).as("components are correct").isTrue();
assertThat(sa[0].equals("a") && sa[1].isEmpty() && sa[2].equals("b")).as("components are correct").isTrue();

sa = new String[] {"", "", "a", ""};
doTestCommaDelimitedListToStringArrayLegalMatch(sa);
Expand Down