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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
pub mod buffer;
pub mod generator;
#[cfg(feature = "experimental")]
pub mod sequence;
use crate::util::{read_byte, write_byte};
use std::io::{Read, Write};
#[derive(Debug)]
pub enum DecodeError {
InvalidHeader,
UnsupportedVersion,
ExtraBytes,
UnexpectedEof,
}
pub enum IndexEncodingVersion {
V0,
#[cfg(feature = "experimental")]
V1,
}
impl Default for IndexEncodingVersion {
fn default() -> Self {
#[cfg(not(feature = "experimental"))]
{
Self::V0
}
#[cfg(feature = "experimental")]
{
Self::V1
}
}
}
impl Into<u8> for IndexEncodingVersion {
fn into(self) -> u8 {
match self {
Self::V0 => 0,
#[cfg(feature = "experimental")]
Self::V1 => 1,
}
}
}
fn encode_v_byte<W: Write>(data: &mut W, mut v: u32) {
loop {
write_byte(data, ((v & 127) | (if v > 127 { 128 } else { 0 })) as u8);
v >>= 7;
if v == 0 {
break;
}
}
}
fn decode_v_byte<R: Read>(data: &mut R) -> u32 {
let lead = read_byte(data) as u32;
if lead < 128 {
return lead;
}
let mut result = lead & 127;
let mut shift = 7;
for _ in 0..4 {
let group = read_byte(data) as u32;
result |= (group & 127) << shift;
shift += 7;
if group < 128 {
break;
}
}
result
}