commit cf8af2de534e9ef5403de6598c4840b3534b0695
parent 91bcdbeb4858cf39875b95b4b3c2be029e16dcfc
Author: Tom Lebreux <me@tomlebreux.com>
Date: Tue, 16 May 2023 20:58:51 -0400
bufio: allow borrowedread up to last byte
Signed-off-by: Tom Lebreux <me@tomlebreux.com>
Diffstat:
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/bufio/memstream.ha b/bufio/memstream.ha
@@ -141,7 +141,7 @@ export fn truncate(in: *memstream) (void | errors::unsupported) = {
// Reads data from a [[dynamic]] or [[fixed]] stream and returns a slice
// borrowed from the internal buffer.
export fn borrowedread(st: *memstream, amt: size) ([]u8 | io::EOF) = {
- if (len(st.buf) - st.pos <= amt) {
+ if (len(st.buf) - st.pos < amt) {
return io::EOF;
};
let buf = st.buf[st.pos..st.pos + amt];
@@ -255,6 +255,16 @@ fn copy(dest: *io::stream, src: *io::stream) (size | io::error) = {
let sink = dynamic(io::mode::WRITE);
io::copy(&sink, &source)!;
assert(bytes::equal(in, buffer(&sink)));
+
+ let in: [6]u8 = [0, 1, 2, 3, 4, 5];
+ let source = dynamic_from(in, io::mode::READ);
+ const borrowed = borrowedread(&source, len(in)-1) as []u8;
+ assert(bytes::equal(borrowed, [0, 1, 2, 3, 4]));
+ let source = dynamic_from(in, io::mode::READ);
+ const borrowed = borrowedread(&source, len(in)) as []u8;
+ assert(bytes::equal(borrowed, [0, 1, 2, 3, 4, 5]));
+ let source = dynamic_from(in, io::mode::READ);
+ assert(borrowedread(&source, len(in)+1) is io::EOF);
};
@test fn fixed() void = {