@@ -28,10 +28,11 @@ #include #include #include "object.h" #include "string.h" +#include "hash.h" struct CFWString { CFWObject obj; char *data; size_t len; @@ -80,10 +81,27 @@ if (str1->len != str2->len) return false; return !memcmp(str1->data, str2->data, str1->len); } + +static uint32_t +hash(void *ptr) +{ + CFWString *str = ptr; + size_t i; + uint32_t hash; + + CFW_HASH_INIT(hash); + + for (i = 0; i < str->len; i++) + CFW_HASH_ADD(hash, str->data[i]); + + CFW_HASH_FINALIZE(hash); + + return hash; +} static void* copy(void *ptr) { CFWString *str = ptr; @@ -175,8 +193,9 @@ .name = "CFWString", .size = sizeof(CFWString), .ctor = ctor, .dtor = dtor, .equal = equal, + .hash = hash, .copy = copy }; CFWClass *cfw_string = &class;