Use Long.parseLong(CharSequence,...) to avoid intermediate String creation

Where possible, switch to the Long.parseLong variant that accepts a
start and end index for the supplied CharSequence, thus avoiding making
unnecessary copies of the String input.

Closes gh-30710
This commit is contained in:
Patrick Strawderman
2023-06-20 20:55:35 -07:00
committed by Sam Brannen
parent af1c06917d
commit 01e90bbd0e
4 changed files with 12 additions and 11 deletions

View File

@@ -174,10 +174,11 @@ 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(StringUtils.trimAllWhitespace(text));
CharSequence trimmedText = StringUtils.trimAllWhitespace(text);
Matcher matcher = DataSizeUtils.PATTERN.matcher(trimmedText);
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));
long amount = Long.parseLong(trimmedText, matcher.start(1), matcher.end(1), 10);
return DataSize.of(amount, unit);
}
catch (Exception ex) {