|
2 | 2 | use encoding_rs::{UTF_16BE, UTF_16LE, UTF_8}; |
3 | 3 |
|
4 | 4 | use crate::encoding::Decoder; |
5 | | -use crate::errors::{Error, IllFormedError, Result, SyntaxError}; |
| 5 | +use crate::errors::{Error, IllFormedError, Result}; |
6 | 6 | use crate::events::{BytesCData, BytesDecl, BytesEnd, BytesStart, BytesText, Event}; |
7 | 7 | use crate::parser::{FeedResult, Parser}; |
8 | 8 | #[cfg(feature = "encoding")] |
@@ -364,39 +364,24 @@ impl ReaderState { |
364 | 364 | debug_assert!(content.starts_with(b"<?"), "{:?}", Bytes(content)); |
365 | 365 | debug_assert!(content.ends_with(b"?>"), "{:?}", Bytes(content)); |
366 | 366 |
|
367 | | - let buf = &content[1..content.len() - 1]; |
368 | | - debug_assert!(buf.len() > 0); |
369 | | - debug_assert_eq!(buf[0], b'?'); |
370 | | - |
371 | | - let len = buf.len(); |
372 | | - // We accept at least <??> |
373 | | - // ~~ - len = 2 |
374 | | - if len > 1 && buf[len - 1] == b'?' { |
375 | | - // Cut of `?` and `?` from start and end |
376 | | - let content = &buf[1..len - 1]; |
377 | | - let len = content.len(); |
378 | | - |
379 | | - if content.starts_with(b"xml") && (len == 3 || is_whitespace(content[3])) { |
380 | | - let event = BytesDecl::from_start(BytesStart::wrap(content, 3)); |
381 | | - |
382 | | - // Try getting encoding from the declaration event |
383 | | - #[cfg(feature = "encoding")] |
384 | | - if self.encoding.can_be_refined() { |
385 | | - if let Some(encoding) = event.encoder() { |
386 | | - self.encoding = EncodingRef::XmlDetected(encoding); |
387 | | - } |
388 | | - } |
| 367 | + // Cut of `<?` and `?>` from start and end |
| 368 | + let content = &content[2..content.len() - 2]; |
| 369 | + let len = content.len(); |
| 370 | + |
| 371 | + if content.starts_with(b"xml") && (len == 3 || is_whitespace(content[3])) { |
| 372 | + let event = BytesDecl::from_start(BytesStart::wrap(content, 3)); |
389 | 373 |
|
390 | | - Ok(Event::Decl(event)) |
391 | | - } else { |
392 | | - Ok(Event::PI(BytesText::wrap(content, self.decoder()))) |
| 374 | + // Try getting encoding from the declaration event |
| 375 | + #[cfg(feature = "encoding")] |
| 376 | + if self.encoding.can_be_refined() { |
| 377 | + if let Some(encoding) = event.encoder() { |
| 378 | + self.encoding = EncodingRef::XmlDetected(encoding); |
| 379 | + } |
393 | 380 | } |
| 381 | + |
| 382 | + Ok(Event::Decl(event)) |
394 | 383 | } else { |
395 | | - // <?....EOF |
396 | | - // ^^^^^ - `buf` does not contains `<`, but we want to report error at `<`, |
397 | | - // so we move offset to it (-2 for `<` and `>`) |
398 | | - self.last_error_offset = self.offset - len - 2; |
399 | | - Err(Error::Syntax(SyntaxError::UnclosedPIOrXmlDecl)) |
| 384 | + Ok(Event::PI(BytesText::wrap(content, self.decoder()))) |
400 | 385 | } |
401 | 386 | } |
402 | 387 | FeedResult::EmitEmptyTag(_) => { |
|
0 commit comments