From: Mark Date: Thu, 29 Jun 2023 20:58:15 +0000 (-0600) Subject: fix ReadlineStream peek_char using CharReader X-Git-Tag: v0.9.2~103 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=bb09de18059953dd45ac2168e63680072bbdc530;p=scryer-prolog.git fix ReadlineStream peek_char using CharReader --- diff --git a/src/parser/char_reader.rs b/src/parser/char_reader.rs index dddabb19..a2a7c2e2 100644 --- a/src/parser/char_reader.rs +++ b/src/parser/char_reader.rs @@ -111,7 +111,7 @@ impl CharReader { } impl CharReader { - fn refresh_buffer(&mut self) -> io::Result<&[u8]> { + pub fn refresh_buffer(&mut self) -> io::Result<&[u8]> { // If we've reached the end of our internal buffer then we need to fetch // some more data from the underlying reader. // Branch using `>=` instead of the more correct `==` diff --git a/src/read.rs b/src/read.rs index 8f70eeec..e42e3439 100644 --- a/src/read.rs +++ b/src/read.rs @@ -83,7 +83,7 @@ fn get_prompt() -> &'static str { #[derive(Debug)] pub struct ReadlineStream { rl: Editor, - pending_input: Cursor, + pending_input: CharReader>, add_history: bool, } @@ -107,7 +107,7 @@ impl ReadlineStream { ReadlineStream { rl, - pending_input: Cursor::new(pending_input.to_owned()), + pending_input: CharReader::new(Cursor::new(pending_input.to_owned())), add_history: add_history, } } @@ -119,29 +119,35 @@ impl ReadlineStream { #[inline] pub fn reset(&mut self) { - self.pending_input.get_mut().clear(); - self.pending_input.set_position(0); + self.pending_input.reset_buffer(); + + let pending_input = self.pending_input.get_mut(); + + pending_input.get_mut().clear(); + pending_input.set_position(0); } fn call_readline(&mut self) -> std::io::Result { match self.rl.readline(get_prompt()) { Ok(text) => { - *self.pending_input.get_mut() = text; - self.pending_input.set_position(0); + self.pending_input.reset_buffer(); + + *self.pending_input.get_mut().get_mut() = text; + self.pending_input.get_mut().set_position(0); unsafe { if PROMPT { - self.rl.history_mut().add(self.pending_input.get_ref()); + self.rl.history_mut().add(self.pending_input.get_ref().get_ref()); self.save_history(); PROMPT = false; } } - if self.pending_input.get_ref().chars().last() != Some('\n') { - *self.pending_input.get_mut() += "\n"; + if self.pending_input.get_ref().get_ref().chars().last() != Some('\n') { + *self.pending_input.get_mut().get_mut() += "\n"; } - Ok(self.pending_input.get_ref().len()) + Ok(self.pending_input.get_ref().get_ref().len()) } Err(ReadlineError::Eof) => Ok(0), Err(e) => Err(Error::new(ErrorKind::InvalidInput, e)), @@ -164,9 +170,13 @@ impl ReadlineStream { } } + #[inline] pub(crate) fn peek_byte(&mut self) -> std::io::Result { + let bytes = self.pending_input.refresh_buffer()?; + let byte = bytes.iter().next().cloned(); + loop { - match self.pending_input.get_ref().bytes().next() { + match byte { Some(0) => { return Ok(0); } @@ -178,7 +188,7 @@ impl ReadlineStream { return Err(e); } Ok(0) => { - self.pending_input.get_mut().push('\u{0}'); + self.pending_input.get_mut().get_mut().push('\u{0}'); return Ok(0); } _ => { @@ -203,24 +213,23 @@ impl Read for ReadlineStream { } impl CharRead for ReadlineStream { + #[inline] fn peek_char(&mut self) -> Option> { loop { - let pos = self.pending_input.position() as usize; - - match self.pending_input.get_ref()[pos ..].chars().next() { - Some('\u{0}') => { + match self.pending_input.peek_char() { + Some(Ok('\u{0}')) => { return Some(Ok('\u{0}')); } - Some(c) => { + Some(Ok(c)) => { return Some(Ok(c)); } - None => { + _ => { match self.call_readline() { Err(e) => { return Some(Err(e)); } Ok(0) => { - self.pending_input.get_mut().push('\u{0}'); + self.pending_input.get_mut().get_mut().push('\u{0}'); return Some(Ok('\u{0}')); } _ => { @@ -232,14 +241,14 @@ impl CharRead for ReadlineStream { } } + #[inline] fn consume(&mut self, nread: usize) { - let offset = self.pending_input.position() as usize; - self.pending_input.set_position((offset + nread) as u64); + self.pending_input.consume(nread); } + #[inline] fn put_back_char(&mut self, c: char) { - let offset = self.pending_input.position() as usize; - self.pending_input.set_position((offset - c.len_utf8()) as u64); + self.pending_input.put_back_char(c); } }