File: mxbmse.c

package info (click to toggle)
egenix-mx-base 3.2.8-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,420 kB
  • ctags: 6,208
  • sloc: ansic: 22,304; python: 18,124; sh: 137; makefile: 121
file content (223 lines) | stat: -rw-r--r-- 4,867 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* 
  mxbmse -- Fast Boyer Moore Search Algorithm (Version 0.9)

  The implementation is reentrant and thread safe. While the
  general ideas behind the Boyer Moore algorithm are in the public
  domain, this implementation falls under the following copyright:

  Copyright (c) 1997-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
  Copyright (c) 2000-2014, eGenix.com Software GmbH; mailto:info@egenix.com

                        All Rights Reserved

  See the documentation for copying information or contact the author
  (mal@lemburg.com).
*/

/* to turn on the debugging printfs (DPRINTF):*/
/* #define MAL_DEBUG */

/* Logging file used by debugging facility */
#ifndef MAL_DEBUG_OUTPUTFILE
# define MAL_DEBUG_OUTPUTFILE "mxTextSearch.log"
#endif

#ifdef MAL_DEBUG_WITH_PYTHON
# include "mx.h"
#endif

#ifdef MX_BUILDING_MXTEXTTOOLS
# include "mx.h"
# include "mxTextTools.h"
#else
# include "mxstdlib.h"
# include "mxbmse.h"
#endif

/* --- Fast Boyer-Moore Implementation (8-bit) ---------------------------- */

mxbmse_data *bm_init(char *match,
		     BM_LENGTH_TYPE match_len)
{
    mxbmse_data *c;
    BM_INDEX_TYPE i;
    BM_SHIFT_TYPE *shift;
    char *m;

    c = newstruct(mxbmse_data);
    c->match = match;
    c->match_len = match_len;
    c->eom = match + match_len - 1;

    /* Length 1 matching does not use a shift table */
    if (match_len == 1)
	return c;

    /* Init shift table */
    for ( shift = c->shift, i = 256; i > 0; i--, shift++ )
	*shift = (BM_SHIFT_TYPE) match_len;

    DPRINTF("shift table for match='%s'\n",match);
    for ( shift = c->shift, m = match, i = match_len - 1; 
	  i >= 0; 
	  i--, m++ ) {
	shift[ (unsigned char) *m ] = (BM_SHIFT_TYPE) i;
	DPRINTF("  char = '%c'  shift = %i\n", *m, i);
    }

    return c;
}

void bm_free(mxbmse_data *c)
{
    if (c)
	free(c);
}

BM_INDEX_TYPE bm_search(mxbmse_data *c,
			char *text,
			BM_INDEX_TYPE start,
			BM_LENGTH_TYPE text_len)
{
    register char *pt;
    register char *eot = text + text_len;
    
    /* Error check */
    if (c == NULL) 
	return -1;

    /* Init text pointer */
    pt = text + start + c->match_len - 1;

    DPRINTF("Init :  %2i %20.20s \t text: %2i %20.20s\n",
	    c->match_len,c->match,start,text+start);

    if (c->match_len > 1)
	for (;;) {
	    register char *pm;

	    pm = c->eom;

	    for (;pt < eot && *pt != *pm; 
		  pt += c->shift[(unsigned char) *pt]);

	    if (pt >= eot) 
		break;

	    /* First char matches.. what about the others ? */
	    {
		register BM_INDEX_TYPE im = c->match_len;

		do {
		    DPRINTF("=match: %2i '%20.20s' \t text: '%20.20s'\n",
			    im,pm,pt);
		    if (--im == 0) 
			/* Match */
			return pt - text + c->match_len;
		    pt--;
		    pm--;
		} while (*pt == *pm);

		/* Mismatch after match: use shift-table */
		{
		    register BM_INDEX_TYPE a,b;

		    a = c->shift[(unsigned char) *pt];
		    b = c->match_len - im + 1;
		    DPRINTF("!match: %2i '%20.20s' \t text: '%20.20s' "
			    "(sh=%i)\n",
			    im,pm,pt,max(a,b));
		    pt += (a > b) ? a : b;
		}
	    }

	}

    /* Special case: matching string has length 1 */
    else {
	register char m = *c->eom;
	
	for (;pt < eot; pt++)
	    if (*pt == m)
		/* Match */
		return pt - text + 1;
    }

    return start; /* no match */
}

/* bm search using the translate table -- 45% slower */

BM_INDEX_TYPE bm_tr_search(mxbmse_data *c,
			   char *text,
			   BM_INDEX_TYPE start,
			   BM_LENGTH_TYPE text_len,
			   char *tr)
{
    register char *pt;
    register char *eot = text + text_len;

    /* Error check */
    if (c == NULL) 
	return -1;

    /* Init text pointer */
    pt = text + start + c->match_len - 1;

    DPRINTF("Init :  %2i '%20.20s' \t text: %2i '%20.20s'\n",
	    c->match_len,c->match,start,text+start);

    if (c->match_len > 1)
	for (;;) {
	    register char *pm;

	    pm = c->eom;

	    for (;pt < eot && tr[(unsigned char) *pt] != *pm; 
		 pt += c->shift[(unsigned char) tr[(unsigned char) *pt]]);

	    if (pt >= eot) 
		break;

	    /* First char matches.. what about the others ? */
	    {
		register BM_INDEX_TYPE im = c->match_len;

		do {
		    DPRINTF("=match: %2i '%20.20s' \t text: '%20.20s'\n",
			    im,pm,pt);
		    if (--im == 0) 
			/* Match */
			return pt - text + c->match_len;
		    pt--;
		    pm--;
		} while (tr[(unsigned char) *pt] == *pm);

		/* Mismatch after match: use shift-table */
		{
		    register BM_INDEX_TYPE a,b;

		    a = c->shift[(unsigned char) tr[(unsigned char) *pt]];
		    b = c->match_len - im + 1;
		    DPRINTF("!match: %2i '%20.20s' \t text: '%20.20s' "
			    "(sh=%i)\n",
			    im,pm,pt,max(a,b));
		    pt += (a > b)?a:b;
		}
	    }

	}

    /* Special case: matching string has length 1 */
    else {
	register char m = *c->eom;
	
	for (;pt < eot; pt++)
	    if (*pt == m)
		/* Match */
		return pt - text + 1;
    }

    return start; /* no match */
}