diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-12-15 15:43:22 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-12-15 15:43:22 +0100 |
| commit | b8620121a692df6313eeb5ccf7baf89c1e364116 (patch) | |
| tree | 2504699a666558206ccf8652ba88973aa3eaae10 /src | |
| parent | a87937d0c433480be4e81ae22ab05e3d5f0c3e19 (diff) | |
Fix nasty string boundary bug 🏗
Diffstat (limited to 'src')
| -rw-r--r-- | src/size.rs | 21 | ||||
| -rw-r--r-- | src/syntax/parsing.rs | 2 |
2 files changed, 10 insertions, 13 deletions
diff --git a/src/size.rs b/src/size.rs index edab21d1..09096783 100644 --- a/src/size.rs +++ b/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 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), + }; - let value = src[..src.len() - 2] + Ok(func(src[..src.len() - 2] .parse::<f32>() - .map_err(|_| ParseSizeError)?; + .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), - }) } } diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs index 41a8728b..dcba1d0f 100644 --- a/src/syntax/parsing.rs +++ b/src/syntax/parsing.rs @@ -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); } } |
