Fix nasty string boundary bug 🏗

This commit is contained in:
Laurenz 2019-12-15 15:43:22 +01:00
parent a87937d0c4
commit b8620121a6
2 changed files with 12 additions and 15 deletions

View File

@ -352,21 +352,18 @@ impl FromStr for Size {
type Err = ParseSizeError; type Err = ParseSizeError;
fn from_str(src: &str) -> Result<Size, ParseSizeError> { fn from_str(src: &str) -> Result<Size, ParseSizeError> {
if src.len() < 2 { let func = match () {
return Err(ParseSizeError); _ if src.ends_with("pt") => Size::pt,
} _ if src.ends_with("mm") => Size::mm,
_ if src.ends_with("cm") => Size::cm,
let value = src[..src.len() - 2] _ if src.ends_with("in") => Size::inches,
.parse::<f32>()
.map_err(|_| ParseSizeError)?;
Ok(match &src[src.len() - 2..] {
"pt" => Size::pt(value),
"mm" => Size::mm(value),
"cm" => Size::cm(value),
"in" => Size::inches(value),
_ => return Err(ParseSizeError), _ => return Err(ParseSizeError),
}) };
Ok(func(src[..src.len() - 2]
.parse::<f32>()
.map_err(|_| ParseSizeError)?))
} }
} }

View File

@ -253,7 +253,7 @@ impl<'s> Parser<'s> {
// This loop does not actually loop, but is used for breaking. // This loop does not actually loop, but is used for breaking.
loop { loop {
if text.ends_with('%') { if text.ends_with('%') {
if let Ok(percent) = text[..text.len() - 1].parse::<f64>() { if let Ok(percent) = text[.. text.len()-1].parse::<f64>() {
break Expression::Num(percent / 100.0); break Expression::Num(percent / 100.0);
} }
} }