diff --git a/Cargo.lock b/Cargo.lock index e87e1f3..15c8473 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -703,6 +703,7 @@ dependencies = [ "ctor", "fancy-regex", "libc", + "memchr", "memmap2", "phf", "phf_codegen", @@ -896,6 +897,7 @@ version = "0.0.1" dependencies = [ "clap", "fancy-regex", + "memchr", "memmap2", "regex", "tempfile", diff --git a/Cargo.toml b/Cargo.toml index 871f72c..61a7d8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ clap_complete = "4.5" clap_mangen = "0.2" fancy-regex = "0.14.0" libc = "0.2.153" +memchr = "2.7.4" memmap2 = "0.9" phf = "0.11.2" phf_codegen = "0.11.2" @@ -56,6 +57,7 @@ clap_complete = { workspace = true } clap_mangen = { workspace = true } ctor = "0.4.1" fancy-regex = { workspace = true } +memchr = { workspace = true } memmap2.workspace = true phf = { workspace = true } sed = { optional = true, version = "0.0.1", package = "uu_sed", path = "src/uu/sed" } diff --git a/src/uu/sed/Cargo.toml b/src/uu/sed/Cargo.toml index 6d94616..8487ab2 100644 --- a/src/uu/sed/Cargo.toml +++ b/src/uu/sed/Cargo.toml @@ -15,6 +15,7 @@ categories = ["command-line-utilities"] [dependencies] clap = { workspace = true } fancy-regex = { workspace = true } +memchr = { workspace = true } regex = { workspace = true } tempfile = { workspace = true } memmap2 = { workspace = true } diff --git a/src/uu/sed/src/fast_io.rs b/src/uu/sed/src/fast_io.rs index 36bb53e..1cc7e12 100644 --- a/src/uu/sed/src/fast_io.rs +++ b/src/uu/sed/src/fast_io.rs @@ -14,6 +14,7 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +use memchr::memchr; #[cfg(unix)] use memmap2::Mmap; @@ -69,10 +70,12 @@ impl<'a> MmapLineCursor<'a> { } let start = self.pos; - let mut end = start; - while end < self.data.len() && self.data[end] != b'\n' { - end += 1; - } + + let mut end = if let Some(pos) = memchr(b'\n', &self.data[start..]) { + pos + start + } else { + self.data.len() + }; if end < self.data.len() { end += 1; // include \n in full span