pub struct Position<'i> { /* private fields */ }
Expand description
A cursor position in a &str
which provides useful methods to manually parse that string.
Implementations§
Source§impl<'i> Position<'i>
impl<'i> Position<'i>
Sourcepub fn new(input: &str, pos: usize) -> Option<Position<'_>>
pub fn new(input: &str, pos: usize) -> Option<Position<'_>>
Attempts to create a new Position
at the given position. If the specified position is
an invalid index, or the specified position is not a valid UTF8 boundary, then None is
returned.
§Examples
let cheart = '💖';
let heart = "💖";
assert_eq!(Position::new(heart, 1), None);
assert_ne!(Position::new(heart, cheart.len_utf8()), None);
Sourcepub fn from_start(input: &'i str) -> Position<'i>
pub fn from_start(input: &'i str) -> Position<'i>
Creates a Position
at the start of a &str
.
§Examples
let start = Position::from_start("");
assert_eq!(start.pos(), 0);
Sourcepub fn pos(&self) -> usize
pub fn pos(&self) -> usize
Returns the byte position of this Position
as a usize
.
§Examples
let input = "ab";
let mut start = Position::from_start(input);
assert_eq!(start.pos(), 0);
Sourcepub fn line_col(&self) -> (usize, usize)
pub fn line_col(&self) -> (usize, usize)
Returns the line and column number of this Position
.
This is an O(n) operation, where n is the number of chars in the input.
You better use pair.line_col()
instead.
§Examples
enum Rule {}
let input = "\na";
let mut state: Box<pest::ParserState<'_, Rule>> = pest::ParserState::new(input);
let mut result = state.match_string("\na");
assert!(result.is_ok());
assert_eq!(result.unwrap().position().line_col(), (2, 2));
Sourcepub fn line_of(&self) -> &'i str
pub fn line_of(&self) -> &'i str
Returns the entire line of the input that contains this Position
.
§Examples
enum Rule {}
let input = "\na";
let mut state: Box<pest::ParserState<'_, Rule>> = pest::ParserState::new(input);
let mut result = state.match_string("\na");
assert!(result.is_ok());
assert_eq!(result.unwrap().position().line_of(), "a");
Trait Implementations§
Source§impl From<Position<'_>> for LineColLocation
impl From<Position<'_>> for LineColLocation
Source§impl<'i> Ord for Position<'i>
impl<'i> Ord for Position<'i>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<'i> PartialOrd for Position<'i>
impl<'i> PartialOrd for Position<'i>
impl<'i> Copy for Position<'i>
impl<'i> Eq for Position<'i>
Auto Trait Implementations§
impl<'i> Freeze for Position<'i>
impl<'i> RefUnwindSafe for Position<'i>
impl<'i> Send for Position<'i>
impl<'i> Sync for Position<'i>
impl<'i> Unpin for Position<'i>
impl<'i> UnwindSafe for Position<'i>
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)