Skip to main content

Strs

Struct Strs 

Source
pub struct Strs(/* private fields */);
Expand description

The str equivalent for Duat.

Due to Duat’s use of a gap buffer to represent the Text, this struct doesn’t represent just one slice like a &str does. Instead, it represents two slices (either being possibly empty).

This is done by making use of a custom dynamically sized type, allowing for near identical utilization.

Implementations§

Source§

impl Strs

Source

pub fn start_point(&self) -> Point

The Point at the end of the Strs.

This is the equivalent of strs.range().start.

Source

pub fn end_point(&self) -> Point

The Point at the end of the Strs.

This is the equivalent of strs.range().end.

Source

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

The char at a given position.

This position can either be a Point or a byte index. Will return None if the position is greater than or equal to self.len() or if it is not located in a utf8 character boundary.

Source

pub fn point_at_byte(&self, byte: 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, char: 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_coords(&self, line: usize, column: usize) -> Point

The Point where the lth line starts, 0 indexed.

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

§Panics

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

Source

pub fn line(&self, n: usize) -> &Strs

The Strs for the nth line.

§Panics

Panics if n >= self.len().line()

Source

pub fn full(&self) -> &Strs

Returns an Strs for the whole Text that this one belongs to.

You should use this function if you want to create an api that requires the whole Strs to be used as an argument. You can accept any Strs, then just transform it to the full one using this function.

If this Strs is from Strs::empty, then this will return itself.

Source

pub fn last_point(&self) -> Point

The last Point associated with a char

This function takes into account the whole Text, not just the parts contained in the Strs. And since a Text can’t be empty, it will always return a Point associated with the \n character.

Source

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

Tries to get a subslice of the Strs

It will return None if the range does not start or end in valid utf8 boundaries. If you expect the value to alway be Some, consider using the index operator ([]) instead.

This method is conceptually similar to &str::get, but the reference is to an Strs struct. This struct points to a subslice of the Strss, which is actually two slices, given the internal gap buffer representation.

This type is supposed to act nearly identically with the str type, only differing in the fact that its maximum lenght is u32::MAX, not usize::MAX.

Source

pub fn empty() -> &'static Self

An empty Strs, useful in some circumstances.

This is the equivalent of a literal "".

Source

pub fn slices(&self, range: impl TextRange) -> [&[u8]; 2]

Returns a struct of two &[u8] representing a [TextRange] from the slice.

Source

pub fn to_array(&self) -> [&str; 2]

Converts this Strs into an array of its two parts.

Source

pub fn lines(&self) -> 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 bytes(&self) -> impl DoubleEndedIterator<Item = u8>

Returns and Iterator over the bytes of this Strs

Source

pub fn chars(&self) -> impl DoubleEndedIterator<Item = char>

Returns and Iterator over the chars of this Strs

Source

pub fn char_indices(&self) -> impl DoubleEndedIterator<Item = (usize, char)>

Returns an Iterator over the chars and their indices.

Source

pub fn byte_range(&self) -> Range<usize>

A Range<usize> of the bytes on this Strs.

Source

pub fn range(&self) -> Range<Point>

A Range<Point> of the bytes on this Strs.

If you just care about the byte indices (most likely), check out Strs::byte_range isntead.

Source

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

Gets the indentation level of this Strs

This assumes that it is a line in the Strs, ending with \n or \r\n.

This is the total “amount of spaces”, that is, how many ' ' character equivalents are here. This depends on your PrintOpts because of the tabstop field.

Source

pub fn len(&self) -> usize

The lenght of this Strs, in bytes.

Source

pub fn is_empty(&self) -> bool

Wether the len of this Strs is equal to 0.

Source

pub fn is_empty_line(&self) -> bool

Wether this is an empty line or not

An empty line is either \n or \r\n. If you want to check wether a line is just whitespace, you can do this:

use duat::prelude::*;

fn is_whitespace(line: &Strs) -> bool {
    line.chars().all(|char| char.is_whitespace())
}
Source

pub fn version(&self) -> u64

Get the current version of the StrsBuf

This version is irrespective of undos/redos, that is, an undo will also bump the version number up. You can use this for communicating version changes, such as with an LSP.

Trait Implementations§

Source§

impl AsRef<Strs> for Text

Source§

fn as_ref(&self) -> &Strs

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

impl Debug for Strs

Source§

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

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

impl Display for Strs

Source§

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

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

impl<Idx: TextRange> Index<Idx> for Strs

Source§

type Output = Strs

The returned type after indexing.
Source§

fn index(&self, index: Idx) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl PartialEq<&Strs> for &Strs

Source§

fn eq(&self, other: &&Strs) -> 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<&Strs> for &str

Source§

fn eq(&self, other: &&Strs) -> 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<&Strs> for String

Source§

fn eq(&self, other: &&Strs) -> 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<&str> for &Strs

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<String> for &Strs

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<'b> RegexHaystack<'b> for Strs

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 &Strs

Auto Trait Implementations§

§

impl Freeze for Strs

§

impl RefUnwindSafe for Strs

§

impl Send for Strs

§

impl !Sized for Strs

§

impl Sync for Strs

§

impl Unpin for Strs

§

impl UnsafeUnpin for Strs

§

impl UnwindSafe for Strs

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more