[go: up one dir, main page]

Scanner

Struct Scanner 

Source
pub struct Scanner<'a> { /* private fields */ }
Expand description

A string scanner.

Implementations§

Source§

impl<'a> Scanner<'a>

Source

pub fn new(string: &'a str) -> Self

Create a new string scanner, starting with a cursor position of 0.

Source

pub fn string(&self) -> &'a str

The full source string.

Source

pub fn cursor(&self) -> usize

The current cursor position.

Source

pub fn done(&self) -> bool

Whether the scanner has fully consumed the string.

Source

pub fn before(&self) -> &'a str

The subslice before the cursor.

Source

pub fn after(&self) -> &'a str

The subslice after the cursor.

Source

pub fn parts(&self) -> (&'a str, &'a str)

The subslices before and after the cursor.

Source

pub fn from(&self, start: usize) -> &'a str

The subslice from start to the cursor.

Snaps start into the bounds of the string and to the next character boundary.

Source

pub fn to(&self, end: usize) -> &'a str

The subslice from the cursor to end.

Snaps end into the bounds of the string and to the next character boundary.

Source

pub fn get(&self, range: Range<usize>) -> &'a str

The subslice from the start to end.

Snaps start and end into the bounds of the string and to the next character boundary.

Source

pub fn peek(&self) -> Option<char>

The character right behind the cursor.

Source

pub fn at<T>(&self, pat: impl Pattern<T>) -> bool

Whether the part right behind the cursor starts with the given pattern.

Source

pub fn scout(&self, n: isize) -> Option<char>

Look at the n-th character relative to the cursor without changing the cursor.

  • scout(-1) is the character before the cursor.
  • scout(0) is the same as peek().

Runs in O(|n|).

Source

pub fn locate(&self, n: isize) -> usize

The byte index of the n-th character relative to the cursor.

  • locate(-1) is the byte position of the character before the cursor.
  • locate(0) is the same as cursor().

Runs in O(|n|).

Source

pub fn eat(&mut self) -> Option<char>

Consume and return the character right behind the cursor.

Source

pub fn uneat(&mut self) -> Option<char>

Consume and return the character right before the cursor, moving it back.

Source

pub fn eat_if<T>(&mut self, pat: impl Pattern<T>) -> bool

Consume the given pattern if that’s what’s right behind the cursor.

Returns true if the pattern was consumed.

Source

pub fn eat_while<T>(&mut self, pat: impl Pattern<T>) -> &'a str

Consume while the given pattern is what’s right behind the cursor.

Returns the consumed substring.

Source

pub fn eat_until<T>(&mut self, pat: impl Pattern<T>) -> &'a str

Consume until the given pattern is what’s right behind the cursor.

Returns the consumed substring.

Source

pub fn eat_whitespace(&mut self) -> &'a str

Consume all whitespace until the next non-whitespace character.

Returns the consumed whitespace.

Source

pub fn expect<T>(&mut self, pat: impl Pattern<T>)

Consume the given pattern if that’s what’s right behind the cursor or panic otherwise.

Source

pub fn jump(&mut self, target: usize)

Jump to a byte position in the source string.

Snaps into the bounds of the string and to the next character boundary.

Trait Implementations§

Source§

impl<'a> Clone for Scanner<'a>

Source§

fn clone(&self) -> Scanner<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Scanner<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Hash for Scanner<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> PartialEq for Scanner<'a>

Source§

fn eq(&self, other: &Scanner<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Copy for Scanner<'a>

Source§

impl<'a> Eq for Scanner<'a>

Source§

impl<'a> StructuralPartialEq for Scanner<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Scanner<'a>

§

impl<'a> RefUnwindSafe for Scanner<'a>

§

impl<'a> Send for Scanner<'a>

§

impl<'a> Sync for Scanner<'a>

§

impl<'a> Unpin for Scanner<'a>

§

impl<'a> UnwindSafe for Scanner<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.