From 5b3569096413660c515969afa415b01e30aaf26f Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:47:45 +0530 Subject: [PATCH 1/2] grep: collapse repeated trailing slashes of a -r operand GNU grep (via fts) trims a run of trailing slashes on a directory operand to one, so `grep -r PAT tree//` prints tree/sub/a.txt. We passed the operand to WalkDir unchanged and printed tree//sub/a.txt. Fixes #184 --- src/searcher.rs | 18 ++++++++++++++++++ tests/test_grep.rs | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/searcher.rs b/src/searcher.rs index 64c4c40..2d980ee 100644 --- a/src/searcher.rs +++ b/src/searcher.rs @@ -156,6 +156,7 @@ impl<'a> Searcher<'a> { start: &Path, strip_root: bool, ) -> ControlFlow<()> { + let start = Self::trim_trailing_slashes(start); let mut walker = WalkDir::new(start) .follow_links(self.config.follow_symlinks) .into_iter(); @@ -723,6 +724,23 @@ impl<'a> Searcher<'a> { Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(bytes) }) } + /// Collapse a run of trailing slashes to one, as GNU grep (via fts) does + /// for a directory operand, so `tree//` walks as `tree/sub/...` rather + /// than `tree//sub/...`. Inner slashes are kept, and so is an operand of + /// two characters or less such as `//`. + fn trim_trailing_slashes(path: &Path) -> &Path { + let bytes = path.as_os_str().as_encoded_bytes(); + let mut len = bytes.len(); + if len > 2 && bytes[len - 1] == b'/' { + while len > 1 && bytes[len - 2] == b'/' { + len -= 1; + } + } + + // SAFETY: We only sliced off ASCII slashes from the end of `path`. + Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(&bytes[..len]) }) + } + fn is_special_file(path: &Path) -> bool { match std::fs::metadata(path) { Ok(m) => { diff --git a/tests/test_grep.rs b/tests/test_grep.rs index 75961f7..d64ce20 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -1555,6 +1555,27 @@ fn recursive_no_file_defaults_to_cwd_not_stdin() { .stdout_contains("a.txt"); } +// Windows joins walked names with `\`, so the exact output differs there. +#[cfg(unix)] +#[test] +fn recursive_collapses_repeated_trailing_slashes() { + // GNU grep (via fts) collapses the trailing slashes of a directory operand + // to one, but keeps inner slashes and a lone `//`. + let (scene, _) = ucmd(); + build_tree(&scene); + for (operand, expected) in [ + ("tree//sub//", "tree//sub/c.txt:grep me\n"), + ("tree/sub///", "tree/sub/c.txt:grep me\n"), + ("tree/sub/", "tree/sub/c.txt:grep me\n"), + ] { + scene + .cmd(env!("CARGO_BIN_EXE_grep")) + .args(&["-r", "grep", operand]) + .succeeds() + .stdout_only(expected); + } +} + #[test] fn recursive_implicit_cwd_strips_dot_prefix() { // `-r` with no path argument searches the implicit ".", so reported paths From 0dd101e6d4421959fd20ef9e0fe037f38e993533 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:52:49 +0530 Subject: [PATCH 2/2] grep: find the trailing slash run with rposition, test .// and drop the unix-only gate --- src/searcher.rs | 21 ++++++++++----------- tests/test_grep.rs | 12 ++++++++---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/searcher.rs b/src/searcher.rs index 2d980ee..1f3aae7 100644 --- a/src/searcher.rs +++ b/src/searcher.rs @@ -724,21 +724,20 @@ impl<'a> Searcher<'a> { Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(bytes) }) } - /// Collapse a run of trailing slashes to one, as GNU grep (via fts) does - /// for a directory operand, so `tree//` walks as `tree/sub/...` rather - /// than `tree//sub/...`. Inner slashes are kept, and so is an operand of - /// two characters or less such as `//`. + /// Reduce a run of trailing slashes on a directory operand to a single + /// slash, as GNU grep does, so `tree//` walks as `tree/sub/...` rather + /// than `tree//sub/...`. Slashes inside the operand are left alone, and + /// so is an operand made only of slashes. fn trim_trailing_slashes(path: &Path) -> &Path { let bytes = path.as_os_str().as_encoded_bytes(); - let mut len = bytes.len(); - if len > 2 && bytes[len - 1] == b'/' { - while len > 1 && bytes[len - 2] == b'/' { - len -= 1; - } - } + let keep = match bytes.iter().rposition(|&b| b != b'/') { + // Keep the last non-slash byte plus at most one slash after it. + Some(last) => bytes.len().min(last + 2), + None => bytes.len(), + }; // SAFETY: We only sliced off ASCII slashes from the end of `path`. - Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(&bytes[..len]) }) + Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(&bytes[..keep]) }) } fn is_special_file(path: &Path) -> bool { diff --git a/tests/test_grep.rs b/tests/test_grep.rs index d64ce20..bc5656d 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -1555,12 +1555,10 @@ fn recursive_no_file_defaults_to_cwd_not_stdin() { .stdout_contains("a.txt"); } -// Windows joins walked names with `\`, so the exact output differs there. -#[cfg(unix)] #[test] fn recursive_collapses_repeated_trailing_slashes() { - // GNU grep (via fts) collapses the trailing slashes of a directory operand - // to one, but keeps inner slashes and a lone `//`. + // GNU grep reduces the trailing slashes of a directory operand to one, + // but keeps the slashes inside it. let (scene, _) = ucmd(); build_tree(&scene); for (operand, expected) in [ @@ -1574,6 +1572,12 @@ fn recursive_collapses_repeated_trailing_slashes() { .succeeds() .stdout_only(expected); } + scene + .cmd(env!("CARGO_BIN_EXE_grep")) + .current_dir(scene.fixtures.plus("tree/sub")) + .args(&["-r", "grep", ".//"]) + .succeeds() + .stdout_only("./c.txt:grep me\n"); } #[test]