Skip to content

Instantly share code, notes, and snippets.

@anzz1
Created May 17, 2023 14:32
Show Gist options
  • Save anzz1/5457e576136362b62c4f2edf52af99f5 to your computer and use it in GitHub Desktop.
Save anzz1/5457e576136362b62c4f2edf52af99f5 to your computer and use it in GitHub Desktop.
joaat
// joaat.c
#include <stdio.h>
unsigned long joaat_i(const unsigned char* s) {
unsigned long hash = 0;
while (*s) {
hash += ((*s>64&&*s<91)?((*s++)+32):*s++); // A-Z -> a-z
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
unsigned long joaat(const unsigned char* s) {
unsigned long hash = 0;
while (*s) {
hash += *s++;
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
int main(int argc, char* argv[])
{
printf("%u %u %u %u\n", joaat(""), joaat("hello"), joaat("Hello"), joaat_i("Hello"));
return 0;
}
-- joaat.lua
function joaat(s)
assert(type(s) == "string", "bad argument #1 to 'joaat' (string expected, got "..type(s)..")")
local h = 0
s = s:lower()
for i = 1, s:len() do
h = h + s:byte(i)
h = h + (h << 10)
h = h ~((h & 0xFFFFFFFF) >> 6)
end
h = h + (h << 3)
h = h ~ ((h & 0xFFFFFFFF) >> 11)
h = h + (h << 15)
return h & 0xFFFFFFFF
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment