Current section
Files
Jump to
Current section
Files
src/typeid@uuid.erl
-module(typeid@uuid).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/typeid/uuid.gleam").
-export([to_string/1, v7/0, from_string/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-file("src/typeid/uuid.gleam", 28).
?DOC(false).
-spec do_to_string(bitstring(), integer(), binary()) -> binary().
do_to_string(Ints, Position, Acc) ->
case Position of
8 ->
do_to_string(Ints, Position + 1, <<Acc/binary, "-"/utf8>>);
13 ->
do_to_string(Ints, Position + 1, <<Acc/binary, "-"/utf8>>);
18 ->
do_to_string(Ints, Position + 1, <<Acc/binary, "-"/utf8>>);
23 ->
do_to_string(Ints, Position + 1, <<Acc/binary, "-"/utf8>>);
_ ->
case Ints of
<<I:4, Rest/bitstring>> ->
String = begin
_pipe = gleam@int:to_base16(I),
string:lowercase(_pipe)
end,
do_to_string(
Rest,
Position + 1,
<<Acc/binary, String/binary>>
);
_ ->
Acc
end
end.
-file("src/typeid/uuid.gleam", 20).
?DOC(false).
-spec to_string(bitstring()) -> binary().
to_string(Uuid) ->
do_to_string(Uuid, 0, <<""/utf8>>).
-file("src/typeid/uuid.gleam", 9).
?DOC(false).
-spec v7() -> binary().
v7() ->
{Sec, Ns} = begin
_pipe = gleam@time@timestamp:system_time(),
gleam@time@timestamp:to_unix_seconds_and_nanoseconds(_pipe)
end,
Stamp = (Sec * 1000) + (Ns div 1000000),
{A@1, B@1} = case crypto:strong_rand_bytes(10) of
<<A:12, B:62, _:6>> -> {A, B};
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"typeid/uuid"/utf8>>,
function => <<"v7"/utf8>>,
line => 14,
value => _assert_fail,
start => 389,
'end' => 463,
pattern_start => 400,
pattern_end => 437})
end,
Value = <<Stamp:48, 7:4, A@1:12, 2:2, B@1:62>>,
to_string(Value).
-file("src/typeid/uuid.gleam", 59).
?DOC(false).
-spec hex_to_int(binary()) -> {ok, integer()} | {error, nil}.
hex_to_int(C) ->
I = case C of
<<"0"/utf8>> ->
0;
<<"1"/utf8>> ->
1;
<<"2"/utf8>> ->
2;
<<"3"/utf8>> ->
3;
<<"4"/utf8>> ->
4;
<<"5"/utf8>> ->
5;
<<"6"/utf8>> ->
6;
<<"7"/utf8>> ->
7;
<<"8"/utf8>> ->
8;
<<"9"/utf8>> ->
9;
<<"a"/utf8>> ->
10;
<<"A"/utf8>> ->
10;
<<"b"/utf8>> ->
11;
<<"B"/utf8>> ->
11;
<<"c"/utf8>> ->
12;
<<"C"/utf8>> ->
12;
<<"d"/utf8>> ->
13;
<<"D"/utf8>> ->
13;
<<"e"/utf8>> ->
14;
<<"E"/utf8>> ->
14;
<<"f"/utf8>> ->
15;
<<"F"/utf8>> ->
15;
_ ->
16
end,
case I of
16 ->
{error, nil};
X ->
{ok, X}
end.
-file("src/typeid/uuid.gleam", 42).
?DOC(false).
-spec do_to_bit_array(binary(), integer(), bitstring()) -> {ok, bitstring()} |
{error, nil}.
do_to_bit_array(Str, Index, Acc) ->
case gleam_stdlib:string_pop_grapheme(Str) of
{error, nil} when Index =:= 32 ->
{ok, Acc};
{ok, {<<"-"/utf8>>, Rest}} when Index < 32 ->
do_to_bit_array(Rest, Index, Acc);
{ok, {C, Rest@1}} when Index < 32 ->
case hex_to_int(C) of
{ok, I} ->
do_to_bit_array(Rest@1, Index + 1, <<Acc/bitstring, I:4>>);
{error, _} ->
{error, nil}
end;
_ ->
{error, nil}
end.
-file("src/typeid/uuid.gleam", 24).
?DOC(false).
-spec from_string(binary()) -> {ok, bitstring()} | {error, nil}.
from_string(Str) ->
do_to_bit_array(Str, 0, <<>>).