[−][src]Type Definition xi_rope::rope::Rope
type Rope = Node<RopeInfo>;
A rope data structure.
A rope is a data structure for strings, specialized for incremental editing operations. Most operations (such as insert, delete, substring) are O(log n). This module provides an immutable (also known as persistent) version of Ropes, and if there are many copies of similar strings, the common parts are shared.
Internally, the implementation uses thread safe reference counting. Mutations are generally copy-on-write, though in-place edits are supported as an optimization when only one reference exists, making the implementation as efficient as a mutable version.
Also note: in addition to the From
traits described below, this module
implements From<Rope> for String
and From<&Rope> for String
, for easy
conversions in both directions.
Examples
Create a Rope
from a String
:
let a = Rope::from("hello "); let b = Rope::from("world"); assert_eq!("hello world", String::from(a.clone() + b.clone())); assert!("hello world" == String::from(a + b));
Get a slice of a Rope
:
let a = Rope::from("hello world"); let b = a.slice(1..9); assert_eq!("ello wor", String::from(&b)); let c = b.slice(1..7); assert_eq!("llo wo", String::from(c));
Replace part of a Rope
:
let mut a = Rope::from("hello world"); a.edit(1..9, "era"); assert_eq!("herald", String::from(a));
Methods
impl Rope
[src]
pub fn edit_str<T: IntervalBounds>(&mut self, iv: T, new: &str)
[src]
Use Rope::edit instead
Edit the string, replacing the byte range [start
..end
] with new
.
Time complexity: O(log n)
pub fn slice<T: IntervalBounds>(&self, iv: T) -> Rope
[src]
Returns a new Rope with the contents of the provided range.
pub fn is_codepoint_boundary(&self, offset: usize) -> bool
[src]
Determine whether offset
lies on a codepoint boundary.
pub fn prev_codepoint_offset(&self, offset: usize) -> Option<usize>
[src]
Return the offset of the codepoint before offset
.
pub fn next_codepoint_offset(&self, offset: usize) -> Option<usize>
[src]
Return the offset of the codepoint after offset
.
pub fn at_or_next_codepoint_boundary(&self, offset: usize) -> Option<usize>
[src]
Returns offset
if it lies on a codepoint boundary. Otherwise returns
the codepoint after offset
.
pub fn at_or_prev_codepoint_boundary(&self, offset: usize) -> Option<usize>
[src]
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>
[src]
pub fn next_grapheme_offset(&self, offset: usize) -> Option<usize>
[src]
pub fn line_of_offset(&self, offset: usize) -> usize
[src]
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.
pub fn offset_of_line(&self, line: usize) -> usize
[src]
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.
ⓘImportant traits for ChunkIter<'a>pub fn iter_chunks<T: IntervalBounds>(&self, range: T) -> ChunkIter
[src]
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.
ⓘImportant traits for LinesRaw<'a>pub fn lines_raw<T: IntervalBounds>(&self, range: T) -> LinesRaw
[src]
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.
ⓘImportant traits for Lines<'a>pub fn lines<T: IntervalBounds>(&self, range: T) -> Lines
[src]
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()
.