mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +00:00
slight clarification / refactoring in unexpand
This keeps equivalent functionality but makes the code slightly cleaner. Also added one more test case.
This commit is contained in:
parent
d290c9bdfd
commit
cee1837879
2 changed files with 25 additions and 31 deletions
|
@ -142,14 +142,22 @@ fn next_tabstop(tabstops: &[usize], col: usize) -> Option<usize> {
|
|||
}
|
||||
}
|
||||
|
||||
fn write_tabs(mut output: &mut BufWriter<Stdout>, tabstops: &[usize], mut scol: usize, col: usize) {
|
||||
while let Some(nts) = next_tabstop(tabstops, scol) {
|
||||
if col < scol + nts {
|
||||
break;
|
||||
}
|
||||
fn write_tabs(mut output: &mut BufWriter<Stdout>, tabstops: &[usize],
|
||||
mut scol: usize, col: usize, prevtab: bool, init: bool, amode: bool) {
|
||||
// This conditional establishes the following:
|
||||
// We never turn a single space before a non-blank into
|
||||
// a tab, unless it's at the start of the line.
|
||||
let ai = init || amode;
|
||||
if (ai && !prevtab && col > scol + 1) ||
|
||||
(col > scol && (init || ai && prevtab)) {
|
||||
while let Some(nts) = next_tabstop(tabstops, scol) {
|
||||
if col < scol + nts {
|
||||
break;
|
||||
}
|
||||
|
||||
safe_unwrap!(output.write_all("\t".as_bytes()));
|
||||
scol += nts;
|
||||
safe_unwrap!(output.write_all("\t".as_bytes()));
|
||||
scol += nts;
|
||||
}
|
||||
}
|
||||
|
||||
while col > scol {
|
||||
|
@ -194,15 +202,9 @@ fn unexpand(options: Options) {
|
|||
while byte < buf.len() {
|
||||
// when we have a finite number of columns, never convert past the last column
|
||||
if lastcol > 0 && col >= lastcol {
|
||||
if (pctype != Tab && col > scol + 1) ||
|
||||
(col > scol && (init || pctype == Tab)) {
|
||||
write_tabs(&mut output, ts, scol, col);
|
||||
} else if col > scol {
|
||||
safe_unwrap!(output.write_all(" ".as_bytes()));
|
||||
}
|
||||
scol = col;
|
||||
|
||||
write_tabs(&mut output, ts, scol, col, pctype == Tab, init, true);
|
||||
safe_unwrap!(output.write_all(&buf[byte..]));
|
||||
scol = col;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -253,15 +255,8 @@ fn unexpand(options: Options) {
|
|||
}
|
||||
},
|
||||
Other | Backspace => { // always
|
||||
// never turn a single space before a non-blank into a tab
|
||||
// unless it's at the start of the line
|
||||
if (tabs_buffered && pctype != Tab && col > scol + 1) ||
|
||||
(col > scol && (init || (tabs_buffered && pctype == Tab))) {
|
||||
write_tabs(&mut output, ts, scol, col);
|
||||
} else if col > scol {
|
||||
safe_unwrap!(output.write_all(" ".as_bytes()));
|
||||
}
|
||||
init = false;
|
||||
write_tabs(&mut output, ts, scol, col, pctype == Tab, init, options.aflag);
|
||||
init = false; // no longer at the start of a line
|
||||
col = if ctype == Other { // use computed width
|
||||
col + cwidth
|
||||
} else if col > 0 { // Backspace case, but only if col > 0
|
||||
|
@ -279,12 +274,7 @@ fn unexpand(options: Options) {
|
|||
}
|
||||
|
||||
// write out anything remaining
|
||||
if col > scol + 1 || (init && col > scol) {
|
||||
write_tabs(&mut output, ts, scol, col);
|
||||
} else if col > scol {
|
||||
safe_unwrap!(output.write_all(" ".as_bytes()));
|
||||
}
|
||||
|
||||
write_tabs(&mut output, ts, scol, col, pctype == Tab, init, true);
|
||||
buf.truncate(0); // clear out the buffer
|
||||
}
|
||||
}
|
||||
|
|
|
@ -105,4 +105,8 @@ fn unexpand_spaces_follow_tabs_1() { // evil
|
|||
assert_eq!(&out[..], b"a\t\t B \t" as &[u8]);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn unexpand_spaces_after_fields() {
|
||||
let out = run(" \t A B C D A\t\n", &["-a"]);
|
||||
assert_eq!(&out[..], b"\t\tA B C D\t\t A\t\n" as &[u8]);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue