hare

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

commit 540bea11afb0e3036513cf47fb75262ab717e464
parent 232209ea43050f1056f043df0eec19a81f1b4885
Author: Ember Sawady <ecs@d2evs.net>
Date:   Tue, 26 Sep 2023 07:36:41 +0000

encoding::base{32,64}+test: make use of io::drain

Signed-off-by: Ember Sawady <ecs@d2evs.net>

Diffstat:
Mencoding/base32/base32.ha | 22++--------------------
Mencoding/base64/base64.ha | 22++--------------------
2 files changed, 4 insertions(+), 40 deletions(-)

diff --git a/encoding/base32/base32.ha b/encoding/base32/base32.ha @@ -397,17 +397,8 @@ export fn decodestr(enc: *encoding, in: str) ([]u8 | errors::invalid) = { for (let i = 0z; i < len(cases); i += 1) { let in = memio::fixed(strings::toutf8(cases[i].0)); let dec = newdecoder(cases[i].2, &in); - let buf: [1]u8 = [0]; - let out: []u8 = []; + let out: []u8 = io::drain(&dec)!; defer free(out); - for (true) match (io::read(&dec, buf)!) { - case let z: size => - if (z > 0) { - append(out, buf[0]); - }; - case io::EOF => - break; - }; assert(bytes::equal(out, strings::toutf8(cases[i].1))); // Testing decodestr should cover decodeslice too @@ -419,17 +410,8 @@ export fn decodestr(enc: *encoding, in: str) ([]u8 | errors::invalid) = { for (let i = 0z; i < len(cases); i += 1) { let in = memio::fixed(strings::toutf8(cases[i].0)); let dec = newdecoder(cases[i].2, &in); - let buf: [1024]u8 = [0...]; - let out: []u8 = []; + let out: []u8 = io::drain(&dec)!; defer free(out); - for (true) match (io::read(&dec, buf)!) { - case let z: size => - if (z > 0) { - append(out, buf[..z]...); - }; - case io::EOF => - break; - }; assert(bytes::equal(out, strings::toutf8(cases[i].1))); }; diff --git a/encoding/base64/base64.ha b/encoding/base64/base64.ha @@ -472,17 +472,8 @@ export fn decode( for (let i = 0z; i < len(cases); i += 1) { let in = memio::fixed(strings::toutf8(cases[i].0)); let decoder = newdecoder(cases[i].2, &in); - let buf: [1]u8 = [0]; - let decb: []u8 = []; + let decb: []u8 = io::drain(&decoder)!; defer free(decb); - for (true) match (io::read(&decoder, buf)!) { - case let z: size => - if (z > 0) { - append(decb, buf[0]); - }; - case io::EOF => - break; - }; assert(bytes::equal(decb, strings::toutf8(cases[i].1))); // Testing decodestr should cover decodeslice too @@ -494,17 +485,8 @@ export fn decode( for (let i = 0z; i < len(cases); i += 1) { let in = memio::fixed(strings::toutf8(cases[i].0)); let decoder = newdecoder(cases[i].2, &in); - let buf: [1024]u8 = [0...]; - let decb: []u8 = []; + let decb: []u8 = io::drain(&decoder)!; defer free(decb); - for (true) match (io::read(&decoder, buf)!) { - case let z: size => - if (z > 0) { - append(decb, buf[..z]...); - }; - case io::EOF => - break; - }; assert(bytes::equal(decb, strings::toutf8(cases[i].1))); };