pub fn parse_iter<'s, I, F, O>(i: I, f: F) -> ParseIter<I, F> ⓘ
Available on crate feature
unstable-parse-iter
only.Expand description
From a ParseIterator
generate an Iterator
.
§Example parsing arguments
fn args(arg_str: &str) -> impl Iterator<Item=&str> {
parse_iter(
StrParser::new(arg_str),
|parser| {
// if we are in a string
if parser.advance_if(|&b| b == b'\'')? {
let s = parser.record()
.while_byte_fn(|&b| b != b'\'')
.consume_to_str();
// consume the ending ' and
// if a space follows consume it to
parser.advance();
parser.advance_if(|&b| b == b' ');
return Some(s)
}
// consume until a whitespace or an '
let s = parser.record()
.while_byte_fn(|&b| !matches!(b, b' ' | b'\''))
.consume_to_str();
// skip an empty space
parser.advance_if(|&b| b == b' ');
Some(s)
}
)
}
let args: Vec<_> = args("arg1 'arg 2' arg3'arg 4'arg5").collect();
assert_eq!(args, ["arg1", "arg 2", "arg3", "arg 4", "arg5"]);