| ... | ... |
@@ -54,7 +54,7 @@ double calculate_score(const char *needle, const char *haystack, size_t *positio |
| 54 | 54 |
return 0; |
| 55 | 55 |
} |
| 56 | 56 |
|
| 57 |
- int bow[m]; |
|
| 57 |
+ score_t match_bonus[m]; |
|
| 58 | 58 |
score_t D[n][m], M[n][m]; |
| 59 | 59 |
bzero(D, sizeof(D)); |
| 60 | 60 |
bzero(M, sizeof(M)); |
| ... | ... |
@@ -70,9 +70,11 @@ double calculate_score(const char *needle, const char *haystack, size_t *positio |
| 70 | 70 |
for(int i = 0; i < m; i++){
|
| 71 | 71 |
char ch = haystack[i]; |
| 72 | 72 |
/* TODO: What about allcaps (ex. README) */ |
| 73 |
- bow[i] = (at_bow && isalnum(ch)) || (isupper(ch) && !isupper(last_ch)); |
|
| 73 |
+ int bow = (at_bow && isalnum(ch)) || (isupper(ch) && !isupper(last_ch)); |
|
| 74 | 74 |
at_bow = !isalnum(ch); |
| 75 | 75 |
last_ch = ch; |
| 76 |
+ |
|
| 77 |
+ match_bonus[i] = bow ? 1.5 : 0; |
|
| 76 | 78 |
} |
| 77 | 79 |
|
| 78 | 80 |
for(int i = 0; i < n; i++){
|
| ... | ... |
@@ -81,12 +83,12 @@ double calculate_score(const char *needle, const char *haystack, size_t *positio |
| 81 | 83 |
int match = tolower(needle[i]) == tolower(haystack[j]); |
| 82 | 84 |
if(match){
|
| 83 | 85 |
score_t score = 0; |
| 84 |
- if(i && j) |
|
| 85 |
- score = M[i-1][j-1]; |
|
| 86 |
- if(bow[j]) |
|
| 87 |
- score += 1.5; |
|
| 88 |
- else if(i && j && D[i-1][j-1]) |
|
| 86 |
+ if(i && j){
|
|
| 87 |
+ score = M[i-1][j-1] + match_bonus[j]; |
|
| 88 |
+ |
|
| 89 |
+ /* consecutive match, doesn't stack with match_bonus */ |
|
| 89 | 90 |
score = max(score, 1 + D[i-1][j-1]); |
| 91 |
+ } |
|
| 90 | 92 |
M[i][j] = D[i][j] = score; |
| 91 | 93 |
} |
| 92 | 94 |
if(j) |