Skip to content

Commit

Permalink
use u16 instead of u32
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Aug 28, 2018
1 parent 785e833 commit 852a3d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
14 changes: 7 additions & 7 deletions blobby/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ pub struct BlobIterator<'a> {

impl<'a> BlobIterator<'a> {
pub fn new(data: &'a [u8]) -> Self {
let (len, data) = data.split_at(4);
let len = LE::read_u32(len) as usize;
let (index, data) = data.split_at(4*len);
let sum = index.chunks(4)
.map(|chunk| LE::read_u32(chunk) as usize)
let (len, data) = data.split_at(2);
let len = LE::read_u16(len) as usize;
let (index, data) = data.split_at(2*len);
let sum = index.chunks(2)
.map(|chunk| LE::read_u16(chunk) as usize)
.fold(0, |acc, x| acc + x);
assert!(sum <= data.len());
Self { index, data, n: 0, cursor: 0 }
Expand All @@ -28,9 +28,9 @@ impl<'a> Iterator for BlobIterator<'a> {
type Item = &'a [u8];

fn next(&mut self) -> Option<&'a [u8]> {
let n = 4*self.n;
let n = 2*self.n;
if n >= self.index.len() { return None; }
let len = LE::read_u32(&self.index[n..n + 4]) as usize;
let len = LE::read_u16(&self.index[n..n + 2]) as usize;
let cursor = self.cursor;
self.cursor += len;
self.n += 1;
Expand Down
12 changes: 6 additions & 6 deletions blobby/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ fn check(data: &[u8], result: &[&[u8]]) {

#[test]
fn empty() {
let data = b"\x00\x00\x00\x00";
let data = b"\x00\x00";
check(data, &[]);
}

#[test]
fn single() {
let data = b"\
\x01\x00\x00\x00\
\x0A\x00\x00\x00\
\x01\x00\
\x0A\x00\
0123456789\
";
check(data, &[b"0123456789"]);
Expand All @@ -32,9 +32,9 @@ fn single() {
#[test]
fn double() {
let data = b"\
\x02\x00\x00\x00\
\x0A\x00\x00\x00\
\x03\x00\x00\x00\
\x02\x00\
\x0A\x00\
\x03\x00\
0123456789\
abc\
";
Expand Down

0 comments on commit 852a3d3

Please sign in to comment.