<commit_before>--- jit-uuid
-- Fast and dependency-free uuid generation for OpenResty/LuaJIT.
-- @module jit-uuid
-- @author <NAME>
-- @license MIT
-- @release 0.0.2

local bit = require 'bit'

local randomseed = math.randomseed
local concat = table.concat
local random = math.random
local match = string.match
local tohex = bit.tohex
local band = bit.band
local bor = bit.bor

local _M = {
  _VERSION = '0.0.2'
}

function _M.seed()
  if ngx then
    randomseed(ngx.time() + ngx.worker.pid())
  elseif package.loaded['socket'] and package.loaded['socket'].gettime then
    randomseed(package.loaded['socket'].gettime()*10000)
  else
    randomseed(os.time())
  end
end

local buf = {0,0,0,0,'-',0,0,'-',0,0,'-',0,0,'-',0,0,0,0,0,0}
local buf_len = #buf

--- Generate a v4 uuid.
-- @treturn string `uuid`: a v4 (randomly generated) uuid.
-- @usage
-- local uuid = require "resty.jit-uuid"
--
-- local u1 = uuid() -- metatable
-- local u2 = uuid.generate()
function _M.generate()
  for i = 1, buf_len do
    if i ~= 9 and i ~= 12 then -- benchmarked
      buf[i] = tohex(random(0, 255), 2)
    end
  end

  buf[5], buf[8], buf[11], buf[14] = '-', '-', '-', '-' -- benchmarked
  buf[9] = tohex(bor(band(random(0, 255), 0x0F), 0x40), 2)
  buf[12] = tohex(bor(band(random(0, 255), 0x3F), 0x80), 2)

  return concat(buf)
end

do
  local find = string.find
  if ngx and find(ngx.config.nginx_configure(),'--with-pcre-jit',nil,true) then
    local re_find = ngx.re.find
    local regex = '^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'

    --- Validate v4 uuids.
    -- Only validates v4 uuids including dashes, version and variant.
    -- Use JIT PCRE if available in OpenResty or fallbacks on Lua pattern.
    -- @param[type=string] str String to verify.<commit_msg>style: cleanup and scope buffer<commit_after>--- jit-uuid
-- Fast and dependency-free uuid generation for OpenResty/LuaJIT.
-- @module jit-uuid
-- @author <NAME>
-- @license MIT
-- @release 0.0.2

local bit = require 'bit'

local concat = table.concat
local random = math.random
local match = string.match
local tohex = bit.tohex
local band = bit.band
local bor = bit.bor

local _M = {
  _VERSION = '0.0.2'
}

function _M.seed()
  if ngx then
    math.randomseed(ngx.time() + ngx.worker.pid())
  elseif package.loaded['socket'] and package.loaded['socket'].gettime then
    math.randomseed(package.loaded['socket'].gettime()*10000)
  else
    math.randomseed(os.time())
  end
end

do
  local buf = {0,0,0,0,'-',0,0,'-',0,0,'-',0,0,'-',0,0,0,0,0,0}
  local buf_len = #buf

  --- Generate a v4 uuid.
  -- @treturn string `uuid`: a v4 (randomly generated) uuid.
  -- @usage
  -- local uuid = require "resty.jit-uuid"
  --
  -- local u1 = uuid() -- metatable
  -- local u2 = uuid.generate()
  function _M.generate()
    for i = 1, buf_len do
      if i ~= 9 and i ~= 12 then -- benchmarked
        buf[i] = tohex(random(0, 255), 2)
      end
    end

    buf[5], buf[8], buf[11], buf[14] = '-', '-', '-', '-' -- benchmarked
    buf[9] = tohex(bor(band(random(0, 255), 0x0F), 0x40), 2)
    buf[12] = tohex(bor(band(random(0, 255), 0x3F), 0x80), 2)

    return concat(buf)
  end
end

do
  local find = string.find
  if ngx and find(ngx.config.nginx_configure(),'--with-pcre-jit',nil,true) then
    local re_find = ngx.re.find
    local regex = '^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'

    --- Validate v4 uuids.
    -- Only validates v4 uuids including dashes, version and variant.
    -- Use JIT PCRE if available in OpenResty or fallbacks on Lua pattern.
    -- @param[type=string] str String to verify.
{'fraction_non_alphanumeric': 0.18191871716892452, 'fraction_numerical': 0.049764998617638924, 'mean_word_length': 3.051511758118701, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2935001', 'n_tokens_mistral': 1489, 'n_tokens_neox': 1429, 'n_words': 358}