From de7ecdd13776b687ae435f499fe500a35afc8b73 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Sun, 9 May 2021 01:03:47 +0300 Subject: [PATCH] Replace other `.position()` patterns with memchr This makes `memchr` a non-optional dependency, due to its use in `read_string` method of `pod::Bytes`. --- Cargo.toml | 4 ++-- src/pod.rs | 2 +- src/read/coff/section.rs | 2 +- src/read/coff/symbol.rs | 4 ++-- src/read/macho/section.rs | 4 ++-- src/read/macho/segment.rs | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 09c02f23..1c8c1543 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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. @@ -59,7 +59,7 @@ unaligned = [] #======================================= # File format features. -archive = ["memchr"] +archive = [] coff = [] elf = [] macho = [] diff --git a/src/pod.rs b/src/pod.rs index 41b2c4d9..d745a1fa 100644 --- a/src/pod.rs +++ b/src/pod.rs @@ -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)?; diff --git a/src/read/coff/section.rs b/src/read/coff/section.rs index bcd20cf3..4a1667a6 100644 --- a/src/read/coff/section.rs +++ b/src/read/coff/section.rs @@ -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[..], } diff --git a/src/read/coff/symbol.rs b/src/read/coff/symbol.rs index 9dc3ba52..5e827342 100644 --- a/src/read/coff/symbol.rs +++ b/src/read/coff/symbol.rs @@ -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[..], }) @@ -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[..], }) diff --git a/src/read/macho/section.rs b/src/read/macho/section.rs index 20385039..7862a354 100644 --- a/src/read/macho/section.rs +++ b/src/read/macho/section.rs @@ -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) => §name[..end], None => sectname, } @@ -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, } diff --git a/src/read/macho/segment.rs b/src/read/macho/segment.rs index c08fe871..6686c101 100644 --- a/src/read/macho/segment.rs +++ b/src/read/macho/segment.rs @@ -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, }