-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
51 lines (46 loc) · 1.19 KB
/
lib.rs
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
47
48
49
50
51
const CHUNK_BITS: u32 = 7;
const CHUNK_MASK: u32 = (1 << CHUNK_BITS) - 1;
const CONT_BIT: u8 = 1 << CHUNK_BITS;
pub fn encode(data: &[u32], mut sink: impl FnMut(u8)) {
for value in data {
let mut value = *value;
loop {
let mut byte = (value & CHUNK_MASK) as u8;
value >>= CHUNK_BITS;
if value != 0 {
byte |= CONT_BIT
}
sink(byte);
if value == 0 {
break;
}
}
}
}
pub fn decode(data: &[u8], mut sink: impl FnMut(u32)) {
let mut value = 0;
for &byte in data.iter() {
value <<= CHUNK_BITS;
value |= byte as u32 & CHUNK_MASK;
if byte & CONT_BIT != CONT_BIT {
sink(value);
value = 0;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn id(xs: &[u32]) -> bool {
let mut packed: Vec<u8> = Vec::new();
encode(xs, |byte| packed.push(byte));
let mut decoded: Vec<u32> = Vec::new();
decode(&packed, |i| decoded.push(i));
xs == decoded.as_slice()
}
quickcheck::quickcheck! {
fn is_encoding(xs: Vec<u32>) -> bool {
id(&xs)
}
}
}