From d74fb62df7d6b1dfc151f738687e936275adc214 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Sun, 25 Jul 2021 14:25:41 -0400 Subject: [PATCH] tac: support null separator --- src/uu/tac/src/tac.rs | 16 ++++++---------- tests/by-util/test_tac.rs | 9 +++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/uu/tac/src/tac.rs b/src/uu/tac/src/tac.rs index 407228e36..01cf09215 100644 --- a/src/uu/tac/src/tac.rs +++ b/src/uu/tac/src/tac.rs @@ -35,15 +35,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let before = matches.is_present(options::BEFORE); let regex = matches.is_present(options::REGEX); - let separator = match matches.value_of(options::SEPARATOR) { - Some(m) => { - if m.is_empty() { - crash!(1, "separator cannot be empty") - } else { - m.to_owned() - } - } - None => "\n".to_owned(), + let raw_separator = matches.value_of(options::SEPARATOR).unwrap_or("\n"); + let separator = if raw_separator.is_empty() { + "\0" + } else { + raw_separator }; let files: Vec = match matches.values_of(options::FILE) { @@ -51,7 +47,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { None => vec!["-".to_owned()], }; - tac(files, before, regex, &separator[..]) + tac(files, before, regex, separator) } pub fn uu_app() -> App<'static, 'static> { diff --git a/tests/by-util/test_tac.rs b/tests/by-util/test_tac.rs index 5fc88770f..599bc19c7 100644 --- a/tests/by-util/test_tac.rs +++ b/tests/by-util/test_tac.rs @@ -73,3 +73,12 @@ fn test_invalid_input() { fn test_no_line_separators() { new_ucmd!().pipe_in("a").succeeds().stdout_is("a"); } + +#[test] +fn test_null_separator() { + new_ucmd!() + .args(&["-s", ""]) + .pipe_in("a\0b\0") + .succeeds() + .stdout_is("b\0a\0"); +}