Skip to content

Commit e8defdf

Browse files
committed
Round numbers, add to_binary function, add comments
Integrate a patch from Jon Kristensen: "We rounded the number to remove an error, added a function to_binary/1 which converted a UUID string to binary and commented your functions a little bit."
1 parent 8f39b27 commit e8defdf

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

uuid.erl

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
% Copyright (c) 2008, Travis Vachon
22
% All rights reserved.
3-
%
3+
%
44
% Redistribution and use in source and binary forms, with or without
55
% modification, are permitted provided that the following conditions are
66
% met:
7-
%
7+
%
88
% * Redistributions of source code must retain the above copyright
99
% notice, this list of conditions and the following disclaimer.
10-
%
10+
%
1111
% * Redistributions in binary form must reproduce the above copyright
1212
% notice, this list of conditions and the following disclaimer in the
1313
% documentation and/or other materials provided with the distribution.
14-
%
14+
%
1515
% * Neither the name of the author nor the names of its contributors
1616
% may be used to endorse or promote products derived from this
1717
% software without specific prior written permission.
18-
%
18+
%
1919
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2020
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2121
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -27,17 +27,33 @@
2727
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2828
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2929
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30-
%
30+
%
3131
-module(uuid).
32-
-export([v4/0, to_string/1, get_parts/1]).
32+
-export([v4/0, to_string/1, get_parts/1, to_binary/1]).
3333
-import(random).
3434

35+
% Generates a random binary UUID.
3536
v4() ->
36-
v4(random:uniform(math:pow(2, 48)) - 1, random:uniform(math:pow(2, 12)) - 1, random:uniform(math:pow(2, 32)) - 1, random:uniform(math:pow(2, 30)) - 1).
37+
v4(random:uniform(round(math:pow(2, 48))) - 1, random:uniform(round(math:pow(2, 12))) - 1, random:uniform(round(math:pow(2, 32))) - 1, random:uniform(round(math:pow(2, 30))) - 1).
3738
v4(R1, R2, R3, R4) ->
3839
<<R1:48, 4:4, R2:12, 2:2, R3:32, R4: 30>>.
40+
41+
% Returns a string representation of a binary UUID.
3942
to_string(U) ->
4043
lists:flatten(io_lib:format("~8.16.0b-~4.16.0b-~4.16.0b-~2.16.0b~2.16.0b-~12.16.0b", get_parts(U))).
4144

45+
% Returns the 32, 16, 16, 8, 8, 48 parts of a binary UUID.
4246
get_parts(<<TL:32, TM:16, THV:16, CSR:8, CSL:8, N:48>>) ->
4347
[TL, TM, THV, CSR, CSL, N].
48+
49+
% Converts a UUID string in the format of 550e8400-e29b-41d4-a716-446655440000
50+
% (with or without the dashes) to binary.
51+
to_binary(U)->
52+
convert(lists:filter(fun(Elem) -> Elem /= $- end, U), []).
53+
54+
% Converts a list of pairs of hex characters (00-ff) to bytes.
55+
convert([], Acc)->
56+
list_to_binary(lists:reverse(Acc));
57+
convert([X, Y | Tail], Acc)->
58+
{ok, [Byte], _} = io_lib:fread("~16u", [X, Y]),
59+
convert(Tail, [Byte | Acc]).

0 commit comments

Comments
 (0)