File: search_utils.c

package info (click to toggle)
staden 2.0.0%2Bb11-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,584 kB
  • sloc: ansic: 240,605; tcl: 65,360; cpp: 12,854; makefile: 11,203; sh: 3,023; fortran: 2,033; perl: 63; awk: 46
file content (317 lines) | stat: -rw-r--r-- 6,563 bytes parent folder | download | duplicates (5)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <stdlib.h>
#include "search_utils.h"

/*
 * An implementation of strstr that skips pads ('*'). Pads are only ignored;
 * they will not be introduced to get a better match (as this is not an
 * alignment algorithm). Unlike strstr, our pstrnstr also has a maximum length
 * on both text and query sequences.
 *
 * Arguments:
 *	text		The text to search through
 *	text_len	Length of text
 *	query		The query sequence to find within 'text'
 *	query_len	Length of query
 *
 * Returns:
 *	A pointer to the first match in text when found.
 *	NULL when a match is not found.
 */
char *pstrnstr(char *text, size_t text_len, char *query, size_t query_len) {
    unsigned int t_ind = 0, q_ind;

    /*
     * Simplest implementation.
     * Not an efficient search, but easy to understand.
     */
    do {
	unsigned int t_ind2;
	for (t_ind2 = t_ind, q_ind = 0;
	     q_ind < query_len && t_ind2 < text_len;) {
	    if (text[t_ind2] == '*')
		t_ind2++;
	    else {
		if (text[t_ind2] != query[q_ind])
		    break;
		t_ind2++;
		q_ind++;
	    }
	}
    
	if (q_ind == query_len)
	    return &text[t_ind];

	t_ind++;
    } while (t_ind < text_len);

    return NULL;
}


/*
 * An inexact implementation of pstrnstr.
 * It will find matches allowing at most 'n' mismatches.
 *
 * Arguments:
 *	text		The text to search through
 *	text_len	Length of text
 *	query		The query sequence to find within 'text'
 *	query_len	Length of query
 *	mismatches	Number of allowable mismatches
 *
 * Returns:
 *	A pointer to the first match in text when found.
 *	NULL when a match is not found.
 */
char *pstrnstr_inexact(char *text, size_t text_len,
		       char *query, size_t query_len,
		       int mismatches, int *n_mis) {
    unsigned int t_ind = 0, q_ind;
    int m;

    if (n_mis)
	*n_mis = 0;

    /*
     * Simplest implementation.
     * Not an efficient search, but easy to understand.
     */
    do {
	unsigned int t_ind2;
	m = 0;
	for (t_ind2 = t_ind, q_ind = 0;
	     q_ind < query_len && t_ind2 < text_len;) {
	    if (text[t_ind2] == '*')
		t_ind2++;
	    else {
		if (text[t_ind2] != query[q_ind]) {
		    if (m++ >= mismatches)
			break;
		}
		t_ind2++;
		q_ind++;
	    }
	}
    
	if (q_ind == query_len) {
	    if (n_mis)
		*n_mis = m;
	    return &text[t_ind];
	}

	t_ind++;
    } while (t_ind < text_len);

    return NULL;
}

/*
 * An inexact implementation of prstrnstr.
 * It will find the last match allowing at most 'n' mismatches.
 *
 * Arguments:
 *	text		The text to search through
 *	text_len	Length of text
 *	query		The query sequence to find within 'text'
 *	query_len	Length of query
 *	mismatches	Number of allowable mismatches
 *
 * Returns:
 *	A pointer to the first match in text when found.
 *	NULL when a match is not found.
 */
char *prstrnstr_inexact(char *text, size_t text_len,
			char *query, size_t query_len,
			int mismatches, int *n_mis) {
    unsigned int t_ind = 0, q_ind;
    int m;
    char *match = NULL;
    char match_mis = 0;

    if (n_mis)
	*n_mis = 0;

    /*
     * Simplest implementation.
     * Not an efficient search, but easy to understand.
     */
    do {
	unsigned int t_ind2;
	m = 0;
	for (t_ind2 = t_ind, q_ind = 0;
	     q_ind < query_len && t_ind2 < text_len;) {
	    if (text[t_ind2] == '*')
		t_ind2++;
	    else {
		if (text[t_ind2] != query[q_ind]) {
		    if (m++ >= mismatches)
			break;
		}
		t_ind2++;
		q_ind++;
	    }
	}
    
	if (q_ind == query_len) {
	    match_mis = m;
	    if (n_mis)
		*n_mis = m;
	    match = &text[t_ind];
	}

	t_ind++;
    } while (t_ind < text_len);

    if (n_mis)
	*n_mis = match_mis;

    return match;
}


/*
 * An implementation of strstr that skips pads.
 *
 * As strstr, returns a pointer into the buffer when it finds a match, or
 * NULL when it does not.
 */
char *pstrstr(char *text, char *pattern) {
    char *text_p, *patt_p;

    /*
     * Simplest implementation.
     * Not an efficient search, but easy to understand.
     */
    do {
	text_p = text;
	patt_p = pattern;
	while (*patt_p && *text_p) {
	    if (*text_p == '*')
		text_p++;
	    else {
		if (*text_p != *patt_p)
		    break;
		text_p++;
		patt_p++;
	    }
	}
	if (*patt_p == 0)
	    return text;

    } while (*text && *++text);

    return NULL;
}


/*
 * A version of strstr with inexact string matching.
 *
 * Finds matches between pattern 'p' and text 't' with at most 'm' mismatches.
 * Padding characters are ignored, but this isn't an alignment algorithm - it
 * will not introduce gaps to get a better match.
 * KFB: 22/11/01 added n_mis to return the number of mismatches found
 */
char *pstrstr_inexact(char *text, char *pattern, int mismatches, int *n_mis) {
    char *text_p, *patt_p;
    int m;

    if (n_mis)
	*n_mis = 0;

    /*
     * Simplest implementation.
     * Not an efficient search, but easy to understand.
     */
    do {
	m = 0;

	/* remove leading pads */
	while (*text && *text == '*')
	    text++;

	text_p = text;
	patt_p = pattern;
	while (*patt_p && *text_p) {
	    if (*text_p == '*')
		text_p++;
	    else {
		if (*text_p != *patt_p) {
		    if (m++ == mismatches)
			break;
		}
		text_p++;
		patt_p++;
	    }
	}
	if (*patt_p == 0) {
	    if (n_mis)
		*n_mis = m;
	    return text;
	}

    } while (*text && *++text);

    return NULL;
}

/*
 * A version of strnstr with inexact string matching, but finding the LAST
 * copy of pattern in text.
 * (Done correctly this would do a reverse search, but this is coded for
 * simplicitly at the moment.)
 *
 * Finds matches between pattern 'p' and text 't' with at most 'm' mismatches.
 * Padding characters are ignored, but this isn't an alignment algorithm - it
 * will not introduce gaps to get a better match.
 *
 * This searches from the end of the string rather than the start, so it
 * will return the last copy of pattern in text.
 */
char *prstrstr_inexact(char *text, char *pattern, int mismatches, int *n_mis) {
    char *text_p, *patt_p;
    int m;
    char *match = NULL;
    char match_mis = 0;

    if (n_mis)
	*n_mis = 0;

    /*
     * Simplest implementation.
     * Not an efficient search, but easy to understand.
     */
    do {
	m = 0;

	/* remove leading pads */
	while (*text && *text == '*')
	    text++;

	text_p = text;
	patt_p = pattern;
	while (*patt_p && *text_p) {
	    if (*text_p == '*')
		text_p++;
	    else {
		if (*text_p != *patt_p) {
		    if (m++ == mismatches)
			break;
		}
		text_p++;
		patt_p++;
	    }
	}
	if (*patt_p == 0) {
	    match_mis = m;
	    match = text;
	}

    } while (*text && *++text);

    if (n_mis)
	*n_mis = match_mis;

    return match;
}