From 90afcd6065e687226d41dc312419c11f59bbe843 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 10 Nov 2015 09:36:38 -0500 Subject: [PATCH] core/filter: Move filter_add() out of the filter namespace Signed-off-by: Anna Schumaker --- core/filter.cpp | 24 +++++++++++++----------- core/tags/track.cpp | 6 +++--- include/core/filter.h | 19 +++---------------- tests/core/filter.cpp | 2 +- 4 files changed, 20 insertions(+), 31 deletions(-) diff --git a/core/filter.cpp b/core/filter.cpp index 11aedb33..5d254f69 100644 --- a/core/filter.cpp +++ b/core/filter.cpp @@ -16,22 +16,24 @@ static struct database filter_index; void filter_init() { index_init(&filter_index, "", false); } void filter_deinit() { db_deinit(&filter_index); } -const std::string filter :: add(const std::string &text, unsigned int index) +void filter_add(const gchar *text, unsigned int index) { - gchar *g_lc = string_lowercase(text.c_str()); - const std::string lc = g_lc; - size_t begin = 0, end; + const gchar *c = g_utf8_next_char(text); + glong begin, end; + gchar *substr; - g_free(g_lc); + for (begin = 0, end = 1; end <= g_utf8_strlen(text, -1); end++) { + substr = g_utf8_substring(text, begin, end); + index_insert(&filter_index, substr, index); - for (end = 1; end <= lc.size(); end++) { - index_insert(&filter_index, - lc.substr(begin, end - begin).c_str(), - index); - if (lc[end] == ' ') + if (g_unichar_isspace(g_utf8_get_char(c))) { + c = g_utf8_next_char(c); begin = ++end; + } + + c = g_utf8_next_char(c); + g_free(substr); } - return lc; } void filter :: search(const std::string &text, struct set *res) diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 39762bbc..9235e852 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -85,9 +85,9 @@ static void track_setup(struct db_entry *dbe) { struct track *track = (struct track *)dbe; - filter :: add(track->tr_title, track->dbe_index); - filter :: add(track->tr_artist->ar_name, track->dbe_index); - filter :: add(track->tr_album->al_name, track->dbe_index); + filter_add(track->tr_lower.c_str(), track->dbe_index); + filter_add(track->tr_artist->ar_lower.c_str(), track->dbe_index); + filter_add(track->tr_album->al_lower.c_str(), track->dbe_index); track->tr_library->li_size++; } diff --git a/include/core/filter.h b/include/core/filter.h index 47f86df1..7a722fd1 100644 --- a/include/core/filter.h +++ b/include/core/filter.h @@ -23,22 +23,6 @@ extern "C" { */ namespace filter { - /** - * Convert the key to lowercase and break into individual words. - * Generate substrings for each word and add each (substring, index) - * pair to the Index. - * - * To generate substrings: iterate over the word starting from the - * first character, and append a character for every iteration. For - * example, the word "goron" would contain the substrings: - * { g, go, gor, goro, goron }. - * - * @param text The text to parse. - * @param index The track index to pair with the text. - * @return The lowercase form of the input text. - */ - const std::string add(const std::string &, unsigned int); - /** * Break the input text into lowercase words and search the Index * for matches. The results set should be filled out with the @@ -63,4 +47,7 @@ namespace filter { void filter_init(); void filter_deinit(); +/* Add the input string to the index. */ +void filter_add(const gchar *, unsigned int); + #endif /* OCARINA_CORE_FILTER_H */ diff --git a/tests/core/filter.cpp b/tests/core/filter.cpp index b1e4b8f8..60c7dd5c 100644 --- a/tests/core/filter.cpp +++ b/tests/core/filter.cpp @@ -30,7 +30,7 @@ static void test_filter() filter_init(); for (i = 0; i < NUM_STRINGS; i++) { - filter :: add(test_strings[i], i); + filter_add(test_strings[i], i); } test_loop_passed(); /* Search for a word! */