Skip to content

Commit

Permalink
Merge pull request #31 from filipegoncalves/master
Browse files Browse the repository at this point in the history
Added EOF parser.
  • Loading branch information
Geal committed May 5, 2015
2 parents d83abb5 + a86b1a8 commit 8594c8b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions src/nom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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));
}

}

0 comments on commit 8594c8b

Please sign in to comment.