Make BitReader rely on passed slice size without the additional arguments.
[nihav.git] / nihav-core / src / io / intcode.rs
index 8d879205762a0c50ba8a1dc6756821ab2aa0befe..5169b41bd7895e9ff780dd4871a507afd1637e1e 100644 (file)
@@ -7,6 +7,8 @@ pub enum UintCodeType {
     Unary012,
     Unary210,
     LimitedUnary(u32, u32),
+    LimitedZeroes(u32),
+    LimitedOnes(u32),
     Golomb(u8),
     Rice(u8),
     Gamma,
@@ -29,7 +31,7 @@ fn read_unary(br: &mut BitReader, terminator: u32) -> BitReaderResult<u32> {
     let mut res: u32 = 0;
     loop {
         if br.read(1)? == terminator { return Ok(res); }
-        res = res + 1;
+        res += 1;
     }
 }
 
@@ -37,7 +39,7 @@ fn read_unary_lim(br: &mut BitReader, len: u32, terminator: u32) -> BitReaderRes
     let mut res: u32 = 0;
     loop {
         if br.read(1)? == terminator { return Ok(res); }
-        res = res + 1;
+        res += 1;
         if res == len { return Ok(res); }
     }
 }
@@ -51,15 +53,15 @@ fn read_golomb(br: &mut BitReader, m: u8) -> BitReaderResult<u32> {
     if m == 0 { return Err(BitReaderError::InvalidValue); }
     let nbits = (8 - m.leading_zeros()) as u8;
     if (m & (m - 1)) == 0 { return read_rice(br, nbits); }
-    let cutoff = ((1 << nbits) - m) as u32;
+    let cutoff = u32::from((1 << nbits) - m);
     let pfx = read_unary(br, 0)?;
     let tail = br.read(nbits - 1)?;
     if tail < cutoff {
-        let res = pfx * (m as u32) + tail;
+        let res = pfx * u32::from(m) + tail;
         Ok (res)
     } else {
         let add = br.read(1)?;
-        let res = pfx * (m as u32) + (tail - cutoff) * 2 + add + cutoff;
+        let res = pfx * u32::from(m) + (tail - cutoff) * 2 + add + cutoff;
         Ok (res)
     }
 }
@@ -101,6 +103,8 @@ impl<'a> IntCodeReader for BitReader<'a> {
         match t {
             UintCodeType::UnaryOnes               => read_unary(self, 0),
             UintCodeType::UnaryZeroes             => read_unary(self, 1),
+            UintCodeType::LimitedZeroes(len)      => read_unary_lim(self, len, 1),
+            UintCodeType::LimitedOnes(len)        => read_unary_lim(self, len, 0),
             UintCodeType::LimitedUnary(len, term) => read_unary_lim(self, len, term),
             UintCodeType::Unary012                => read_unary_lim(self, 2, 0),
             UintCodeType::Unary210                => read_unary210(self),
@@ -137,7 +141,7 @@ mod test {
     fn int_codes() {
         const GDATA: [u8; 6] = [0b000_001_01, 0b0_0110_011, 0b1_1000_100, 0b1_1010_101, 0b10_10111_1, 0b1000_0000];
         let src = &GDATA;
-        let mut br = BitReader::new(src, src.len(), BitReaderMode::BE);
+        let mut br = BitReader::new(src, BitReaderMode::BE);
         for i in 0..11 {
             assert_eq!(br.read_code(UintCodeType::Golomb(5)).unwrap(), i);
         }