diff --git a/README.md b/README.md index ba6d8b26c..b88d3cec8 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ There is already a large list of parsers available, like: - **multispace**: will return the longest array containing space, \r or \n - **be_u8**, **be_u16**, **be_u32**, **be_u64** to parse big endian unsigned integers of multiple sizes - **be_f32**, **be_f64** to parse big endian floating point numbers +- **eof**: a parser that is successful only if the input is over. In any other case, it returns an error. #### Making new parsers with macros diff --git a/src/nom.rs b/src/nom.rs index eca470699..aad1eb176 100644 --- a/src/nom.rs +++ b/src/nom.rs @@ -262,6 +262,14 @@ pub fn be_f64(input: &[u8]) -> IResult<&[u8], f64> { } } +pub fn eof(input:&[u8]) -> IResult<&[u8], &[u8]> { + if input.len() == 0 { + Done(input, input) + } else { + Error(Position(0, input)) + } +} + #[cfg(test)] mod tests { use super::*; @@ -353,4 +361,16 @@ mod tests { assert_eq!(Incomplete(Needed::Size(9)), res3); } + #[test] + fn end_of_input() { + let not_over = &b"Hello, world!"[..]; + let is_over = &b""[..]; + + let res_not_over = eof(not_over); + assert_eq!(res_not_over, Error(Position(0, not_over))); + + let res_over = eof(is_over); + assert_eq!(res_over, Done(is_over, is_over)); + } + }