Skip to content
6 changes: 5 additions & 1 deletion src/sed/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,12 @@ fn process_file(
'x' => {
// Exchange the contents of the pattern and hold spaces.
let (pat_content, pat_has_newline) = pattern.fields_mut()?;

// Swap newline if hold space is logically non-empty.
if !context.hold.content.is_empty() || context.hold.has_newline {
std::mem::swap(pat_has_newline, &mut context.hold.has_newline);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment explaining what it is doing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping ? :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hii! so sorry about pushing this off.... I've made the requested changes!

}
std::mem::swap(pat_content, &mut context.hold.content);
std::mem::swap(pat_has_newline, &mut context.hold.has_newline);
}
'y' => {
let trans = extract_variant!(command, Transliteration);
Expand Down
11 changes: 11 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,3 +1195,14 @@ fn test_print_command_adds_newline() {
.succeeds()
.stdout_is("foo\nfoo");
}

////////////////////////////////////////////////////////////
// Test for issue #254: Missing newline in exchanged output with `2x`
#[test]
fn test_exchange_command_adds_newline() {
new_ucmd!()
.args(&["2x"])
.pipe_in("a\nb\nc\n")
.succeeds()
.stdout_is("a\n\nc\n");
}
Loading