fix bugs in PausedPrologStream (#661)

This commit is contained in:
Mark Thom
2020-08-08 13:11:09 -06:00
parent f627b32355
commit 6e5d2d6a36

View File

@@ -6,7 +6,7 @@ use crate::machine::machine_errors::*;
use crate::machine::machine_indices::*; use crate::machine::machine_indices::*;
use crate::machine::machine_state::*; use crate::machine::machine_state::*;
use std::cmp::{min, Ordering}; use std::cmp::Ordering;
use std::cell::RefCell; use std::cell::RefCell;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
@@ -110,7 +110,7 @@ fn parser_top_to_bytes(mut buf: Vec<io::Result<char>>) -> io::Result<Vec<u8>> {
/* all these streams are closed automatically when the instance is /* all these streams are closed automatically when the instance is
* dropped. */ * dropped. */
pub enum StreamInstance { enum StreamInstance {
Bytes(Cursor<Vec<u8>>), Bytes(Cursor<Vec<u8>>),
InputFile(ClauseName, File), InputFile(ClauseName, File),
OutputFile(ClauseName, File, bool), // File, append. OutputFile(ClauseName, File, bool), // File, append.
@@ -129,16 +129,20 @@ impl StreamInstance {
StreamInstance::PausedPrologStream(ref mut put_back, ref mut stream) => { StreamInstance::PausedPrologStream(ref mut put_back, ref mut stream) => {
let mut index = 0; let mut index = 0;
while index < min(buf.len(), put_back.len()) { while index < buf.len() {
let b = put_back.pop().unwrap(); if let Some(b) = put_back.pop() {
buf[index] = b; buf[index] = b;
index += 1; index += 1;
} else {
break;
}
} }
if index == buf.len() { if index == buf.len() {
Ok(buf.len()) Ok(buf.len())
} else { } else {
stream.read(&mut buf[index ..]) stream.read(&mut buf[index ..])
.map(|bytes_read| bytes_read + index)
} }
} }
StreamInstance::InputFile(_, ref mut file) => { StreamInstance::InputFile(_, ref mut file) => {