Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed incorrect behaviour on str impl. #96

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,18 +666,33 @@ impl<'a> TryFromCtx<'a, StrCtx> for &'a str {
fn try_from_ctx(src: &'a [u8], ctx: StrCtx) -> Result<(Self, usize), Self::Error> {
let len = match ctx {
StrCtx::Length(len) => len,
StrCtx::Delimiter(delimiter) => src.iter().take_while(|c| **c != delimiter).count(),
StrCtx::Delimiter(delimiter) => {
match src.iter().copied().position(|c| c == delimiter) {
Some(str_len) => str_len,
None => {
return Err(error::Error::BadInput {
size: 0,
msg: "No delimiter was found.",
})
}
}
}
StrCtx::DelimiterUntil(delimiter, len) => {
if len > src.len() {
return Err(error::Error::TooBig {
size: len,
len: src.len(),
});
};
src.iter()
.take_while(|c| **c != delimiter)
.take(len)
.count()
match src.iter().copied().position(|c| c == delimiter) {
Some(str_len) => str_len.min(len),
None => {
return Err(error::Error::BadInput {
size: 0,
msg: "No delimiter was found.",
})
}
}
}
};

Expand Down Expand Up @@ -750,6 +765,12 @@ macro_rules! sizeof_impl {
size_of::<$ty>()
}
}
impl SizeWith for $ty {
#[inline]
fn size_with(_ctx: &()) -> usize {
size_of::<$ty>()
}
}
};
}

Expand Down Expand Up @@ -830,6 +851,11 @@ impl<Ctx: Copy, T: TryIntoCtx<Ctx, Error = error::Error>, const N: usize> TryInt
Ok(offset)
}
}
impl<Ctx, T: SizeWith<Ctx>, const N: usize> SizeWith<Ctx> for [T; N] {
fn size_with(ctx: &Ctx) -> usize {
T::size_with(ctx) * N
}
}

#[cfg(feature = "std")]
impl<'a> TryFromCtx<'a> for &'a CStr {
Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ mod tests {
let error = bytes.pread_with::<&str>(7, StrCtx::Delimiter(SPACE));
#[cfg(feature = "std")]
println!("{error:?}");
assert!(error.is_ok());
assert!(error.is_err());
}

/// In this test, we are testing preading
Expand All @@ -390,7 +390,7 @@ mod tests {
let hello_world = bytes.pread_with::<&str>(0, StrCtx::Delimiter(NULL));
#[cfg(feature = "std")]
println!("1 {hello_world:?}");
assert!(hello_world.unwrap().is_empty());
assert!(hello_world.is_err());
let error = bytes.pread_with::<&str>(7, StrCtx::Delimiter(SPACE));
#[cfg(feature = "std")]
println!("2 {error:?}");
Expand Down Expand Up @@ -421,10 +421,8 @@ mod tests {
.pread_with::<&str>(0, StrCtx::Delimiter(NULL))
.unwrap();
assert_eq!(more, "more");
let bytes = bytes
.pread_with::<&str>(more.len() + 1, StrCtx::Delimiter(NULL))
.unwrap();
assert_eq!(bytes, "bytes");
let result = bytes.pread_with::<&str>(more.len() + 1, StrCtx::Delimiter(NULL));
assert!(result.is_err());
}

use core::fmt::{self, Display};
Expand Down
7 changes: 6 additions & 1 deletion tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::ops::{Deref, DerefMut};

use scroll::ctx::SizeWith as _;
use scroll::{ctx, Cread, Pread, Result};
use scroll::{ctx, Cread, Endian, Pread, Result};

#[derive(Default)]
pub struct Section<'a> {
Expand Down Expand Up @@ -376,3 +376,8 @@ fn test_fixed_array_rw() {
buf.pwrite([0x1337u16, 0x1337], 0).unwrap();
assert_eq!(buf, bytes);
}

#[test]
fn test_fixed_array_size_with() {
assert_eq!(<[u32; 3]>::size_with(&Endian::Little), 12);
}
Loading