Skip to content

Commit 8594c8b

Browse files
committed
Merge pull request #31 from filipegoncalves/master
Added EOF parser.
2 parents d83abb5 + a86b1a8 commit 8594c8b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ There is already a large list of parsers available, like:
105105
- **multispace**: will return the longest array containing space, \r or \n
106106
- **be_u8**, **be_u16**, **be_u32**, **be_u64** to parse big endian unsigned integers of multiple sizes
107107
- **be_f32**, **be_f64** to parse big endian floating point numbers
108+
- **eof**: a parser that is successful only if the input is over. In any other case, it returns an error.
108109

109110
#### Making new parsers with macros
110111

src/nom.rs

+20
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ pub fn be_f64(input: &[u8]) -> IResult<&[u8], f64> {
262262
}
263263
}
264264

265+
pub fn eof(input:&[u8]) -> IResult<&[u8], &[u8]> {
266+
if input.len() == 0 {
267+
Done(input, input)
268+
} else {
269+
Error(Position(0, input))
270+
}
271+
}
272+
265273
#[cfg(test)]
266274
mod tests {
267275
use super::*;
@@ -353,4 +361,16 @@ mod tests {
353361
assert_eq!(Incomplete(Needed::Size(9)), res3);
354362
}
355363

364+
#[test]
365+
fn end_of_input() {
366+
let not_over = &b"Hello, world!"[..];
367+
let is_over = &b""[..];
368+
369+
let res_not_over = eof(not_over);
370+
assert_eq!(res_not_over, Error(Position(0, not_over)));
371+
372+
let res_over = eof(is_over);
373+
assert_eq!(res_over, Done(is_over, is_over));
374+
}
375+
356376
}

0 commit comments

Comments
 (0)