2015-10-22 20:49:08 +02:00
|
|
|
#include "ranked_match.hh"
|
|
|
|
|
2015-10-30 14:57:46 +01:00
|
|
|
#include "utf8_iterator.hh"
|
2015-10-29 14:36:30 +01:00
|
|
|
#include "unit_tests.hh"
|
|
|
|
|
2015-10-22 20:49:08 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-10-30 14:57:46 +01:00
|
|
|
using Utf8It = utf8::iterator<const char*>;
|
|
|
|
|
2015-10-29 14:36:30 +01:00
|
|
|
static int count_word_boundaries_match(StringView candidate, StringView query)
|
2015-10-22 20:49:08 +02:00
|
|
|
{
|
2015-10-29 14:36:30 +01:00
|
|
|
int count = 0;
|
2016-02-18 00:05:08 +01:00
|
|
|
Utf8It query_it{query.begin(), query};
|
2015-10-30 14:57:46 +01:00
|
|
|
Codepoint prev = 0;
|
|
|
|
for (Utf8It it{candidate.begin(), candidate}; it != candidate.end(); ++it)
|
2015-10-22 20:49:08 +02:00
|
|
|
{
|
2015-10-30 14:57:46 +01:00
|
|
|
const Codepoint c = *it;
|
2015-10-29 14:36:30 +01:00
|
|
|
const bool is_word_boundary = prev == 0 or
|
2015-10-30 14:57:46 +01:00
|
|
|
(!iswalnum(prev) and iswalnum(c)) or
|
2015-10-29 14:36:30 +01:00
|
|
|
(islower(prev) and isupper(c));
|
|
|
|
prev = c;
|
2015-10-22 20:49:08 +02:00
|
|
|
|
2015-10-29 14:36:30 +01:00
|
|
|
if (not is_word_boundary)
|
|
|
|
continue;
|
2015-10-22 20:49:08 +02:00
|
|
|
|
2015-11-11 01:21:20 +01:00
|
|
|
const Codepoint lc = to_lower(c);
|
2016-02-18 00:05:08 +01:00
|
|
|
for (auto qit = query_it; qit != query.end(); ++qit)
|
2015-10-22 20:49:08 +02:00
|
|
|
{
|
2015-10-30 14:57:46 +01:00
|
|
|
const Codepoint qc = *qit;
|
2015-10-29 14:36:30 +01:00
|
|
|
if (qc == (islower(qc) ? lc : c))
|
|
|
|
{
|
|
|
|
++count;
|
2016-02-18 00:05:08 +01:00
|
|
|
query_it = qit+1;
|
2015-10-29 14:36:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-10-22 20:49:08 +02:00
|
|
|
}
|
2016-02-18 00:05:08 +01:00
|
|
|
if (query_it == query.end())
|
2015-10-29 14:36:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2015-10-22 20:49:08 +02:00
|
|
|
|
2015-10-30 14:57:46 +01:00
|
|
|
static bool smartcase_eq(Codepoint query, Codepoint candidate)
|
2015-10-29 14:36:30 +01:00
|
|
|
{
|
2015-11-11 01:21:20 +01:00
|
|
|
return query == (islower(query) ? to_lower(candidate) : candidate);
|
2015-10-29 14:36:30 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 00:05:51 +01:00
|
|
|
static bool subsequence_match_smart_case(StringView str, StringView subseq, int& index_sum)
|
2015-10-29 14:36:30 +01:00
|
|
|
{
|
2016-02-29 00:05:51 +01:00
|
|
|
index_sum = 0;
|
2016-03-25 00:45:56 +01:00
|
|
|
auto it = str.begin();
|
2016-02-29 00:05:51 +01:00
|
|
|
int index = 0;
|
2016-03-25 00:45:56 +01:00
|
|
|
for (auto subseq_it = subseq.begin(); subseq_it != subseq.end();
|
|
|
|
subseq_it = utf8::next(subseq_it, subseq.end()))
|
2015-10-29 14:36:30 +01:00
|
|
|
{
|
|
|
|
if (it == str.end())
|
|
|
|
return false;
|
2016-03-25 00:45:56 +01:00
|
|
|
const Codepoint c = utf8::codepoint(subseq_it, subseq.end());
|
|
|
|
while (not smartcase_eq(c, utf8::read_codepoint(it, subseq.end())))
|
2015-10-29 14:36:30 +01:00
|
|
|
{
|
2016-02-29 00:05:51 +01:00
|
|
|
++index;
|
2016-03-25 00:45:56 +01:00
|
|
|
if (it == str.end())
|
2015-10-29 14:36:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-29 00:05:51 +01:00
|
|
|
index_sum += index++;
|
2015-10-22 20:49:08 +02:00
|
|
|
}
|
2015-10-29 14:36:30 +01:00
|
|
|
return true;
|
2015-10-22 20:49:08 +02:00
|
|
|
}
|
|
|
|
|
2015-10-27 22:25:18 +01:00
|
|
|
RankedMatch::RankedMatch(StringView candidate, StringView query)
|
|
|
|
{
|
2015-10-29 14:36:30 +01:00
|
|
|
if (candidate.empty() or query.length() > candidate.length())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (query.empty())
|
2015-10-27 22:25:18 +01:00
|
|
|
{
|
|
|
|
m_candidate = candidate;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-29 00:05:51 +01:00
|
|
|
if (not subsequence_match_smart_case(candidate, query, m_match_index_sum))
|
2015-10-29 14:36:30 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_candidate = candidate;
|
|
|
|
|
|
|
|
m_first_char_match = smartcase_eq(query[0], candidate[0]);
|
|
|
|
m_word_boundary_match_count = count_word_boundaries_match(candidate, query);
|
|
|
|
m_only_word_boundary = m_word_boundary_match_count == query.length();
|
|
|
|
m_prefix = std::equal(query.begin(), query.end(), candidate.begin(), smartcase_eq);
|
2015-10-27 22:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RankedMatch::operator<(const RankedMatch& other) const
|
|
|
|
{
|
2016-03-24 23:04:56 +01:00
|
|
|
kak_assert((bool)*this and (bool)other);
|
|
|
|
|
2016-03-02 14:30:54 +01:00
|
|
|
if (m_prefix != other.m_prefix)
|
|
|
|
return m_prefix;
|
|
|
|
|
2016-02-29 00:05:51 +01:00
|
|
|
if (m_first_char_match != other.m_first_char_match)
|
|
|
|
return m_first_char_match;
|
|
|
|
|
2016-02-23 00:07:29 +01:00
|
|
|
if (m_only_word_boundary and other.m_only_word_boundary)
|
|
|
|
{
|
|
|
|
if (m_word_boundary_match_count != other.m_word_boundary_match_count)
|
|
|
|
return m_word_boundary_match_count > other.m_word_boundary_match_count;
|
|
|
|
}
|
|
|
|
else if (m_only_word_boundary or other.m_only_word_boundary)
|
|
|
|
return m_only_word_boundary;
|
2015-10-29 14:36:30 +01:00
|
|
|
|
|
|
|
if (m_word_boundary_match_count != other.m_word_boundary_match_count)
|
|
|
|
return m_word_boundary_match_count > other.m_word_boundary_match_count;
|
|
|
|
|
2016-02-29 00:05:51 +01:00
|
|
|
if (m_match_index_sum != other.m_match_index_sum)
|
|
|
|
return m_match_index_sum < other.m_match_index_sum;
|
2015-10-29 14:36:30 +01:00
|
|
|
|
|
|
|
return std::lexicographical_compare(
|
2015-10-30 14:57:46 +01:00
|
|
|
Utf8It{m_candidate.begin(), m_candidate}, Utf8It{m_candidate.end(), m_candidate},
|
|
|
|
Utf8It{other.m_candidate.begin(), other.m_candidate}, Utf8It{other.m_candidate.end(), other.m_candidate},
|
|
|
|
[](Codepoint a, Codepoint b) {
|
2015-10-29 14:36:30 +01:00
|
|
|
const bool low_a = islower(a), low_b = islower(b);
|
|
|
|
return low_a == low_b ? a < b : low_a;
|
|
|
|
});
|
2015-10-27 22:25:18 +01:00
|
|
|
}
|
|
|
|
|
2015-10-29 14:36:30 +01:00
|
|
|
UnitTest test_ranked_match{[] {
|
|
|
|
kak_assert(count_word_boundaries_match("run_all_tests", "rat") == 3);
|
2016-02-18 00:05:08 +01:00
|
|
|
kak_assert(count_word_boundaries_match("run_all_tests", "at") == 2);
|
|
|
|
kak_assert(count_word_boundaries_match("countWordBoundariesMatch", "wm") == 2);
|
|
|
|
kak_assert(count_word_boundaries_match("countWordBoundariesMatch", "cobm") == 3);
|
|
|
|
kak_assert(count_word_boundaries_match("countWordBoundariesMatch", "cWBM") == 4);
|
2015-10-29 14:36:30 +01:00
|
|
|
}};
|
|
|
|
|
2015-10-22 20:49:08 +02:00
|
|
|
}
|