Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stream)!: Allow mutable iteration #274

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions proptest-regressions/stream/tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc dd80dcf3d84999d176d263e5de7ab8bd3c9b7d0b24fa74cb6004556677665b72 # shrinks to byte_len = 11, start = 0
48 changes: 24 additions & 24 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ where
c == '\r' || c == '\n'
}) {
None if PARTIAL && input.is_partial() => Err(ErrMode::Incomplete(Needed::Unknown)),
None => Ok(input.finish()),
None => Ok(input.peek_finish()),
Some(offset) => {
let (new_input, res) = input.next_slice(offset);
let (new_input, res) = input.peek_slice(offset);
let bytes = new_input.as_bstr();
let nth = bytes[0];
if nth == b'\r' {
Expand Down Expand Up @@ -935,7 +935,7 @@ where
if offset == 0 {
return Err(ErrMode::from_error_kind(input, ErrorKind::Slice));
} else {
return Ok((input.next_slice(offset).0, value));
return Ok((input.peek_slice(offset).0, value));
}
}
}
Expand All @@ -944,7 +944,7 @@ where
if <I as StreamIsPartial>::is_partial_supported() && input.is_partial() {
Err(ErrMode::Incomplete(Needed::new(1)))
} else {
Ok((input.finish().0, value))
Ok((input.peek_finish().0, value))
}
})
.parse_next(input)
Expand Down Expand Up @@ -1100,7 +1100,7 @@ where
if offset == 0 {
return Err(ErrMode::from_error_kind(input, ErrorKind::Slice));
} else {
return Ok((input.next_slice(offset).0, value));
return Ok((input.peek_slice(offset).0, value));
}
}
}
Expand All @@ -1109,7 +1109,7 @@ where
if <I as StreamIsPartial>::is_partial_supported() && input.is_partial() {
Err(ErrMode::Incomplete(Needed::new(1)))
} else {
Ok((input.finish().0, value))
Ok((input.peek_finish().0, value))
}
})
.parse_next(input)
Expand Down Expand Up @@ -1232,7 +1232,7 @@ where
// Must be at least one digit
return Err(ErrMode::from_error_kind(input, ErrorKind::Slice));
}
let (remaining, parsed) = input.next_slice(offset);
let (remaining, parsed) = input.peek_slice(offset);

let mut res = O::default();
for c in parsed.as_bstr() {
Expand Down Expand Up @@ -1482,15 +1482,15 @@ where
} else if i2.eof_offset() == current_len {
let offset = i2.offset_from(&start);
input.reset(start);
return Ok(input.next_slice(offset));
return Ok(input.peek_slice(offset));
} else {
input = i2;
}
}
Err(ErrMode::Backtrack(_)) => {
if input.next_token().expect("eof_offset > 0").1.as_char() == control_char {
if input.peek_token().expect("eof_offset > 0").1.as_char() == control_char {
let next = control_char.len_utf8();
let (i2, _) = escapable.parse_next(input.next_slice(next).0)?;
let (i2, _) = escapable.parse_next(input.peek_slice(next).0)?;
if i2.eof_offset() == 0 {
return Err(ErrMode::Incomplete(Needed::Unknown));
} else {
Expand All @@ -1499,7 +1499,7 @@ where
} else {
let offset = input.offset_from(&start);
input.reset(start);
return Ok(input.next_slice(offset));
return Ok(input.peek_slice(offset));
}
}
Err(e) => {
Expand Down Expand Up @@ -1536,29 +1536,29 @@ where
// does not consume anything
if i2.eof_offset() == 0 {
input.reset(start);
return Ok(input.finish());
return Ok(input.peek_finish());
} else if i2.eof_offset() == current_len {
let offset = i2.offset_from(&start);
input.reset(start);
return Ok(input.next_slice(offset));
return Ok(input.peek_slice(offset));
} else {
input = i2;
}
}
Err(ErrMode::Backtrack(_)) => {
if input.next_token().expect("eof_offset > 0").1.as_char() == control_char {
if input.peek_token().expect("eof_offset > 0").1.as_char() == control_char {
let next = control_char.len_utf8();
let (i2, _) = escapable.parse_next(input.next_slice(next).0)?;
let (i2, _) = escapable.parse_next(input.peek_slice(next).0)?;
if i2.eof_offset() == 0 {
input.reset(start);
return Ok(input.finish());
return Ok(input.peek_finish());
} else {
input = i2;
}
} else {
let offset = input.offset_from(&start);
input.reset(start);
return Ok(input.next_slice(offset));
return Ok(input.peek_slice(offset));
}
}
Err(e) => {
Expand All @@ -1567,7 +1567,7 @@ where
}
}

Ok(input.finish())
Ok(input.peek_finish())
}

/// Matches a byte string with escaped characters.
Expand Down Expand Up @@ -1684,9 +1684,9 @@ where
}
}
Err(ErrMode::Backtrack(_)) => {
if input.next_token().expect("eof_offset > 0").1.as_char() == control_char {
if input.peek_token().expect("eof_offset > 0").1.as_char() == control_char {
let next = control_char.len_utf8();
let (i2, o) = transform.parse_next(input.next_slice(next).0)?;
let (i2, o) = transform.parse_next(input.peek_slice(next).0)?;
res.accumulate(o);
if i2.eof_offset() == 0 {
return Err(ErrMode::Incomplete(Needed::Unknown));
Expand Down Expand Up @@ -1727,20 +1727,20 @@ where
Ok((i2, o)) => {
res.accumulate(o);
if i2.eof_offset() == 0 {
return Ok((input.finish().0, res));
return Ok((input.peek_finish().0, res));
} else if i2.eof_offset() == current_len {
return Ok((input, res));
} else {
input = i2;
}
}
Err(ErrMode::Backtrack(_)) => {
if input.next_token().expect("eof_offset > 0").1.as_char() == control_char {
if input.peek_token().expect("eof_offset > 0").1.as_char() == control_char {
let next = control_char.len_utf8();
let (i2, o) = transform.parse_next(input.next_slice(next).0)?;
let (i2, o) = transform.parse_next(input.peek_slice(next).0)?;
res.accumulate(o);
if i2.eof_offset() == 0 {
return Ok((input.finish().0, res));
return Ok((input.peek_finish().0, res));
} else {
input = i2;
}
Expand Down
8 changes: 4 additions & 4 deletions src/binary/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ where
// The parser functions might already slice away all fully read bytes.
// That's why `offset / 8` isn't necessarily needed at all times.
let remaining_bytes_index = offset / 8 + if offset % 8 == 0 { 0 } else { 1 };
let (input, _) = rest.next_slice(remaining_bytes_index);
let (input, _) = rest.peek_slice(remaining_bytes_index);
Ok((input, result))
}
Err(ErrMode::Incomplete(n)) => Err(ErrMode::Incomplete(n.map(|u| u.get() / 8 + 1))),
Expand Down Expand Up @@ -105,9 +105,9 @@ where
{
trace("bytes", move |(input, offset): (I, usize)| {
let (inner, _) = if offset % 8 != 0 {
input.next_slice(1 + offset / 8)
input.peek_slice(1 + offset / 8)
} else {
input.next_slice(offset / 8)
input.peek_slice(offset / 8)
};
let i = (input, offset);
match parser.parse_next(inner) {
Expand Down Expand Up @@ -221,7 +221,7 @@ where
offset = 0;
}
}
let (input, _) = input.next_slice(cnt);
let (input, _) = input.peek_slice(cnt);
Ok(((input, end_offset), acc))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ where
I: StreamIsPartial,
I: Stream<Token = u8>,
{
input.next_token().ok_or_else(|| {
input.peek_token().ok_or_else(|| {
if PARTIAL && input.is_partial() {
ErrMode::Incomplete(Needed::new(1))
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/combinator/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn rest<I, E: ParseError<I>>(input: I) -> IResult<I, <I as Stream>::Slice, E
where
I: Stream,
{
trace("rest", move |input: I| Ok(input.finish())).parse_next(input)
trace("rest", move |input: I| Ok(input.peek_finish())).parse_next(input)
}

/// Return the length of the remaining input.
Expand Down Expand Up @@ -176,7 +176,7 @@ where
{
trace("eof", move |input: I| {
if input.eof_offset() == 0 {
Ok(input.next_slice(0))
Ok(input.peek_slice(0))
} else {
Err(ErrMode::from_error_kind(input, ErrorKind::Eof))
}
Expand Down
4 changes: 2 additions & 2 deletions src/combinator/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ where
Ok((mut input, _)) => {
let offset = input.offset_from(&checkpoint);
input.reset(checkpoint);
let (input, recognized) = input.next_slice(offset);
let (input, recognized) = input.peek_slice(offset);
Ok((input, recognized))
}
Err(e) => Err(e),
Expand Down Expand Up @@ -589,7 +589,7 @@ where
Ok((mut input, result)) => {
let offset = input.offset_from(&checkpoint);
input.reset(checkpoint);
let (input, recognized) = input.next_slice(offset);
let (input, recognized) = input.peek_slice(offset);
Ok((input, (result, recognized)))
}
Err(e) => Err(e),
Expand Down
2 changes: 1 addition & 1 deletion src/combinator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ fn alt_test() {
}

fn work(input: &[u8]) -> IResult<&[u8], &[u8], ErrorStr> {
Ok(input.finish())
Ok(input.peek_finish())
}

#[allow(unused_variables)]
Expand Down
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ pub trait Parser<I, O, E> {
/// let mut parser = separated_pair(alpha1.span(), ',', alpha1.span());
///
/// assert_eq!(parser.parse(Located::new("abcd,efgh")), Ok((0..4, 5..9)));
/// assert_eq!(parser.parse_next(Located::new("abcd;")),Err(ErrMode::Backtrack(Error::new(Located::new("abcd;").next_slice(4).0, ErrorKind::Verify))));
/// assert_eq!(parser.parse_next(Located::new("abcd;")),Err(ErrMode::Backtrack(Error::new(Located::new("abcd;").peek_slice(4).0, ErrorKind::Verify))));
/// ```
fn span(self) -> Span<Self, I, O, E>
where
Expand Down Expand Up @@ -323,7 +323,7 @@ pub trait Parser<I, O, E> {
/// let mut consumed_parser = separated_pair(alpha1.value(1).with_span(), ',', alpha1.value(2).with_span());
///
/// assert_eq!(consumed_parser.parse(Located::new("abcd,efgh")), Ok(((1, 0..4), (2, 5..9))));
/// assert_eq!(consumed_parser.parse_next(Located::new("abcd;")),Err(ErrMode::Backtrack(Error::new(Located::new("abcd;").next_slice(4).0, ErrorKind::Verify))));
/// assert_eq!(consumed_parser.parse_next(Located::new("abcd;")),Err(ErrMode::Backtrack(Error::new(Located::new("abcd;").peek_slice(4).0, ErrorKind::Verify))));
///
/// // the second output (representing the consumed input)
/// // should be the same as that of the `span` parser.
Expand Down
Loading
Loading