ocarina/core/filter.cpp

69 lines
1.3 KiB
C++
Raw Normal View History

/**
* Copyright 2013 (c) Anna Schumaker.
*/
#include <core/filter.h>
extern "C" {
#include <core/index.h>
#include <core/string.h>
}
#include <algorithm>
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 std::string &text, struct set *res)
{
gchar *g_lc = string_lowercase(text.c_str());
const std::string lc = g_lc;
size_t begin = 0, end;
index_entry *found;
g_free(g_lc);
set_clear(res);
for (end = 1; end <= lc.size(); end++) {
end = lc.find(' ', begin);
if (end == std::string::npos)
end = lc.size();
found = INDEX_ENTRY(db_get(&filter_index,
lc.substr(begin, end- begin).c_str()));
if (!found) {
set_clear(res);
return;
}
if (begin == 0)
set_copy(&found->ie_set, res);
else
set_inline_intersect(&found->ie_set, res);
begin = ++end;
}
}