Expand description
§index-to-position — convert a string index to a line and column
Given a byte offset into a string, find its line and column. Useful for turning a parser
or lexer error offset into a human-readable line:column. A Rust port of the
index-to-position npm package.
use index_to_position::index_to_position;
let text = "foo\nbar\nbaz";
let pos = index_to_position(text, 5); // the 'a' in "bar"
assert_eq!((pos.line, pos.column), (1, 1));Indices are byte offsets (the idiomatic Rust string index, as produced by str
slicing and most parsers), and lines and columns are zero-based by default. Use
index_to_position_with for one-based output, and PositionFinder when you need to
resolve many indices into the same text efficiently.
use index_to_position::{index_to_position_with, Options};
let pos = index_to_position_with("foo\nbar", 4, Options::new().one_based(true));
assert_eq!((pos.line, pos.column), (2, 1));Zero dependencies and #![no_std].
Structs§
- Options
- Whether the line and/or column should be one-based.
- Position
- A line and column within a string.
- Position
Finder - Resolves many byte indices into the same text efficiently.
Functions§
- index_
to_ position - Convert a byte
indexintotextto a zero-basedPosition. - index_
to_ position_ with - Convert a byte
indexintotextto aPositionwith the givenOptions.