mirror of
https://github.com/uutils/sed.git
synced 2026-06-10 16:14:15 -07:00
Preserve new line on exchanges (x) with empty hold spaces. (#259)
* fix: remove newline swap in exchange and swap content only * fix: support non empty hold spaces and newline appended hold spaces * nit: period in comment * nit: useless comment removal * chore: clarify newline swap logic for `x`
This commit is contained in:
@@ -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);
|
||||
}
|
||||
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);
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user