Crate line_numbers

Source
Expand description

Efficiently find line numbers and line spans within a string.

use line_numbers::LinePositions;

let s = "foo\nbar\nbaz\n";
let s_lines: Vec<_> = s.lines().collect();

let line_positions = LinePositions::from(s);

let offset = 5;
let (line_num, column) = line_positions.from_offset(offset);

println!(
    "Offset {} is on line {} (column {}), and the text of that line is {:?}.",
    offset,
    line_num.display(),
    column,
    s_lines[line_num.as_usize()]
);

Structsยง

LineNumber
A distinct number type for line numbers, to prevent confusion with other numerical data.
LinePositions
A struct for efficiently converting absolute string positions to line-relative positions.
SingleLineSpan
A range within a single line of a string.