summaryrefslogtreecommitdiff
path: root/src/word_db.cc
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2015-10-22 19:49:08 +0100
committerMaxime Coste <frrrwww@gmail.com>2015-10-22 19:49:08 +0100
commitc77cb7c777ffc6f23ad53003d3531c1957fcdd0b (patch)
tree2f4454188d84c1a6ed20169ad2b6b5f62b384e80 /src/word_db.cc
parent019b3235b0e7aaad086631e1ee9b3102f096d349 (diff)
Extract WordDB::RankedWord as RankedMatch in its own file
Diffstat (limited to 'src/word_db.cc')
-rw-r--r--src/word_db.cc44
1 files changed, 5 insertions, 39 deletions
diff --git a/src/word_db.cc b/src/word_db.cc
index c1c8f967..80b59f0c 100644
--- a/src/word_db.cc
+++ b/src/word_db.cc
@@ -146,42 +146,8 @@ int WordDB::get_word_occurences(StringView word) const
return 0;
}
-WordDB::RankedWordList WordDB::find_matching(StringView query)
+RankedMatchList WordDB::find_matching(StringView query)
{
- auto match_rank = [](StringView candidate, StringView query)
- {
- int rank = 0;
- auto it = candidate.begin();
- char prev = 0;
- for (auto c : query)
- {
- if (it == candidate.end())
- return 0;
-
- const bool islow = islower(c);
- auto eq_c = [islow, c](char ch) { return islow ? tolower(ch) == c : ch == c; };
-
- if (eq_c(*it)) // improve rank on contiguous
- ++rank;
-
- while (!eq_c(*it))
- {
- prev = *it;
- if (++it == candidate.end())
- return 0;
- }
- // Improve rank on word boundaries
- if (prev == 0 or prev == '_' or
- (islower(prev) and isupper(*it)))
- rank += 5;
-
- prev = c;
- ++rank;
- ++it;
- }
- return rank;
- };
-
auto matches = [](UsedLetters query, UsedLetters letters)
{
return (query & letters) == query;
@@ -189,7 +155,7 @@ WordDB::RankedWordList WordDB::find_matching(StringView query)
update_db();
const UsedLetters letters = used_letters(query);
- RankedWordList res;
+ RankedMatchList res;
for (auto&& word : m_words)
{
if (query.empty())
@@ -211,14 +177,14 @@ WordDB::RankedWordList WordDB::find_matching(StringView query)
UnitTest test_word_db{[]()
{
- auto cmp_words = [](const WordDB::RankedWord& lhs, const WordDB::RankedWord& rhs) {
+ auto cmp_words = [](const RankedMatch& lhs, const RankedMatch& rhs) {
return lhs.word < rhs.word;
};
- auto eq = [](ArrayView<const WordDB::RankedWord> lhs, const WordList& rhs) {
+ auto eq = [](ArrayView<const RankedMatch> lhs, const WordList& rhs) {
return lhs.size() == rhs.size() and
std::equal(lhs.begin(), lhs.end(), rhs.begin(),
- [](const WordDB::RankedWord& lhs, const StringView& rhs) {
+ [](const RankedMatch& lhs, const StringView& rhs) {
return lhs.word == rhs;
});
};