Skip to content

Commit

Permalink
Added string_len_u32 feature flag to use fixed sized string length
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishat Zakirov committed Jun 9, 2024
1 parent 2cbb1d2 commit 09dfdeb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license = "MIT"

[features]
wasm32 = []
string_len_u32 = []

[dependencies]
thiserror = "1"
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<'a> BinaryReader<'a> {

/// Read a length-prefixed `String` from the stream.
pub fn read_string(&mut self) -> Result<String> {
let chars = if cfg!(feature = "wasm32") {
let chars = if cfg!(any(feature = "wasm32", feature = "string_len_u32")) {
let str_len = self.read_u32()?;
let mut chars: Vec<u8> = vec![0; str_len as usize];
self.stream.read_exact(&mut chars)?;
Expand All @@ -118,7 +118,7 @@ impl<'a> BinaryReader<'a> {

/// Read a 7bit encoded length-prefixed `String` from the stream.
pub fn read_7bit_encoded_len_string(&mut self) -> Result<String> {
let chars = if cfg!(feature = "wasm32") {
let chars = if cfg!(any(feature = "wasm32", feature = "string_len_u32")) {
let str_len = self.read_7bit_encoded_u32()?;
let mut chars: Vec<u8> = vec![0; str_len as usize];
self.stream.read_exact(&mut chars)?;
Expand Down Expand Up @@ -345,11 +345,11 @@ impl<'a> BinaryWriter<'a> {
/// Write a length-prefixed `String` to the stream.
///
/// The length of the `String` is written as a `usize`
/// unless the `wasm32` feature is enabled
/// unless the `wasm32` or `string_len_u32` feature is enabled
/// in which case the length is a `u32`.
pub fn write_string<S: AsRef<str>>(&mut self, value: S) -> Result<usize> {
let bytes = value.as_ref().as_bytes();
if cfg!(feature = "wasm32") {
if cfg!(any(feature = "wasm32", feature = "string_len_u32")) {
self.write_u32(bytes.len() as u32)?;
} else {
self.write_usize(bytes.len())?;
Expand All @@ -360,11 +360,11 @@ impl<'a> BinaryWriter<'a> {
/// Write a 7bit encoded length-prefixed `String` to the stream.
///
/// The length of the `String` is written as a 7bit encoded `usize`
/// unless the `wasm32` feature is enabled
/// unless the `wasm32` or `string_len_u32` feature is enabled
/// in which case the length is a 7bit encoded `u32`.
pub fn write_7bit_encoded_len_string<S: AsRef<str>>(&mut self, value: S) -> Result<usize> {
let bytes = value.as_ref().as_bytes();
if cfg!(feature = "wasm32") {
if cfg!(any(feature = "wasm32", feature = "string_len_u32")) {
self.write_7bit_encoded_u32(bytes.len() as u32)?;
} else {
self.write_7bit_encoded_usize(bytes.len())?;
Expand Down
10 changes: 8 additions & 2 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,20 @@ fn borrow_test() -> Result<()> {

#[test]
fn slice_test() -> Result<()> {
let size: usize = if cfg!(any(feature = "wasm32", feature = "string_len_u32")) {
19
} else {
23
};

let mut stream = MemoryStream::new();
let mut writer = BinaryWriter::new(&mut stream, Endian::Big);
writer.write_u32(42)?;
writer.write_string("foo")?;
writer.write_7bit_encoded_len_string("bar")?;
writer.write_char('b')?;

assert_eq!(23, writer.len()?);
assert_eq!(size, writer.len()?);

let buffer: Vec<u8> = stream.into();

Expand All @@ -138,7 +144,7 @@ fn slice_test() -> Result<()> {
let value = reader.read_char()?;
assert_eq!('b', value);

assert_eq!(23, reader.len()?);
assert_eq!(size, reader.len()?);

Ok(())
}
Expand Down

0 comments on commit 09dfdeb

Please sign in to comment.