grep: keep invalid UTF-8 text under -I (#49)

This commit is contained in:
Wondr
2026-06-05 16:06:49 +02:00
committed by GitHub
parent abcdd7e84d
commit 7c79cb4e3a
2 changed files with 16 additions and 1 deletions
+3 -1
View File
@@ -493,7 +493,9 @@ impl<'a> Searcher<'a> {
if let Some(positions) = self.session_match_line(line) {
// TODO: GNU grep respects LANG. Here, I'm always checking for valid UTF-8.
if !self.session_mark_binary_if(|| std::str::from_utf8(line).is_err()) {
if self.config.binary_mode != BinaryMode::WithoutMatch
&& !self.session_mark_binary_if(|| std::str::from_utf8(line).is_err())
{
return Ok(false);
}
+13
View File
@@ -939,6 +939,7 @@ fn binary_files_text_forces_text_mode() {
fn binary_files_without_match_skips() {
let (scene, _) = ucmd();
scene.fixtures.write_bytes("b", b"hit\0more\n");
scene.fixtures.write_bytes("invalid", b"a\x9db\n");
let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
c.args(&["-I", "hit", "b"]).fails_with_code(1).no_output();
@@ -947,6 +948,18 @@ fn binary_files_without_match_skips() {
c.args(&["--binary-files=without-match", "hit", "b"])
.fails_with_code(1)
.no_output();
let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
c.args(&["-I", "a", "invalid"])
.succeeds()
.stdout_is_bytes(b"a\x9db\n")
.no_stderr();
let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
c.args(&["--binary-files=without-match", "a", "invalid"])
.succeeds()
.stdout_is_bytes(b"a\x9db\n")
.no_stderr();
}
fn build_tree(scene: &TestScenario) {