diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index cdf3185e41a..195dfbf636b 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -91,6 +91,16 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec), ParseError> { // Parse a number from the byte sequence. let s = from_utf8(&bytes[i..]).unwrap(); match s.parse::() { + // After a tab list, `+0` or `/0` sets no tab size yet, + // as in GNU: a later number still gives one, and if + // none does the list is used as if it were not there. + Ok(0) + if remaining_mode != RemainingMode::None + && !nums.is_empty() + && !is_specifier_already_used => + { + break; + } // Tab size must be positive. Ok(0) => return Err(ParseError::TabSizeCannotBeZero), // Tab stops must be ascending. A number after `/` or `+` is a @@ -139,6 +149,11 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec), ParseError> { } } } + + // A `+0` or `/0` that no later number replaced leaves the list as given. + if !is_specifier_already_used { + remaining_mode = RemainingMode::None; + } // If no numbers could be parsed (for example, if `s` were "+,+,+"), // then just use the default tabstops. if nums.is_empty() { diff --git a/tests/by-util/test_expand.rs b/tests/by-util/test_expand.rs index 901b78f2647..dd7eb65ba43 100644 --- a/tests/by-util/test_expand.rs +++ b/tests/by-util/test_expand.rs @@ -546,3 +546,26 @@ fn test_multibyte_char_split_across_reads() { .succeeds() .stdout_only("\u{e9} z\n"); } + +#[test] +fn test_zero_increment_after_a_tab_list() { + // As in GNU, `+0` and `/0` after a tab list set no tab size: the list is + // used as given, unless a later number supplies the size. + for (list, expected) in [ + ("4,+0", "a b c d e\n"), + ("4,/0", "a b c d e\n"), + ("2,6,+0", "a b c d e\n"), + ("2,6,/0", "a b c d e\n"), + ("4,+0,8", "a b c d e\n"), + ("4,+0,+2", "a b c d e\n"), + ("4,/0,10", "a b c d e\n"), + // A trailing `+` without a number sets no tab size either. + ("2,6,+", "a b c d e\n"), + ] { + new_ucmd!() + .args(&["-t", list]) + .pipe_in("a\tb\tc\td\te\n") + .succeeds() + .stdout_only(expected); + } +}