Merge branch '5.3.x'

This commit is contained in:
Sam Brannen
2022-07-04 19:31:47 +02:00
8 changed files with 84 additions and 29 deletions

View File

@@ -232,21 +232,23 @@ public abstract class StringUtils {
}
/**
* Trim <i>all</i> whitespace from the given {@code String}:
* Trim <em>all</em> whitespace from the given {@code CharSequence}:
* leading, trailing, and in between characters.
* @param str the {@code String} to check
* @return the trimmed {@code String}
* @param text the {@code CharSequence} to check
* @return the trimmed {@code CharSequence}
* @since 5.3.22
* @see #trimAllWhitespace(String)
* @see java.lang.Character#isWhitespace
*/
public static String trimAllWhitespace(String str) {
if (!hasLength(str)) {
return str;
public static CharSequence trimAllWhitespace(CharSequence text) {
if (!hasLength(text)) {
return text;
}
int len = str.length();
StringBuilder sb = new StringBuilder(str.length());
int len = text.length();
StringBuilder sb = new StringBuilder(text.length());
for (int i = 0; i < len; i++) {
char c = str.charAt(i);
char c = text.charAt(i);
if (!Character.isWhitespace(c)) {
sb.append(c);
}
@@ -254,6 +256,21 @@ public abstract class StringUtils {
return sb.toString();
}
/**
* Trim <em>all</em> whitespace from the given {@code String}:
* leading, trailing, and in between characters.
* @param str the {@code String} to check
* @return the trimmed {@code String}
* @see #trimAllWhitespace(CharSequence)
* @see java.lang.Character#isWhitespace
*/
public static String trimAllWhitespace(String str) {
if (str == null) {
return null;
}
return trimAllWhitespace((CharSequence) str).toString();
}
/**
* Trim leading whitespace from the given {@code String}.
* @param str the {@code String} to check

View File

@@ -174,7 +174,7 @@ public final class DataSize implements Comparable<DataSize>, Serializable {
public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) {
Assert.notNull(text, "Text must not be null");
try {
Matcher matcher = DataSizeUtils.PATTERN.matcher(text);
Matcher matcher = DataSizeUtils.PATTERN.matcher(StringUtils.trimAllWhitespace(text));
Assert.state(matcher.matches(), "Does not match data size pattern");
DataUnit unit = DataSizeUtils.determineDataUnit(matcher.group(2), defaultUnit);
long amount = Long.parseLong(matcher.group(1));