Skip to main content

CharCursor

Struct CharCursor 

Source
pub struct CharCursor<'a> { /* private fields */ }
Expand description

Forward cursor over a &str, positioned only ever at char boundaries.

§Examples

use latexml_core::util::char_cursor::CharCursor;

// Take a run of ASCII letters, then whatever single character follows —
// even a 3-byte one. A byte walker would split it; this cannot.
let mut cur = CharCursor::new("word“tail");
let start = cur.pos();
cur.take_while(char::is_alphanumeric);
assert_eq!(cur.slice_from(start), "word");
assert_eq!(cur.next(), Some('“'));

Implementations§

Source§

impl<'a> CharCursor<'a>

Source

pub fn new(src: &'a str) -> Self

Start at the beginning of src.

Source

pub fn pos(&self) -> usize

Byte offset of the next character — a valid boundary, and the mark to hand to slice_from.

Source

pub fn peek(&mut self) -> Option<char>

The next character, without consuming it.

Source

pub fn peek_second(&mut self) -> Option<char>

The character after the next one, without consuming anything.

This is the i + 1 < len lookahead a byte walker spells out by hand, with no arithmetic on indices — the arithmetic is where the bug lives.

Source

pub fn next(&mut self) -> Option<char>

Consume and return the next character, advancing by its full width.

Source

pub fn take_while(&mut self, pred: impl FnMut(char) -> bool)

Consume characters while pred holds.

Source

pub fn is_done(&mut self) -> bool

true once the input is exhausted.

Source

pub fn slice_from(&self, mark: usize) -> &'a str

The text between a previous pos mark and the current position.

Infallible — both ends came from CharIndices, so both are char boundaries. That is the entire point of the type.

§Panics

Only if mark did not come from this cursor’s pos, or is ahead of the current position. Both are caller bugs, not input-dependent.

Source

pub fn rest(&self) -> &'a str

The remaining, unconsumed text.

Auto Trait Implementations§

§

impl<'a> Freeze for CharCursor<'a>

§

impl<'a> RefUnwindSafe for CharCursor<'a>

§

impl<'a> Send for CharCursor<'a>

§

impl<'a> Sync for CharCursor<'a>

§

impl<'a> Unpin for CharCursor<'a>

§

impl<'a> UnsafeUnpin for CharCursor<'a>

§

impl<'a> UnwindSafe for CharCursor<'a>

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