pub struct LineIndex { /* private fields */ }Expand description
Maps byte offsets in a source string to line and column positions.
Lines and columns are 0-indexed internally. Use LineIndex::line_col
to get a (line, col) pair and add 1 when displaying to users.
The index is built in O(n) time and each lookup is O(log n) via binary search. This struct is intentionally dependency-free so it can be reused by WASM bindings, an LSP server, or any other consumer.
Implementations§
Source§impl LineIndex
impl LineIndex
Sourcepub fn line_col(&self, offset: usize) -> (usize, usize)
pub fn line_col(&self, offset: usize) -> (usize, usize)
Convert a byte offset to a 0-indexed (line, column) pair.
If offset is past the end of the source, the last line is returned
with the column clamped to the line length.
Sourcepub fn line_start(&self, line: usize) -> Option<usize>
pub fn line_start(&self, line: usize) -> Option<usize>
Byte offset of the start of the given 0-indexed line.
Returns None if line is out of bounds.
Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Total number of lines (at least 1 for non-empty or even empty input).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for LineIndex
impl RefUnwindSafe for LineIndex
impl Send for LineIndex
impl Sync for LineIndex
impl Unpin for LineIndex
impl UnsafeUnpin for LineIndex
impl UnwindSafe for LineIndex
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more