From 4cf18e96f3113c07d179fee2bf966bc933e3ab80 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Mon, 31 May 2021 21:19:19 +0200 Subject: [PATCH] seq: change default value for -t and remove dubious escape sequences GNU seq does not support -t, but always outputs a newline at the end. Therefore, our default for -t should be \n. Also removes support for escape sequences (interpreting a literal "\n" as a newline). This is not what GNU seq is doing, and unexpected. --- src/uu/seq/src/seq.rs | 32 ++++++++++---------------------- tests/by-util/test_seq.rs | 12 ++++++++++++ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/uu/seq/src/seq.rs b/src/uu/seq/src/seq.rs index 4737fe872..bdab044c5 100644 --- a/src/uu/seq/src/seq.rs +++ b/src/uu/seq/src/seq.rs @@ -34,7 +34,7 @@ fn get_usage() -> String { #[derive(Clone)] struct SeqOptions { separator: String, - terminator: Option, + terminator: String, widths: bool, } @@ -81,10 +81,6 @@ impl FromStr for Number { } } -fn escape_sequences(s: &str) -> String { - s.replace("\\n", "\n").replace("\\t", "\t") -} - pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); let matches = App::new(executable!()) @@ -104,7 +100,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { Arg::with_name(OPT_TERMINATOR) .short("t") .long("terminator") - .help("Terminator character (defaults to separator)") + .help("Terminator character (defaults to \\n)") .takes_value(true) .number_of_values(1), ) @@ -126,14 +122,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let numbers = matches.values_of(ARG_NUMBERS).unwrap().collect::>(); - let mut options = SeqOptions { - separator: "\n".to_owned(), - terminator: None, - widths: false, + let options = SeqOptions { + separator: matches.value_of(OPT_SEPARATOR).unwrap_or("\n").to_string(), + terminator: matches.value_of(OPT_TERMINATOR).unwrap_or("\n").to_string(), + widths: matches.is_present(OPT_WIDTHS), }; - options.separator = matches.value_of(OPT_SEPARATOR).unwrap_or("\n").to_string(); - options.terminator = matches.value_of(OPT_TERMINATOR).map(String::from); - options.widths = matches.is_present(OPT_WIDTHS); let mut largest_dec = 0; let mut padding = 0; @@ -187,11 +180,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 { if largest_dec > 0 { largest_dec -= 1; } - let separator = escape_sequences(&options.separator[..]); - let terminator = match options.terminator { - Some(term) => escape_sequences(&term[..]), - None => separator.clone(), - }; match (first, last, increment) { (Number::BigInt(first), Number::BigInt(last), Number::BigInt(increment)) => { @@ -199,8 +187,8 @@ pub fn uumain(args: impl uucore::Args) -> i32 { first, increment, last, - separator, - terminator, + options.separator, + options.terminator, options.widths, padding, ) @@ -210,8 +198,8 @@ pub fn uumain(args: impl uucore::Args) -> i32 { increment.into_f64(), last.into_f64(), largest_dec, - separator, - terminator, + options.separator, + options.terminator, options.widths, padding, ), diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index 3da1a84ca..98eb23598 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -28,6 +28,18 @@ fn test_separator_and_terminator() { .args(&["-s", ",", "-t", "!", "2", "6"]) .run() .stdout_is("2,3,4,5,6!"); + new_ucmd!() + .args(&["-s", ",", "2", "6"]) + .run() + .stdout_is("2,3,4,5,6\n"); + new_ucmd!() + .args(&["-s", "\n", "2", "6"]) + .run() + .stdout_is("2\n3\n4\n5\n6\n"); + new_ucmd!() + .args(&["-s", "\\n", "2", "6"]) + .run() + .stdout_is("2\\n3\\n4\\n5\\n6\n"); } #[test]