hare

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

commit d6a83c1a73fbab71d2e32f9eea612c34f6a6f777
parent 285ca433af30adfa6fce319abc0f5ce7205c72ee
Author: Sebastian <sebastian@sebsite.pw>
Date:   Wed, 27 Apr 2022 21:00:47 -0400

all: don't use io::write when full write is required

In all places where it's expected that all data will be written,
io::write is replaced with either io::writeall or fmt::fprint, depending
on the context.

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mbufio/buffered.ha | 14+++++++-------
Mbufio/memstream.ha | 22++++++++++++----------
Mcmd/hare/plan.ha | 2+-
Mcmd/hare/subcmds.ha | 2+-
Mcmd/harec/gen.ha | 2+-
Mcmd/haredoc/html.ha | 14+++++++-------
Mcrypto/argon2/argon2.ha | 6+++---
Mencoding/base32/base32.ha | 8++++----
Mencoding/base64/base64.ha | 2+-
Mencoding/hex/hex.ha | 4++--
Mfmt/fmt.ha | 30+++++++++++++++---------------
Mstrio/ops.ha | 2+-
Mstrio/stream.ha | 8++++----
13 files changed, 59 insertions(+), 57 deletions(-)

diff --git a/bufio/buffered.ha b/bufio/buffered.ha @@ -84,7 +84,7 @@ export fn flush(s: *bufstream) (io::error | void) = { if (s.wavail == 0) { return; }; - io::write(s.source, s.wbuffer[..s.wavail])?; + io::writeall(s.source, s.wbuffer[..s.wavail])?; s.wavail = 0; return; }; @@ -240,9 +240,9 @@ fn buffered_write(s: *io::stream, buf: const []u8) (size | io::error) = { let f = buffered(&sink, [], wbuf); defer io::close(&f)!; - assert(io::write(&f, [1, 3, 3, 7]) as size == 4); + assert(io::writeall(&f, [1, 3, 3, 7]) as size == 4); assert(len(buffer(&sink)) == 0); - assert(io::write(&f, [1, 3, 3, 7]) as size == 4); + assert(io::writeall(&f, [1, 3, 3, 7]) as size == 4); assert(flush(&f) is void); assert(bytes::equal(buffer(&sink), [1, 3, 3, 7, 1, 3, 3, 7])); @@ -253,9 +253,9 @@ fn buffered_write(s: *io::stream, buf: const []u8) (size | io::error) = { let wbuf: [4]u8 = [0...]; let f = buffered(&sink, [], wbuf); - assert(io::write(&f, [1, 3, 3, 7]) as size == 4); + assert(io::writeall(&f, [1, 3, 3, 7]) as size == 4); assert(len(buffer(&sink)) == 0); - assert(io::write(&f, [1, 3, 3, 7]) as size == 4); + assert(io::writeall(&f, [1, 3, 3, 7]) as size == 4); assert(bytes::equal(buffer(&sink), [1, 3, 3, 7])); io::close(&f)!; // Should flush assert(bytes::equal(buffer(&sink), [1, 3, 3, 7, 1, 3, 3, 7])); @@ -267,8 +267,8 @@ fn buffered_write(s: *io::stream, buf: const []u8) (size | io::error) = { let wbuf: [1024]u8 = [0...]; let f = buffered(&sink, [], wbuf); - assert(io::write(&f, strings::toutf8("hello")) as size == 5); + assert(io::writeall(&f, strings::toutf8("hello")) as size == 5); assert(len(buffer(&sink)) == 0); - assert(io::write(&f, strings::toutf8(" world!\n")) as size == 8); + assert(io::writeall(&f, strings::toutf8(" world!\n")) as size == 8); assert(bytes::equal(buffer(&sink), strings::toutf8("hello world!\n"))); }; diff --git a/bufio/memstream.ha b/bufio/memstream.ha @@ -197,9 +197,9 @@ fn copy(dest: *io::stream, src: *io::stream) (io::error | size) = { @test fn dynamic() void = { let s = dynamic(io::mode::RDWR); - assert(io::write(&s, [1, 2, 3]) as size == 3); + assert(io::writeall(&s, [1, 2, 3]) as size == 3); assert(bytes::equal(buffer(&s), [1, 2, 3])); - assert(io::write(&s, [4, 5]) as size == 2); + assert(io::writeall(&s, [4, 5]) as size == 2); assert(bytes::equal(buffer(&s), [1, 2, 3, 4, 5])); let buf: [2]u8 = [0...]; assert(io::seek(&s, 0, io::whence::SET) as io::off == 0: io::off); @@ -207,19 +207,19 @@ fn copy(dest: *io::stream, src: *io::stream) (io::error | size) = { assert(io::read(&s, buf[..]) as size == 2 && bytes::equal(buf, [3, 4])); assert(io::read(&s, buf[..]) as size == 1 && buf[0] == 5); assert(io::read(&s, buf[..]) is io::EOF); - assert(io::write(&s, [6, 7, 8]) as size == 3); + assert(io::writeall(&s, [6, 7, 8]) as size == 3); assert(bytes::equal(buffer(&s), [1, 2, 3, 4, 5, 6, 7, 8])); reset(&s); assert(len(buffer(&s)) == 0); - assert(io::write(&s, [1, 2, 3]) as size == 3); + assert(io::writeall(&s, [1, 2, 3]) as size == 3); assert(truncate(&s) is void); assert(len(buffer(&s)) == 0); let sl: []u8 = alloc([1, 2, 3]); let s = dynamic_from(sl, io::mode::WRITE); - assert(io::write(&s, [0, 0]) as size == 2); + assert(io::writeall(&s, [0, 0]) as size == 2); assert(io::seek(&s, 0, io::whence::END) as io::off == 5: io::off); - assert(io::write(&s, [4, 5, 6]) as size == 3); + assert(io::writeall(&s, [4, 5, 6]) as size == 3); assert(bytes::equal(buffer(&s), [0, 0, 1, 2, 3, 4, 5, 6])); assert(io::read(&s, buf[..]) as io::error is errors::unsupported); io::close(&s)!; @@ -231,6 +231,8 @@ fn copy(dest: *io::stream, src: *io::stream) (io::error | size) = { assert(io::read(&s, buf[..]) is io::EOF); assert(io::write(&s, [1, 2]) as io::error is errors::unsupported); io::close(&s)!; + assert(io::writeall(&s, [1, 2]) as io::error is errors::unsupported); + io::close(&s)!; let in: [6]u8 = [0, 1, 2, 3, 4, 5]; let source = dynamic_from(in, io::mode::READ); @@ -245,11 +247,11 @@ fn copy(dest: *io::stream, src: *io::stream) (io::error | size) = { defer io::close(&stream)!; let n = 0z; - n += io::write(&stream, strings::toutf8("hello ")) as size; - n += io::write(&stream, strings::toutf8("world")) as size; + n += io::writeall(&stream, strings::toutf8("hello ")) as size; + n += io::writeall(&stream, strings::toutf8("world")) as size; assert(bytes::equal(buf[..n], strings::toutf8("hello world"))); assert(io::seek(&stream, 6, io::whence::SET) as io::off == 6: io::off); - io::write(&stream, strings::toutf8("asdf")) as size; + io::writeall(&stream, strings::toutf8("asdf")) as size; assert(bytes::equal(buf[..n], strings::toutf8("hello asdfd"))); let out: [2]u8 = [0...]; @@ -258,7 +260,7 @@ fn copy(dest: *io::stream, src: *io::stream) (io::error | size) = { assert(io::read(&s, out[..1]) as size == 1 && out[0] == 1); assert(io::seek(&s, 1, io::whence::CUR) as io::off == 2: io::off); assert(io::read(&s, buf[..]) is io::EOF); - assert(io::write(&s, [1, 2]) as io::error is errors::unsupported); + assert(io::writeall(&s, [1, 2]) as io::error is errors::unsupported); let in: [6]u8 = [0, 1, 2, 3, 4, 5]; let out: [6]u8 = [0...]; diff --git a/cmd/hare/plan.ha b/cmd/hare/plan.ha @@ -277,7 +277,7 @@ fn execute( progress_clear(plan); cleared = true; }; - io::write(os::stderr, buf[..n])?; + io::writeall(os::stderr, buf[..n])?; case io::EOF => break; }; diff --git a/cmd/hare/subcmds.ha b/cmd/hare/subcmds.ha @@ -180,7 +180,7 @@ fn build(args: []str) void = { fmt::fatal("{} {}: build failed", os::args[0], os::args[1]); }; if (tty::isatty(os::stdout_file)) { - io::write(os::stdout_file, ['\a'])!; + fmt::print("\a")!; }; }; diff --git a/cmd/harec/gen.ha b/cmd/harec/gen.ha @@ -90,7 +90,7 @@ fn gen_func(ctx: *context, decl: *unit::decl) void = { }; }; - io::write(ctx.out, bufio::buffer(&ctx.buf))!; + io::writeall(ctx.out, bufio::buffer(&ctx.buf))!; fmt::fprintln(ctx.out, "}\n")!; }; diff --git a/cmd/haredoc/html.ha b/cmd/haredoc/html.ha @@ -31,19 +31,19 @@ fn html_escape(out: io::handle, in: str) (size | io::error) = { match (strings::next(&iter)) { case void => break; case let rn: rune => - z += io::write(out, switch (rn) { + z += fmt::fprint(out, switch (rn) { case '&' => - yield strings::toutf8("&amp;"); + yield "&amp;"; case '<' => - yield strings::toutf8("&lt;"); + yield "&lt;"; case '>' => - yield strings::toutf8("&gt;"); + yield "&gt;"; case '"' => - yield strings::toutf8("&quot;"); + yield "&quot;"; case '\'' => - yield strings::toutf8("&apos;"); + yield "&apos;"; case => - yield utf8::encoderune(rn); + yield strings::fromutf8(utf8::encoderune(rn)); })?; }; }; diff --git a/crypto/argon2/argon2.ha b/crypto/argon2/argon2.ha @@ -344,13 +344,13 @@ fn varhash(dest: []u8, block: []u8) void = { hash::write(&h, block); hash::sum(&h, v[..]); - io::write(&destbuf, v[..32])!; + io::writeall(&destbuf, v[..32])!; for (let i = 1z; i < r; i += 1) { hash::reset(&h); hash::write(&h, v[..]); hash::sum(&h, v[..]); - io::write(&destbuf, v[..32])!; + io::writeall(&destbuf, v[..32])!; }; const remainder = len(dest) - 32 * r; @@ -358,7 +358,7 @@ fn varhash(dest: []u8, block: []u8) void = { defer hash::close(&hend); hash::write(&hend, v[..]); hash::sum(&hend, v[..remainder]); - io::write(&destbuf, v[..remainder])!; + io::writeall(&destbuf, v[..remainder])!; }; fn divceil(dividend: u32, divisor: u32) u32 = { diff --git a/encoding/base32/base32.ha b/encoding/base32/base32.ha @@ -160,7 +160,7 @@ fn encode_closer(s: *io::stream) (void | io::error) = { for (let i = 0z; i < np; i += 1) { encb[7 - i] = PADDING; }; - io::write(s.out, encb)!; // TODO https://todo.sr.ht/~sircmpwn/hare/568 + io::writeall(s.out, encb)!; }; // Encodes a byte slice in base-32, using the given encoding, returning a slice @@ -168,7 +168,7 @@ fn encode_closer(s: *io::stream) (void | io::error) = { export fn encodeslice(enc: *encoding, in: []u8) []u8 = { let out = bufio::dynamic(io::mode::WRITE); let encoder = new_encoder(enc, &out); - io::write(&encoder, in)!; + io::writeall(&encoder, in)!; io::close(&encoder)!; return bufio::buffer(&out); }; @@ -203,7 +203,7 @@ export fn encodestr(enc: *encoding, in: []u8) str = { for (let i = 0z; i <= len(in); i += 1) { let out = bufio::dynamic(io::mode::RDWR); let enc = new_encoder(&std_encoding, &out); - io::write(&enc, in[..i]) as size; + io::writeall(&enc, in[..i]) as size; io::close(&enc)!; let outb = bufio::buffer(&out); assert(bytes::equal(outb, strings::toutf8(expect[i]))); @@ -215,7 +215,7 @@ export fn encodestr(enc: *encoding, in: []u8) str = { out = bufio::dynamic(io::mode::RDWR); enc = new_encoder(&hex_encoding, &out); - io::write(&enc, in[..i]) as size; + io::writeall(&enc, in[..i]) as size; io::close(&enc)!; let outb = bufio::buffer(&out); assert(bytes::equal(outb, strings::toutf8(expect_hex[i]))); diff --git a/encoding/base64/base64.ha b/encoding/base64/base64.ha @@ -157,7 +157,7 @@ fn encode_closer(s: *io::stream) (void | io::error) = { for (let i = 0z; i < np; i += 1) { encb[3 - i] = PADDING; }; - io::write(s.out, encb)?; + io::writeall(s.out, encb)?; }; // Encodes a byte slice in base 64, using the given encoding, returning a slice diff --git a/encoding/hex/hex.ha b/encoding/hex/hex.ha @@ -21,9 +21,9 @@ export fn encode(sink: io::handle, b: []u8) (size | io::error) = { for (let i = 0z; i < len(b); i += 1) { let s = strconv::u8tosb(b[i], strconv::base::HEX_LOWER); if (len(s) == 1) { - z += io::write(sink, ['0'])?; + z += fmt::fprint(sink, "0")?; }; - z += io::write(sink, strings::toutf8(s))?; + z += fmt::fprint(sink, s)?; }; return z; }; diff --git a/fmt/fmt.ha b/fmt/fmt.ha @@ -71,7 +71,7 @@ export fn fprintfln( fmt: str, args: field... ) (io::error | size) = { - return fprintf(h, fmt, args...)? + io::write(h, ['\n'])?; + return fprintf(h, fmt, args...)? + io::writeall(h, ['\n'])?; }; // Formats values for printing using the default format modifiers and writes @@ -116,7 +116,7 @@ export fn bsprint(buf: []u8, args: formattable...) str = { // Formats values for printing using the default format modifiers and writes // them to an [[io::handle]] separated by spaces and followed by a line feed. export fn fprintln(h: io::handle, args: formattable...) (io::error | size) = { - return fprint(h, args...)? + io::write(h, ['\n'])?; + return fprint(h, args...)? + io::writeall(h, ['\n'])?; }; // Formats values for printing using the default format modifiers and writes @@ -127,7 +127,7 @@ export fn fprint(h: io::handle, args: formattable...) (io::error | size) = { for (let i = 0z; i < len(args); i += 1) { n += format(h, args[i], &mod)?; if (i != len(args) - 1) { - n += io::write(h, [' '])?; + n += io::writeall(h, [' '])?; }; }; return n; @@ -195,7 +195,7 @@ export fn fprintf( }; if (r == '{') { - n += io::write(h, utf8::encoderune('{'))?; + n += io::writeall(h, utf8::encoderune('{'))?; continue; }; @@ -269,9 +269,9 @@ export fn fprintf( assert(r == '}', "Invalid format string (hanging '}')"); }; - n += io::write(h, utf8::encoderune('}'))?; + n += io::writeall(h, utf8::encoderune('}'))?; } else { - n += io::write(h, utf8::encoderune(r))?; + n += io::writeall(h, utf8::encoderune(r))?; }; }; @@ -301,7 +301,7 @@ fn format( }; for (z < mod.width: size) { - z += io::write(out, pad)?; + z += io::writeall(out, pad)?; }; if (mod.padding != padding::ALIGN_LEFT) { @@ -318,31 +318,31 @@ fn format_raw( ) (size | io::error) = { match (arg) { case let s: str => - return io::write(out, strings::toutf8(s)); + return io::writeall(out, strings::toutf8(s)); case let r: rune => - return io::write(out, utf8::encoderune(r)); + return io::writeall(out, utf8::encoderune(r)); case let b: bool => - return io::write(out, + return io::writeall(out, strings::toutf8(if (b) "true" else "false")); case let n: types::numeric => const s = strconv::numerictosb(n, mod.base); - return io::write(out, strings::toutf8(s)); + return io::writeall(out, strings::toutf8(s)); case let p: uintptr => const s = strconv::uptrtosb(p, mod.base); - return io::write(out, strings::toutf8(s)); + return io::writeall(out, strings::toutf8(s)); case let v: nullable *void => match (v) { case let v: *void => - let n = io::write(out, strings::toutf8("0x"))?; + let n = io::writeall(out, strings::toutf8("0x"))?; const s = strconv::uptrtosb(v: uintptr, strconv::base::HEX_LOWER); - n += io::write(out, strings::toutf8(s))?; + n += io::writeall(out, strings::toutf8(s))?; return n; case null => return format(out, "(null)", mod); }; case void => - return io::write(out, strings::toutf8("void")); + return io::writeall(out, strings::toutf8("void")); }; }; diff --git a/strio/ops.ha b/strio/ops.ha @@ -83,4 +83,4 @@ export fn rjoin(out: io::handle, delim: str, strs: str...) (size | io::error) = // Appends a rune to a stream. export fn appendrune(out: io::handle, r: rune) (size | io::error) = - io::write(out, utf8::encoderune(r)); + io::writeall(out, utf8::encoderune(r)); diff --git a/strio/stream.ha b/strio/stream.ha @@ -51,8 +51,8 @@ fn fixed_write(s: *io::stream, buf: const []u8) (size | io::error) = { @test fn fixed() void = { static let buf: [1024]u8 = [0...]; let stream = fixed(buf); - io::write(&stream, strings::toutf8("hello ")) as size; - io::write(&stream, strings::toutf8("world")) as size; + io::writeall(&stream, strings::toutf8("hello ")) as size; + io::writeall(&stream, strings::toutf8("world")) as size; assert(string(&stream) == "hello world"); }; @@ -95,7 +95,7 @@ fn dynamic_close(s: *io::stream) (void | io::error) = { @test fn dynamic() void = { let stream = dynamic(); defer io::close(&stream)!; - io::write(&stream, strings::toutf8("hello ")) as size; - io::write(&stream, strings::toutf8("world")) as size; + io::writeall(&stream, strings::toutf8("hello ")) as size; + io::writeall(&stream, strings::toutf8("world")) as size; assert(string(&stream) == "hello world"); };