pub struct PieceTable<'b> { /* private fields */ }Implementations§
Source§impl<'b> PieceTable<'b>
impl<'b> PieceTable<'b>
Sourcepub fn new(initial: &'b str) -> Self
pub fn new(initial: &'b str) -> Self
Create a new PieceTable with the initial contents set to initial.
§Examples
let pt = PieceTable::new("initial");
assert_eq!(pt.text(), "initial");Sourcepub fn text(&self) -> String
pub fn text(&self) -> String
Collect the text from the piece table.
This function allocates a new string. You can use PieceTable::iter
to iterate over &str chunks without allocations.
§Examples
let mut pt = PieceTable::new("content");
pt.insert(0, "abcd, ");
assert_eq!(pt.text(), "abcd, content");Sourcepub fn remove<R>(&mut self, range: R)where
R: RangeBounds<usize>,
pub fn remove<R>(&mut self, range: R)where
R: RangeBounds<usize>,
Removes the text in the given char index range.
§Examples
let mut pt = PieceTable::new("hello_there");
pt.insert(5, " ");
pt.insert(7, " ");
pt.remove(6..=8);
assert_eq!(pt.text(), "hello there");let mut pt = PieceTable::new("012345");
pt.remove(0..=5);
assert_eq!(pt.text(), "");let mut pt = PieceTable::new("012345");
pt.remove(5..0);
assert_eq!(pt.text(), "012345"); // unchangedSourcepub fn insert(&mut self, char_idx: usize, text: &str)
pub fn insert(&mut self, char_idx: usize, text: &str)
Insert content at position index.
§Examples
let mut pt = PieceTable::new("rld");
pt.insert(0, "hellowo");
pt.insert(5, " ");
assert_eq!(pt.text(), "hello world");§Panics
Will panic if index is larger than the size of the contents.
ⓘ
let mut pt = PieceTable::new("012");
pt.insert(4, " "); // will panicSourcepub fn len_chars(&self) -> usize
pub fn len_chars(&self) -> usize
Total number of chars in the piece table.
Runs in O(1).
§Examples
let mut pt = PieceTable::new("123456");
assert_eq!(pt.len_chars(), 6);Trait Implementations§
Source§impl<'b> Debug for PieceTable<'b>
impl<'b> Debug for PieceTable<'b>
Auto Trait Implementations§
impl<'b> Freeze for PieceTable<'b>
impl<'b> RefUnwindSafe for PieceTable<'b>
impl<'b> Send for PieceTable<'b>
impl<'b> Sync for PieceTable<'b>
impl<'b> Unpin for PieceTable<'b>
impl<'b> UnwindSafe for PieceTable<'b>
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
Mutably borrows from an owned value. Read more