From fe67cf92ccdd1498c4073eb42e465a203b3c361a Mon Sep 17 00:00:00 2001
From: Andrea Shepard <andrea@persephoneslair.org>
Date: Mon, 6 Feb 2017 20:21:14 +0000
Subject: [PATCH] Implement string-hashing support functions

---
 src/datastructures.c | 25 +++++++++++++++++++++++++
 src/internal.h       |  3 +++
 2 files changed, 28 insertions(+)

diff --git a/src/datastructures.c b/src/datastructures.c
index 0feeb217..ba619074 100644
--- a/src/datastructures.c
+++ b/src/datastructures.c
@@ -386,6 +386,23 @@ HHashValue h_hash_ptr(const void *p) {
   return (uintptr_t)p >> 4;
 }
 
+HHashValue h_hash_str(const void *p) {
+  const char *s;
+
+  s = (const char *)p;
+
+  return h_djbhash_str(s);
+}
+
+bool h_eq_str(const void *p, const void *q) {
+  if (p && q) {
+    if (strcmp((const char *)p, (const char *)q) == 0) return true;
+    else return false;
+  } else {
+    return h_eq_ptr(p, q);
+  }
+}
+
 uint32_t h_djbhash(const uint8_t *buf, size_t len) {
   uint32_t hash = 5381;
   while (len--) {
@@ -394,6 +411,14 @@ uint32_t h_djbhash(const uint8_t *buf, size_t len) {
   return hash;
 }
 
+uint32_t h_djbhash_str(const char *s) {
+  uint32_t hash = 5381;
+  while (*s != '\0') {
+    hash = hash * 33 + (uint8_t)(*s++);
+  }
+  return hash;
+}
+
 void h_symbol_put(HParseState *state, const char* key, void *value) {
   if (!state->symbol_table) {
     state->symbol_table = h_slist_new(state->arena);
diff --git a/src/internal.h b/src/internal.h
index 2b2d6004..b4af5c55 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -460,8 +460,11 @@ typedef HHashTable HHashSet;
 bool h_hashset_equal(const HHashSet *a, const HHashSet *b);
 
 bool h_eq_ptr(const void *p, const void *q);
+bool h_eq_str(const void *p, const void *q);
 HHashValue h_hash_ptr(const void *p);
+HHashValue h_hash_str(const void *p);
 uint32_t h_djbhash(const uint8_t *buf, size_t len);
+uint32_t h_djbhash_str(const char *s);
 
 void h_symbol_put(HParseState *state, const char* key, void *value);
 void* h_symbol_get(HParseState *state, const char* key);
-- 
GitLab