From 1bab37354716a45c50870a93a969cd4784fd34e1 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 14 Nov 2024 21:59:40 +0000 Subject: [PATCH] core: implement string hashing using FNV-1a --- core/hash.c | 16 ++++++++++++++++ core/include/blue/core/hash.h | 9 +++++++++ 2 files changed, 25 insertions(+) create mode 100644 core/hash.c create mode 100644 core/include/blue/core/hash.h diff --git a/core/hash.c b/core/hash.c new file mode 100644 index 0000000..a77c107 --- /dev/null +++ b/core/hash.c @@ -0,0 +1,16 @@ +#include + +#define FNV1_OFFSET_BASIS 0xcbf29ce484222325 +#define FNV1_PRIME 0x100000001b3 + +uint64_t b_hash_string(const char *s) +{ + uint64_t hash = FNV1_OFFSET_BASIS; + + for (size_t i = 0; s[i]; i++) { + hash ^= s[i]; + hash *= FNV1_PRIME; + } + + return hash; +} diff --git a/core/include/blue/core/hash.h b/core/include/blue/core/hash.h new file mode 100644 index 0000000..7fa1158 --- /dev/null +++ b/core/include/blue/core/hash.h @@ -0,0 +1,9 @@ +#ifndef BLUELIB_CORE_HASH_H_ +#define BLUELIB_CORE_HASH_H_ + +#include +#include + +BLUE_API uint64_t b_hash_string(const char *s); + +#endif \ No newline at end of file