From aba3970d6248e140618f1675021148a62546eaf8 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Fri, 3 Oct 2025 02:35:13 +0900 Subject: [PATCH] Fix some char boundary violation bugs. --- src/formatter.rs | 4 ++-- src/graphemes.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index 6327623..af53c53 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -512,9 +512,9 @@ pub fn find_good_break(slice: &RopeSlice, lower_limit: usize, byte_idx: usize) - let mut prev = if i == slice_len { None } else { - Some(slice.char(byte_idx)) + Some(slice.char(i)) }; - let mut char_itr = slice.chars_at(byte_idx); + let mut char_itr = slice.chars_at(i); while i > lower_limit { let c = char_itr.prev(); if WS_CHARS.contains(&c.unwrap()) && prev.map(|pc| !WS_CHARS.contains(&pc)).unwrap_or(true) diff --git a/src/graphemes.rs b/src/graphemes.rs index e245a56..4ba62a1 100644 --- a/src/graphemes.rs +++ b/src/graphemes.rs @@ -41,6 +41,8 @@ pub fn prev_grapheme_boundary(slice: &RopeSlice, byte_idx: usize) -> usize { // Bounds check debug_assert!(byte_idx <= slice.len()); + let byte_idx = slice.ceil_char_boundary(byte_idx); + // Get the chunk with our byte index in it. let (mut chunk, mut chunk_byte_idx) = slice.chunk(byte_idx); @@ -82,6 +84,8 @@ pub fn next_grapheme_boundary(slice: &RopeSlice, byte_idx: usize) -> usize { // Bounds check debug_assert!(byte_idx <= slice.len()); + let byte_idx = slice.floor_char_boundary(byte_idx); + // Get the chunk with our byte index in it. let (mut chunk, mut chunk_byte_idx) = slice.chunk(byte_idx); @@ -115,6 +119,11 @@ pub fn is_grapheme_boundary(slice: &RopeSlice, byte_idx: usize) -> bool { // Get the chunk with our byte index in it. let (chunk, chunk_byte_idx) = slice.chunk(byte_idx); + // Can't be a grapheme boundary if it's not a char boundary. + if !chunk.is_char_boundary(chunk_byte_idx) { + return false; + } + // Set up the grapheme cursor. let mut gc = GraphemeCursor::new(byte_idx, slice.len(), true);