mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 16:44:06 +02:00
fix writing unaligned negative numbers
This commit is contained in:
parent
23ed7b0e4a
commit
62288921ed
3 changed files with 4 additions and 7 deletions
|
|
@ -56,7 +56,7 @@
|
||||||
|
|
||||||
use err_derive::Error;
|
use err_derive::Error;
|
||||||
|
|
||||||
pub use bitbuffer_derive::{BitRead, BitReadSized};
|
pub use bitbuffer_derive::{BitRead, BitReadSized, BitWrite, BitWriteSized};
|
||||||
pub use endianness::*;
|
pub use endianness::*;
|
||||||
pub use read::{BitRead, BitReadSized, LazyBitRead, LazyBitReadSized};
|
pub use read::{BitRead, BitReadSized, LazyBitRead, LazyBitReadSized};
|
||||||
pub use readbuffer::BitReadBuffer;
|
pub use readbuffer::BitReadBuffer;
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,9 @@ where
|
||||||
fn push_bits(&mut self, bits: usize, count: usize) {
|
fn push_bits(&mut self, bits: usize, count: usize) {
|
||||||
debug_assert!(count < USIZE_BITS - 8);
|
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 bit_offset = self.bit_len & 7;
|
||||||
let last_written_byte = if bit_offset > 0 {
|
let last_written_byte = if bit_offset > 0 {
|
||||||
self.bytes.pop().unwrap_or(0)
|
self.bytes.pop().unwrap_or(0)
|
||||||
|
|
@ -176,12 +179,6 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
if type_bit_size < USIZE_BITS {
|
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);
|
self.push_bits(value.into_usize_unchecked(), count);
|
||||||
} else {
|
} else {
|
||||||
self.push_non_fit_bits(value.into_bytes(), count)
|
self.push_non_fit_bits(value.into_bytes(), count)
|
||||||
|
|
|
||||||
0
tests/roundtrip.rs
Normal file
0
tests/roundtrip.rs
Normal file
Loading…
Add table
Add a link
Reference in a new issue