From 8655cbfa63917bd00ffaff548c4aba8b09cf2b13 Mon Sep 17 00:00:00 2001
From: Arthur Carcano <arthur.carcano@ocamlpro.com>
Date: Mon, 24 Jul 2023 16:09:15 +0200
Subject: [PATCH] Delete architecture-specific memchr code in std::sys

Currently all architecture-specific memchr code is only used in
`std::io`. Most of the actual `memchr` capacity exposed to the user
through the slice API is instead implemented in core::slice::memchr.

Hence this commit deletes memchr from std::sys[_common] and replace
calls to it by calls to core::slice::memchr functions. This deletes
(r)memchr from the list of symbols linked to libc.
---
 library/std/src/io/buffered/linewritershim.rs |  2 +-
 library/std/src/io/mod.rs                     |  2 +-
 library/std/src/sys/hermit/memchr.rs          |  1 -
 library/std/src/sys/hermit/os.rs              |  1 -
 library/std/src/sys/sgx/memchr.rs             |  1 -
 library/std/src/sys/sgx/mod.rs                |  1 -
 library/std/src/sys/solid/memchr.rs           | 21 --------
 library/std/src/sys/solid/mod.rs              |  1 -
 library/std/src/sys/unix/memchr.rs            | 40 ---------------
 library/std/src/sys/unix/mod.rs               |  1 -
 library/std/src/sys/unix/os.rs                |  2 +-
 library/std/src/sys/unsupported/common.rs     |  4 --
 library/std/src/sys/windows/memchr.rs         |  5 --
 library/std/src/sys/windows/mod.rs            |  1 -
 library/std/src/sys_common/memchr.rs          | 51 -------------------
 library/std/src/sys_common/mod.rs             |  1 -
 16 files changed, 3 insertions(+), 132 deletions(-)
 delete mode 100644 library/std/src/sys/hermit/memchr.rs
 delete mode 100644 library/std/src/sys/sgx/memchr.rs
 delete mode 100644 library/std/src/sys/solid/memchr.rs
 delete mode 100644 library/std/src/sys/unix/memchr.rs
 delete mode 100644 library/std/src/sys/windows/memchr.rs
 delete mode 100644 library/std/src/sys_common/memchr.rs

diff --git a/library/std/src/io/buffered/linewritershim.rs b/library/std/src/io/buffered/linewritershim.rs
index f2a55da05b22e..7eadfd413661d 100644
--- a/library/std/src/io/buffered/linewritershim.rs
+++ b/library/std/src/io/buffered/linewritershim.rs
@@ -1,5 +1,5 @@
 use crate::io::{self, BufWriter, IoSlice, Write};
-use crate::sys_common::memchr;
+use core::slice::memchr;
 
 /// Private helper struct for implementing the line-buffered writing logic.
 /// This shim temporarily wraps a BufWriter, and uses its internals to
diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs
index 5c1d2d8f46cd4..bbab2cc20ff49 100644
--- a/library/std/src/io/mod.rs
+++ b/library/std/src/io/mod.rs
@@ -258,7 +258,7 @@ use crate::ops::{Deref, DerefMut};
 use crate::slice;
 use crate::str;
 use crate::sys;
-use crate::sys_common::memchr;
+use core::slice::memchr;
 
 #[stable(feature = "bufwriter_into_parts", since = "1.56.0")]
 pub use self::buffered::WriterPanicked;
diff --git a/library/std/src/sys/hermit/memchr.rs b/library/std/src/sys/hermit/memchr.rs
deleted file mode 100644
index 9967482197eb3..0000000000000
--- a/library/std/src/sys/hermit/memchr.rs
+++ /dev/null
@@ -1 +0,0 @@
-pub use core::slice::memchr::{memchr, memrchr};
diff --git a/library/std/src/sys/hermit/os.rs b/library/std/src/sys/hermit/os.rs
index e53dbae611981..6fbbc330d2578 100644
--- a/library/std/src/sys/hermit/os.rs
+++ b/library/std/src/sys/hermit/os.rs
@@ -9,7 +9,6 @@ use crate::path::{self, PathBuf};
 use crate::str;
 use crate::sync::Mutex;
 use crate::sys::hermit::abi;
-use crate::sys::memchr;
 use crate::sys::unsupported;
 use crate::vec;
 
diff --git a/library/std/src/sys/sgx/memchr.rs b/library/std/src/sys/sgx/memchr.rs
deleted file mode 100644
index 9967482197eb3..0000000000000
--- a/library/std/src/sys/sgx/memchr.rs
+++ /dev/null
@@ -1 +0,0 @@
-pub use core::slice::memchr::{memchr, memrchr};
diff --git a/library/std/src/sys/sgx/mod.rs b/library/std/src/sys/sgx/mod.rs
index 9865a945bad1d..ef1f077239d47 100644
--- a/library/std/src/sys/sgx/mod.rs
+++ b/library/std/src/sys/sgx/mod.rs
@@ -21,7 +21,6 @@ pub mod fd;
 pub mod fs;
 #[path = "../unsupported/io.rs"]
 pub mod io;
-pub mod memchr;
 pub mod net;
 pub mod os;
 #[path = "../unix/os_str.rs"]
diff --git a/library/std/src/sys/solid/memchr.rs b/library/std/src/sys/solid/memchr.rs
deleted file mode 100644
index 452b7a3de1b33..0000000000000
--- a/library/std/src/sys/solid/memchr.rs
+++ /dev/null
@@ -1,21 +0,0 @@
-pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
-    let p = unsafe {
-        libc::memchr(
-            haystack.as_ptr() as *const libc::c_void,
-            needle as libc::c_int,
-            haystack.len(),
-        )
-    };
-    if p.is_null() { None } else { Some(p as usize - (haystack.as_ptr() as usize)) }
-}
-
-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
-    let p = unsafe {
-        libc::memrchr(
-            haystack.as_ptr() as *const libc::c_void,
-            needle as libc::c_int,
-            haystack.len(),
-        )
-    };
-    if p.is_null() { None } else { Some(p as usize - (haystack.as_ptr() as usize)) }
-}
diff --git a/library/std/src/sys/solid/mod.rs b/library/std/src/sys/solid/mod.rs
index 923d27fd9369d..373e4b7073165 100644
--- a/library/std/src/sys/solid/mod.rs
+++ b/library/std/src/sys/solid/mod.rs
@@ -40,7 +40,6 @@ pub mod pipe;
 pub mod process;
 pub mod stdio;
 pub use self::itron::thread;
-pub mod memchr;
 pub mod thread_local_dtor;
 pub mod thread_local_key;
 pub use self::itron::thread_parking;
diff --git a/library/std/src/sys/unix/memchr.rs b/library/std/src/sys/unix/memchr.rs
deleted file mode 100644
index 73ba604eccba2..0000000000000
--- a/library/std/src/sys/unix/memchr.rs
+++ /dev/null
@@ -1,40 +0,0 @@
-// Original implementation taken from rust-memchr.
-// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
-
-pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
-    let p = unsafe {
-        libc::memchr(
-            haystack.as_ptr() as *const libc::c_void,
-            needle as libc::c_int,
-            haystack.len(),
-        )
-    };
-    if p.is_null() { None } else { Some(p.addr() - haystack.as_ptr().addr()) }
-}
-
-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
-    #[cfg(target_os = "linux")]
-    fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
-        // GNU's memrchr() will - unlike memchr() - error if haystack is empty.
-        if haystack.is_empty() {
-            return None;
-        }
-        let p = unsafe {
-            libc::memrchr(
-                haystack.as_ptr() as *const libc::c_void,
-                needle as libc::c_int,
-                haystack.len(),
-            )
-        };
-        // FIXME: this should *likely* use `offset_from`, but more
-        // investigation is needed (including running tests in miri).
-        if p.is_null() { None } else { Some(p.addr() - haystack.as_ptr().addr()) }
-    }
-
-    #[cfg(not(target_os = "linux"))]
-    fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
-        core::slice::memchr::memrchr(needle, haystack)
-    }
-
-    memrchr_specific(needle, haystack)
-}
diff --git a/library/std/src/sys/unix/mod.rs b/library/std/src/sys/unix/mod.rs
index 326f1481e1918..134859af183ac 100644
--- a/library/std/src/sys/unix/mod.rs
+++ b/library/std/src/sys/unix/mod.rs
@@ -24,7 +24,6 @@ pub mod kernel_copy;
 #[cfg(target_os = "l4re")]
 mod l4re;
 pub mod locks;
-pub mod memchr;
 #[cfg(not(target_os = "l4re"))]
 pub mod net;
 #[cfg(target_os = "l4re")]
diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs
index a68c14758ff79..24c403ef28ca7 100644
--- a/library/std/src/sys/unix/os.rs
+++ b/library/std/src/sys/unix/os.rs
@@ -21,8 +21,8 @@ use crate::sync::{PoisonError, RwLock};
 use crate::sys::common::small_c_string::{run_path_with_cstr, run_with_cstr};
 use crate::sys::cvt;
 use crate::sys::fd;
-use crate::sys::memchr;
 use crate::vec;
+use core::slice::memchr;
 
 #[cfg(all(target_env = "gnu", not(target_os = "vxworks")))]
 use crate::sys::weak::weak;
diff --git a/library/std/src/sys/unsupported/common.rs b/library/std/src/sys/unsupported/common.rs
index 5cd9e57de19ee..bb760e16c1647 100644
--- a/library/std/src/sys/unsupported/common.rs
+++ b/library/std/src/sys/unsupported/common.rs
@@ -1,9 +1,5 @@
 use crate::io as std_io;
 
-pub mod memchr {
-    pub use core::slice::memchr::{memchr, memrchr};
-}
-
 // SAFETY: must be called only once during runtime initialization.
 // NOTE: this is not guaranteed to run, for example when Rust code is called externally.
 pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {}
diff --git a/library/std/src/sys/windows/memchr.rs b/library/std/src/sys/windows/memchr.rs
deleted file mode 100644
index b9e5bcc1b4bbd..0000000000000
--- a/library/std/src/sys/windows/memchr.rs
+++ /dev/null
@@ -1,5 +0,0 @@
-// Original implementation taken from rust-memchr.
-// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
-
-// Fallback memchr is fastest on Windows.
-pub use core::slice::memchr::{memchr, memrchr};
diff --git a/library/std/src/sys/windows/mod.rs b/library/std/src/sys/windows/mod.rs
index bcc172b0fae36..64fd156917be6 100644
--- a/library/std/src/sys/windows/mod.rs
+++ b/library/std/src/sys/windows/mod.rs
@@ -21,7 +21,6 @@ pub mod fs;
 pub mod handle;
 pub mod io;
 pub mod locks;
-pub mod memchr;
 pub mod net;
 pub mod os;
 pub mod os_str;
diff --git a/library/std/src/sys_common/memchr.rs b/library/std/src/sys_common/memchr.rs
deleted file mode 100644
index b219e87891264..0000000000000
--- a/library/std/src/sys_common/memchr.rs
+++ /dev/null
@@ -1,51 +0,0 @@
-// Original implementation taken from rust-memchr.
-// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
-
-use crate::sys::memchr as sys;
-
-#[cfg(test)]
-mod tests;
-
-/// A safe interface to `memchr`.
-///
-/// Returns the index corresponding to the first occurrence of `needle` in
-/// `haystack`, or `None` if one is not found.
-///
-/// memchr reduces to super-optimized machine code at around an order of
-/// magnitude faster than `haystack.iter().position(|&b| b == needle)`.
-/// (See benchmarks.)
-///
-/// # Examples
-///
-/// This shows how to find the first position of a byte in a byte string.
-///
-/// ```ignore (cannot-doctest-private-modules)
-/// use memchr::memchr;
-///
-/// let haystack = b"the quick brown fox";
-/// assert_eq!(memchr(b'k', haystack), Some(8));
-/// ```
-#[inline]
-pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
-    sys::memchr(needle, haystack)
-}
-
-/// A safe interface to `memrchr`.
-///
-/// Returns the index corresponding to the last occurrence of `needle` in
-/// `haystack`, or `None` if one is not found.
-///
-/// # Examples
-///
-/// This shows how to find the last position of a byte in a byte string.
-///
-/// ```ignore (cannot-doctest-private-modules)
-/// use memchr::memrchr;
-///
-/// let haystack = b"the quick brown fox";
-/// assert_eq!(memrchr(b'o', haystack), Some(17));
-/// ```
-#[inline]
-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
-    sys::memrchr(needle, haystack)
-}
diff --git a/library/std/src/sys_common/mod.rs b/library/std/src/sys_common/mod.rs
index e9c727cbbd122..d9497a31c6fbc 100644
--- a/library/std/src/sys_common/mod.rs
+++ b/library/std/src/sys_common/mod.rs
@@ -24,7 +24,6 @@ pub mod backtrace;
 pub mod fs;
 pub mod io;
 pub mod lazy_box;
-pub mod memchr;
 pub mod once;
 pub mod process;
 pub mod thread;