summaryrefslogtreecommitdiff
path: root/src/libtracker-sparql/core/tracker-collation.c
blob: 0e82d66dda9b0413485db3964d2849ac8269a962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "config.h"

#include <glib.h>
#include <glib/gi18n.h>

#include "tracker-collation.h"

static gboolean
skip_non_alphanumeric (const gchar **str,
                       gint         *len)
{
	const gchar *remaining = *str, *end = &remaining[*len];
	gboolean found = FALSE, is_alnum;
	gunichar unichar;

	while (remaining < end) {
		unichar = g_utf8_get_char (remaining);
		is_alnum = g_unichar_isalnum (unichar);
		if (is_alnum)
			break;

		found = TRUE;
		remaining = g_utf8_next_char (remaining);
	}

	/* The string must not be left empty */
	if (remaining == end)
		return FALSE;

	if (found) {
		*len = end - remaining;
		*str = remaining;
	}

	return found;
}

static gboolean
check_remove_prefix (const gchar  *str,
                     gint          len,
                     const gchar  *prefix,
                     gint          prefix_len,
                     const gchar **str_out,
                     gint         *len_out)
{
	const gchar *remaining;
	gchar *strstart;
	gint remaining_len;

	if (len <= prefix_len)
		return FALSE;

	/* Check that the prefix matches */
	strstart = g_utf8_casefold (str, prefix_len);
	if (strcmp (strstart, prefix) != 0) {
		g_free (strstart);
		return FALSE;
	}

	/* Check that the following letter is a break
	 * character.
	 */
	g_free (strstart);
	remaining = &str[prefix_len];
	remaining_len = len - prefix_len;

	if (!skip_non_alphanumeric (&remaining, &remaining_len))
		return FALSE;

	*len_out = remaining_len;
	*str_out = remaining;
	return TRUE;
}

/* Helper function valid for all implementations */
gint
tracker_collation_utf8_title (gpointer      collator,
                              gint          len1,
                              gconstpointer str1,
                              gint          len2,
                              gconstpointer str2)
{
	const gchar *title_beginnings_str;
	static gchar **title_beginnings = NULL;
	const gchar *res1 = NULL, *res2 = NULL;
	gint i;

	skip_non_alphanumeric ((const gchar **) &str1, &len1);
	skip_non_alphanumeric ((const gchar **) &str2, &len2);

	/* Translators: this is a '|' (U+007C) separated list of common
	 * title beginnings. Meant to be skipped for sorting purposes,
	 * case doesn't matter. Given English media is quite common, it is
	 * advised to leave the untranslated articles in addition to
	 * the translated ones.
	 */
	title_beginnings_str = N_("the|a|an");

	if (!title_beginnings)
		title_beginnings = g_strsplit (_(title_beginnings_str), "|", -1);

	for (i = 0; title_beginnings[i]; i++) {
		gchar *prefix;
		gint prefix_len;

		prefix = g_utf8_casefold (title_beginnings[i], -1);
		prefix_len = strlen (prefix);

		if (!res1)
			check_remove_prefix (str1, len1, prefix, prefix_len,
			                     &res1, &len1);
		if (!res2)
			check_remove_prefix (str2, len2, prefix, prefix_len,
			                     &res2, &len2);
		g_free (prefix);
	}

	if (!res1)
		res1 = str1;
	if (!res2)
		res2 = str2;

	return tracker_collation_utf8 (collator, len1, res1, len2, res2);
}