Commit f6802836 authored by Jürg Billeter's avatar Jürg Billeter

libtracker-common: Update tracker_filename_casecmp_without_extension

The function was refactored in tracker-0.12 branch. This merges the
changes back into master.
parent a5ceff37
......@@ -856,62 +856,55 @@ gboolean
tracker_filename_casecmp_without_extension (const gchar *a,
const gchar *b)
{
const gchar *ca = a;
const gchar *cb = b;
gboolean period_a = FALSE;
gboolean period_b = FALSE;
gboolean match = TRUE;
gchar *pa;
gchar *pb;
gint len_a;
gint len_b;
g_return_val_if_fail (a != NULL, FALSE);
g_return_val_if_fail (b != NULL, FALSE);
while (1) {
if (*ca == '\0' && *cb == '\0')
break;
pa = strrchr (a, '.');
pb = strrchr (b, '.');
if (*ca != '\0' && *cb != '\0') {
if (g_ascii_tolower (*ca) != g_ascii_tolower (*cb)) {
match = FALSE;
break;
}
}
if (*ca == '.')
period_a = TRUE;
if (*cb == '.')
period_b = TRUE;
if (*ca == '\0' || *cb == '\0') {
match = FALSE;
break;
}
ca ++; cb ++;
/* Did we find a "." */
if (pa) {
len_a = pa - a;
} else {
len_a = -1;
}
if (!match) {
/* If the mismatch was past the last '.' then forgive it. */
if (*ca != '\0' && period_a) {
match = TRUE;
if (pb) {
len_b = pb - b;
} else {
len_b = -1;
}
while (*(ca ++) != '\0') {
if (*ca == '.') {
match = FALSE;
}
}
}
/* If one has a "." and the other doesn't, we do length
* comparison with strlen() which is less optimal but this is
* not a case we consider common operation.
*/
if (len_a == -1 && len_b > -1) {
len_a = strlen (a);
} else if (len_b == -1 && len_a > -1) {
len_b = strlen (b);
}
if (*cb != '\0' && period_b) {
match = TRUE;
/* If we have length for both and it's different then these
* strings are not the same. If we have no length for the
* strings then it's a simple -1 != -1 comparison.
*/
if (len_a != len_b) {
return FALSE;
}
while (*(cb ++) != '\0') {
if (*cb == '.') {
match = FALSE;
}
}
}
/* Now we know we either have the same length string or no
* extension in a and b, meaning it's a strcmp() of the
* string only. We test only len_a or len_b here for that:
*/
if (G_UNLIKELY (len_a == -1)) {
return g_ascii_strcasecmp (a, b) == 0;
}
return match;
return g_ascii_strncasecmp (a, b, len_a) == 0;
}
......@@ -181,14 +181,13 @@ test_file_get_mime_type (void)
g_free (dir_name);
}
#define assert_filename_match(a, b) { \
g_assert (tracker_filename_casecmp_without_extension (a, b) == TRUE); \
g_assert (tracker_filename_casecmp_without_extension (b, a) == TRUE); }
g_assert_cmpint (tracker_filename_casecmp_without_extension (a, b), ==, TRUE); \
g_assert_cmpint (tracker_filename_casecmp_without_extension (b, a), ==, TRUE); }
#define assert_no_filename_match(a, b) { \
g_assert (tracker_filename_casecmp_without_extension (a, b) == FALSE); \
g_assert (tracker_filename_casecmp_without_extension (b, a) == FALSE); }
g_assert_cmpint (tracker_filename_casecmp_without_extension (a, b), ==, FALSE); \
g_assert_cmpint (tracker_filename_casecmp_without_extension (b, a), ==, FALSE); }
static void
test_case_match_filename_without_extension ()
......@@ -210,8 +209,6 @@ test_case_match_filename_without_extension ()
assert_filename_match ("", ".");
}
int
main (int argc, char **argv)
{
......@@ -224,14 +221,11 @@ main (int argc, char **argv)
g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/path_evaluate_name",
test_path_evaluate_name);
g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/path_list_filter_duplicates",
test_path_list_filter_duplicates);
g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/file_get_mime_type",
test_file_get_mime_type);
g_test_add_func ("/libtracker-common/tracker-utils/case_match_filename_without_extension",
g_test_add_func ("/tracker/libtracker-common/tracker-file-utils/case_match_filename_without_extension",
test_case_match_filename_without_extension);
result = g_test_run ();
......
......@@ -75,7 +75,6 @@ test_seconds_estimate_to_string ()
g_print ("%s\n", result);
}
int
main (int argc, char **argv)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment