Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 11 additions & 1 deletion src/uu/fold/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,17 @@ fn process_utf8_line<W: Write>(line: &str, ctx: &mut FoldContext<'_, W>) -> URes
let mut iter = line.char_indices().peekable();

while let Some((byte_idx, ch)) = iter.next() {
let next_idx = iter.peek().map(|(idx, _)| *idx).unwrap_or(line_bytes.len());
let mut next_idx = iter.peek().map(|(idx, _)| *idx).unwrap_or(line_bytes.len());

// Include combining characters with the base character
while let Some(&(_, next_ch)) = iter.peek() {
if unicode_width::UnicodeWidthChar::width(next_ch).unwrap_or(1) == 0 {
iter.next();
next_idx = iter.peek().map(|(idx, _)| *idx).unwrap_or(line_bytes.len());
} else {
break;
}
}
Copy link
Contributor

@cakebaker cakebaker Nov 19, 2025

Choose a reason for hiding this comment

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

I think you can set next_idx after the loop as you don't use it in the loop.

Suggested change
let mut next_idx = iter.peek().map(|(idx, _)| *idx).unwrap_or(line_bytes.len());
// Include combining characters with the base character
while let Some(&(_, next_ch)) = iter.peek() {
if unicode_width::UnicodeWidthChar::width(next_ch).unwrap_or(1) == 0 {
iter.next();
next_idx = iter.peek().map(|(idx, _)| *idx).unwrap_or(line_bytes.len());
} else {
break;
}
}
// Include combining characters with the base character
while let Some(&(_, next_ch)) = iter.peek() {
if unicode_width::UnicodeWidthChar::width(next_ch).unwrap_or(1) == 0 {
iter.next();
} else {
break;
}
}
let next_idx = iter.peek().map(|(idx, _)| *idx).unwrap_or(line_bytes.len());

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Much cleaner, thanks!


if ch == '\n' {
*ctx.last_space = None;
Expand Down
35 changes: 35 additions & 0 deletions tests/by-util/test_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore fullwidth

use uutests::new_ucmd;

#[test]
Expand Down Expand Up @@ -597,3 +599,36 @@ fn test_all_tab_advances_at_non_utf8_character() {
.succeeds()
.stdout_is_fixture_bytes("non_utf8_tab_stops_w16.expected");
}

#[test]
fn test_combining_characters_nfc() {
// e acute NFC form (single character)
let e_acute_nfc = "\u{00E9}"; // é as single character
new_ucmd!()
.arg("-w2")
.pipe_in(format!("{}{}{}", e_acute_nfc, e_acute_nfc, e_acute_nfc))
.succeeds()
.stdout_is(format!("{}{}\n{}", e_acute_nfc, e_acute_nfc, e_acute_nfc));
}

#[test]
fn test_combining_characters_nfd() {
// e acute NFD form (base + combining acute)
let e_acute_nfd = "e\u{0301}"; // e + combining acute accent
new_ucmd!()
.arg("-w2")
.pipe_in(format!("{}{}{}", e_acute_nfd, e_acute_nfd, e_acute_nfd))
.succeeds()
.stdout_is(format!("{}{}\n{}", e_acute_nfd, e_acute_nfd, e_acute_nfd));
}

#[test]
fn test_fullwidth_characters() {
// e fullwidth (takes 2 columns)
let e_fullwidth = "\u{FF45}"; // e
new_ucmd!()
.arg("-w2")
.pipe_in(format!("{}{}", e_fullwidth, e_fullwidth))
.succeeds()
.stdout_is(format!("{}\n{}", e_fullwidth, e_fullwidth));
}
Loading