Packages

Nimiq Address implementation for Gleam

Current section

Files

Jump to
nimiq_address src nimiq@base32.erl
Raw

src/nimiq@base32.erl

-module(nimiq@base32).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/nimiq/base32.gleam").
-export([encode/2, decode/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/nimiq/base32.gleam", 12).
-spec do_encode(bitstring(), binary(), binary()) -> binary().
do_encode(Buf, Alphabet, Acc) ->
{Idx, Rest@5} = case Buf of
<<Char:5, Rest/bitstring>> ->
{Char, Rest};
<<Char@1:4, Rest@1/bitstring>> ->
{erlang:'bsl'(Char@1, 1), Rest@1};
<<Char@2:3, Rest@2/bitstring>> ->
{erlang:'bsl'(Char@2, 2), Rest@2};
<<Char@3:2, Rest@3/bitstring>> ->
{erlang:'bsl'(Char@3, 3), Rest@3};
<<Char@4:1, Rest@4/bitstring>> ->
{erlang:'bsl'(Char@4, 4), Rest@4};
_ ->
erlang:error(#{gleam_error => panic,
message => <<"One recursion too many"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"nimiq/base32"/utf8>>,
function => <<"do_encode"/utf8>>,
line => 23})
end,
Char@5 = gleam@string:slice(Alphabet, Idx, 1),
case Rest@5 of
<<>> ->
<<Acc/binary, Char@5/binary>>;
_ ->
do_encode(Rest@5, Alphabet, <<Acc/binary, Char@5/binary>>)
end.
-file("src/nimiq/base32.gleam", 8).
?DOC(" Encodes a BitArray into a base32 string using the provided alphabet.\n").
-spec encode(bitstring(), binary()) -> binary().
encode(Buf, Alphabet) ->
do_encode(Buf, Alphabet, <<""/utf8>>).
-file("src/nimiq/base32.gleam", 89).
-spec do_index_of(list(DLC), DLC, integer()) -> gleam@option:option(integer()).
do_index_of(List, Elem, Index) ->
case List of
[] ->
none;
[Head | _] when Head =:= Elem ->
{some, Index};
[_ | Tail] ->
do_index_of(Tail, Elem, Index + 1)
end.
-file("src/nimiq/base32.gleam", 85).
-spec index_of(list(DKZ), DKZ) -> gleam@option:option(integer()).
index_of(List, Elem) ->
do_index_of(List, Elem, 0).
-file("src/nimiq/base32.gleam", 52).
-spec do_decode(list(binary()), list(binary()), integer(), bitstring()) -> {ok,
bitstring()} |
{error, binary()}.
do_decode(Chars, Alphabet, Bit_length, Acc) ->
Next_bit_length = Bit_length + 5,
case Chars of
[] ->
{ok, Acc};
[Char | Rest] ->
Idx = index_of(Alphabet, Char),
case Idx of
{some, I} ->
do_decode(
Rest,
Alphabet,
Next_bit_length,
gleam@bit_array:append(Acc, <<I:5>>)
);
none ->
{error, <<"Missing character in alphabet"/utf8>>}
end
end.
-file("src/nimiq/base32.gleam", 38).
?DOC(" Decodes a base32 string into a BitArray using the provided alphabet.\n").
-spec decode(binary(), binary()) -> {ok, bitstring()} | {error, binary()}.
decode(Str, Alphabet) ->
Decoded = begin
_pipe = Str,
_pipe@1 = gleam@string:trim(_pipe),
_pipe@2 = gleam@string:to_graphemes(_pipe@1),
_pipe@3 = gleam@list:filter(_pipe@2, fun(X) -> X /= <<"="/utf8>> end),
do_decode(_pipe@3, gleam@string:to_graphemes(Alphabet), 0, <<>>)
end,
case Decoded of
{ok, Bytes} ->
{ok, Bytes};
{error, Msg} ->
{error, Msg}
end.