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;
fn from_str(src: &str) -> Result<Size, ParseSizeError> {
if src.len() < 2 {
return Err(ParseSizeError);
}
let value = src[..src.len() - 2]
.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),
let func = match () {
_ if src.ends_with("pt") => Size::pt,
_ if src.ends_with("mm") => Size::mm,
_ if src.ends_with("cm") => Size::cm,
_ if src.ends_with("in") => Size::inches,
_ => 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.
loop {
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);
}
}