Installation
[]
= "0.2"
no_std targets disable the default std feature; the crate then relies only on core and alloc:
= { = "0.2", = false }
Performance
A Span is a Copy value (two packed 32-bit offsets, eight bytes), and line/column resolution is a binary search over line starts — O(log lines), never a re-scan of the source. Latest local Criterion means (cargo bench, Windows x86_64, Rust stable):
Span::merge— ~0.6 ns/opLineIndex::offset(line/col → byte) — ~2.5 ns/opLineIndex::line_col(byte → line/col) — ~8.7 ns/opLineIndex::new— ~8.4 µs to index 1 000 lines (the onlyO(n)operation; lookups allocate nothing)
Features
BytePos— a 4-byteCopybyte offset; the atom every span is built from.Span— a half-openstart..endbyte range withlen,is_empty,contains, ordering, and an associative, commutativemerge. Thestart <= endinvariant is enforced at construction.LineCol— a resolved 1-based line/column, where the column counts Unicode scalar values (never bytes, never inside a multi-byte sequence).LineIndex— built once per source; mapsBytePos↔LineColinO(log lines), handling\nand\r\nuniformly, with no allocation on the lookup path.
Correctness is held to the project invariants by property tests cross-checked against a naive reference resolver over UTF-8 input including multi-byte characters and CRLF.
Usage
use ;
let src = "fn main() {\n work();\n}\n";
// Spans are half-open byte ranges; merge covers both inputs.
let call = new;
assert_eq!;
// Resolve a byte offset to a human (line, column) coordinate.
let index = new;
let lc = index.line_col;
assert_eq!;
// The mapping is reversible.
assert_eq!;
API Overview
For a complete reference with examples, see docs/API.md.
BytePos— a byte offset into one source.Span— a half-open byte range withmerge,contains, and ordering.LineCol— a resolved 1-based line/column coordinate.LineIndex— byte ↔ line/column resolution inO(log lines).
Contributing
See dev/DIRECTIVES.md for engineering standards and the definition of done. Before a PR: cargo fmt --all, cargo clippy --all-targets --all-features -- -D warnings, and cargo test --all-features must be clean.