Skip to content

Commit

Permalink
Stop at the > in PiParser which is consistent with other search fun…
Browse files Browse the repository at this point in the history
…ctions

The parser search the end of processing instruction and this is the last byte of it
  • Loading branch information
Mingun authored and dralley committed Jun 9, 2024
1 parent f84f92c commit 79b2fda
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/reader/buffered_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ macro_rules! impl_buffered_source {

match parser.feed(available) {
Some(i) => {
// We does not include `>` in data
buf.extend_from_slice(&available[..i - 1]);
buf.extend_from_slice(&available[..i]);
done = true;
i
// +1 for `>` which we do not include
i + 1
}
None => {
buf.extend_from_slice(available);
Expand Down
24 changes: 11 additions & 13 deletions src/reader/pi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// After successful search the parser will return [`Some`] with position where
/// processing instruction is ended (the position after `?>`). If search was
/// unsuccessful, a [`None`] will be returned. You typically would expect positive
/// result of search, so that you should feed new data until yo'll get it.
/// result of search, so that you should feed new data until you get it.
///
/// NOTE: after successful match the parser does not returned to the initial
/// state and should not be used anymore. Create a new parser if you want to perform
Expand All @@ -25,9 +25,9 @@
/// // ...get new chunk of data
/// assert_eq!(parser.feed(b" with = 'some > and ?"), None);
/// // ...get another chunk of data
/// assert_eq!(parser.feed(b"' inside?>and the text follow..."), Some(10));
/// // ^ ^
/// // 0 10
/// assert_eq!(parser.feed(b"' inside?>and the text follow..."), Some(9));
/// // ^ ^
/// // 0 9
/// ```
///
/// [`feed`]: Self::feed()
Expand Down Expand Up @@ -56,11 +56,9 @@ impl PiParser {
pub fn feed(&mut self, bytes: &[u8]) -> Option<usize> {
for i in memchr::memchr_iter(b'>', bytes) {
match i {
// +1 for `>` which should be included in event
0 if self.0 => return Some(1),
0 if self.0 => return Some(0),
// If the previous byte is `?`, then we found `?>`
// +1 for `>` which should be included in event
i if i > 0 && bytes[i - 1] == b'?' => return Some(i + 1),
i if i > 0 && bytes[i - 1] == b'?' => return Some(i),
_ => {}
}
}
Expand Down Expand Up @@ -95,11 +93,11 @@ fn pi() {
assert_eq!(parse_pi(b"?", true), Err(true)); // ?|?

assert_eq!(parse_pi(b">", false), Err(false)); // x|>
assert_eq!(parse_pi(b">", true), Ok(1)); // ?|>
assert_eq!(parse_pi(b">", true), Ok(0)); // ?|>

assert_eq!(parse_pi(b"?>", false), Ok(2)); // x|?>
assert_eq!(parse_pi(b"?>", true), Ok(2)); // ?|?>
assert_eq!(parse_pi(b"?>", false), Ok(1)); // x|?>
assert_eq!(parse_pi(b"?>", true), Ok(1)); // ?|?>

assert_eq!(parse_pi(b">?>", false), Ok(3)); // x|>?>
assert_eq!(parse_pi(b">?>", true), Ok(1)); // ?|>?>
assert_eq!(parse_pi(b">?>", false), Ok(2)); // x|>?>
assert_eq!(parse_pi(b">?>", true), Ok(0)); // ?|>?>
}
8 changes: 4 additions & 4 deletions src/reader/slice_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
let mut parser = PiParser::default();

if let Some(i) = parser.feed(self) {
*position += i;
// We does not include `>` in data
let bytes = &self[..i - 1];
*self = &self[i..];
// +1 for `>` which we do not include
*position += i + 1;
let bytes = &self[..i];
*self = &self[i + 1..];
Ok((bytes, true))
} else {
*position += self.len();
Expand Down

0 comments on commit 79b2fda

Please sign in to comment.