From f0f7bd124cb6e085fbc1e009ecf4db49644aa1cd Mon Sep 17 00:00:00 2001 From: c Date: Mon, 16 Dec 2024 12:23:00 +0100 Subject: [PATCH] Less indexing, less unwraps --- src/css/parser.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/css/parser.rs b/src/css/parser.rs index 83886dd..5e33bfa 100644 --- a/src/css/parser.rs +++ b/src/css/parser.rs @@ -390,6 +390,10 @@ pub(crate) fn parse_color_attribute( parse_color(&value.tokens).or_else(|e| parse_faulty_color(e, text)) } +fn parse_color_part(text: &str, index: std::ops::Range) -> Option { + u8::from_str_radix(text.get(index)?, 16).ok() +} + // Both Firefox and Chromium accept "00aabb" as a bgcolor - I'm not sure this has ever been legal, // but regrettably I've had e-mails which were unreadable without doing this. fn parse_faulty_color( @@ -397,10 +401,10 @@ fn parse_faulty_color( text: &str, ) -> Result>> { let text = text.trim(); - if text.chars().all(|c| c.is_hex_digit()) && text.len() == 6 { - let r = u8::from_str_radix(&text[0..2], 16).unwrap(); - let g = u8::from_str_radix(&text[2..4], 16).unwrap(); - let b = u8::from_str_radix(&text[4..6], 16).unwrap(); + let r = parse_color_part(text, 0..2); + let g = parse_color_part(text, 2..4); + let b = parse_color_part(text, 4..6); + if let (Some(r), Some(g), Some(b)) = (r, g, b) { return Ok(Colour::Rgb(r, g, b)); } Err(e) @@ -618,7 +622,7 @@ fn parse_string_token(text: &str) -> IResult<&str, Token> { loop { match chars.next() { - None => return Ok((&text[text.len()..], Token::String(s.into()))), + None => return Ok(("", Token::String(s.into()))), Some((i, c)) if c == end_char => { return Ok((&text[i + 1..], Token::String(s.into()))); }