From cbab8677e672f562db1b990426d31dc853cb00f6 Mon Sep 17 00:00:00 2001 From: Sebastian Bentmar Holgersson Date: Tue, 3 Jan 2023 18:23:28 +0000 Subject: [PATCH] expand: improve handing of + --- src/uu/expand/src/expand.rs | 18 +++++----- tests/by-util/test_expand.rs | 66 ++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index cbb5593f9..8ba9d74db 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -38,7 +38,7 @@ static DEFAULT_TABSTOP: usize = 8; /// The mode to use when replacing tabs beyond the last one specified in /// the `--tabs` argument. -#[derive(PartialEq)] +#[derive(PartialEq, Debug)] enum RemainingMode { None, Slash, @@ -126,12 +126,8 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec), ParseError> { let bytes = word.as_bytes(); for i in 0..bytes.len() { match bytes[i] { - b'+' => { - remaining_mode = RemainingMode::Plus; - } - b'/' => { - remaining_mode = RemainingMode::Slash; - } + b'+' => remaining_mode = RemainingMode::Plus, + b'/' => remaining_mode = RemainingMode::Slash, _ => { // Parse a number from the byte sequence. let s = from_utf8(&bytes[i..]).unwrap(); @@ -190,10 +186,16 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec), ParseError> { // then just use the default tabstops. if nums.is_empty() { nums = vec![DEFAULT_TABSTOP]; + remaining_mode = RemainingMode::None; + } + + if nums.len() == 1 { + remaining_mode = RemainingMode::None; } Ok((remaining_mode, nums)) } +#[derive(Debug)] struct Options { files: Vec, tabstops: Vec, @@ -374,7 +376,7 @@ enum CharType { fn expand(options: &Options) -> std::io::Result<()> { use self::CharType::*; - + println!("{:?}", options); let mut output = BufWriter::new(stdout()); let ts = options.tabstops.as_ref(); let mut buf = Vec::new(); diff --git a/tests/by-util/test_expand.rs b/tests/by-util/test_expand.rs index ac9eb1fad..c4da7503f 100644 --- a/tests/by-util/test_expand.rs +++ b/tests/by-util/test_expand.rs @@ -275,7 +275,7 @@ fn test_tabs_shortcut() { .args(&["-2", "-5", "-7"]) .pipe_in("\ta\tb\tc") .succeeds() - // 01234567890 + // 01234567890 .stdout_is(" a b c"); } @@ -285,7 +285,7 @@ fn test_comma_separated_tabs_shortcut() { .args(&["-2,5", "-7"]) .pipe_in("\ta\tb\tc") .succeeds() - // 01234567890 + // 01234567890 .stdout_is(" a b c"); } @@ -295,6 +295,66 @@ fn test_tabs_and_tabs_shortcut_mixed() { .args(&["-2", "--tabs=5", "-7"]) .pipe_in("\ta\tb\tc") .succeeds() - // 01234567890 + // 01234567890 .stdout_is(" a b c"); } + +#[test] +fn test_ignore_initial_plus() { + new_ucmd!() + .args(&["--tabs=+3"]) + .pipe_in("\ta\tb\tc") + .succeeds() + // 01234567890 + .stdout_is(" a b c"); +} + +#[test] +fn test_ignore_initial_pluses() { + new_ucmd!() + .args(&["--tabs=++3"]) + .pipe_in("\ta\tb\tc") + .succeeds() + // 01234567890 + .stdout_is(" a b c"); +} + +#[test] +fn test_ignore_initial_slash() { + new_ucmd!() + .args(&["--tabs=/3"]) + .pipe_in("\ta\tb\tc") + .succeeds() + // 01234567890 + .stdout_is(" a b c"); +} + +#[test] +fn test_ignore_initial_slashes() { + new_ucmd!() + .args(&["--tabs=//3"]) + .pipe_in("\ta\tb\tc") + .succeeds() + // 01234567890 + .stdout_is(" a b c"); +} + +#[test] +fn test_ignore_initial_plus_slash_combination() { + new_ucmd!() + .args(&["--tabs=+/3"]) + .pipe_in("\ta\tb\tc") + .succeeds() + // 01234567890 + .stdout_is(" a b c"); +} + +#[test] +fn test_comma_with_plus_and_multi_character_values() { + new_ucmd!() + .args(&["--tabs=3,+6"]) + .pipe_in("\taaa\tbbbb\tcccc") + .succeeds() + // 01234567890 + .stdout_is(" aaa bbb ccc"); +}