[][src]Struct uwl::Stream

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

A stream of characters. Handles ASCII and/or Unicode.

Methods

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

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

Create a new stream from a source.

pub fn offset(&self) -> usize[src]

The "offset"; the start of the current char.

Example

use uwl::Stream;

let mut stream = Stream::new("a 🍆");

assert_eq!(stream.offset(), 0);
stream.next();
assert_eq!(stream.offset(), 1);
stream.next();
assert_eq!(stream.offset(), 2);
stream.next();
assert_eq!(stream.offset(), 6);

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

Returns the source the stream is operating on.

Example

use uwl::Stream;

let stream = Stream::new("Once upon a time... life");

assert_eq!(stream.source(), "Once upon a time... life");

pub fn len(&self) -> usize[src]

The provided source's length. Returns the amount of bytes.

Example

use uwl::Stream;

let mut stream = Stream::new("abc🍆");
assert_eq!(stream.len(), 7);
stream.next();
// Regardless of any modification method present on the stream,
// `len` always returns a constant.
assert_eq!(stream.len(), 7);

pub fn is_empty(&self) -> bool[src]

Is the provided source empty?

Example

use uwl::Stream;

let stream = Stream::new("");

assert!(stream.is_empty());
assert_eq!(stream.source(), "");

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

Returns the remainder (after the offset).

Example

use uwl::Stream;

let mut stream = Stream::new("foo bar");

assert_eq!(stream.take_until(|s| s == ' '), "foo");
assert_eq!(stream.next(), Some(' '));
assert_eq!(stream.rest(), "bar");

pub fn at_end(&self) -> bool[src]

Determines the end of the input.

Example

use uwl::Stream;

let mut stream = Stream::new("a");

assert!(!stream.at_end());
stream.next();
assert!(stream.at_end());
assert_eq!(stream.current(), None);

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

Fetch the current char.

Example

use uwl::Stream;

let stream = Stream::new("hello");

assert_eq!(stream.current(), Some('h'));

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

Advance to the next char

Example

use uwl::Stream;

let mut stream = Stream::new("hello");

assert_eq!(stream.current(), Some('h'));

assert_eq!(stream.next(), Some('h'));
assert_eq!(stream.current(), Some('e'));

pub fn take_while(&mut self, f: impl FnMut(char) -> bool) -> &'a str[src]

Consume while true.

Example

use uwl::Stream;

let mut stream = Stream::new("hello");

assert_eq!(stream.current(), Some('h'));
assert_eq!(stream.take_while(|s| s.is_alphabetic()), "hello");
assert_eq!(stream.current(), None);

pub fn take_until(&mut self, f: impl FnMut(char) -> bool) -> &'a str[src]

Consume until true.

Example

use uwl::Stream;

let mut stream = Stream::new("hello!");

assert_eq!(stream.current(), Some('h'));
assert_eq!(stream.take_until(|s| s == '!'), "hello");
assert_eq!(stream.current(), Some('!'));

pub fn peek_while(&self, f: impl FnMut(char) -> bool) -> &'a str[src]

Lookahead for as long as f returns true. Returns a substring up to the ending character that it landed on.

Example

use uwl::{Stream, CharExt};

let mut stream = Stream::new("hello _wo_r_l_4d");

assert_eq!(stream.peek_while(|s| s.is_alphabetic()), "hello");
assert_eq!(stream.rest(), "hello _wo_r_l_4d");
stream.take_while(|s| s.is_alphabetic());
stream.next();
assert_eq!(stream.peek_while(|s| s.is_diglet()), "_wo_r_l_4d");
assert_eq!(stream.rest(), "_wo_r_l_4d");

pub fn peek_until(&self, f: impl FnMut(char) -> bool) -> &'a str[src]

Lookahead for as long as f does not return true. Returns a substring up to the end it landed on.

Example

use uwl::Stream;

let mut stream = Stream::new("hello!");

assert_eq!(stream.peek_until(|s| s == '!'), "hello");
assert_eq!(stream.rest(), "hello!");

pub fn peek_for(&self, much: usize) -> &'a str[src]

Lookahead by x chars. Returns a substring up to the ending character that it landed on.

Example

use uwl::Stream;

let mut stream = Stream::new("hello world");

assert_eq!(stream.current(), Some('h'));
assert_eq!(stream.peek_for(5), "hello");

for _ in 0..5 {
    stream.next();
}

assert_eq!(stream.next(), Some(' '));
assert_eq!(stream.peek_for(5), "world");
assert_eq!(stream.next(), Some('w'));
assert_eq!(stream.next(), Some('o'));
assert_eq!(stream.next(), Some('r'));
assert_eq!(stream.next(), Some('l'));
assert_eq!(stream.next(), Some('d'));

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

Advance by x characters. Short-circuits if there are no more characters available.

Example

use uwl::Stream;

let mut stream = Stream::new("hello world");

assert_eq!(stream.advance(5), "hello");
stream.next();
assert_eq!(stream.advance(5), "world");
assert!(stream.at_end());

pub fn eat(&mut self, m: &str) -> bool[src]

Advance if the leading string matches to the expected input. Returns true on succession, false on failure.

Example

use uwl::Stream;

let mut stream = Stream::new("hello world");

assert!(stream.eat("hello"));
assert!(!stream.eat("not a space"));
assert!(stream.eat(" "));
assert_eq!(stream.rest(), "world");

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

Lookahead by x characters. Returns the character it landed on.

Example

use uwl::Stream;

let mut stream = Stream::new("hello");

assert_eq!(stream.current(), Some('h'));
assert_eq!(stream.peek(1), Some('e'));
assert_eq!(stream.current(), Some('h'));
assert_eq!(stream.peek(2), Some('l'));
assert_eq!(stream.current(), Some('h'));

pub fn set(&mut self, pos: usize)[src]

Set the offset to the pos.

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

Increment the offset by n bytes.

Trait Implementations

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

impl<'a> Copy for Stream<'a>[src]

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

impl<'a> Default for Stream<'a>[src]

Auto Trait Implementations

impl<'a> Unpin for Stream<'a>

impl<'a> Send for Stream<'a>

impl<'a> Sync for Stream<'a>

Blanket Implementations

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

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> Into<U> for T where
    U: From<T>, 
[src]

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.

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

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

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