Skip to main content

line_col

Function line_col 

Source
pub fn line_col(content: &str, offset: usize) -> (usize, usize)
Expand description

A byte offset into 1-based (line, column).

The column counts CHARACTERS, not bytes. Editors place a cursor by character, so a byte column puts the caret in the wrong place on any line containing a non-ASCII character — an accented identifier, a comment in a language that is not English, an emoji in a string. Byte offsets are what the matchers produce and characters are what the reader wants, so the conversion belongs here rather than at each call site.

An offset past the end clamps to the last position rather than panicking: this is a reporting path, and a wrong column is a far better outcome than taking down a commit hook.