pub struct Stream<'a> { /* private fields */ }
Expand description
A stream of bytes and characters.
ImplementationsΒ§
SourceΒ§impl<'a> Stream<'a>
impl<'a> Stream<'a>
Sourcepub fn offset(&self) -> usize
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);
Sourcepub fn source(&self) -> &'a str
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");
Sourcepub fn len(&self) -> usize
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);
Sourcepub fn is_empty(&self) -> bool
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);
Sourcepub fn rest(&self) -> &'a str
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");
Sourcepub fn peek(&self, amount: usize) -> Option<u8>
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'));
Sourcepub fn peek_char(&self, amount: usize) -> Option<char>
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('π'));
Sourcepub fn current(&self) -> Option<u8>
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'));
Sourcepub fn current_char(&self) -> Option<char>
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('π'));
Sourcepub fn next(&mut self) -> Option<u8>
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'));
Sourcepub fn next_char(&mut self) -> Option<char>
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('π'));
Sourcepub fn peek_while(&self, f: impl FnMut(u8) -> bool) -> &'a str
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");
Sourcepub fn peek_while_char(&self, f: impl FnMut(char) -> bool) -> &'a str
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");
Sourcepub fn peek_until(&self, f: impl FnMut(u8) -> bool) -> &'a str
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!");
Sourcepub fn peek_until_char(&self, f: impl FnMut(char) -> bool) -> &'a str
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π");
Sourcepub fn take_while(&mut self, f: impl FnMut(u8) -> bool) -> &'a str
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);
Sourcepub fn take_while_char(&mut self, f: impl FnMut(char) -> bool) -> &'a str
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);
Sourcepub fn take_until(&mut self, f: impl FnMut(u8) -> bool) -> &'a str
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'!'));
Sourcepub fn take_until_char(&mut self, f: impl FnMut(char) -> bool) -> &'a str
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('π'));
Sourcepub fn peek_for(&self, amount: usize) -> &'a str
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'));
Sourcepub fn peek_for_char(&self, amount: usize) -> &'a str
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), "ππ ππππ");
Sourcepub fn advance(&mut self, amount: usize) -> &'a str
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());
Sourcepub fn advance_char(&mut self, amount: usize) -> &'a str
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());
Sourcepub fn eat(&mut self, m: &str) -> bool
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");