pub struct LineIndex { /* private fields */ }Expand description
a table of line-start byte offsets, built once from the source, used to map a byte offset to a 1-based line and column.
line_starts[0] is always 0 (the first line starts at the first byte); an
entry offset + 1 is pushed after each \n byte. \r\n needs no special
case: the \n ends the line and a lone \r is just whitespace inside the
preceding line.
Implementations§
Source§impl LineIndex
impl LineIndex
Sourcepub fn location(&self, src: &str, byte_offset: usize) -> (usize, usize)
pub fn location(&self, src: &str, byte_offset: usize) -> (usize, usize)
the 1-based line and 1-based column of byte_offset in src.
the column is a count of characters from the start of the line, so a tab
counts as one column, not eight. this is deliberate: the logical column is
character-based, and any tab-expanded display column is the renderer’s job
later. an offset at end of file returns a valid pair and never panics.
an offset beyond src.len() is clamped to src.len() rather than
panicking; this keeps WASM alive when a downstream caller (e.g. the
diagnostics layer) passes a span-derived offset that is slightly wrong.