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>
impl<'a> CharCursor<'a>
Sourcepub fn pos(&self) -> usize
pub fn pos(&self) -> usize
Byte offset of the next character — a valid boundary, and the mark to
hand to slice_from.
Sourcepub fn peek_second(&mut self) -> Option<char>
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.
Sourcepub fn next(&mut self) -> Option<char>
pub fn next(&mut self) -> Option<char>
Consume and return the next character, advancing by its full width.
Sourcepub fn take_while(&mut self, pred: impl FnMut(char) -> bool)
pub fn take_while(&mut self, pred: impl FnMut(char) -> bool)
Consume characters while pred holds.
Sourcepub fn slice_from(&self, mark: usize) -> &'a str
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.