/
UUID.hx
139 lines (121 loc) · 4.24 KB
/
UUID.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*!
Math.uuid.js (v1.4)
http://www.broofa.com
mailto:robert@broofa.com
Copyright (c) 2010 Robert Kieffer
Haxe conversion by Richard Janicek
Dual licensed under the MIT and GPL licenses.
*/
package co.janicek.core.math;
using co.janicek.core.math.RandomCore;
using co.janicek.core.NullCore;
using co.janicek.core.math.MathCore;
using Std;
using EReg;
using StringTools;
/**
* Random universally unique identifier (UUID) generators.
* @see <a href="http://en.wikipedia.org/wiki/Uuid">http://en.wikipedia.org/wiki/Uuid</a>
*/
class UUID {
private static var CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");
/**
* Generate a UUID.
*
* EXAMPLES:
*
* One argument - returns ID of the specified length
* >>> uuid(15) // 15 character ID (default base=62)
* "VcydxgltxrVZSTV"
*
* Two arguments - returns ID of the specified length, and radix. (Radix must be <= 62)
* >>> uuid(8, 2) // 8 character ID (base=2)
* "01001010"
* >>> uuid(8, 10) // 8 character ID (base=10)
* "47473046"
* >>> uuid(8, 16) // 8 character ID (base=16)
* "098F4D35"
*
* @param length the desired number of characters
* @param radix the number of allowable values for each character, clamped to [2,62], default = 62
* @param seed optional PRNG seed
*/
public static function uuid( length : Int, ?radix : Int, ?seed : Int ) {
if (seed.isNull()) seed = RandomCore.makeRandomSeed();
var chars = CHARS, uuid = [], i;
radix = radix.coalesce(chars.length).clampInt(2, chars.length);
for (i in 0...length) uuid[i] = chars[0 | ((seed = seed.nextParkMiller()).toFloat() * radix).int()];
return uuid.join("");
}
/**
* Generate a UUID using specific characters.
* @param length the desired number of characters
* @param ?characters optional desired characters to use to build the UUID
* @param ?seed optional PRNG seed
*/
public static function uuidChars( length : Int, ?characters : String, ?seed : Int ) {
if (seed.isNull())
seed = RandomCore.makeRandomSeed();
var chars = characters.isNull() ? CHARS : characters.split("");
if (chars.length < 2)
throw "must have at least 2 characters";
var uuid = [];
var radix = chars.length;
for (i in 0...length) uuid[i] = chars[0 | ((seed = seed.nextParkMiller()).toFloat() * radix).int()];
return uuid.join("");
}
/**
* generate RFC4122, version 4 ID
* example "92329D39-6F5C-4520-ABFC-AAB64544E172"
* @param seed optional PRNG seed
*/
public static function uuidRfc4122V4( ?seed : Int ) {
if (seed.isNull()) seed = RandomCore.makeRandomSeed();
var chars = CHARS, uuid = [], i;
// rfc4122, version 4 form
var r : Int;
// rfc4122 requires these characters
uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
uuid[14] = "4";
// Fill in random data. At i==19 set the high bits of clock sequence as
// per rfc4122, sec. 4.1.5
for (i in 0...36) {
if (uuid[i].isNull()) {
r = 0 | ((seed = seed.nextParkMiller()).toFloat() * 16).int();
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
return uuid.join("");
}
/**
* A more performant, but slightly bulkier, RFC4122v4 solution. We boost performance
* by minimizing calls to random()
*/
public static function uuidFast( ?seed : Int ) {
if (seed.isNull()) seed = RandomCore.makeRandomSeed();
var chars = CHARS, uuid = new Array(), rnd=0, r;
for (i in 0...36) {
if (i==8 || i==13 || i==18 || i==23) {
uuid[i] = "-";
} else if (i==14) {
uuid[i] = "4";
} else {
if (rnd <= 0x02) rnd = 0x2000000 + ((seed = seed.nextParkMiller()).toFloat() * 0x1000000).int() | 0;
r = rnd & 0xf;
rnd = rnd >> 4;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
return uuid.join("");
}
/**
* A more compact, but less performant, RFC4122v4 solution:
*/
public static function uuidCompact( ?seed : Int ) {
if (seed.isNull()) seed = RandomCore.makeRandomSeed();
return new EReg("[xy]", "g").map("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx", function (c) {
var r = ((seed = seed.nextParkMiller()).toFloat() * 16).int() | 0, v = c.matched(0) == "x" ? r : (r & 0x3 | 0x8);
return v.hex();
});
}
}