| ... | ... |
@@ -58,6 +58,16 @@ void mat_print(score_t *mat, char name, const char *needle, const char *haystack |
| 58 | 58 |
|
| 59 | 59 |
#define MATCH_MAX_LEN 1024 |
| 60 | 60 |
|
| 61 |
+struct match_struct {
|
|
| 62 |
+ int needle_len; |
|
| 63 |
+ int haystack_len; |
|
| 64 |
+ |
|
| 65 |
+ char lower_needle[MATCH_MAX_LEN]; |
|
| 66 |
+ char lower_haystack[MATCH_MAX_LEN]; |
|
| 67 |
+ |
|
| 68 |
+ score_t match_bonus[MATCH_MAX_LEN]; |
|
| 69 |
+}; |
|
| 70 |
+ |
|
| 61 | 71 |
static void precompute_bonus(const char *haystack, score_t *match_bonus) {
|
| 62 | 72 |
/* Which positions are beginning of words */ |
| 63 | 73 |
int m = strlen(haystack); |
| ... | ... |
@@ -69,12 +79,32 @@ static void precompute_bonus(const char *haystack, score_t *match_bonus) {
|
| 69 | 79 |
} |
| 70 | 80 |
} |
| 71 | 81 |
|
| 82 |
+static void setup_match_struct(struct match_struct *match, const char *needle, const char *haystack) {
|
|
| 83 |
+ match->needle_len = strlen(needle); |
|
| 84 |
+ match->haystack_len = strlen(haystack); |
|
| 85 |
+ |
|
| 86 |
+ if (match->needle_len > MATCH_MAX_LEN || match->needle_len > match->haystack_len) {
|
|
| 87 |
+ return; |
|
| 88 |
+ } |
|
| 89 |
+ |
|
| 90 |
+ for (int i = 0; i < match->needle_len; i++) |
|
| 91 |
+ match->lower_needle[i] = tolower(needle[i]); |
|
| 92 |
+ |
|
| 93 |
+ for (int i = 0; i < match->haystack_len; i++) |
|
| 94 |
+ match->lower_haystack[i] = tolower(haystack[i]); |
|
| 95 |
+ |
|
| 96 |
+ precompute_bonus(haystack, match->match_bonus); |
|
| 97 |
+} |
|
| 98 |
+ |
|
| 72 | 99 |
score_t match_positions(const char *needle, const char *haystack, size_t *positions) {
|
| 73 | 100 |
if (!*needle) |
| 74 | 101 |
return SCORE_MIN; |
| 75 | 102 |
|
| 76 |
- int n = strlen(needle); |
|
| 77 |
- int m = strlen(haystack); |
|
| 103 |
+ struct match_struct match; |
|
| 104 |
+ setup_match_struct(&match, needle, haystack); |
|
| 105 |
+ |
|
| 106 |
+ int n = match.needle_len; |
|
| 107 |
+ int m = match.haystack_len; |
|
| 78 | 108 |
|
| 79 | 109 |
if (m > MATCH_MAX_LEN || n > m) {
|
| 80 | 110 |
/* |
| ... | ... |
@@ -94,26 +124,18 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi |
| 94 | 124 |
return SCORE_MAX; |
| 95 | 125 |
} |
| 96 | 126 |
|
| 97 |
- char lower_needle[MATCH_MAX_LEN]; |
|
| 98 |
- char lower_haystack[MATCH_MAX_LEN]; |
|
| 99 |
- |
|
| 100 |
- for (int i = 0; i < n; i++) |
|
| 101 |
- lower_needle[i] = tolower(needle[i]); |
|
| 102 |
- |
|
| 103 |
- for (int i = 0; i < m; i++) |
|
| 104 |
- lower_haystack[i] = tolower(haystack[i]); |
|
| 105 |
- |
|
| 106 |
- score_t match_bonus[MATCH_MAX_LEN]; |
|
| 107 |
- score_t D[n][m], M[n][m]; |
|
| 108 |
- |
|
| 109 |
- score_t *last_D, *last_M; |
|
| 110 |
- score_t *curr_D, *curr_M; |
|
| 127 |
+ const char *lower_needle = match.lower_needle; |
|
| 128 |
+ const char *lower_haystack = match.lower_haystack; |
|
| 129 |
+ const score_t *match_bonus = match.match_bonus; |
|
| 111 | 130 |
|
| 112 | 131 |
/* |
| 113 | 132 |
* D[][] Stores the best score for this position ending with a match. |
| 114 | 133 |
* M[][] Stores the best possible score at this position. |
| 115 | 134 |
*/ |
| 116 |
- precompute_bonus(haystack, match_bonus); |
|
| 135 |
+ score_t D[n][m], M[n][m]; |
|
| 136 |
+ |
|
| 137 |
+ score_t *last_D, *last_M; |
|
| 138 |
+ score_t *curr_D, *curr_M; |
|
| 117 | 139 |
|
| 118 | 140 |
for (int i = 0; i < n; i++) {
|
| 119 | 141 |
score_t prev_score = SCORE_MIN; |