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> PartialEq for Position<'i>
impl<'i> PartialEq for Position<'i>
source§impl<'i> PartialOrd for Position<'i>
impl<'i> PartialOrd for Position<'i>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moreimpl<'i> Copy for Position<'i>
impl<'i> Eq for Position<'i>
Auto Trait Implementations§
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