isValidDirective reports whether dir is a valid directive text, meaning angle brackets are matched, ignoring comments and strings.
(dir Directive)
| 265 | // isValidDirective reports whether dir is a valid directive text, |
| 266 | // meaning angle brackets are matched, ignoring comments and strings. |
| 267 | func isValidDirective(dir Directive) bool { |
| 268 | var ( |
| 269 | depth int |
| 270 | inquote uint8 |
| 271 | incomment bool |
| 272 | ) |
| 273 | for i, c := range dir { |
| 274 | switch { |
| 275 | case incomment: |
| 276 | if c == '>' { |
| 277 | if n := 1 + i - len(endComment); n >= 0 && bytes.Equal(dir[n:i+1], endComment) { |
| 278 | incomment = false |
| 279 | } |
| 280 | } |
| 281 | // Just ignore anything in comment |
| 282 | case inquote != 0: |
| 283 | if c == inquote { |
| 284 | inquote = 0 |
| 285 | } |
| 286 | // Just ignore anything within quotes |
| 287 | case c == '\'' || c == '"': |
| 288 | inquote = c |
| 289 | case c == '<': |
| 290 | if i+len(begComment) < len(dir) && bytes.Equal(dir[i:i+len(begComment)], begComment) { |
| 291 | incomment = true |
| 292 | } else { |
| 293 | depth++ |
| 294 | } |
| 295 | case c == '>': |
| 296 | if depth == 0 { |
| 297 | return false |
| 298 | } |
| 299 | depth-- |
| 300 | } |
| 301 | } |
| 302 | return depth == 0 && inquote == 0 && !incomment |
| 303 | } |
| 304 | |
| 305 | // Flush flushes any buffered XML to the underlying writer. |
| 306 | // See the EncodeToken documentation for details about when it is necessary. |