1
0
Fork 0
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:
Robin Appelman 2019-03-08 22:28:06 +01:00
commit bb54b1c917
2 changed files with 68 additions and 48 deletions

View file

@ -17,4 +17,7 @@ bitstream_reader_derive = { version = "0.4", path = "bitstream_reader_derive" }
[dev-dependencies] [dev-dependencies]
maplit = "1.0.1" maplit = "1.0.1"
[features]
unchecked_utf8 = []
[workspace] [workspace]

View file

@ -8,10 +8,10 @@ use std::rc::Rc;
use num_traits::{Float, PrimInt}; use num_traits::{Float, PrimInt};
use crate::{ReadError, Result};
use crate::endianness::Endianness; use crate::endianness::Endianness;
use crate::is_signed::IsSigned; use crate::is_signed::IsSigned;
use crate::unchecked_primitive::{UncheckedPrimitiveFloat, UncheckedPrimitiveInt}; use crate::unchecked_primitive::{UncheckedPrimitiveFloat, UncheckedPrimitiveInt};
use crate::{ReadError, Result};
const USIZE_SIZE: usize = size_of::<usize>(); const USIZE_SIZE: usize = size_of::<usize>();
@ -35,7 +35,7 @@ const USIZE_SIZE: usize = size_of::<usize>();
/// # } /// # }
/// ``` /// ```
pub struct BitBuffer<E> pub struct BitBuffer<E>
where where
E: Endianness, E: Endianness,
{ {
bytes: Rc<Vec<u8>>, bytes: Rc<Vec<u8>>,
@ -45,7 +45,7 @@ where
} }
impl<E> BitBuffer<E> impl<E> BitBuffer<E>
where where
E: Endianness, E: Endianness,
{ {
/// Create a new BitBuffer from a byte vector /// Create a new BitBuffer from a byte vector
@ -73,7 +73,7 @@ where
} }
impl<E> BitBuffer<E> impl<E> BitBuffer<E>
where where
E: Endianness, E: Endianness,
{ {
/// The available number of bits in the buffer /// 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 /// 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 /// # Errors
/// ///
/// - [`ReadError::NotEnoughData`]: not enough bits available in the buffer /// - [`ReadError::NotEnoughData`]: not enough bits available in the buffer
@ -381,19 +387,32 @@ where
/// [`ReadError::NotEnoughData`]: enum.ReadError.html#variant.NotEnoughData /// [`ReadError::NotEnoughData`]: enum.ReadError.html#variant.NotEnoughData
/// [`ReadError::Utf8Error`]: enum.ReadError.html#variant.Utf8Error /// [`ReadError::Utf8Error`]: enum.ReadError.html#variant.Utf8Error
pub fn read_string(&self, position: usize, byte_len: Option<usize>) -> Result<String> { pub fn read_string(&self, position: usize, byte_len: Option<usize>) -> Result<String> {
let bytes = self.read_string_bytes(position, byte_len)?; match byte_len {
let raw_string = String::from_utf8(bytes)?; Some(byte_len) => {
if byte_len.is_some() { let bytes = self.read_bytes(position, byte_len)?;
Ok(raw_string.trim_end_matches(char::from(0)).to_owned()) let raw_string = if cfg!(feature = "unchecked_utf8") {
unsafe {
String::from_utf8_unchecked(bytes)
}
} else { } 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>> { fn read_string_bytes(&self, position: usize) -> Vec<u8> {
match byte_len {
Some(len) => return self.read_bytes(position, len),
None => {
let mut acc = Vec::with_capacity(25); let mut acc = Vec::with_capacity(25);
let mut pos = position; let mut pos = position;
loop { loop {
@ -409,15 +428,13 @@ where
let byte = if E::is_le() { bytes[i] } else { bytes[1 + i] }; let byte = if E::is_le() { bytes[i] } else { bytes[1 + i] };
if byte == 0 { if byte == 0 {
return Ok(acc); return acc;
} }
acc.push(byte); acc.push(byte);
} }
pos += read; pos += read;
} }
} }
};
}
/// Read a sequence of bits from the buffer as float /// Read a sequence of bits from the buffer as float
/// ///