mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 16:44:06 +02:00
add feature to disable checking strings for valid utf8
This commit is contained in:
parent
1c82edd402
commit
bb54b1c917
2 changed files with 68 additions and 48 deletions
|
|
@ -17,4 +17,7 @@ bitstream_reader_derive = { version = "0.4", path = "bitstream_reader_derive" }
|
|||
[dev-dependencies]
|
||||
maplit = "1.0.1"
|
||||
|
||||
[features]
|
||||
unchecked_utf8 = []
|
||||
|
||||
[workspace]
|
||||
|
|
@ -8,10 +8,10 @@ use std::rc::Rc;
|
|||
|
||||
use num_traits::{Float, PrimInt};
|
||||
|
||||
use crate::{ReadError, Result};
|
||||
use crate::endianness::Endianness;
|
||||
use crate::is_signed::IsSigned;
|
||||
use crate::unchecked_primitive::{UncheckedPrimitiveFloat, UncheckedPrimitiveInt};
|
||||
use crate::{ReadError, Result};
|
||||
|
||||
const USIZE_SIZE: usize = size_of::<usize>();
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ const USIZE_SIZE: usize = size_of::<usize>();
|
|||
/// # }
|
||||
/// ```
|
||||
pub struct BitBuffer<E>
|
||||
where
|
||||
where
|
||||
E: Endianness,
|
||||
{
|
||||
bytes: Rc<Vec<u8>>,
|
||||
|
|
@ -45,7 +45,7 @@ where
|
|||
}
|
||||
|
||||
impl<E> BitBuffer<E>
|
||||
where
|
||||
where
|
||||
E: Endianness,
|
||||
{
|
||||
/// Create a new BitBuffer from a byte vector
|
||||
|
|
@ -73,7 +73,7 @@ where
|
|||
}
|
||||
|
||||
impl<E> BitBuffer<E>
|
||||
where
|
||||
where
|
||||
E: Endianness,
|
||||
{
|
||||
/// The available number of bits in the buffer
|
||||
|
|
@ -349,6 +349,12 @@ where
|
|||
///
|
||||
/// You can either read a fixed number of bytes, or a dynamic length null-terminated string
|
||||
///
|
||||
/// # Features
|
||||
///
|
||||
/// To disable the overhead of checking if the read bytes are valid you can enable the `unchecked_utf8`
|
||||
/// feature of the crate to use `String::from_utf8_unchecked` instead of `String::from_utf8`
|
||||
/// to create the string from the read bytes.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// - [`ReadError::NotEnoughData`]: not enough bits available in the buffer
|
||||
|
|
@ -381,19 +387,32 @@ where
|
|||
/// [`ReadError::NotEnoughData`]: enum.ReadError.html#variant.NotEnoughData
|
||||
/// [`ReadError::Utf8Error`]: enum.ReadError.html#variant.Utf8Error
|
||||
pub fn read_string(&self, position: usize, byte_len: Option<usize>) -> Result<String> {
|
||||
let bytes = self.read_string_bytes(position, byte_len)?;
|
||||
let raw_string = String::from_utf8(bytes)?;
|
||||
if byte_len.is_some() {
|
||||
Ok(raw_string.trim_end_matches(char::from(0)).to_owned())
|
||||
match byte_len {
|
||||
Some(byte_len) => {
|
||||
let bytes = self.read_bytes(position, byte_len)?;
|
||||
let raw_string = if cfg!(feature = "unchecked_utf8") {
|
||||
unsafe {
|
||||
String::from_utf8_unchecked(bytes)
|
||||
}
|
||||
} else {
|
||||
Ok(raw_string)
|
||||
String::from_utf8(bytes)?
|
||||
};
|
||||
Ok(raw_string.trim_end_matches(char::from(0)).to_owned())
|
||||
},
|
||||
None => {
|
||||
let bytes = self.read_string_bytes(position);
|
||||
if cfg!(feature = "unchecked_utf8") {
|
||||
unsafe {
|
||||
Ok(String::from_utf8_unchecked(bytes))
|
||||
}
|
||||
} else {
|
||||
String::from_utf8(bytes).map_err(ReadError::from)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_string_bytes(&self, position: usize, byte_len: Option<usize>) -> Result<Vec<u8>> {
|
||||
match byte_len {
|
||||
Some(len) => return self.read_bytes(position, len),
|
||||
None => {
|
||||
fn read_string_bytes(&self, position: usize) -> Vec<u8> {
|
||||
let mut acc = Vec::with_capacity(25);
|
||||
let mut pos = position;
|
||||
loop {
|
||||
|
|
@ -409,15 +428,13 @@ where
|
|||
let byte = if E::is_le() { bytes[i] } else { bytes[1 + i] };
|
||||
|
||||
if byte == 0 {
|
||||
return Ok(acc);
|
||||
return acc;
|
||||
}
|
||||
acc.push(byte);
|
||||
}
|
||||
pos += read;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Read a sequence of bits from the buffer as float
|
||||
///
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue