Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/sed/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,10 @@ fn get_cmd_spec(
n_addr: 2,
handler: compile_empty_command,
}),
'z' if !posix => Ok(CommandSpec {
n_addr: 2,
handler: compile_empty_command,
}),
'l' => Ok(CommandSpec {
n_addr: 2,
handler: compile_number_command,
Expand Down
6 changes: 6 additions & 0 deletions src/sed/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,12 @@ fn process_file(
let trans = extract_variant!(command, Transliteration);
transliterate(&mut pattern, trans)?;
}
'z' => {
// Clear the pattern contents, but preserve newline state
// so automatic printing still emits an empty record.
let (pat_content, _) = pattern.fields_mut()?;
pat_content.clear();
}
':' => {
// Branch target; do nothing.
}
Expand Down
36 changes: 36 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,42 @@ check_output!(
["-e", r"y10\123456789198765432\101", LINES1]
);
check_output!(trans_no_new_line, ["-e", r"y/l/L/", NO_NEW_LINE]);

#[test]
fn pattern_clear_with_z_command() {
new_ucmd!()
.arg("z")
.pipe_in("a\nb\n")
.succeeds()
.stdout_is("\n\n");
}

#[test]
fn pattern_clear_with_z_command_silent_print() {
new_ucmd!()
.args(&["-n", "z;p"])
.pipe_in("a\nb\n")
.succeeds()
.stdout_is("\n\n");
}

#[test]
fn pattern_clear_with_z_preserves_substitution_flag() {
new_ucmd!()
.args(&["-n", "s/a/b/;z;t changed;b;:changed;c\\\nchanged"])
.pipe_in("a\n")
.succeeds()
.stdout_is("changed\n");
}

#[test]
fn pattern_clear_with_z_is_non_posix() {
new_ucmd!()
.args(&["--posix", "z"])
.fails()
.code_is(1)
.stderr_is("sed: <script argument 1>:1:1: error: invalid command code `z'\n");
}
check_output!(trans_newline, ["-e", r"1N;2y/\n/X/", LINES1]);

////////////////////////////////////////////////////////////
Expand Down
Loading