mirror of
https://github.com/iDvel/rime-ice.git
synced 2026-04-25 03:24:41 +00:00
feat: uuid.lua 生成符合RFC 4122标准的UUID v4 (#1383)
This commit is contained in:
46
lua/uuid.lua
Normal file
46
lua/uuid.lua
Normal 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
|
||||
Reference in New Issue
Block a user