Skip to main content

byte_to_utf16_offset

Function byte_to_utf16_offset 

Source
pub fn byte_to_utf16_offset(s: &str, byte_offset: usize) -> u32
Expand description

Converts a byte offset within s to a UTF-16 code unit offset (LSP Position.character).

byte_offset must fall on a UTF-8 char boundary of s (e.g. one produced by str::find/rfind/slicing, never an arbitrary user-controlled value).

§Panics

Panics if byte_offset is out of bounds or does not fall on a UTF-8 char boundary of s.

§Examples

// ASCII: byte offset equals UTF-16 offset
assert_eq!(byte_to_utf16_offset("hello", 2), 2);

// Unicode: "日本語" - each char is 3 bytes but 1 UTF-16 code unit
assert_eq!(byte_to_utf16_offset("日本語", 0), 0);
assert_eq!(byte_to_utf16_offset("日本語", 3), 1);
assert_eq!(byte_to_utf16_offset("日本語", 6), 2);

// Emoji: "😀" is 4 bytes but 2 UTF-16 code units (surrogate pair)
assert_eq!(byte_to_utf16_offset("😀test", 4), 2);