Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local uuid = {}
- local function fold(init, op, ...)
- local result = init
- local args = table.pack(...)
- for i = 1, args.n do
- result = op(result, args[i])
- end
- return result
- end
- local function band(...)
- return fold(0xFFFFFFFF, function(a, b) return a & b end, ...)
- end
- local function bor(...)
- return fold(0, function(a, b) return a | b end, ...)
- end
- function uuid.next()
- local sets = {4, 2, 2, 2, 6}
- local result = ""
- local pos = 0
- for _, set in ipairs(sets) do
- if result:len() > 0 then
- result = result .. "-"
- end
- for _ = 1, set do
- local byte = math.random(0, 255)
- if pos == 6 then
- byte = bor(band(byte, 0x0F), 0x40)
- elseif pos == 8 then
- byte = bor(band(byte, 0x3F), 0x80)
- end
- result = result .. string.format("%02x", byte)
- pos = pos + 1
- end
- end
- return result
- end
- return uuid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement