new simple_hash()
Costa Tsaousis (ktsaou) committed
Mar 16, 2015 at 19:43 UTC
2676a00e476f38e125b88eafa4a8c2be87985964
1 file changed
+7
-4
src/common.c
+7
-4
@@ -16,13 +16,16 @@
16
#include "common.h"
17
#include "web_client.h"
18
19
-// TODO: this can be optimized to avoid strlen()
19
+// http://stackoverflow.com/questions/7666509/hash-function-for-string
20
unsigned long simple_hash(const char *name)
21
{
22
- int i, len = strlen(name);
23
- unsigned long hash = 0;
22
+ const char *s = name;
23
+ unsigned long hash = 5381;
24
+ int i;
25
25
- for(i = 0; i < len ;i++) hash += (i * name[i]) + i + name[i];
26
+ while((i = *s++)) hash = ((hash << 5) + hash) + i;
27
+
28
+ // fprintf(stderr, "HASH: %lu %s\n", hash, name);
29
30
return hash;
31
}