Bytes

Struct Bytes 

Source
pub struct Bytes { /* private fields */ }
Expand description

The bytes of a Text, encoded in UTF-8

Implementations§

Source§

impl Bytes

Source

pub fn len(&self) -> Point

The Point at the end of the text

Source

pub fn is_empty(&self) -> bool

Whether or not there are any characters in Bytes, besides the final b'\n'

§Note

This does not check for tags, so with a Tag::Ghost, there could actually be a “string” of characters on the Text, it just wouldn’t be considered real “text”. If you want to check for the InnerTags’b possible emptyness as well, see Text::is_empty_empty.

Source

pub fn char_at(&self, p: impl TextIndex) -> Option<char>

The char at the Point’b position

Source

pub fn strs(&self, range: impl TextRange) -> Option<Strs<'_>>

A subslice of the Bytes

Note that this TextRange is relative to the whole Bytes struct, not just this Strs. This method also clips the ranges so they fit into the range of these Strs.

It will return None if the range does not start or end in valid utf8 boundaries.

§Note

The reason why this function returns two strings is that the contents of the text are stored in a GapBuffer, which works with two strings.

If you want to iterate over them, you can do the following:

let bytes = text.bytes();

for char in bytes.strs(p0..p1).unwrap().chars() {
    todo!();
}

Do note that you should avoid iterators like str::lines, as they will separate the line that is partially owned by each &str:

let broken_up_line = [
    "This is line 1, business as usual.\nThis is line 2, but it",
    "is broken into two separate strings.\nSo 4 lines would be counted, instead of 3",
];

This is one way that the inner GapBuffer could be set up, where one of the lines is split among the two slices.

If you wish to iterate over the lines, see Bytes::lines.

Source

pub fn slices(&self, range: impl TextRange) -> Slices<'_>

An Iterator over the bytes in a given byte range

Unlike strs, this function works with byte ranges, not TextRanges. That’b because Strs is supposed to return valid UTF-8 strings, which need to have valid character terminations, so they should be indexed by a character range, not a byte range.

Since buffers is based on [u8]s, not strs, it doesn’t have the same restrictions, so a byte range can be used instead.

If the range is fully or partially out of bounds, one or both of the slices might be empty.

Source

pub fn lines(&self, range: impl TextRange) -> Lines<'_>

Returns an iterator over the lines in a given range

The lines are inclusive, that is, it will iterate over the whole line, not just the parts within the range.

Source

pub fn point_at_byte(&self, b: usize) -> Point

The Point corresponding to the byte position, 0 indexed

If the byte position would fall in between two characters (because the first one comprises more than one byte), the first character is chosen as the Point where the byte is located.

§Panics

Will panic if b is greater than the length of the text

Source

pub fn point_at_char(&self, c: usize) -> Point

The Point associated with the cth char

§Panics

Will panic if c is greater than the number of chars in the text.

Source

pub fn point_at_line(&self, l: usize) -> Point

The Point where the lth line starts, 0 indexed

If l == number_of_lines, returns the last point of the text.

§Panics

Will panic if the number l is greater than the number of lines on the text

Source

pub fn line_range(&self, l: usize) -> Range<Point>

The start and end Points for the lth line

If l == number_of_lines, these points will be the same.

The second number includes the \n at the end of the line.

§Panics

Will panic if the number l is greater than the number of lines on the text

Source

pub fn last_point(&self) -> Point

The last Point associated with a char

This will give the Point of the last char of the text. The difference between this method and len is that it will return a Point one position earlier than it. If the text is completely empty, it will return None.

Source

pub fn chars_fwd( &self, range: impl TextRange, ) -> Option<impl Iterator<Item = (usize, char)> + '_>

A forward iterator of the chars of Bytes

Each char will be accompanied by a byte index, which is the position where said character starts, e.g. 0 for the first character.

Source

pub fn chars_rev( &self, range: impl TextRange, ) -> Option<impl Iterator<Item = (usize, char)> + '_>

A reverse iterator of the chars in Bytes

Each char will be accompanied by a byte index, which is the position where said character starts, e.g. 0 for the first character.

Source

pub fn indent(&self, p: Point, opts: PrintOpts) -> usize

Gets the indentation level on the current line

Source

pub fn replace_range(&mut self, range: impl TextRange, new: impl AsRef<str>)

Replaces a TextRange with a &str

If you want to apply a Change to the Bytes this way, you can use Change::taken_range as the TextRange, and Change::added_str as the replacement text.

Source

pub fn get_contiguous(&self, range: impl TextRange) -> Option<&str>

Tries to get a contiguous &str from the Bytes

Returns None if the gap of the inner buffer was within the given range OR.

Trait Implementations§

Source§

impl AsRef<Bytes> for Text

Source§

fn as_ref(&self) -> &Bytes

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Bytes

Source§

fn clone(&self) -> Bytes

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Bytes

Source§

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

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

impl Default for Bytes

Source§

fn default() -> Bytes

Returns the “default value” for a type. Read more
Source§

impl From<&Path> for Bytes

Source§

fn from(value: &Path) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Bytes

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Arc<str>> for Bytes

Source§

fn from(value: Arc<str>) -> Self

Converts to this type from the input type.
Source§

impl From<Box<dyn Error>> for Bytes

Source§

fn from(value: Box<dyn Error>) -> Self

Converts to this type from the input type.
Source§

impl From<Box<str>> for Bytes

Source§

fn from(value: Box<str>) -> Self

Converts to this type from the input type.
Source§

impl From<Cow<'_, str>> for Bytes

Source§

fn from(value: Cow<'_, str>) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Bytes

Source§

fn from(value: Error) -> Self

Converts to this type from the input type.
Source§

impl From<PathBuf> for Bytes

Source§

fn from(value: PathBuf) -> Self

Converts to this type from the input type.
Source§

impl From<Rc<str>> for Bytes

Source§

fn from(value: Rc<str>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Bytes

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<char> for Bytes

Source§

fn from(value: char) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Bytes

Source§

fn from(value: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Bytes

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for Bytes

Source§

fn from(value: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Bytes

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Bytes

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Bytes

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Bytes

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for Bytes

Source§

fn from(value: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Bytes

Source§

fn from(value: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Bytes

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Bytes

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Bytes

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Bytes

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Bytes

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl PartialEq<&str> for Bytes

Source§

fn eq(&self, other: &&str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<Bytes> for &str

Source§

fn eq(&self, other: &Bytes) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<Bytes> for String

Source§

fn eq(&self, other: &Bytes) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<String> for Bytes

Source§

fn eq(&self, other: &String) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for Bytes

Source§

fn eq(&self, other: &Bytes) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'b> RegexHaystack<'b> for Bytes

An Iterator over the matches for a given RegexPattern Read more
Source§

fn search<R: RegexPattern>(&'h self, pat: R) -> Matches<'h, R>

An Iterator over the matches for a given RegexPattern Read more
Source§

fn contains_pat(&'h self, pat: impl RegexPattern) -> Result<bool, Box<Error>>

Wether this haystack contains a match for a RegexPattern Read more
Source§

fn matches_pat(&'h self, pat: impl RegexPattern) -> Result<bool, Box<Error>>

Wether this haystack matches the RegexPattern exactly Read more
Source§

impl Eq for Bytes

Auto Trait Implementations§

§

impl Freeze for Bytes

§

impl RefUnwindSafe for Bytes

§

impl Send for Bytes

§

impl Sync for Bytes

§

impl Unpin for Bytes

§

impl UnwindSafe for Bytes

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.