Bugfix: Really fix the crash in bm.c, do memory checking after each allocation, corre...
[2dmatching.git] / baeza.c
blob920535f97f7c8e06f3ac5cf5cdbfd9f5c80ede7f
1 #include "code.h"
3 unsigned int baeza( char **text, char **pattern, int m, int n )
5 unsigned int i, j, row, matches = 0;
7 int x, y;
9 /* Iterate every m rows (first check lines 0 to m - 1) */
10 for ( row = m - 1; row < n; row += m ) {
12 /* Search the same row for occurences of the different lines of the pattern */
13 for ( i = 0; i < m; i++ ) {
15 /* In each row start searching from the begining */
16 x = 0;
18 while ( x != -1) {
20 /* Check text's row against each pattern's row */
21 x = aho( text, pattern, m, n, row, x, i, n );
23 /* If a row is not found procceed to the next row */
24 if ( x != -1 ) {
26 /* If there is a match at i = 3, check vertically the text above for i = 0-2 and below for 4-m*/
28 /* Check lines above the current */
29 for ( j = 0; j < i; j++ ) {
30 y = aho( text, pattern, m, n, row - i + j, x, j, x + m );
32 if ( y != x)
33 goto newline;
36 /* Check lines below the current */
37 for ( j = 1; j < m - i; j++ ) {
39 /* Avoid crashing when x = n or n-1 */
40 if ( row + j >= n )
41 goto newline;
43 y = aho( text, pattern, m, n, row + j, x, i + j, x + m );
45 if ( y != x )
46 goto newline;
49 /* If we reached here we should have a match */
50 matches++;
52 /* If a mismatch is found, don't bother searching for the other y lines */
53 newline:
55 /* Now increase counter to continue search in the same row */
56 x++;
58 else break;
62 return matches;