From c24797815af2c7544537f972f252c5cc1f8c9f9a Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Sat, 30 Mar 2024 14:16:34 +0000 Subject: [PATCH] Deduplicate eof error handling --- src/lib.rs | 53 +++++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 84c3ae3..0f8cd08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1228,10 +1228,6 @@ impl<'a> Parser<'a> { p } - fn eof(&self) -> bool { - self.ch.is_none() - } - fn bump(&mut self) { self.ch = self.rdr.next(); match self.ch { @@ -1256,6 +1252,18 @@ impl<'a> Parser<'a> { }) } + #[cold] + fn eof_error(&self, expecting: &[Option]) -> Result { + self.error(format!("expecting \"{:?}\" but found EOF.", expecting)) + } + + fn char_or_eof(&self, expecting: &[Option]) -> Result { + match self.ch { + Some(ch) => Ok(ch), + None => self.eof_error(expecting), + } + } + /// Consume all the white space until the end of the line or a tab fn parse_whitespace(&mut self) { while let Some(c) = self.ch { @@ -1364,12 +1372,9 @@ impl<'a> Parser<'a> { let mut result: String = String::new(); while !endpoint.contains(&self.ch) { - match self.ch { - None => { - return self.error(format!("expecting \"{:?}\" but found EOF.", endpoint)); - } + match self.char_or_eof(endpoint)? { #[cfg(feature = "inline-comment")] - Some(space) if check_inline_comment && (space == ' ' || space == '\t') => { + space if check_inline_comment && (space == ' ' || space == '\t') => { self.bump(); match self.ch { @@ -1386,12 +1391,9 @@ impl<'a> Parser<'a> { } } } - Some('\\') if self.opt.enabled_escape => { + '\\' if self.opt.enabled_escape => { self.bump(); - if self.eof() { - return self.error(format!("expecting \"{:?}\" but found EOF.", endpoint)); - } - match self.ch.unwrap() { + match self.char_or_eof(endpoint)? { '0' => result.push('\0'), 'a' => result.push('\x07'), 'b' => result.push('\x08'), @@ -1404,9 +1406,8 @@ impl<'a> Parser<'a> { let mut code: String = String::with_capacity(4); for _ in 0..4 { self.bump(); - if self.eof() { - return self.error(format!("expecting \"{:?}\" but found EOF.", endpoint)); - } else if let Some('\\') = self.ch { + let ch = self.char_or_eof(endpoint)?; + if ch == '\\' { self.bump(); if self.ch != Some('\n') { return self.error(format!( @@ -1416,24 +1417,20 @@ impl<'a> Parser<'a> { )); } } - code.push(self.ch.unwrap()); + + code.push(ch); } let r = u32::from_str_radix(&code[..], 16); - match r { - Ok(c) => match char::from_u32(c) { - Some(c) => result.push(c), - None => { - return self.error("unknown character in \\xHH form"); - } - }, - Err(_) => return self.error("unknown character in \\xHH form"), + match r.ok().and_then(char::from_u32) { + Some(ch) => result.push(ch), + None => return self.error("unknown character in \\xHH form"), } } c => result.push(c), } } - Some(c) => { - result.push(c); + ch => { + result.push(ch); } } self.bump();