2015-11-10 10:17:52 -05:00
|
|
|
/*
|
2013-08-25 10:33:48 -04:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
|
2015-11-30 10:01:39 -05:00
|
|
|
#include <core/containers/index.h>
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/filter.h>
|
2015-01-30 10:03:18 -05:00
|
|
|
#include <core/string.h>
|
2013-08-25 10:33:48 -04:00
|
|
|
|
|
|
|
|
2015-11-04 12:23:00 -05:00
|
|
|
static struct database filter_index;
|
2015-09-29 16:25:16 -04:00
|
|
|
|
|
|
|
void filter_init() { index_init(&filter_index, "", false); }
|
|
|
|
void filter_deinit() { db_deinit(&filter_index); }
|
2013-08-25 10:33:48 -04:00
|
|
|
|
2015-11-10 09:36:38 -05:00
|
|
|
void filter_add(const gchar *text, unsigned int index)
|
2013-08-25 10:33:48 -04:00
|
|
|
{
|
2015-11-10 09:36:38 -05:00
|
|
|
const gchar *c = g_utf8_next_char(text);
|
|
|
|
glong begin, end;
|
|
|
|
gchar *substr;
|
2014-03-15 20:44:07 -04:00
|
|
|
|
2015-11-10 09:36:38 -05:00
|
|
|
for (begin = 0, end = 1; end <= g_utf8_strlen(text, -1); end++) {
|
|
|
|
substr = g_utf8_substring(text, begin, end);
|
|
|
|
index_insert(&filter_index, substr, index);
|
2015-09-08 09:14:39 -04:00
|
|
|
|
2015-11-10 09:36:38 -05:00
|
|
|
if (g_unichar_isspace(g_utf8_get_char(c))) {
|
|
|
|
c = g_utf8_next_char(c);
|
2015-01-30 10:03:18 -05:00
|
|
|
begin = ++end;
|
2015-11-10 09:36:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
c = g_utf8_next_char(c);
|
|
|
|
g_free(substr);
|
2015-01-30 10:03:18 -05:00
|
|
|
}
|
2013-08-25 10:33:48 -04:00
|
|
|
}
|
|
|
|
|
2015-11-10 10:12:20 -05:00
|
|
|
void filter_search(const gchar *text, struct set *res)
|
2013-08-25 10:33:48 -04:00
|
|
|
{
|
2015-11-10 10:12:20 -05:00
|
|
|
gchar *lower = string_lowercase(text);
|
|
|
|
struct index_entry *found;
|
|
|
|
glong begin, end;
|
|
|
|
gchar *c, *substr;
|
2013-08-25 10:33:48 -04:00
|
|
|
|
2015-10-21 15:57:21 -04:00
|
|
|
set_clear(res);
|
2015-11-10 10:12:20 -05:00
|
|
|
c = lower;
|
|
|
|
|
|
|
|
for (begin = 0, end = 1; end <= g_utf8_strlen(lower, -1); end++) {
|
|
|
|
c = g_utf8_next_char(c);
|
|
|
|
if ((*c != '\0') && !g_unichar_isspace(g_utf8_get_char(c)))
|
|
|
|
continue;
|
2014-11-06 08:24:13 -05:00
|
|
|
|
2015-11-10 10:12:20 -05:00
|
|
|
substr = g_utf8_substring(lower, begin, end);
|
|
|
|
found = INDEX_ENTRY(db_get(&filter_index, substr));
|
|
|
|
g_free(substr);
|
2015-01-30 10:52:15 -05:00
|
|
|
|
|
|
|
if (!found) {
|
2015-10-21 15:57:21 -04:00
|
|
|
set_clear(res);
|
2015-11-10 10:12:20 -05:00
|
|
|
break;
|
2015-01-30 10:52:15 -05:00
|
|
|
}
|
2013-08-25 10:33:48 -04:00
|
|
|
|
2015-01-30 10:52:15 -05:00
|
|
|
if (begin == 0)
|
2015-10-21 15:57:21 -04:00
|
|
|
set_copy(&found->ie_set, res);
|
2015-01-30 10:52:15 -05:00
|
|
|
else
|
2015-10-21 15:57:21 -04:00
|
|
|
set_inline_intersect(&found->ie_set, res);
|
2013-08-25 10:33:48 -04:00
|
|
|
|
2015-11-10 10:12:20 -05:00
|
|
|
c = g_utf8_next_char(c);
|
2015-01-30 10:52:15 -05:00
|
|
|
begin = ++end;
|
|
|
|
}
|
2015-11-10 10:12:20 -05:00
|
|
|
|
|
|
|
g_free(lower);
|
2013-08-25 10:33:48 -04:00
|
|
|
}
|