Skip to main content

linecol

Function linecol 

Source
pub fn linecol(src: &str, byte_offset: usize) -> (usize, usize)
Expand description

Convert a byte offset into a 1-based (line, column) pair.

src is treated as UTF-8; columns are counted in Unicode scalar values (i.e. chars), not bytes, so a span pointing at the byte after µ reports column 2 rather than 3. Both the returned line and column are at least 1, and offsets past the end of src are clamped to the end. Offsets that fall in the middle of a UTF-8 code-point round down to the start of that code-point.

§Examples

use mos_core::linecol;

assert_eq!(linecol("a\nb", 2), (2, 1));