Struct xi_rope::Rope
[−]
[src]
pub struct Rope { // some fields omitted }
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 reference counting (not thread safe, though
it would be easy enough to modify to use Arc
instead of Rc
if that were
required). 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" == 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_str(1, 9, "era"); assert_eq!("herald", String::from(a));
Methods
impl Rope
[src]
fn len(&self) -> usize
Returns the length of self
.
The length is in bytes, the same as str::len()
.
Time complexity: O(1)
fn push_to_string(&self, dst: &mut String)
Appends self
to the destination string.
Time complexity: effectively O(n)
fn slice(self, start: usize, end: usize) -> Rope
Returns a slice of the string from the byte range [start
..end
).
Time complexity: O(log n)
fn push_str(&mut self, s: &str)
Append s
to the string.
fn edit(&mut self, start: usize, end: usize, new: Rope)
Edit the string, replacing the byte range [start
..end
] with new
.
Note: edit
and edit_str
may be merged, using traits.
Time complexity: O(log n)
fn edit_str(&mut self, start: usize, end: usize, new: &str)
Edit the string, replacing the byte range [start
..end
] with new
.
Note: edit
and edit_str
may be merged, using traits.
Time complexity: O(log n)
fn iter_chunks(&self) -> 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.
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)
fn offset_of_line(&self, line: usize) -> usize
Return the byte offset corresponding to the line number line
.
The line number is 0-based.
Time complexity: O(log n)
fn lines_raw(&self) -> 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.
fn lines(&self) -> 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()
.
fn prev_codepoint_offset(&self, offset: usize) -> Option<usize>
Return the offset of the codepoint before offset
.
fn next_codepoint_offset(&self, offset: usize) -> Option<usize>
Return the offset of the codepoint after offset
.
fn prev_grapheme_offset(&self, offset: usize) -> Option<usize>
fn next_grapheme_offset(&self, offset: usize) -> Option<usize>
fn byte_at(&self, offset: usize) -> u8
Trait Implementations
impl Clone for Rope
[src]
fn clone(&self) -> Rope
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<T: AsRef<str>> From<T> for Rope
[src]
impl Add<Rope> for Rope
[src]
type Output = Rope
The resulting type after applying the +
operator
fn add(self, rhs: Rope) -> Rope
The method for the +
operator
impl<T: AsRef<str>> Add<T> for Rope
[src]
type Output = Rope
The resulting type after applying the +
operator
fn add(self, rhs: T) -> Rope
The method for the +
operator
impl PartialEq for Rope
[src]
fn eq(&self, rhs: &Rope) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl Eq for Rope
[src]
impl PartialEq<str> for Rope
[src]
fn eq(&self, rhs: &str) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl<'a> PartialEq<&'a str> for Rope
[src]
fn eq(&self, rhs: &&str) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl PartialEq<String> for Rope
[src]
fn eq(&self, rhs: &String) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.