Struct Stream

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

A stream of bytes and characters.

ImplementationsΒ§

SourceΒ§

impl<'a> Stream<'a>

Source

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

Create a new stream from a string source.

Source

pub fn offset(&self) -> usize

Returns the current position of the stream.

Β§Example
use uwl::Stream;

let mut stream = Stream::new("an πŸ†");

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(), 3);
stream.next_char();
assert_eq!(stream.offset(), 7);
Source

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

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");
Source

pub fn len(&self) -> usize

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);
Source

pub fn is_empty(&self) -> bool

Returns a boolean indicating whether there are no more bytes to parse.

Β§Example
use uwl::Stream;

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

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

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

Returns the remainder (after the offset).

Β§Example
use uwl::Stream;

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

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

pub fn peek(&self, amount: usize) -> Option<u8>

Look ahead by the amount of bytes.

Β§Example
use uwl::Stream;

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

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

pub fn peek_char(&self, amount: usize) -> Option<char>

Look ahead by the amount of characters.

Β§Example
use uwl::Stream;

let mut stream = Stream::new("πŸ†πŸ‘…πŸ†");

assert_eq!(stream.current_char(), Some('πŸ†'));
assert_eq!(stream.peek_char(0), Some('πŸ†'));
assert_eq!(stream.current_char(), Some('πŸ†'));
assert_eq!(stream.peek_char(1), Some('πŸ‘…'));
assert_eq!(stream.current_char(), Some('πŸ†'));
assert_eq!(stream.peek_char(2), Some('πŸ†'));
Source

pub fn current(&self) -> Option<u8>

Fetch the current byte.

Β§Example
use uwl::Stream;

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

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

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

Fetch the current character.

Β§Example
use uwl::Stream;

let stream = Stream::new("πŸ˜ƒ πŸ˜‚");

assert_eq!(stream.current_char(), Some('πŸ˜ƒ'));
Source

pub fn next(&mut self) -> Option<u8>

Advance to the next byte. Returns the current byte prior to advancement.

Β§Example
use uwl::Stream;

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

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

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

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

Advance to the next character. Returns the current character prior to advancement.

Β§Example
use uwl::Stream;

let mut stream = Stream::new("πŸ˜ƒ πŸ˜‚");

assert_eq!(stream.next_char(), Some('πŸ˜ƒ'));
assert_eq!(stream.next(), Some(b' '));
assert_eq!(stream.current_char(), Some('πŸ˜‚'));
Source

pub fn peek_while(&self, f: impl FnMut(u8) -> bool) -> &'a str

Look ahead based on a predicate while it returns true. Returns a string before the lookahead and after.

Β§Example
use uwl::Stream;

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

assert_eq!(stream.peek_while(|b| b.is_ascii_alphabetic()), "hello");
assert_eq!(stream.rest(), "hello _wo_r_l_4d");
stream.take_while(|b| b.is_ascii_alphabetic());
assert_eq!(stream.next(), Some(b' '));
assert_eq!(stream.peek_while(|b| b == b'_' || b.is_ascii_alphanumeric()), "_wo_r_l_4d");
assert_eq!(stream.rest(), "_wo_r_l_4d");
Source

pub fn peek_while_char(&self, f: impl FnMut(char) -> bool) -> &'a str

Look ahead based on a predicate while it returns true. Returns a string before the lookahead and after.

Β§Example
use uwl::Stream;

let mut stream = Stream::new("πŸ†πŸ†πŸ†πŸ†πŸ† _wo_r_l_4d");

assert_eq!(stream.peek_while_char(|c| c == 'πŸ†'), "πŸ†πŸ†πŸ†πŸ†πŸ†");
assert_eq!(stream.rest(), "πŸ†πŸ†πŸ†πŸ†πŸ† _wo_r_l_4d");
stream.take_while_char(|c| c == 'πŸ†');
assert_eq!(stream.next(), Some(b' '));
assert_eq!(stream.peek_while(|b| b == b'_' || b.is_ascii_alphanumeric()), "_wo_r_l_4d");
assert_eq!(stream.rest(), "_wo_r_l_4d");
Source

pub fn peek_until(&self, f: impl FnMut(u8) -> bool) -> &'a str

Look ahead based on a predicate until it returns true. Returns a string before the lookahead and after.

Β§Example
use uwl::Stream;

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

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

pub fn peek_until_char(&self, f: impl FnMut(char) -> bool) -> &'a str

Look ahead based on a predicate until it returns true. Returns a string before the lookahead and after.

Β§Example
use uwl::Stream;

let mut stream = Stream::new("helloπŸ†");

assert_eq!(stream.peek_until_char(|c| c == 'πŸ†'), "hello");
assert_eq!(stream.rest(), "helloπŸ†");
Source

pub fn take_while(&mut self, f: impl FnMut(u8) -> bool) -> &'a str

Consume bytes based on a predicate while it returns true. Returns a string before the consumption and after.

Β§Example
use uwl::Stream;

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

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

pub fn take_while_char(&mut self, f: impl FnMut(char) -> bool) -> &'a str

Consume bytes based on a predicate while it returns true. Returns a string before the consumption and after.

Β§Example
use uwl::Stream;

let mut stream = Stream::new("πŸ†πŸ†πŸ†");

assert_eq!(stream.current_char(), Some('πŸ†'));
assert_eq!(stream.take_while_char(|c| c == 'πŸ†'), "πŸ†πŸ†πŸ†");
assert_eq!(stream.current_char(), None);
Source

pub fn take_until(&mut self, f: impl FnMut(u8) -> bool) -> &'a str

Consume bytes based on a predicate until it returns true. Returns a string before the consumption and after.

Β§Example
use uwl::Stream;

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

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

pub fn take_until_char(&mut self, f: impl FnMut(char) -> bool) -> &'a str

Consume bytes based on a predicate until it returns true. Returns a string before the consumption and after.

Β§Example
use uwl::Stream;

let mut stream = Stream::new("helloπŸ†");

assert_eq!(stream.current(), Some(b'h'));
assert_eq!(stream.take_until_char(|c| c == 'πŸ†'), "hello");
assert_eq!(stream.current_char(), Some('πŸ†'));
Source

pub fn peek_for(&self, amount: usize) -> &'a str

Look ahead by the amount of bytes. Returns a string before the lookahead and after.

Β§Note

If the amount exceeds the amount of available bytes, this will return the rest of the source.

Β§Example
use uwl::Stream;

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

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

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

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

pub fn peek_for_char(&self, amount: usize) -> &'a str

Look ahead by the amount of characters. Returns a string before the lookahead and after.

Β§Note

If the amount exceeds the amount of available characters, this will return the rest of the source.

Β§Example
use uwl::Stream;

let mut stream = Stream::new("hello πŸ†πŸ† πŸ†πŸ†πŸ†πŸ†");

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

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

assert_eq!(stream.next(), Some(b' '));
assert_eq!(stream.peek_for_char(2), "πŸ†πŸ†");
assert_eq!(stream.peek_for_char(10), "πŸ†πŸ† πŸ†πŸ†πŸ†πŸ†");
Source

pub fn advance(&mut self, amount: usize) -> &'a str

Advance onward by the amount of bytes. Returns a string before the advancement and after.

Β§Note

If the amount exceeds the amount of available bytes, this will return the rest of the source.

Β§Example
use uwl::Stream;

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

assert_eq!(stream.advance(5), "hello");
assert_eq!(stream.next(), Some(b' '));
assert_eq!(stream.advance(2), "wo");
assert_eq!(stream.advance(5), "rld");
assert!(stream.is_empty());
Source

pub fn advance_char(&mut self, amount: usize) -> &'a str

Advance onward by the amount of bytes. Returns a string before the advancement and after.

Β§Note

If the amount exceeds the amount of available bytes, this will return the rest of the source.

Β§Example
use uwl::Stream;

let mut stream = Stream::new("hello πŸ†πŸ† πŸ†πŸ†πŸ†");

assert_eq!(stream.advance_char(5), "hello");
assert_eq!(stream.next(), Some(b' '));
assert_eq!(stream.advance_char(2), "πŸ†πŸ†");
assert_eq!(stream.next(), Some(b' '));
assert_eq!(stream.advance_char(6), "πŸ†πŸ†πŸ†");
assert!(stream.is_empty());
Source

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

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");
Source

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

Set the offset to the pos.

Source

pub fn increment(&mut self, amount: usize)

Increments the offset by the amount

Trait ImplementationsΒ§

SourceΒ§

impl<'a> Clone for Stream<'a>

SourceΒ§

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

Returns a copy 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<'a> Debug for Stream<'a>

SourceΒ§

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

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

impl<'a> Default for Stream<'a>

SourceΒ§

fn default() -> Stream<'a>

Returns the β€œdefault value” for a type. Read more
SourceΒ§

impl<'a> Copy for Stream<'a>

Auto Trait ImplementationsΒ§

Β§

impl<'a> Freeze for Stream<'a>

Β§

impl<'a> RefUnwindSafe for Stream<'a>

Β§

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

Β§

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

Β§

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

Β§

impl<'a> UnwindSafe for Stream<'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, 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.