pub struct LineIndex {
newline_positions: Vec<usize>,
}
impl LineIndex {
pub fn new(chars: &[char]) -> Self {
let newline_positions = chars
.iter()
.enumerate()
.filter(|(_, &c)| c == '\n')
.map(|(i, _)| i)
.collect();
Self { newline_positions }
}
pub fn lineno_colno(&self, pos: usize) -> (u32, u32) {
let line_idx = self.newline_positions.partition_point(|&nl| nl < pos);
let line = (line_idx + 1) as u32;
let line_start = if line_idx == 0 {
0
} else {
self.newline_positions[line_idx - 1] + 1
};
let col = (pos - line_start) as u32;
(line, col)
}
}