Skip to main content

offset_to_byte

Function offset_to_byte 

Source
pub fn offset_to_byte(
    line: &str,
    offset: usize,
    encoding: PositionEncoding,
) -> usize
Expand description

Map a position offset (in the given encoding) into a byte offset into line, clamped to a char boundary.

  • PositionEncoding::Utf8 / PositionEncoding::Utf16 walk the line’s chars once accumulating the encoded length, bailing at the start of the char a mid-char offset would land in (the LSP behavior).
  • PositionEncoding::Char treats offset as a character index and maps to the byte offset of that char, clamping past end-of-line to the line length (the #1289 WASM behavior).

The result is always a valid char boundary in line, so slicing &line[..offset_to_byte(...)] never panics.