Struct Scanner

Source
pub struct Scanner { /* private fields */ }
Expand description

This is used to analyze string. It can be initialized using either Scanner::from or Scanner::new.

§Example

use snob::Scanner;

let mut scanner = Scanner::new("Hello, world!");
if let Some(position) = scanner.starts_with("Hello") {
    scanner.goto(position);
}

Implementations§

Source§

impl Scanner

Source

pub fn new(source: impl AsRef<str>) -> Self

Create a new Scanner with a given source.

§Example
use snob::Scanner;

let scanner = Scanner::new("Hello, world!");
Source

pub fn source(&self) -> &[char]

Get the full source being used in this scanner, as a slice of characters

§Example
use snob::Scanner;

let scanner = Scanner::new("Hello, world!");
let source = scanner.source().iter().collect::<String>();
assert_eq!(scanner.source().iter().collect::<String>(), "Hello, world!");
Source

pub fn len(&self) -> usize

Get the full length of the source being used in this scanner

§Example
use snob::Scanner;

let scanner = Scanner::new("Hello, world!");
assert_eq!(scanner.len(), 13);
Source

pub fn is_empty(&self) -> bool

Returns true if the scanner’s source is an empty string

§Example
use snob::Scanner;

let scanner = Scanner::new("Hello, world!");
assert!(!scanner.is_empty());
Source

pub fn char_at(&self, index: usize) -> Option<char>

Get the character at a given position in the string.

§Example
use snob::Scanner;

let scanner = Scanner::new("Hello, world!");
assert!(!scanner.is_empty());
Source

pub fn position(&self) -> usize

Get the current position in the string. When the Scanner is created, this value is zero.

§Example
use snob::Scanner;

let mut scanner = Scanner::new("Hello, world!");
assert_eq!(scanner.position(), 0);
scanner.advance(5);
assert_eq!(scanner.position(), 5);
scanner.goto(3);
assert_eq!(scanner.position(), 3);
Source

pub fn is_at_end(&self) -> bool

Returns true if the scanner’s position has reached the end of its source.

§Example
use snob::Scanner;

let mut scanner = Scanner::new("Hello, world!");
assert!(!scanner.is_at_end());

if let Some(position) = scanner.starts_with("Hello, world!") {
    scanner.goto(position);
}

assert!(scanner.is_at_end());
Source

pub fn goto(&mut self, position: usize) -> Option<String>

Set the scanner’s position. If the position out of range out the source, then None is returned. Otherwise, the subslice from the old position to the new position is returned. If the latter is less than the former, then the string is reversed.

§Example
use snob::Scanner;

let mut scanner = Scanner::new("Hello, world!");
scanner.goto(3);
assert_eq!(scanner.position(), 3);
Source

pub fn advance(&mut self, amount: isize) -> Option<String>

Increase the position by the given amount. If the new position is out of the range of the source, then None is returned. Otherwise, the subslice from the old position to the new position is returned. If the latter is less than the former, then the string is reversed.

§Example
use snob::Scanner;

let mut scanner = Scanner::new("Hello, world!");
scanner.advance(5);
assert_eq!(scanner.position(), 5);
Source

pub fn find_substring(&self, substring: impl AsRef<str>) -> Option<usize>

Looks for the given substring in the remainder of the scanner. If the substring is found, the position of the first character in the substring is returned. Otherwise, None is returned.

§Example
use snob::Scanner;

let scanner = Scanner::new("Hello, world!");
let position = scanner.find_substring("lo")?;
assert_eq!(position, 3);
Source

pub fn starts_with(&self, substring: impl AsRef<str>) -> Option<usize>

If source[position..] starts with the given string, then this returns the ending position of the substring. Otherwise, None is returned.

§Example
use snob::Scanner;

let scanner = Scanner::new("Hello, world!");
let position = scanner.starts_with("Hello")?;
assert_eq!(position, 5);
Source

pub fn advance_if_starts_with( &mut self, substring: impl AsRef<str>, ) -> Option<String>

If source[position..] starts with the given string, then this returns a copy of the substring. Otherwise, None is returned. This is the equivalent of: self.goto(self.starts_with(substring)?).

§Example
use snob::Scanner;

let mut scanner = Scanner::new("Hello, world!");
let substring = scanner.advance_if_starts_with("Hello")?;
assert_eq!(substring, "Hello");
assert_eq!(scanner.position(), 5);
Source

pub fn any(&self, cset: impl CharacterSet) -> Option<usize>

If the next character in the scanner is contained in the given cset, then the position after the next character is returned. Otherwise, None is returned.

§Example
use snob::Scanner;

let scanner = Scanner::new("Hello, world!");
let position = scanner.any('H')?;
assert_eq!(position, 1);
Source

pub fn many(&self, cset: impl CharacterSet) -> Option<usize>

If the next character in the scanner is contained in the given cset, then the position after the longest initial sequence of characters in cset is returned. Otherwise, None is returned.

§Example
use snob::Scanner;
use snob::csets::AsciiLetters;

let scanner = Scanner::new("Hello, world!");
let position = scanner.many(AsciiLetters)?;
assert_eq!(position, 5);
Source

pub fn upto(&self, cset: impl CharacterSet) -> Option<usize>

If the remainder of the scanner contains a character from the given cset, then the position of the aforementioned character is returned. Otherwise, None is returned.

§Example
use snob::Scanner;

let scanner = Scanner::new("Hello, world!");
let position = scanner.upto(' ')?;
assert_eq!(position, 6);

Trait Implementations§

Source§

impl AsRef<[char]> for Scanner

Source§

fn as_ref(&self) -> &[char]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Scanner

Source§

fn clone(&self) -> Scanner

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 From<&str> for Scanner

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Box<[char]>> for Scanner

Source§

fn from(value: Box<[char]>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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.