Fix Segment._split_cells for non-unit-width characters (emoji, CJK)#4118
Open
ShiLiguo123 wants to merge 1 commit into
Open
Fix Segment._split_cells for non-unit-width characters (emoji, CJK)#4118ShiLiguo123 wants to merge 1 commit into
ShiLiguo123 wants to merge 1 commit into
Conversation
The old algorithm used a heuristic initial position estimate int((cut / cell_length) * len(text)) and then adjusted in a while loop. This fails for strings with mixed single/double cell-width characters (emoji, CJK). Replace with a simple character-by-character walk-through that correctly tracks cumulative cell positions. Fixes Textualize#3299
Member
|
Your PR has been closed due to a AI policy violation. Please read the following before submitting further PRs. https://github.com/Textualize/textual/blob/main/AI_POLICY.md |
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.
Summary
Fix
Segment._split_cells()to correctly handle strings containing double-cell-width characters (emoji, CJK characters, etc.).Root Cause
The old algorithm used a heuristic initial position:
pos = int((cut / cell_length) * len(text)). This assumes uniform character width, which fails badly for mixed-width strings.For example:
Fix
Replaced the heuristic + while-loop with a character-by-character walk-through. The new algorithm:
Fixes #3299