Repository navigation
grep: collapse repeated trailing slashes of a -r operand - #187
Conversation
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 uutils#184
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
| 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'/' { |
There was a problem hiding this comment.
this is exactly the gnulib fts_open loop (2 < len, then 1 < len && v[len - 2] == '/')
please rewrite it in your own way, e.g. with rposition of the last non / byte
| // to one, but keeps inner slashes and a lone `//`. | ||
| let (scene, _) = ucmd(); | ||
| build_tree(&scene); | ||
| for (operand, expected) in [ |
There was a problem hiding this comment.
please also add the .// case from the issue
| #[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 `//`. |
There was a problem hiding this comment.
the lone // case isn't tested, please drop it from the comment
| } | ||
|
|
||
| // Windows joins walked names with `\`, so the exact output differs there. | ||
| #[cfg(unix)] |
There was a problem hiding this comment.
why only unix? tree/sub/ + c.txt should give the same on windows, no?
…he unix-only gate
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
Thanks for your PR |
Fixes #184
With
-r, GNU grep (via fts) collapses a run of trailing slashes on a directory operand to one before it builds the names of the files it finds. uutils grep handed the operand toWalkDirunchanged, andPath::joinadds nothing after a base that already ends in/:process_dirnow trims the trailing run to one slash first, using the sameas_encoded_bytesslicing asstrip_dot_prefix. Like fts it keeps inner slashes (tree///sub//printstree///sub/a.txt) and leaves an operand of two characters or less such as//alone. Which files get searched and the exit status don't change.Tests:
recursive_collapses_repeated_trailing_slashes(tree//sub//,tree/sub///,tree/sub/); fails on main. It is#[cfg(unix)]because Windows joins walked names with\.cargo test,cargo fmt --checkandcargo clippy --all-targets -- -D warningspass locallyI used an AI assistant (Grok Bot) to help write this change. I reviewed the diff, compared the output with GNU grep 3.11 on my machine and ran the tests above.