diff --git a/crates/rope/src/rope.rs b/crates/rope/src/rope.rs index db79e6b03f5e0705a67f7acec5bf20eaaa875cd8..b0b34c95120acddb26081b9265346c80a5afc99c 100644 --- a/crates/rope/src/rope.rs +++ b/crates/rope/src/rope.rs @@ -30,6 +30,13 @@ impl Rope { Self::default() } + /// Checks that `index`-th byte is the first byte in a UTF-8 code point + /// sequence or the end of the string. + /// + /// The start and end of the string (when `index == self.len()`) are + /// considered to be boundaries. + /// + /// Returns `false` if `index` is greater than `self.len()`. pub fn is_char_boundary(&self, offset: usize) -> bool { if self.chunks.is_empty() { return offset == 0; diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index d61038d746fa1bebbf0b92a99b0a59f650bc5704..2eacef5ae037e0d45c53404f30d8c81bdedf4dc1 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -2400,7 +2400,9 @@ impl BufferSnapshot { } else if bias == Bias::Right && offset == self.len() { Anchor::MAX } else { - if !self.visible_text.is_char_boundary(offset) { + if offset > self.visible_text.len() { + panic!("offset {} is out of bounds", offset) + } else if !self.visible_text.is_char_boundary(offset) { // find the character let char_start = self.visible_text.floor_char_boundary(offset); // `char_start` must be less than len and a char boundary