Skip to content

expand: count a multi-byte char split across reads as one column - #16512

Open
Xnadir wants to merge 2 commits into
uutils:mainfrom
Xnadir:expand-multibyte-split
Open

Xnadir wants to merge 2 commits into
uutils:mainfrom
Xnadir:expand-multibyte-split

Conversation

@Xnadir

@Xnadir Xnadir commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Fixes #16503

When a read ended inside a UTF-8 character (a slow pipe, or a file where the character straddles the 8 KiB BufReader buffer), expand_buf saw the lead byte at the end of one buffer and the continuation byte at the start of the next. char_width_at resyncs each as an invalid byte of width 1, so é counted as two columns and the next tab came out one space short.

expand_file now checks the end of each buffer (in UTF-8 mode only) with a small incomplete_utf8_suffix helper: if the last 1-3 bytes begin a sequence that the buffer cuts short, they are held back and joined to the next read. The join only allocates when there is something to carry. If the file ends inside a character, the held bytes are written as before.

Tests:

  • test_multibyte_char_split_across_read_buffers: 8191 ASCII bytes followed by é\tz, so é is split by the read buffer. On main the tab becomes 7 spaces; with the fix it is 8. This is a portable version of the cmd_shell + sleep test in Add as ignored some failing tests found by oss scanner  #16493 and exercises the same path on every platform.
  • test_file_ending_inside_a_multibyte_char: a file ending in a lone \xC3 still gets its bytes written unchanged (passes before and after).
  • Unit test for incomplete_utf8_suffix (complete, cut 2/3/4-byte sequences, a stray continuation byte, a byte that is never a lead byte).

cargo test --no-default-features --features expand --test tests -- test_expand:: passes (50), as does cargo test -p uu_expand. cargo clippy -p uu_expand --all-targets -- -D warnings and rustfmt are clean.

unexpand (#16504) has the same problem; I'll send that separately so each PR stays small.

AI disclosure: written with help from Claude Code. Nothing was derived from GNU source.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings November 15, 2026 11:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Comment thread tests/by-util/test_expand.rs Outdated
}

#[test]
fn test_multibyte_char_split_across_read_buffers() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i merged the ignored test too. please merge these

@codspeed

codspeed Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 12 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 20 untouched benchmarks
⏩ 469 skipped benchmarks1


Comparing Xnadir:expand-multibyte-split (7c4c559) with main (f88b235)

Open in CodSpeed

Footnotes

  1. 469 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for the work on this!
please rebase on main and un-ignore test_multibyte_char_split_across_reads (from #16493), merged with your new test as asked inline

@Xnadir
Xnadir force-pushed the expand-multibyte-split branch from ef8aa61 to b91bcf0 Compare November 16, 2026 12:23
Copilot AI balanced review requested due to automatic review settings November 16, 2026 12:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Xnadir

Xnadir commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, done:

  • Rebased on main.
  • Merged my test into test_multibyte_char_split_across_reads, which is no longer ignored. It keeps the pipe check under #[cfg(unix)] and adds the file case where é straddles the 8 KiB read buffer, so the path is also covered on Windows/macOS runners.
  • Moved incomplete_utf8_suffix into uucore::char_width, with its unit test there, so unexpand (unexpand: count a multi-byte char split across reads as one column #16513) uses the same function instead of a copy.

@sylvestre
sylvestre force-pushed the expand-multibyte-split branch from b91bcf0 to dc503a3 Compare November 17, 2026 07:08
Copilot AI balanced review requested due to automatic review settings November 17, 2026 07:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre
sylvestre force-pushed the expand-multibyte-split branch from dc503a3 to 555a5c7 Compare November 18, 2026 22:55
Copilot AI balanced review requested due to automatic review settings November 18, 2026 22:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/tail/follow-name is no longer failing!
Congrats! The gnu test tests/tail/pid-pipe is no longer failing!

@sylvestre

Copy link
Copy Markdown
Contributor

it has been conflicting for more than 7 days, sorry! please rebase it

@Xnadir
Xnadir force-pushed the expand-multibyte-split branch from 555a5c7 to 3acd8f7 Compare November 28, 2026 18:05
Copilot AI balanced review requested due to automatic review settings November 28, 2026 18:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre
sylvestre force-pushed the expand-multibyte-split branch from 3acd8f7 to 88d765f Compare November 29, 2026 20:08
Copilot AI balanced review requested due to automatic review settings November 29, 2026 20:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 06:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Xnadir and others added 2 commits December 1, 2026 16:40
When a read ends inside a UTF-8 character, hold its first bytes back and
join them to the next read, so the character is measured once instead of
as two invalid bytes of one column each. The check lives in
uucore::char_width::incomplete_utf8_suffix so unexpand can share it.

Un-ignore test_multibyte_char_split_across_reads and add a portable case
where the character straddles the read buffer of a file.

Fixes uutils#16503

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sylvestre
sylvestre force-pushed the expand-multibyte-split branch from b0bccfd to 7c4c559 Compare December 1, 2026 15:40
Copilot AI balanced review requested due to automatic review settings December 1, 2026 15:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

expand: multi-byte char split across reads is one column off

3 participants