Skip to content

Commit 9f117a3

Browse files
committed
Remove dead code - these checks already performed by a parser
(review with with whitespace ignored mode)
1 parent 698f88c commit 9f117a3

File tree

1 file changed

+40
-62
lines changed

1 file changed

+40
-62
lines changed

src/reader/state.rs

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -338,49 +338,39 @@ impl ReaderState {
338338
debug_assert!(content.ends_with(b"-->"), "{:?}", Bytes(content));
339339

340340
let buf = &content[1..content.len() - 1];
341-
342341
let len = buf.len();
343-
if buf.starts_with(b"!--") {
344-
debug_assert!(buf.ends_with(b"--"));
345-
if self.config.check_comments {
346-
// search if '--' not in comments
347-
let mut haystack = &buf[3..len - 2];
348-
let mut off = 0;
349-
while let Some(p) = memchr::memchr(b'-', haystack) {
350-
off += p + 1;
351-
// if next byte after `-` is also `-`, return an error
352-
if buf[3 + off] == b'-' {
353-
// Explanation of the magic:
354-
//
355-
// - `self.offset`` just after `>`,
356-
// - `buf` contains `!-- con--tent --`
357-
// - `p` is counted from byte after `<!--`
358-
//
359-
// <!-- con--tent -->:
360-
// ~~~~~~~~~~~~~~~~ : - buf
361-
// : =========== : - zone of search (possible values of `p`)
362-
// : |---p : - p is counted from | (| is 0)
363-
// : : : ^ - self.offset
364-
// ^ : : - self.offset - len
365-
// ^ : - self.offset - len + 2
366-
// ^ - self.offset - len + 2 + p
367-
self.last_error_offset = self.offset - len + 2 + p;
368-
return Err(Error::IllFormed(IllFormedError::DoubleHyphenInComment));
369-
}
370-
haystack = &haystack[p + 1..];
342+
if self.config.check_comments {
343+
// search if '--' not in comments
344+
let mut haystack = &buf[3..len - 2];
345+
let mut off = 0;
346+
while let Some(p) = memchr::memchr(b'-', haystack) {
347+
off += p + 1;
348+
// if next byte after `-` is also `-`, return an error
349+
if buf[3 + off] == b'-' {
350+
// Explanation of the magic:
351+
//
352+
// - `self.offset`` just after `>`,
353+
// - `buf` contains `!-- con--tent --`
354+
// - `p` is counted from byte after `<!--`
355+
//
356+
// <!-- con--tent -->:
357+
// ~~~~~~~~~~~~~~~~ : - buf
358+
// : =========== : - zone of search (possible values of `p`)
359+
// : |---p : - p is counted from | (| is 0)
360+
// : : : ^ - self.offset
361+
// ^ : : - self.offset - len
362+
// ^ : - self.offset - len + 2
363+
// ^ - self.offset - len + 2 + p
364+
self.last_error_offset = self.offset - len + 2 + p;
365+
return Err(Error::IllFormed(IllFormedError::DoubleHyphenInComment));
371366
}
367+
haystack = &haystack[p + 1..];
372368
}
373-
Ok(Event::Comment(BytesText::wrap(
374-
&buf[3..len - 2],
375-
self.decoder(),
376-
)))
377-
} else {
378-
// <!....>
379-
// ^^^^^ - `buf` does not contain `<` and `>`, but `self.offset` is after `>`.
380-
// ^------- We report error at that position, so we need to subtract 2 and buf len
381-
self.last_error_offset = self.offset - len - 2;
382-
Err(Error::Syntax(SyntaxError::UnclosedComment))
383369
}
370+
Ok(Event::Comment(BytesText::wrap(
371+
&buf[3..len - 2],
372+
self.decoder(),
373+
)))
384374
}
385375
FeedResult::EmitDoctype(_) => {
386376
debug_assert!(content.len() > 9, "{:?}", Bytes(content));
@@ -392,30 +382,18 @@ impl ReaderState {
392382
debug_assert!(content.ends_with(b">"), "{:?}", Bytes(content));
393383

394384
let buf = &content[1..content.len() - 1];
395-
let uncased_starts_with = |string: &[u8], prefix: &[u8]| {
396-
string.len() >= prefix.len() && string[..prefix.len()].eq_ignore_ascii_case(prefix)
397-
};
398-
399-
if uncased_starts_with(buf, b"!DOCTYPE") {
400-
match buf[8..].iter().position(|&b| !is_whitespace(b)) {
401-
Some(start) => Ok(Event::DocType(BytesText::wrap(
402-
&buf[8 + start..],
403-
self.decoder(),
404-
))),
405-
None => {
406-
// Because we here, we at least read `<!DOCTYPE>` and offset after `>`.
407-
// We want report error at place where name is expected - this is just
408-
// before `>`
409-
self.last_error_offset = self.offset - 1;
410-
return Err(Error::IllFormed(IllFormedError::MissingDoctypeName));
411-
}
385+
match buf[8..].iter().position(|&b| !is_whitespace(b)) {
386+
Some(start) => Ok(Event::DocType(BytesText::wrap(
387+
&buf[8 + start..],
388+
self.decoder(),
389+
))),
390+
None => {
391+
// Because we here, we at least read `<!DOCTYPE>` and offset after `>`.
392+
// We want report error at place where name is expected - this is just
393+
// before `>`
394+
self.last_error_offset = self.offset - 1;
395+
return Err(Error::IllFormed(IllFormedError::MissingDoctypeName));
412396
}
413-
} else {
414-
// <!....>
415-
// ^^^^^ - `buf` does not contain `<` and `>`, but `self.offset` is after `>`.
416-
// ^------- We report error at that position, so we need to subtract 2 and buf len
417-
self.last_error_offset = self.offset - len - 2;
418-
Err(Error::Syntax(SyntaxError::UnclosedDoctype))
419397
}
420398
}
421399
FeedResult::EmitPI(_) => {

0 commit comments

Comments
 (0)