uuid: Use mathx's _random instead of stringx's

This commit is contained in:
rhy 2023-01-09 12:11:53 -03:00
parent e630418372
commit 57bc1a2edb

View File

@ -6,15 +6,11 @@ local path = (...):gsub("uuid", "")
local uuid = {} local uuid = {}
--helper for optionally passed random; defaults to love.math.random if present, otherwise math.random --(internal; use a provided random generator object, or not)
local _global_random = math.random local function _random(rng, ...)
if love and love.math and love.math.random then if rng then return rng:random(...) end
_global_random = love.math.random if love then return nlove.math.random(...) end
end return math.random(...)
local function _random(min, max, r)
return r and r:random(min, max)
or _global_random(min, max)
end end
local uuid4_template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" local uuid4_template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
@ -24,7 +20,7 @@ function uuid.uuid4(rng)
return uuid4_template:gsub("[xy]", function (c) return uuid4_template:gsub("[xy]", function (c)
-- x should be 0x0-0xF, the single y should be 0x8-0xB -- x should be 0x0-0xF, the single y should be 0x8-0xB
-- 4 should always just be 4 (denoting uuid version) -- 4 should always just be 4 (denoting uuid version)
return string.format("%x", c == "x" and _random(0x0, 0xF, rng) or _random(0x8, 0xB, rng)) return string.format("%x", c == "x" and _random(rng, 0x0, 0xF) or _random(rng, 0x8, 0xB))
end) end)
end end