@@ -100,27 +100,45 @@ impl ReaderState {
100100 off += p + 1 ;
101101 // if next byte after `-` is also `-`, return an error
102102 if buf[ 3 + off] == b'-' {
103+ // Explanation of the magic:
104+ //
105+ // - `self.offset`` just after `>`,
106+ // - `buf` contains `!-- con--tent --`
107+ // - `p` is counted from byte after `<!--`
108+ //
109+ // <!-- con--tent -->:
110+ // ~~~~~~~~~~~~~~~~ : - buf
111+ // : =========== : - zone of search (possible values of `p`)
112+ // : |---p : - p is counted from | (| is 0)
113+ // : : : ^ - self.offset
114+ // ^ : : - self.offset - len
115+ // ^ : - self.offset - len + 2
116+ // ^ - self.offset - len + 2 + p
103117 self . last_error_offset = self . offset - len + 2 + p;
104118 return Err ( Error :: IllFormed ( IllFormedError :: DoubleHyphenInComment ) ) ;
105119 }
120+ // Continue search after single `-` (+1 to skip it)
106121 haystack = & haystack[ p + 1 ..] ;
107122 }
108123 }
109124 Ok ( Event :: Comment ( BytesText :: wrap (
125+ // Cut of `!--` and `--` from start and end
110126 & buf[ 3 ..len - 2 ] ,
111127 self . decoder ( ) ,
112128 ) ) )
113129 }
114130 BangType :: CData if uncased_starts_with ( buf, b"![CDATA[" ) => {
115131 debug_assert ! ( buf. ends_with( b"]]" ) ) ;
116132 Ok ( Event :: CData ( BytesCData :: wrap (
133+ // Cut of `![CDATA[` and `]]` from start and end
117134 & buf[ 8 ..len - 2 ] ,
118135 self . decoder ( ) ,
119136 ) ) )
120137 }
121138 BangType :: DocType if uncased_starts_with ( buf, b"!DOCTYPE" ) => {
122139 match buf[ 8 ..] . iter ( ) . position ( |& b| !is_whitespace ( b) ) {
123140 Some ( start) => Ok ( Event :: DocType ( BytesText :: wrap (
141+ // Cut of `!DOCTYPE` and any number of spaces from start
124142 & buf[ 8 + start..] ,
125143 self . decoder ( ) ,
126144 ) ) ) ,
@@ -209,6 +227,7 @@ impl ReaderState {
209227 // We accept at least <??>
210228 // ~~ - len = 2
211229 if len > 1 && buf[ len - 1 ] == b'?' {
230+ // Cut of `?` and `?` from start and end
212231 let content = & buf[ 1 ..len - 1 ] ;
213232 let len = content. len ( ) ;
214233
0 commit comments