/*
- * Low-level tools.
- *
- * - dict type
- * - urlenc/urldec for dict.
- * - Postgres array parser.
+ * A string to string dictionary.
*/
#include <usual/mdict.h>
#include <ctype.h>
-
-/*
- * Quick dicts for urlencode.
- * - fast lookup
- * - fast creation
- * - not very memory-efficient
- * - existing keys cannot be freed
- */
-
struct MDict {
struct CBTree *tree;
CxMem *cx;
}
el = cbtree_lookup(dict->tree, key, klen);
if (el) {
- mbuf_free(&el->val);
+ cx_free(dict->cx, mbuf_data(&el->val));
mbuf_init_fixed_reader(&el->val, vptr, vlen);
} else {
kptr = cx_alloc(dict->cx, klen + 1);
return true;
}
+bool mdict_del_key(struct MDict *dict, const char *key, unsigned klen)
+{
+ return cbtree_delete(dict->tree, key, klen);
+}
+
/*
* walk over key-val pairs
*/
/* insert value */
el = cbtree_lookup(dict->tree, k, klen);
if (el) {
- mbuf_free(&el->val);
+ cx_free(dict->cx, mbuf_data(&el->val));
mbuf_init_fixed_reader(&el->val, v, vlen);
} else {
el = cx_alloc(dict->cx, sizeof(*el));
/** Put string to dict */
bool mdict_put_str(struct MDict *dict, const char *key, unsigned klen, const char *val, unsigned vlen);
+/** Remove a key from dict */
+bool mdict_del_key(struct MDict *dict, const char *key, unsigned klen);
+
/** Signature for walker callback */
typedef bool (*mdict_walker_f)(void *arg, const struct MBuf *k, const struct MBuf *v);
return mdict_put_str(dict, key, klen, val, vlen);
}
+/** Remove value from dict */
+static inline bool mdict_del(struct MDict *dict, const char *key)
+{
+ return mdict_del_key(dict, key, strlen(key));
+}
+
/** Urldecode string and add keys with values to dict */
bool mdict_urldecode(struct MDict *dict, const char *str, unsigned len);