Skip to main content

utf8_to_utf16_offset

Function utf8_to_utf16_offset 

Source
pub fn utf8_to_utf16_offset(text: &str, utf8_offset: usize) -> usize
Expand description

Converts a UTF-8 byte offset into a UTF-16 code-unit offset.

Platform text input counts in UTF-16 code units, while Rust strings index by UTF-8 bytes. Use this to translate an offset in a String into the offset the platform expects. An offset past the end returns the UTF-16 length of text. An offset inside a character returns the offset of that character’s end.

§Examples

use gpui::utf8_to_utf16_offset;

// "A" is 1 byte and 1 code unit. The emoji is 4 bytes and 2 code units.
assert_eq!(utf8_to_utf16_offset("A😀B", 5), 3);
assert_eq!(utf8_to_utf16_offset("A😀B", 100), 4);