shithub: snippets

Download patch

ref: d70003df03613f39bcb8db0c0ecca0fcb1d5b58f
parent: 4fb339ca7d208f3ccc55af2c71b7149adfa1d697
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Nov 26 13:58:59 EST 2020

calculate strlen in qpget if not set; add more asserts; set value type to void ptr for qpget

--- a/qp.c
+++ b/qp.c
@@ -56,12 +56,16 @@
 }
 
 int
-qpget(Trie *t, char *k, int len, char **pk, char **pv)
+qpget(Trie *t, char *k, int len, char **pk, void **pv)
 {
 	Tbitmap b;
 
 	assert(k != nil && pk != nil && pv != nil);
 
+	if(len < 1)
+		len = strlen(k);
+	if(t == nil)
+		return -1;
 	for(; isbranch(t); t = twig(t, twigoff(t, b))){
 		b = twigbit(t, k, len);
 		if(!hastwig(t, b))
@@ -119,6 +123,7 @@
 
 	if(t == nil){
 		t = malloc(sizeof(*t));
+		assert(t != nil);
 		t->leaf.k = k;
 		t->leaf.v = v;
 		return t;
@@ -156,6 +161,7 @@
 	t2 = *t;
 	b2 = nibbit(k2, f);
 	t->branch.twigs = malloc(sizeof(*t)*2);
+	assert(t->branch.twigs != nil);
 	t->branch.x = (uvlong)f<<62 | (uvlong)i<<16 | b1 | b2;
 	*twig(t, twigoff(t, b1)) = t1;
 	*twig(t, twigoff(t, b2)) = t2;
--- a/qp.h
+++ b/qp.h
@@ -1,6 +1,6 @@
 typedef union Trie Trie;
 #pragma incomplete Trie
 
-int qpget(Trie *t, char *k, int len, char **pk, char **pv);
+int qpget(Trie *t, char *k, int len, char **pk, void **pv);
 int qpnext(Trie *t, char **pk, int *plen, void **pv);
 Trie *qpset(Trie *t, char *k, int len, void *v);