Use StringBuilder.append(char) where possible

To slightly improve performance, this commit switches to
StringBuilder.append(char) instead of StringBuilder.append(String)
whenever we append a single character to a StringBuilder.

Closes gh-27098
This commit is contained in:
Sam Brannen
2021-06-24 18:53:51 +02:00
parent ddbb7c1b5b
commit a2ef6badc4
74 changed files with 232 additions and 220 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -111,7 +111,7 @@ public class HttpRangeTests {
// 1. At limit..
StringBuilder atLimit = new StringBuilder("bytes=0-0");
for (int i=0; i < 99; i++) {
atLimit.append(",").append(i).append("-").append(i + 1);
atLimit.append(',').append(i).append('-').append(i + 1);
}
List<HttpRange> ranges = HttpRange.parseRanges(atLimit.toString());
assertThat(ranges.size()).isEqualTo(100);
@@ -119,7 +119,7 @@ public class HttpRangeTests {
// 2. Above limit..
StringBuilder aboveLimit = new StringBuilder("bytes=0-0");
for (int i=0; i < 100; i++) {
aboveLimit.append(",").append(i).append("-").append(i + 1);
aboveLimit.append(',').append(i).append('-').append(i + 1);
}
assertThatIllegalArgumentException().isThrownBy(() ->
HttpRange.parseRanges(aboveLimit.toString()));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.multipart.support;
import java.util.HashMap;
@@ -80,7 +81,7 @@ public class DefaultMultipartHttpServletRequestTests {
for (String key : this.queryParams.keySet()) {
for (String value : this.queryParams.get(key)) {
this.servletRequest.addParameter(key, value);
query.append(query.length() > 0 ? "&" : "").append(key).append("=").append(value);
query.append(query.length() > 0 ? "&" : "").append(key).append('=').append(value);
}
}
this.servletRequest.setQueryString(query.toString());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2004-2019 the original author or authors.
* Copyright 2004-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,15 +33,15 @@ public class JavaScriptUtilsTests {
public void escape() {
StringBuilder sb = new StringBuilder();
sb.append('"');
sb.append("'");
sb.append("\\");
sb.append("/");
sb.append("\t");
sb.append("\n");
sb.append("\r");
sb.append("\f");
sb.append("\b");
sb.append("\013");
sb.append('\'');
sb.append('\\');
sb.append('/');
sb.append('\t');
sb.append('\n');
sb.append('\r');
sb.append('\f');
sb.append('\b');
sb.append('\013');
assertThat(JavaScriptUtils.javaScriptEscape(sb.toString())).isEqualTo("\\\"\\'\\\\\\/\\t\\n\\n\\f\\b\\v");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -1210,7 +1210,7 @@ public class PathPatternTests {
private String elementsToString(List<Element> elements) {
StringBuilder s = new StringBuilder();
for (Element element: elements) {
s.append("[").append(element.value()).append("]");
s.append('[').append(element.value()).append(']');
}
return s.toString();
}