Current section

23 Versions

Jump to

Compare versions

5 files changed
+90 additions
-42 deletions
  @@ -5,7 +5,7 @@
5 5 [<<"LICENSE">>,<<"README.md">>,<<"include/mnesia-macros.lfe">>,
6 6 <<"rebar.config">>,<<"rebar.lock">>,<<"src/lutil-file.lfe">>,
7 7 <<"src/lutil-math.lfe">>,<<"src/lutil-text.lfe">>,<<"src/lutil-type.lfe">>,
8 - <<"src/lutil.app.src">>,<<"src/lutil.lfe">>]}.
8 + <<"src/lutil-uuid.lfe">>,<<"src/lutil.app.src">>,<<"src/lutil.lfe">>]}.
9 9 {<<"licenses">>,[<<"BSD-3">>]}.
10 10 {<<"links">>,
11 11 [{<<"GitHub">>,<<"https://github.com/lfex/lutil">>},
  @@ -16,4 +16,4 @@
16 16 [{<<"app">>,<<"lfe">>},
17 17 {<<"optional">>,false},
18 18 {<<"requirement">>,<<"2.0.1">>}]}]}.
19 - {<<"version">>,<<"0.12.2">>}.
19 + {<<"version">>,<<"0.13.0">>}.
  @@ -3,7 +3,7 @@
3 3 ]}.
4 4
5 5 {plugins, [
6 - {rebar3_lfe, "0.3.1"}
6 + {rebar3_lfe, "0.3.2"}
7 7 ]}.
8 8
9 9 {provider_hooks, [
  @@ -25,7 +25,7 @@
25 25 {test, [
26 26 {deps, [
27 27 {proper, "1.3.0"},
28 - {ltest, "0.13.0"}
28 + {ltest, "0.13.1"}
29 29 ]},
30 30 {plugins, [
31 31 {rebar3_proper, "0.12.1"}
  @@ -0,0 +1,82 @@
1 + ;;;; Adapted from the implementations given here:
2 + ;;;; * https://github.com/okeuday/uuid/blob/7a0824a4158b5a5122ae660f59e7eac5e9138505/src/uuid.erl#L628
3 + ;;;; * https://github.com/afiskon/erlang-uuid-v4/blob/8c03a11524f6bccf984575877b533ef30a009175/src/uuid.erl"
4 + (defmodule lutil-uuid
5 + (export all))
6 +
7 + (defun v4 ()
8 + ;; XXX rand:bytes is Erlang 24+ only; when that's the oldest version we
9 + ;; support, we can turn this back on
10 + ;;(v4 (rand:bytes 16)))
11 + (v4 (crypto:strong_rand_bytes 16)))
12 +
13 + (defun v4
14 + ((type) (when (== type 'strong))
15 + (v4 (crypto:strong_rand_bytes 16)))
16 + ((type) (when (is_atom type))
17 + ;; XXX rand:bytes is Erlang 24+ only; when that's the oldest version we
18 + ;; support, we can turn this back on
19 + ;;(v4 (rand:bytes 16)))
20 + (v4 (crypto:strong_rand_bytes 16)))
21 + ((in-bytes)
22 + (let (((binary (r1 (size 48))
23 + (_ (size 4))
24 + (r2 (size 12))
25 + (_ (size 2))
26 + (r3 (size 62))) in-bytes))
27 + (binary (r1 (size 48))
28 + (0 (size 1))
29 + (1 (size 1))
30 + (0 (size 1))
31 + (0 (size 1))
32 + (r2 (size 12))
33 + (1 (size 1))
34 + (0 (size 1))
35 + (r3 (size 62))))))
36 +
37 + (defun v4->str (uuid-bytes)
38 + (let* (((binary
39 + (p1 (size 32))
40 + (p2 (size 16))
41 + (p3 (size 16))
42 + (p4 (size 16))
43 + (p5 (size 48))) uuid-bytes)
44 + (segmented (list p1 p2 (band p3 #x0fff) (band p4 #x3fff) (bor #x8000 p5)))
45 + (format-template "~8.16.0b-~4.16.0b-4~3.16.0b-~4.16.0b-~12.16.0b"))
46 + (io_lib:format format-template segmented)))
47 +
48 + (defun four ()
49 + (four #(type bitstring)))
50 +
51 + (defun four
52 + "A wrapper for uuid4/0."
53 + ;; Example usage:
54 + ;;
55 + ;; > (lutil-uuid:four (tuple 'type 'binary))
56 + ;; #B(50 101 51 53 49 99 48 97 45 50 100 100 52 45 52 54 56 55 45 50 ...)
57 + ;;
58 + ;; > (lutil-uuid:four (tuple 'type 'list))
59 + ;; "65c0aff3-421e-40bf-0f64-3ac0d1e0b72d"
60 + ;;
61 + ;; > (lutil-uuid:four (tuple 'type 'atom))
62 + ;; '
63 + ((`#(type binary))
64 + (v4))
65 + ((`#(type ,type binary))
66 + (v4 type))
67 + ((`#(type bitstring))
68 + (list_to_binary (v4->str (v4))))
69 + ((`#(type ,type bitstring))
70 + (list_to_binary (v4->str (v4 type))))
71 + ((`#(type list))
72 + (v4->str (v4)))
73 + ((`#(type string))
74 + (v4->str (v4)))
75 + ((`#(type ,type list))
76 + (v4->str (v4 type)))
77 + ((`#(type ,type string))
78 + (v4->str (v4 type)))
79 + ((`#(type atom))
80 + (list_to_atom (v4->str (v4))))
81 + ((`#(type ,type atom))
82 + (list_to_atom (v4->str (v4 type)))))
  @@ -1,6 +1,6 @@
1 1 {application,lutil,
2 2 [{description,"LFE Utility Functions and Macros"},
3 - {vsn,"0.12.2"},
3 + {vsn,"0.13.0"},
4 4 {modules,[lutil]},
5 5 {registered,[]},
6 6 {applications,[kernel,stdlib,crypto]},
  @@ -4,38 +4,10 @@
4 4 (export all))
5 5
6 6 (defun uuid4 ()
7 - "Adapted from the implementation given here:
8 - https://github.com/afiskon/erlang-uuid-v4/blob/8c03a11524f6bccf984575877b533ef30a009175/src/uuid.erl
9 - "
10 - (let* (((binary
11 - (a (size 32))
12 - (b (size 16))
13 - (c (size 16))
14 - (d (size 16))
15 - (e (size 48))) (crypto:strong_rand_bytes 16))
16 - (format-template '"~8.16.0b-~4.16.0b-4~3.16.0b-~4.16.0b-~12.16.0b")
17 - (uuid-data (list a b (band c #x0fff) (band d #x3fff) (bor #x8000 e)))
18 - (string (io_lib:format format-template uuid-data)))
19 - (list_to_binary string)))
7 + (lutil-uuid:four #(type strong bitstring)))
20 8
21 - (defun uuid4
22 - "A wrapper for uuid4/0."
23 - ;; Example usage:
24 - ;;
25 - ;; > (lutil:uuid4 (tuple 'type 'binary))
26 - ;; #B(50 101 51 53 49 99 48 97 45 50 100 100 52 45 52 54 56 55 45 50 ...)
27 - ;;
28 - ;; > (lutil:uuid4 (tuple 'type 'list))
29 - ;; "65c0aff3-421e-40bf-0f64-3ac0d1e0b72d"
30 - ;;
31 - ;; > (lutil:uuid4 (tuple 'type 'atom))
32 - ;; '
33 - (((tuple 'type 'binary))
34 - (uuid4))
35 - (((tuple 'type 'list))
36 - (binary_to_list (uuid4)))
37 - (((tuple 'type 'atom))
38 - (binary_to_atom (uuid4) 'latin1)))
9 + (defun uuid4 (opts)
10 + (lutil-uuid:four opts))
39 11
40 12 (defun version ()
41 13 (version 'lutil))
  @@ -77,9 +49,3 @@
77 49 (lambda (x)
78 50 (element 1 x))
79 51 (element 3 env))))
80 -
81 - (defun shuffle (items)
82 - (list-comp ((<- `#(,_ ,n) (lists:sort
83 - (list-comp ((<- m items))
84 - `#(,(rand:uniform) ,m)))))
85 - n))