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’b possible emptyness as
well, see Text::is_empty_empty.
Sourcepub fn strs(&self, range: impl TextRange) -> Option<Strs<'_>>
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.
Sourcepub fn slices(&self, range: impl TextRange) -> Slices<'_> ⓘ
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.
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 line_range(&self, l: usize) -> Range<Point>
pub fn line_range(&self, l: usize) -> Range<Point>
Sourcepub fn last_point(&self) -> Point
pub fn last_point(&self) -> Point
Sourcepub fn chars_fwd(
&self,
range: impl TextRange,
) -> Option<impl Iterator<Item = (usize, char)> + '_>
pub fn chars_fwd( &self, range: impl TextRange, ) -> Option<impl Iterator<Item = (usize, char)> + '_>
Sourcepub fn chars_rev(
&self,
range: impl TextRange,
) -> Option<impl Iterator<Item = (usize, char)> + '_>
pub fn chars_rev( &self, range: impl TextRange, ) -> Option<impl Iterator<Item = (usize, char)> + '_>
Sourcepub fn indent(&self, p: Point, opts: PrintOpts) -> usize
pub fn indent(&self, p: Point, opts: PrintOpts) -> usize
Gets the indentation level on the current line
Sourcepub fn replace_range(&mut self, range: impl TextRange, new: impl AsRef<str>)
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.
Trait Implementations§
Source§impl<'b> RegexHaystack<'b> for Bytes
impl<'b> RegexHaystack<'b> for Bytes
Source§fn try_search<R: RegexPattern>(
&'b self,
pat: R,
) -> Result<Matches<'b, R>, Box<Error>>
fn try_search<R: RegexPattern>( &'b self, pat: R, ) -> Result<Matches<'b, R>, Box<Error>>
Source§fn contains_pat(&'h self, pat: impl RegexPattern) -> Result<bool, Box<Error>>
fn contains_pat(&'h self, pat: impl RegexPattern) -> Result<bool, Box<Error>>
RegexPattern Read moreSource§fn matches_pat(&'h self, pat: impl RegexPattern) -> Result<bool, Box<Error>>
fn matches_pat(&'h self, pat: impl RegexPattern) -> Result<bool, Box<Error>>
RegexPattern exactly Read moreimpl 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> 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.