Skip to content

Commit 343976f

Browse files
committed
Return None for too-large offset
1 parent e8a9330 commit 343976f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/line-index/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ impl WideChar {
8484
#[derive(Debug, Clone, PartialEq, Eq)]
8585
pub struct LineIndex {
8686
/// Offset the beginning of each line (except the first, which always has offset 0).
87+
///
88+
/// Invariant: Always non-empty and the last element holds the length of the original text.
8789
newlines: Box<[TextSize]>,
8890
/// List of non-ASCII characters on each line.
8991
line_wide_chars: IntMap<u32, Box<[WideChar]>>,
@@ -125,6 +127,8 @@ impl LineIndex {
125127
cur_col += c_len;
126128
}
127129

130+
newlines.push(TextSize::of(text));
131+
128132
// Save any wide characters seen in the last line
129133
if !wide_chars.is_empty() {
130134
line_wide_chars.insert(line, wide_chars.into_boxed_slice());
@@ -143,8 +147,12 @@ impl LineIndex {
143147
}
144148

145149
/// Transforms the `TextSize` into a `LineCol`, or returns `None` if the `offset` was invalid,
146-
/// e.g. if it points to the middle of a multi-byte character.
150+
/// e.g. if it extends past the end of the text or points to the middle of a multi-byte
151+
/// character.
147152
pub fn try_line_col(&self, offset: TextSize) -> Option<LineCol> {
153+
if offset > *self.newlines.last().unwrap() {
154+
return None;
155+
}
148156
let line = self.newlines.partition_point(|&it| it <= offset);
149157
let start = self.start_offset(line)?;
150158
let col = offset - start;

0 commit comments

Comments
 (0)