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()),
|
||||
},
|
||||
Ok(2) => {
|
||||
if buf[0] != b'\x1B' {
|
||||
// this is not an escape sequence, but we read two bytes, save the second byte
|
||||
// for later
|
||||
self.leftover = Some(buf[1]);
|
||||
}
|
||||
|
||||
let mut iter = buf[1..2].iter().map(|c| Ok(*c)).chain(source.bytes());
|
||||
parse_event(Ok(buf[0]), &mut iter)
|
||||
let mut option_iter = &mut Some(buf[1]).into_iter();
|
||||
let result = {
|
||||
let mut iter = option_iter.map(|c| Ok(c)).chain(source.bytes());
|
||||
parse_event(Ok(buf[0]), &mut iter)
|
||||
};
|
||||
// If the option_iter wasn't consumed, keep the byte for later.
|
||||
self.leftover = option_iter.next();
|
||||
result
|
||||
}
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => Err(e),
|
||||
|
|
Loading…
Reference in New Issue