SILICON
Loading...
Searching...
No Matches
componentSearchMatcher.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
19
20#include <algorithm>
21#include <ranges>
22
23#include <QStringList>
24
25
26namespace SILICON::ui {
27
28namespace {
29
30int editDistance(QString lhs, QString rhs)
31{
32 lhs = lhs.toCaseFolded();
33 rhs = rhs.toCaseFolded();
34
35 std::vector<int> previous(rhs.size() + 1);
36 std::vector<int> current(rhs.size() + 1);
37
38 for (qsizetype i = 0; i <= rhs.size(); ++i) {
39 previous[i] = static_cast<int>(i);
40 }
41
42 for (qsizetype i = 1; i <= lhs.size(); ++i) {
43 current[0] = static_cast<int>(i);
44
45 for (qsizetype j = 1; j <= rhs.size(); ++j) {
46 const int substitutionCost = lhs[i - 1] == rhs[j - 1] ? 0 : 1;
47 current[j] = std::min(
48 {previous[j] + 1, current[j - 1] + 1, previous[j - 1] + substitutionCost});
49 }
50
51 std::swap(previous, current);
52 }
53
54 return previous[rhs.size()];
55}
56
57bool isFuzzyMatch(const QString& query, const QString& text, const int distance)
58{
59 if (query.isEmpty())
60 return true;
61
62 const int fieldSize = static_cast<int>(std::min(query.size(), text.size()));
63 const int maxDistance = std::max(1, fieldSize / 3);
64 return distance <= maxDistance;
65}
66
67} // namespace
68
70
71std::vector<Match> rank(QStringList candidates, const QString& query,
72 const bool includeNonMatches)
73{
74 const QString normalizedQuery = query.trimmed();
75
76 std::vector<Match> ranked;
77 ranked.reserve(static_cast<size_t>(candidates.size()));
78
79 for (int index = 0; index < candidates.size(); ++index) {
80 const QString& text = candidates[index];
81 int rank = 3;
82 int distance = 0;
83
84 if (!normalizedQuery.isEmpty()) {
85 if (text.compare(normalizedQuery, Qt::CaseInsensitive) == 0)
86 rank = 0;
87 else if (text.startsWith(normalizedQuery, Qt::CaseInsensitive))
88 rank = 1;
89 else if (text.contains(normalizedQuery, Qt::CaseInsensitive))
90 rank = 2;
91
92 distance = editDistance(normalizedQuery, text);
93 }
94
95 const bool matches = rank < 3 || isFuzzyMatch(normalizedQuery, text, distance);
96 if (includeNonMatches || matches)
97 ranked.push_back({index, rank, distance, matches});
98 }
99
100 std::ranges::sort(ranked, [&](const Match& lhs, const Match& rhs) {
101 if (lhs.rank != rhs.rank)
102 return lhs.rank < rhs.rank;
103
104 if (lhs.distance != rhs.distance)
105 return lhs.distance < rhs.distance;
106
107 if (candidates[lhs.index].size() != candidates[rhs.index].size())
108 return candidates[lhs.index].size() < candidates[rhs.index].size();
109
110 return QString::compare(candidates[lhs.index], candidates[rhs.index],
111 Qt::CaseInsensitive)
112 < 0;
113 });
114
115 return ranked;
116}
117
118} // namespace componentSearchMatcher
119
120} // namespace SILICON::ui
std::vector< Match > rank(QStringList candidates, const QString &query, const bool includeNonMatches)