summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libappstream-glib/as-app.h2
-rw-r--r--libappstream-glib/as-self-test.c37
-rw-r--r--libappstream-glib/as-utils.c71
-rw-r--r--libappstream-glib/as-utils.h2
4 files changed, 111 insertions, 1 deletions
diff --git a/libappstream-glib/as-app.h b/libappstream-glib/as-app.h
index 4949c21..afb3417 100644
--- a/libappstream-glib/as-app.h
+++ b/libappstream-glib/as-app.h
@@ -240,7 +240,7 @@ GPtrArray *as_app_get_vetos (AsApp *app);
const gchar *as_app_get_icon (AsApp *app);
const gchar *as_app_get_icon_path (AsApp *app);
const gchar *as_app_get_id_filename (AsApp *app);
-const gchar *as_app_get_id (AsApp *app);
+const gchar *as_app_get_id (AsApp *app);
const gchar *as_app_get_pkgname_default (AsApp *app);
const gchar *as_app_get_source_pkgname (AsApp *app);
const gchar *as_app_get_project_group (AsApp *app);
diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c
index 281c0bf..86e584b 100644
--- a/libappstream-glib/as-self-test.c
+++ b/libappstream-glib/as-self-test.c
@@ -2148,6 +2148,42 @@ as_test_store_speed_desktop_func (void)
}
static void
+as_test_utils_overlap_func (void)
+{
+ gchar *tmp;
+
+ /* same */
+ tmp = as_utils_get_string_overlap ("dave.ttf", "dave.ttf");
+ g_assert_cmpstr (tmp, ==, "dave.ttf");
+ g_free (tmp);
+
+ /* only prefix */
+ tmp = as_utils_get_string_overlap ("dave.ttf", "daniel.doc");
+ g_assert_cmpstr (tmp, ==, "da");
+ g_free (tmp);
+
+ /* only suffix */
+ tmp = as_utils_get_string_overlap ("dave.ttf", "sara.ttf");
+ g_assert_cmpstr (tmp, ==, ".ttf");
+ g_free (tmp);
+
+ /* different */
+ tmp = as_utils_get_string_overlap ("dave.ttf", "bob.doc");
+ g_assert_cmpstr (tmp, ==, NULL);
+ g_free (tmp);
+
+ /* long left */
+ tmp = as_utils_get_string_overlap ("aaabbbccc.ttf", "bbbccc.ttf");
+ g_assert_cmpstr (tmp, ==, "bbbccc.ttf");
+ g_free (tmp);
+
+ /* long right */
+ tmp = as_utils_get_string_overlap ("bbbccc.ttf", "aaabbbccc.ttf");
+ g_assert_cmpstr (tmp, ==, "bbbccc.ttf");
+ g_free (tmp);
+}
+
+static void
as_test_utils_icons_func (void)
{
gchar *tmp;
@@ -2700,6 +2736,7 @@ main (int argc, char **argv)
g_test_add_func ("/AppStream/node{intltool}", as_test_node_intltool_func);
g_test_add_func ("/AppStream/node{sort}", as_test_node_sort_func);
g_test_add_func ("/AppStream/utils", as_test_utils_func);
+ g_test_add_func ("/AppStream/utils{overlap}", as_test_utils_overlap_func);
g_test_add_func ("/AppStream/utils{icons}", as_test_utils_icons_func);
g_test_add_func ("/AppStream/utils{spdx-token}", as_test_utils_spdx_token_func);
g_test_add_func ("/AppStream/yaml", as_test_yaml_func);
diff --git a/libappstream-glib/as-utils.c b/libappstream-glib/as-utils.c
index 44f6d30..2f5c8a4 100644
--- a/libappstream-glib/as-utils.c
+++ b/libappstream-glib/as-utils.c
@@ -904,3 +904,74 @@ as_utils_find_icon_filename (const gchar *destdir,
"Failed to find icon %s", search);
return NULL;
}
+
+/**
+ * as_utils_get_string_overlap_prefix:
+ */
+static gchar *
+as_utils_get_string_overlap_prefix (const gchar *s1, const gchar *s2)
+{
+ guint i;
+ for (i = 0; s1[i] != '\0' && s2[i] != '\0'; i++) {
+ if (s1[i] != s2[i])
+ break;
+ }
+ if (i == 0)
+ return NULL;
+ if (s1[i - 1] == '-')
+ i--;
+ return g_strndup (s1, i);
+}
+
+/**
+ * as_utils_get_string_overlap_suffix:
+ */
+static gchar *
+as_utils_get_string_overlap_suffix (const gchar *s1, const gchar *s2)
+{
+ guint i;
+ guint len1 = strlen (s1);
+ guint len2 = strlen (s2);
+ for (i = 0; i <= len1 && i <= len2; i++) {
+ if (s1[len1 - i] != s2[len2 - i])
+ break;
+ }
+ if (i <= 1)
+ return NULL;
+ return g_strdup (&s1[len1 - i + 1]);
+}
+
+/**
+ * as_utils_get_string_overlap:
+ * @s1: A string.
+ * @s2: Another string
+ *
+ * Return a prefix and sufffix that is common to both strings.
+ *
+ * Returns: (transfer full): a newly allocated %NULL terminated string, or %NULL
+ *
+ * Since: 0.3.1
+ */
+gchar *
+as_utils_get_string_overlap (const gchar *s1, const gchar *s2)
+{
+ _cleanup_free_ gchar *prefix = NULL;
+ _cleanup_free_ gchar *suffix = NULL;
+
+ g_return_val_if_fail (s1 != NULL, NULL);
+ g_return_val_if_fail (s2 != NULL, NULL);
+
+ /* same? */
+ if (g_strcmp0 (s1, s2) == 0)
+ return g_strdup (s1);
+
+ prefix = as_utils_get_string_overlap_prefix (s1, s2);
+ suffix = as_utils_get_string_overlap_suffix (s1, s2);
+ if (prefix == NULL && suffix == NULL)
+ return NULL;
+ if (prefix != NULL && suffix == NULL)
+ return g_strdup (prefix);
+ if (prefix == NULL && suffix != NULL)
+ return g_strdup (suffix);
+ return g_strdup_printf ("%s%s", prefix, suffix);
+}
diff --git a/libappstream-glib/as-utils.h b/libappstream-glib/as-utils.h
index 2536ec0..283c99f 100644
--- a/libappstream-glib/as-utils.h
+++ b/libappstream-glib/as-utils.h
@@ -47,6 +47,8 @@ gboolean as_utils_check_url_exists (const gchar *url,
gchar *as_utils_find_icon_filename (const gchar *destdir,
const gchar *search,
GError **error);
+gchar *as_utils_get_string_overlap (const gchar *s1,
+ const gchar *s2);
G_END_DECLS