-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUUIDGenerator.xojo_code
More file actions
139 lines (117 loc) · 3.21 KB
/
UUIDGenerator.xojo_code
File metadata and controls
139 lines (117 loc) · 3.21 KB
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
#tag Class
Protected Class UUIDGenerator
#tag Method, Flags = &h21
Private Function ByteToHex(bytes() As UInt8, start As Integer, count As Integer) As String
// Convert a subarray of bytes into a hex string
Var result As String
For i As Integer = start To start + count - 1
result = result + bytes(i).ToHex(2)
Next
Return result
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function EncodeBase64(bytes() As UInt8) As String
// Encode the UUID bytes in Base64
Var mb As New MemoryBlock(bytes.Count)
For i As Integer = 0 To bytes.LastIndex
mb.UInt8Value(i) = bytes(i)
Next
Return EncodeBase64(mb)
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function FormatUUIDHex(bytes() As UInt8, includeHyphens As Boolean) As String
// Convert the 16 bytes to hexadecimal UUID format, with or without hyphens
Var parts() As String
parts.Add(ByteToHex(bytes, 0, 4))
parts.Add(ByteToHex(bytes, 4, 2))
parts.Add(ByteToHex(bytes, 6, 2))
parts.Add(ByteToHex(bytes, 8, 2))
parts.Add(ByteToHex(bytes, 10, 6))
If includeHyphens Then
Return Join( parts,"-")
Else
Return Join( parts,"")
End If
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function GenerateRandomUUIDBytes() As UInt8()
// Generates an array of 16 bytes UUID v4 with compliant version and variant bits
Var bytes() As UInt8
Var rnd As New Random
For i As Integer = 0 To 15
bytes.Add(rnd.InRange(0, 255))
Next
// Set version (4)
bytes(6) = Bitwise.BitAnd(bytes(6), &b00001111)
bytes(6) = Bitwise.BitOr(bytes(6), &b01000000)
// Set variant (10xxxxxx)
bytes(8) = Bitwise.BitAnd(bytes(8), &b00111111)
bytes(8) = Bitwise.BitOr(bytes(8), &b10000000)
Return bytes
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function GenerateUUIDv4(outputFormat As UUIDFormat = UUIDFormat.Hex, includeHyphens As Boolean = True) As String
// MAIN METHOD
Var uuidBytes() As UInt8 = GenerateRandomUUIDBytes()
Select Case outputFormat
Case UUIDFormat.Hex
Return FormatUUIDHex(uuidBytes, includeHyphens)
Case UUIDFormat.Base64
Return EncodeBase64(uuidBytes)
Else
Return ""
End Select
End Function
#tag EndMethod
#tag Enum, Name = UUIDFormat, Type = Integer, Flags = &h0
Hex
Base64
#tag EndEnum
#tag ViewBehavior
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
InitialValue="-2147483648"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag EndViewBehavior
End Class
#tag EndClass