WuManber.xs

filter: Statementstmt 100% 39 / 39 Branchbran 55.0% 11 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.7% 50 / 59 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« wumanber_impl.c ↑ Summary
1 /*
2 * WuManber.xs
3 * Copyright (c) 2007-2010, Juergen Weigert, Novell Inc.
4 * This module is free software. It may be used, redistributed
5 * and/or modified under the same terms as Perl itself.
6 *
7 * see perldoc perlxstut
8 * see Rolf Stiebe; Textalgoritmen WS 2005/6
9 * see TR94-17_WuManber.pdf
10 */
11  
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
15  
16 #include "wumanber_impl.h"
17  
18  
19 16 static void push_result(unsigned int idx, unsigned long offset, void *data)
20 {
21 16 AV *r = (AV *)data;
22  
23 // In perl, indices run from 0..n_pat-1
24 // In C, indices run from 1..n_pat
25  
26 #if 1
27 16 AV *loc = (AV *)sv_2mortal((SV *)newAV());
28 16 av_push(loc, newSVnv(offset));
29 16 av_push(loc, newSVnv(idx-1));
30 16 av_push(r, newRV((SV *)loc));
31 #else
32 av_push(r, newSVnv(offset));
33 av_push(r, newSVnv(idx-1));
34 #endif
35 16 }
36  
37 MODULE = Search::WuManber PACKAGE = Search::WuManber
38 PROTOTYPES: ENABLE
39  
40 #define BLOCK_SIZE 3
41 #define HASH1_SIZE 0x10
42 int
43 init_tables(obj)
44 HV *obj
45  
46 PREINIT:
47 AV* p;
48 SV** pp;
49 SV **svp;
50 int i, n_patterns;
51 unsigned char **pattern_list;
52 unsigned int case_sensitive;
53  
54 INIT:
55 // init PAT table
56 1 pp = hv_fetch(obj, "patterns", 8, 0);
57 1 ▶ if (!pp) croak("init_tables: no patterns in obj\n");
Branch: 1 branch
BranchTrueFalseTotal
if (!pp) croak("init_tables: no patterns in obj\n"); 0 1 1
58  
59 // next test needed to avoid segv
60 1 ▶ if (SvTYPE(SvRV(*pp)) != SVt_PVAV) croak("init_tables: patterns not an ARRAY-ref\n");
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(*pp)) != SVt_PVAV) croak("init_tables: patterns not an ARRAY-ref\n"); 0 1 1
61 1 p = (AV *)SvRV(*pp);
62 1 n_patterns = av_len(p);
63 1 pattern_list = (unsigned char **)calloc(sizeof(unsigned char *), n_patterns+2);
64  
65 1 svp = hv_fetch(obj, "case_sensitive", 14, 0);
66 1 ▶ if (!svp) croak("init_tables: no 'case_sensitive' in obj\n");
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) croak("init_tables: no 'case_sensitive' in obj\n"); 0 1 1
67 1 case_sensitive = SvUV(*svp);
68  
69 CODE:
70 1 i = 0;
71 14 ▶ while (i++ <= n_patterns)
Branch: 1 branch
BranchTrueFalseTotal
while (i++ <= n_patterns) 13 1 14
72 {
73 13 SV** ep = av_fetch(p, i-1, 0);
74 STRLEN slen;
75 unsigned char *e;
76  
77 // next test not really needed. perl converts almost anything to string.
78 13 ▶ if (!SvPOK(*ep)) croak("init_tables: pattern[%d] is not a string\n", i);
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOK(*ep)) croak("init_tables: pattern[%d] is not a string\n", i); 0 13 13
79 26 pattern_list[i] = e = (unsigned char *)SvPV(*ep, slen);
80  
81 // printf("pattern[%d] = '%s'\n", i, e);
82 }
83 1 pattern_list[i] = NULL; // just to be sure
84  
85 1 struct WuManber *wm = (struct WuManber *)calloc(1, sizeof(struct WuManber));
86 1 wm->progname = "perl(Search::WuManber)";
87 1 prep_pat(wm, n_patterns+1, pattern_list, !case_sensitive);
88  
89 // FIXME: this needs a destructor, to free the memory bound in wm's pointers.
90 1 (void)hv_store(obj, "wm", 2, newSVpvn((char *)wm, sizeof(*wm)), 0);
91  
92 1 ▶ (void)hv_store(obj, "BLOCK_SIZE", 9, newSViv(wm->use_bs1?1:(wm->use_bs3?3:2)), 0);
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store(obj, "BLOCK_SIZE", 9, newSViv(wm->use_bs1?1:(wm->use_bs3?3:2)), 0); 1 0 1
(void)hv_store(obj, "BLOCK_SIZE", 9, newSViv(wm->use_bs1?1:(wm->use_bs3?3:2)), 0); 0 1 1
93 1 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 1 1
94 OUTPUT:
95 RETVAL
96  
97  
98 SV *
99 find_all(obj,textsv)
100 HV *obj
101 SV *textsv
102  
103 PREINIT:
104 AV *r; // return value
105 STRLEN text_len, n;
106 unsigned char *text;
107 struct WuManber *wm;
108 SV **svp;
109  
110 1 text = (unsigned char *)SvPV(textsv, text_len);
111 // warn("find_all: text='%s', text_len=%d\n", text, (unsigned int)text_len);
112  
113 INIT:
114 1 svp = hv_fetch(obj, "wm", 2, 0);
115 1 ▶ if (!svp) croak("find_all: no 'wm' in obj\n");
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) croak("find_all: no 'wm' in obj\n"); 0 1 1
116 1 wm = (struct WuManber *)SvPV(*svp, n);
117 1 ▶ if (!svp) croak("find_all: sizeof(wm)=%d, expected %d\n", (int)n, (int)sizeof(struct WuManber));
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) croak("find_all: sizeof(wm)=%d, expected %d\n", (int)n, (int)sizeof(struct WuManber)); 0 1 1
118 1 search_init(wm, "argv[0]");
119  
120 1 r = (AV *)sv_2mortal((SV *)newAV());
121 1 wm->cb = push_result;
122 1 wm->cb_data = (void *)r;
123  
124 CODE:
125 1 search_text(wm, text, text_len);
126 1 RETVAL = newRV((SV *)r);
127 OUTPUT:
128 RETVAL
129  
« wumanber_impl.c ↑ Summary