Merge branch 'vfs-dev'
[lunaix-os.git] / lunaix-os / libs / hash.c
1 #include <lib/hash.h>
2
3 /**
4  * @brief Simple string hash function
5  *
6  * ref: https://stackoverflow.com/a/7666577
7  *
8  * @param str
9  * @return unsigned int
10  */
11 uint32_t
12 strhash_32(char* str, uint32_t truncate_to)
13 {
14     if (!str)
15         return 0;
16
17     uint32_t hash = 5381;
18     int c;
19
20     while ((c = *str++))
21         hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
22
23     return hash >> (HASH_SIZE_BITS - truncate_to);
24 }