Skip to content

Commit 8613f3e

Browse files
committed
Fix delete command pattern printing
Add pattern_deleted flag to ProcessingContext to prevent automatic pattern printing after 'd', 'D', and 'c' commands.
1 parent 5e9b821 commit 8613f3e

4 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/sed/command.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ pub struct ProcessingContext {
7070
pub substitution_made: bool,
7171
/// Elements to append at the end of each command processing cycle
7272
pub append_elements: Vec<AppendElement>,
73+
/// True if a delete command was executed (prevents automatic printing)
74+
pub pattern_deleted: bool,
7375
}
7476

7577
#[derive(Clone, Debug)]

src/sed/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ fn build_context(matches: &ArgMatches) -> ProcessingContext {
217217
range_commands: Vec::new(),
218218
substitution_made: false,
219219
append_elements: Vec::new(),
220+
pattern_deleted: false,
220221
}
221222
}
222223

src/sed/processor.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ fn process_file(
435435
'lines: while let Some(mut pattern) = reader.get_line()? {
436436
context.line_number += 1;
437437
context.substitution_made = false;
438+
context.pattern_deleted = false;
438439
// Set the script command from which to start.
439440
let mut current: Option<Rc<RefCell<Command>>> =
440441
if let Some(action) = context.input_action.take() {
@@ -494,6 +495,7 @@ fn process_file(
494495
// At range end replace pattern space with text and
495496
// start the next cycle.
496497
pattern.clear();
498+
context.pattern_deleted = true;
497499
if command.addr2.is_none() || context.last_address || reader.last_line()? {
498500
let text = extract_variant!(command, Text);
499501
output.write_str(text.as_ref())?;
@@ -503,6 +505,7 @@ fn process_file(
503505
'd' => {
504506
// Delete the pattern space and start the next cycle.
505507
pattern.clear();
508+
context.pattern_deleted = true;
506509
break;
507510
}
508511
'D' => {
@@ -515,6 +518,7 @@ fn process_file(
515518
} else {
516519
// Same as d
517520
pattern.clear();
521+
context.pattern_deleted = true;
518522
break;
519523
}
520524
}
@@ -653,7 +657,7 @@ fn process_file(
653657
current = command.next.clone();
654658
}
655659

656-
if !context.quiet {
660+
if !context.quiet && !context.pattern_deleted {
657661
write_chunk(output, context, &pattern)?;
658662
}
659663

tests/by-util/test_sed.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,3 +1232,45 @@ fn test_exchange_command_adds_newline() {
12321232
.succeeds()
12331233
.stdout_is("a\n\nc\n");
12341234
}
1235+
1236+
////////////////////////////////////////////////////////////
1237+
// Test for delete command preventing automatic pattern printing
1238+
#[test]
1239+
fn test_delete_command_prevents_automatic_printing() {
1240+
// Test 'd' command - delete line 2
1241+
new_ucmd!()
1242+
.args(&["2d"])
1243+
.pipe_in("line1\nline2\nline3")
1244+
.succeeds()
1245+
.stdout_is("line1\nline3");
1246+
}
1247+
1248+
#[test]
1249+
fn test_delete_range_prevents_automatic_printing() {
1250+
// Test 'd' command on range - delete lines 2-3
1251+
new_ucmd!()
1252+
.args(&["2,3d"])
1253+
.pipe_in("line1\nline2\nline3\nline4")
1254+
.succeeds()
1255+
.stdout_is("line1\nline4");
1256+
}
1257+
1258+
#[test]
1259+
fn test_change_command_prevents_automatic_printing() {
1260+
// Test 'c' command - change line 2
1261+
new_ucmd!()
1262+
.args(&["2c\\replaced"])
1263+
.pipe_in("line1\nline2\nline3")
1264+
.succeeds()
1265+
.stdout_is("line1\nreplaced\nline3");
1266+
}
1267+
1268+
#[test]
1269+
fn test_uppercase_delete_prevents_automatic_printing() {
1270+
// Test 'D' command - delete up to newline and restart
1271+
new_ucmd!()
1272+
.args(&["-e", "N", "-e", "D"])
1273+
.pipe_in("line1\nline2\nline3")
1274+
.succeeds()
1275+
.stdout_is("line3\n");
1276+
}

0 commit comments

Comments
 (0)