ocarina/core/filter.c

70 lines
1.4 KiB
C

/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <core/containers/index.h>
#include <core/filter.h>
#include <core/string.h>
static struct database filter_index;
void filter_init() { index_init(&filter_index, "", false); }
void filter_deinit() { db_deinit(&filter_index); }
void filter_add(const gchar *text, unsigned int index)
{
const gchar *c = g_utf8_next_char(text);
glong begin, end;
gchar *substr;
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);
if (g_unichar_isspace(g_utf8_get_char(c))) {
c = g_utf8_next_char(c);
begin = ++end;
}
c = g_utf8_next_char(c);
g_free(substr);
}
}
void filter_search(const gchar *text, struct set *res)
{
gchar *lower = string_lowercase(text);
struct index_entry *found;
glong begin, end;
gchar *c, *substr;
set_clear(res);
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;
substr = g_utf8_substring(lower, begin, end);
found = INDEX_ENTRY(db_get(&filter_index, substr));
g_free(substr);
if (!found) {
set_clear(res);
break;
}
if (begin == 0)
set_copy(&found->ie_set, res);
else
set_inline_intersect(&found->ie_set, res);
c = g_utf8_next_char(c);
begin = ++end;
}
g_free(lower);
}