commit 624e2ab468eeeddc35265dd8944a02eae4d0e5f7
parent ef9c75a0d02a24f5e1962f0a11fe834906cebef5
Author: vaplv <vaplv@free.fr>
Date: Thu, 11 Dec 2014 14:11:23 +0100
Add and test the str_hash function
Diffstat:
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/src/str.h b/src/str.h
@@ -16,8 +16,9 @@
#ifndef STR_H
#define STR_H
-#include "rsys.h"
+#include "hash.h"
#include "mem_allocator.h"
+#include "rsys.h"
struct str {
/* Internal data. Should not be publicly accessed */
@@ -137,4 +138,20 @@ str_copy_and_release(struct str* dst, struct str* src)
return res;
}
+static INLINE size_t
+str_hash(const struct str* str)
+{
+ const char* c_str;
+ ASSERT(str);
+ c_str = str_cget(str);
+#ifdef ARCH_32BITS
+ return (size_t)hash_fnv32(c_str, str->len);
+#elif defined ARCH_64BITS
+ return (size_t)hash_fnv64(c_str, str->len);
+#else
+ #error "Unexpected architecture"
+#endif
+}
+
#endif /* STR_H */
+
diff --git a/src/test_str.c b/src/test_str.c
@@ -96,7 +96,10 @@ main(int argc, char** argv)
CHECK(strcmp(str_cget(&str2), "0Hello world!"), 0);
CHECK(strcmp(str_cget(&str), "0Hello world!"), 0);
+ CHECK(str_hash(&str), str_hash(&str2));
str_clear(&str2);
+ NCHECK(str_hash(&str), str_hash(&str2));
+
str_copy_and_clear(&str2, &str);
CHECK(strcmp(str_cget(&str2), "0Hello world!"), 0);
CHECK(str_len(&str2), strlen(str_cget(&str2)));
@@ -106,6 +109,7 @@ main(int argc, char** argv)
str_init(&allocator_proxy, &str2);
str_set(&str2, "ABC Hello a0123456789ABCDEFbcdefgh world! insert");
+ NCHECK(str_hash(&str), str_hash(&str2));
str_copy_and_clear(&str, &str2);
CHECK(0, strcmp
(str_cget(&str), "ABC Hello a0123456789ABCDEFbcdefgh world! insert"));