From 3f423fb3ae27e40e70e1f998018bb70dfffbe050 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 30 Jan 2015 10:52:15 -0500 Subject: [PATCH] filter: Use string :: lowercase() for searching This is a much simpler way of doing things. Signed-off-by: Anna Schumaker --- core/filter.cpp | 89 ++++++++++--------------------------------- tests/core/filter.cpp | 3 ++ 2 files changed, 23 insertions(+), 69 deletions(-) diff --git a/core/filter.cpp b/core/filter.cpp index a60c81ae..2d1977ee 100644 --- a/core/filter.cpp +++ b/core/filter.cpp @@ -8,56 +8,9 @@ #include #include -#include static Index filter_index("", false); -static void parse_text(const std::string &text, std::list &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 tmp; set_intersection(entry->begin(), entry->end(), res.begin(), res.end(), - std::inserter >(tmp, tmp.begin())); + std::inserter(tmp, tmp.begin())); res.swap(tmp); } -static void find_intersection(std::string &text, std::set &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 &res) { - std::list parsed; - std::list::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; + } } diff --git a/tests/core/filter.cpp b/tests/core/filter.cpp index 5852d19a..645a865d 100644 --- a/tests/core/filter.cpp +++ b/tests/core/filter.cpp @@ -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)