pub struct Position<'i> {
input: &'i str,
pos: usize,
}
Expand description
A cursor position in a &str
which provides useful methods to manually parse that string.
Fields§
§input: &'i str
§pos: usize
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<'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,
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> AllocForAny<T> for T
impl<T> AllocForAny<T> for T
Source§fn galloc(self) -> &'static T
fn galloc(self) -> &'static T
Provides a method to allocate an instance of T
on the heap with a static lifetime.
This implementation of galloc
takes ownership of the T
instance and uses the alloc
function to place it in a location with a static lifetime, presumably managing it in a way that ensures its persistence for the duration of the program.
This can be particularly useful for scenarios where a static lifetime is required, such as when interfacing with systems or patterns that necessitate global state or long-lived data.
Source§fn galloc_mut(self) -> &'static T
fn galloc_mut(self) -> &'static T
Provides a method that moves the instance and returns a reference to it allocated with a static lifetime.
This method utilizes alloc_mut
to perform the allocation, likely involving allocating the resource in a manner that ensures it lives for the entire duration of the application.
These semantics allow the user to safely assume that the reference will not expire during the program’s execution, making it suitable for long-lived data structures or operations that require such guarantees.
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more