From ebdb51c3bc4231db793e81736c25503e488caea9 Mon Sep 17 00:00:00 2001 From: tzlil Date: Tue, 26 Sep 2023 03:37:16 +0300 Subject: cleanup --- base64.joy | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/base64.joy b/base64.joy index 0aa97b4..10827c7 100644 --- a/base64.joy +++ b/base64.joy @@ -1,29 +1,26 @@ DEFINE -shl == 2 *; -shr == 2 /; -and63 == [64 / 64 *] nullary -; +shl == 2 *; +shr == 2 /; +and63 == dup 64 / 64 * -; b64enc1 == ord 16 [shl] times [ord 8 [shl] times] dip + [ord] dip +; -b64enc2 == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" swap [] [ - [18 [shr] times and63 at] - [12 [shr] times and63 at] - [6 [shr] times and63 at] - [and63 at] - ] construct "" cons cons cons cons [pop] dip; +shrat == [shr] times and63 at; +b64enc2 == ["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" swap] [ + # [18 12 6 0 ] [[shrat] cons] map crashes + [18 shrat] + [12 shrat] + [6 shrat] + [0 shrat] + ] construct "" 4 [cons] times popd; -s2l == [null] [pop []] [uncons] [cons] linrec; -b64enc == +s2l == [null] [pop []] [uncons] [cons] linrec; +b64enc == s2l "" swap - # pad with null bytes - # calculate how many bytes to pad - [size 3 rem 3 swap - 3 rem] nullary - # i cant use null bytes in strings (hence s2l) + dup size 3 rem 3 swap - 3 rem [0 chr [] cons concat] times - # len(s)/3 times - [size 3 /] nullary - [[3 take] nullary s2l [b64enc1 b64enc2] infra first rollup [swap concat] dip 3 drop] times - # remove the now-empty string + dup size 3 / + [dup 3 take s2l [b64enc1 b64enc2] infra first rollup [swap concat] dip 3 drop] times pop - "\n" concat + "\n" concat; END stdin [feof not] [fgets b64enc putchars] while fclose. -- cgit 1.4.1