From 83f5e77d52613c06621316ec6c9590052f3fb6dd Mon Sep 17 00:00:00 2001 From: antifallobst Date: Mon, 5 Jun 2023 17:41:34 +0200 Subject: [PATCH] feature (strings): implemented DJB string hashing function --- inc/utils/string.h | 2 ++ src/utils/string.c | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/inc/utils/string.h b/inc/utils/string.h index 2405c8d..0861204 100644 --- a/inc/utils/string.h +++ b/inc/utils/string.h @@ -57,4 +57,6 @@ bool string_is_char_alpha (char chr); bool string_is_char_uppercase (char chr); bool string_is_char_lowercase (char chr); +uint32_t string_hash_djb (string_t str); + #endif //NOX_STRING_H diff --git a/src/utils/string.c b/src/utils/string.c index 6936356..c4343ef 100644 --- a/src/utils/string.c +++ b/src/utils/string.c @@ -462,4 +462,14 @@ bool string_is_char_lowercase (char chr) { return true; } return false; -} \ No newline at end of file +} + +uint32_t string_hash_djb(string_t str) { + uint32_t hash = 5381; + + while (*(++str) != '\0') { + hash = ((hash << 5) + hash) + (*str); + } + + return hash; +}