ocarina/core/filter.cpp

67 lines
1.3 KiB
C++

/**
* Copyright 2013 (c) Anna Schumaker.
*/
#include <core/filter.h>
#include <core/index.h>
extern "C" {
#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); }
const std::string filter :: add(const std::string &text, unsigned int index)
{
gchar *g_lc = string_lowercase(text.c_str());
const std::string lc = g_lc;
size_t begin = 0, end;
g_free(g_lc);
for (end = 1; end <= lc.size(); end++) {
index_insert(&filter_index,
lc.substr(begin, end - begin).c_str(),
index);
if (lc[end] == ' ')
begin = ++end;
}
return lc;
}
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;
}
}