From f2d1a82f3495cbfaa0547448b7f56bb308e8347e Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Wed, 19 Aug 2020 14:26:07 -0400 Subject: [PATCH] remove unwraps from escape parsing these could not cause panics, but it is nice to reduce the number of unwraps either way --- src/parse/ident.rs | 14 ++++++++------ src/parse/value/css_function.rs | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/parse/ident.rs b/src/parse/ident.rs index b55a3ed..bfe9af0 100644 --- a/src/parse/ident.rs +++ b/src/parse/ident.rs @@ -95,16 +95,18 @@ impl<'a> Parser<'a> { break; } value *= 16; - span = span.merge(next.pos()); - value += as_hex(self.toks.next().unwrap().kind) + span = span.merge(next.pos); + value += as_hex(next.kind); + self.toks.next(); } - if self.toks.peek().is_some() && self.toks.peek().unwrap().kind.is_whitespace() { + if matches!(self.toks.peek(), Some(Token { kind: ' ', .. }) | Some(Token { kind: '\n', .. }) | Some(Token { kind: '\t', .. })) + { self.toks.next(); } } else { - let next = self.toks.next().unwrap(); - span = span.merge(next.pos()); - value = next.kind as u32; + span = span.merge(first.pos); + value = first.kind as u32; + self.toks.next(); } let c = std::char::from_u32(value).ok_or(("Invalid escape sequence.", span))?; diff --git a/src/parse/value/css_function.rs b/src/parse/value/css_function.rs index df0f60a..b900685 100644 --- a/src/parse/value/css_function.rs +++ b/src/parse/value/css_function.rs @@ -372,7 +372,8 @@ impl<'a> Parser<'a> { span = span.merge(next.pos); self.toks.peek_forward(1); } - if self.toks.peek().is_some() && self.toks.peek().unwrap().kind.is_whitespace() { + if matches!(self.toks.peek(), Some(Token { kind: ' ', .. }) | Some(Token { kind: '\n', .. }) | Some(Token { kind: '\t', .. })) + { self.toks.peek_forward(1); } } else {