Skip to content

Commit

Permalink
Reduce unneeded trim operations in Size::from_str()
Browse files Browse the repository at this point in the history
We already trim the input at the very start (so we can read only alphabetical
characters from the end) and stop at the first alphabetical char, so there's no
need to trim `unit`.

For `number`, the start has already been trimmed so we only need to trim any
spaces captured between the scalar quantity and the start of the unit name.
  • Loading branch information
mqudsi committed Apr 27, 2024
1 parent e859431 commit 84b14d6
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/from_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ impl FromStr for Size {
Some(idx) => s.split_at(idx),
};

let number: f64 = num_str.trim().parse().map_err(|_| ParseSizeError)?;
let unit = unit.trim().to_lowercase();
let number: f64 = num_str.trim_end().parse().map_err(|_| ParseSizeError)?;
let unit = unit.to_lowercase();

let multiplier = match unit.as_str().trim_end_matches('s') {
"" | "b" | "byte" => B,
Expand Down

0 comments on commit 84b14d6

Please sign in to comment.