commit bda1f6e4e8e69fd843b0c6c86bce11ed7e17f0d6 parent b801af0c192c9c200ca782f8ab47dafb589bcf24 Author: Drew DeVault <sir@cmpwn.com> Date: Sun, 22 May 2022 18:40:16 +0200 bufio: add borrowedread Signed-off-by: Drew DeVault <sir@cmpwn.com> Diffstat:
M | bufio/memstream.ha | | | 11 | +++++++++++ |
1 file changed, 11 insertions(+), 0 deletions(-)
diff --git a/bufio/memstream.ha b/bufio/memstream.ha @@ -136,6 +136,17 @@ export fn truncate(in: *memstream) (void | errors::unsupported) = { delete(in.buf[..]); }; +// 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) { + return io::EOF; + }; + let buf = st.buf[st.pos..st.pos + amt]; + st.pos += len(buf); + return buf; +}; + fn dynamic_write(s: *io::stream, buf: const []u8) (size | io::error) = { let s = s: *memstream; insert(s.buf[s.pos], buf...);