2014-09-16 08:09:27 -04:00
|
|
|
/**
|
2013-08-25 10:33:48 -04:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/filter.h>
|
2015-09-08 09:39:35 -04:00
|
|
|
|
|
|
|
extern "C" {
|
2015-11-07 17:02:06 -05:00
|
|
|
#include <core/index.h>
|
2015-01-30 10:03:18 -05:00
|
|
|
#include <core/string.h>
|
2015-09-08 09:39:35 -04:00
|
|
|
}
|
2013-08-25 10:33:48 -04:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
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-10-21 15:57:21 -04:00
|
|
|
void filter :: search(const std::string &text, struct set *res)
|
2013-08-25 10:33:48 -04:00
|
|
|
{
|
2015-09-08 09:14:39 -04:00
|
|
|
gchar *g_lc = string_lowercase(text.c_str());
|
|
|
|
const std::string lc = g_lc;
|
2015-01-30 10:52:15 -05:00
|
|
|
size_t begin = 0, end;
|
2015-10-21 10:41:42 -04:00
|
|
|
index_entry *found;
|
2013-08-25 10:33:48 -04:00
|
|
|
|
2015-09-08 09:14:39 -04:00
|
|
|
g_free(g_lc);
|
2015-10-21 15:57:21 -04:00
|
|
|
set_clear(res);
|
2014-11-06 08:24:13 -05:00
|
|
|
|
2015-01-30 10:52:15 -05:00
|
|
|
for (end = 1; end <= lc.size(); end++) {
|
|
|
|
end = lc.find(' ', begin);
|
|
|
|
if (end == std::string::npos)
|
|
|
|
end = lc.size();
|
|
|
|
|
2015-11-04 12:23:00 -05:00
|
|
|
found = INDEX_ENTRY(db_get(&filter_index,
|
|
|
|
lc.substr(begin, end- begin).c_str()));
|
2015-01-30 10:52:15 -05:00
|
|
|
if (!found) {
|
2015-10-21 15:57:21 -04:00
|
|
|
set_clear(res);
|
2015-01-30 10:52:15 -05:00
|
|
|
return;
|
|
|
|
}
|
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-01-30 10:52:15 -05:00
|
|
|
begin = ++end;
|
|
|
|
}
|
2013-08-25 10:33:48 -04:00
|
|
|
}
|