summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/apps/apps.vala3
-rw-r--r--lib/apps/fuzzy.vala30
2 files changed, 17 insertions, 16 deletions
diff --git a/lib/apps/apps.vala b/lib/apps/apps.vala
index c96a2b2..bbe07c6 100644
--- a/lib/apps/apps.vala
+++ b/lib/apps/apps.vala
@@ -17,7 +17,7 @@ public class AstalApps.Apps : Object {
/**
* The minimum score the application has to meet in order to be included in queries.
*/
- public int min_score { get; set; default = 0; }
+ public double min_score { get; set; default = 0; }
/**
* Extra multiplier to apply when matching the `name` of an application.
@@ -108,7 +108,6 @@ public class AstalApps.Apps : Object {
r += am.executable * executable_multiplier;
if (include_description)
r += am.description * description_multiplier;
-
return r;
}
diff --git a/lib/apps/fuzzy.vala b/lib/apps/fuzzy.vala
index 97277bd..7745320 100644
--- a/lib/apps/fuzzy.vala
+++ b/lib/apps/fuzzy.vala
@@ -1,8 +1,4 @@
namespace AstalApps {
-private int max(int a, int b) {
- return a > b ? a : b;
-}
-
private int fuzzy_match_string(string pattern, string str) {
const int unmatched_letter_penalty = -1;
int score = 100;
@@ -10,14 +6,17 @@ private int fuzzy_match_string(string pattern, string str) {
if (pattern.length == 0) return score;
if (str.length < pattern.length) return int.MIN;
+ bool found = fuzzy_match_recurse(pattern, str, score, true, out score);
score += unmatched_letter_penalty * (str.length - pattern.length);
- score = fuzzy_match_recurse(pattern, str, score, true);
+
+ if(!found) score = -10;
return score;
}
-private int fuzzy_match_recurse(string pattern, string str, int score, bool first_char) {
- if (pattern.length == 0) return score;
+private bool fuzzy_match_recurse(string pattern, string str, int score, bool first_char, out int result) {
+ result = score;
+ if (pattern.length == 0) return true;
int match_idx = 0;
int offset = 0;
@@ -26,16 +25,19 @@ private int fuzzy_match_recurse(string pattern, string str, int score, bool firs
while ((match_idx = str.casefold().substring(offset).index_of_char(search)) >= 0) {
offset += match_idx;
- int subscore = fuzzy_match_recurse(
+ int subscore;
+ bool found = fuzzy_match_recurse(
pattern.substring(1),
str.substring(offset + 1),
- compute_score(offset, first_char, str, offset), false);
- best_score = max(best_score, subscore);
+ compute_score(offset, first_char, str, offset), false, out subscore);
+ if(!found) break;
+ best_score = int.max(best_score, subscore);
offset++;
}
-
- if (best_score == int.MIN) return int.MIN;
- return score + best_score;
+
+ if (best_score == int.MIN) return false;
+ result += best_score;
+ return true;
}
private int compute_score(int jump, bool first_char, string match, int idx) {
@@ -63,7 +65,7 @@ private int compute_score(int jump, bool first_char, string match, int idx) {
score += first_letter_bonus;
}
if (first_char) {
- score += max(leading_letter_penalty * jump, max_leading_letter_penalty);
+ score += int.max(leading_letter_penalty * jump, max_leading_letter_penalty);
}
return score;