X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/978c2c944cb540827ba53ceb2301e4bd460d9591..b3f83f127273da21506f697e256ae010587b10f1:/zipmap.c diff --git a/zipmap.c b/zipmap.c index 05bf6d6d..f45ef0dd 100644 --- a/zipmap.c +++ b/zipmap.c @@ -116,7 +116,7 @@ static unsigned int zipmapDecodeLength(unsigned char *p) { unsigned int len = *p; if (len < ZIPMAP_BIGLEN) return len; - memcpy(&len,p,sizeof(unsigned int)); + memcpy(&len,p+1,sizeof(unsigned int)); return len; } @@ -363,6 +363,15 @@ int zipmapExists(unsigned char *zm, unsigned char *key, unsigned int klen) { return zipmapLookupRaw(zm,key,klen,NULL,NULL,NULL) != NULL; } +/* Return the number of entries inside a zipmap */ +unsigned int zipmapLen(unsigned char *zm) { + unsigned char *p = zipmapRewind(zm); + unsigned int len = 0; + + while((p = zipmapNext(p,NULL,NULL,NULL,NULL)) != NULL) len++; + return len; +} + void zipmapRepr(unsigned char *p) { unsigned int l; @@ -405,6 +414,13 @@ int main(void) { unsigned char *zm; zm = zipmapNew(); + + zm = zipmapSet(zm,(unsigned char*) "name",4, (unsigned char*) "foo",3,NULL); + zm = zipmapSet(zm,(unsigned char*) "surname",7, (unsigned char*) "foo",3,NULL); + zm = zipmapSet(zm,(unsigned char*) "age",3, (unsigned char*) "foo",3,NULL); + zipmapRepr(zm); + exit(1); + zm = zipmapSet(zm,(unsigned char*) "hello",5, (unsigned char*) "world!",6,NULL); zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "bar",3,NULL); zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "!",1,NULL);