Factor out checking of the end of `<!` syntax constructs into a function and fix part of tafia/quick-xml#344
As a result the same optimization is applied for borrowed parsed in DOCTYPE
parsing as in non-borrowing one
This fixes the following tests (which is the reason of
reader::test::buffered::read_bang_element::comment::not_closed3
reader::test::buffered::read_bang_element::comment::with_content
@@ -1080,18 +1080,7 @@ impl<'b, 'i, R: BufRead + 'i> BufferedInput<'b, 'i, &'b mut Vec<u8>> for R {
self.consume(used);
read += used;
- let finished = match bang_type {
- BangType::Comment => read >= 5 && buf.ends_with(b"--"),
- BangType::CData => buf.ends_with(b"]]"),
- BangType::DocType => {
- memchr::memchr2_iter(b'<', b'>', buf)
- .map(|p| if buf[p] == b'<' { 1i32 } else { -1 })
- .sum::<i32>()
- == 0
- }
- };
-
- if finished {
+ if bang_type.check_finished(buf, read) {
break;
} else {
// '>' was omitted in the extend_from_slice above
@@ -1293,20 +1282,9 @@ impl<'a> BufferedInput<'a, 'a, ()> for &'a [u8] {
let bang_type = BangType::new(self[1..].first().copied())?;
for i in memchr::memchr_iter(b'>', self) {
- let finished = match bang_type {
- BangType::Comment => i >= 5 && self[..i].ends_with(b"--"),
- BangType::CData => self[..i].ends_with(b"]]"),
- BangType::DocType => {
- // Inefficient, but unlikely to happen often
- let open = self[..i].iter().filter(|b| **b == b'<').count();
- let closed = self[..i].iter().filter(|b| **b == b'>').count();
- open == closed
- }
- };
-
- if finished {
- *position += i + 1;
let bytes = &self[..i];
+ if bang_type.check_finished(bytes, i + 1) {
+ *position += i + 1;
// Skip the '>' too.
*self = &self[i + 1..];
return Ok(Some(bytes));
@@ -1418,6 +1396,28 @@ impl BangType {
None => return Err(Error::UnexpectedEof("Bang".to_string())),
})
}
+
+ /// Checks that element is finished
+ ///
+ /// # Parameters
+ /// - `buf`: data from the `!` symbol
+ /// - `index`: position of the `>` symbol
+ #[inline(always)]
+ fn check_finished(&self, buf: &[u8], index: usize) -> bool {
+ match self {
+ // Need to read at least 6 symbols (`!---->`) for properly finished comment
+ // <!----> - XML comment
+ // 012345 - index
+ Self::Comment => index > 5 && buf.ends_with(b"--"),
+ Self::CData => buf.ends_with(b"]]"),
+ Self::DocType => {
+ memchr::memchr2_iter(b'<', b'>', buf)
+ .map(|p| if buf[p] == b'<' { 1i32 } else { -1 })
+ .sum::<i32>()
+ == 0
+ }
+ }
+ }
}
/// A function to check whether the byte is a whitespace (blank, new line, carriage return or tab)
