Skip to content

unexpand: count a multi-byte char split across reads as one column - #16513

Open
Xnadir wants to merge 3 commits into
uutils:mainfrom
Xnadir:unexpand-multibyte-split
Open

Xnadir wants to merge 3 commits into
uutils:mainfrom
Xnadir:unexpand-multibyte-split

Conversation

@Xnadir

@Xnadir Xnadir commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Fixes #16504

Same problem as expand (#16503, fixed in #16512): when a read ends inside a UTF-8 character, unexpand_buf sees the lead byte at the end of one buffer and the continuation byte at the start of the next, and counts each as an invalid byte one column wide. é then takes two columns, so é + 7 spaces + z with -a gives é\t z instead of é\tz.

unexpand_input now holds back the last 1-3 bytes of a buffer when they begin a sequence the buffer cuts short (UTF-8 mode only), and joins them to the next read. The join only allocates when there is something to carry. If the input ends inside a character, the held bytes are written as before. The per-line loop moved into a small unexpand_lines helper so the leftover can go through the same path.

incomplete_utf8_suffix is the same helper as in #16512. I kept a copy in each crate so the two PRs are independent; once both land I can move it to uucore::char_width in a follow-up if you prefer.

Tests:

  • test_multibyte_char_split_across_read_buffers: 8191 ASCII bytes, then é, 8 spaces and z, with -a, so é is split by the 8 KiB read buffer. It fails on main (\t z) and passes with the fix. This is a portable version of the cmd_shell + sleep test in Add as ignored some failing tests found by oss scanner  #16493.
  • test_file_ending_inside_a_multibyte_char: a file ending in a lone \xC3 keeps its bytes (passes before and after).
  • Unit test for incomplete_utf8_suffix.

cargo test --no-default-features --features unexpand --test tests -- test_unexpand:: passes (60), as does cargo test -p uu_unexpand. cargo clippy -p uu_unexpand --all-targets -- -D warnings and rustfmt are clean.

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:51

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_unexpand.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

@sylvestre

Copy link
Copy Markdown
Contributor

AI disclosure: written with help from Claude Code.

Seems that it is fully written with Claude
Otherwise, you would not have duplicated the code from here:
https://github.com/uutils/coreutils/pull/16512/changes

please adding some tech debt for no good reason ...

@codspeed

codspeed Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 33.74%

1 warning, 1 note

⚠️ 124 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

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

⚡ 2 improved benchmarks
❌ 4 regressed benchmarks
✅ 409 untouched benchmarks
⏩ 70 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory socket_addrs_direct[100000] 8.6 KB 68 KB -87.29%
❌ Memory hostname_ip_lookup[100000] 100.9 KB 160.2 KB -37.06%
❌ CPU Simulation unexpand_large_file[10] 83 ms 85.9 ms -3.3%
❌ CPU Simulation unexpand_many_lines[100000] 39.8 ms 41.2 ms -3.28%
⚡ CPU Simulation expand_custom_tabstops[50000] 27.9 ms 26.2 ms +6.37%
⚡ CPU Simulation expand_many_lines[100000] 38.9 ms 36.6 ms +6.31%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Xnadir:unexpand-multibyte-split (cea2056) with main (887b9e6)2

Open in CodSpeed

Footnotes

  1. 70 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. ↩

  2. No successful run was found on main (7b68df0) during the generation of this report, so 887b9e6 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/id/smack is no longer failing!
Congrats! The gnu test tests/mkdir/smack-no-root is no longer failing!
Congrats! The gnu test tests/mkdir/smack-root is no longer failing!
Congrats! The gnu test tests/df/skip-rootfs is now passing!

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for the PR!
Two things are still open. The new test should be merged with the ignored test_multibyte_char_split_across_reads (and that one un-ignored). And incomplete_utf8_suffix should be shared with expand in uucore instead of copied.
Please also check the failing Tests job.

@Xnadir
Xnadir force-pushed the unexpand-multibyte-split branch from 020584f to 5d77de4 Compare November 16, 2026 12:24
Copilot AI balanced review requested due to automatic review settings November 16, 2026 12:24

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

Fair point, and you're right about the duplication. It was a bad call to copy the helper so the two PRs could merge in any order. Yes, Claude Code wrote most of this; I reviewed and tested it, but I should have caught that.

Fixed:

@sylvestre
sylvestre force-pushed the unexpand-multibyte-split branch from 5d77de4 to 7e78a90 Compare November 17, 2026 05:37
Copilot AI balanced review requested due to automatic review settings November 17, 2026 05:37

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 unexpand-multibyte-split branch from 7e78a90 to b808972 Compare November 18, 2026 21:31
Copilot AI balanced review requested due to automatic review settings November 18, 2026 21:31

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

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.48 MB (+100 KB, +0.06%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)

@sylvestre

Copy link
Copy Markdown
Contributor

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

@Xnadir
Xnadir force-pushed the unexpand-multibyte-split branch from b808972 to 2777364 Compare November 28, 2026 18:06
Copilot AI balanced review requested due to automatic review settings November 28, 2026 18:06

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 November 29, 2026 04:47
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>
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. Uses the
uucore::char_width::incomplete_utf8_suffix helper added for expand.

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#16504

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sylvestre
sylvestre force-pushed the unexpand-multibyte-split branch from 2777364 to cea2056 Compare November 29, 2026 03:47
Copilot AI balanced review requested due to automatic review settings November 29, 2026 03:47

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.

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.

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

3 participants