Fix nasty string boundary bug 🏗
This commit is contained in:
parent
a87937d0c4
commit
b8620121a6
25
src/size.rs
25
src/size.rs
@ -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)?))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user