From c7cd0c40e06c013b490c2f9f3939189503e9b060 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Mon, 14 Jul 2014 02:06:02 +0200
Subject: Add support for custom str_map key comp. fun.
That wasn't hard.
---
src/common.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
(limited to 'src')
diff --git a/src/common.c b/src/common.c
index d905929..2907159 100644
--- a/src/common.c
+++ b/src/common.c
@@ -571,6 +571,9 @@ struct str_map
size_t alloc; ///< Number of allocated entries
size_t len; ///< Number of entries in the table
void (*free) (void *); ///< Callback to destruct the payload
+
+ /// Callback to compare keys for equivalence
+ int (*key_cmp) (const char *, const char *);
};
// As long as you don't remove the current entry, you can modify the map.
@@ -593,6 +596,7 @@ str_map_init (struct str_map *self)
self->alloc = STR_MAP_MIN_ALLOC;
self->len = 0;
self->free = NULL;
+ self->key_cmp = strcmp;
self->map = xcalloc (self->alloc, sizeof *self->map);
}
@@ -692,7 +696,7 @@ str_map_set (struct str_map *self, const char *key, void *value)
struct str_map_link *iter = self->map[pos];
for (; iter; iter = iter->next)
{
- if (strcmp (key, iter->key))
+ if (self->key_cmp (key, iter->key))
continue;
// Storing the same data doesn't destroy it
@@ -741,7 +745,7 @@ str_map_find (struct str_map *self, const char *key)
{
struct str_map_link *iter = self->map[str_map_pos (self, key)];
for (; iter; iter = iter->next)
- if (!strcmp (key, (char *) iter + sizeof *iter))
+ if (!self->key_cmp (key, (const char *) iter + sizeof *iter))
return iter->data;
return NULL;
}
--
cgit v1.2.3-70-g09d2