| ... | ... |
@@ -55,6 +55,33 @@ void mat_print(score_t *mat, char name, const char *needle, const char *haystack |
| 55 | 55 |
} |
| 56 | 56 |
#endif |
| 57 | 57 |
|
| 58 |
+static void precompute_bonus(const char *haystack, score_t *match_bonus) {
|
|
| 59 |
+ /* Which positions are beginning of words */ |
|
| 60 |
+ int m = strlen(haystack); |
|
| 61 |
+ char last_ch = '\0'; |
|
| 62 |
+ for (int i = 0; i < m; i++) {
|
|
| 63 |
+ char ch = haystack[i]; |
|
| 64 |
+ |
|
| 65 |
+ score_t score = 0; |
|
| 66 |
+ if (isalnum(ch)) {
|
|
| 67 |
+ if (!last_ch || last_ch == '/') {
|
|
| 68 |
+ score = SCORE_MATCH_SLASH; |
|
| 69 |
+ } else if (last_ch == '-' || last_ch == '_' || last_ch == ' ' || |
|
| 70 |
+ (last_ch >= '0' && last_ch <= '9')) {
|
|
| 71 |
+ score = SCORE_MATCH_WORD; |
|
| 72 |
+ } else if (last_ch >= 'a' && last_ch <= 'z' && ch >= 'A' && ch <= 'Z') {
|
|
| 73 |
+ /* CamelCase */ |
|
| 74 |
+ score = SCORE_MATCH_CAPITAL; |
|
| 75 |
+ } else if (last_ch == '.') {
|
|
| 76 |
+ score = SCORE_MATCH_DOT; |
|
| 77 |
+ } |
|
| 78 |
+ } |
|
| 79 |
+ |
|
| 80 |
+ match_bonus[i] = score; |
|
| 81 |
+ last_ch = ch; |
|
| 82 |
+ } |
|
| 83 |
+} |
|
| 84 |
+ |
|
| 58 | 85 |
score_t match_positions(const char *needle, const char *haystack, size_t *positions) {
|
| 59 | 86 |
if (!*needle) |
| 60 | 87 |
return SCORE_MIN; |
| ... | ... |
@@ -89,30 +116,7 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi |
| 89 | 116 |
* D[][] Stores the best score for this position ending with a match. |
| 90 | 117 |
* M[][] Stores the best possible score at this position. |
| 91 | 118 |
*/ |
| 92 |
- |
|
| 93 |
- /* Which positions are beginning of words */ |
|
| 94 |
- char last_ch = '\0'; |
|
| 95 |
- for (int i = 0; i < m; i++) {
|
|
| 96 |
- char ch = haystack[i]; |
|
| 97 |
- |
|
| 98 |
- score_t score = 0; |
|
| 99 |
- if (isalnum(ch)) {
|
|
| 100 |
- if (!last_ch || last_ch == '/') {
|
|
| 101 |
- score = SCORE_MATCH_SLASH; |
|
| 102 |
- } else if (last_ch == '-' || last_ch == '_' || last_ch == ' ' || |
|
| 103 |
- (last_ch >= '0' && last_ch <= '9')) {
|
|
| 104 |
- score = SCORE_MATCH_WORD; |
|
| 105 |
- } else if (last_ch >= 'a' && last_ch <= 'z' && ch >= 'A' && ch <= 'Z') {
|
|
| 106 |
- /* CamelCase */ |
|
| 107 |
- score = SCORE_MATCH_CAPITAL; |
|
| 108 |
- } else if (last_ch == '.') {
|
|
| 109 |
- score = SCORE_MATCH_DOT; |
|
| 110 |
- } |
|
| 111 |
- } |
|
| 112 |
- |
|
| 113 |
- match_bonus[i] = score; |
|
| 114 |
- last_ch = ch; |
|
| 115 |
- } |
|
| 119 |
+ precompute_bonus(haystack, match_bonus); |
|
| 116 | 120 |
|
| 117 | 121 |
for (int i = 0; i < n; i++) {
|
| 118 | 122 |
score_t prev_score = SCORE_MIN; |