core/filter: Move filter_search() out of the filter namespace
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
90afcd6065
commit
f2664aa8f2
|
@ -36,26 +36,28 @@ void filter_add(const gchar *text, unsigned int index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void filter :: search(const std::string &text, struct set *res)
|
void filter_search(const gchar *text, struct set *res)
|
||||||
{
|
{
|
||||||
gchar *g_lc = string_lowercase(text.c_str());
|
gchar *lower = string_lowercase(text);
|
||||||
const std::string lc = g_lc;
|
struct index_entry *found;
|
||||||
size_t begin = 0, end;
|
glong begin, end;
|
||||||
index_entry *found;
|
gchar *c, *substr;
|
||||||
|
|
||||||
g_free(g_lc);
|
|
||||||
set_clear(res);
|
set_clear(res);
|
||||||
|
c = lower;
|
||||||
|
|
||||||
for (end = 1; end <= lc.size(); end++) {
|
for (begin = 0, end = 1; end <= g_utf8_strlen(lower, -1); end++) {
|
||||||
end = lc.find(' ', begin);
|
c = g_utf8_next_char(c);
|
||||||
if (end == std::string::npos)
|
if ((*c != '\0') && !g_unichar_isspace(g_utf8_get_char(c)))
|
||||||
end = lc.size();
|
continue;
|
||||||
|
|
||||||
|
substr = g_utf8_substring(lower, begin, end);
|
||||||
|
found = INDEX_ENTRY(db_get(&filter_index, substr));
|
||||||
|
g_free(substr);
|
||||||
|
|
||||||
found = INDEX_ENTRY(db_get(&filter_index,
|
|
||||||
lc.substr(begin, end- begin).c_str()));
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
set_clear(res);
|
set_clear(res);
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (begin == 0)
|
if (begin == 0)
|
||||||
|
@ -63,6 +65,9 @@ void filter :: search(const std::string &text, struct set *res)
|
||||||
else
|
else
|
||||||
set_inline_intersect(&found->ie_set, res);
|
set_inline_intersect(&found->ie_set, res);
|
||||||
|
|
||||||
|
c = g_utf8_next_char(c);
|
||||||
begin = ++end;
|
begin = ++end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g_free(lower);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ void QueueWindow :: filter(std::string &text)
|
||||||
{
|
{
|
||||||
_q_search_empty = (text.find_first_not_of(" \t") == std::string::npos);
|
_q_search_empty = (text.find_first_not_of(" \t") == std::string::npos);
|
||||||
if (!_q_search_empty)
|
if (!_q_search_empty)
|
||||||
filter :: search(text, &_q_search_res);
|
filter_search(text.c_str(), &_q_search_res);
|
||||||
q_filter->refilter();
|
q_filter->refilter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,17 +23,6 @@ extern "C" {
|
||||||
*/
|
*/
|
||||||
namespace filter {
|
namespace filter {
|
||||||
|
|
||||||
/**
|
|
||||||
* Break the input text into lowercase words and search the Index
|
|
||||||
* for matches. The results set should be filled out with the
|
|
||||||
* intersection of the IndexEntry for each word. If any word does
|
|
||||||
* not appear in the index, then the result set should be empty.
|
|
||||||
*
|
|
||||||
* @param text The text to search for.
|
|
||||||
* @param res The results set to fill in with matching indexes.
|
|
||||||
*/
|
|
||||||
void search(const std::string &, struct set *);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the input text to lowercase and returns the result.
|
* Converts the input text to lowercase and returns the result.
|
||||||
*
|
*
|
||||||
|
@ -50,4 +39,7 @@ void filter_deinit();
|
||||||
/* Add the input string to the index. */
|
/* Add the input string to the index. */
|
||||||
void filter_add(const gchar *, unsigned int);
|
void filter_add(const gchar *, unsigned int);
|
||||||
|
|
||||||
|
/* Search for the input string in the index. */
|
||||||
|
void filter_search(const gchar *, struct set *);
|
||||||
|
|
||||||
#endif /* OCARINA_CORE_FILTER_H */
|
#endif /* OCARINA_CORE_FILTER_H */
|
||||||
|
|
|
@ -34,35 +34,35 @@ static void test_filter()
|
||||||
} test_loop_passed();
|
} test_loop_passed();
|
||||||
|
|
||||||
/* Search for a word! */
|
/* Search for a word! */
|
||||||
filter :: search("hyrule", &res);
|
filter_search("hyrule", &res);
|
||||||
test_equal(set_size(&res), 3);
|
test_equal(set_size(&res), 3);
|
||||||
test_equal(set_has(&res, 1), (bool)true); /* hyrule symphony */
|
test_equal(set_has(&res, 1), (bool)true); /* hyrule symphony */
|
||||||
test_equal(set_has(&res, 3), (bool)true); /* hyrule field */
|
test_equal(set_has(&res, 3), (bool)true); /* hyrule field */
|
||||||
test_equal(set_has(&res, 4), (bool)true); /* hyrule castle */
|
test_equal(set_has(&res, 4), (bool)true); /* hyrule castle */
|
||||||
|
|
||||||
/* A second search should clear the set. */
|
/* A second search should clear the set. */
|
||||||
filter :: search("zelda", &res);
|
filter_search("zelda", &res);
|
||||||
test_equal(set_size(&res), 2);
|
test_equal(set_size(&res), 2);
|
||||||
test_equal(set_has(&res, 11), (bool)true); /* princess zelda */
|
test_equal(set_has(&res, 11), (bool)true); /* princess zelda */
|
||||||
test_equal(set_has(&res, 13), (bool)true); /* the legend of zelda medley */
|
test_equal(set_has(&res, 13), (bool)true); /* the legend of zelda medley */
|
||||||
|
|
||||||
/* Partial word search. */
|
/* Partial word search. */
|
||||||
filter :: search("ko", &res);
|
filter_search("ko", &res);
|
||||||
test_equal(set_size(&res), 2);
|
test_equal(set_size(&res), 2);
|
||||||
test_equal(set_has(&res, 0), (bool)true); /* koji kondo */
|
test_equal(set_has(&res, 0), (bool)true); /* koji kondo */
|
||||||
test_equal(set_has(&res, 2), (bool)true); /* kokiri forest */
|
test_equal(set_has(&res, 2), (bool)true); /* kokiri forest */
|
||||||
|
|
||||||
/* Multiple word search. */
|
/* Multiple word search. */
|
||||||
filter :: search("hyrule field", &res);
|
filter_search("hyrule field", &res);
|
||||||
test_equal(set_size(&res), 1);
|
test_equal(set_size(&res), 1);
|
||||||
test_equal(set_has(&res, 3), (bool)true); /* hyrule field */
|
test_equal(set_has(&res, 3), (bool)true); /* hyrule field */
|
||||||
|
|
||||||
/* Search for unknown word. */
|
/* Search for unknown word. */
|
||||||
filter :: search("field termina", &res);
|
filter_search("field termina", &res);
|
||||||
test_equal(set_size(&res), 0);
|
test_equal(set_size(&res), 0);
|
||||||
|
|
||||||
/* Search for empty string. */
|
/* Search for empty string. */
|
||||||
filter :: search("", &res);
|
filter_search("", &res);
|
||||||
test_equal(set_size(&res), 0);
|
test_equal(set_size(&res), 0);
|
||||||
|
|
||||||
filter_deinit();
|
filter_deinit();
|
||||||
|
|
|
@ -144,20 +144,22 @@ static void test_track_filter()
|
||||||
{
|
{
|
||||||
struct set search = SET_INIT();
|
struct set search = SET_INIT();
|
||||||
|
|
||||||
filter :: search("Title Theme", &search);
|
filter_search("Title Theme", &search);
|
||||||
test_equal(set_size(&search), (size_t)1);
|
test_equal(set_size(&search), (size_t)1);
|
||||||
test_equal(set_has(&search, 0), true);
|
test_equal(set_has(&search, 0), true);
|
||||||
|
|
||||||
filter :: search("Koji Kondo", &search);
|
filter_search("Koji Kondo", &search);
|
||||||
test_equal(set_size(&search), (size_t)1);
|
test_equal(set_size(&search), (size_t)1);
|
||||||
test_equal(set_has(&search, 0), true);
|
test_equal(set_has(&search, 0), true);
|
||||||
|
|
||||||
filter :: search("Hyrule Symphony", &search);
|
filter_search("Hyrule Symphony", &search);
|
||||||
test_equal(set_size(&search), (size_t)1);
|
test_equal(set_size(&search), (size_t)1);
|
||||||
test_equal(set_has(&search, 0), true);
|
test_equal(set_has(&search, 0), true);
|
||||||
|
|
||||||
filter :: search("No Track", &search);
|
filter_search("No Track", &search);
|
||||||
test_equal(set_size(&search), (size_t)0);
|
test_equal(set_size(&search), (size_t)0);
|
||||||
|
|
||||||
|
set_deinit(&search);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_track_compare()
|
static void test_track_compare()
|
||||||
|
|
Loading…
Reference in New Issue