[][src]Struct luaparse::InputCursor

pub struct InputCursor<'a> { /* fields omitted */ }

An input cursor.

Implementations

impl<'a> InputCursor<'a>[src]

pub fn new(buf: &'a str) -> Self[src]

Create a new cursor from a buffer.

pub fn peek_nth(&self, n: usize) -> Option<char>[src]

Returns the nth character after the cursor without advancing the cursor.

let cursor = InputCursor::new("hello world!");
assert_eq!(cursor.peek_nth(0), Some('h'));
assert_eq!(cursor.peek_nth(2), Some('l'));

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

Returns the next character without advancing the cursor. Equivalent to peek_nth(0).

let cursor = InputCursor::new("hello world!");
assert_eq!(cursor.peek(), Some('h'));
assert_eq!(cursor.peek(), cursor.peek_nth(0));

pub fn pos(&self) -> Position[src]

Returns the position of the following character.

let mut cursor = InputCursor::new("hello\nworld");
assert_eq!(cursor.pos(), Position {
    byte: 0,
    line: 1,
    col: 1,
});
cursor.skip_n(5);
assert_eq!(cursor.pos(), Position {
    byte: 5,
    line: 1,
    col: 6,
});
cursor.next();
assert_eq!(cursor.pos(), Position {
    byte: 6,
    line: 2,
    col: 1,
});

pub fn pos_no_newline(&self) -> Position[src]

Returns the position of the following character.

Only differs from pos when the cursor is immediately after a newline character. In that case, unlike pos, this method does not go to the next line.

let mut cursor = InputCursor::new("hello\nworld");
assert_eq!(cursor.pos(), Position {
    byte: 0,
    line: 1,
    col: 1,
});
cursor.skip_n(5);
assert_eq!(cursor.pos(), Position {
    byte: 5,
    line: 1,
    col: 6,
});
cursor.next();
assert_eq!(cursor.pos(), Position {
    byte: 6,
    line: 1,
    col: 7,
});

pub fn prev_pos(&self) -> Position[src]

Returns the position of the preceding character.

let mut cursor = InputCursor::new("hello\nworld");
assert_eq!(cursor.prev_pos(), Position {
    byte: 0,
    line: 1,
    col: 1,
});
cursor.skip_n(5);
assert_eq!(cursor.prev_pos(), Position {
    byte: 4,
    line: 1,
    col: 5,
});
cursor.next();
assert_eq!(cursor.prev_pos(), Position {
    byte: 5,
    line: 1,
    col: 6,
});

pub fn remaining(&self) -> &'a str[src]

Returns the remaining string as a subslice.

let mut cursor = InputCursor::new("hello world");
cursor.skip_n(6);
assert_eq!(cursor.remaining(), "world");

pub fn substr<I>(&self, range: I) -> &'a str where
    I: RangeBounds<Position>, 
[src]

Returns a substring of the buffer.

Accepts a range defined by two Positions.

The positions must be previously obtained using pos or its variant. The function may panic if the position is invalid.

let mut cursor = InputCursor::new("hello world!");
let start = cursor.pos();
cursor.skip_n(5);
let end = cursor.prev_pos();
assert_eq!(cursor.substr(start..=end), "hello");

pub fn skip_n(&mut self, n: usize)[src]

Advances the cursor by n characters.

let mut cursor = InputCursor::new("hello world!");
cursor.skip_n(6);
assert_eq!(cursor.remaining(), "world!");

pub fn consume_n(&mut self, n: usize) -> &'a str[src]

Consumes n characters starting from the current position.

let mut cursor = InputCursor::new("hello world!");
let s = cursor.consume_n(5);
assert_eq!(s, "hello");
assert_eq!(cursor.remaining(), " world!");

pub fn consume_n_if<P>(&mut self, n: usize, predicate: P) -> Option<&'a str> where
    P: FnOnce(&str) -> bool
[src]

Consumes n characters starting from the current position if the consumed string satisfies a predicate.

let mut cursor = InputCursor::new("hello world!");
assert_eq!(cursor.consume_n_if(5, |s| s.chars().all(|c| c.is_ascii_alphanumeric())), Some("hello"));
assert_eq!(cursor.remaining(), " world!");
assert_eq!(cursor.consume_n_if(5, |s| s.chars().all(|c| c.is_ascii_alphanumeric())), None);
assert_eq!(cursor.remaining(), " world!");
assert_eq!(cursor.consume_n_if(100, |_| true), None);

pub fn consume_if<P>(&mut self, predicate: P) -> Option<char> where
    P: FnOnce(char) -> bool
[src]

Consumes the following character if it satisfies the predicate.

let mut cursor = InputCursor::new("hello world!");
assert_eq!(cursor.consume_if(|c| c.is_ascii_alphanumeric()), Some('h'));
assert_eq!(cursor.consume_if(|c| c == ' '), None);

pub fn consume_while<P>(&mut self, predicate: P) -> Option<&'a str> where
    P: FnMut(char) -> bool
[src]

Consumes characters while the predicate is satisfied. Returns None if the string is empty.

let mut cursor = InputCursor::new("hello\nworld!");
assert_eq!(cursor.consume_while(|c| c != '\n'), Some("hello"));
assert_eq!(cursor.consume_while(|c| c != '\n'), None);
assert_eq!(cursor.next(), Some('\n'));

pub fn consume_first_n_while<P>(
    &mut self,
    n: usize,
    predicate: P
) -> Option<&'a str> where
    P: FnMut(char) -> bool
[src]

Consumes no more than n characters while they satisfy the predicate. Returns None if the string is empty.

let mut cursor = InputCursor::new("hello\nworld!");
assert_eq!(cursor.consume_first_n_while(4, |c| c != '\n'), Some("hell"));
assert_eq!(cursor.consume_first_n_while(4, |c| c != '\n'), Some("o"));
assert_eq!(cursor.consume_first_n_while(4, |c| c != '\n'), None);

pub fn starts_with(&self, s: &str) -> bool[src]

Checks whether the remaining string starts with the given substring.

let mut cursor = InputCursor::new("hello\nworld!");
assert!(cursor.starts_with("hell"));
cursor.skip_n(2);
assert!(cursor.starts_with("llo\nw"));
assert!(!cursor.starts_with("jelly"));

pub fn skip_line(&mut self) -> Position[src]

Skips the characters until the next line. Returns the position of the character preceding the line terminator (\r, \n, \r\n, \n\r).

let mut cursor = InputCursor::new("hello\r\nworld!");
let start = cursor.pos();
let end = cursor.skip_line();
assert_eq!(cursor.substr(start..=end), "hello");
assert_eq!(cursor.remaining(), "world!");

Trait Implementations

impl<'a> Clone for InputCursor<'a>[src]

impl<'a> Debug for InputCursor<'a>[src]

impl<'a> Iterator for InputCursor<'a>[src]

type Item = char

The type of the elements being iterated over.

Auto Trait Implementations

impl<'a> RefUnwindSafe for InputCursor<'a>

impl<'a> Send for InputCursor<'a>

impl<'a> Sync for InputCursor<'a>

impl<'a> Unpin for InputCursor<'a>

impl<'a> UnwindSafe for InputCursor<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.