|
1 | 1 | % Copyright (c) 2008, Travis Vachon
|
2 | 2 | % All rights reserved.
|
3 |
| -% |
| 3 | +% |
4 | 4 | % Redistribution and use in source and binary forms, with or without
|
5 | 5 | % modification, are permitted provided that the following conditions are
|
6 | 6 | % met:
|
7 |
| -% |
| 7 | +% |
8 | 8 | % * Redistributions of source code must retain the above copyright
|
9 | 9 | % notice, this list of conditions and the following disclaimer.
|
10 |
| -% |
| 10 | +% |
11 | 11 | % * Redistributions in binary form must reproduce the above copyright
|
12 | 12 | % notice, this list of conditions and the following disclaimer in the
|
13 | 13 | % documentation and/or other materials provided with the distribution.
|
14 |
| -% |
| 14 | +% |
15 | 15 | % * Neither the name of the author nor the names of its contributors
|
16 | 16 | % may be used to endorse or promote products derived from this
|
17 | 17 | % software without specific prior written permission.
|
18 |
| -% |
| 18 | +% |
19 | 19 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20 | 20 | % "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21 | 21 | % LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
27 | 27 | % LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
28 | 28 | % NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
29 | 29 | % SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30 |
| -% |
| 30 | +% |
31 | 31 | -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]). |
33 | 33 | -import(random).
|
34 | 34 |
|
| 35 | +% Generates a random binary UUID. |
35 | 36 | 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). |
37 | 38 | v4(R1, R2, R3, R4) ->
|
38 | 39 | <<R1:48, 4:4, R2:12, 2:2, R3:32, R4: 30>>.
|
| 40 | + |
| 41 | +% Returns a string representation of a binary UUID. |
39 | 42 | to_string(U) ->
|
40 | 43 | 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))).
|
41 | 44 |
|
| 45 | +% Returns the 32, 16, 16, 8, 8, 48 parts of a binary UUID. |
42 | 46 | get_parts(<<TL:32, TM:16, THV:16, CSR:8, CSL:8, N:48>>) ->
|
43 | 47 | [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