pub struct Node<N: NodeInfo>(/* private fields */);Expand description
A b-tree node storing leaves at the bottom, and with info retained at each node. It is implemented with atomic reference counting and copy-on-write semantics, so an immutable clone is a very cheap operation, and nodes can be shared across threads. Even so, it is designed to be updated in place, with efficiency similar to a mutable data structure, using uniqueness of reference count to detect when this operation is safe.
When the leaf is a string, this is a rope data structure (a persistent rope in functional programming jargon). However, it is not restricted to strings, and it is expected to be the basis for a number of data structures useful for text processing.
Implementations§
Source§impl Node<BreaksInfo>
impl Node<BreaksInfo>
pub fn new_no_break(len: usize) -> Breaks
Source§impl Node<RopeInfo>
impl Node<RopeInfo>
Sourcepub fn edit_str<T: IntervalBounds>(&mut self, iv: T, new: &str)
👎Deprecated since 0.3.0: Use Rope::edit instead
pub fn edit_str<T: IntervalBounds>(&mut self, iv: T, new: &str)
Edit the string, replacing the byte range [start..end] with new.
Time complexity: O(log n)
Sourcepub fn slice<T: IntervalBounds>(&self, iv: T) -> Rope
pub fn slice<T: IntervalBounds>(&self, iv: T) -> Rope
Returns a new Rope with the contents of the provided range.
Sourcepub fn is_codepoint_boundary(&self, offset: usize) -> bool
pub fn is_codepoint_boundary(&self, offset: usize) -> bool
Determine whether offset lies on a codepoint boundary.
Sourcepub fn prev_codepoint_offset(&self, offset: usize) -> Option<usize>
pub fn prev_codepoint_offset(&self, offset: usize) -> Option<usize>
Return the offset of the codepoint before offset.
Sourcepub fn next_codepoint_offset(&self, offset: usize) -> Option<usize>
pub fn next_codepoint_offset(&self, offset: usize) -> Option<usize>
Return the offset of the codepoint after offset.
Sourcepub fn at_or_next_codepoint_boundary(&self, offset: usize) -> Option<usize>
pub fn at_or_next_codepoint_boundary(&self, offset: usize) -> Option<usize>
Returns offset if it lies on a codepoint boundary. Otherwise returns
the codepoint after offset.
Sourcepub fn at_or_prev_codepoint_boundary(&self, offset: usize) -> Option<usize>
pub fn at_or_prev_codepoint_boundary(&self, offset: usize) -> Option<usize>
Returns offset if it lies on a codepoint boundary. Otherwise returns
the codepoint before offset.
pub fn prev_grapheme_offset(&self, offset: usize) -> Option<usize>
pub fn next_grapheme_offset(&self, offset: usize) -> Option<usize>
Sourcepub fn line_of_offset(&self, offset: usize) -> usize
pub fn line_of_offset(&self, offset: usize) -> usize
Return the line number corresponding to the byte index offset.
The line number is 0-based, thus this is equivalent to the count of newlines
in the slice up to offset.
Time complexity: O(log n)
§Panics
This function will panic if offset > self.len(). Callers are expected to
validate their input.
Sourcepub fn offset_of_line(&self, line: usize) -> usize
pub fn offset_of_line(&self, line: usize) -> usize
Return the byte offset corresponding to the line number line.
If line is equal to one plus the current number of lines,
this returns the offset of the end of the rope. Arguments higher
than this will panic.
The line number is 0-based.
Time complexity: O(log n)
§Panics
This function will panic if line > self.measure::<LinesMetric>() + 1.
Callers are expected to validate their input.
Sourcepub fn iter_chunks<T: IntervalBounds>(&self, range: T) -> ChunkIter<'_> ⓘ
pub fn iter_chunks<T: IntervalBounds>(&self, range: T) -> ChunkIter<'_> ⓘ
Returns an iterator over chunks of the rope.
Each chunk is a &str slice borrowed from the rope’s storage. The size
of the chunks is indeterminate but for large strings will generally be
in the range of 511-1024 bytes.
The empty string will yield a single empty slice. In all other cases, the slices will be nonempty.
Time complexity: technically O(n log n), but the constant factor is so tiny it is effectively O(n). This iterator does not allocate.
Sourcepub fn lines_raw<T: IntervalBounds>(&self, range: T) -> LinesRaw<'_> ⓘ
pub fn lines_raw<T: IntervalBounds>(&self, range: T) -> LinesRaw<'_> ⓘ
An iterator over the raw lines. The lines, except the last, include the terminating newline.
The return type is a Cow<str>, and in most cases the lines are slices
borrowed from the rope.
Sourcepub fn lines<T: IntervalBounds>(&self, range: T) -> Lines<'_> ⓘ
pub fn lines<T: IntervalBounds>(&self, range: T) -> Lines<'_> ⓘ
An iterator over the lines of a rope.
Lines are ended with either Unix (\n) or MS-DOS (\r\n) style line endings.
The line ending is stripped from the resulting string. The final line ending
is optional.
The return type is a Cow<str>, and in most cases the lines are slices borrowed
from the rope.
The semantics are intended to match str::lines().
pub fn byte_at(&self, offset: usize) -> u8
pub fn slice_to_cow<T: IntervalBounds>(&self, range: T) -> Cow<'_, str>
Source§impl<T: Clone> Node<SpansInfo<T>>
impl<T: Clone> Node<SpansInfo<T>>
Sourcepub fn transform<N: NodeInfo>(
&self,
base_start: usize,
base_end: usize,
xform: &mut Transformer<'_, N>,
) -> Self
pub fn transform<N: NodeInfo>( &self, base_start: usize, base_end: usize, xform: &mut Transformer<'_, N>, ) -> Self
Perform operational transformation on a spans object intended to be edited into a sequence at the given offset.
Sourcepub fn merge<F, O>(&self, other: &Self, f: F) -> Spans<O>
pub fn merge<F, O>(&self, other: &Self, f: F) -> Spans<O>
Creates a new Spans instance by merging spans from other with self,
using a closure to transform values.
New spans are created from non-overlapping regions of existing spans, and by combining overlapping regions into new spans. In all cases, new values are generated by calling a closure that transforms the value of the existing span or spans.
§Panics
Panics if self and other have different lengths.
pub fn iter(&self) -> SpanIter<'_, T> ⓘ
pub fn iter_chunks<I: IntervalBounds>(&self, range: I) -> SpanIter<'_, T> ⓘ
Sourcepub fn apply_shape<M: NodeInfo>(&mut self, delta: &Delta<M>)
pub fn apply_shape<M: NodeInfo>(&mut self, delta: &Delta<M>)
Applies a generic delta to self, inserting empty spans for any
added regions.
This is intended to be used to keep spans up to date with a Rope
as edits occur.
Sourcepub fn delete_after(&mut self, interval: Interval)
pub fn delete_after(&mut self, interval: Interval)
Deletes all spans that intersect with interval and that come after.
Source§impl<N: NodeInfo> Node<N>
impl<N: NodeInfo> Node<N>
pub fn from_leaf(l: N::L) -> Node<N>
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Sourcepub fn ptr_eq(&self, other: &Self) -> bool
pub fn ptr_eq(&self, other: &Self) -> bool
Returns true if these two Nodes share the same underlying data.
This is principally intended to be used by the druid crate, without needing
to actually add a feature and implement druid’s Data trait.
pub fn concat(rope1: Node<N>, rope2: Node<N>) -> Node<N>
pub fn measure<M: Metric<N>>(&self) -> usize
pub fn subseq<T: IntervalBounds>(&self, iv: T) -> Node<N>
pub fn edit<T, IV>(&mut self, iv: IV, new: T)
pub fn convert_metrics<M1: Metric<N>, M2: Metric<N>>(&self, m1: usize) -> usize
Source§impl<N: DefaultMetric> Node<N>
impl<N: DefaultMetric> Node<N>
Sourcepub fn count<M: Metric<N>>(&self, offset: usize) -> usize
pub fn count<M: Metric<N>>(&self, offset: usize) -> usize
Measures the length of the text bounded by DefaultMetric::measure(offset) with another metric.
§Examples
use crate::xi_rope::{Rope, LinesMetric};
// the default metric of Rope is BaseMetric (aka number of bytes)
let my_rope = Rope::from("first line \n second line \n");
// count the number of lines in my_rope
let num_lines = my_rope.count::<LinesMetric>(my_rope.len());
assert_eq!(2, num_lines);Sourcepub fn count_base_units<M: Metric<N>>(&self, offset: usize) -> usize
pub fn count_base_units<M: Metric<N>>(&self, offset: usize) -> usize
Measures the length of the text bounded by M::measure(offset) with the default metric.
§Examples
use crate::xi_rope::{Rope, LinesMetric};
// the default metric of Rope is BaseMetric (aka number of bytes)
let my_rope = Rope::from("first line \n second line \n");
// get the byte offset of the line at index 1
let byte_offset = my_rope.count_base_units::<LinesMetric>(1);
assert_eq!(12, byte_offset);