mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 20:47:46 +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) {
|
fn write_tabs(mut output: &mut BufWriter<Stdout>, tabstops: &[usize],
|
||||||
while let Some(nts) = next_tabstop(tabstops, scol) {
|
mut scol: usize, col: usize, prevtab: bool, init: bool, amode: bool) {
|
||||||
if col < scol + nts {
|
// This conditional establishes the following:
|
||||||
break;
|
// 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()));
|
safe_unwrap!(output.write_all("\t".as_bytes()));
|
||||||
scol += nts;
|
scol += nts;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while col > scol {
|
while col > scol {
|
||||||
|
@ -194,15 +202,9 @@ fn unexpand(options: Options) {
|
||||||
while byte < buf.len() {
|
while byte < buf.len() {
|
||||||
// when we have a finite number of columns, never convert past the last column
|
// when we have a finite number of columns, never convert past the last column
|
||||||
if lastcol > 0 && col >= lastcol {
|
if lastcol > 0 && col >= lastcol {
|
||||||
if (pctype != Tab && col > scol + 1) ||
|
write_tabs(&mut output, ts, scol, col, pctype == Tab, init, true);
|
||||||
(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;
|
|
||||||
|
|
||||||
safe_unwrap!(output.write_all(&buf[byte..]));
|
safe_unwrap!(output.write_all(&buf[byte..]));
|
||||||
|
scol = col;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,15 +255,8 @@ fn unexpand(options: Options) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Other | Backspace => { // always
|
Other | Backspace => { // always
|
||||||
// never turn a single space before a non-blank into a tab
|
write_tabs(&mut output, ts, scol, col, pctype == Tab, init, options.aflag);
|
||||||
// unless it's at the start of the line
|
init = false; // no longer at the start of a 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;
|
|
||||||
col = if ctype == Other { // use computed width
|
col = if ctype == Other { // use computed width
|
||||||
col + cwidth
|
col + cwidth
|
||||||
} else if col > 0 { // Backspace case, but only if col > 0
|
} else if col > 0 { // Backspace case, but only if col > 0
|
||||||
|
@ -279,12 +274,7 @@ fn unexpand(options: Options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// write out anything remaining
|
// write out anything remaining
|
||||||
if col > scol + 1 || (init && col > scol) {
|
write_tabs(&mut output, ts, scol, col, pctype == Tab, init, true);
|
||||||
write_tabs(&mut output, ts, scol, col);
|
|
||||||
} else if col > scol {
|
|
||||||
safe_unwrap!(output.write_all(" ".as_bytes()));
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.truncate(0); // clear out the buffer
|
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]);
|
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