Struct uwl::StringStream [−][src]
pub struct StringStream<'a> { pub src: &'a str, pub line: usize, pub column: usize, // some fields omitted }
A stream of chars. Handles both ASCII and Unicode.
Note
This stream returns chars as &str
s. In instances like take_while
, the &str
refers to actual multi-char substrings (e.g "foo").
Fields
src: &'a str
The source this stream operates on.
line: usize
Current line.
column: usize
Current column.
Methods
impl<'a> StringStream<'a>
[src]
impl<'a> StringStream<'a>
pub fn new(src: &'a str) -> Self
[src]
pub fn new(src: &'a str) -> Self
Create a new stream from a source.
pub fn current(&self) -> Option<&'a str>
[src]
pub fn current(&self) -> Option<&'a str>
Fetch the current char.
Example
use uwl::StringStream; let stream = StringStream::new("hello"); assert_eq!(stream.current(), Some("h"));
pub fn next(&mut self) -> Option<&'a str>
[src]
pub fn next(&mut self) -> Option<&'a str>
Advance to the next char
Example
use uwl::StringStream; let mut stream = StringStream::new("hello"); assert_eq!(stream.current(), Some("h")); stream.next(); assert_eq!(stream.current(), Some("e"));
pub fn peek(&mut self, ahead: usize) -> Option<&'a str>
[src]
pub fn peek(&mut self, ahead: usize) -> Option<&'a str>
Lookahead by x chars. Returns the char it landed on. This does not actually modify the order, it just needs to temporarily advance.
Example
use uwl::StringStream; let mut stream = StringStream::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 peek_str(&mut self, ahead: usize) -> &'a str
[src]
pub fn peek_str(&mut self, ahead: usize) -> &'a str
Lookahead by x chars. Returns a substring up to the end it landed on. This does not actually modify the order, it just needs to temporarily advance.
Example
use uwl::StringStream; let mut stream = StringStream::new("hello world"); assert_eq!(stream.current(), Some("h")); assert_eq!(stream.peek_str(5), "hello"); for _ in 0..5 { stream.next(); } assert_eq!(stream.next(), Some(" ")); assert_eq!(stream.peek_str(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 take_while(
&mut self,
f: impl Fn(&str) -> bool
) -> &'a str
[src]
pub fn take_while(
&mut self,
f: impl Fn(&str) -> bool
) -> &'a str
Consume while true.
Example
use uwl::StringStream; // Import a few utility methods (for `is_alphabetic`) use uwl::StrExt; let mut stream = StringStream::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 Fn(&str) -> bool
) -> &'a str
[src]
pub fn take_until(
&mut self,
f: impl Fn(&str) -> bool
) -> &'a str
Consume until true.
Example
use uwl::StringStream; let mut stream = StringStream::new("hello!"); assert_eq!(stream.current(), Some("h")); assert_eq!(stream.take_until(|s| s == "!"), "hello"); assert_eq!(stream.current(), Some("!"));
pub fn at_end(&self) -> bool
[src]
pub fn at_end(&self) -> bool
Determines the end of the input.
Example
use uwl::StringStream; let mut stream = StringStream::new("a"); assert!(!stream.at_end()); stream.next(); assert!(stream.at_end()); assert_eq!(stream.current(), None);
pub fn offset(&self) -> usize
[src]
pub fn offset(&self) -> usize
The "offset"; the start of the current char.
Example
use uwl::StringStream; let mut stream = StringStream::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 set(&mut self, pos: usize)
[src]
pub fn set(&mut self, pos: usize)
Set the offset. Panics if the offset is in the middle of a unicode character, or exceeds the length of the input.
pub unsafe fn set_unchecked(&mut self, pos: usize)
[src]
pub unsafe fn set_unchecked(&mut self, pos: usize)
Set the offset without any checks.
Trait Implementations
impl<'a> Debug for StringStream<'a>
[src]
impl<'a> Debug for StringStream<'a>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<'a> Clone for StringStream<'a>
[src]
impl<'a> Clone for StringStream<'a>
fn clone(&self) -> StringStream<'a>
[src]
fn clone(&self) -> StringStream<'a>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl<'a> Send for StringStream<'a>
impl<'a> Send for StringStream<'a>
impl<'a> Sync for StringStream<'a>
impl<'a> Sync for StringStream<'a>