Discard leftover input when consumed (#76)
* Discard leftover input when consumed The iterator given to `parse_event` now `take()` the value from `leftover`. * Always save leftover `parse_event` will consume it when required. * Remove OptionIterator, directly uses std::option::Iter
This commit is contained in:
parent
00ca758d45
commit
c2df3d7edd
16
src/input.rs
16
src/input.rs
|
@ -56,14 +56,14 @@ impl<R: Read> Iterator for Events<R> {
|
||||||
c => parse_event(Ok(c), &mut source.bytes()),
|
c => parse_event(Ok(c), &mut source.bytes()),
|
||||||
},
|
},
|
||||||
Ok(2) => {
|
Ok(2) => {
|
||||||
if buf[0] != b'\x1B' {
|
let mut option_iter = &mut Some(buf[1]).into_iter();
|
||||||
// this is not an escape sequence, but we read two bytes, save the second byte
|
let result = {
|
||||||
// for later
|
let mut iter = option_iter.map(|c| Ok(c)).chain(source.bytes());
|
||||||
self.leftover = Some(buf[1]);
|
parse_event(Ok(buf[0]), &mut iter)
|
||||||
}
|
};
|
||||||
|
// If the option_iter wasn't consumed, keep the byte for later.
|
||||||
let mut iter = buf[1..2].iter().map(|c| Ok(*c)).chain(source.bytes());
|
self.leftover = option_iter.next();
|
||||||
parse_event(Ok(buf[0]), &mut iter)
|
result
|
||||||
}
|
}
|
||||||
Ok(_) => unreachable!(),
|
Ok(_) => unreachable!(),
|
||||||
Err(e) => Err(e),
|
Err(e) => Err(e),
|
||||||
|
|
Loading…
Reference in New Issue