summaryrefslogtreecommitdiff
path: root/match.c
diff options
context:
space:
mode:
authorJohn Hawthorn <john.hawthorn@gmail.com>2014-07-29 22:05:15 -0700
committerJohn Hawthorn <john.hawthorn@gmail.com>2014-07-30 10:25:21 -0700
commitb76c67f554085652e4128e2ea8e85f24fd26ba17 (patch)
treeaa1ae1f46e0960275ad97aa63204d10a8a21cf1f /match.c
parent1eec4e7c2b5405a638c92a0142741d8fca11e1df (diff)
Move scoring magic numbers into macros
Diffstat (limited to 'match.c')
-rw-r--r--match.c27
1 files changed, 20 insertions, 7 deletions
diff --git a/match.c b/match.c
index 2210998..eb4cb72 100644
--- a/match.c
+++ b/match.c
@@ -65,6 +65,15 @@ double calculate_score(const char *needle, const char *haystack, size_t *positio
* M[][] Stores the best possible score at this position.
*/
+#define SCORE_GAP_LEADING -0.01
+#define SCORE_GAP_TRAILING -0.01
+#define SCORE_GAP_INNER -0.01
+#define SCORE_MATCH_CONSECUTIVE 1.0
+#define SCORE_MATCH_SLASH 1.5
+#define SCORE_MATCH_WORD 1.2
+#define SCORE_MATCH_CAPITAL 1.1
+#define SCORE_MATCH_DOT 0.8
+
/* Which positions are beginning of words */
char last_ch = '\0';
for(int i = 0; i < m; i++){
@@ -73,18 +82,18 @@ double calculate_score(const char *needle, const char *haystack, size_t *positio
score_t score = 0;
if(isalnum(ch)){
if(last_ch == '/'){
- score = 1.5;
+ score = SCORE_MATCH_SLASH;
}else if(last_ch == '-' ||
last_ch == '_' ||
last_ch == ' ' ||
(last_ch >= '0' && last_ch <= '9')){
- score = 1.2;
+ score = SCORE_MATCH_WORD;
}else if(last_ch >= 'a' && last_ch <= 'z' &&
ch >= 'A' && ch <= 'Z'){
/* CamelCase */
- score = 1.1;
+ score = SCORE_MATCH_CAPITAL;
}else if(last_ch == '.'){
- score = 0.8;
+ score = SCORE_MATCH_DOT;
}
}
@@ -102,14 +111,18 @@ double calculate_score(const char *needle, const char *haystack, size_t *positio
score = max(score, M[i-1][j-1] + match_bonus[j]);
/* consecutive match, doesn't stack with match_bonus */
- score = max(score, D[i-1][j-1] + 1.0);
+ score = max(score, D[i-1][j-1] + SCORE_MATCH_CONSECUTIVE);
}else if(!i){
- score = (j * -0.01) + match_bonus[j];
+ score = (j * SCORE_GAP_LEADING) + match_bonus[j];
}
D[i][j] = score;
}
if(j){
- score = max(score, M[i][j-1] - 0.01);
+ if(i == n-1){
+ score = max(score, M[i][j-1] + SCORE_GAP_TRAILING);
+ }else{
+ score = max(score, M[i][j-1] + SCORE_GAP_INNER);
+ }
}
M[i][j] = score;
}