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 ...
This commit is contained in:
Lukas Wirth
2025-10-22 23:05:43 +00:00
committed by GitHub
parent a96bf504e0
commit 044701e3a5
2 changed files with 8 additions and 3 deletions
+7 -2
View File
@@ -93,6 +93,11 @@ impl Chunk {
pub fn tabs(&self) -> Bitmap { pub fn tabs(&self) -> Bitmap {
self.tabs 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)] #[derive(Clone, Copy, Debug)]
@@ -123,8 +128,8 @@ impl<'a> ChunkSlice<'a> {
} }
#[inline(always)] #[inline(always)]
pub fn is_char_boundary(self, offset: usize) -> bool { pub fn is_char_boundary(&self, offset: usize) -> bool {
self.text.is_char_boundary(offset) (1 as Bitmap).unbounded_shl(offset as u32) & self.chars != 0 || offset == self.text.len()
} }
#[inline(always)] #[inline(always)]
+1 -1
View File
@@ -44,7 +44,7 @@ impl Rope {
} }
let (start, _, item) = self.chunks.find::<usize, _>((), &offset, Bias::Left); let (start, _, item) = self.chunks.find::<usize, _>((), &offset, Bias::Left);
let chunk_offset = offset - start; 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) .unwrap_or(false)
} }