X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;ds=sidebyside;f=nihav-core%2Fsrc%2Fio%2Fintcode.rs;h=a8437e8afeb7663ab0a98fada140eb3e108ad964;hb=9da33f04bbd89f18942bbafc6d135b5d3f102953;hp=8d879205762a0c50ba8a1dc6756821ab2aa0befe;hpb=5641dccfbf2a70d589cf094a0d4ed5a10f919f00;p=nihav.git diff --git a/nihav-core/src/io/intcode.rs b/nihav-core/src/io/intcode.rs index 8d87920..a8437e8 100644 --- a/nihav-core/src/io/intcode.rs +++ b/nihav-core/src/io/intcode.rs @@ -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 { 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 { 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),