commit 90e7993b3629d1334cb71f1248d3700f6eea7992
parent 76e8fad0f03bd09e201031300e795de589135008
Author: Drew DeVault <sir@cmpwn.com>
Date: Thu, 25 Feb 2021 14:09:45 -0500
strio: add truncate, reset
Diffstat:
1 file changed, 20 insertions(+), 0 deletions(-)
diff --git a/strio/dynamic.ha b/strio/dynamic.ha
@@ -36,6 +36,26 @@ export fn finish(s: *io::stream) str = {
return strings::from_utf8(buf);
};
+// Resets the buffer's length to zero, but keeps the allocated memory around for
+// future writes.
+export fn reset(s: *io::stream) (void | io::unsupported) = {
+ if (s.writer != &dynamic_write || s.closer != &dynamic_close) {
+ return io::unsupported;
+ };
+ const s = s: *dynamic_stream;
+ s.buf = s.buf[..0];
+};
+
+// Truncates the buffer, freeing memory associated with it and setting its
+// length to zero.
+export fn truncate(s: *io::stream) (void | io::unsupported) = {
+ if (s.writer != &dynamic_write || s.closer != &dynamic_close) {
+ return io::unsupported;
+ };
+ let s = s: *dynamic_stream;
+ delete(s.buf[..]);
+};
+
fn dynamic_write(s: *io::stream, buf: const []u8) (size | io::error) = {
let s = s: *dynamic_stream;
append(s.buf, ...buf);