commit 7b323f92e632e7c9bc8c5cd6e3b14a2b570ebc37
parent 7073df590dd57ec89312ffb9425aff22956bf78f
Author: Edin Taric <unicorn@regrow.earth>
Date: Wed, 11 May 2022 21:39:57 +0200
encoding::hex::encode: Re-added and wrote test
Signed-off-by: Edin Taric <unicorn@regrow.earth>
Diffstat:
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/encoding/hex/hex.ha b/encoding/hex/hex.ha
@@ -76,13 +76,30 @@ export fn encodestr(in: []u8) str = {
return strio::string(&out);
};
-@test fn encode() void = {
+@test fn encodestr() void = {
let in: [_]u8 = [0xCA, 0xFE, 0xBA, 0xBE, 0xDE, 0xAD, 0xF0, 0x0D];
let s = encodestr(in);
defer free(s);
assert(s == "cafebabedeadf00d");
};
+// Encodes a byte slice as a hexadecimal string and writes it to an
+// [[io::handle]].
+export fn encode(out: io::handle, in: []u8) (size | io::error) = {
+ const enc = newencoder(out);
+ return io::writeall(&enc, in);
+};
+
+@test fn encode() void = {
+ const in: [_]u8 = [0xCA, 0xFE, 0xBA, 0xBE, 0xDE, 0xAD, 0xF0, 0x0D];
+
+ let out = strio::dynamic();
+ defer io::close(&out)!;
+
+ encode(&out, in)!;
+ assert(strio::string(&out) == "cafebabedeadf00d");
+};
+
export type decoder = struct {
stream: io::stream,
in: io::handle,