Fix UTF-8 char boundary handling in cursor movement and rendering#48
Merged
Merged
Conversation
Vertical cursor movement (up/down) could place the cursor at a byte offset that falls inside a multi-byte UTF-8 character, causing panics when rendering the editor or slicing strings. Added snap_to_char_boundary helper and applied defensive .get() slicing in render.rs and parser.rs. https://claude.ai/code/session_01J88TRUMkjBFCGXeBwcMNSk
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
This PR fixes potential panics and incorrect cursor positioning when working with multibyte UTF-8 characters. The changes ensure that cursor positions are always snapped to valid UTF-8 character boundaries during vertical movement and rendering.
Key Changes
Buffer vertical movement: Added
snap_to_char_boundary()helper method that walks backwards from a byte offset to find the nearest valid UTF-8 character boundary. This method is now called inmove_up()andmove_down()to ensure the cursor never lands in the middle of a multibyte character.Cursor rendering: Updated
render_editor()in the UI layer to snap the cursor column to a valid char boundary before rendering. Also fixed the cursor character extraction to properly handle multibyte characters by usingchars().next()instead of byte-based slicing, which could panic or produce incorrect output on non-ASCII text.Parser safety: Added bounds checking in
fixup_link_lines()to use.get()instead of direct indexing, preventing potential panics on edge cases.Notable Implementation Details
snap_to_char_boundary()method usesline.is_char_boundary()to safely identify valid UTF-8 boundaries, walking backwards one byte at a time until a boundary is found.chars().next()andto_string(), then calculating the proper offset for the "after" portion.move_up()andmove_down()with multibyte UTF-8 characters (Greek letters) to prevent regression.https://claude.ai/code/session_01J88TRUMkjBFCGXeBwcMNSk