pub struct LineIndex { /* private fields */ }Expand description
Pre-computed line index for O(log n) line/column lookups.
Instead of iterating from the start of the file for each lookup, we build an index of line start positions once and use binary search.
Implementations§
Source§impl LineIndex
impl LineIndex
Sourcepub fn new(source: &str) -> Self
pub fn new(source: &str) -> Self
Build a line index from source text.
Time: O(n) where n is the length of the source Space: O(lines) for storing line start positions
Sourcepub fn line_col(&self, offset: usize) -> (usize, usize)
pub fn line_col(&self, offset: usize) -> (usize, usize)
Convert a byte offset to (line, column), both 1-indexed.
Time: O(log n) where n is the number of lines
§Panics
Does not panic; returns the last valid position if offset is past end.
Sourcepub fn line_start(&self, line: usize) -> Option<usize>
pub fn line_start(&self, line: usize) -> Option<usize>
Get the byte offset where a line starts (1-indexed line number).
Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Get the total number of lines.
Auto Trait Implementations§
impl Freeze for LineIndex
impl RefUnwindSafe for LineIndex
impl Send for LineIndex
impl Sync for LineIndex
impl Unpin 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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more