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₁andh₂high costs O(|h₁ − h₂| + 1), copying at most 64 bytes, and sokjoins O(k log n); Text::as_strjoins 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
impl Text
Sourcepub fn root(&self) -> Option<&Node<Run>>
pub fn root(&self) -> Option<&Node<Run>>
Returns the tree of the text’s runs, or None when it holds none.
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.
Sourcepub fn blocks(&self) -> impl Iterator<Item = &str>
pub fn blocks(&self) -> impl Iterator<Item = &str>
Returns the blocks of UTF-8 the text is made of, in order.
Sourcepub fn of_block(
block: Block,
from: usize,
to: usize,
) -> Result<Text, StructureError>
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.
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.
Sourcepub fn byte_window(
&self,
from: usize,
to: usize,
) -> Result<Text, StructureError>
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.
Sourcepub fn concat(&self, other: &Text) -> Text
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.
Sourcepub fn slice(&self, start: &Int, end: &Int) -> Option<Text>
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.
Sourcepub fn find_from(&self, needle: &Text, from: &Int) -> Option<usize>
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.
Sourcepub fn code_points(&self) -> Vec<Text>
pub fn code_points(&self) -> Vec<Text>
Returns each code point as a one-code-point text, in order.
Sourcepub fn graphemes(&self) -> Vec<Text>
pub fn graphemes(&self) -> Vec<Text>
Returns each extended grapheme cluster, in order (text.graphemes).
Sourcepub fn normalize(&self) -> Text
pub fn normalize(&self) -> Text
Returns the text in Unicode normalization form C (text.normalize).
Sourcepub fn lowercase(&self) -> Text
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).