Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Factor out BangType and BangType creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Mar 8, 2022
1 parent e46193b commit a885278
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions src/reader.rs
Expand Up @@ -1049,22 +1049,7 @@ impl<'b, 'i, R: BufRead + 'i> BufferedInput<'b, 'i, &'b mut Vec<u8>> for R {
buf.push(b'!');
self.consume(1);

enum BangType {
// <![CDATA[...]]>
CData,
// <!--...-->
Comment,
// <!DOCTYPE...>
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() {
Expand Down Expand Up @@ -1303,22 +1288,7 @@ impl<'a> BufferedInput<'a, 'a, ()> for &'a [u8] {
// start with it.
debug_assert_eq!(self[0], b'!');

enum BangType {
// <![CDATA[...]]>
CData,
// <!--...-->
Comment,
// <!DOCTYPE...>
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 {
Expand Down Expand Up @@ -1426,6 +1396,28 @@ impl<'a> BufferedInput<'a, 'a, ()> for &'a [u8] {
}
}

/// Possible elements started with `<!`
enum BangType {
/// <![CDATA[...]]>
CData,
/// <!--...-->
Comment,
/// <!DOCTYPE...>
DocType,
}
impl BangType {
#[inline(always)]
fn new(byte: Option<u8>) -> Result<Self> {
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 {
Expand Down

0 comments on commit a885278

Please sign in to comment.