Browse code

Move equality detection within calculate_score

John Hawthorn authored on 08/06/2016 06:28:31
Showing 1 changed files

... ...
@@ -56,12 +56,23 @@ void mat_print(score_t *mat, char name, const char *needle, const char *haystack
56 56
 #endif
57 57
 
58 58
 score_t calculate_score(const char *needle, const char *haystack, size_t *positions) {
59
-	if (!*haystack || !*needle)
59
+	if (!*needle)
60 60
 		return SCORE_MIN;
61 61
 
62 62
 	int n = strlen(needle);
63 63
 	int m = strlen(haystack);
64 64
 
65
+	if (n == m) {
66
+		/* Since this method can only be called with a haystack which
67
+		 * matches needle. If the lengths of the strings are equal the
68
+		 * strings themselves must also be equal (ignoring case).
69
+		 */
70
+		if (positions)
71
+			for (int i = 0; i < n; i++)
72
+				positions[i] = i;
73
+		return SCORE_MAX;
74
+	}
75
+
65 76
 	if (m > 1024) {
66 77
 		/*
67 78
 		 * Unreasonably large candidate: return no score
... ...
@@ -164,18 +175,7 @@ score_t calculate_score(const char *needle, const char *haystack, size_t *positi
164 175
 }
165 176
 
166 177
 score_t match_positions(const char *needle, const char *haystack, size_t *positions) {
167
-	if (!*needle) {
168
-		return SCORE_MAX;
169
-	} else if (!strcasecmp(needle, haystack)) {
170
-		if (positions) {
171
-			int n = strlen(needle);
172
-			for (int i = 0; i < n; i++)
173
-				positions[i] = i;
174
-		}
175
-		return SCORE_MAX;
176
-	} else {
177
-		return calculate_score(needle, haystack, positions);
178
-	}
178
+	return calculate_score(needle, haystack, positions);
179 179
 }
180 180
 
181 181
 score_t match(const char *needle, const char *haystack) {