1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#[macro_use]
extern crate nom;
use nom::Err::*;
use nom::Needed::Unknown;
pub fn take_varint(i: &[u8]) -> nom::IResult<&[u8], usize> {
let mut res: usize = 0;
let mut count: usize = 0;
let mut remainder = i;
loop {
let byte = match take!(remainder, 1) {
Ok((rest, bytes)) => {remainder = rest; bytes[0]},
Err(_) => return Err(Incomplete(Unknown)),
};
res += ((byte as usize) & 127) << (count * 7);
count += 1;
if (byte >> 7) == 0 {
return Ok((remainder, res));
}
}
}
#[cfg(test)]
mod test {
#[test]
fn parse_varint_simple() {
assert_eq!(super::take_varint(&[0x0b, 0x01, 0x02, 0x03]), Ok((b"\x01\x02\x03" as &[u8], 11)));
}
#[test]
fn parse_varint_twobyte() {
assert_eq!(super::take_varint(&[0x84, 0x02, 0x04, 0x05, 0x06]), Ok((b"\x04\x05\x06" as &[u8], 260)));
}
}