PieceTable

Struct PieceTable 

Source
pub struct PieceTable<'b> { /* private fields */ }

Implementations§

Source§

impl<'b> PieceTable<'b>

Source

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

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

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"); // unchanged
Source

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

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

pub fn len_bytes(&self) -> usize

Total number of bytes in the piece table.

Runs in O(1).

§Examples
let mut pt = PieceTable::new("1234⑤");
assert_eq!(pt.len_bytes(), 7); // the 5 takes 3 bytes
Source

pub fn iter(&self) -> impl Iterator<Item = &str>

Returns an iterator over all the &str chunks in the table.

§Examples
let mut pt = PieceTable::new("hithere");
pt.insert(2, ", and hello, ");
assert_eq!(pt.iter().collect::<String>(), "hi, and hello, there");

Trait Implementations§

Source§

impl<'b> Debug for PieceTable<'b>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for PieceTable<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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