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
…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.
Merging this PR will improve performance by 65.27%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ❌ | Simulation | du_all_wide_tree[(5000, 500)] |
16.3 ms | 17.1 ms | -4.14% |
| ⚡ | Simulation | unexpand_large_file[10] |
282.3 ms | 130 ms | ×2.2 |
| ⚡ | Simulation | unexpand_many_lines[100000] |
134.7 ms | 62.1 ms | ×2.2 |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing sylvestre:expand-unexpand-mb-width (b21d0d6) with main (3ceaab5)
Footnotes
-
46 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. ↩
f0138a1 to
3c971f8
Compare
|
GNU testsuite comparison: |
e607a91 to
b21d0d6
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.