Skip to content

Commit 493f75e

Browse files
committed
Optimize whitespace checks in StringUtils (as far as possible on JDK 8)
Closes gh-31067
1 parent df066d8 commit 493f75e

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,26 +247,26 @@ public static String trimWhitespace(String str) {
247247
/**
248248
* Trim <em>all</em> whitespace from the given {@code CharSequence}:
249249
* leading, trailing, and in between characters.
250-
* @param text the {@code CharSequence} to check
250+
* @param str the {@code CharSequence} to check
251251
* @return the trimmed {@code CharSequence}
252252
* @since 5.3.22
253253
* @see #trimAllWhitespace(String)
254254
* @see java.lang.Character#isWhitespace
255255
*/
256-
public static CharSequence trimAllWhitespace(CharSequence text) {
257-
if (!hasLength(text)) {
258-
return text;
256+
public static CharSequence trimAllWhitespace(CharSequence str) {
257+
if (!hasLength(str)) {
258+
return str;
259259
}
260260

261-
int len = text.length();
262-
StringBuilder sb = new StringBuilder(text.length());
261+
int len = str.length();
262+
StringBuilder sb = new StringBuilder(str.length());
263263
for (int i = 0; i < len; i++) {
264-
char c = text.charAt(i);
264+
char c = str.charAt(i);
265265
if (!Character.isWhitespace(c)) {
266266
sb.append(c);
267267
}
268268
}
269-
return sb.toString();
269+
return sb;
270270
}
271271

272272
/**
@@ -278,9 +278,10 @@ public static CharSequence trimAllWhitespace(CharSequence text) {
278278
* @see java.lang.Character#isWhitespace
279279
*/
280280
public static String trimAllWhitespace(String str) {
281-
if (str == null) {
282-
return null;
281+
if (!hasLength(str)) {
282+
return str;
283283
}
284+
284285
return trimAllWhitespace((CharSequence) str).toString();
285286
}
286287

0 commit comments

Comments
 (0)