feat(str): add SStrHash

This commit is contained in:
Adam Heinermann 2025-08-31 22:14:28 -07:00 committed by fallenoak
parent a2959efef5
commit 0f453bd413
3 changed files with 150 additions and 0 deletions

View file

@ -50,6 +50,13 @@ int32_t s_initialized;
double s_realDigit[20][10];
const uint32_t s_hashtable[16] = {
0x486E26EE, 0xDCAA16B3, 0xE1918EEF, 0x202DAFDB,
0x341C7DC7, 0x1C365303, 0x40EF2D37, 0x65FD5E49,
0xD6057177, 0x904ECE93, 0x1C38024F, 0x98FD323B,
0xE3061AE7, 0xA39B0FA1, 0x9797F25F, 0xE4444563,
};
void GetNextTextUpper(uint32_t* orig, const char** string, uint32_t* upper) {
uint8_t byte = **string;
int32_t v3 = bytesFromUTF8[byte];
@ -349,6 +356,42 @@ char* SStrDupA(const char* string, const char* filename, uint32_t linenumber) {
return dup;
}
uint32_t SStrHash(const char* string, uint32_t flags, uint32_t seed) {
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(string);
STORM_VALIDATE_END;
uint32_t result = seed ? seed : 0x7FED7FED;
uint32_t adjust = 0xEEEEEEEE;
uint32_t ch;
if (flags & SSTR_HASH_CASESENSITIVE) {
for (; *string; string++) {
ch = *string;
result = (s_hashtable[ch / 16] - s_hashtable[ch % 16]) ^ (adjust + result);
adjust = 33 * adjust + result + ch + 3;
}
}
else {
for (; *string; string++) {
ch = *string;
if (ch >= 'a' && ch <= 'z') {
ch -= 32;
}
if (ch == '/') {
ch = '\\';
}
result = (s_hashtable[ch / 16] - s_hashtable[ch % 16]) ^ (adjust + result);
adjust = 33 * adjust + result + ch + 3;
}
}
return result ? result : 1;
}
uint32_t SStrHashHT(const char* string) {
char normalized[0x400];
char* buf = normalized;

View file

@ -5,9 +5,13 @@
#include <cstdint>
#include <cstdlib>
#define STORM_MAX_PATH 260
#define STORM_MAX_STR 0x7FFFFFFF
#define SSTR_HASH_CASESENSITIVE 1
char* SStrChr(char* string, char search);
const char* SStrChr(const char* string, char search);
@ -24,6 +28,8 @@ size_t SStrCopy(char* dest, const char* source, size_t destsize = STORM_MAX_STR)
char* SStrDupA(const char* string, const char* filename, uint32_t linenumber);
uint32_t SStrHash(const char* string, uint32_t flags = 0, uint32_t seed = 0);
uint32_t SStrHashHT(const char* string);
size_t SStrLen(const char* string);