-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
27 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
local bitop = bit or bit32 or require("nightfox.lib.native_bit") | ||
|
||
-- Reference for dj2b algorithem | ||
-- https://theartincode.stanis.me/008-djb2/ | ||
local function djb2(s) | ||
local h = 5381 | ||
local t = { string.byte(s, 1, #s) } | ||
for i = 1, #t do | ||
h = bitop.lshift(h, 5) + t[i] -- h * 33 + c | ||
end | ||
return h | ||
end | ||
|
||
-- Reference: https://github.com/catppuccin/nvim/blob/151b5f6aa74f08a707a7862519bdc38bb2b9f505/lua/catppuccin/lib/hashing.lua | ||
local function hash(x) | ||
local t = type(x) | ||
local function serialize(val) | ||
local t = type(val) | ||
if t == "table" then | ||
local h = 0 | ||
for k, v in pairs(x) do | ||
h = bitop.bxor(h, djb2(k .. hash(v))) | ||
local result = "" | ||
for k, v in pairs(val) do | ||
result = result .. tostring(k) .. serialize(v) | ||
end | ||
return h | ||
return result | ||
elseif t == "function" then | ||
return djb2(string.dump(x)) | ||
return string.dump(val) | ||
else | ||
return tostring(val) | ||
end | ||
end | ||
|
||
local function checksum(val) | ||
local str = serialize(val) | ||
local hash = 5381 | ||
for i = 1, #str do | ||
hash = bit.lshift(hash, 5) + str:byte(i) | ||
end | ||
return tostring(x) | ||
return hash | ||
end | ||
|
||
return hash | ||
return checksum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters