Introduce lenient parsing in DataSize regarding whitespace

Prior to this commit, a DataSize input string could not be parsed if it
contained any whitespace.

With this commit, a DataSize input string can contain leading, trailing,
or 'in between' whitespace. For example, the following will be parsed
to the same DataSize value.

- "1024B"
- "1024 B"
- " 1024B "
- " 1024 B "

Closes gh-28643
This commit is contained in:
Sam Brannen
2022-07-04 18:26:00 +02:00
parent bf39492c34
commit 2bf5f7a6b3
2 changed files with 21 additions and 7 deletions

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));