Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/uu/expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec<usize>), ParseError> {
// Parse a number from the byte sequence.
let s = from_utf8(&bytes[i..]).unwrap();
match s.parse::<usize>() {
// 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
Expand Down Expand Up @@ -139,6 +149,11 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec<usize>), 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() {
Expand Down
23 changes: 23 additions & 0 deletions tests/by-util/test_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading