diff options
author | kotontrion <[email protected]> | 2024-10-13 08:40:10 +0200 |
---|---|---|
committer | kotontrion <[email protected]> | 2024-10-13 08:40:10 +0200 |
commit | 856f6d06464f5ced12be8dd1a288daccda44c3e5 (patch) | |
tree | c301093ce6a4d238e87dd7a7503402dcef3e73d6 | |
parent | eb5ca1125ba043fb83c3a5ae8182ec48aff85c1b (diff) |
apps: fix style
-rw-r--r-- | lib/apps/apps.vala | 2 | ||||
-rw-r--r-- | lib/apps/fuzzy.vala | 112 |
2 files changed, 56 insertions, 58 deletions
diff --git a/lib/apps/apps.vala b/lib/apps/apps.vala index 2a0d507..b07961e 100644 --- a/lib/apps/apps.vala +++ b/lib/apps/apps.vala @@ -8,7 +8,7 @@ public class Apps : Object { public bool show_hidden { get; set; } public List<weak Application> list { owned get { return _list.copy(); } } - public double min_score { get; set; default = 0.5; } + public int min_score { get; set; default = 0; } public double name_multiplier { get; set; default = 2; } public double entry_multiplier { get; set; default = 1; } diff --git a/lib/apps/fuzzy.vala b/lib/apps/fuzzy.vala index d6dac3d..f93b2eb 100644 --- a/lib/apps/fuzzy.vala +++ b/lib/apps/fuzzy.vala @@ -1,75 +1,73 @@ namespace AstalApps { - private int max(int a, int b) { - return a > b ? a : b; - } +private int max(int a, int b) { + return a > b ? a : b; +} - public int fuzzy_match_string(string pattern, string str) { - const int unmatched_letter_penalty = -1; - int score = 100; +public int fuzzy_match_string(string pattern, string str) { + const int unmatched_letter_penalty = -1; + int score = 100; - if (pattern.length == 0) return score; - if (str.length < pattern.length) return int.MIN; + if (pattern.length == 0) return score; + if (str.length < pattern.length) return int.MIN; - score += unmatched_letter_penalty * (str.length - pattern.length); - score = fuzzy_match_recurse(pattern, str, score, true); + score += unmatched_letter_penalty * (str.length - pattern.length); + score = fuzzy_match_recurse(pattern, str, score, true); - return score; - } + return score; +} - private int fuzzy_match_recurse(string pattern, string str, int score, bool first_char) { - if (pattern.length == 0) return score; +private int fuzzy_match_recurse(string pattern, string str, int score, bool first_char) { + if (pattern.length == 0) return score; + + int match_idx = 0; + int offset = 0; + unichar search = pattern.casefold().get_char(0); + int best_score = int.MIN; + + while ((match_idx = str.casefold().substring(offset).index_of_char(search)) >= 0) { + offset += match_idx; + int subscore = fuzzy_match_recurse( + pattern.substring(1), + str.substring(offset + 1), + compute_score(offset, first_char, str, offset), false); + best_score = max(best_score, subscore); + offset++; + } - int match_idx = 0; - int offset = 0; - unichar search = pattern.casefold().get_char(0); - int best_score = int.MIN; + if (best_score == int.MIN) return int.MIN; + return score + best_score; +} - while ((match_idx = str.casefold().substring(offset).index_of_char(search)) >= 0) { - offset += match_idx; - int subscore = fuzzy_match_recurse( - pattern.substring(1), - str.substring(offset + 1), - compute_score(offset, first_char, str, offset), false); - best_score = max(best_score, subscore); - offset++; - } +private int compute_score(int jump, bool first_char, string match, int idx) { + const int adjacency_bonus = 15; + const int separator_bonus = 30; + const int camel_bonus = 30; + const int first_letter_bonus = 15; + const int leading_letter_penalty = -5; + const int max_leading_letter_penalty = -15; + int score = 0; - if (best_score == int.MIN) return int.MIN; - return score + best_score; + if (!first_char && jump == 0) { + score += adjacency_bonus; } - - private int compute_score(int jump, bool first_char, string match, int idx) { - const int adjacency_bonus = 15; - const int separator_bonus = 30; - const int camel_bonus = 30; - const int first_letter_bonus = 15; - const int leading_letter_penalty = -5; - const int max_leading_letter_penalty = -15; - - int score = 0; - - if (!first_char && jump == 0) { - score += adjacency_bonus; - } - if (!first_char || jump > 0) { - if (match[idx].isupper() && match[idx-1].islower()) { - score += camel_bonus; - } - if (match[idx].isalnum() && !match[idx-1].isalnum()) { - score += separator_bonus; - } - } - if (first_char && jump == 0) { - score += first_letter_bonus; + if (!first_char || jump > 0) { + if (match[idx].isupper() && match[idx-1].islower()) { + score += camel_bonus; } - if (first_char) { - score += max(leading_letter_penalty * jump, max_leading_letter_penalty); + if (match[idx].isalnum() && !match[idx-1].isalnum()) { + score += separator_bonus; } - - return score; + } + if (first_char && jump == 0) { + score += first_letter_bonus; + } + if (first_char) { + score += max(leading_letter_penalty * jump, max_leading_letter_penalty); } + return score; +} } |