pub struct StringStream<'a> {
pub src: &'a str,
// some fields omitted
}
A stream of chars. Handles both ASCII and Unicode.
This stream returns chars as &str
s. In instances like take_while
, the &str
refers to actual multi-char substrings (e.g "foo").
The source this stream operates on.
Create a new stream from a source.
Fetch the current char.
use uwl::StringStream;
let stream = StringStream::new("hello");
assert_eq!(stream.current(), Some("h"));
Advance to the next char
use uwl::StringStream;
let mut stream = StringStream::new("hello");
assert_eq!(stream.current(), Some("h"));
stream.next();
assert_eq!(stream.current(), Some("e"));
Advance by x chars.
use uwl::StringStream;
let mut stream = StringStream::new("hello world");
assert_eq!(stream.advance(5), Some("hello"));
stream.next();
assert_eq!(stream.advance(5), Some("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.
use uwl::StringStream;
let mut stream = StringStream::new("hello world");
assert!(stream.eat("hello"));
assert!(!stream.eat("not a space"));
assert!(stream.eat(" "));
assert_eq!(stream.rest(), "world");
Lookahead by x chars. Returns the char it landed on.
This does not actually modify, it just needs to temporarily advance.
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"));
Lookahead by x chars. Returns a substring up to the end it landed on.
This does not actually modify, it just needs to temporarily advance.
use uwl::StringStream;
let mut stream = StringStream::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"));
Deprecated since 0.1.3:
renamed to peek_for
Lookahead by x chars. Returns a substring up to the end it landed on.
This does not actually modify, it just needs to temporarily advance.
Use peek_for
instead.
Consume while true.
use uwl::StringStream;
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);
Consume until true.
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("!"));
Slice a portion from the stream by using rules defined by the formatting string
{}
=> the portion to return
(x)
=> optional text x that may be present, ignored
- letters/numbers => text to expect
To espace {
or (
, use them twice. For example, ((
/{{
.
This is not necessary for }
or )
.
Whitespace between {
and }
is skipped.
If parsing does not succeed, this won't advance.
On Err
, the expected string, or "parse failed", are returned.
Get anything between html h1
tags
use uwl::StringStream;
let mut stream = StringStream::new("<h1>hello world!</h1>");
assert_eq!(stream.parse("<h1>{}</h1>"), Ok("hello world!"));
Parse html tags
use uwl::StringStream;
let mut stream = StringStream::new("<h2></h2>");
assert_eq!(stream.parse("<(/){}>"), Ok("h2"));
assert_eq!(stream.parse("<(/){}>"), Ok("h2"));
Returns the remainder (after the offset).
use uwl::StringStream;
let mut stream = StringStream::new("foo bar");
assert_eq!(stream.take_until(|s| s == " "), "foo");
assert_eq!(stream.next(), Some(" "));
assert_eq!(stream.rest(), "bar");
Determines the end of the input.
use uwl::StringStream;
let mut stream = StringStream::new("a");
assert!(!stream.at_end());
stream.next();
assert!(stream.at_end());
assert_eq!(stream.current(), None);
The "offset"; the start of the current char.
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);
Set the offset.
Panics if the offset is in the middle of a unicode character, or exceeds the length of the input.
Set the offset without any checks.
Formats the value using the given formatter. Read more
Performs copy-assignment from source
. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static