@@ -5,109 +5,109 @@ class_name UUID extends Node
5
5
const BYTE_MASK : int = 0b11111111
6
6
7
7
static func uuidbin ():
8
- randomize ()
8
+ randomize ()
9
9
# 16 random bytes with the bytes on index 6 and 8 modified
10
- return [
11
- randi () & BYTE_MASK , randi () & BYTE_MASK , randi () & BYTE_MASK , randi () & BYTE_MASK ,
12
- randi () & BYTE_MASK , randi () & BYTE_MASK , ((randi () & BYTE_MASK ) & 0x0f ) | 0x40 , randi () & BYTE_MASK ,
13
- ((randi () & BYTE_MASK ) & 0x3f ) | 0x80 , randi () & BYTE_MASK , randi () & BYTE_MASK , randi () & BYTE_MASK ,
14
- randi () & BYTE_MASK , randi () & BYTE_MASK , randi () & BYTE_MASK , randi () & BYTE_MASK ,
10
+ return [
11
+ randi () & BYTE_MASK , randi () & BYTE_MASK , randi () & BYTE_MASK , randi () & BYTE_MASK ,
12
+ randi () & BYTE_MASK , randi () & BYTE_MASK , ((randi () & BYTE_MASK ) & 0x0f ) | 0x40 , randi () & BYTE_MASK ,
13
+ ((randi () & BYTE_MASK ) & 0x3f ) | 0x80 , randi () & BYTE_MASK , randi () & BYTE_MASK , randi () & BYTE_MASK ,
14
+ randi () & BYTE_MASK , randi () & BYTE_MASK , randi () & BYTE_MASK , randi () & BYTE_MASK ,
15
15
]
16
16
17
17
static func uuidbinrng (rng : RandomNumberGenerator ):
18
- rng .randomize ()
19
- return [
20
- rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK ,
21
- rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , ((rng .randi () & BYTE_MASK ) & 0x0f ) | 0x40 , rng .randi () & BYTE_MASK ,
22
- ((rng .randi () & BYTE_MASK ) & 0x3f ) | 0x80 , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK ,
23
- rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK ,
18
+ rng .randomize ()
19
+ return [
20
+ rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK ,
21
+ rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , ((rng .randi () & BYTE_MASK ) & 0x0f ) | 0x40 , rng .randi () & BYTE_MASK ,
22
+ ((rng .randi () & BYTE_MASK ) & 0x3f ) | 0x80 , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK ,
23
+ rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK , rng .randi () & BYTE_MASK ,
24
24
]
25
25
26
26
static func v4 ():
27
27
# 16 random bytes with the bytes on index 6 and 8 modified
28
- var b = uuidbin ()
29
- return '%02x%02x%02x%02x -%02x%02x -%02x%02x -%02x%02x -%02x%02x%02x%02x%02x%02x ' % [
30
- # low
31
- b [0 ], b [1 ], b [2 ], b [3 ],
28
+ var b = uuidbin ()
29
+ return '%02x%02x%02x%02x -%02x%02x -%02x%02x -%02x%02x -%02x%02x%02x%02x%02x%02x ' % [
30
+ # low
31
+ b [0 ], b [1 ], b [2 ], b [3 ],
32
32
33
- # mid
34
- b [4 ], b [5 ],
33
+ # mid
34
+ b [4 ], b [5 ],
35
35
36
- # hi
37
- b [6 ], b [7 ],
36
+ # hi
37
+ b [6 ], b [7 ],
38
38
39
- # clock
40
- b [8 ], b [9 ],
39
+ # clock
40
+ b [8 ], b [9 ],
41
41
42
- # clock
43
- b [10 ], b [11 ], b [12 ], b [13 ], b [14 ], b [15 ]
44
- ]
42
+ # clock
43
+ b [10 ], b [11 ], b [12 ], b [13 ], b [14 ], b [15 ]
44
+ ]
45
45
46
46
static func v4_rng (rng : RandomNumberGenerator ):
47
47
# 16 random bytes with the bytes on index 6 and 8 modified
48
- var b = uuidbinrng (rng )
49
- return '%02x%02x%02x%02x -%02x%02x -%02x%02x -%02x%02x -%02x%02x%02x%02x%02x%02x ' % [
50
- # low
51
- b [0 ], b [1 ], b [2 ], b [3 ],
48
+ var b = uuidbinrng (rng )
49
+ return '%02x%02x%02x%02x -%02x%02x -%02x%02x -%02x%02x -%02x%02x%02x%02x%02x%02x ' % [
50
+ # low
51
+ b [0 ], b [1 ], b [2 ], b [3 ],
52
52
53
- # mid
54
- b [4 ], b [5 ],
53
+ # mid
54
+ b [4 ], b [5 ],
55
55
56
- # hi
57
- b [6 ], b [7 ],
56
+ # hi
57
+ b [6 ], b [7 ],
58
58
59
- # clock
60
- b [8 ], b [9 ],
59
+ # clock
60
+ b [8 ], b [9 ],
61
61
62
- # clock
63
- b [10 ], b [11 ], b [12 ], b [13 ], b [14 ], b [15 ]
64
- ]
62
+ # clock
63
+ b [10 ], b [11 ], b [12 ], b [13 ], b [14 ], b [15 ]
64
+ ]
65
65
66
66
var _uuid : Array
67
67
68
68
func _init (rng := RandomNumberGenerator .new ()) -> void :
69
- _uuid = uuidbinrng (rng )
69
+ _uuid = uuidbinrng (rng )
70
70
71
71
func as_array () -> Array :
72
- return _uuid .duplicate ()
72
+ return _uuid .duplicate ()
73
73
74
74
func as_dict (big_endian := true ) -> Dictionary :
75
- if big_endian :
76
- return {
77
- "low" : (_uuid [0 ] << 24 ) + (_uuid [1 ] << 16 ) + (_uuid [2 ] << 8 ) + _uuid [3 ],
78
- "mid" : (_uuid [4 ] << 8 ) + _uuid [5 ],
79
- "hi" : (_uuid [6 ] << 8 ) + _uuid [7 ],
80
- "clock" : (_uuid [8 ] << 8 ) + _uuid [9 ],
81
- "node" : (_uuid [10 ] << 40 ) + (_uuid [11 ] << 32 ) + (_uuid [12 ] << 24 ) + (_uuid [13 ] << 16 ) + (_uuid [14 ] << 8 ) + _uuid [15 ]
82
- }
83
- else :
84
- return {
85
- "low" : _uuid [0 ] + (_uuid [1 ] << 8 ) + (_uuid [2 ] << 16 ) + (_uuid [3 ] << 24 ),
86
- "mid" : _uuid [4 ] + (_uuid [5 ] << 8 ),
87
- "hi" : _uuid [6 ] + (_uuid [7 ] << 8 ),
88
- "clock" : _uuid [8 ] + (_uuid [9 ] << 8 ),
89
- "node" : _uuid [10 ] + (_uuid [11 ] << 8 ) + (_uuid [12 ] << 16 ) + (_uuid [13 ] << 24 ) + (_uuid [14 ] << 32 ) + (_uuid [15 ] << 40 )
90
- }
91
-
75
+ if big_endian :
76
+ return {
77
+ "low" : (_uuid [0 ] << 24 ) + (_uuid [1 ] << 16 ) + (_uuid [2 ] << 8 ) + _uuid [3 ],
78
+ "mid" : (_uuid [4 ] << 8 ) + _uuid [5 ],
79
+ "hi" : (_uuid [6 ] << 8 ) + _uuid [7 ],
80
+ "clock" : (_uuid [8 ] << 8 ) + _uuid [9 ],
81
+ "node" : (_uuid [10 ] << 40 ) + (_uuid [11 ] << 32 ) + (_uuid [12 ] << 24 ) + (_uuid [13 ] << 16 ) + (_uuid [14 ] << 8 ) + _uuid [15 ]
82
+ }
83
+ else :
84
+ return {
85
+ "low" : _uuid [0 ] + (_uuid [1 ] << 8 ) + (_uuid [2 ] << 16 ) + (_uuid [3 ] << 24 ),
86
+ "mid" : _uuid [4 ] + (_uuid [5 ] << 8 ),
87
+ "hi" : _uuid [6 ] + (_uuid [7 ] << 8 ),
88
+ "clock" : _uuid [8 ] + (_uuid [9 ] << 8 ),
89
+ "node" : _uuid [10 ] + (_uuid [11 ] << 8 ) + (_uuid [12 ] << 16 ) + (_uuid [13 ] << 24 ) + (_uuid [14 ] << 32 ) + (_uuid [15 ] << 40 )
90
+ }
91
+
92
92
func as_string () -> String :
93
- return '%02x%02x%02x%02x -%02x%02x -%02x%02x -%02x%02x -%02x%02x%02x%02x%02x%02x ' % [
94
- # low
95
- _uuid [0 ], _uuid [1 ], _uuid [2 ], _uuid [3 ],
93
+ return '%02x%02x%02x%02x -%02x%02x -%02x%02x -%02x%02x -%02x%02x%02x%02x%02x%02x ' % [
94
+ # low
95
+ _uuid [0 ], _uuid [1 ], _uuid [2 ], _uuid [3 ],
96
96
97
- # mid
98
- _uuid [4 ], _uuid [5 ],
97
+ # mid
98
+ _uuid [4 ], _uuid [5 ],
99
99
100
- # hi
101
- _uuid [6 ], _uuid [7 ],
100
+ # hi
101
+ _uuid [6 ], _uuid [7 ],
102
102
103
- # clock
104
- _uuid [8 ], _uuid [9 ],
103
+ # clock
104
+ _uuid [8 ], _uuid [9 ],
105
105
106
- # node
107
- _uuid [10 ], _uuid [11 ], _uuid [12 ], _uuid [13 ], _uuid [14 ], _uuid [15 ]
108
- ]
106
+ # node
107
+ _uuid [10 ], _uuid [11 ], _uuid [12 ], _uuid [13 ], _uuid [14 ], _uuid [15 ]
108
+ ]
109
109
110
110
func is_equal (other ) -> bool :
111
111
# Godot Engine compares Array recursively
112
112
# There's no need for custom comparison here.
113
- return _uuid == other ._uuid
113
+ return _uuid == other ._uuid
0 commit comments