Fix some char boundary violation bugs.

This commit is contained in:
Nathan Vegdahl 2025-10-03 02:35:13 +09:00
parent ffd3a83cbd
commit aba3970d62
2 changed files with 11 additions and 2 deletions

View File

@ -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)

View File

@ -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);