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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
pub trait Write {
    fn write(&mut self, v: u64) -> std::io::Result<&mut Self>;
}

/// Write `u64` in the LEB128+ format to `std::io::Write` stream.
///
/// ## Examples
///
/// ```
/// let mut f = || -> std::io::Result<()> {
///     let mut v = vec![];
///     {
///         use leb128plus::Write;
///         let mut c = std::io::Cursor::new(&mut v);
///         c.write(0)?
///             .write(127)?
///             .write(128)?
///             .write(0xFF)?
///             .write(0x17F)?
///             .write(0x407F)?
///             .write(0x4080)?;
///     }
///     assert_eq!(v, [
///         0,
///         127,
///         128, 0,
///         0xFF, 0,
///         0xFF, 1,
///         0xFF, 0x7F,
///         0x80, 0x80, 0x00
///     ]);
///     Ok(())
/// };
/// f().unwrap();
/// ```
impl<T: std::io::Write> Write for T {
    fn write(&mut self, mut v: u64) -> std::io::Result<&mut Self> {
        loop {
            let x = v as u8;
            v = v >> 7;
            if v == 0 {
                self.write(&[x])?;
                break Ok(self);
            }
            self.write(&[0x80 | x])?;
            v = v - 1;
        }
    }
}

pub trait Read {
    fn read(&mut self) -> std::io::Result<u64>;
}

/// Read `u64` in the LEB128+ format from `std::io::Read` stream.
///
/// ## Example
///
/// ```
/// let mut c = std::io::Cursor::new(&[
///     0,
///     127,
///     128, 0,
///     0xFF, 0,
///     0xFF, 1,
///     0xFF, 0x7F,
///     0x80, 0x80, 0,
///     128,
/// ]);
/// use leb128plus::Read;
/// assert_eq!(c.read().unwrap(), 0);
/// assert_eq!(c.read().unwrap(), 127);
/// assert_eq!(c.read().unwrap(), 128);
/// assert_eq!(c.read().unwrap(), 0xFF);
/// assert_eq!(c.read().unwrap(), 0x17F);
/// assert_eq!(c.read().unwrap(), 0x407F);
/// assert_eq!(c.read().unwrap(), 0x4080);
/// assert!(match c.read() {
///     Result::Err(_) => true,
///     _ => false
/// });
/// ```
impl<T: std::io::Read> Read for T {
    fn read(&mut self) -> std::io::Result<u64> {
        let mut result = 0;
        let mut shift = 0;
        loop {
            let mut v = [0];
            self.read_exact(&mut v)?;
            result += (v[0] as u64) << shift;
            if v[0] < 128 {
                break Ok(result);
            }
            shift += 7;
        }
    }
}

/// Write `u64` in the LEB128+ format.
///
/// Examples
///
/// ```
/// let mut v = vec![];
/// {
///     let mut c = std::io::Cursor::new(&mut v);
///     leb128plus::write(&mut c, 0);
///     leb128plus::write(&mut c, 127);
///     leb128plus::write(&mut c, 128);
///     leb128plus::write(&mut c, 0xFF);
///     leb128plus::write(&mut c, 0x17F);
///     leb128plus::write(&mut c, 0x407F);
///     leb128plus::write(&mut c, 0x4080);
/// }
/// assert_eq!(v, [
///     0,
///     127,
///     128, 0,
///     0xFF, 0,
///     0xFF, 1,
///     0xFF, 0x7F,
///     0x80, 0x80, 0x00
/// ]);
/// ```
pub fn write(w: &mut std::io::Write, mut v: u64) -> std::io::Result<()> {
    loop {
        let x = v as u8;
        v = v >> 7;
        if v == 0 {
            w.write(&[x])?;
            break Ok(());
        }
        w.write(&[0x80 | x])?;
        v = v - 1;
    }
}