Skip to content

Commit

Permalink
bug fixes for bitcursor<&[u8]>
Browse files Browse the repository at this point in the history
  • Loading branch information
bbaldino committed Jun 7, 2024
1 parent 66af442 commit 1c84322
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/bit_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ impl BitCursor<&BitSlice<u8, Msb0>> {

impl BitCursor<&[u8]> {
pub fn remaining_slice(&self) -> &BitSlice<u8, Msb0> {
let len = self.pos.min(self.inner.len() as u64);
&self.inner.view_bits()[(len as usize)..]
// Here we have to mulitply the slice length by 8, since it's in bytes
let len = self.pos.min((self.inner.len() * 8) as u64);
&self.inner.view_bits::<Msb0>()[(len as usize)..]
}
}

Expand Down Expand Up @@ -211,7 +212,7 @@ impl Read for BitCursor<&[u8]> {
"Attempted byte-level read when not on byte boundary",
));
}
match self.inner.read(buf) {
match self.remaining_slice().read(buf) {
Ok(n) => {
self.pos += (n * 8) as u64;
Ok(n)
Expand Down Expand Up @@ -336,7 +337,7 @@ where
mod test {
use std::io::{Seek, SeekFrom};

use bitvec::{order::Msb0, vec::BitVec};
use bitvec::{bits, order::Msb0, vec::BitVec};
use ux::u1;

use crate::{bit_read::BitRead, bit_read_exts::BitReadExts};
Expand Down Expand Up @@ -403,13 +404,22 @@ mod test {
fn test_sub_cursor_vec() {
let data = BitVec::<u8, Msb0>::from_vec(vec![1, 2, 3, 4]);
let mut cursor = BitCursor::new(data);
println!("{cursor:x}");

let _ = cursor.read_u8().unwrap();
let mut sub_cursor = cursor.sub_cursor(0..24);
println!("{sub_cursor:x}");

assert_eq!(sub_cursor.remaining_slice().len(), 24);
assert_eq!(sub_cursor.read_u8().unwrap(), 2);
}

#[test]
fn test_remaining_slice_u8() {
let data: Vec<u8> = vec![0b00001111, 0b10101010];

let mut cursor = BitCursor::new(&data[..]);
cursor.read_u4().unwrap();

let slice = cursor.remaining_slice();
assert_eq!(slice, bits![u8, Msb0; 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0]);
}
}

0 comments on commit 1c84322

Please sign in to comment.