diff --git a/src/reader.rs b/src/reader.rs index c72f9da..a178a23 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -1049,22 +1049,7 @@ impl<'b, 'i, R: BufRead + 'i> BufferedInput<'b, 'i, &'b mut Vec> for R { buf.push(b'!'); self.consume(1); - enum BangType { - // - CData, - // - Comment, - // - DocType, - } - - let bang_type = match self.peek_one()? { - Some(b'[') => BangType::CData, - Some(b'-') => BangType::Comment, - Some(b'D') | Some(b'd') => BangType::DocType, - Some(_) => return Err(Error::UnexpectedBang), - None => return Err(Error::UnexpectedEof("Bang".to_string())), - }; + let bang_type = BangType::new(self.peek_one()?)?; loop { let available = match self.fill_buf() { @@ -1303,22 +1288,7 @@ impl<'a> BufferedInput<'a, 'a, ()> for &'a [u8] { // start with it. debug_assert_eq!(self[0], b'!'); - enum BangType { - // - CData, - // - Comment, - // - DocType, - } - - let bang_type = match &self[1..].first() { - Some(b'[') => BangType::CData, - Some(b'-') => BangType::Comment, - Some(b'D') | Some(b'd') => BangType::DocType, - Some(_) => return Err(Error::UnexpectedBang), - None => return Err(Error::UnexpectedEof("Bang".to_string())), - }; + let bang_type = BangType::new(self[1..].first().copied())?; for i in memchr::memchr_iter(b'>', self) { let finished = match bang_type { @@ -1426,6 +1396,28 @@ impl<'a> BufferedInput<'a, 'a, ()> for &'a [u8] { } } +/// Possible elements started with ` + CData, + /// + Comment, + /// + DocType, +} +impl BangType { + #[inline(always)] + fn new(byte: Option) -> Result { + Ok(match byte { + Some(b'[') => Self::CData, + Some(b'-') => Self::Comment, + Some(b'D') | Some(b'd') => Self::DocType, + Some(_) => return Err(Error::UnexpectedBang), + None => return Err(Error::UnexpectedEof("Bang".to_string())), + }) + } +} + /// A function to check whether the byte is a whitespace (blank, new line, carriage return or tab) #[inline] pub(crate) fn is_whitespace(b: u8) -> bool {