Browse code

Move scoring magic numbers into macros

John Hawthorn authored on 30/07/2014 05:05:15
Showing 1 changed files

  • match.c index 2210998..eb4cb72 100644
... ...
@@ -65,6 +65,15 @@ double calculate_score(const char *needle, const char *haystack, size_t *positio
65 65
 	 * M[][] Stores the best possible score at this position.
66 66
 	 */
67 67
 
68
+#define SCORE_GAP_LEADING      -0.01
69
+#define SCORE_GAP_TRAILING     -0.01
70
+#define SCORE_GAP_INNER        -0.01
71
+#define SCORE_MATCH_CONSECUTIVE 1.0
72
+#define SCORE_MATCH_SLASH       1.5
73
+#define SCORE_MATCH_WORD        1.2
74
+#define SCORE_MATCH_CAPITAL     1.1
75
+#define SCORE_MATCH_DOT         0.8
76
+
68 77
 	/* Which positions are beginning of words */
69 78
 	char last_ch = '\0';
70 79
 	for(int i = 0; i < m; i++){
... ...
@@ -73,18 +82,18 @@ double calculate_score(const char *needle, const char *haystack, size_t *positio
73 82
 		score_t score = 0;
74 83
 		if(isalnum(ch)){
75 84
 			if(last_ch == '/'){
76
-				score = 1.5;
85
+				score = SCORE_MATCH_SLASH;
77 86
 			}else if(last_ch == '-' ||
78 87
 					last_ch == '_' ||
79 88
 					last_ch == ' ' ||
80 89
 					(last_ch >= '0' && last_ch <= '9')){
81
-				score = 1.2;
90
+				score = SCORE_MATCH_WORD;
82 91
 			}else if(last_ch >= 'a' && last_ch <= 'z' &&
83 92
 					ch >= 'A' && ch <= 'Z'){
84 93
 				/* CamelCase */
85
-				score = 1.1;
94
+				score = SCORE_MATCH_CAPITAL;
86 95
 			}else if(last_ch == '.'){
87
-				score = 0.8;
96
+				score = SCORE_MATCH_DOT;
88 97
 			}
89 98
 		}
90 99
 
... ...
@@ -102,14 +111,18 @@ double calculate_score(const char *needle, const char *haystack, size_t *positio
102 111
 					score = max(score, M[i-1][j-1] + match_bonus[j]);
103 112
 
104 113
 					/* consecutive match, doesn't stack with match_bonus */
105
-					score = max(score, D[i-1][j-1] + 1.0);
114
+					score = max(score, D[i-1][j-1] + SCORE_MATCH_CONSECUTIVE);
106 115
 				}else if(!i){
107
-					score = (j * -0.01) + match_bonus[j];
116
+					score = (j * SCORE_GAP_LEADING) + match_bonus[j];
108 117
 				}
109 118
 				D[i][j] = score;
110 119
 			}
111 120
 			if(j){
112
-				score = max(score, M[i][j-1] - 0.01);
121
+				if(i == n-1){
122
+					score = max(score, M[i][j-1] + SCORE_GAP_TRAILING);
123
+				}else{
124
+					score = max(score, M[i][j-1] + SCORE_GAP_INNER);
125
+				}
113 126
 			}
114 127
 			M[i][j] = score;
115 128
 		}