From 7e58a4fef898ea50e03aec329f96d701168730f5 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 30 Jan 2015 10:03:18 -0500 Subject: [PATCH] filter: Simplify adding words to the filter I can use the lowercase text from string :: lowercase() and take a single pass over a single string (rather than iterating through a list of strings). Signed-off-by: Anna Schumaker --- core/filter.cpp | 41 ++++++++++------------------------------- include/core/filter.h | 2 +- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/core/filter.cpp b/core/filter.cpp index a10c9d1c..a60c81ae 100644 --- a/core/filter.cpp +++ b/core/filter.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include #include @@ -58,38 +58,17 @@ static void parse_text(const std::string &text, std::list &ret) ret.push_back(word); } -static void add_substrings(const std::string &text, unsigned int index) +const std::string filter :: add(const std::string &text, unsigned int index) { - std::string substr; - for (unsigned int i = 1; i <= text.size(); i++) { - substr = text.substr(0, i); - filter_index.insert(substr, index); + const std::string lc = string :: lowercase(text); + size_t begin = 0, end; + + for (end = 1; end <= lc.size(); end++) { + filter_index.insert(lc.substr(begin, end - begin), index); + if (lc[end] == ' ') + begin = ++end; } -} - -static std::string reassemble_text(std::list text) -{ - std::string res; - std::list::iterator it = text.begin(); - - if (it == text.end()) - return res; - - res += *it; - for (it++; it != text.end(); it++) - res += " " + *it; - return res; -} - -std::string filter :: add(const std::string &text, unsigned int index) -{ - std::list parsed; - std::list::iterator it; - - parse_text(text, parsed); - for (it = parsed.begin(); it != parsed.end(); it++) - add_substrings(*it, index); - return reassemble_text(parsed); + return lc; } static void do_set_intersection(IndexEntry *entry, diff --git a/include/core/filter.h b/include/core/filter.h index 3a195758..bf180a08 100644 --- a/include/core/filter.h +++ b/include/core/filter.h @@ -35,7 +35,7 @@ namespace filter { * @param index The track index to pair with the text. * @return The lowercase form of the input text. */ - std::string add(const std::string &, unsigned int); + const std::string add(const std::string &, unsigned int); /** * Break the input text into lowercase words and search the Index