Add a base64 encoder to std

This commit is contained in:
darkf 2017-06-01 21:31:33 +00:00
parent 1217fe951f
commit 1ea120a387
1 changed files with 28 additions and 0 deletions

28
mods/std/base64.lamb Normal file
View File

@ -0,0 +1,28 @@
import("std/list").
import("std/str").
charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".
_b64(n) ->
list\map(\shift -> list\at(charset, (n >> shift) & 63),
[18, 12, 6, 0]).
f([]) -> [].
f(a :: b :: c :: xs) -> do
v = (a << 16) | (b << 8) | c;
_b64(v) + f(xs)
end.
f(a :: b :: []) -> do
v = (a << 16) | (b << 8);
list\take(3, _b64(v)) + ["="]
end.
f(a :: []) -> do
v = a << 16;
list\take(2, _b64(v)) + ["=="]
end.
base64_encode(s) -> do
bytes = list\map(ord, s);
str\concat(f(bytes))
end.