aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/suggestions.cc
diff options
context:
space:
mode:
authorregnat <rg@regnat.ovh>2022-03-03 13:12:27 +0100
committerregnat <rg@regnat.ovh>2022-03-07 10:09:10 +0100
commit2405bbbb5edd204d6031edcd470a2057f4a25782 (patch)
tree92f43382357a0a59ddba42400ef94bc4ce371112 /src/libutil/suggestions.cc
parentc0792b1546ceed1c02a02ca1286ead55f79d5798 (diff)
Add some tests for the suggestions
Diffstat (limited to 'src/libutil/suggestions.cc')
-rw-r--r--src/libutil/suggestions.cc13
1 files changed, 3 insertions, 10 deletions
diff --git a/src/libutil/suggestions.cc b/src/libutil/suggestions.cc
index 96b48416b..bcd93aa6b 100644
--- a/src/libutil/suggestions.cc
+++ b/src/libutil/suggestions.cc
@@ -5,15 +5,8 @@
namespace nix {
-/**
- * Return `some(distance)` where distance is an integer representing some
- * notion of distance between both arguments.
- *
- * If the distance is too big, return none
- */
-int distanceBetween(std::string_view first, std::string_view second)
+int levenshteinDistance(std::string_view first, std::string_view second)
{
- // Levenshtein distance.
// Implementation borrowed from
// https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_rows
@@ -49,7 +42,7 @@ Suggestions Suggestions::bestMatches (
std::set<Suggestion> res;
for (const auto & possibleMatch : allMatches) {
res.insert(Suggestion {
- .distance = distanceBetween(query, possibleMatch),
+ .distance = levenshteinDistance(query, possibleMatch),
.suggestion = possibleMatch,
});
}
@@ -63,7 +56,7 @@ Suggestions Suggestions::trim(int limit, int maxDistance) const
int count = 0;
for (auto & elt : suggestions) {
- if (count >= limit || elt.distance >= maxDistance)
+ if (count >= limit || elt.distance > maxDistance)
break;
count++;
res.insert(elt);