2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

Module:Sandbox/机智的小鱼君/uuidv4

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索
Template-info.svg 模块文档  [查看] [编辑] [历史] [刷新]
v4
  1. 64F6C879-CC66-4E96-8C55-C4478D7AA05D
  2. 64F6C879-CC66-4E96-8C55-C4478D7AA05D
  3. 64F6C879-CC66-4E96-8C55-C4478D7AA05D
fake
  1. d6cce35c-487a-58fe-ab29-e32c2621f38d
  2. d6cce35c-487a-58fe-ab29-e32c2621f38d
  3. d6cce35c-487a-58fe-ab29-e32c2621f38d
--[[
The MIT License (MIT)
Copyright (c) 2012 Toby Jennings
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
associated documentation files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, publish, distribute, 
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--]]
local p = {}
-----
math.randomseed(os.time())
math.random()
-----
local function num2bs(num)
  local _mod = math.fmod or math.mod
  local _floor = math.floor
  --
  local result = ''
  if (num == 0) then
    return '0'
  end
  while (num > 0) do
    result = _mod(num, 2) .. result
    num = _floor(num * 0.5)
  end
  return result
end
--
local function bs2num(num)
  local _sub = string.sub
  local index, result = 0, 0
  if (num == '0') then
    return 0
  end
  for i = #num, 1, -1 do
    local this_val = _sub(num, i, i)
    if this_val == '1' then
      result = result + (2 ^ index)
    end
    index = index + 1
  end
  return result
end
--
local function padbits(num, bits)
  if #num == bits then
    return num
  end
  if #num > bits then
    print('too many bits')
  end
  local pad = bits - #num
  for i = 1, pad do
    num = '0' .. num
  end
  return num
end
--
function p.uuid_v4()
  local _rnd = math.random
  local _fmt = string.format
  --
  _rnd()
  --
  local time_low_a = _rnd(0, 65535)
  local time_low_b = _rnd(0, 65535)
  --
  local time_mid = _rnd(0, 65535)
  --
  local time_hi = _rnd(0, 4095)
  time_hi = padbits(num2bs(time_hi), 12)
  local time_hi_and_version = bs2num('0100' .. time_hi)
  --
  local clock_seq_hi_res = _rnd(0, 63)
  clock_seq_hi_res = padbits(num2bs(clock_seq_hi_res), 6)
  clock_seq_hi_res = '10' .. clock_seq_hi_res
  --
  local clock_seq_low = _rnd(0, 255)
  clock_seq_low = padbits(num2bs(clock_seq_low), 8)
  --
  local clock_seq = bs2num(clock_seq_hi_res .. clock_seq_low)
  --
  local node = {}
  for i = 1, 6 do
    node[i] = _rnd(0, 255)
  end
  --
  local guid = ''
  guid = guid .. padbits(_fmt('%X', time_low_a), 4)
  guid = guid .. padbits(_fmt('%X', time_low_b), 4) .. '-'
  guid = guid .. padbits(_fmt('%X', time_mid), 4) .. '-'
  guid = guid .. padbits(_fmt('%X', time_hi_and_version), 4) .. '-'
  guid = guid .. padbits(_fmt('%X', clock_seq), 4) .. '-'
  --
  for i = 1, 6 do
    guid = guid .. padbits(_fmt('%X', node[i]), 2)
  end
  --
  return guid
end
--
function p.fake_uuid()
  local seed = {'e', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
  local tb = {}
  for i = 1, 32 do
    table.insert(tb, seed[math.random(1, 16)])
  end
  local sid = table.concat(tb)
  return string.format(
    '%s-%s-%s-%s-%s',
    string.sub(sid, 1, 8),
    string.sub(sid, 9, 12),
    string.sub(sid, 13, 16),
    string.sub(sid, 17, 20),
    string.sub(sid, 21, 32)
  )
end
--
return p