Files
rime-ice/lua/uuid.lua
Yuri Sequoia 739a87af0a fix(uuid): resolve duplicate UUID generation by improving random seeding (#1473)
- Moved math.randomseed() from the generation function to the initialization phase.
- Replaced os.time() with a more precise seed (os.time + os.clock) to prevent identical sequences when calls occur within the same second.
2026-02-04 18:42:18 +08:00

47 lines
917 B
Lua

local function yield_cand(seg, text)
local cand = Candidate("", seg.start, seg._end, text, "")
cand.quality = 100
yield(cand)
end
local fmt = string.format
local rand = math.random
local randomseed = math.randomseed
local function generate_uuid_v4()
return fmt(
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
rand(0, 255),
rand(0, 255),
rand(0, 255),
rand(0, 255),
rand(0, 255),
rand(0, 255),
((rand(0, 255) % 16) + 64),
rand(0, 255),
((rand(0, 255) % 64) + 128),
rand(0, 255),
rand(0, 255),
rand(0, 255),
rand(0, 255),
rand(0, 255),
rand(0, 255),
rand(0, 255)
)
end
local M = {}
function M.init(env)
randomseed(os.time() + os.clock() * 1000)
M.uuid = env.engine.schema.config:get_string(env.name_space:gsub("^*", "")) or "uuid"
end
function M.func(input, seg, _)
if input == M.uuid then
yield_cand(seg, generate_uuid_v4())
end
end
return M