Skip to content

Commit

Permalink
Replace other .position() patterns with memchr
Browse files Browse the repository at this point in the history
This makes `memchr` a non-optional dependency, due to its use in
`read_string` method of `pod::Bytes`.
  • Loading branch information
nagisa committed May 10, 2021
1 parent 119d995 commit de7ecdd
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ crc32fast = { version = "1.2", optional = true }
flate2 = { version = "1", optional = true }
indexmap = { version = "1.1", optional = true }
wasmparser = { version = "0.57", optional = true }
memchr = { version = "2.4", optional = true, default-features = false }
memchr = { version = "2.4", default-features = false }

# Internal feature, only used when building as part of libstd, not part of the
# stable interface of this crate.
Expand Down Expand Up @@ -59,7 +59,7 @@ unaligned = []

#=======================================
# File format features.
archive = ["memchr"]
archive = []
coff = []
elf = []
macho = []
Expand Down
2 changes: 1 addition & 1 deletion src/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl<'data> Bytes<'data> {
/// Reads past the null byte, but doesn't return it.
#[inline]
pub fn read_string(&mut self) -> Result<&'data [u8]> {
match self.0.iter().position(|&x| x == 0) {
match memchr::memchr(b'\0', self.0) {
Some(null) => {
// These will never fail.
let bytes = self.read_bytes(null)?;
Expand Down
2 changes: 1 addition & 1 deletion src/read/coff/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl pe::ImageSectionHeader {
/// Return the raw section name.
pub fn raw_name(&self) -> &[u8] {
let bytes = &self.name;
match bytes.iter().position(|&x| x == 0) {
match memchr::memchr(b'\0', bytes) {
Some(end) => &bytes[..end],
None => &bytes[..],
}
Expand Down
4 changes: 2 additions & 2 deletions src/read/coff/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'data> SymbolTable<'data> {
.read_error("Invalid COFF symbol index")?;
let bytes = bytes_of_slice(entries);
// The name is padded with nulls.
Ok(match bytes.iter().position(|&x| x == 0) {
Ok(match memchr::memchr(b'\0', bytes) {
Some(end) => &bytes[..end],
None => &bytes[..],
})
Expand Down Expand Up @@ -182,7 +182,7 @@ impl pe::ImageSymbol {
.read_error("Invalid COFF symbol name offset")
} else {
// The name is inline and padded with nulls.
Ok(match self.name.iter().position(|&x| x == 0) {
Ok(match memchr::memchr(b'\0', &self.name) {
Some(end) => &self.name[..end],
None => &self.name[..],
})
Expand Down
4 changes: 2 additions & 2 deletions src/read/macho/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ pub trait Section: Debug + Pod {
/// Return the `sectname` bytes up until the null terminator.
fn name(&self) -> &[u8] {
let sectname = &self.sectname()[..];
match sectname.iter().position(|&x| x == 0) {
match memchr::memchr(b'\0', sectname) {
Some(end) => &sectname[..end],
None => sectname,
}
Expand All @@ -254,7 +254,7 @@ pub trait Section: Debug + Pod {
/// Return the `segname` bytes up until the null terminator.
fn segment_name(&self) -> &[u8] {
let segname = &self.segname()[..];
match segname.iter().position(|&x| x == 0) {
match memchr::memchr(b'\0', segname) {
Some(end) => &segname[..end],
None => segname,
}
Expand Down
2 changes: 1 addition & 1 deletion src/read/macho/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub trait Segment: Debug + Pod {
/// Return the `segname` bytes up until the null terminator.
fn name(&self) -> &[u8] {
let segname = &self.segname()[..];
match segname.iter().position(|&x| x == 0) {
match memchr::memchr(b'\0', segname) {
Some(end) => &segname[..end],
None => segname,
}
Expand Down

0 comments on commit de7ecdd

Please sign in to comment.