hare

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

commit b8499183ddef9e9170ba4bb8e5c60b60531b5f3f
parent 78bedb21079c4c51939de6ee60adb4983ffb8ff1
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri, 26 Feb 2021 14:59:58 -0500

encoding::hex: new module

Diffstat:
Aencoding/hex/hex.ha | 25+++++++++++++++++++++++++
1 file changed, 25 insertions(+), 0 deletions(-)

diff --git a/encoding/hex/hex.ha b/encoding/hex/hex.ha @@ -0,0 +1,25 @@ +use io; +use strconv; +use strings; +use strio; + +// Encodes a byte slice to hex and writes it to a string. The caller must free +// this string. +export fn encode(b: []u8) str = { + let buf = strio::dynamic(); + for (let i = 0z; i < len(b); i += 1) { + let s = strconv::u8tosb(b[i], strconv::base::HEX_LOWER); + if (len(s) == 1) { + io::write(buf, ['0': u32: u8]); + }; + io::write(buf, strings::to_utf8(s)) as size; + }; + return strio::finish(buf); +}; + +@test fn encode() void = { + let in: [_]u8 = [0xCA, 0xFE, 0xBA, 0xBE, 0xDE, 0xAD, 0xF0, 0x0D]; + let s = encode(in); + defer free(s); + assert(s == "cafebabedeadf00d"); +};