expand/unexpand: fix display width of 3- and 4-byte UTF-8 characters #13352
Open
sylvestre wants to merge 3 commits into
Open
expand/unexpand: fix display width of 3- and 4-byte UTF-8 characters #13352sylvestre wants to merge 3 commits into
sylvestre wants to merge 3 commits into
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
sylvestre
force-pushed
the
expand-unexpand-mb-width
branch
2 times, most recently
from
July 11, 2026 01:41
f0138a1 to
3c971f8
Compare
|
GNU testsuite comparison: |
sylvestre
force-pushed
the
expand-unexpand-mb-width
branch
from
July 11, 2026 21:18
e607a91 to
b21d0d6
Compare
cakebaker
reviewed
Jul 13, 2026
|
|
||
| // Fill the remaining columns. Wide blanks that were not absorbed into a tab | ||
| // are emitted verbatim; everything else is a plain space. | ||
| let mut wi = 0; |
cakebaker
reviewed
Jul 13, 2026
| | '\u{3000}') | ||
| } | ||
|
|
||
| fn next_char_info(utf8: bool, buf: &[u8], byte: usize) -> (CharType, usize, usize) { |
Contributor
There was a problem hiding this comment.
It's a bit confusing that the "char info" of this function is (CharType, usize, usize) whereas the "char info" of char_info_at is (Option<char>, usize, usize).
…lper The UTF-8 sequence length was derived from char::from(b).len_utf8(), which uses the leading byte's value as a codepoint rather than decoding the length from its bit pattern. That is only correct for 2-byte characters; 3- and 4-byte ones were split into single bytes and each counted as one column, so a following tab expanded to the wrong column. Derive the length from the leading byte and measure the display width with unicode-width. expand and unexpand needed identical logic, so it lives in a new uucore::char_width helper (char_width_at) instead of being duplicated. Should make test tests/expand/mb.sh pass
The fast path that batches leading spaces/tabs was gated on --no-utf8, so the default (UTF-8) mode fell back to the per-byte loop, which the display-width fix made measurably slower on ASCII input. Leading whitespace is ASCII and independent of UTF-8 mode, so run the fast path there too, and add the missing lastcol == 0 guard so it does not apply when multiple tab stops cap conversion.
GNU treats a wide blank such as U+3000 (any character for which iswblank is true) as a convertible blank: a run of them that reaches a tab stop is absorbed into a tab, but a run that does not is left as-is. Recognize the Unicode blank set in next_char_info, advance the column by the blank's display width, and remember buffered wide blanks so write_tabs can emit them verbatim when their run is not converted (and never let a tab straddle one). Expose the decoded char from uucore::char_width via char_info_at so unexpand reuses the shared UTF-8 decoding instead of duplicating it. Should make test tests/unexpand/mb.sh pass.
sylvestre
force-pushed
the
expand-unexpand-mb-width
branch
from
July 13, 2026 13:05
b21d0d6 to
c4a82b1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.