line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#ifdef __cplusplus |
2
|
|
|
|
|
|
|
extern "C" { |
3
|
|
|
|
|
|
|
#endif |
4
|
|
|
|
|
|
|
#include "EXTERN.h" |
5
|
|
|
|
|
|
|
#include "perl.h" |
6
|
|
|
|
|
|
|
#include "XSUB.h" |
7
|
|
|
|
|
|
|
#ifdef __cplusplus |
8
|
|
|
|
|
|
|
} |
9
|
|
|
|
|
|
|
#endif |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
#include "darts.h" |
12
|
|
|
|
|
|
|
#define MAX_NMATCH 1024 |
13
|
|
|
|
|
|
|
|
14
|
0
|
|
|
|
|
|
static IV da_make(AV *av){ |
15
|
0
|
0
|
|
|
|
|
Darts::DoubleArray *dp = new Darts::DoubleArray; |
16
|
0
|
0
|
|
|
|
|
std::vector keys; |
|
|
0
|
|
|
|
|
|
17
|
|
|
|
|
|
|
int i, l; |
18
|
0
|
0
|
|
|
|
|
for (i = 0, l = av_len(av)+1; i < l; i++){ |
|
|
0
|
|
|
|
|
|
19
|
0
|
0
|
|
|
|
|
keys.push_back(SvPV_nolen(AvARRAY(av)[i])); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
20
|
|
|
|
|
|
|
} |
21
|
0
|
0
|
|
|
|
|
dp->build(keys.size(), &keys[0], 0, 0); |
22
|
0
|
|
|
|
|
|
return PTR2IV(dp); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
static int da_free(IV dpi){ |
26
|
0
|
0
|
|
|
|
|
delete INT2PTR(Darts::DoubleArray *, dpi); |
27
|
0
|
|
|
|
|
|
return 0; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
static IV da_open(char *filename){ |
31
|
0
|
|
|
|
|
|
Darts::DoubleArray *dp = new Darts::DoubleArray; |
32
|
0
|
0
|
|
|
|
|
if (dp->open(filename) == -1){ |
33
|
0
|
0
|
|
|
|
|
delete dp; |
34
|
0
|
|
|
|
|
|
return 0; |
35
|
|
|
|
|
|
|
} |
36
|
0
|
|
|
|
|
|
return PTR2IV(dp); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
static size_t da_search(IV dpi, char *str){ |
40
|
0
|
|
|
|
|
|
Darts::DoubleArray *dp = INT2PTR(Darts::DoubleArray *, dpi); |
41
|
|
|
|
|
|
|
Darts::DoubleArray::result_pair_type result_pair [MAX_NMATCH]; |
42
|
0
|
0
|
|
|
|
|
size_t num = dp->commonPrefixSearch(str, result_pair, sizeof(result_pair)); |
43
|
0
|
|
|
|
|
|
return num; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
static SV *do_callback(SV *callback, SV *s){ |
48
|
0
|
|
|
|
|
|
dSP; |
49
|
|
|
|
|
|
|
int argc; |
50
|
|
|
|
|
|
|
SV *retval; |
51
|
0
|
|
|
|
|
|
ENTER; |
52
|
0
|
|
|
|
|
|
SAVETMPS; |
53
|
0
|
0
|
|
|
|
|
PUSHMARK(sp); |
54
|
0
|
0
|
|
|
|
|
XPUSHs(s); |
55
|
0
|
|
|
|
|
|
PUTBACK; |
56
|
0
|
|
|
|
|
|
argc = call_sv(callback, G_SCALAR); |
57
|
0
|
|
|
|
|
|
SPAGAIN; |
58
|
0
|
0
|
|
|
|
|
if (argc != 1){ |
59
|
0
|
|
|
|
|
|
croak("fallback sub must return scalar!"); |
60
|
|
|
|
|
|
|
} |
61
|
0
|
|
|
|
|
|
retval = newSVsv(POPs); |
62
|
0
|
|
|
|
|
|
PUTBACK; |
63
|
0
|
0
|
|
|
|
|
FREETMPS; |
64
|
0
|
|
|
|
|
|
LEAVE; |
65
|
0
|
|
|
|
|
|
return retval; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
static SV *do_hvlookup(SV *hashref, char *str, size_t len){ |
69
|
0
|
|
|
|
|
|
SV **val = hv_fetch((HV *)SvRV(hashref), str, len, 0 ); |
70
|
0
|
0
|
|
|
|
|
return val && *val ? *val : &PL_sv_undef; |
|
|
0
|
|
|
|
|
|
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
static SV *da_gsub(IV dpi, SV *src, SV *rep){ |
75
|
0
|
0
|
|
|
|
|
SV *result = newSV(SvCUR(src)); |
76
|
0
|
|
|
|
|
|
Darts::DoubleArray *dp = INT2PTR(Darts::DoubleArray *, dpi); |
77
|
|
|
|
|
|
|
Darts::DoubleArray::result_pair_type result_pair[MAX_NMATCH]; |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
char *head = SvPV_nolen(src); |
|
|
0
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
char *tail = head + SvCUR(src); |
81
|
|
|
|
|
|
|
|
82
|
0
|
0
|
|
|
|
|
while (head < tail) { |
83
|
0
|
|
|
|
|
|
char *ohead = head; |
84
|
0
|
|
|
|
|
|
size_t size, rlen = 0; |
85
|
0
|
0
|
|
|
|
|
while(head < tail){ |
86
|
|
|
|
|
|
|
size = |
87
|
0
|
0
|
|
|
|
|
dp->commonPrefixSearch(head,result_pair, sizeof(result_pair)); |
88
|
0
|
0
|
|
|
|
|
if (size) break; |
89
|
0
|
|
|
|
|
|
head++; |
90
|
|
|
|
|
|
|
} |
91
|
0
|
0
|
|
|
|
|
if (head != ohead){ |
92
|
0
|
|
|
|
|
|
size_t d = (head - ohead); |
93
|
0
|
|
|
|
|
|
rlen += d; |
94
|
0
|
0
|
|
|
|
|
if (rlen > SvCUR(result)) SvGROW(result, SvCUR(result)*2); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
sv_catpvn(result, ohead, d); |
96
|
|
|
|
|
|
|
} |
97
|
0
|
0
|
|
|
|
|
if (size) { |
98
|
0
|
|
|
|
|
|
size_t seekto = 0; |
99
|
0
|
0
|
|
|
|
|
for (size_t i = 0; i < size; ++i) { |
100
|
0
|
0
|
|
|
|
|
if (seekto < result_pair[i].length) |
101
|
0
|
|
|
|
|
|
seekto = result_pair[i].length; |
102
|
|
|
|
|
|
|
} |
103
|
0
|
0
|
|
|
|
|
if (seekto) { |
104
|
0
|
|
|
|
|
|
SV *ret = SvTYPE(SvRV(rep)) == SVt_PVCV |
105
|
0
|
0
|
|
|
|
|
? do_callback(rep, newSVpvn(head, seekto)) |
106
|
0
|
0
|
|
|
|
|
: do_hvlookup(rep, head, seekto); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
rlen += SvCUR(ret); |
108
|
0
|
0
|
|
|
|
|
if (rlen > SvCUR(result)) SvGROW(result, SvCUR(result)*2); |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
109
|
0
|
0
|
|
|
|
|
sv_catpvn(result, SvPVX(ret), SvCUR(ret)); |
110
|
0
|
|
|
|
|
|
head += seekto; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
0
|
|
|
|
|
|
return result; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
MODULE = Text::Darts PACKAGE = Text::Darts |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
IV |
120
|
|
|
|
|
|
|
xs_make(av) |
121
|
|
|
|
|
|
|
AV *av |
122
|
|
|
|
|
|
|
CODE: |
123
|
0
|
|
|
|
|
|
RETVAL = da_make(av); |
124
|
|
|
|
|
|
|
OUTPUT: |
125
|
|
|
|
|
|
|
RETVAL |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
int |
128
|
|
|
|
|
|
|
xs_free(dpi) |
129
|
|
|
|
|
|
|
IV dpi; |
130
|
|
|
|
|
|
|
CODE: |
131
|
0
|
|
|
|
|
|
RETVAL = da_free(dpi); |
132
|
|
|
|
|
|
|
OUTPUT: |
133
|
|
|
|
|
|
|
RETVAL |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
IV |
136
|
|
|
|
|
|
|
xs_open(filename) |
137
|
|
|
|
|
|
|
char *filename |
138
|
|
|
|
|
|
|
CODE: |
139
|
0
|
|
|
|
|
|
RETVAL = da_open(filename); |
140
|
|
|
|
|
|
|
OUTPUT: |
141
|
|
|
|
|
|
|
RETVAL |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
SV * |
144
|
|
|
|
|
|
|
xs_gsub(dpi, src, rep) |
145
|
|
|
|
|
|
|
IV dpi; |
146
|
|
|
|
|
|
|
SV *src; |
147
|
|
|
|
|
|
|
SV *rep; |
148
|
|
|
|
|
|
|
CODE: |
149
|
0
|
|
|
|
|
|
RETVAL = da_gsub(dpi, src, rep); |
150
|
|
|
|
|
|
|
OUTPUT: |
151
|
|
|
|
|
|
|
RETVAL |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
size_t |
154
|
|
|
|
|
|
|
xs_search(dpi, str) |
155
|
|
|
|
|
|
|
IV dpi; |
156
|
|
|
|
|
|
|
char *str; |
157
|
|
|
|
|
|
|
CODE: |
158
|
0
|
|
|
|
|
|
RETVAL = da_search(dpi, str); |
159
|
|
|
|
|
|
|
OUTPUT: |
160
|
|
|
|
|
|
|
RETVAL |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
char * |
163
|
|
|
|
|
|
|
DARTS_VERSION() |
164
|
|
|
|
|
|
|
CODE: |
165
|
0
|
|
|
|
|
|
RETVAL = DARTS_VERSION; |
166
|
|
|
|
|
|
|
OUTPUT: |
167
|
|
|
|
|
|
|
RETVAL |