Skip to main content

Utf16Index

Struct Utf16Index 

Source
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

Source

pub fn new(text: &str) -> Self

Index text. Reads it once.

Source

pub fn char_count(&self) -> usize

The length of the text, in characters.

Source

pub fn utf16_len(&self) -> usize

The length of the text, in UTF-16 code units.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Utf16Index

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Utf16Index

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Utf16Index

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Utf16Index

Source§

impl PartialEq for Utf16Index

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Utf16Index

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.