diff options
author | John Hawthorn <john.hawthorn@gmail.com> | 2016-06-07 23:28:31 -0700 |
---|---|---|
committer | John Hawthorn <john.hawthorn@gmail.com> | 2016-06-08 17:40:00 -0700 |
commit | 33d04f141241382b8f0e5bd382a9b1764063aed6 (patch) | |
tree | 98c1c508c8136708dd021b2c746421cf541051fe /src/match.c | |
parent | e6f83538bd42a39867753902d31f3b370f5aa1c6 (diff) |
Move equality detection within calculate_score
Diffstat (limited to 'src/match.c')
-rw-r--r-- | src/match.c | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/src/match.c b/src/match.c index 9602700..61b9a1d 100644 --- a/src/match.c +++ b/src/match.c @@ -56,12 +56,23 @@ void mat_print(score_t *mat, char name, const char *needle, const char *haystack #endif score_t calculate_score(const char *needle, const char *haystack, size_t *positions) { - if (!*haystack || !*needle) + if (!*needle) return SCORE_MIN; int n = strlen(needle); int m = strlen(haystack); + if (n == m) { + /* Since this method can only be called with a haystack which + * matches needle. If the lengths of the strings are equal the + * strings themselves must also be equal (ignoring case). + */ + if (positions) + for (int i = 0; i < n; i++) + positions[i] = i; + return SCORE_MAX; + } + if (m > 1024) { /* * Unreasonably large candidate: return no score @@ -164,18 +175,7 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi } score_t match_positions(const char *needle, const char *haystack, size_t *positions) { - if (!*needle) { - return SCORE_MAX; - } else if (!strcasecmp(needle, haystack)) { - if (positions) { - int n = strlen(needle); - for (int i = 0; i < n; i++) - positions[i] = i; - } - return SCORE_MAX; - } else { - return calculate_score(needle, haystack, positions); - } + return calculate_score(needle, haystack, positions); } score_t match(const char *needle, const char *haystack) { |