filter: Use string :: lowercase() for searching

This is a much simpler way of doing things.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-01-30 10:52:15 -05:00
parent 7e58a4fef8
commit 3f423fb3ae
2 changed files with 23 additions and 69 deletions

View File

@ -8,56 +8,9 @@
#include <core/string.h>
#include <algorithm>
#include <list>
static Index filter_index("", false);
static void parse_text(const std::string &text, std::list<std::string> &ret)
{
std::string word;
char c;
for (unsigned int i = 0; i < text.size(); i++) {
c = text[i];
if ( (c >= 'a') && (c <= 'z') ) {
word += c;
continue;
} else if ( (c >= 'A') && (c <= 'Z') ) {
word += (c + ('a' - 'A'));
continue;
} else if ( (c >= '0') && (c <= '9') ) {
word += c;
continue;
}
switch (c) {
case '\\':
case '/':
case ',':
case ';':
case '(':
case ')':
case '_':
case '-':
case '~':
case '+':
case '"':
case ' ':
case ' ':
if (word != "") {
ret.push_back(word);
word = "";
}
default:
break;
};
}
if (word != "")
ret.push_back(word);
}
const std::string filter :: add(const std::string &text, unsigned int index)
{
const std::string lc = string :: lowercase(text);
@ -77,37 +30,35 @@ static void do_set_intersection(IndexEntry *entry,
std::set<unsigned int> tmp;
set_intersection(entry->begin(), entry->end(), res.begin(), res.end(),
std::inserter<std::set<unsigned int> >(tmp, tmp.begin()));
std::inserter(tmp, tmp.begin()));
res.swap(tmp);
}
static void find_intersection(std::string &text, std::set<unsigned int> &res)
{
IndexEntry *it = filter_index.find(text);
if (it)
do_set_intersection(it, res);
else
res.clear();
}
void filter :: search(const std::string &text, std::set<unsigned int> &res)
{
std::list<std::string> parsed;
std::list<std::string>::iterator it;
const std::string lc = string :: lowercase(text);
size_t begin = 0, end;
IndexEntry *found;
res.clear();
parse_text(text, parsed);
if (parsed.size() == 0)
return;
for (end = 1; end <= lc.size(); end++) {
end = lc.find(' ', begin);
if (end == std::string::npos)
end = lc.size();
it = parsed.begin();
found = filter_index.find(*it);
if (!found)
return;
std::copy(found->begin(), found->end(), std::inserter(res, res.begin()));
found = filter_index.find(lc.substr(begin, end - begin));
if (!found) {
res.clear();
return;
}
for (it++; it != parsed.end(); it++)
find_intersection(*it, res);
if (begin == 0)
std::copy(found->begin(), found->end(),
std::inserter(res, res.begin()));
else
do_set_intersection(found, res);
begin = ++end;
}
}

View File

@ -83,6 +83,9 @@ static void test_search()
unsigned int res5[] = {};
do_test_search("unknown terms", 0, res5);
do_test_search("the chestZ", 0, res5);
unsigned int res6[] = {0, 1, 8};
do_test_search("d", 3, res6);
}
int main(int argc, char **argv)