hare

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

commit fd7addb2076da0cbf82a4f3530183e5fa6b026cb
parent f86268c19270790d619b9c9f033d2ea2affaa083
Author: Sebastian <sebastian@sebsite.pw>
Date:   Mon, 27 Nov 2023 01:19:04 -0500

test: hexdump non-printable ASCII

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

Diffstat:
Mtest/+test.ha | 42++++++++++++++++++++++++------------------
1 file changed, 24 insertions(+), 18 deletions(-)

diff --git a/test/+test.ha b/test/+test.ha @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MPL-2.0 // (c) Hare authors <https://harelang.org> +use ascii; use bufio; use encoding::hex; use encoding::utf8; @@ -213,24 +214,8 @@ fn do_test(ctx: *context, test: test) void = { time_diff / 1000000000, time_diff % 1000000000)!; - const stdout = memio::buffer(&ctx.stdout); - const stdout = match (strings::fromutf8(stdout)) { - case let s: str => - yield strings::dup(s); - case utf8::invalid => - let s = memio::dynamic(); - hex::dump(&s, stdout)!; - yield memio::string(&s)!; - }; - const stderr = memio::buffer(&ctx.stderr); - const stderr = match (strings::fromutf8(stderr)) { - case let s: str => - yield strings::dup(s); - case utf8::invalid => - let s = memio::dynamic(); - hex::dump(&s, stderr)!; - yield memio::string(&s)!; - }; + const stdout = printable(memio::buffer(&ctx.stdout)); + const stderr = printable(memio::buffer(&ctx.stderr)); if (failed && (stdout != "" || stderr != "")) { append(ctx.output, output { test = test.name, @@ -263,6 +248,27 @@ fn run_test(ctx: *context, test: test) status = { return n; }; +fn printable(buf: []u8) str = { + match (strings::fromutf8(buf)) { + case let s: str => + let it = strings::iter(s); + for (true) match (strings::next(&it)) { + case void => + return strings::dup(s); + case let r: rune => + if (ascii::valid(r) && !ascii::isprint(r) + && r != '\t' && r != '\n') { + break; + }; + }; + case utf8::invalid => void; + }; + + let s = memio::dynamic(); + hex::dump(&s, buf)!; + return memio::string(&s)!; +}; + fn dots(n: size) void = { for (let i = 0z; i < n; i += 1) { fmt::print(".")!;