Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/match.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ char *strcasechr(const char *s, char c) {
}

int has_match(const char *needle, const char *haystack) {
if (!needle || !haystack || !*needle) {
return 0;
}

while (*needle) {
char nch = *needle++;

Expand Down Expand Up @@ -108,7 +112,7 @@ static inline void match_row(const struct match_struct *match, int row, score_t
}

score_t match(const char *needle, const char *haystack) {
if (!*needle)
if (!needle || !haystack || !*needle)
return SCORE_MIN;

struct match_struct match;
Expand Down Expand Up @@ -173,13 +177,18 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi
return SCORE_MAX;
}

// Allocate for both D and M matrices in one contiguous block
score_t *block = (score_t*)malloc(sizeof(score_t) * MATCH_MAX_LEN * n * 2);
if (!block) {
return SCORE_MIN;
}
/*
* D[][] Stores the best score for this position ending with a match.
* M[][] Stores the best possible score at this position.
*/
score_t (*D)[MATCH_MAX_LEN], (*M)[MATCH_MAX_LEN];
M = malloc(sizeof(score_t) * MATCH_MAX_LEN * n);
D = malloc(sizeof(score_t) * MATCH_MAX_LEN * n);
score_t (*D)[MATCH_MAX_LEN] = (score_t(*)[MATCH_MAX_LEN])block;
score_t (*M)[MATCH_MAX_LEN] = (score_t(*)[MATCH_MAX_LEN])(block
+ MATCH_MAX_LEN * n);

match_row(&match, 0, D[0], M[0], D[0], M[0]);
for (int i = 1; i < n; i++) {
Expand Down Expand Up @@ -217,8 +226,7 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi

score_t result = M[n - 1][m - 1];

free(M);
free(D);
free(block);

return result;
}