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
33 changes: 28 additions & 5 deletions src/uu/fold/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub fn uu_app() -> Command {
.arg(
Arg::new(options::CHARACTERS)
.long(options::CHARACTERS)
.short('c')
.help(translate!("fold-characters-help"))
.conflicts_with(options::BYTES)
.action(ArgAction::SetTrue),
Expand Down Expand Up @@ -260,9 +261,31 @@ fn next_tab_stop(col_count: usize) -> usize {

fn compute_col_count(buffer: &[u8], mode: WidthMode) -> usize {
match mode {
WidthMode::Characters => std::str::from_utf8(buffer)
.map(|s| s.chars().count())
.unwrap_or(buffer.len()),
WidthMode::Characters => {
if let Ok(s) = std::str::from_utf8(buffer) {
let mut width = 0;
for ch in s.chars() {
match ch {
'\r' => width = 0,
'\t' => width = next_tab_stop(width),
'\x08' => width = width.saturating_sub(1),
_ => width += 1,
}
}
width
} else {
let mut width = 0;
for &byte in buffer {
match byte {
CR => width = 0,
TAB => width = next_tab_stop(width),
0x08 => width = width.saturating_sub(1),
_ => width += 1,
}
}
width
}
}
WidthMode::Columns => {
if let Ok(s) = std::str::from_utf8(buffer) {
let mut width = 0;
Expand Down Expand Up @@ -382,7 +405,7 @@ fn process_ascii_line<W: Write>(line: &[u8], ctx: &mut FoldContext<'_, W>) -> UR
*ctx.col_count = ctx.col_count.saturating_sub(1);
idx += 1;
}
TAB if ctx.mode == WidthMode::Columns => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was doing some research to see why this wasn't included in the original implementation and it appears that this was only introduced in 9.7

TAB => {
loop {
let next_stop = next_tab_stop(*ctx.col_count);
if next_stop > ctx.width && !ctx.output.is_empty() {
Expand Down Expand Up @@ -523,7 +546,7 @@ fn process_utf8_chars<W: Write>(line: &str, ctx: &mut FoldContext<'_, W>) -> URe
continue;
}

if ctx.mode == WidthMode::Columns && ch == '\t' {
if ch == '\t' {
loop {
let next_stop = next_tab_stop(*ctx.col_count);
if next_stop > ctx.width && !ctx.output.is_empty() {
Expand Down
27 changes: 27 additions & 0 deletions tests/by-util/test_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ fn test_wide_characters_with_characters_option() {
.stdout_is("\u{B250}\u{B250}\u{B250}\n");
}

#[test]
fn test_wide_characters_with_characters_short_option() {
new_ucmd!()
.args(&["-c", "-w", "5"])
.pipe_in("\u{B250}\u{B250}\u{B250}\n")
.succeeds()
.stdout_is("\u{B250}\u{B250}\u{B250}\n");
}

#[test]
fn test_multiple_wide_characters_in_column_mode() {
let wide = '\u{FF1A}';
Expand Down Expand Up @@ -540,6 +549,24 @@ fn test_fold_after_tab() {
.stdout_is("a\tbb\nb\n");
}

#[test]
fn test_fold_characters_tab_advances_to_next_tab_stop() {
new_ucmd!()
.args(&["-c", "-w", "4"])
.pipe_in("ab\tcd\n")
.succeeds()
.stdout_is("ab\n\t\ncd\n");
}

#[test]
fn test_fold_characters_tab_with_non_ascii() {
new_ucmd!()
.args(&["-c", "-w", "2"])
.pipe_in("\u{00E9}\tb\n")
.succeeds()
.stdout_is("\u{00E9}\n\t\nb\n");
}

#[test]
fn test_fold_at_tab_as_word_boundary() {
new_ucmd!()
Expand Down
Loading