Node

Struct Node 

Source
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>

Source§

impl Node<RopeInfo>

Source

pub fn edit_str<T: IntervalBounds>(&mut self, iv: T, new: &str)

👎Deprecated since 0.3.0: Use Rope::edit instead

Edit the string, replacing the byte range [start..end] with new.

Time complexity: O(log n)

Source

pub fn slice<T: IntervalBounds>(&self, iv: T) -> Rope

Returns a new Rope with the contents of the provided range.

Source

pub fn is_codepoint_boundary(&self, offset: usize) -> bool

Determine whether offset lies on a codepoint boundary.

Source

pub fn prev_codepoint_offset(&self, offset: usize) -> Option<usize>

Return the offset of the codepoint before offset.

Source

pub fn next_codepoint_offset(&self, offset: usize) -> Option<usize>

Return the offset of the codepoint after offset.

Source

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.

Source

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.

Source

pub fn prev_grapheme_offset(&self, offset: usize) -> Option<usize>

Source

pub fn next_grapheme_offset(&self, offset: usize) -> Option<usize>

Source

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.

Source

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.

Source

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.

Source

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.

Source

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().

Source

pub fn byte_at(&self, offset: usize) -> u8

Source

pub fn slice_to_cow<T: IntervalBounds>(&self, range: T) -> Cow<'_, str>

Source§

impl<T: Clone> Node<SpansInfo<T>>

Source

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.

Source

pub fn merge<F, O>(&self, other: &Self, f: F) -> Spans<O>
where F: FnMut(&T, Option<&T>) -> O, O: Clone,

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.

Source

pub fn iter(&self) -> SpanIter<'_, T>

Source

pub fn iter_chunks<I: IntervalBounds>(&self, range: I) -> SpanIter<'_, T>

Source

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.

Source

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>

Source

pub fn from_leaf(l: N::L) -> Node<N>

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

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.

Source

pub fn concat(rope1: Node<N>, rope2: Node<N>) -> Node<N>

Source

pub fn measure<M: Metric<N>>(&self) -> usize

Source

pub fn subseq<T: IntervalBounds>(&self, iv: T) -> Node<N>

Source

pub fn edit<T, IV>(&mut self, iv: IV, new: T)
where T: Into<Node<N>>, IV: IntervalBounds,

Source

pub fn convert_metrics<M1: Metric<N>, M2: Metric<N>>(&self, m1: usize) -> usize

Source§

impl<N: DefaultMetric> Node<N>

Source

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);
Source

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);

Trait Implementations§

Source§

impl<N: Clone + NodeInfo> Clone for Node<N>

Source§

fn clone(&self) -> Node<N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<N: NodeInfo> Default for Node<N>

Source§

fn default() -> Node<N>

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

impl<'a> From<&'a Node<RopeInfo>> for String

Source§

fn from(r: &Rope) -> String

Converts to this type from the input type.
Source§

impl From<Node<RopeInfo>> for String

Source§

fn from(r: Rope) -> String

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<N> Freeze for Node<N>

§

impl<N> RefUnwindSafe for Node<N>

§

impl<N> Send for Node<N>
where N: Sync + Send, <N as NodeInfo>::L: Sync + Send,

§

impl<N> Sync for Node<N>
where N: Sync + Send, <N as NodeInfo>::L: Sync + Send,

§

impl<N> Unpin for Node<N>

§

impl<N> UnwindSafe for Node<N>

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.