Skip to content

Commit

Permalink
Use the leb128 crate where we can
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Jan 28, 2019
1 parent 44d04ba commit e16a6c1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "0.1.0"
[dependencies]
failure = "0.1.2"
id-arena = { version = "2.1.0", features = ['rayon'] }
leb128 = "0.2.3"
log = "0.4"
rayon = "1.0.3"
wasmparser = "0.26"
Expand Down
26 changes: 7 additions & 19 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,16 @@ impl<'data> Encoder<'data> {
self.u32(amt as u32)
}

pub fn u32(&mut self, mut amt: u32) {
while amt >= (1 << 7) {
self.byte((amt as u8) & 0x7f | 0x80);
amt >>= 7;
}
self.byte(amt as u8);
pub fn u32(&mut self, amt: u32) {
leb128::write::unsigned(&mut self.dst, amt.into()).unwrap();
}

pub fn i32(&mut self, val: i32) {
self.i64(val as i64);
leb128::write::signed(&mut self.dst, val.into()).unwrap();
}

pub fn i64(&mut self, mut val: i64) {
let mut done = false;
while !done {
let mut byte = (val as i8) & 0x7f;
val >>= 7;
if (val == 0 && (byte & 0x40 == 0)) || (val == -1 && (byte & 0x40 != 0)) {
done = true;
} else {
byte |= 0x80u8 as i8;
}
self.byte(byte as u8);
}
pub fn i64(&mut self, val: i64) {
leb128::write::signed(&mut self.dst, val).unwrap();
}

pub fn f32(&mut self, val: f32) {
Expand Down Expand Up @@ -92,6 +78,8 @@ impl<'data> Encoder<'data> {
self.dst.len()
}

// TODO: don't write this code here, use upstream once
// gimli-rs/leb128#6 is implemented
pub fn u32_at(&mut self, pos: usize, mut amt: u32) {
for i in 0..MAX_U32_LENGTH {
let flag = if i == MAX_U32_LENGTH - 1 { 0 } else { 0x80 };
Expand Down

0 comments on commit e16a6c1

Please sign in to comment.