2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-06-09 18:22:33 +00:00

chore: call common djb2 implementations in game hashing funcs

This commit is contained in:
Jan Laupetin
2026-02-21 10:52:36 +00:00
parent 9502ebfc26
commit f7e0cb3c45
10 changed files with 318 additions and 164 deletions
+100
View File
@@ -0,0 +1,100 @@
#pragma once
#include <cctype>
#include <cstdint>
// This header contains multiple varying implementations of the DJB2 algorithm:
// http://www.cse.yorku.ca/~oz/hash.html
constexpr uint32_t DJB2_STARTING_VALUE = 5381;
static constexpr uint32_t djb2(const char* str, uint32_t hash)
{
for (char c = *str; c; c = *++str)
{
// hash * 33 + c
hash = ((hash << 5) + hash) + c;
}
return hash;
}
static constexpr uint32_t djb2(const char* str)
{
return djb2(str, DJB2_STARTING_VALUE);
}
static constexpr uint32_t djb2_nocase(const char* str, uint32_t hash)
{
for (char c = *str; c; c = *++str)
{
// Or with 0x20 makes the string case-insensitive
// but also messes up non-letters
hash = ((hash << 5) + hash) + (c | 0x20);
}
return hash;
}
static constexpr uint32_t djb2_nocase(const char* str)
{
return djb2_nocase(str, DJB2_STARTING_VALUE);
}
static constexpr uint32_t djb2_lower(const char* str, uint32_t hash)
{
for (char c = *str; c; c = *++str)
hash = ((hash << 5) + hash) + std::tolower(c);
return hash;
}
static constexpr uint32_t djb2_lower(const char* str)
{
return djb2_lower(str, DJB2_STARTING_VALUE);
}
static constexpr uint32_t djb2_xor(const char* str, uint32_t hash)
{
for (char c = *str; c; c = *++str)
{
hash = ((hash << 5) + hash) ^ c;
}
return hash;
}
static constexpr uint32_t djb2_xor(const char* str)
{
return djb2_xor(str, DJB2_STARTING_VALUE);
}
static constexpr uint32_t djb2_xor_nocase(const char* str, uint32_t hash)
{
for (char c = *str; c; c = *++str)
{
// Or with 0x20 makes the string case-insensitive
// but also messes up non-letters
hash = ((hash << 5) + hash) ^ (c | 0x20);
}
return hash;
}
static constexpr uint32_t djb2_xor_nocase(const char* str)
{
return djb2_xor_nocase(str, DJB2_STARTING_VALUE);
}
static constexpr uint32_t djb2_xor_lower(const char* str, uint32_t hash)
{
for (char c = *str; c; c = *++str)
hash = ((hash << 5) + hash) ^ std::tolower(c);
return hash;
}
static constexpr uint32_t djb2_xor_lower(const char* str)
{
return djb2_xor_lower(str, DJB2_STARTING_VALUE);
}