summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-06-02 09:38:55 -0700
committerRose Hogenson <rosehogenson@posteo.net>2024-06-02 09:38:55 -0700
commit07c1a4a35923079d92867794b8ea621d44543ef1 (patch)
tree1edd1dbc9d952cde22013d37efb86acbf1b0b17c
parent22b8b7e2478e3b205c89cc7a700058bfe39175f2 (diff)
downloadsml-07c1a4a35923079d92867794b8ea621d44543ef1.tar.zst
Improve read.
-rw-r--r--bytecode/src/main.rs16
1 files changed, 7 insertions, 9 deletions
diff --git a/bytecode/src/main.rs b/bytecode/src/main.rs
index 6ccd4b8..ccb7c9a 100644
--- a/bytecode/src/main.rs
+++ b/bytecode/src/main.rs
@@ -6,7 +6,7 @@ use encoding::{Arg, Op};
use heap::Heap;
use std::error::Error;
use std::ffi::OsString;
-use std::io::{stdin, BufWriter, Read, Write};
+use std::io::{stdin, BufRead, BufWriter, Write};
use value::Value;
trait ReadArg {
@@ -183,16 +183,14 @@ impl State {
let Ok(len) = usize::try_from(ilen) else {
return Err(Box::from("read: len is negative"));
};
- let mut n = 0;
- for byte in stdin().lock().bytes().take(len) {
- let byte = byte?;
+ let mut stdin = stdin().lock();
+ let buf = stdin.fill_buf()?;
+ let n = std::cmp::min(len, buf.len());
+ for (i, &byte) in buf[..n].iter().enumerate() {
self.heap
- .poke(ptr, off + n, Value::from_int(i64::from(byte)))?;
- n += 1;
- if byte == b'\n' {
- break;
- }
+ .poke(ptr, off + i, Value::from_int(i64::from(byte)))?;
}
+ stdin.consume(n);
self.heap.locals[usize::from(op.out)] = Value::from_int(
i64::try_from(n)
.expect("we can never read more than len bytes, and len fit in an i64"),