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 <Anna@OcarinaProject.net>
This commit is contained in:
parent
6c4dc5b1db
commit
7e58a4fef8
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
#include <core/filter.h>
|
#include <core/filter.h>
|
||||||
#include <core/index.h>
|
#include <core/index.h>
|
||||||
#include <core/print.h>
|
#include <core/string.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
@ -58,38 +58,17 @@ static void parse_text(const std::string &text, std::list<std::string> &ret)
|
||||||
ret.push_back(word);
|
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;
|
const std::string lc = string :: lowercase(text);
|
||||||
for (unsigned int i = 1; i <= text.size(); i++) {
|
size_t begin = 0, end;
|
||||||
substr = text.substr(0, i);
|
|
||||||
filter_index.insert(substr, index);
|
for (end = 1; end <= lc.size(); end++) {
|
||||||
|
filter_index.insert(lc.substr(begin, end - begin), index);
|
||||||
|
if (lc[end] == ' ')
|
||||||
|
begin = ++end;
|
||||||
}
|
}
|
||||||
}
|
return lc;
|
||||||
|
|
||||||
static std::string reassemble_text(std::list<std::string> text)
|
|
||||||
{
|
|
||||||
std::string res;
|
|
||||||
std::list<std::string>::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<std::string> parsed;
|
|
||||||
std::list<std::string>::iterator it;
|
|
||||||
|
|
||||||
parse_text(text, parsed);
|
|
||||||
for (it = parsed.begin(); it != parsed.end(); it++)
|
|
||||||
add_substrings(*it, index);
|
|
||||||
return reassemble_text(parsed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void do_set_intersection(IndexEntry *entry,
|
static void do_set_intersection(IndexEntry *entry,
|
||||||
|
|
|
@ -35,7 +35,7 @@ namespace filter {
|
||||||
* @param index The track index to pair with the text.
|
* @param index The track index to pair with the text.
|
||||||
* @return The lowercase form of the input 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
|
* Break the input text into lowercase words and search the Index
|
||||||
|
|
Loading…
Reference in New Issue