const CHECKPOINT_BYTES: usize = 4096;
#[derive(Clone, Debug)]
pub struct LineIndex {
starts: Vec<usize>,
ascii: bool,
checkpoints: Vec<usize>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Position {
pub offset: usize,
pub line: usize,
pub column: usize,
}
fn count_chars(text: &str, from: usize, to: usize) -> usize {
let bytes = text.as_bytes();
let to = to.min(bytes.len());
let from = from.min(to);
bytes[from..to]
.iter()
.filter(|&&b| b & 0xC0 != 0x80)
.count()
}
impl LineIndex {
pub fn new(text: &str) -> LineIndex {
let bytes = text.as_bytes();
let mut starts = vec![0];
let mut i = 0;
while i < bytes.len() {
match bytes[i] {
b'\n' => starts.push(i + 1),
b'\r' if bytes.get(i + 1) == Some(&b'\n') => {
starts.push(i + 2);
i += 1;
}
b'\r' => starts.push(i + 1),
_ => {}
}
i += 1;
}
let ascii = text.is_ascii();
let mut checkpoints = Vec::new();
if !ascii {
let mut chars = 0;
for at in (0..=bytes.len()).step_by(CHECKPOINT_BYTES) {
if at > 0 {
chars += count_chars(text, at - CHECKPOINT_BYTES, at);
}
checkpoints.push(chars);
}
}
LineIndex {
starts,
ascii,
checkpoints,
}
}
pub fn line(&self, offset: usize) -> usize {
self.starts.partition_point(|&start| start <= offset) - 1
}
pub fn line_start(&self, offset: usize) -> usize {
self.starts[self.line(offset)]
}
pub(crate) fn bounds(&self, text: &str, line: usize) -> (usize, usize) {
let start = self.starts.get(line).copied().unwrap_or(text.len());
let next = self.starts.get(line + 1).copied().unwrap_or(text.len());
let raw = text.get(start..next).unwrap_or_default();
let content = raw.strip_suffix('\n').unwrap_or(raw);
let content = content.strip_suffix('\r').unwrap_or(content);
(start, start + content.len())
}
pub fn char_offset(&self, text: &str, offset: usize) -> usize {
let offset = offset.min(text.len());
if self.ascii {
return offset;
}
let checkpoint = offset / CHECKPOINT_BYTES;
let before = self.checkpoints.get(checkpoint).copied().unwrap_or(0);
before + count_chars(text, checkpoint * CHECKPOINT_BYTES, offset)
}
pub fn byte_offset(&self, text: &str, chars: usize) -> usize {
if self.ascii {
return chars.min(text.len());
}
let checkpoint =
(self.checkpoints.partition_point(|&before| before <= chars)).saturating_sub(1);
let mut count = self.checkpoints.get(checkpoint).copied().unwrap_or(0);
let bytes = text.as_bytes();
let mut at = checkpoint * CHECKPOINT_BYTES;
loop {
while bytes.get(at).is_some_and(|&b| b & 0xC0 == 0x80) {
at += 1;
}
if count == chars || at >= bytes.len() {
return at.min(bytes.len());
}
count += 1;
at += 1;
}
}
pub fn position(&self, text: &str, offset: usize) -> Position {
let line = self.line(offset);
let chars = self.char_offset(text, offset);
let line_chars = self.char_offset(text, self.starts[line]);
Position {
offset: chars,
line: line + 1,
column: chars - line_chars + 1,
}
}
pub fn line_column(&self, text: &str, offset: usize) -> (usize, usize) {
let position = self.position(text, offset);
(position.line, position.column)
}
#[cfg(feature = "reference")]
pub(crate) fn column(&self, text: &str, offset: usize, tab_width: u32) -> u32 {
let start = self.line_start(offset);
let mut column = 0u32;
for c in text.get(start..offset).unwrap_or("").chars() {
column = match c {
'\t' if tab_width > 0 => (column / tab_width + 1).saturating_mul(tab_width),
'\x0C' => 0,
_ => column.saturating_add(1),
};
}
column
}
}
#[derive(Clone, Debug)]
pub struct LineCursor<'t> {
lines: &'t LineIndex,
text: &'t str,
line: usize,
byte: usize,
chars: usize,
line_chars: usize,
}
impl<'t> LineCursor<'t> {
pub fn new(lines: &'t LineIndex, text: &'t str) -> LineCursor<'t> {
LineCursor {
lines,
text,
line: 0,
byte: 0,
chars: 0,
line_chars: 0,
}
}
fn count(&self, from: usize, to: usize) -> usize {
if self.lines.ascii {
to - from
} else {
count_chars(self.text, from, to)
}
}
pub fn position(&mut self, offset: usize) -> Position {
let offset = offset.min(self.text.len());
if offset < self.byte {
let position = self.lines.position(self.text, offset);
self.line = position.line - 1;
self.byte = offset;
self.chars = position.offset;
self.line_chars = position.offset + 1 - position.column;
return position;
}
let starts = &self.lines.starts;
while let Some(&next) = starts.get(self.line + 1)
&& next <= offset
{
self.chars += self.count(self.byte, next);
self.byte = next;
self.line += 1;
self.line_chars = self.chars;
}
self.chars += self.count(self.byte, offset);
self.byte = offset;
Position {
offset: self.chars,
line: self.line + 1,
column: self.chars - self.line_chars + 1,
}
}
}
#[cfg(test)]
mod tests {
use super::{CHECKPOINT_BYTES, LineCursor, LineIndex, Position};
#[test]
fn columns_count_code_points_expand_tabs_and_restart_at_form_feeds() {
let text = "ab\r\n\täx\rcd\n \x0C y";
let lines = LineIndex::new(text);
let x = text.find('x').unwrap_or_default();
assert_eq!(lines.line(x), 1);
assert_eq!(lines.column(text, x, 8), 9);
assert_eq!(lines.column(text, x, 4), 5);
assert_eq!(lines.line(text.find('d').unwrap_or_default()), 2);
assert_eq!(lines.column(text, text.len() - 1, 8), 1);
}
#[test]
fn people_count_lines_and_columns_from_one_in_code_points() {
let text = "ab\r\n\täx";
let lines = LineIndex::new(text);
assert_eq!(lines.line_column(text, 0), (1, 1));
assert_eq!(lines.line_column(text, text.len() - 1), (2, 3));
assert_eq!(lines.line_column(text, text.len()), (2, 4));
}
#[test]
fn a_column_too_large_to_count_stays_at_the_largest() {
let text = "\t\t\tx";
let lines = LineIndex::new(text);
assert_eq!(lines.column(text, 3, u32::MAX / 2 + 1), u32::MAX);
}
fn counted(text: &str) -> Vec<(usize, Position)> {
let mut out = Vec::new();
let (mut line, mut column, mut offset) = (1, 1, 0);
let mut chars = text.char_indices().peekable();
loop {
let Some(&(at, c)) = chars.peek() else {
out.push((
text.len(),
Position {
offset,
line,
column,
},
));
return out;
};
out.push((
at,
Position {
offset,
line,
column,
},
));
chars.next();
offset += 1;
let breaks = c == '\n' || (c == '\r' && chars.peek().map(|&(_, n)| n) != Some('\n'));
if breaks {
line += 1;
column = 1;
} else {
column += 1;
}
}
}
#[test]
fn positions_count_code_points_across_checkpoints_and_long_lines() {
let mut text = String::new();
for i in 0..300 {
text.push_str(["ab\n", "\u{e4}\u{20ac}x\r\n", "\u{1f600}\r", "\t\u{3b1}\n"][i % 4]);
}
for _ in 0..CHECKPOINT_BYTES {
text.push_str("a\u{e9}");
}
let lines = LineIndex::new(&text);
let mut cursor = LineCursor::new(&lines, &text);
for (k, (at, expected)) in counted(&text).into_iter().enumerate() {
assert_eq!(cursor.position(at), expected, "the cursor at byte {at}");
if k % 13 == 0 {
assert_eq!(lines.position(&text, at), expected, "at byte {at}");
assert_eq!(lines.byte_offset(&text, expected.offset), at);
}
}
let (back, expected) = counted(&text)[5];
assert_eq!(cursor.position(back), expected);
let (on, expected) = counted(&text)[9];
assert_eq!(cursor.position(on), expected);
}
#[test]
fn an_ascii_text_counts_code_points_as_bytes() {
let text = "one\ntwo\r\nthree";
let lines = LineIndex::new(text);
let mut cursor = LineCursor::new(&lines, text);
for (at, expected) in counted(text) {
assert_eq!(lines.position(text, at), expected);
assert_eq!(cursor.position(at), expected);
assert_eq!(lines.byte_offset(text, expected.offset), at);
}
assert_eq!(lines.byte_offset(text, text.len() + 1), text.len());
}
}