|
| 1 | +//! Contains a parser for an XML processing instruction. |
| 2 | +
|
| 3 | +/// A parser that search a `?>` sequence in the slice. |
| 4 | +/// |
| 5 | +/// To use a parser create an instance of parser and [`feed`] data into it. |
| 6 | +/// After successful search the parser will return [`Some`] with position where |
| 7 | +/// processing instruction is ended (the position after `?>`). If search was |
| 8 | +/// unsuccessful, a [`None`] will be returned. You typically would expect positive |
| 9 | +/// result of search, so that you should feed new data until yo'll get it. |
| 10 | +/// |
| 11 | +/// NOTE: after successful match the parser does not returned to the initial |
| 12 | +/// state and should not be used anymore. Create a new parser if you want to perform |
| 13 | +/// new search. |
| 14 | +/// |
| 15 | +/// # Example |
| 16 | +/// |
| 17 | +/// ``` |
| 18 | +/// # use quick_xml::reader::PiParser; |
| 19 | +/// # use pretty_assertions::assert_eq; |
| 20 | +/// let mut parser = PiParser::default(); |
| 21 | +/// |
| 22 | +/// // Parse `<?instruction with = 'some > and ?' inside?>and the text follow...` |
| 23 | +/// // splitted into three chunks |
| 24 | +/// assert_eq!(parser.feed(b"<?instruction"), None); |
| 25 | +/// // ...get new chunk of data |
| 26 | +/// assert_eq!(parser.feed(b" with = 'some > and ?"), None); |
| 27 | +/// // ...get another chunk of data |
| 28 | +/// assert_eq!(parser.feed(b"' inside?>and the text follow..."), Some(10)); |
| 29 | +/// // ^ ^ |
| 30 | +/// // 0 10 |
| 31 | +/// ``` |
| 32 | +/// |
| 33 | +/// [`feed`]: Self::feed() |
| 34 | +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 35 | +pub struct PiParser( |
| 36 | + /// A flag that indicates was the `bytes` in the previous attempt to find the |
| 37 | + /// end ended with `?`. |
| 38 | + pub bool, |
| 39 | +); |
| 40 | + |
| 41 | +impl PiParser { |
| 42 | + /// Determines the end position of a processing instruction in the provided slice. |
| 43 | + /// Processing instruction ends on the first occurrence of `?>` which cannot be |
| 44 | + /// escaped. |
| 45 | + /// |
| 46 | + /// Returns position after the `?>` or `None` if such sequence was not found. |
| 47 | + /// |
| 48 | + /// [Section 2.6]: Parameter entity references MUST NOT be recognized within |
| 49 | + /// processing instructions, so parser do not search for them. |
| 50 | + /// |
| 51 | + /// # Parameters |
| 52 | + /// - `bytes`: a slice to find the end of a processing instruction. |
| 53 | + /// Should contain text in ASCII-compatible encoding |
| 54 | + /// |
| 55 | + /// [Section 2.6]: https://www.w3.org/TR/xml11/#sec-pi |
| 56 | + pub fn feed(&mut self, bytes: &[u8]) -> Option<usize> { |
| 57 | + for i in memchr::memchr_iter(b'>', bytes) { |
| 58 | + match i { |
| 59 | + // +1 for `>` which should be included in event |
| 60 | + 0 if self.0 => return Some(1), |
| 61 | + // If the previous byte is `?`, then we found `?>` |
| 62 | + // +1 for `>` which should be included in event |
| 63 | + i if i > 0 && bytes[i - 1] == b'?' => return Some(i + 1), |
| 64 | + _ => {} |
| 65 | + } |
| 66 | + } |
| 67 | + self.0 = bytes.last().copied() == Some(b'?'); |
| 68 | + None |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn pi() { |
| 74 | + use pretty_assertions::assert_eq; |
| 75 | + |
| 76 | + /// Returns `Ok(pos)` with the position in the buffer where processing |
| 77 | + /// instruction is ended. |
| 78 | + /// |
| 79 | + /// Returns `Err(internal_state)` if parsing is not done yet. |
| 80 | + fn parse_pi(bytes: &[u8], had_question_mark: bool) -> Result<usize, bool> { |
| 81 | + let mut parser = PiParser(had_question_mark); |
| 82 | + match parser.feed(bytes) { |
| 83 | + Some(i) => Ok(i), |
| 84 | + None => Err(parser.0), |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Comments shows which character was seen the last before calling `feed`. |
| 89 | + // `x` means any character, pipe denotes start of the buffer that passed to `feed` |
| 90 | + |
| 91 | + assert_eq!(parse_pi(b"", false), Err(false)); // x| |
| 92 | + assert_eq!(parse_pi(b"", true), Err(false)); // ?| |
| 93 | + |
| 94 | + assert_eq!(parse_pi(b"?", false), Err(true)); // x|? |
| 95 | + assert_eq!(parse_pi(b"?", true), Err(true)); // ?|? |
| 96 | + |
| 97 | + assert_eq!(parse_pi(b">", false), Err(false)); // x|> |
| 98 | + assert_eq!(parse_pi(b">", true), Ok(1)); // ?|> |
| 99 | + |
| 100 | + assert_eq!(parse_pi(b"?>", false), Ok(2)); // x|?> |
| 101 | + assert_eq!(parse_pi(b"?>", true), Ok(2)); // ?|?> |
| 102 | + |
| 103 | + assert_eq!(parse_pi(b">?>", false), Ok(3)); // x|>?> |
| 104 | + assert_eq!(parse_pi(b">?>", true), Ok(1)); // ?|>?> |
| 105 | +} |
0 commit comments