byte_offset_to_line

Function byte_offset_to_line 

Source
pub fn byte_offset_to_line(source: &str, offset: usize) -> usize
Expand description

Convert a byte offset to a 1-based line number.

Counts the number of newlines before the given byte offset and adds 1. If offset exceeds source.len(), it is clamped to source.len().

Performance note: This function is O(n) in the source length. For repeated lookups on the same source, use LineIndex for O(log n) lookups.