pub struct Bytes { /* private fields */ }Expand description
The bytes of a Text, encoded in UTF-8
Implementations§
Source§impl Bytes
impl Bytes
Sourcepub fn is_empty(&self) -> bool
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’s possible emptyness as
well, see Text::is_empty_empty.
Sourcepub fn buffers(&self, range: impl RangeBounds<usize>) -> Buffers<'_> ⓘ
pub fn buffers(&self, range: impl RangeBounds<usize>) -> Buffers<'_> ⓘ
An Iterator over the bytes in a given byte range
Unlike strs, this function works with byte ranges, not
TextRanges. That’s 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.
Sourcepub fn strs(&self, range: impl TextRange) -> Option<Strs<'_>>
pub fn strs(&self, range: impl TextRange) -> Option<Strs<'_>>
An Iterator over the &strs of the Text
§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();
let chars = bytes.strs(p0..p1).unwrap().chars();
for char in 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.
Sourcepub fn lines(&self, range: impl TextRange) -> Lines<'_>
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.
Sourcepub fn point_at_byte(&self, b: usize) -> Point
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
Sourcepub fn point_at_char(&self, c: usize) -> Point
pub fn point_at_char(&self, c: usize) -> Point
Sourcepub fn point_at_line(&self, l: usize) -> Point
pub fn point_at_line(&self, l: usize) -> Point
Sourcepub fn points_of_line(&self, l: usize) -> [Point; 2]
pub fn points_of_line(&self, l: usize) -> [Point; 2]
Sourcepub fn last_point(&self) -> Point
pub fn last_point(&self) -> Point
Sourcepub fn chars_fwd(
&self,
range: impl TextRange,
) -> Option<impl Iterator<Item = (Point, char)> + '_>
pub fn chars_fwd( &self, range: impl TextRange, ) -> Option<impl Iterator<Item = (Point, char)> + '_>
A forward iterator of the chars of Bytes
Each char will be accompanied by a Point, which is the
position where said character starts, e.g.
Point::default() for the first character
Sourcepub fn chars_rev(
&self,
range: impl TextRange,
) -> Option<impl Iterator<Item = (Point, char)> + '_>
pub fn chars_rev( &self, range: impl TextRange, ) -> Option<impl Iterator<Item = (Point, char)> + '_>
A reverse iterator of the chars in Bytes
Each char will be accompanied by a Point, which is the
position where said character starts, e.g.
Point::default() for the first character
Source§impl Bytes
impl Bytes
Sourcepub fn search_fwd<R: RegexPattern>(
&self,
pat: R,
range: impl TextRange,
) -> Result<impl Iterator<Item = R::Match> + '_, Box<Error>>
pub fn search_fwd<R: RegexPattern>( &self, pat: R, range: impl TextRange, ) -> Result<impl Iterator<Item = R::Match> + '_, Box<Error>>
Searches forward for a RegexPattern in a range
A RegexPattern can either be a single regex string, an
array of strings, or a slice of strings. When there are more
than one pattern, The return value will include which pattern
matched.
The patterns will also automatically be cached, so you don’t need to do that.
Sourcepub fn search_rev<R: RegexPattern>(
&self,
pat: R,
range: impl TextRange,
) -> Result<impl Iterator<Item = R::Match> + '_, Box<Error>>
pub fn search_rev<R: RegexPattern>( &self, pat: R, range: impl TextRange, ) -> Result<impl Iterator<Item = R::Match> + '_, Box<Error>>
Searches in reverse for a RegexPattern in a range
A RegexPattern can either be a single regex string, an
array of strings, or a slice of strings. When there are more
than one pattern, The return value will include which pattern
matched.
The patterns will also automatically be cached, so you don’t need to do that.