1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 08:34:07 +02:00

fix writing unaligned negative numbers

This commit is contained in:
Robin Appelman 2021-07-12 19:18:56 +02:00
commit 62288921ed
3 changed files with 4 additions and 7 deletions

View file

@ -56,7 +56,7 @@
use err_derive::Error;
pub use bitbuffer_derive::{BitRead, BitReadSized};
pub use bitbuffer_derive::{BitRead, BitReadSized, BitWrite, BitWriteSized};
pub use endianness::*;
pub use read::{BitRead, BitReadSized, LazyBitRead, LazyBitReadSized};
pub use readbuffer::BitReadBuffer;

View file

@ -102,6 +102,9 @@ where
fn push_bits(&mut self, bits: usize, count: usize) {
debug_assert!(count < USIZE_BITS - 8);
// ensure there are no stray bits
let bits = bits & (usize::MAX >> count);
let bit_offset = self.bit_len & 7;
let last_written_byte = if bit_offset > 0 {
self.bytes.pop().unwrap_or(0)
@ -176,12 +179,6 @@ where
}
if type_bit_size < USIZE_BITS {
// if T::is_signed() && count < type_bit_size {
// // set
// let sign_bit = T::one() << (count - 1);
// let value = abs(value) | sign_bit;
// }
self.push_bits(value.into_usize_unchecked(), count);
} else {
self.push_non_fit_bits(value.into_bytes(), count)

0
tests/roundtrip.rs Normal file
View file