Struct Rope
pub struct Rope { /* private fields */ }Expand description
A utf8 text rope.
The time complexity of nearly all edit and query operations on Rope are
worst-case O(log N) in the length of the rope. Rope is designed to work
efficiently even for huge (in the gigabytes) and pathological (all on one
line) texts.
§Editing Operations
The editing operations on Rope are insertion and removal of text. For
example:
let mut rope = Rope::from_str("Hello みんなさん!");
rope.remove(6..21);
rope.insert(6, "world");
assert_eq!(rope, "Hello world!");§Query Operations
Rope provides a rich set of efficient query functions, including querying
rope length in bytes/chars/lines, fetching individual chars or lines,
and converting between different indexing metrics.
For example, to find the starting byte index of a given line:
use ropey::LineType::LF_CR;
let rope = Rope::from_str("Hello みんなさん!\nHow are you?\nThis text has multiple lines!");
assert_eq!(rope.line_to_byte_idx(0, LF_CR), 0);
assert_eq!(rope.line_to_byte_idx(1, LF_CR), 23);
assert_eq!(rope.line_to_byte_idx(2, LF_CR), 36);§Slicing
You can take immutable slices of a Rope using slice():
let mut rope = Rope::from_str("Hello みんなさん!");
let middle = rope.slice(3..12);
assert_eq!(middle, "lo みん");§Cloning
Cloning Ropes is extremely cheap, running in O(1) time and taking a
small constant amount of memory for the new clone, regardless of text size.
This is accomplished by data sharing between Rope clones. The memory used
by clones only grows incrementally as the their contents diverge due to
edits. All of this is thread safe, so clones can be sent freely between
threads.
The primary intended use case for this feature is to allow asynchronous (in
the general sense, not necessarily in the async sense) processing of
Ropes. For example, saving a large document to disk in a separate thread
while the user continues to perform edits.
Implementations§
§impl Rope
impl Rope
pub fn from_reader<T>(reader: T) -> Result<Rope, Error>where
T: Read,
pub fn from_reader<T>(reader: T) -> Result<Rope, Error>where
T: Read,
Creates a Rope from the output of a reader.
This is a convenience function, and provides no specific guarantees about performance or internal implementation aside from the runtime complexity listed below.
When more precise control over IO behavior, buffering, etc. is desired,
you should handle IO yourself and use [RopeBuilder] to build the
Rope.
Runs in O(N) time.
§Errors
- If the reader returns an error,
from_readerstops and returns that error. - If non-utf8 data is encountered, an IO error with kind
InvalidDatais returned.
Note: some data from the reader is likely consumed even if there is an error.
pub fn write_to<T>(&self, writer: T) -> Result<(), Error>where
T: Write,
pub fn write_to<T>(&self, writer: T) -> Result<(), Error>where
T: Write,
Writes the contents of the Rope to a writer.
This is a convenience function, and provides no specific guarantees about performance or internal implementation aside from the runtime complexity listed below.
When more precise control over IO behavior, buffering, etc. is
desired, you should handle IO yourself and use the [Chunks]
iterator to iterate through the Rope’s contents.
Runs in O(N) time.
§Errors
- If the writer returns an error,
write_tostops and returns that error.
Note: some data may have been written even if an error is returned.
pub fn insert(&mut self, byte_idx: usize, text: &str)
pub fn insert(&mut self, byte_idx: usize, text: &str)
Inserts text at byte index byte_idx.
Runs in O(M log N) time, where N is the length of the Rope and M
is the length of text.
§Example
let mut rope = Rope::from_str("Hello!");
rope.insert(5, " world");
assert_eq!("Hello world!", rope);§Panics
- If
byte_idxis out of bounds (i.e.byte_idx > len()). - If
byte_idxis not on a char boundary.
pub fn insert_char(&mut self, byte_idx: usize, ch: char)
pub fn insert_char(&mut self, byte_idx: usize, ch: char)
pub fn remove<R>(&mut self, byte_range: R)where
R: RangeBounds<usize>,
pub fn remove<R>(&mut self, byte_range: R)where
R: RangeBounds<usize>,
Removes the text in the given byte index range.
Uses range syntax, e.g. 2..7, 2.., etc.
Runs in O(M + log N) time, where N is the length of the Rope and M
is the length of the range being removed.
§Example
let mut rope = Rope::from_str("Hello world!");
rope.remove(5..);
assert_eq!("Hello", rope);§Panics
- If the start of the range is greater than the end.
- If the end of the range is out of bounds (i.e.
end > len()). - If the range ends are not on char boundaries.
pub fn slice<R>(&self, byte_range: R) -> RopeSlice<'_>where
R: RangeBounds<usize>,
pub fn slice<R>(&self, byte_range: R) -> RopeSlice<'_>where
R: RangeBounds<usize>,
Gets an immutable slice of the Rope.
Uses range syntax, e.g. 2..7, 2.., etc.
§Example
let rope = Rope::from_str("Hello world!");
let slice = rope.slice(..5);
assert_eq!("Hello", slice);Runs in O(log N) time.
§Panics
- If the start of the range is greater than the end.
- If the end of the range is out of bounds (i.e.
end > len()). - If the range ends are not on char boundaries.
pub fn len_lines(&self, line_type: LineType) -> usize
pub fn len_lines(&self, line_type: LineType) -> usize
Total number of lines in the text, according to the passed line type.
Runs in best case O(1) time, worst-case O(log N).
pub fn is_char_boundary(&self, byte_idx: usize) -> bool
pub fn is_char_boundary(&self, byte_idx: usize) -> bool
Returns whether byte_idx is a char boundary.
Runs in O(log N) time.
§Panics
Panics if byte_idx is out of bounds (i.e. byte_idx > len()).
pub fn floor_char_boundary(&self, byte_idx: usize) -> usize
pub fn floor_char_boundary(&self, byte_idx: usize) -> usize
Returns the byte index of the closest char boundary less than or
equal to byte_idx.
Runs in O(log N) time.
§Panics
Panics if byte_idx is out of bounds (i.e. byte_idx > len()).
pub fn ceil_char_boundary(&self, byte_idx: usize) -> usize
pub fn ceil_char_boundary(&self, byte_idx: usize) -> usize
Returns the byte index of the closest char boundary greater than or
equal to byte_idx.
Runs in O(log N) time.
§Panics
Panics if byte_idx is out of bounds (i.e. byte_idx > len()).
pub fn trailing_line_break_idx(&self, line_type: LineType) -> Option<usize>
pub fn trailing_line_break_idx(&self, line_type: LineType) -> Option<usize>
If the text ends with a line break, returns its byte index.
Otherwise returns None.
Note: a CRLF pair is always treated as a single unit, and thus
this function will return the index of the CR in such cases, even
with LineType::LF where CR is not on its own recognized as a line
break.
Runs in O(log N) time.
pub fn byte(&self, byte_idx: usize) -> u8
pub fn byte(&self, byte_idx: usize) -> u8
Returns the byte at byte_idx.
Runs in O(log N) time.
§Panics
Panics if byte_idx is out of bounds (i.e. byte_idx >= len()).
pub fn char(&self, byte_idx: usize) -> char
pub fn char(&self, byte_idx: usize) -> char
Returns the char at byte_idx.
Note that this takes a byte index, not a char index.
Runs in O(log N) time.
§Panics
- If
byte_idxis out of bounds (i.e.byte_idx >= len()). - If
byte_idxis not a char boundary.
pub fn line(&self, line_idx: usize, line_type: LineType) -> RopeSlice<'_>
pub fn line(&self, line_idx: usize, line_type: LineType) -> RopeSlice<'_>
Returns the line at line_idx, according to the given line type.
Note: lines are zero-indexed.
Runs in O(log N) time.
§Panics
Panics if line_idx is out of bounds (i.e. line_idx >= len_lines()).
pub fn chunk(&self, byte_idx: usize) -> (&str, usize)
pub fn chunk(&self, byte_idx: usize) -> (&str, usize)
Returns the chunk containing the byte at byte_idx.
Also returns the byte index of the beginning of the chunk.
Note: for convenience, a one-past-the-end byte_idx returns the last
chunk of the RopeSlice.
Runs in O(log N) time.
§Panics
Panics if byte_idx is out of bounds (i.e. byte_idx > len()).
pub fn byte_to_line_idx(&self, byte_idx: usize, line_type: LineType) -> usize
pub fn byte_to_line_idx(&self, byte_idx: usize, line_type: LineType) -> usize
Returns the line index of the line that the given byte belongs to.
Notes:
- Counts lines according to the passed line type.
- Lines are zero-indexed. Therefore this is functionally equivalent to counting the line breaks before the specified byte.
byte_idxcan be one-past-the-end, which will return the last line index.
Runs in O(log N) time.
§Panics
Panics if byte_idx is out of bounds (i.e. byte_idx > len()).
pub fn line_to_byte_idx(&self, line_idx: usize, line_type: LineType) -> usize
pub fn line_to_byte_idx(&self, line_idx: usize, line_type: LineType) -> usize
Returns the byte index of the start of the given line.
Notes:
- Counts lines according to the passed line type.
- Lines are zero-indexed.
line_idxcan be one-past-the-end, which will return one-past-the-end byte index.
Runs in O(log N) time.
§Panics
Panics if line_idx is out of bounds (i.e. line_idx > len_lines()).
pub fn bytes(&self) -> Bytes<'_> ⓘ
pub fn bytes(&self) -> Bytes<'_> ⓘ
Creates an iterator over the bytes of the Rope.
Runs in O(log N) time.
pub fn bytes_at(&self, byte_idx: usize) -> Bytes<'_> ⓘ
pub fn bytes_at(&self, byte_idx: usize) -> Bytes<'_> ⓘ
Creates an iterator over the bytes of the Rope, starting at byte
byte_idx.
If byte_idx == len() then an iterator at the end of the
Rope is created (i.e. next() will return None).
Runs in O(log N) time.
§Panics
Panics if byte_idx is out of bounds (i.e. byte_idx > len()).
pub fn chars(&self) -> Chars<'_> ⓘ
pub fn chars(&self) -> Chars<'_> ⓘ
Creates an iterator over the chars of the Rope.
Runs in O(log N) time.
pub fn chars_at(&self, byte_idx: usize) -> Chars<'_> ⓘ
pub fn chars_at(&self, byte_idx: usize) -> Chars<'_> ⓘ
Creates an iterator over the chars of the Rope, starting
at byte_idx.
Note that this takes a byte index, not a char index.
If byte_idx == len() then an iterator at the end of the
Rope is created (i.e. next() will return None).
Runs in O(log N) time.
§Panics
- If
byte_idxis out of bounds (i.e.byte_idx > len()). - If
byte_idxis not a char boundary.
pub fn char_indices(&self) -> CharIndices<'_> ⓘ
pub fn char_indices(&self) -> CharIndices<'_> ⓘ
pub fn char_indices_at(&self, byte_idx: usize) -> CharIndices<'_> ⓘ
pub fn char_indices_at(&self, byte_idx: usize) -> CharIndices<'_> ⓘ
Creates an iterator over the chars of the Rope and their byte
positions, starting at byte_idx.
Note that this takes a byte index, not a char index.
If byte_idx == len() then an iterator at the end of the Rope is
created (i.e. next() will return None).
On each call to next or
prev this iterator returns the byte index
where the returned character starts.
Runs in O(log N) time.
§Panics
- If
byte_idxis out of bounds (i.e.byte_idx > len()). - If
byte_idxis not a char boundary.
pub fn lines(&self, line_type: LineType) -> Lines<'_> ⓘ
pub fn lines(&self, line_type: LineType) -> Lines<'_> ⓘ
Creates an iterator over the lines of the Rope.
Note: the iterator will iterate over lines according to the passed line type.
Runs in O(log N) time.
pub fn lines_at(&self, line_idx: usize, line_type: LineType) -> Lines<'_> ⓘ
pub fn lines_at(&self, line_idx: usize, line_type: LineType) -> Lines<'_> ⓘ
Creates an iterator over the lines of the Rope, starting at line
line_idx.
Notes:
- The iterator will iterate over lines according to the passed line type.
- If
line_idx == len_lines()then an iterator at the end of theRopeis created (i.e.next()will returnNone).
Runs in O(log N) time.
§Panics
Panics if line_idx is out of bounds (i.e. line_idx > len_lines()).
pub fn chunks(&self) -> Chunks<'_> ⓘ
pub fn chunks(&self) -> Chunks<'_> ⓘ
Creates an iterator over the chunks of the Rope.
Runs in O(log N) time.
pub fn chunks_at(&self, byte_idx: usize) -> (Chunks<'_>, usize)
pub fn chunks_at(&self, byte_idx: usize) -> (Chunks<'_>, usize)
Creates an iterator over the chunks of the Rope, with the iterator
starting at the chunk containing byte_idx.
Also returns the byte index of the beginning of the chunk to be
yielded by next().
If byte_idx == len() an iterator at the end of the Rope
(yielding None on a call to next()) is created, and the returned
byte index is the end of the text.
Runs in O(log N) time.
§Panics
Panics if byte_idx is out of bounds (i.e. byte_idx > len()).
pub fn chunk_cursor(&self) -> ChunkCursor<'_>
pub fn chunk_cursor(&self) -> ChunkCursor<'_>
Creates a cursor for navigating the chunks of the text, starting on the first chunk.
Runs in O(log N) time.
pub fn chunk_cursor_at(&self, byte_idx: usize) -> ChunkCursor<'_>
pub fn chunk_cursor_at(&self, byte_idx: usize) -> ChunkCursor<'_>
Creates a cursor for navigating the chunks of the text, with the
cursor starting at the chunk containing byte_idx.
For convenience, byte_idx == len() is accepted, and puts the
cursor on the last chunk. Note that for non-zero-length texts this
is redundant with byte_idx == len() - 1.
Runs in O(log N) time.
§Panics
Panics if byte_idx is out of bounds (i.e. byte_idx > len()).
§impl Rope
Non-panicking versions of some of Rope’s methods.
impl Rope
Non-panicking versions of some of Rope’s methods.
pub fn try_insert(&mut self, byte_idx: usize, text: &str) -> Result<(), Error>
pub fn try_insert(&mut self, byte_idx: usize, text: &str) -> Result<(), Error>
Non-panicking version of insert().
On failure this leaves the rope untouched and returns the cause of the failure.
pub fn try_insert_char(
&mut self,
byte_idx: usize,
ch: char,
) -> Result<(), Error>
pub fn try_insert_char( &mut self, byte_idx: usize, ch: char, ) -> Result<(), Error>
Non-panicking version of insert_char().
On failure this leaves the rope untouched and returns the cause of the failure.
pub fn try_remove<R>(&mut self, byte_range: R) -> Result<(), Error>where
R: RangeBounds<usize>,
pub fn try_remove<R>(&mut self, byte_range: R) -> Result<(), Error>where
R: RangeBounds<usize>,
Non-panicking version of remove().
On failure this leaves the rope untouched and returns the cause of the failure.
pub fn try_slice<R>(&self, byte_range: R) -> Result<RopeSlice<'_>, Error>where
R: RangeBounds<usize>,
pub fn try_slice<R>(&self, byte_range: R) -> Result<RopeSlice<'_>, Error>where
R: RangeBounds<usize>,
Non-panicking version of slice().
On failure this returns the cause of the failure.
pub fn get_byte(&self, byte_idx: usize) -> Option<u8>
pub fn get_byte(&self, byte_idx: usize) -> Option<u8>
Non-panicking version of byte().
If byte_idx is out of bounds, returns None.
pub fn get_char(&self, byte_idx: usize) -> Result<char, Error>
pub fn get_char(&self, byte_idx: usize) -> Result<char, Error>
Non-panicking version of char().
Will fail if byte_idx is either:
- Not a char boundary.
- Out of bounds.
On failure returns the cause of failure.
Trait Implementations§
§impl DisplayWidth for Rope
impl DisplayWidth for Rope
fn display_width(&self) -> usize
impl Eq for Rope
Source§impl<'a> From<&'a Rope> for RegexCursorInput<'a>
impl<'a> From<&'a Rope> for RegexCursorInput<'a>
§impl<'a> FromIterator<&'a str> for Rope
impl<'a> FromIterator<&'a str> for Rope
§impl<'a> FromIterator<Cow<'a, str>> for Rope
impl<'a> FromIterator<Cow<'a, str>> for Rope
§impl FromIterator<String> for Rope
impl FromIterator<String> for Rope
§impl Ord for Rope
impl Ord for Rope
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
§impl PartialOrd for Rope
impl PartialOrd for Rope
Source§impl RopeExt for Rope
impl RopeExt for Rope
fn as_slice(&self) -> RopeSlice<'_>
Source§fn len_lines_unix(&self) -> usize
fn len_lines_unix(&self) -> usize
fn find_next_byte<R>(&self, byte_range: R, needles: &[u8]) -> Option<usize>
fn find_prev_byte<R>(&self, byte_range: R, needles: &[u8]) -> Option<usize>
fn get_grapheme(&self, byte_index: usize) -> Option<RopeSlice<'_>>
fn grapheme(&self, byte_index: usize) -> RopeSlice<'_>
fn graphemes(&self) -> Graphemes<'_> ⓘ
fn is_grapheme_boundary(&self, byte_offset: usize) -> bool
fn is_grapheme_start(&self, byte_index: usize) -> bool
fn snap_to_grapheme_start(&self, byte_index: usize) -> Option<usize>
fn last_grapheme_start(&self) -> Option<usize>
fn prev_grapheme_boundary(&self, byte_offset: usize) -> Option<usize>
fn next_grapheme_boundary(&self, byte_offset: usize) -> Option<usize>
fn floor_grapheme_boundary(&self, byte_offset: usize) -> usize
fn ceil_grapheme_boundary(&self, byte_offset: usize) -> usize
fn display_column(&self, byte_index: usize) -> usize
Auto Trait Implementations§
impl Freeze for Rope
impl RefUnwindSafe for Rope
impl Send for Rope
impl Sync for Rope
impl Unpin for Rope
impl UnsafeUnpin for Rope
impl UnwindSafe for Rope
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.