Skip to content

Commit c086d43

Browse files
authored
Merge pull request #10533 from ChrisDryden/refactor-fold-compute-col-count
fold: refactor compute_col_count and add character mode tests
1 parent 26a29ca commit c086d43

2 files changed

Lines changed: 56 additions & 46 deletions

File tree

src/uu/fold/src/fold.rs

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -260,57 +260,33 @@ fn next_tab_stop(col_count: usize) -> usize {
260260
}
261261

262262
fn compute_col_count(buffer: &[u8], mode: WidthMode) -> usize {
263-
match mode {
264-
WidthMode::Characters => {
265-
if let Ok(s) = std::str::from_utf8(buffer) {
266-
let mut width = 0;
267-
for ch in s.chars() {
268-
match ch {
269-
'\r' => width = 0,
270-
'\t' => width = next_tab_stop(width),
271-
'\x08' => width = width.saturating_sub(1),
272-
_ => width += 1,
263+
if let Ok(s) = std::str::from_utf8(buffer) {
264+
let mut width = 0;
265+
for ch in s.chars() {
266+
match ch {
267+
'\r' => width = 0,
268+
'\t' => width = next_tab_stop(width),
269+
'\x08' => width = width.saturating_sub(1),
270+
_ => {
271+
width += match mode {
272+
WidthMode::Characters => 1,
273+
WidthMode::Columns => UnicodeWidthChar::width(ch).unwrap_or(0),
273274
}
274275
}
275-
width
276-
} else {
277-
let mut width = 0;
278-
for &byte in buffer {
279-
match byte {
280-
CR => width = 0,
281-
TAB => width = next_tab_stop(width),
282-
0x08 => width = width.saturating_sub(1),
283-
_ => width += 1,
284-
}
285-
}
286-
width
287276
}
288277
}
289-
WidthMode::Columns => {
290-
if let Ok(s) = std::str::from_utf8(buffer) {
291-
let mut width = 0;
292-
for ch in s.chars() {
293-
match ch {
294-
'\r' => width = 0,
295-
'\t' => width = next_tab_stop(width),
296-
'\x08' => width = width.saturating_sub(1),
297-
_ => width += UnicodeWidthChar::width(ch).unwrap_or(0),
298-
}
299-
}
300-
width
301-
} else {
302-
let mut width = 0;
303-
for &byte in buffer {
304-
match byte {
305-
CR => width = 0,
306-
TAB => width = next_tab_stop(width),
307-
0x08 => width = width.saturating_sub(1),
308-
_ => width += 1,
309-
}
310-
}
311-
width
278+
width
279+
} else {
280+
let mut width = 0;
281+
for &byte in buffer {
282+
match byte {
283+
CR => width = 0,
284+
TAB => width = next_tab_stop(width),
285+
0x08 => width = width.saturating_sub(1),
286+
_ => width += 1,
312287
}
313288
}
289+
width
314290
}
315291
}
316292

tests/by-util/test_fold.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
5-
// spell-checker:ignore fullwidth
5+
// spell-checker:ignore fullwidth refgh tefgh nefgh
66

77
use bytecount::count;
88
use unicode_width::UnicodeWidthChar;
@@ -958,3 +958,37 @@ fn test_fullwidth_characters() {
958958
.succeeds()
959959
.stdout_is(format!("{e_fullwidth}\n{e_fullwidth}"));
960960
}
961+
962+
#[test]
963+
fn test_character_mode_special_chars() {
964+
for (args, input, expected) in [
965+
// backspace decreases column
966+
(&["-c", "-w", "5"][..], "abcde\x08fg\n", "abcde\x08f\ng\n"),
967+
// carriage return resets column
968+
(&["-c", "-w", "5"], "abcd\refgh\n", "abcd\refgh\n"),
969+
// tab at start exceeds width
970+
(&["-c", "-w", "4"], "\tabc\n", "\t\nabc\n"),
971+
// multiple tabs
972+
(&["-c", "-w", "10"], "a\tb\tc\n", "a\tb\n\tc\n"),
973+
// basic folding
974+
(&["-c", "-w", "3"], "abcdef\n", "abc\ndef\n"),
975+
// preserves empty lines
976+
(&["-c", "-w", "5"], "abc\n\ndef\n", "abc\n\ndef\n"),
977+
// word boundary with -s
978+
(&["-c", "-s", "-w", "5"], "ab cd ef\n", "ab \ncd ef\n"),
979+
// tab as word boundary
980+
(&["-c", "-s", "-w", "10"], "abcd\tefgh\n", "abcd\t\nefgh\n"),
981+
// wide chars count as 1 in -c mode
982+
(
983+
&["-c", "-w", "3"],
984+
"\u{FF1A}\u{FF1A}\u{FF1A}\u{FF1A}\n",
985+
"\u{FF1A}\u{FF1A}\u{FF1A}\n\u{FF1A}\n",
986+
),
987+
] {
988+
new_ucmd!()
989+
.args(args)
990+
.pipe_in(input)
991+
.succeeds()
992+
.stdout_is(expected);
993+
}
994+
}

0 commit comments

Comments
 (0)