Skip to content

Instantly share code, notes, and snippets.

@ianbattersby
Created December 5, 2022 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianbattersby/a49e7534adf2b2ec9fafbb0a7beeafd4 to your computer and use it in GitHub Desktop.
Save ianbattersby/a49e7534adf2b2ec9fafbb0a7beeafd4 to your computer and use it in GitHub Desktop.
Lua has check on ARM64
local M = {}
local B = bit or bit32 or require("catppuccin.lib.native_bit")
local hash_str = function(str) -- MurmurOAAT_32, https://stackoverflow.com/questions/7666509/hash-function-for-string
local hash = 0x12345678
local tbl = { string.byte(str, 1, #str) }
for i = 1, #tbl do
hash = B.bxor(hash, tbl[i])
hash = hash * 0x5bd1e995
hash = B.bxor(hash, B.rshift(hash, 15))
end
return hash
end
function M.hash(tbl) -- Xor hashing: https://codeforces.com/blog/entry/85900
local t = type(tbl)
if t == "boolean" then
return hash_str(tbl and "1" or "0")
elseif t == "string" then
return hash_str(tbl)
elseif t == "number" then
return tostring(tbl)
elseif t == "function" then
return hash_str(string.dump(tbl))
else
local hash = 0
for k, v in pairs(tbl) do
local hash_v = M.hash(v)
hash = B.bxor(hash, hash_str(k .. ":" .. hash_v))
print(k .. ":" .. hash_v)
end
return hash
end
end
local test = {
dap = {
enabled = true,
enable_ui = true, -- enable nvim-dap-ui
},
}
print(M.hash(test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment