apse.c

filter: Statementstmt 65.9% 470 / 713 Branchbran 53.2% 216 / 406 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 61.3% 686 / 1119 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/String/Approx.pm ↑ Summary Approx.xs »
1 /*
2  
3 Copyright (C) by Jarkko Hietaniemi, 1998,1999,2000,2001,2002,2003,2006.
4 All Rights Reserved.
5  
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of either:
8  
9 a) the GNU Library General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any
11 later version, or
12  
13 b) the "Artistic License" which comes with Perl source code.
14  
15 Other free software licensing schemes are negotiable.
16  
17 Furthermore:
18  
19 (1) This software is provided as-is, without warranties or
20 obligations of any kind.
21  
22 (2) You shall include this copyright notice intact in all copies
23 and derived materials.
24  
25 */
26  
27 /*
28  
29 $Id: apse.c,v 1.1 1999/06/23 16:09:13 jhi Exp jhi $
30  
31 */
32  
33 #include "apse.h"
34  
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <ctype.h>
39 #include <assert.h>
40  
41 #define APSE_BITS_IN_BITVEC (8*sizeof(apse_vec_t))
42  
43 #define APSE_CHAR_MAX 256
44  
45 #ifdef APSE_DEBUGGING
46 #define APSE_DEBUG(x) x
47 #else
48 #define APSE_DEBUG(x)
49 #endif
50  
51 #define APSE_BIT(i) ((apse_vec_t)1 << ((i)%APSE_BITS_IN_BITVEC))
52 #define APSE_IDX(p, q, i) ((p)*(q)+((i)/APSE_BITS_IN_BITVEC))
53 #define APSE_BIT_SET(bv, p, q, i) ((bv[APSE_IDX(p, q, i)] |= APSE_BIT(i)))
54 #define APSE_BIT_CLR(bv, p, q, i) ((bv[APSE_IDX(p, q, i)] &= ~APSE_BIT(i)))
55 #define APSE_BIT_TST(bv, p, q, i) ((bv[APSE_IDX(p, q, i)] & APSE_BIT(i)))
56  
57 #define APSE_MATCH_STATE_BOT 0
58 #define APSE_MATCH_STATE_SEARCH 1
59 #define APSE_MATCH_STATE_BEGIN 2
60 #define APSE_MATCH_STATE_FAIL 3
61 #define APSE_MATCH_STATE_GREEDY 4
62 #define APSE_MATCH_STATE_END 5
63 #define APSE_MATCH_STATE_EOT 6
64  
65 #define APSE_TEST_HIGH_BIT(i) \
66 (((i) & ((apse_vec_t)1 << (APSE_BITS_IN_BITVEC - 1))) ? 1 : 0)
67  
68 /* In case you are reading the TR 91-11 of University of Arizona, page 6:
69 * j+1 state
70 * j prev_state
71 * d i
72 * d-1 prev_i
73 */
74  
75 #define APSE_NEXT_EXACT(state, prev_state, text, i, carry) \
76 (state[i] = ((prev_state[i] << 1 | carry) & text))
77  
78 #define APSE_NEXT_APPROX(state, prev_state, text, i, prev_i, carry) \
79 (state[i] = (((prev_state[i] << 1) & text) | \
80 prev_state[prev_i] | \
81 ((state[prev_i] | prev_state[prev_i]) << 1) | \
82 carry))
83  
84 #define APSE_NEXT_COMMON(state, prev_state, text, i) \
85 (state[i] = (prev_state[i] << 1) & text)
86  
87 #define APSE_NEXT_INSERT(state, prev_state, i, prev_i) \
88 (state[i] |= prev_state[prev_i])
89  
90 #define APSE_NEXT_DELETE(state, i, prev_i) \
91 (state[i] |= (state[prev_i] << 1))
92  
93 #define APSE_NEXT_SUBSTI(state, prev_state, i, prev_i) \
94 (state[i] |= (prev_state[prev_i] << 1))
95  
96 #define APSE_NEXT_CARRY(state, i, carry) \
97 (state[i] |= carry)
98  
99 #define APSE_EXACT_MATCH_BEGIN(ap) (ap->state[0] & 1)
100  
101 #define APSE_APPROX_MATCH_BEGIN(ap) \
102 (ap->state[ap->largest_distance + ap->match_begin_bitvector] > \
103 ap->match_begin_prefix && \
104 ap->state[ap->largest_distance + ap->match_begin_bitvector] & \
105 ap->match_begin_prefix)
106  
107 #define APSE_PREFIX_DELETE_MASK(ap) \
108 do { if (ap->edit_deletions < ap->edit_distance && \
109 ap->text_position < ap->edit_distance) \
110 ap->state[h] &= ap->match_begin_bitmask; } while (0)
111  
112 #define APSE_DEBUG_SINGLE(ap, i) \
113 APSE_DEBUG(printf("%c %2ld %2ld %s\n", \
114 isprint(ap->text[ap->text_position])? \
115 ap->text[ap->text_position]:'.', \
116 ap->text_position, i, \
117 _apse_fbin(ap->state[i], \
118 ap->pattern_size, 1)))
119  
120  
121 #define APSE_DEBUG_MULTIPLE_FIRST(ap, i) \
122 APSE_DEBUG(printf("%c %2ld %2ld", \
123 isprint(ap->text[ap->text_position])? \
124 ap->text[ap->text_position]:'.', \
125 ap->text_position, i))
126  
127 #define APSE_DEBUG_MULTIPLE_REST(ap, i, j) \
128 APSE_DEBUG(printf(" %s", \
129 _apse_fbin(ap->state[j], \
130 ap->pattern_size, \
131 i == ap->bitvectors_in_state-1)))
132  
133  
134 #ifdef APSE_DEBUGGING
135 static char *_apse_fbin(apse_vec_t v, apse_size_t n, apse_bool_t last);
136  
137 static char *_apse_fbin(apse_vec_t v, apse_size_t n, apse_bool_t last) {
138 static char s[APSE_BITS_IN_BITVEC + 1] = { 0 }; /* non-reentrant */
139  
140 if (v) {
141 static const char *b =
142 "0000100001001100001010100110111000011001010111010011101101111111";
143 apse_size_t i;
144  
145 for (i = 0; i < APSE_BITS_IN_BITVEC && i < n && v; i += 4) {
146 (void)memcpy(s + i, b + ((v & 0x0f) << 2), (size_t)4);
147 v >>= 4;
148 }
149  
150 if (i < APSE_BITS_IN_BITVEC)
151 memset(s + i, '0', APSE_BITS_IN_BITVEC - i);
152 } else
153 memset(s, '0', APSE_BITS_IN_BITVEC);
154  
155 if (last)
156 s[n % APSE_BITS_IN_BITVEC] = 0;
157  
158 return s;
159 }
160 #endif
161  
162 /* The code begins. */
163  
164 88 apse_bool_t apse_set_pattern(apse_t* ap,
165 unsigned char* pattern,
166 apse_size_t pattern_size) {
167 apse_size_t i;
168  
169 88 ▶ if (ap->case_mask)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->case_mask) 0 88 88
170 0 free(ap->case_mask);
171 88 ▶ if (ap->fold_mask)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->fold_mask) 0 88 88
172 0 free(ap->fold_mask);
173  
174 88 ap->pattern_mask = 0;
175 88 ap->fold_mask = 0;
176 88 ap->case_mask = 0;
177  
178 88 ap->is_greedy = 0;
179  
180 88 ap->prev_equal = 0;
181 88 ap->prev_active = 0;
182  
183 88 ap->pattern_size = pattern_size;
184 88 ap->bitvectors_in_state = (pattern_size - 1)/APSE_BITS_IN_BITVEC + 1;
185  
186 88 ▶ if (ap->edit_distance)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->edit_distance) 0 88 88
187 0 ap->largest_distance = ap->edit_distance * ap->bitvectors_in_state;
188 else
189 88 ap->largest_distance = 0;
190  
191 88 ap->bytes_in_state = ap->bitvectors_in_state * sizeof(apse_vec_t);
192  
193 88 ap->case_mask = calloc((apse_size_t)APSE_CHAR_MAX, ap->bytes_in_state);
194 88 ▶ if (ap->case_mask == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->case_mask == 0) 0 88 88
195 0 goto out;
196  
197 1931 ▶ for (i = 0; i < pattern_size; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pattern_size; i++) 1843 88 1931
198 1843 APSE_BIT_SET(ap->case_mask,
199 (unsigned)pattern[i],
200 ap->bitvectors_in_state, i);
201  
202 88 ap->pattern_mask = ap->case_mask;
203  
204 88 ap->match_end_bitmask =
205 88 (apse_vec_t)1 << ((pattern_size - 1) % APSE_BITS_IN_BITVEC);
206  
207 88 out:
208 88 ▶ if (ap && ap->case_mask)
Branch: 2 branches
BranchTrueFalseTotal
if (ap && ap->case_mask) 88 0 88
if (ap && ap->case_mask) 88 0 88
209 88 return 1;
210 else {
211 0 ▶ if (ap->case_mask)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->case_mask) 0 0 0
212 0 free(ap->case_mask);
213 0 ▶ if (ap)
Branch: 1 branch
BranchTrueFalseTotal
if (ap) 0 0 0
214 0 free(ap);
215 0 return 0;
216 }
217 }
218  
219 115 void apse_set_greedy(apse_t *ap, apse_bool_t greedy) {
220 115 ap->is_greedy = greedy;
221 115 }
222  
223 0 apse_bool_t apse_get_greedy(apse_t *ap) {
224 0 return ap->is_greedy;
225 }
226  
227 0 void apse_set_match_bot_callback(apse_t *ap,
228 void* (*match_bot_callback)(apse_t* ap)) {
229 0 ap->match_bot_callback = match_bot_callback;
230 0 }
231  
232 0 void apse_set_match_begin_callback(apse_t *ap,
233 void* (*match_begin_callback)(apse_t* ap)) {
234 0 ap->match_begin_callback = match_begin_callback;
235 0 }
236  
237 0 void apse_set_match_fail_callback(apse_t *ap,
238 void* (*match_fail_callback)(apse_t* ap)) {
239 0 ap->match_fail_callback = match_fail_callback;
240 0 }
241  
242 0 void apse_set_match_end_callback(apse_t *ap,
243 void* (*match_end_callback)(apse_t* ap)) {
244 0 ap->match_end_callback = match_end_callback;
245 0 }
246  
247 0 void apse_set_match_eot_callback(apse_t *ap,
248 void* (*match_eot_callback)(apse_t* ap)) {
249 0 ap->match_eot_callback = match_eot_callback;
250 0 }
251  
252 0 void* (*apse_get_match_bot_callback(apse_t * ap))(apse_t *ap) {
253 0 return ap->match_bot_callback;
254 }
255  
256 0 void* (*apse_get_match_begin_callback(apse_t * ap))(apse_t *ap) {
257 0 return ap->match_begin_callback;
258 }
259  
260 0 void* (*apse_get_match_fail_callback(apse_t * ap))(apse_t *ap) {
261 0 return ap->match_fail_callback;
262 }
263  
264 0 void* (*apse_get_match_end_callback(apse_t * ap))(apse_t *ap) {
265 0 return ap->match_end_callback;
266 }
267  
268 0 void* (*apse_get_match_eot_callback(apse_t * ap))(apse_t *ap) {
269 0 return ap->match_eot_callback;
270 }
271  
272 7 static int _apse_wrap_slice(apse_t* ap,
273 apse_ssize_t begin_in,
274 apse_ssize_t size_in,
275 apse_ssize_t* begin_out,
276 apse_ssize_t* size_out) {
277 7 ▶ if (begin_in < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (begin_in < 0) { 0 7 7
278 0 ▶ if ((apse_size_t)-begin_in > ap->pattern_size)
Branch: 1 branch
BranchTrueFalseTotal
if ((apse_size_t)-begin_in > ap->pattern_size) 0 0 0
279 0 return 0;
280 0 begin_in = ap->pattern_size + begin_in;
281 }
282  
283 7 ▶ if (size_in < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (size_in < 0) { 0 7 7
284 0 ▶ if (-size_in > begin_in)
Branch: 1 branch
BranchTrueFalseTotal
if (-size_in > begin_in) 0 0 0
285 0 return 0;
286 0 size_in = -size_in;
287 0 begin_in -= size_in;
288 }
289  
290 7 ▶ if ((apse_size_t)begin_in >= ap->pattern_size)
Branch: 1 branch
BranchTrueFalseTotal
if ((apse_size_t)begin_in >= ap->pattern_size) 0 7 7
291 0 return 0;
292
293 7 ▶ if ((apse_size_t)begin_in + size_in > ap->pattern_size)
Branch: 1 branch
BranchTrueFalseTotal
if ((apse_size_t)begin_in + size_in > ap->pattern_size) 0 7 7
294 0 size_in = ap->pattern_size - begin_in;
295  
296 7 ▶ if (begin_out)
Branch: 1 branch
BranchTrueFalseTotal
if (begin_out) 7 0 7
297 7 *begin_out = begin_in;
298  
299 7 ▶ if (size_out)
Branch: 1 branch
BranchTrueFalseTotal
if (size_out) 7 0 7
300 7 *size_out = size_in;
301  
302 7 return 1;
303 }
304  
305 0 apse_bool_t apse_set_anychar(apse_t *ap, apse_ssize_t pattern_index) {
306 0 apse_size_t bitvectors_in_state = ap->bitvectors_in_state;
307 apse_ssize_t true_index, i;
308 0 apse_bool_t okay = 0;
309  
310 0 ▶ if (!_apse_wrap_slice(ap, pattern_index, (apse_ssize_t)1,
Branch: 1 branch
BranchTrueFalseTotal
if (!_apse_wrap_slice(ap, pattern_index, (apse_ssize_t)1, 0 0 0
311 &true_index, 0))
312 0 goto out;
313  
314 0 ▶ for (i = 0; i < APSE_CHAR_MAX; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < APSE_CHAR_MAX; i++) 0 0 0
315 0 APSE_BIT_SET(ap->case_mask,
316 i, bitvectors_in_state, pattern_index);
317 0 ▶ if (ap->fold_mask)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->fold_mask) 0 0 0
318 0 ▶ for (i = 0; i < APSE_CHAR_MAX; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < APSE_CHAR_MAX; i++) 0 0 0
319 0 APSE_BIT_SET(ap->fold_mask,
320 i, bitvectors_in_state, pattern_index);
321  
322 0 okay = 1;
323  
324 0 out:
325
326 0 return okay;
327 }
328  
329 0 apse_bool_t apse_set_charset(apse_t* ap,
330 apse_ssize_t pattern_index,
331 unsigned char* set,
332 apse_size_t set_size,
333 apse_bool_t complement) {
334 0 apse_size_t bitvectors_in_state = ap->bitvectors_in_state;
335 apse_ssize_t true_index;
336 0 apse_bool_t okay = 0;
337 apse_size_t i;
338  
339 0 ▶ if (!_apse_wrap_slice(ap, pattern_index, (apse_ssize_t)1,
Branch: 1 branch
BranchTrueFalseTotal
if (!_apse_wrap_slice(ap, pattern_index, (apse_ssize_t)1, 0 0 0
340 &true_index, 0))
341 0 goto out;
342
343 0 ▶ if (complement) {
Branch: 1 branch
BranchTrueFalseTotal
if (complement) { 0 0 0
344 0 ▶ for (i = 0; i < set_size; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < set_size; i++) 0 0 0
345 0 APSE_BIT_CLR(ap->case_mask,
346 (unsigned)set[i],
347 bitvectors_in_state, true_index);
348 } else {
349 0 ▶ for (i = 0; i < set_size; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < set_size; i++) 0 0 0
350 0 APSE_BIT_SET(ap->case_mask,
351 (unsigned)set[i],
352 bitvectors_in_state, true_index);
353 }
354 0 ▶ if (ap->fold_mask)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->fold_mask) 0 0 0
355 0 apse_set_caseignore_slice(ap, pattern_index,
356 (apse_ssize_t)1, (apse_bool_t)1);
357  
358 0 okay = 1;
359  
360 0 out:
361  
362 0 return okay;
363 }
364  
365 1158 static void _apse_reset_state(apse_t* ap) {
366 apse_size_t i, j;
367  
368 1158 (void)memset(ap->state, 0, ap->bytes_in_all_states);
369 1158 (void)memset(ap->prev_state, 0, ap->bytes_in_all_states);
370  
371 1158 ap->prev_equal = 0;
372 1158 ap->prev_active = 0;
373  
374 2776 ▶ for (i = 1; i <= ap->edit_distance; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i <= ap->edit_distance; i++) { 1618 1158 2776
375 4751 ▶ for (j = 0; j < i; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < i; j++) { 3133 1618 4751
376 #ifdef APSE_DEBUGGING
377 int k = APSE_IDX(i, ap->bitvectors_in_state, j);
378 int l = ap->bytes_in_all_states/sizeof(apse_vec_t);
379 assert (k < l);
380 #endif
381 3133 APSE_BIT_SET(ap->prev_state, i, ap->bitvectors_in_state, j);
382 }
383 }
384 1158 }
385  
386 0 apse_bool_t apse_set_text_position(apse_t *ap,
387 apse_size_t text_position) {
388 0 ap->text_position = text_position;
389  
390 0 return 1;
391 }
392  
393 0 apse_size_t apse_get_text_position(apse_t *ap) {
394 0 return ap->text_position;
395 }
396  
397 5 apse_bool_t apse_set_text_initial_position(apse_t *ap,
398 apse_size_t text_initial_position) {
399 5 ap->text_initial_position = text_initial_position;
400  
401 5 return 1;
402 }
403  
404 0 apse_size_t apse_get_text_initial_position(apse_t *ap) {
405 0 return ap->text_initial_position;
406 }
407  
408 4 apse_bool_t apse_set_text_final_position(apse_t *ap,
409 apse_size_t text_final_position) {
410 4 ap->text_final_position = text_final_position;
411  
412 4 return 1;
413 }
414  
415 0 apse_size_t apse_get_text_final_position(apse_t *ap) {
416 0 return ap->text_final_position;
417 }
418  
419 0 apse_bool_t apse_set_text_position_range(apse_t *ap,
420 apse_size_t text_position_range) {
421 0 ap->text_position_range = text_position_range;
422  
423 0 return 1;
424 }
425  
426 0 apse_size_t apse_get_text_position_range(apse_t *ap) {
427 0 return ap->text_position_range;
428 }
429  
430 1151 void apse_reset(apse_t *ap) {
431 1151 _apse_reset_state(ap);
432  
433 1151 ap->text_position = ap->text_initial_position;
434 #if 0
435 ap->text_position_range = APSE_MATCH_BAD; /* Do not reset this. */
436 #endif
437  
438 1151 ap->match_state = APSE_MATCH_STATE_BOT;
439 1151 ap->match_begin = APSE_MATCH_BAD;
440 1151 ap->match_end = APSE_MATCH_BAD;
441 1151 }
442  
443 224 apse_bool_t apse_set_edit_distance(apse_t *ap, apse_size_t edit_distance) {
444 /* TODO: waste not--reuse if possible */
445  
446 224 ▶ if (ap->state)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->state) 136 88 224
447 136 free(ap->state);
448 224 ▶ if (ap->prev_state)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->prev_state) 136 88 224
449 136 free(ap->prev_state);
450  
451 224 ▶ if (edit_distance >= ap->pattern_size)
Branch: 1 branch
BranchTrueFalseTotal
if (edit_distance >= ap->pattern_size) 13 211 224
452 13 edit_distance = ap->pattern_size;
453  
454 224 ap->edit_distance = edit_distance;
455  
456 224 ap->bytes_in_all_states = (edit_distance + 1) * ap->bytes_in_state;
457  
458 224 ap->state = ap->prev_state = 0;
459  
460 224 ap->state = calloc(edit_distance + 1, ap->bytes_in_state);
461 224 ▶ if (ap->state == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->state == 0) 0 224 224
462 0 goto out;
463  
464 224 ap->prev_state = calloc(edit_distance + 1, ap->bytes_in_state);
465 224 ▶ if (ap->prev_state == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->prev_state == 0) 0 224 224
466 0 goto out;
467  
468 224 apse_reset(ap);
469  
470 224 ▶ if (!ap->has_different_distances) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ap->has_different_distances) { 224 0 224
471 224 ap->edit_insertions = edit_distance;
472 224 ap->edit_deletions = edit_distance;
473 224 ap->edit_substitutions = edit_distance;
474 }
475  
476 224 ▶ if (ap->edit_distance && ap->bitvectors_in_state)
Branch: 2 branches
BranchTrueFalseTotal
if (ap->edit_distance && ap->bitvectors_in_state) 182 42 224
if (ap->edit_distance && ap->bitvectors_in_state) 182 0 182
477 182 ap->largest_distance = ap->edit_distance * ap->bitvectors_in_state;
478 else
479 42 ap->largest_distance = 0;
480  
481 224 ap->match_begin_bitvector =
482 224 (edit_distance + 1) / APSE_BITS_IN_BITVEC;
483 224 ap->match_begin_prefix = ((apse_vec_t)1 << edit_distance) - 1;
484 224 ap->match_begin_bitmask =
485 224 ((apse_vec_t)1 << edit_distance) - 1;
486  
487 224 ap->match_end_bitvector =
488 224 (ap->pattern_size - 1) / APSE_BITS_IN_BITVEC;
489  
490 #ifdef APSE_DEBUGGING
491 if (ap->has_different_distances) {
492 printf("(edit distances: ");
493 printf("insertions = %ld, deletions = %ld, substitutions = %ld)\n",
494 ap->edit_insertions,
495 ap->edit_deletions,
496 ap->edit_substitutions);
497 } else
498 printf("(edit_distance = %ld)\n", ap->edit_distance);
499 #endif
500  
501 224 out:
502 224 ▶ return ap->state && ap->prev_state;
Branch: 2 branches
BranchTrueFalseTotal
return ap->state && ap->prev_state; 224 0 224
return ap->state && ap->prev_state; 224 0 224
503 }
504  
505 0 apse_size_t apse_get_edit_distance(apse_t *ap) {
506 0 return ap->edit_distance;
507 }
508  
509 8 apse_bool_t apse_set_minimal_distance(apse_t* ap, apse_bool_t minimal) {
510 8 ap->use_minimal_distance = minimal;
511 8 return 1;
512 }
513  
514 0 apse_bool_t apse_get_minimal_distance(apse_t *ap) {
515 0 return ap->use_minimal_distance;
516 }
517  
518 0 apse_bool_t apse_set_exact_slice(apse_t* ap,
519 apse_ssize_t exact_begin,
520 apse_ssize_t exact_size,
521 apse_bool_t exact) {
522 apse_ssize_t true_begin, true_size;
523 0 apse_bool_t okay = 0;
524 apse_size_t i, j;
525  
526 0 ▶ if (!ap->exact_mask) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ap->exact_mask) { 0 0 0
527  
528 0 ap->exact_mask = calloc((size_t)1, ap->bytes_in_state);
529 0 ▶ if (ap->exact_mask == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->exact_mask == 0) 0 0 0
530 0 goto out;
531  
532 0 ap->exact_positions = 0;
533 }
534  
535 0 ▶ if (!_apse_wrap_slice(ap, exact_begin, exact_size,
Branch: 1 branch
BranchTrueFalseTotal
if (!_apse_wrap_slice(ap, exact_begin, exact_size, 0 0 0
536 &true_begin, &true_size))
537 0 goto out;
538  
539 0 ▶ if (exact) {
Branch: 1 branch
BranchTrueFalseTotal
if (exact) { 0 0 0
540 0 for (i = true_begin, j = true_begin + true_size;
541 0 ▶ i < j && i < ap->pattern_size; i++) {
Branch: 2 branches
BranchTrueFalseTotal
i < j && i < ap->pattern_size; i++) { 0 0 0
i < j && i < ap->pattern_size; i++) { 0 0 0
542 0 ▶ if (!APSE_BIT_TST(ap->exact_mask, 0, 0, i))
Branch: 1 branch
BranchTrueFalseTotal
if (!APSE_BIT_TST(ap->exact_mask, 0, 0, i)) 0 0 0
543 0 ap->exact_positions++;
544 0 APSE_BIT_SET(ap->exact_mask, 0, 0, i);
545 }
546 } else {
547 0 for (i = true_begin, j = true_begin + true_size;
548 0 ▶ i < j && i < ap->pattern_size; i++) {
Branch: 2 branches
BranchTrueFalseTotal
i < j && i < ap->pattern_size; i++) { 0 0 0
i < j && i < ap->pattern_size; i++) { 0 0 0
549 0 ▶ if (APSE_BIT_TST(ap->exact_mask, 0, 0, i))
Branch: 1 branch
BranchTrueFalseTotal
if (APSE_BIT_TST(ap->exact_mask, 0, 0, i)) 0 0 0
550 0 ap->exact_positions--;
551 0 APSE_BIT_CLR(ap->exact_mask, 0, 0, i);
552 }
553 }
554  
555 0 okay = 1;
556  
557 0 out:
558 0 return okay;
559 }
560  
561 7 apse_bool_t apse_set_caseignore_slice(apse_t* ap,
562 apse_ssize_t caseignore_begin,
563 apse_ssize_t caseignore_size,
564 apse_bool_t caseignore) {
565 apse_size_t i, j;
566 int k;
567 apse_ssize_t true_begin, true_size;
568 7 apse_bool_t okay = 0;
569  
570 7 ▶ if (!ap->fold_mask) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ap->fold_mask) { 7 0 7
571  
572 7 ap->fold_mask = calloc((apse_size_t)APSE_CHAR_MAX,
573 ap->bytes_in_state);
574 7 ▶ if (ap->fold_mask == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->fold_mask == 0) 0 7 7
575 0 goto out;
576  
577 7 memcpy(ap->fold_mask,
578 7 ap->case_mask,
579 7 APSE_CHAR_MAX * ap->bytes_in_state);
580  
581 7 ap->pattern_mask = ap->fold_mask;
582 }
583  
584 7 ▶ if (!_apse_wrap_slice(ap, caseignore_begin, caseignore_size,
Branch: 1 branch
BranchTrueFalseTotal
if (!_apse_wrap_slice(ap, caseignore_begin, caseignore_size, 0 7 7
585 &true_begin, &true_size))
586 0 goto out;
587  
588 7 ▶ if (caseignore) {
Branch: 1 branch
BranchTrueFalseTotal
if (caseignore) { 7 0 7
589 7 for (i = true_begin, j = true_begin + true_size;
590 53 ▶ i < j && i < ap->pattern_size; i++) {
Branch: 2 branches
BranchTrueFalseTotal
i < j && i < ap->pattern_size; i++) { 46 7 53
i < j && i < ap->pattern_size; i++) { 46 0 46
591 11822 ▶ for (k = 0; k < APSE_CHAR_MAX; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < APSE_CHAR_MAX; k++) { 11776 46 11822
592 11776 ▶ if (APSE_BIT_TST(ap->case_mask,
Branch: 1 branch
BranchTrueFalseTotal
if (APSE_BIT_TST(ap->case_mask, 46 11730 11776
593 k, ap->bitvectors_in_state, i)) {
594 46 ▶ if (isupper(k))
Branch: 1 branch
BranchTrueFalseTotal
if (isupper(k)) 2 44 46
595 2 APSE_BIT_SET(ap->fold_mask,
596 tolower(k),
597 ap->bitvectors_in_state, i);
598 44 ▶ else if (islower(k))
Branch: 1 branch
BranchTrueFalseTotal
else if (islower(k)) 39 5 44
599 39 APSE_BIT_SET(ap->fold_mask,
600 toupper(k),
601 ap->bitvectors_in_state, i);
602 }
603 }
604 }
605 } else {
606 0 for (i = true_begin, j = true_begin + true_size;
607 0 ▶ i < j && i < ap->pattern_size; i++) {
Branch: 2 branches
BranchTrueFalseTotal
i < j && i < ap->pattern_size; i++) { 0 0 0
i < j && i < ap->pattern_size; i++) { 0 0 0
608 0 ▶ for (k = 0; k < APSE_CHAR_MAX; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < APSE_CHAR_MAX; k++) { 0 0 0
609 0 ▶ if (APSE_BIT_TST(ap->case_mask,
Branch: 1 branch
BranchTrueFalseTotal
if (APSE_BIT_TST(ap->case_mask, 0 0 0
610 k, ap->bitvectors_in_state, i)) {
611 0 ▶ if (isupper(k))
Branch: 1 branch
BranchTrueFalseTotal
if (isupper(k)) 0 0 0
612 0 APSE_BIT_CLR(ap->fold_mask,
613 tolower(k),
614 ap->bitvectors_in_state, i);
615 else
616 0 ▶ if (islower(k))
Branch: 1 branch
BranchTrueFalseTotal
if (islower(k)) 0 0 0
617 0 APSE_BIT_CLR(ap->fold_mask,
618 toupper(k),
619 ap->bitvectors_in_state, i);
620 }
621 }
622 }
623 }
624  
625 7 okay = 1;
626  
627 7 out:
628 7 return okay;
629 }
630  
631 88 void apse_destroy(apse_t *ap) {
632 88 ▶ if (ap->case_mask) free(ap->case_mask);
Branch: 1 branch
BranchTrueFalseTotal
if (ap->case_mask) free(ap->case_mask); 88 0 88
633 88 ▶ if (ap->fold_mask) free(ap->fold_mask);
Branch: 1 branch
BranchTrueFalseTotal
if (ap->fold_mask) free(ap->fold_mask); 7 81 88
634 88 ▶ if (ap->state) free(ap->state);
Branch: 1 branch
BranchTrueFalseTotal
if (ap->state) free(ap->state); 88 0 88
635 88 ▶ if (ap->prev_state) free(ap->prev_state);
Branch: 1 branch
BranchTrueFalseTotal
if (ap->prev_state) free(ap->prev_state); 88 0 88
636 88 ▶ if (ap->exact_mask) free(ap->exact_mask);
Branch: 1 branch
BranchTrueFalseTotal
if (ap->exact_mask) free(ap->exact_mask); 0 88 88
637 88 free(ap);
638 88 }
639  
640 88 apse_t *apse_create(unsigned char* pattern,
641 apse_size_t pattern_size,
642 apse_size_t edit_distance) {
643 apse_t *ap;
644 88 apse_bool_t okay = 0;
645  
646 APSE_DEBUG(printf("(apse version %u.%u)\n",
647 APSE_MAJOR_VERSION, APSE_MINOR_VERSION));
648  
649 APSE_DEBUG(
650 printf("(pattern = \"%s\", pattern_size = %ld)\n",
651 pattern, pattern_size));
652  
653 88 ap = calloc((size_t)1, sizeof(*ap));
654 88 ▶ if (ap == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ap == 0) 0 88 88
655 0 return 0;
656  
657 88 ap->pattern_size = 0;
658 88 ap->pattern_mask = 0;
659  
660 88 ap->edit_distance = 0;
661 88 ap->has_different_distances = 0;
662 88 ap->edit_insertions = 0;
663 88 ap->edit_deletions = 0;
664 88 ap->edit_substitutions = 0;
665 88 ap->use_minimal_distance = 0;
666  
667 88 ap->bitvectors_in_state = 0;
668 88 ap->bytes_in_state = 0;
669 88 ap->bytes_in_all_states = 0;
670 88 ap->largest_distance = 0;
671  
672 88 ap->text = 0;
673 88 ap->text_size = 0;
674 88 ap->text_position = 0;
675 88 ap->text_initial_position = 0;
676 88 ap->text_final_position = APSE_MATCH_BAD;
677 88 ap->text_position_range = APSE_MATCH_BAD;
678  
679 88 ap->state = 0;
680 88 ap->prev_state = 0;
681 88 ap->match_begin_bitmask = 0;
682 88 ap->match_begin_prefix = 0;
683 88 ap->match_end_bitvector = 0;
684 88 ap->match_end_bitmask = 0;
685 88 ap->match_state = APSE_MATCH_STATE_BOT;
686 88 ap->match_begin = APSE_MATCH_BAD;
687 88 ap->match_end = APSE_MATCH_BAD;
688  
689 88 ap->match_bot_callback = 0;
690 88 ap->match_begin_callback = 0;
691 88 ap->match_fail_callback = 0;
692 88 ap->match_end_callback = 0;
693 88 ap->match_eot_callback = 0;
694  
695 88 ap->exact_positions = 0;
696 88 ap->exact_mask = 0;
697  
698 88 ap->is_greedy = 0;
699  
700 88 ap->custom_data = 0;
701 88 ap->custom_data_size = 0;
702  
703 88 ▶ if (!apse_set_pattern(ap, (unsigned char *)pattern, pattern_size))
Branch: 1 branch
BranchTrueFalseTotal
if (!apse_set_pattern(ap, (unsigned char *)pattern, pattern_size)) 0 88 88
704 0 goto out;
705  
706 88 ▶ if (!apse_set_edit_distance(ap, edit_distance))
Branch: 1 branch
BranchTrueFalseTotal
if (!apse_set_edit_distance(ap, edit_distance)) 0 88 88
707 0 goto out;
708  
709 88 ap->edit_insertions = ap->edit_deletions =
710 88 ap->edit_substitutions = ap->edit_distance;
711  
712 88 ap->largest_distance = edit_distance * ap->bitvectors_in_state;
713  
714 #ifdef APSE_DEBUGGING
715 printf("(size of bitvector = %ld, bitvectors_in_state = %ld)\n",
716 (long)sizeof(apse_vec_t), ap->bitvectors_in_state);
717 printf("(bytes_in_state = %ld, states = %ld, bytes_in_all_states = %ld)\n",
718 ap->bytes_in_state, ap->edit_distance + 1, ap->bytes_in_all_states);
719 printf("(match_begin_bitvector = %ld, match_begin_bitmask = %s)\n",
720 ap->match_begin_bitvector,
721 _apse_fbin(ap->match_begin_bitmask, ap->pattern_size, 1));
722 printf("(match_end_bitvector = %ld, match_end_bitmask = %s)\n",
723 ap->match_end_bitvector,
724 _apse_fbin(ap->match_end_bitmask, ap->pattern_size, 1));
725 printf("(largest_distance = %ld, match_begin_prefix = %s)\n",
726 ap->largest_distance,
727 _apse_fbin(ap->match_begin_prefix, ap->pattern_size, 1));
728 if (ap->bitvectors_in_state == 1)
729 printf("(single bitvector");
730 else
731 printf("(multiple bitvectors");
732 printf(")\n");
733 #endif
734 88 okay = 1;
735  
736 88 out:
737 88 ▶ if (!okay) {
Branch: 1 branch
BranchTrueFalseTotal
if (!okay) { 0 88 88
738 0 apse_destroy(ap);
739 0 ap = 0;
740 }
741  
742 88 return ap;
743 }
744  
745 8 apse_bool_t apse_set_insertions(apse_t *ap, apse_size_t insertions) {
746 8 apse_bool_t okay = 0;
747  
748 8 ▶ if (insertions > ap->edit_distance)
Branch: 1 branch
BranchTrueFalseTotal
if (insertions > ap->edit_distance) 0 8 8
749 0 insertions = ap->edit_distance;
750 8 ap->edit_insertions = insertions;
751 8 ap->has_different_distances = 1;
752  
753 APSE_DEBUG((printf("(edit distances: insertions = %ld, deletions = %ld, substitutions = %ld)\n",
754 ap->edit_insertions,
755 ap->edit_deletions,
756 ap->edit_substitutions)));
757  
758 8 okay = 1;
759  
760 8 return okay;
761 }
762  
763 0 apse_size_t apse_get_insertions(apse_t *ap) {
764 0 ▶ if (ap->has_different_distances)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->has_different_distances) 0 0 0
765 0 return ap->edit_insertions;
766 else
767 0 return ap->edit_distance;
768 }
769  
770 3 apse_bool_t apse_set_deletions(apse_t *ap, apse_size_t deletions) {
771 3 apse_bool_t okay = 0;
772  
773 3 ▶ if (deletions > ap->edit_distance)
Branch: 1 branch
BranchTrueFalseTotal
if (deletions > ap->edit_distance) 0 3 3
774 0 deletions = ap->edit_distance;
775 3 ap->edit_deletions = deletions;
776 3 ap->has_different_distances = 1;
777  
778 APSE_DEBUG((printf("(edit distances: insertions = %ld, deletions = %ld, substitutions = %ld)\n",
779 ap->edit_insertions,
780 ap->edit_deletions,
781 ap->edit_substitutions)));
782  
783 3 okay = 1;
784  
785 3 return okay;
786 }
787  
788 0 apse_size_t apse_get_deletions(apse_t *ap) {
789 0 ▶ if (ap->has_different_distances)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->has_different_distances) 0 0 0
790 0 return ap->edit_deletions;
791 else
792 0 return ap->edit_distance;
793 }
794  
795 3 apse_bool_t apse_set_substitutions(apse_t *ap, apse_size_t substitutions) {
796 3 apse_bool_t okay = 0;
797  
798 3 ▶ if (substitutions > ap->edit_distance)
Branch: 1 branch
BranchTrueFalseTotal
if (substitutions > ap->edit_distance) 0 3 3
799 0 substitutions = ap->edit_distance;
800 3 ap->edit_substitutions = substitutions;
801 3 ap->has_different_distances = 1;
802  
803 APSE_DEBUG((printf("(edit distances: insertions = %ld, deletions = %ld, substitutions = %ld)\n",
804 ap->edit_insertions,
805 ap->edit_deletions,
806 ap->edit_substitutions)));
807  
808 3 okay = 1;
809  
810 3 return okay;
811 }
812  
813 0 apse_size_t apse_get_substitutions(apse_t *ap) {
814 0 ▶ if (ap->has_different_distances)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->has_different_distances) 0 0 0
815 0 return ap->edit_substitutions;
816 else
817 0 return ap->edit_distance;
818 }
819  
820 #ifdef APSE_DEBUGGING
821 static const char* apse_match_state_name(apse_t* ap) {
822 switch (ap->match_state) {
823 case APSE_MATCH_STATE_BOT: return "BOT";
824 case APSE_MATCH_STATE_SEARCH: return "SEARCH";
825 case APSE_MATCH_STATE_BEGIN: return "BEGIN";
826 case APSE_MATCH_STATE_FAIL: return "FAIL";
827 case APSE_MATCH_STATE_GREEDY: return "GREEDY";
828 case APSE_MATCH_STATE_END: return "END";
829 case APSE_MATCH_STATE_EOT: return "EOT";
830 default: return "***UNKNOWN***";
831 }
832 }
833 #endif
834  
835 676 static void _apse_match_bot(apse_t *ap) {
836 APSE_DEBUG(printf("(text = \"%*.*s\", text_size = %ld)\n",
837 (int)ap->text_size, (int)ap->text_size, ap->text,
838 ap->text_size));
839 676 apse_reset(ap);
840 APSE_DEBUG(printf("(match state %s)\n", apse_match_state_name(ap)));
841 APSE_DEBUG(printf("(text begin %ld)\n", ap->text_position));
842 676 ▶ if (ap->match_bot_callback)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->match_bot_callback) 0 676 676
843 0 ap->match_bot_callback(ap);
844 676 }
845  
846 583 static void _apse_match_begin(apse_t *ap) {
847 583 ap->match_state = APSE_MATCH_STATE_BEGIN;
848 APSE_DEBUG(printf("(match state %s)\n", apse_match_state_name(ap)));
849 APSE_DEBUG(printf("(match begin %ld)\n", ap->text_position));
850 583 ap->match_begin = ap->text_position;
851 583 ▶ if (ap->match_begin_callback)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->match_begin_callback) 0 583 583
852 0 ap->match_begin_callback(ap);
853 583 }
854  
855 22 static void _apse_match_fail(apse_t *ap) {
856 22 ap->match_state = APSE_MATCH_STATE_FAIL;
857 APSE_DEBUG(printf("(match state %s)\n", apse_match_state_name(ap)));
858 22 ap->match_begin = APSE_MATCH_BAD;
859 APSE_DEBUG(printf("(match fail %ld)\n", ap->text_position));
860 22 ▶ if (ap->match_fail_callback)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->match_fail_callback) 0 22 22
861 0 ap->match_fail_callback(ap);
862 22 ap->match_state = APSE_MATCH_STATE_SEARCH;
863 APSE_DEBUG(printf("(match state %s)\n", apse_match_state_name(ap)));
864 22 }
865  
866 324 static void _apse_match_end(apse_t *ap) {
867 324 ap->match_state = APSE_MATCH_STATE_END;
868 APSE_DEBUG(printf("(match state %s)\n", apse_match_state_name(ap)));
869 #ifdef APSE_DEBUGGING
870 printf("(match end %ld)\n", ap->match_end);
871 printf("(match string \"%.*s\")\n",
872 (int)(ap->match_end - ap->match_begin + 1),
873 ap->text + ap->match_begin);
874 printf("(match length %ld)\n",
875 ap->match_end - ap->match_begin + 1);
876 #endif
877 324 ▶ if (ap->match_end_callback)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->match_end_callback) 0 324 324
878 0 ap->match_end_callback(ap);
879 324 ap->match_state = APSE_MATCH_STATE_SEARCH;
880 APSE_DEBUG(printf("(match state %s)\n", apse_match_state_name(ap)));
881 324 }
882  
883 1022 static void _apse_match_eot(apse_t *ap) {
884 1022 ap->match_state = APSE_MATCH_STATE_EOT;
885 APSE_DEBUG(printf("(match state %s)\n", apse_match_state_name(ap)));
886 1022 ap->text_position = ap->text_size;
887 APSE_DEBUG(printf("(text end %ld)\n", ap->text_position));
888 1022 ▶ if (ap->match_eot_callback)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->match_eot_callback) 0 1022 1022
889 0 ap->match_eot_callback(ap);
890 1022 }
891  
892 6828 static apse_bool_t _apse_match_next_state(apse_t *ap) {
893 apse_size_t h, i, j, k;
894 apse_vec_t match;
895  
896 6828 k = ap->edit_distance * ap->bitvectors_in_state;
897  
898 6828 switch (ap->match_state) {
899 1849 case APSE_MATCH_STATE_SEARCH:
900 1849 ▶ if (APSE_EXACT_MATCH_BEGIN(ap) || APSE_APPROX_MATCH_BEGIN(ap))
Branch: 3 branches
BranchTrueFalseTotal
if (APSE_EXACT_MATCH_BEGIN(ap) || APSE_APPROX_MATCH_BEGIN(ap)) 1498 351 1849
if (APSE_EXACT_MATCH_BEGIN(ap) || APSE_APPROX_MATCH_BEGIN(ap)) 232 1266 1498
if (APSE_EXACT_MATCH_BEGIN(ap) || APSE_APPROX_MATCH_BEGIN(ap)) 232 0 232
901 583 _apse_match_begin(ap);
902 1849 break;
903 4489 case APSE_MATCH_STATE_BEGIN:
904 {
905 4489 apse_size_t equal = 0;
906 4489 apse_size_t active = 0;
907  
908 4489 for (h = 0;
909 31363 ▶ h <= k;
Branch: 1 branch
BranchTrueFalseTotal
h <= k; 26874 4489 31363
910 26874 h += ap->bitvectors_in_state) {
911 89363 ▶ for (i = h, j = h + ap->bitvectors_in_state - 1; i < j; j--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = h, j = h + ap->bitvectors_in_state - 1; i < j; j--) 73785 15578 89363
912 73785 ▶ if (ap->state[j] != ap->prev_state[j])
Branch: 1 branch
BranchTrueFalseTotal
if (ap->state[j] != ap->prev_state[j]) 11296 62489 73785
913 11296 break;
914 26874 ▶ if (ap->prev_state[j] == ap->state[j])
Branch: 1 branch
BranchTrueFalseTotal
if (ap->prev_state[j] == ap->state[j]) 9303 17571 26874
915 9303 equal++;
916 26874 ▶ if (ap->state[j])
Branch: 1 branch
BranchTrueFalseTotal
if (ap->state[j]) 23026 3848 26874
917 23026 active++;
918 }
919 #ifdef APSE_DEBUGGING
920 printf("(equal = %d, active = %d)\n", equal, active);
921 #endif
922 4489 ▶ if ((equal == ap->edit_distance + 1 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((equal == ap->edit_distance + 1 && 693 3796 4489
923 693 ▶ ap->is_greedy == 0)
Branch: 1 branch
BranchTrueFalseTotal
ap->is_greedy == 0) 509 184 693
924 4305 ||
925 4305 ▶ (equal < ap->prev_equal &&
Branch: 1 branch
BranchTrueFalseTotal
(equal < ap->prev_equal && 242 4063 4305
926 242 ▶ ap->prev_active &&
Branch: 1 branch
BranchTrueFalseTotal
ap->prev_active && 242 0 242
927 242 ▶ active > ap->prev_active &&
Branch: 1 branch
BranchTrueFalseTotal
active > ap->prev_active && 43 199 242
928 43 ▶ ap->text_position - ap->match_begin < 8 * ap->bytes_in_state &&
Branch: 1 branch
BranchTrueFalseTotal
ap->text_position - ap->match_begin < 8 * ap->bytes_in_state && 43 0 43
929 43 ▶ !APSE_BIT_TST(ap->state,
Branch: 1 branch
BranchTrueFalseTotal
!APSE_BIT_TST(ap->state, 7 36 43
930 ap->edit_distance,
931 ap->bitvectors_in_state,
932 ap->text_position - ap->match_begin))) {
933 191 ap->match_begin = ap->text_position;
934 #ifdef APSE_DEBUGGING
935 printf("(slide begin %d)\n", ap->match_begin);
936 #endif
937 }
938 4298 ▶ else if (active == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (active == 0) 22 4276 4298
939 22 _apse_match_fail(ap);
940 4489 ap->prev_equal = equal;
941 4489 ap->prev_active = active;
942 }
943 4489 break;
944 490 default:
945 490 break;
946 }
947  
948 6828 for (match = 0, h = 0;
949 39633 ▶ h <= k;
Branch: 1 branch
BranchTrueFalseTotal
h <= k; 32805 6828 39633
950 32805 h += ap->bitvectors_in_state)
951 32805 match |= ap->state[h + ap->match_end_bitvector];
952  
953 6828 ▶ if (match & ap->match_end_bitmask) {
Branch: 1 branch
BranchTrueFalseTotal
if (match & ap->match_end_bitmask) { 630 6198 6828
954 630 ▶ if (ap->match_state == APSE_MATCH_STATE_BEGIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (ap->match_state == APSE_MATCH_STATE_BEGIN) { 311 319 630
955 311 ▶ if (ap->is_greedy) {
Branch: 1 branch
BranchTrueFalseTotal
if (ap->is_greedy) { 262 49 311
956 262 ap->match_state = APSE_MATCH_STATE_GREEDY;
957 APSE_DEBUG(printf("(match state %s)\n",
958 apse_match_state_name(ap)));
959 APSE_DEBUG(
960 printf("(greedy match continue %ld)\n",
961 ap->text_position));
962 } else {
963 49 ap->match_state = APSE_MATCH_STATE_END;
964 49 ap->match_end = ap->text_position;
965 }
966 }
967 6198 ▶ } else if (ap->match_state == APSE_MATCH_STATE_GREEDY) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ap->match_state == APSE_MATCH_STATE_GREEDY) { 171 6027 6198
968 171 ap->match_state = APSE_MATCH_STATE_END;
969 APSE_DEBUG(printf("(match state %s)\n", apse_match_state_name(ap)));
970 171 ap->match_end = ap->text_position - 1;
971 APSE_DEBUG(printf("(match end %ld)\n", ap->match_end));
972 }
973  
974 6828 return ap->match_state;
975 }
976  
977 0 static void _apse_exact_multiple(apse_t* ap) {
978 apse_size_t h;
979 0 apse_size_t g = ap->edit_distance * ap->bitvectors_in_state;
980  
981 0 ▶ for (h = 0; h < ap->bitvectors_in_state; h++)
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < ap->bitvectors_in_state; h++) 0 0 0
982 0 ap->state[g + h] &= ~ap->exact_mask[h];
983 0 }
984  
985 464 static apse_bool_t _apse_match_single_simple(apse_t *ap) {
986 /* single apse_vec_t, edit_distance */
987  
988 APSE_DEBUG(printf("(match single simple)\n"));
989 3334 ▶ for ( ; ap->text_position < ap->text_size; ap->text_position++) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; ap->text_position < ap->text_size; ap->text_position++) { 3038 296 3334
990 3038 apse_vec_t t =
991 3038 ap->pattern_mask[(unsigned)ap->text[ap->text_position] *
992 3038 ap->bitvectors_in_state];
993 apse_size_t h, g;
994 3038 APSE_NEXT_EXACT(ap->state, ap->prev_state, t, (apse_size_t)0, 1);
995 APSE_DEBUG_SINGLE(ap, (apse_size_t)0);
996  
997 7137 ▶ for (g = 0, h = 1; h <= ap->edit_distance; g = h, h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (g = 0, h = 1; h <= ap->edit_distance; g = h, h++) { 4099 3038 7137
998 4099 APSE_NEXT_APPROX(ap->state, ap->prev_state, t, h, g, 1);
999 APSE_DEBUG_SINGLE(ap, h);
1000 }
1001  
1002 3038 ▶ if (ap->exact_positions)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->exact_positions) 0 3038 3038
1003 0 ap->state[ap->edit_distance] &= ~ap->exact_mask[0];
1004  
1005 3038 ▶ if (_apse_match_next_state(ap) == APSE_MATCH_STATE_END)
Branch: 1 branch
BranchTrueFalseTotal
if (_apse_match_next_state(ap) == APSE_MATCH_STATE_END) 168 2870 3038
1006 168 return 1;
1007  
1008 2870 (void)memcpy(ap->prev_state, ap->state, ap->bytes_in_all_states);
1009 }
1010  
1011 296 return 0;
1012 }
1013  
1014 6 static apse_bool_t _apse_match_multiple_simple(apse_t *ap) {
1015 /* multiple apse_vec_t:s, has_different_distances */
1016 apse_size_t h, i;
1017  
1018 APSE_DEBUG(printf("(match multiple simple)\n"));
1019 1578 ▶ for ( ; ap->text_position < ap->text_size; ap->text_position++) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; ap->text_position < ap->text_size; ap->text_position++) { 1572 6 1578
1020 1572 apse_vec_t *t =
1021 1572 ap->pattern_mask +
1022 1572 (unsigned)ap->text[ap->text_position] * ap->bitvectors_in_state;
1023 apse_vec_t c, d;
1024  
1025 APSE_DEBUG_MULTIPLE_FIRST(ap, (apse_size_t)0);
1026 11088 ▶ for (c = 1, i = 0; i < ap->bitvectors_in_state; i++, c = d) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 1, i = 0; i < ap->bitvectors_in_state; i++, c = d) { 9516 1572 11088
1027 9516 d = APSE_TEST_HIGH_BIT(ap->state[i]);
1028 9516 APSE_NEXT_EXACT(ap->state, ap->prev_state, t[i], i, c);
1029 APSE_DEBUG_MULTIPLE_REST(ap, i, i);
1030 }
1031 APSE_DEBUG(printf("\n"));
1032  
1033 16963 ▶ for (h = 1; h <= ap->edit_distance; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 1; h <= ap->edit_distance; h++) { 15391 1572 16963
1034 15391 apse_size_t kj = h * ap->bitvectors_in_state,
1035 15391 jj = kj - ap->bitvectors_in_state;
1036  
1037 APSE_DEBUG_MULTIPLE_FIRST(ap, h);
1038 15391 for (c = 1, i = 0;
1039 109564 ▶ i < ap->bitvectors_in_state;
Branch: 1 branch
BranchTrueFalseTotal
i < ap->bitvectors_in_state; 94173 15391 109564
1040 94173 i++, kj++, jj++, c = d) {
1041 94173 d = APSE_TEST_HIGH_BIT(ap->state[kj]);
1042 94173 APSE_NEXT_APPROX(ap->state, ap->prev_state,
1043 t[i], kj, jj, c);
1044 APSE_DEBUG_MULTIPLE_REST(ap, i, kj);
1045 }
1046 APSE_DEBUG(printf("\n"));
1047 }
1048  
1049 1572 ▶ if (ap->exact_positions)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->exact_positions) 0 1572 1572
1050 0 _apse_exact_multiple(ap);
1051  
1052 1572 ▶ if (_apse_match_next_state(ap) == APSE_MATCH_STATE_END)
Branch: 1 branch
BranchTrueFalseTotal
if (_apse_match_next_state(ap) == APSE_MATCH_STATE_END) 0 1572 1572
1053 0 return 1;
1054  
1055 1572 (void)memcpy(ap->prev_state, ap->state,
1056 ap->bytes_in_all_states);
1057 }
1058  
1059 6 return 0;
1060 }
1061  
1062 181 static apse_bool_t _apse_match_single_complex(apse_t *ap) {
1063 /* single apse_vec_t, has_different_distances */
1064 APSE_DEBUG(printf("(match single complex)\n"));
1065 1496 ▶ for ( ; ap->text_position < ap->text_size; ap->text_position++) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; ap->text_position < ap->text_size; ap->text_position++) { 1366 130 1496
1066 1366 unsigned char o = ap->text[ap->text_position];
1067 1366 apse_vec_t t =
1068 1366 ap->pattern_mask[(unsigned int)o * ap->bitvectors_in_state];
1069 apse_size_t h, g;
1070  
1071 1366 APSE_NEXT_EXACT(ap->state, ap->prev_state, t, (apse_size_t)0, 1);
1072 APSE_DEBUG_SINGLE(ap, (apse_size_t)0);
1073  
1074 2741 ▶ for (g = 0, h = 1; h <= ap->edit_distance; g = h, h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (g = 0, h = 1; h <= ap->edit_distance; g = h, h++) { 1375 1366 2741
1075 1375 apse_bool_t has_insertions = h <= ap->edit_insertions;
1076 1375 apse_bool_t has_deletions = h <= ap->edit_deletions;
1077 1375 apse_bool_t has_substitutions = h <= ap->edit_substitutions;
1078  
1079 1375 APSE_NEXT_COMMON(ap->state, ap->prev_state, t, h);
1080 1375 ▶ if (has_insertions)
Branch: 1 branch
BranchTrueFalseTotal
if (has_insertions) 926 449 1375
1081 926 APSE_NEXT_INSERT(ap->state, ap->prev_state, h, g);
1082 1375 ▶ if (has_deletions)
Branch: 1 branch
BranchTrueFalseTotal
if (has_deletions) 923 452 1375
1083 923 APSE_NEXT_DELETE(ap->state, h, g);
1084 1375 ▶ if (has_substitutions)
Branch: 1 branch
BranchTrueFalseTotal
if (has_substitutions) 931 444 1375
1085 931 APSE_NEXT_SUBSTI(ap->state, ap->prev_state, h, g);
1086 1375 ▶ APSE_NEXT_CARRY(ap->state, h,
Branch: 2 branches
BranchTrueFalseTotal
APSE_NEXT_CARRY(ap->state, h, 452 923 1375
APSE_NEXT_CARRY(ap->state, h, 444 8 452
1087 has_deletions || has_substitutions ? 1 : 0);
1088 1375 ▶ APSE_PREFIX_DELETE_MASK(ap);
Branch: 2 branches
BranchTrueFalseTotal
APSE_PREFIX_DELETE_MASK(ap); 452 923 1375
APSE_PREFIX_DELETE_MASK(ap); 60 392 452
1089 APSE_DEBUG_SINGLE(ap, h);
1090 }
1091  
1092 1366 ▶ if (ap->exact_positions)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->exact_positions) 0 1366 1366
1093 0 ap->state[ap->edit_distance] &= ~ap->exact_mask[0];
1094  
1095 1366 ▶ if (_apse_match_next_state(ap) == APSE_MATCH_STATE_END)
Branch: 1 branch
BranchTrueFalseTotal
if (_apse_match_next_state(ap) == APSE_MATCH_STATE_END) 51 1315 1366
1096 51 return 1;
1097  
1098 1315 (void)memcpy(ap->prev_state, ap->state,
1099 ap->bytes_in_all_states);
1100  
1101 }
1102  
1103 130 return 0;
1104 }
1105  
1106 1 static apse_bool_t _apse_match_multiple_complex(apse_t *ap) {
1107 /* multiple apse_vec_t:s, has_different_distances */
1108 apse_size_t h, i;
1109  
1110 APSE_DEBUG(printf("(match multiple complex)\n"));
1111 142 ▶ for ( ; ap->text_position < ap->text_size; ap->text_position++) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; ap->text_position < ap->text_size; ap->text_position++) { 142 0 142
1112 142 unsigned char o = ap->text[ap->text_position];
1113 142 apse_vec_t *t =
1114 142 ap->pattern_mask + (unsigned)o * ap->bitvectors_in_state;
1115 apse_vec_t c, d;
1116  
1117 APSE_DEBUG_MULTIPLE_FIRST(ap, (apse_size_t)0);
1118 568 ▶ for (c = 1, i = 0; i < ap->bitvectors_in_state; i++, c = d) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 1, i = 0; i < ap->bitvectors_in_state; i++, c = d) { 426 142 568
1119 426 d = APSE_TEST_HIGH_BIT(ap->state[i]);
1120 426 APSE_NEXT_EXACT(ap->state, ap->prev_state, t[i], i, c);
1121 APSE_DEBUG_MULTIPLE_REST(ap, i, i);
1122 }
1123 APSE_DEBUG(printf("\n"));
1124  
1125 993 ▶ for (h = 1; h <= ap->edit_distance; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 1; h <= ap->edit_distance; h++) { 852 141 993
1126 apse_size_t
1127 852 kj = h * ap->bitvectors_in_state,
1128 852 jj = kj - ap->bitvectors_in_state;
1129  
1130 852 apse_bool_t has_insertions = h <= ap->edit_insertions;
1131 852 apse_bool_t has_deletions = h <= ap->edit_deletions;
1132 852 apse_bool_t has_substitutions = h <= ap->edit_substitutions;
1133  
1134 APSE_DEBUG_MULTIPLE_FIRST(ap, h);
1135 /* Is there such a thing as too much manual optimization? */
1136 852 ▶ if (has_insertions) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_insertions) { 0 852 852
1137 0 ▶ if (has_deletions && has_substitutions) {
Branch: 2 branches
BranchTrueFalseTotal
if (has_deletions && has_substitutions) { 0 0 0
if (has_deletions && has_substitutions) { 0 0 0
1138 0 for (c = 1, i = 0;
1139 0 ▶ i < ap->bitvectors_in_state;
Branch: 1 branch
BranchTrueFalseTotal
i < ap->bitvectors_in_state; 0 0 0
1140 0 i++, kj++, jj++, c = d) {
1141 0 d = APSE_TEST_HIGH_BIT(ap->state[kj]);
1142 0 APSE_NEXT_COMMON(ap->state, ap->prev_state, t[i], kj);
1143 0 APSE_NEXT_INSERT(ap->state, ap->prev_state, kj, jj);
1144 0 APSE_NEXT_DELETE(ap->state, kj, jj);
1145 0 APSE_NEXT_SUBSTI(ap->state, ap->prev_state, kj, jj);
1146 0 APSE_NEXT_CARRY(ap->state, kj, c);
1147 0 ▶ APSE_PREFIX_DELETE_MASK(ap);
Branch: 2 branches
BranchTrueFalseTotal
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
1148 APSE_DEBUG_MULTIPLE_REST(ap, i, kj);
1149 }
1150 0 ▶ } else if (has_deletions) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (has_deletions) { 0 0 0
1151 0 for (c = 1, i = 0;
1152 0 ▶ i < ap->bitvectors_in_state;
Branch: 1 branch
BranchTrueFalseTotal
i < ap->bitvectors_in_state; 0 0 0
1153 0 i++, kj++, jj++, c = d) {
1154 0 d = APSE_TEST_HIGH_BIT(ap->state[kj]);
1155 0 APSE_NEXT_COMMON(ap->state, ap->prev_state, t[i], kj);
1156 0 APSE_NEXT_INSERT(ap->state, ap->prev_state, kj, jj);
1157 0 APSE_NEXT_DELETE(ap->state, kj, jj);
1158 0 APSE_NEXT_CARRY(ap->state, kj, c);
1159 0 ▶ APSE_PREFIX_DELETE_MASK(ap);
Branch: 2 branches
BranchTrueFalseTotal
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
1160 APSE_DEBUG_MULTIPLE_REST(ap, i, kj);
1161 }
1162 0 ▶ } else if (has_substitutions) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (has_substitutions) { 0 0 0
1163 0 for (c = 1, i = 0;
1164 0 ▶ i < ap->bitvectors_in_state;
Branch: 1 branch
BranchTrueFalseTotal
i < ap->bitvectors_in_state; 0 0 0
1165 0 i++, kj++, jj++, c = d) {
1166 0 d = APSE_TEST_HIGH_BIT(ap->state[kj]);
1167 0 APSE_NEXT_COMMON(ap->state, ap->prev_state, t[i], kj);
1168 0 APSE_NEXT_INSERT(ap->state, ap->prev_state, kj, jj);
1169 0 APSE_NEXT_SUBSTI(ap->state, ap->prev_state, kj, jj);
1170 0 APSE_NEXT_CARRY(ap->state, kj, c);
1171 0 ▶ APSE_PREFIX_DELETE_MASK(ap);
Branch: 2 branches
BranchTrueFalseTotal
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
1172 APSE_DEBUG_MULTIPLE_REST(ap, i, kj);
1173 }
1174 } else {
1175 0 for (c = 1, i = 0;
1176 0 ▶ i < ap->bitvectors_in_state;
Branch: 1 branch
BranchTrueFalseTotal
i < ap->bitvectors_in_state; 0 0 0
1177 0 i++, kj++, jj++, c = d) {
1178 0 d = APSE_TEST_HIGH_BIT(ap->state[kj]);
1179 0 APSE_NEXT_COMMON(ap->state, ap->prev_state, t[i], kj);
1180 0 APSE_NEXT_INSERT(ap->state, ap->prev_state, kj, jj);
1181 0 APSE_NEXT_CARRY(ap->state, kj, c);
1182 0 ▶ APSE_PREFIX_DELETE_MASK(ap);
Branch: 2 branches
BranchTrueFalseTotal
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
1183 APSE_DEBUG_MULTIPLE_REST(ap, i, kj);
1184 }
1185 }
1186 } else {
1187 852 ▶ if (has_deletions && has_substitutions) {
Branch: 2 branches
BranchTrueFalseTotal
if (has_deletions && has_substitutions) { 852 0 852
if (has_deletions && has_substitutions) { 852 0 852
1188 852 for (c = 1, i = 0;
1189 3408 ▶ i < ap->bitvectors_in_state;
Branch: 1 branch
BranchTrueFalseTotal
i < ap->bitvectors_in_state; 2556 852 3408
1190 2556 i++, kj++, jj++, c = d) {
1191 2556 d = APSE_TEST_HIGH_BIT(ap->state[kj]);
1192 2556 APSE_NEXT_COMMON(ap->state, ap->prev_state, t[i], kj);
1193 2556 APSE_NEXT_DELETE(ap->state, kj, jj);
1194 2556 APSE_NEXT_SUBSTI(ap->state, ap->prev_state, kj, jj);
1195 2556 APSE_NEXT_CARRY(ap->state, kj, c);
1196 2556 ▶ APSE_PREFIX_DELETE_MASK(ap);
Branch: 2 branches
BranchTrueFalseTotal
APSE_PREFIX_DELETE_MASK(ap); 0 2556 2556
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
1197 APSE_DEBUG_MULTIPLE_REST(ap, i, kj);
1198 }
1199 0 ▶ } else if (has_deletions) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (has_deletions) { 0 0 0
1200 0 for (c = 1, i = 0;
1201 0 ▶ i < ap->bitvectors_in_state;
Branch: 1 branch
BranchTrueFalseTotal
i < ap->bitvectors_in_state; 0 0 0
1202 0 i++, kj++, jj++, c = d) {
1203 0 d = APSE_TEST_HIGH_BIT(ap->state[kj]);
1204 0 APSE_NEXT_COMMON(ap->state, ap->prev_state, t[i], kj);
1205 0 APSE_NEXT_DELETE(ap->state, kj, jj);
1206 0 APSE_NEXT_CARRY(ap->state, kj, c);
1207 0 ▶ APSE_PREFIX_DELETE_MASK(ap);
Branch: 2 branches
BranchTrueFalseTotal
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
1208 APSE_DEBUG_MULTIPLE_REST(ap, i, kj);
1209 }
1210 0 ▶ } else if (has_substitutions) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (has_substitutions) { 0 0 0
1211 0 for (c = 1, i = 0;
1212 0 ▶ i < ap->bitvectors_in_state;
Branch: 1 branch
BranchTrueFalseTotal
i < ap->bitvectors_in_state; 0 0 0
1213 0 i++, kj++, jj++, c = d) {
1214 0 d = APSE_TEST_HIGH_BIT(ap->state[kj]);
1215 0 APSE_NEXT_COMMON(ap->state, ap->prev_state, t[i], kj);
1216 0 APSE_NEXT_SUBSTI(ap->state, ap->prev_state, kj, jj);
1217 0 APSE_NEXT_CARRY(ap->state, kj, c);
1218 0 ▶ APSE_PREFIX_DELETE_MASK(ap);
Branch: 2 branches
BranchTrueFalseTotal
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
APSE_PREFIX_DELETE_MASK(ap); 0 0 0
1219 APSE_DEBUG_MULTIPLE_REST(ap, i, kj);
1220 }
1221 }
1222 }
1223 APSE_DEBUG(printf("\n"));
1224  
1225  
1226 852 ▶ if (ap->exact_positions)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->exact_positions) 0 852 852
1227 0 _apse_exact_multiple(ap);
1228
1229 852 ▶ if (_apse_match_next_state(ap) == APSE_MATCH_STATE_END)
Branch: 1 branch
BranchTrueFalseTotal
if (_apse_match_next_state(ap) == APSE_MATCH_STATE_END) 1 851 852
1230 1 return 1;
1231  
1232 851 (void)memcpy(ap->prev_state, ap->state,
1233 ap->bytes_in_all_states);
1234 }
1235 }
1236  
1237 0 return 0;
1238 }
1239  
1240 706 static apse_bool_t __apse_match(apse_t *ap,
1241 unsigned char *text,
1242 apse_size_t text_size) {
1243 706 apse_bool_t did_match = 0;
1244  
1245 APSE_DEBUG(printf("(match enter)\n"));
1246  
1247 706 ▶ if (ap->match_state == APSE_MATCH_STATE_BOT) {
Branch: 1 branch
BranchTrueFalseTotal
if (ap->match_state == APSE_MATCH_STATE_BOT) { 676 30 706
1248 676 ap->text = text;
1249 676 ▶ if (ap->text_final_position == APSE_MATCH_BAD)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->text_final_position == APSE_MATCH_BAD) 672 4 676
1250 672 ap->text_size = text_size;
1251 else
1252 4 ap->text_size =
1253 4 ap->text_final_position > text_size ?
1254 4 ▶ text_size : ap->text_final_position + 1;
Branch: 1 branch
BranchTrueFalseTotal
text_size : ap->text_final_position + 1; 4 0 4
1255 676 _apse_match_bot(ap);
1256 30 ▶ } else if (ap->match_state == APSE_MATCH_STATE_EOT)
Branch: 1 branch
BranchTrueFalseTotal
} else if (ap->match_state == APSE_MATCH_STATE_EOT) 17 13 30
1257 17 goto leave;
1258  
1259 689 ▶ if (ap->edit_deletions >= ap->pattern_size ||
Branch: 1 branch
BranchTrueFalseTotal
if (ap->edit_deletions >= ap->pattern_size || 676 13 689
1260 676 ▶ ap->edit_substitutions >= ap->pattern_size) {
Branch: 1 branch
BranchTrueFalseTotal
ap->edit_substitutions >= ap->pattern_size) { 0 676 676
1261 13 ap->match_state = APSE_MATCH_STATE_END;
1262 13 ap->match_begin = ap->text_initial_position;
1263 13 ap->match_end = ap->text_size - 1;
1264 13 ap->text_position = ap->text_size;
1265 13 goto out;
1266 }
1267  
1268 676 if (ap->pattern_size - ap->edit_deletions >
1269 676 ▶ ap->text_size - ap->text_initial_position) {
Branch: 1 branch
BranchTrueFalseTotal
ap->text_size - ap->text_initial_position) { 18 658 676
1270 18 ap->match_state = APSE_MATCH_STATE_EOT;
1271 18 ap->text_position = ap->text_size;
1272 18 goto out;
1273 }
1274  
1275 658 ▶ if (text_size + ap->edit_distance < ap->pattern_size + ap->text_position) {
Branch: 1 branch
BranchTrueFalseTotal
if (text_size + ap->edit_distance < ap->pattern_size + ap->text_position) { 6 652 658
1276 6 ap->text_position = ap->text_size;
1277 6 goto eot;
1278 }
1279  
1280 652 ▶ if (ap->match_state == APSE_MATCH_STATE_SEARCH) {
Branch: 1 branch
BranchTrueFalseTotal
if (ap->match_state == APSE_MATCH_STATE_SEARCH) { 7 645 652
1281 7 ap->text_position++;
1282 7 _apse_reset_state(ap);
1283 }
1284  
1285 652 ▶ if (ap->text_position_range != APSE_MATCH_BAD &&
Branch: 1 branch
BranchTrueFalseTotal
if (ap->text_position_range != APSE_MATCH_BAD && 0 652 652
1286 0 ap->text_position - ap->text_initial_position >
1287 0 ▶ ap->text_position_range) {
Branch: 1 branch
BranchTrueFalseTotal
ap->text_position_range) { 0 0 0
1288 0 ap->match_state = APSE_MATCH_STATE_END;
1289 0 goto eot;
1290 }
1291  
1292 652 ap->match_state = APSE_MATCH_STATE_SEARCH;
1293 APSE_DEBUG(printf("(match state %s)\n", apse_match_state_name(ap)));
1294 APSE_DEBUG(printf("(match search %ld)\n", ap->text_position));
1295  
1296 652 ▶ if (ap->has_different_distances) {
Branch: 1 branch
BranchTrueFalseTotal
if (ap->has_different_distances) { 182 470 652
1297 182 ▶ if (ap->bitvectors_in_state == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ap->bitvectors_in_state == 1) { 181 1 182
1298 181 ▶ if (_apse_match_single_complex(ap))
Branch: 1 branch
BranchTrueFalseTotal
if (_apse_match_single_complex(ap)) 51 130 181
1299 51 goto out;
1300 } else {
1301 1 ▶ if (_apse_match_multiple_complex(ap))
Branch: 1 branch
BranchTrueFalseTotal
if (_apse_match_multiple_complex(ap)) 1 0 1
1302 1 goto out;
1303 }
1304 } else {
1305 470 ▶ if (ap->bitvectors_in_state == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ap->bitvectors_in_state == 1) { 464 6 470
1306 464 ▶ if (_apse_match_single_simple(ap))
Branch: 1 branch
BranchTrueFalseTotal
if (_apse_match_single_simple(ap)) 168 296 464
1307 168 goto out;
1308 } else {
1309 6 ▶ if (_apse_match_multiple_simple(ap))
Branch: 1 branch
BranchTrueFalseTotal
if (_apse_match_multiple_simple(ap)) 6 0 6
1310 0 goto out;
1311 }
1312 }
1313  
1314 6 out:
1315  
1316 683 ▶ if (ap->match_state == APSE_MATCH_STATE_GREEDY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ap->match_state == APSE_MATCH_STATE_GREEDY) { 91 592 683
1317 91 ap->match_state = APSE_MATCH_STATE_END;
1318 91 ap->match_end = ap->text_position - 1;
1319 APSE_DEBUG(printf("(greedy match end %ld)\n", ap->match_end));
1320 }
1321  
1322 683 ▶ if (ap->match_state == APSE_MATCH_STATE_END) {
Branch: 1 branch
BranchTrueFalseTotal
if (ap->match_state == APSE_MATCH_STATE_END) { 359 324 683
1323 324 _apse_match_end(ap);
1324 324 did_match = 1;
1325 }
1326  
1327 359 eot:
1328  
1329 689 ▶ if (ap->text_position == ap->text_size)
Branch: 1 branch
BranchTrueFalseTotal
if (ap->text_position == ap->text_size) 220 469 689
1330 469 _apse_match_eot(ap);
1331  
1332 220 leave:
1333  
1334 APSE_DEBUG(printf("(match leave)\n"));
1335  
1336 706 return did_match;
1337 }
1338  
1339 594 static apse_bool_t _apse_match(apse_t *ap,
1340 unsigned char *text,
1341 apse_size_t text_size) {
1342 594 ▶ if (ap->use_minimal_distance) {
Branch: 1 branch
BranchTrueFalseTotal
if (ap->use_minimal_distance) { 42 552 594
1343 42 apse_set_edit_distance(ap, 0);
1344 42 ▶ if (__apse_match(ap, text, text_size))
Branch: 1 branch
BranchTrueFalseTotal
if (__apse_match(ap, text, text_size)) 7 35 42
1345 7 return 1;
1346 else {
1347 apse_size_t minimal_edit_distance;
1348 35 apse_size_t previous_edit_distance = 0;
1349 apse_size_t next_edit_distance;
1350
1351 35 for (next_edit_distance = 1;
1352 59 ▶ next_edit_distance <= ap->pattern_size;
Branch: 1 branch
BranchTrueFalseTotal
next_edit_distance <= ap->pattern_size; 53 6 59
1353 24 next_edit_distance *= 2) {
1354 53 apse_set_edit_distance(ap, next_edit_distance);
1355 53 ▶ if (__apse_match(ap, text, text_size))
Branch: 1 branch
BranchTrueFalseTotal
if (__apse_match(ap, text, text_size)) 29 24 53
1356 29 break;
1357 24 previous_edit_distance = next_edit_distance;
1358 }
1359 35 minimal_edit_distance = next_edit_distance;
1360 35 ▶ if (next_edit_distance > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (next_edit_distance > 1) { 18 17 35
1361 do {
1362 24 minimal_edit_distance =
1363 24 (previous_edit_distance + next_edit_distance) / 2;
1364 24 ▶ if (minimal_edit_distance == previous_edit_distance)
Branch: 1 branch
BranchTrueFalseTotal
if (minimal_edit_distance == previous_edit_distance) 18 6 24
1365 18 break;
1366 6 apse_set_edit_distance(ap, minimal_edit_distance);
1367 6 ▶ if (__apse_match(ap, text, text_size))
Branch: 1 branch
BranchTrueFalseTotal
if (__apse_match(ap, text, text_size)) 6 0 6
1368 6 next_edit_distance = minimal_edit_distance;
1369 else
1370 0 previous_edit_distance = minimal_edit_distance;
1371 6 ▶ } while (previous_edit_distance <= next_edit_distance);
Branch: 1 branch
BranchTrueFalseTotal
} while (previous_edit_distance <= next_edit_distance); 6 0 6
1372 18 ▶ if (!__apse_match(ap, text, text_size))
Branch: 1 branch
BranchTrueFalseTotal
if (!__apse_match(ap, text, text_size)) 18 0 18
1373 18 minimal_edit_distance++;
1374 }
1375 35 apse_set_edit_distance(ap, minimal_edit_distance);
1376 35 __apse_match(ap, text, text_size);
1377
1378 35 return 1;
1379 }
1380 } else
1381 552 return __apse_match(ap, text, text_size);
1382 }
1383  
1384 251 apse_bool_t apse_match(apse_t *ap,
1385 unsigned char *text, apse_size_t text_size) {
1386 251 apse_bool_t did_match = _apse_match(ap, text, text_size);
1387  
1388 251 _apse_match_eot(ap);
1389 251 apse_reset(ap);
1390  
1391 251 return did_match;
1392 }
1393  
1394 0 apse_bool_t apse_match_next(apse_t *ap,
1395 unsigned char *text, apse_size_t text_size) {
1396 0 apse_bool_t did_match = _apse_match(ap, text, text_size);
1397  
1398 0 ▶ if (!did_match)
Branch: 1 branch
BranchTrueFalseTotal
if (!did_match) 0 0 0
1399 0 ap->match_state = APSE_MATCH_STATE_BOT;
1400  
1401 0 return did_match;
1402 }
1403  
1404 24 apse_ssize_t apse_index(apse_t *ap,
1405 unsigned char *text, apse_size_t text_size) {
1406 24 apse_size_t did_match = _apse_match(ap, text, text_size);
1407 24 _apse_match_eot(ap);
1408 24 ap->match_state = APSE_MATCH_STATE_BOT;
1409  
1410 24 ▶ return did_match ? ap->match_begin : APSE_MATCH_BAD;
Branch: 1 branch
BranchTrueFalseTotal
return did_match ? ap->match_begin : APSE_MATCH_BAD; 18 6 24
1411 }
1412  
1413 0 apse_ssize_t apse_index_next(apse_t *ap,
1414 unsigned char *text, apse_size_t text_size) {
1415 0 apse_bool_t did_match = _apse_match(ap, text, text_size);
1416  
1417 0 ▶ if (!did_match)
Branch: 1 branch
BranchTrueFalseTotal
if (!did_match) 0 0 0
1418 0 ap->match_state = APSE_MATCH_STATE_BOT;
1419  
1420 0 ▶ return did_match ? ap->match_begin : APSE_MATCH_BAD;
Branch: 1 branch
BranchTrueFalseTotal
return did_match ? ap->match_begin : APSE_MATCH_BAD; 0 0 0
1421 }
1422  
1423 319 static apse_bool_t _apse_slice(apse_t *ap,
1424 unsigned char *text,
1425 apse_size_t text_size,
1426 apse_size_t *match_begin,
1427 apse_size_t *match_size) {
1428 319 apse_bool_t did_match = _apse_match(ap, text, text_size);
1429  
1430 319 ▶ if (did_match) {
Branch: 1 branch
BranchTrueFalseTotal
if (did_match) { 161 158 319
1431 161 ▶ if (match_begin)
Branch: 1 branch
BranchTrueFalseTotal
if (match_begin) 161 0 161
1432 161 *match_begin = ap->match_begin;
1433 161 ▶ if (match_size)
Branch: 1 branch
BranchTrueFalseTotal
if (match_size) 161 0 161
1434 161 *match_size = ap->match_end - ap->match_begin + 1;
1435 } else {
1436 158 ▶ if (match_begin)
Branch: 1 branch
BranchTrueFalseTotal
if (match_begin) 158 0 158
1437 158 *match_begin = APSE_MATCH_BAD;
1438 158 ▶ if (match_size)
Branch: 1 branch
BranchTrueFalseTotal
if (match_size) 158 0 158
1439 158 *match_size = APSE_MATCH_BAD;
1440 }
1441  
1442 319 return did_match;
1443 }
1444  
1445 278 apse_bool_t apse_slice(apse_t *ap,
1446 unsigned char *text,
1447 apse_size_t text_size,
1448 apse_size_t *match_begin,
1449 apse_size_t *match_size) {
1450 apse_bool_t did_match =
1451 278 _apse_slice(ap, text, text_size, match_begin, match_size);
1452  
1453 278 _apse_match_eot(ap);
1454 278 ap->match_state = APSE_MATCH_STATE_BOT;
1455  
1456 278 return did_match;
1457 }
1458  
1459 41 apse_bool_t apse_slice_next(apse_t* ap,
1460 unsigned char* text,
1461 apse_size_t text_size,
1462 apse_size_t* match_begin,
1463 apse_size_t* match_size) {
1464 apse_bool_t did_match =
1465 41 _apse_slice(ap, text, text_size, match_begin, match_size);
1466  
1467 41 ▶ if (!did_match)
Branch: 1 branch
BranchTrueFalseTotal
if (!did_match) 29 12 41
1468 29 ap->match_state = APSE_MATCH_STATE_BOT;
1469  
1470 41 return did_match;
1471 }
1472  
1473 0 void apse_set_custom_data(apse_t* ap,
1474 void* custom_data,
1475 apse_size_t custom_data_size) {
1476 0 ap->custom_data = custom_data;
1477 0 ap->custom_data_size = custom_data_size;
1478 0 }
1479  
1480 0 void* apse_get_custom_data(apse_t* ap,
1481 apse_size_t* custom_data_size) {
1482 0 ▶ if (custom_data_size)
Branch: 1 branch
BranchTrueFalseTotal
if (custom_data_size) 0 0 0
1483 0 *custom_data_size = ap->custom_data_size;
1484 0 return ap->custom_data;
1485 }
« blib/lib/String/Approx.pm ↑ Summary Approx.xs »