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.
§Panics
Panics if the position is out of bounds.
§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.
§Panics
Panics if the position is out of bounds (e.g., beyond the end of the input).
§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§
impl<'i> Copy for Position<'i>
impl Eq for Position<'_>
Source§impl From<Position<'_>> for LineColLocation
impl From<Position<'_>> for LineColLocation
Source§impl<'i> Ord for Position<'i>
§Panics
Panics when comparing positions from different input strings.
impl<'i> Ord for Position<'i>
§Panics
Panics when comparing positions from different input strings.