MCPcopy Create free account
hub / github.com/PHPantom-dev/phpantom_lsp / utf16_col_to_byte_offset

Function utf16_col_to_byte_offset

src/util.rs:383–392  ·  view source on GitHub ↗

Convert a UTF-16 column offset to a byte offset within a single line. LSP positions use UTF-16 code units for the character offset. When a line contains multi-byte characters (e.g. `ń` is 2 bytes in UTF-8 but 1 UTF-16 code unit), the two offsets diverge. This helper walks the line counting UTF-16 code units and returns the corresponding byte position. Returns `line.len()` if `utf16_col` is pas

(line: &str, utf16_col: u32)

Source from the content-addressed store, hash-verified

381///
382/// Returns `line.len()` if `utf16_col` is past the end of the line.
383pub(crate) fn utf16_col_to_byte_offset(line: &str, utf16_col: u32) -> usize {
384 let mut col = 0u32;
385 for (i, ch) in line.char_indices() {
386 if col == utf16_col {
387 return i;
388 }
389 col += ch.len_utf16() as u32;
390 }
391 line.len()
392}
393
394/// Convert a byte offset within a single line to a UTF-16 column offset.
395///

Callers 2

detect_docblock_triggerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected