Return None for too-large offset

This commit is contained in:
Ariel Davis 2023-05-06 02:08:47 -07:00
parent e8a93306f0
commit 343976fe56

View file

@ -84,6 +84,8 @@ impl WideChar {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LineIndex {
/// Offset the beginning of each line (except the first, which always has offset 0).
///
/// Invariant: Always non-empty and the last element holds the length of the original text.
newlines: Box<[TextSize]>,
/// List of non-ASCII characters on each line.
line_wide_chars: IntMap<u32, Box<[WideChar]>>,
@ -125,6 +127,8 @@ impl LineIndex {
cur_col += c_len;
}
newlines.push(TextSize::of(text));
// Save any wide characters seen in the last line
if !wide_chars.is_empty() {
line_wide_chars.insert(line, wide_chars.into_boxed_slice());
@ -143,8 +147,12 @@ impl LineIndex {
}
/// Transforms the `TextSize` into a `LineCol`, or returns `None` if the `offset` was invalid,
/// e.g. if it points to the middle of a multi-byte character.
/// e.g. if it extends past the end of the text or points to the middle of a multi-byte
/// character.
pub fn try_line_col(&self, offset: TextSize) -> Option<LineCol> {
if offset > *self.newlines.last().unwrap() {
return None;
}
let line = self.newlines.partition_point(|&it| it <= offset);
let start = self.start_offset(line)?;
let col = offset - start;