summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-utils.c
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2016-08-25 11:27:57 +0100
committerRichard Hughes <richard@hughsie.com>2016-08-25 19:21:54 +0100
commit67f486be685533602eba085fc4accdabda6be4bf (patch)
tree34a8e697f5ecf036b27b56c2b73e45f6712cf4e5 /libappstream-glib/as-utils.c
parente552f4245472e484dc8cb9e8930826e436d3225f (diff)
downloadappstream-glib-67f486be685533602eba085fc4accdabda6be4bf.tar.gz
Add a hash function specifically designed for unique-ids
Diffstat (limited to 'libappstream-glib/as-utils.c')
-rw-r--r--libappstream-glib/as-utils.c43
1 files changed, 42 insertions, 1 deletions
diff --git a/libappstream-glib/as-utils.c b/libappstream-glib/as-utils.c
index c959677..6ec25cf 100644
--- a/libappstream-glib/as-utils.c
+++ b/libappstream-glib/as-utils.c
@@ -1828,7 +1828,7 @@ as_utils_unique_id_equal (const gchar *unique_id1, const gchar *unique_id2)
/* invalid */
if (!as_utils_unique_id_valid (unique_id1) ||
!as_utils_unique_id_valid (unique_id2))
- return FALSE;
+ return g_strcmp0 (unique_id1, unique_id2) == 0;
/* look at each part */
for (i = 0; i < AS_UTILS_UNIQUE_ID_PARTS; i++) {
@@ -1855,3 +1855,44 @@ as_utils_unique_id_equal (const gchar *unique_id1, const gchar *unique_id2)
}
return TRUE;
}
+
+/**
+ * as_utils_unique_id_hash:
+ * @unique_id: a unique ID
+ *
+ * Converts a unique-id to a hash value.
+ *
+ * This function implements the widely used DJB hash on the ID subset of the
+ * unique-id string.
+ *
+ * It can be passed to g_hash_table_new() as the hash_func parameter,
+ * when using non-NULL strings or unique_ids as keys in a GHashTable.
+ *
+ * Returns: a hash value corresponding to the key
+ *
+ * Since: 0.6.2
+ */
+guint
+as_utils_unique_id_hash (const gchar *unique_id)
+{
+ gsize i;
+ guint hash = 5381;
+ guint section_cnt = 0;
+
+ /* not a unique ID */
+ if (!as_utils_unique_id_valid (unique_id))
+ return g_str_hash (unique_id);
+
+ /* only include the app-id */
+ for (i = 0; unique_id[i] != '\0'; i++) {
+ if (unique_id[i] == '/') {
+ if (++section_cnt > 4)
+ break;
+ continue;
+ }
+ if (section_cnt < 4)
+ continue;
+ hash = (guint) ((hash << 5) + hash) + (guint) (unique_id[i]);
+ }
+ return hash;
+}