pub struct Utf16Index { /* private fields */ }Expand description
Character offsets and UTF-16 offsets over one text, both ways.
A character takes one UTF-16 code unit unless it is outside the Basic
Multilingual Plane (above U+FFFF, most emoji), when it takes two, a
surrogate pair. So the index keeps only where those characters are.
§Invariant
supplementary holds, in ascending order, the character offset of every
character above U+FFFF. The UTF-16 offset of character offset c is
c plus the number of entries below c, and the k-th entry (from 0)
starts at UTF-16 offset supplementary[k] + k, which also ascends. Both
conversions are a binary search over it: O(log s) for s such
characters, nothing at all for a text with none, and the index takes
s words. Building it reads the text once.
§Rounding
A UTF-16 offset can fall between the two halves of a surrogate pair, in the middle of a character, where no character offset is. A single offset is moved back to the start of that character; a range is widened to take in the whole character at either end. Every conversion clamps to the text.
Implementations§
Source§impl Utf16Index
impl Utf16Index
Sourcepub fn char_count(&self) -> usize
pub fn char_count(&self) -> usize
The length of the text, in characters.
Sourcepub fn char_to_utf16(&self, char_offset: usize) -> usize
pub fn char_to_utf16(&self, char_offset: usize) -> usize
The UTF-16 offset of character offset char_offset, clamped to the
end of the text.
Sourcepub fn utf16_to_char(&self, utf16_offset: usize) -> usize
pub fn utf16_to_char(&self, utf16_offset: usize) -> usize
The character offset of UTF-16 offset utf16_offset, clamped to the
end of the text. An offset between the halves of a surrogate pair is
the start of that character.
Sourcepub fn char_range_to_utf16(&self, range: Range<usize>) -> Range<usize> ⓘ
pub fn char_range_to_utf16(&self, range: Range<usize>) -> Range<usize> ⓘ
A character range as the UTF-16 range over the same characters. Both ends are clamped; an end before the start is taken as the start.
Sourcepub fn utf16_range_to_chars(&self, range: Range<usize>) -> Range<usize> ⓘ
pub fn utf16_range_to_chars(&self, range: Range<usize>) -> Range<usize> ⓘ
A UTF-16 range as the character range over the same characters, widened to whole characters: a start in the middle of a surrogate pair moves back to the character’s start, an end in the middle moves on past the character. An empty range stays empty, an insertion point at its start’s character offset, so it never takes in a character it only touched. Both ends are clamped; an end before the start is taken as the start.