Returns a slice of this buffer starting at a certain bit offset. If the offset is byte-aligned the returned buffer is a shallow clone, otherwise a new buffer is allocated and filled with a copy of the bits in the range.
(&self, offset: usize, len: usize)
| 328 | /// If the offset is byte-aligned the returned buffer is a shallow clone, |
| 329 | /// otherwise a new buffer is allocated and filled with a copy of the bits in the range. |
| 330 | pub fn bit_slice(&self, offset: usize, len: usize) -> Self { |
| 331 | if offset % 8 == 0 { |
| 332 | return self.slice_with_length(offset / 8, bit_util::ceil(len, 8)); |
| 333 | } |
| 334 | |
| 335 | let chunks = self.bit_chunks(offset, len); |
| 336 | |
| 337 | let buffer: Vec<u64> = if chunks.remainder_len() > 0 { |
| 338 | chunks.iter().chain(Some(chunks.remainder_bits())).collect() |
| 339 | } else { |
| 340 | chunks.iter().collect() |
| 341 | }; |
| 342 | let mut buffer = Buffer::from_vec(buffer); |
| 343 | // Update length to be byte-aligned |
| 344 | buffer.length = bit_util::ceil(len, 8); |
| 345 | buffer |
| 346 | } |
| 347 | |
| 348 | /// Returns a `BitChunks` instance which can be used to iterate over this buffers bits |
| 349 | /// in larger chunks and starting at arbitrary bit offsets. |