Skip to main content

Text

Struct Text 

Source
pub struct Text { /* private fields */ }
Expand description

A finite sequence of Unicode code points, stored as UTF-8.

Length, indexing and slicing count code points. Nothing is normalized: a Text is exactly the code points that built it, and equality and order compare code points. UTF-8 preserves code-point order under byte comparison, so comparing the bytes is data.md’s lexicographic order by code point, with a proper prefix first.

A text is a balanced tree of runs, each a window onto a shared block of UTF-8, kept in the tree every list, dictionary and set keeps its elements in, each leaf one run and each node recording how many bytes and code points lie below it. Joining two texts copies neither unless both are short, and cutting a part out of one shares every block and node it keeps, so a text made from others shares what they have in common, in memory and in what history stores for it (data.md, Sharing; trace.md, Texts). For a text of n runs:

  • finding a code-point position walks down the tree, then through one run, in O(log n + STRIDE): a run whose every code point is ASCII is indexed in constant time, and another through a table of every STRIDEth code point’s place, made the first time the run is indexed;
  • finding a byte position costs O(log n);
  • joining texts whose trees are h₁ and h₂ high costs O(|h₁ − h₂| + 1), copying at most 64 bytes, and so k joins O(k log n);
  • Text::as_str joins the runs of a text of more than one once, in O(bytes), and keeps the result where every copy of the value finds it, so code that needs one contiguous string pays for it once.

Implementations§

Source§

impl Text

Source

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

Returns the text holding exactly code_points.

Source

pub fn root(&self) -> Option<&Node<Run>>

Returns the tree of the text’s runs, or None when it holds none.

Source

pub fn run(&self) -> Option<&Run>

Returns the text’s one run, when it is made of exactly one.

Source

pub fn as_str(&self) -> &str

Returns the text’s UTF-8.

Source

pub fn to_shared(&self) -> Arc<str> ⓘ

Returns the text’s UTF-8 as a shared string: the block itself when the text is a whole block, and otherwise its UTF-8, made once when the text has more than one run.

Source

pub fn blocks(&self) -> impl Iterator<Item = &str>

Returns the blocks of UTF-8 the text is made of, in order.

Source

pub fn len(&self) -> usize

Returns how many code points the text holds.

Source

pub fn is_empty(&self) -> bool

Returns whether the text holds none.

Source

pub fn byte_len(&self) -> usize

Returns how many bytes the text’s UTF-8 takes.

Source

pub fn of_block( block: Block, from: usize, to: usize, ) -> Result<Text, StructureError>

Returns the run of block’s bytes from from up to to, sharing the block.

Counting the run’s code points costs time linear in its length.

§Errors

StructureError::Run unless from ≤ to ≤ block.len() and both fall between characters.

Source

pub fn join_shared(left: Text, right: Text) -> Text

Returns left’s code points, then right’s, keeping both texts as they are, however short.

Text::concat copies two short texts into one run; this never copies, so a text rebuilt from history shares every run and node it was rebuilt from. An empty half is no part of the tree, so joining one returns the other. The two trees are joined as Node::branch joins any two, keeping the result balanced.

Source

pub fn byte_window( &self, from: usize, to: usize, ) -> Result<Text, StructureError>

Returns the bytes of the text from from up to to, sharing every block and node it keeps.

§Errors

StructureError::Run unless from ≤ to ≤ the text’s byte length and both fall between characters.

Source

pub fn concat(&self, other: &Text) -> Text

Returns self + other, sharing both.

Two trees of at most 64 bytes together that the join meets, the two texts themselves or a subtree of the higher and the lower where it lands, are copied into one run; the rest of either is shared.

Source

pub fn slice(&self, start: &Int, end: &Int) -> Option<Text>

Returns the code points from start up to, not including, end, or None when 0 ≤ start ≤ end ≤ length does not hold.

Source

pub fn find_from(&self, needle: &Text, from: &Int) -> Option<usize>

Returns the code-point index of the first occurrence of needle at or after code point from, or None when there is none or from lies past the end. The empty text occurs at every index up to the length.

Source

pub fn code_points(&self) -> Vec<Text>

Returns each code point as a one-code-point text, in order.

Source

pub fn graphemes(&self) -> Vec<Text>

Returns each extended grapheme cluster, in order (text.graphemes).

Source

pub fn normalize(&self) -> Text

Returns the text in Unicode normalization form C (text.normalize).

Source

pub fn lowercase(&self) -> Text

Returns the text under Unicode’s default, locale-independent full lowercase mapping, with its one conditional rule, Final_Sigma: a capital sigma that ends a word lowercases to the final form (text.lowercase).

Source

pub fn uppercase(&self) -> Text

Returns the text under Unicode’s default, locale-independent full uppercase mapping, which has no conditional rule (text.uppercase).

Source

pub fn to_bytes(&self) -> Bytes

Returns the text’s UTF-8 encoding.

Trait Implementations§

Source§

impl Clone for Text

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 Text

Source§

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

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

impl Default for Text

Source§

fn default() -> Self

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

impl Display for Text

Source§

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

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

impl Eq for Text

Source§

impl From<Node<Run>> for Text

Source§

fn from(root: Node<Run>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Text

Source§

fn from(code_points: String) -> Self

Converts to this type from the input type.
Source§

impl Ord for Text

Source§

fn cmp(&self, other: &Text) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Text

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Text

Source§

fn partial_cmp(&self, other: &Text) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

§

impl Forget for Text

§

impl Freeze for Text
where *const ArcInner<Inner<Run>> is !null: Freeze, PhantomData<ArcInner<Inner<Run>>>: Freeze, *const ArcInner<OnceLock<Arc<str>>> is !null: Freeze, PhantomData<ArcInner<OnceLock<Arc<str>>>>: Freeze,

§

impl Move for Text

§

impl RefUnwindSafe for Text

§

impl Send for Text
where Arc<Inner<Run>>: Send, Arc<OnceLock<Arc<str>>>: Send,

§

impl Sync for Text
where Arc<Inner<Run>>: Sync, Arc<OnceLock<Arc<str>>>: Sync,

§

impl Unpin for Text
where Arc<Inner<Run>>: Unpin, Arc<OnceLock<Arc<str>>>: Unpin,

§

impl UnsafeUnpin for Text
where *const ArcInner<Inner<Run>> is !null: UnsafeUnpin, PhantomData<ArcInner<Inner<Run>>>: UnsafeUnpin, *const ArcInner<OnceLock<Arc<str>>> is !null: UnsafeUnpin, PhantomData<ArcInner<OnceLock<Arc<str>>>>: UnsafeUnpin,

§

impl UnwindSafe for Text

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<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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.