-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtil.lua
More file actions
327 lines (276 loc) · 8.04 KB
/
Util.lua
File metadata and controls
327 lines (276 loc) · 8.04 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
local Util, _ = LibStub:NewLibrary("EventSourcing/Util", 1)
if not Util then
return end
-- Searches for the first index that has value >= to the given value
function Util.BinarySearch(list, value, comparator, min, max)
Util.assertTable(list, 'list')
if min == nil then
min = 1
max = #list
end
if min > max then
return nil
end
local test = math.floor((max + min) / 2)
local result = comparator(list[test], value)
if result == 0 then
if (list[test] == value) then
return test
end
-- Linear search left side
local linearTest = test
while linearTest > 1 and result == 0 do
linearTest = linearTest - 1
result = comparator(list[linearTest], value)
if list[linearTest] == value then
return linearTest
end
end
linearTest = test
result = 0
while linearTest < #list and result == 0 do
linearTest = linearTest + 1
result = comparator(list[linearTest], value)
if list[linearTest] == value then
return linearTest
end
end
end
if result == -1 then
-- Tested element < value, solution not in left side
return Util.BinarySearch(list, value, comparator, test + 1, max)
elseif test == min then
return test
else
-- Tested element >= value, solution is in left side or current element
return Util.BinarySearch(list, value, comparator, min, test)
end
end
function Util.IsSorted(list, comparator)
if (comparator == nil) then
error("Missing argument comparator")
end
for i = 1, #list - 1 do
if comparator(list[i], list[i+1]) == 1 then
return false
end
end
return true
end
function Util.DumpTable(table, indent)
indent = indent or 0;
local keys = {};
for k in pairs(table) do
keys[#keys+1] = k;
end
print(string.rep(' ', indent)..'{');
indent = indent + 1;
for k, v in pairs(table) do
local key = k;
if (type(key) == 'string') then
if not (string.match(key, '^[A-Za-z_][0-9A-Za-z_]*$')) then
key = "['"..key.."']";
end
elseif (type(key) == 'number') then
key = "["..key.."]";
end
if (type(v) == 'table') then
if (next(v)) then
print(string.rep(' ', indent), tostring(key), "=");
Util.DumpTable(v, indent);
else
print(string.rep(' ', indent), tostring(key), "= {},");
end
elseif (type(v) == 'string') then
print(string.rep(' ', indent), tostring(key), " = ", "'"..v.."'");
else
print(string.rep(' ', indent), tostring(key), " = ", tostring(v));
end
end
indent = indent - 1;
print(string.rep(' ', indent)..'}');
end
function Util.CreateFieldSorter(field)
return function(a, b)
if a[field] < b[field] then
return -1
elseif a[field] > b[field] then
return 1
else
return 0
end
end
end
-- Return the 1-based unix epoch week number
function Util.WeekNumber(unixtimestamp)
Util.assertNumber(unixtimestamp, 'unixtimestamp')
return 1 + math.floor(unixtimestamp / 604800)
end
function Util.WeekStart(week)
return (week - 1) * 604800
end
local function recursiveCompare(a, b, fields, i)
if a[fields[i]] == b[fields[i]] then
if i < #fields then
return recursiveCompare(a, b, fields, i + 1)
else
return 0
end
elseif a[fields[i]] < b[fields[i]] then
return -1
else
return 1
end
end
function Util.CreateMultiFieldSorter(...)
local fields = {...}
return function(a, b)
return recursiveCompare(a, b, fields, 1)
end
end
function Util.InvertSorter(sorter)
return function(a, b)
return -1 * sorter(a, b)
end
end
function Util.time()
if GetServerTime then
return GetServerTime()
else
return os.time()
end
end
function Util.UUID()
local random = math.random(1, 2^31 - 1)
local ts = Util.time()
local hex = string.format('%08x%08x', ts, random)
return hex
end
function Util.wipe(data)
if (table.wipe) then
table.wipe(data)
else
for k, _ in pairs(data) do
data[k] = nil
end
end
end
function Util.IntegerToBytes(number)
return math.floor(number / 2^24),
math.floor((number % 2^24) / 2^16),
math.floor((number % 2^16) / 2^8),
number % 2^8
end
function Util.ByteSum(number)
return math.floor(number / 2^24)
+ math.floor((number % 2^24) / 2^16) + math.floor((number % 2^16) / 2^8) + number % 2^8;
end
function Util.IntegerChecksumCoroutine()
-- for i=1, #str do
-- local s = string.byte(str, i, i)
-- a = (a+s)%65521
-- b = (b+a)%65521
-- end
-- return b*65536+a
return coroutine.create(function(number)
local a = 1
local b = 0
local b1, b2, b3, b4
local next = number
while(true) do
if type(next) ~= 'number' then
error("Given element is not a number")
end
b1, b2, b3, b4 = Util.IntegerToBytes(math.floor(next))
a = (a + b1)
b = (b + a)
a = (a + b2)
b = (b + a)
a = (a + b3)
b = (b + a)
a = (a + b4) % 65521
b = (b + a) % 65521
next = coroutine.yield(b * 65536 + a)
end
end)
end
local defaultCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
function Util.random(length, alternativeCharset)
local charset = alternativeCharset or defaultCharset
local charsetLength = string.len(charset)
local result = ""
while string.len(result) < length do
local i = math.random(1, charsetLength)
result = result .. charset.sub(charset, i, i)
end
return result
end
function Util.getIntegerGuid(target)
return Util.getIntegerFromGuid(UnitGUID(target))
end
function Util.getIntegerFromGuid(guid)
return tonumber(string.sub(guid, -8), 16)
end
local GUIDPrefix = string.sub(UnitGUID("player"), 1, -9)
function Util.getGuidFromInteger(int)
return GUIDPrefix .. string.format("%08X", int)
end
function Util.guid()
local hex = '0123456789abcdef'
return table.concat({
Util.random(8, hex),
Util.random(4, hex),
Util.random(4, hex),
Util.random(4, hex),
Util.random(12, hex),
}, '-')
end
function Util.assertType(arg, name, expectedType, optional)
if optional and arg == nil then
return
end
if type(arg) ~= expectedType then
error(string.format("Expected argument %s to be of type %s, got %s insead", name, expectedType, type(arg)))
end
end
function Util.assertFunction(arg, name, optional)
return Util.assertType(arg, name, 'function', optional)
end
function Util.assertTable(arg, name, optional)
return Util.assertType(arg, name, 'table', optional)
end
function Util.assertString(arg, name, optional)
return Util.assertType(arg, name, 'string', optional)
end
function Util.assertNumber(arg, name, optional)
return Util.assertType(arg, name, 'number', optional)
end
function Util.assertTableWithFunctions(arg, funcs, name)
Util.assertTable(arg, name)
Util.assertTable(funcs)
for _, v in ipairs(funcs) do
Util.assertFunction(arg[v], string.format("%s:%s", name, v))
end
end
function Util.assertLogger(arg, optional)
if optional and arg == nil then
return
end
Util.assertTableWithFunctions(arg, {
"Warning",
"Info",
"Error",
"Trace",
"Debug",
"Fatal"
}, "logger")
end
function Util.assertInstanceOf(arg, type, typeName, optional)
if optional and arg == nil then
return
end
Util.assertTable(arg)
if getmetatable(arg) ~= type then
error(string.format("Argument must be of type %s", typeName))
end
end