hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 172bf36d3834b36349e16f6c50f74e99368edbbc
parent dedc4342fc1ba0750e0c7c40109b6a4b414b2ccf
Author: Bor Grošelj Simić <bgs@turminal.net>
Date:   Tue, 19 Apr 2022 21:13:47 +0200

strio: simplify and unify fixed and dynamic types

Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>

Diffstat:
Mglob/glob.ha | 12++++++------
Mscripts/gen-stdlib | 2+-
Mstdlib.mk | 4++--
Mstrio/dynamic.ha | 14+++++++-------
Mstrio/fixed.ha | 35++++++++++++-----------------------
5 files changed, 28 insertions(+), 39 deletions(-)

diff --git a/glob/glob.ha b/glob/glob.ha @@ -29,11 +29,11 @@ export type generator = struct { pats: strstack, matc: size, flgs: flags, - tmps: strio::dynamic_stream, + tmps: strio::stream, }; export type strstack = struct { - bufv: []strio::dynamic_stream, + bufv: []strio::stream, bufc: size, }; @@ -234,7 +234,7 @@ fn strstack_free(ss: *strstack) void = { fn strstack_size(ss: *strstack) size = ss.bufc; -fn strstack_push(ss: *strstack) *strio::dynamic_stream = { +fn strstack_push(ss: *strstack) *strio::stream = { if (ss.bufc == len(ss.bufv)) { append(ss.bufv, strio::dynamic()); }; @@ -257,11 +257,11 @@ fn strstack_sort(ss: *strstack, pos: size) void = { return; }; let s = ss.bufv[pos..ss.bufc]; - sort::sort(s, size(strio::dynamic_stream), &bufcmp); + sort::sort(s, size(strio::stream), &bufcmp); }; fn bufcmp(a: const *void, b: const *void) int = strings::strcmp( - strio::string(b: *strio::dynamic_stream), - strio::string(a: *strio::dynamic_stream), + strio::string(b: *strio::stream), + strio::string(a: *strio::stream), ); diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -1127,7 +1127,7 @@ strio() { dynamic.ha \ fixed.ha \ ops.ha - gen_ssa strio io strings encoding::utf8 errors + gen_ssa strio io strings slices encoding::utf8 errors } temp() { diff --git a/stdlib.mk b/stdlib.mk @@ -1710,7 +1710,7 @@ stdlib_strio_any_srcs= \ $(STDLIB)/strio/fixed.ha \ $(STDLIB)/strio/ops.ha -$(HARECACHE)/strio/strio-any.ssa: $(stdlib_strio_any_srcs) $(stdlib_rt) $(stdlib_io_$(PLATFORM)) $(stdlib_strings_$(PLATFORM)) $(stdlib_encoding_utf8_$(PLATFORM)) $(stdlib_errors_$(PLATFORM)) +$(HARECACHE)/strio/strio-any.ssa: $(stdlib_strio_any_srcs) $(stdlib_rt) $(stdlib_io_$(PLATFORM)) $(stdlib_strings_$(PLATFORM)) $(stdlib_slices_$(PLATFORM)) $(stdlib_encoding_utf8_$(PLATFORM)) $(stdlib_errors_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(HARECACHE)/strio @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nstrio \ @@ -3703,7 +3703,7 @@ testlib_strio_any_srcs= \ $(STDLIB)/strio/fixed.ha \ $(STDLIB)/strio/ops.ha -$(TESTCACHE)/strio/strio-any.ssa: $(testlib_strio_any_srcs) $(testlib_rt) $(testlib_io_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_encoding_utf8_$(PLATFORM)) $(testlib_errors_$(PLATFORM)) +$(TESTCACHE)/strio/strio-any.ssa: $(testlib_strio_any_srcs) $(testlib_rt) $(testlib_io_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_slices_$(PLATFORM)) $(testlib_encoding_utf8_$(PLATFORM)) $(testlib_errors_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(TESTCACHE)/strio @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nstrio \ diff --git a/strio/dynamic.ha b/strio/dynamic.ha @@ -5,7 +5,7 @@ use errors; use io; use strings; -export type dynamic_stream = struct { +export type stream = struct { stream: io::stream, buf: []u8, }; @@ -16,8 +16,8 @@ export type dynamic_stream = struct { // Calling [[io::close]] on this stream will free the buffer. If stream's data // is transfered via [[string]], it shouldn't be closed as long if the data is // freed. -export fn dynamic() dynamic_stream = { - return dynamic_stream { +export fn dynamic() stream = { + return stream { stream = io::stream { writer = &dynamic_write, closer = &dynamic_close, @@ -29,24 +29,24 @@ export fn dynamic() dynamic_stream = { // Resets the buffer's length to zero, but keeps the allocated memory around for // future writes. -export fn reset(in: *dynamic_stream) void = { +export fn reset(in: *stream) void = { in.buf = in.buf[..0]; }; // Truncates the buffer, freeing memory associated with it and setting its // length to zero. -export fn truncate(in: *dynamic_stream) void = { +export fn truncate(in: *stream) void = { delete(in.buf[..]); }; fn dynamic_write(s: *io::stream, buf: const []u8) (size | io::error) = { - let s = s: *dynamic_stream; + let s = s: *stream; append(s.buf, buf...); return len(buf); }; fn dynamic_close(s: *io::stream) void = { - const s = s: *dynamic_stream; + const s = s: *stream; free(s.buf); }; diff --git a/strio/fixed.ha b/strio/fixed.ha @@ -2,48 +2,37 @@ // (c) 2022 Alexey Yerin <yyp@disroot.org> // (c) 2021 Drew DeVault <sir@cmpwn.com> use io; +use slices; use strings; -export type fixed_stream = struct { - stream: io::stream, - buf: []u8, - cur: []u8, -}; - // Creates a write-only string stream using the provided buffer for storage. // The program aborts if writes would exceed the buffer's capacity. The stream // doesn't need to be closed. -export fn fixed(in: []u8) fixed_stream = { - return fixed_stream { +export fn fixed(in: []u8) stream = { + return stream { stream = io::stream { writer = &fixed_write, ... }, - buf = in, - cur = in, + buf = in[..0], }; }; // Returns the current contents of the buffer as a string. Aborts the program if // invalid UTF-8 has been written to the buffer. -export fn string(in: (*fixed_stream | *dynamic_stream)) str = { - match (in) { - case let s: *fixed_stream => - const n = len(s.buf) - len(s.cur); - return strings::fromutf8(s.buf[..n]); - case let s: *dynamic_stream => - return strings::fromutf8(s.buf); - }; +export fn string(in: *stream) str = { + let in = in: *stream; + return strings::fromutf8(in.buf); }; fn fixed_write(s: *io::stream, buf: const []u8) (size | io::error) = { - let stream = s: *fixed_stream; - if (len(stream.cur) == 0) { + let s = s: *stream; + let cap = slices::cap(s.buf); + if (cap == len(s.buf)) { abort("strio::fixed buffer exceeded"); }; - const n = if (len(buf) > len(stream.cur)) len(stream.cur) else len(buf); - stream.cur[..n] = buf[..n]; - stream.cur = stream.cur[n..]; + let n = if (cap - len(s.buf) < len(buf)) cap - len(s.buf) else len(buf); + static append(s.buf, buf[..n]...); return n; };