From 044701e3a59ee2bf4b70b69449ecc26d620b673d Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 23 Oct 2025 01:05:43 +0200 Subject: [PATCH] rope: Implement `Rope::is_char_boundary` via chars bitmap (#40945) Slightly more efficient. No new tests as we already have tests verifiying this. Release Notes: - N/A *or* Added/Fixed/Improved ... --- crates/rope/src/chunk.rs | 9 +++++++-- crates/rope/src/rope.rs | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/rope/src/chunk.rs b/crates/rope/src/chunk.rs index 515ae8c768657681de1614ba03fdce6d176b96ca..51904cd8e2217dc56947f6026fff674147cffea5 100644 --- a/crates/rope/src/chunk.rs +++ b/crates/rope/src/chunk.rs @@ -93,6 +93,11 @@ impl Chunk { pub fn tabs(&self) -> Bitmap { self.tabs } + + #[inline(always)] + pub fn is_char_boundary(&self, offset: usize) -> bool { + (1 as Bitmap).unbounded_shl(offset as u32) & self.chars != 0 || offset == self.text.len() + } } #[derive(Clone, Copy, Debug)] @@ -123,8 +128,8 @@ impl<'a> ChunkSlice<'a> { } #[inline(always)] - pub fn is_char_boundary(self, offset: usize) -> bool { - self.text.is_char_boundary(offset) + pub fn is_char_boundary(&self, offset: usize) -> bool { + (1 as Bitmap).unbounded_shl(offset as u32) & self.chars != 0 || offset == self.text.len() } #[inline(always)] diff --git a/crates/rope/src/rope.rs b/crates/rope/src/rope.rs index c710ed86570ef5aadd7f7a31f9c440226b073ae8..204b13cfae2c27441de1d9265cd964fa1b4215af 100644 --- a/crates/rope/src/rope.rs +++ b/crates/rope/src/rope.rs @@ -44,7 +44,7 @@ impl Rope { } let (start, _, item) = self.chunks.find::((), &offset, Bias::Left); let chunk_offset = offset - start; - item.map(|chunk| chunk.text.is_char_boundary(chunk_offset)) + item.map(|chunk| chunk.is_char_boundary(chunk_offset)) .unwrap_or(false) }