feat: uuid.lua 生成符合RFC 4122标准的UUID v4 (#1383)

This commit is contained in:
WantChane
2025-10-31 15:59:04 +00:00
committed by GitHub
parent 69c1b294c5
commit 49fb60f2d5
9 changed files with 87 additions and 1 deletions

46
lua/uuid.lua Normal file
View File

@@ -0,0 +1,46 @@
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) & 0x0F) | 0x40,
rand(0, 255),
(rand(0, 255) & 0x3F) | 0x80,
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)
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
randomseed(os.time())
yield_cand(seg, generate_uuid_v4())
end
end
return M