re2_xs.cc

filter: Statementstmt 0.0% 0 / 141 Branchbran 0.0% 0 / 156 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 297 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« re2/re2/stringpiece.h ↑ Summary blib/lib/re/engine/RE2.pm »
1 #define PERL_NO_GET_CONTEXT
2  
3 // This needs to be first, Perl is rude and defines macros like "Copy"
4 #include <re2/re2.h>
5 #include <map>
6 #include "re2_xs.h"
7  
8 using std::map;
9 using std::string;
10  
11 namespace {
12 REGEXP * RE2_comp(pTHX_ SV * const, U32);
13 char * RE2_intuit(pTHX_ REGEXP * const, SV *, const char *,
14 char *, char *, U32, re_scream_pos_data *);
15 I32 RE2_exec(pTHX_ REGEXP * const, char *, char *,
16 char *, SSize_t, SV *, void *, U32);
17 SV * RE2_checkstr(pTHX_ REGEXP * const);
18 void RE2_free(pTHX_ REGEXP * const);
19 SV * RE2_package(pTHX_ REGEXP * const);
20 #ifdef USE_ITHREADS
21 void * RE2_dupe(pTHX_ REGEXP * const, CLONE_PARAMS *);
22 #endif
23  
24 0 static SV * stringify(pTHX_ const U32 flags, const char *const exp,
25 const STRLEN plen)
26 {
27 0 SV * wrapped = newSVpvn("(?", 2), * wrapped_unset = newSVpvn("", 0);
28 0 sv_2mortal(wrapped);
29 0 sv_2mortal(wrapped_unset);
30  
31 0 ▶ sv_catpvn(flags & RXf_PMf_FOLD ? wrapped : wrapped_unset, "i", 1);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(flags & RXf_PMf_FOLD ? wrapped : wrapped_unset, "i", 1); 0 0 0
32 0 ▶ sv_catpvn(flags & RXf_PMf_MULTILINE ? wrapped : wrapped_unset, "m", 1);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(flags & RXf_PMf_MULTILINE ? wrapped : wrapped_unset, "m", 1); 0 0 0
33 0 ▶ sv_catpvn(flags & RXf_PMf_SINGLELINE ? wrapped : wrapped_unset, "s", 1);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(flags & RXf_PMf_SINGLELINE ? wrapped : wrapped_unset, "s", 1); 0 0 0
34  
35 0 ▶ if (SvCUR(wrapped_unset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(wrapped_unset)) { 0 0 0
36 0 sv_catpvn(wrapped, "-", 1);
37 0 sv_catsv(wrapped, wrapped_unset);
38 }
39  
40 0 sv_catpvn(wrapped, ":", 1);
41 0 sv_catpvn(wrapped, exp, plen);
42 0 sv_catpvn(wrapped, ")", 1);
43  
44 0 return wrapped;
45 }
46 };
47  
48 const regexp_engine re2_engine = {
49 RE2_comp,
50 RE2_exec,
51 RE2_intuit,
52 RE2_checkstr,
53 RE2_free,
54 Perl_reg_numbered_buff_fetch,
55 Perl_reg_numbered_buff_store,
56 Perl_reg_numbered_buff_length,
57 Perl_reg_named_buff,
58 Perl_reg_named_buff_iter,
59 RE2_package,
60 #if defined(USE_ITHREADS)
61 RE2_dupe,
62 #endif
63 NULL,
64 };
65  
66 namespace {
67  
68 REGEXP *
69 0 RE2_comp(pTHX_ SV * const pattern, const U32 flags)
70 {
71 const char *exp;
72 STRLEN plen;
73 0 RE2::Options options;
74  
75 0 U32 extflags = flags;
76 0 bool perl_only = false;
77  
78 0 exp = SvPV(pattern, plen);
79  
80 /* C<split " ">, bypass the engine alltogether and act as perl does */
81 0 ▶ if (flags & RXf_SPLIT && plen == 1 && exp[0] == ' ')
Branch: 3 branches
BranchTrueFalseTotal
if (flags & RXf_SPLIT && plen == 1 && exp[0] == ' ') 0 0 0
if (flags & RXf_SPLIT && plen == 1 && exp[0] == ' ') 0 0 0
if (flags & RXf_SPLIT && plen == 1 && exp[0] == ' ') 0 0 0
82 0 extflags |= (RXf_SKIPWHITE|RXf_WHITE);
83  
84 /* RXf_NULL - Have C<split //> split by characters */
85 0 ▶ if (plen == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (plen == 0) 0 0 0
86 0 extflags |= RXf_NULL;
87  
88 /* RXf_START_ONLY - Have C<split /^/> split on newlines */
89 0 ▶ else if (plen == 1 && exp[0] == '^')
Branch: 2 branches
BranchTrueFalseTotal
else if (plen == 1 && exp[0] == '^') 0 0 0
else if (plen == 1 && exp[0] == '^') 0 0 0
90 0 extflags |= RXf_START_ONLY;
91  
92 /* RXf_WHITE - Have C<split /\s+/> split on whitespace */
93 0 ▶ else if (plen == 3 && strnEQ("\\s+", exp, 3))
Branch: 2 branches
BranchTrueFalseTotal
else if (plen == 3 && strnEQ("\\s+", exp, 3)) 0 0 0
else if (plen == 3 && strnEQ("\\s+", exp, 3)) 0 0 0
94 0 extflags |= RXf_WHITE;
95  
96 0 ▶ if (flags & RXf_PMf_EXTENDED)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & RXf_PMf_EXTENDED) 0 0 0
97 0 perl_only = true; /* /x */
98  
99 0 options.set_case_sensitive(!(flags & RXf_PMf_FOLD)); /* /i */
100  
101 // XXX: Need to compile two versions?
102 /* The pattern is not UTF-8. Tell RE2 to treat it as Latin1. */
103 #ifdef RXf_UTF8
104 if (!(flags & RXf_UTF8))
105 #else
106 0 ▶ if (!SvUTF8(pattern))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(pattern)) 0 0 0
107 #endif
108 0 options.set_encoding(RE2::Options::EncodingLatin1);
109  
110 0 options.set_log_errors(false);
111  
112 0 ▶ SV *const max_mem = cophh_fetch_pvs(PL_curcop->cop_hints_hash,
Branch: 1 branch
BranchTrueFalseTotal
SV *const max_mem = cophh_fetch_pvs(PL_curcop->cop_hints_hash, 0 0 0
113 "re::engine::RE2::max-mem", 0);
114 0 ▶ if (SvOK(max_mem) && SvIV_nomg(max_mem)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvOK(max_mem) && SvIV_nomg(max_mem)) { 0 0 0
if (SvOK(max_mem) && SvIV_nomg(max_mem)) { 0 0 0
if (SvOK(max_mem) && SvIV_nomg(max_mem)) { 0 0 0
if (SvOK(max_mem) && SvIV_nomg(max_mem)) { 0 0 0
115 0 ▶ options.set_max_mem(SvIV(max_mem));
Branch: 1 branch
BranchTrueFalseTotal
options.set_max_mem(SvIV(max_mem)); 0 0 0
116 }
117  
118 0 ▶ SV *const longest_match = cophh_fetch_pvs(PL_curcop->cop_hints_hash,
Branch: 1 branch
BranchTrueFalseTotal
SV *const longest_match = cophh_fetch_pvs(PL_curcop->cop_hints_hash, 0 0 0
119 "re::engine::RE2::longest-match", 0);
120 0 ▶ if (SvOK(longest_match) && SvTRUE(longest_match)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvOK(longest_match) && SvTRUE(longest_match)) { 0 0 0
if (SvOK(longest_match) && SvTRUE(longest_match)) { 0 0 0
if (SvOK(longest_match) && SvTRUE(longest_match)) { 0 0 0
if (SvOK(longest_match) && SvTRUE(longest_match)) { 0 0 0
121 0 options.set_longest_match(true);
122 }
123  
124 0 ▶ SV *const never_nl = cophh_fetch_pvs(PL_curcop->cop_hints_hash,
Branch: 1 branch
BranchTrueFalseTotal
SV *const never_nl = cophh_fetch_pvs(PL_curcop->cop_hints_hash, 0 0 0
125 "re::engine::RE2::never-nl", 0);
126 0 ▶ if (SvOK(never_nl) && SvTRUE(never_nl)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvOK(never_nl) && SvTRUE(never_nl)) { 0 0 0
if (SvOK(never_nl) && SvTRUE(never_nl)) { 0 0 0
if (SvOK(never_nl) && SvTRUE(never_nl)) { 0 0 0
if (SvOK(never_nl) && SvTRUE(never_nl)) { 0 0 0
127 0 options.set_never_nl(true);
128 }
129  
130 // Try and compile first, if this fails we will fallback to Perl regex via
131 // Perl_re_compile.
132 0 ▶ const SV *const wrapped = stringify(aTHX_ flags, exp, plen);
Branch: 1 branch
BranchTrueFalseTotal
const SV *const wrapped = stringify(aTHX_ flags, exp, plen); 0 0 0
133 0 RE2 * ri = NULL;
134  
135 0 ▶ if (!perl_only) {
Branch: 1 branch
BranchTrueFalseTotal
if (!perl_only) { 0 0 0
136 0 ▶ ri = new RE2 (re2::StringPiece(SvPVX(wrapped), SvCUR(wrapped)), options);
Branch: 3 branches
BranchTrueFalseTotal
ri = new RE2 (re2::StringPiece(SvPVX(wrapped), SvCUR(wrapped)), options); 0 0 0
ri = new RE2 (re2::StringPiece(SvPVX(wrapped), SvCUR(wrapped)), options); 0 0 0
ri = new RE2 (re2::StringPiece(SvPVX(wrapped), SvCUR(wrapped)), options); 0 0 0
137 }
138  
139 0 ▶ if (perl_only || ri->error_code()) {
Branch: 3 branches
BranchTrueFalseTotal
if (perl_only || ri->error_code()) { 0 0 0
if (perl_only || ri->error_code()) { 0 0 0
if (perl_only || ri->error_code()) { 0 0 0
140 // Failed. Are we in strict RE2 only mode?
141 0 ▶ SV *const re2_strict = cophh_fetch_pvs(PL_curcop->cop_hints_hash,
Branch: 1 branch
BranchTrueFalseTotal
SV *const re2_strict = cophh_fetch_pvs(PL_curcop->cop_hints_hash, 0 0 0
142 "re::engine::RE2::strict", 0);
143  
144 0 ▶ if (SvOK(re2_strict) && SvTRUE(re2_strict)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvOK(re2_strict) && SvTRUE(re2_strict)) { 0 0 0
if (SvOK(re2_strict) && SvTRUE(re2_strict)) { 0 0 0
if (SvOK(re2_strict) && SvTRUE(re2_strict)) { 0 0 0
if (SvOK(re2_strict) && SvTRUE(re2_strict)) { 0 0 0
145 0 ▶ const string error = ri->error();
Branch: 1 branch
BranchTrueFalseTotal
const string error = ri->error(); 0 0 0
146 0 ▶ delete ri;
Branch: 1 branch
BranchTrueFalseTotal
delete ri; 0 0 0
147  
148 0 ▶ croak("%s", perl_only
Branch: 1 branch
BranchTrueFalseTotal
croak("%s", perl_only 0 0 0
149 ? "/x is not supported by RE2"
150 : error.c_str());
151 return NULL; // unreachable
152 }
153  
154 0 ▶ delete ri;
Branch: 1 branch
BranchTrueFalseTotal
delete ri; 0 0 0
155  
156 // Fallback. Perl will either compile or print errors for us.
157 0 ▶ return Perl_re_compile(aTHX_ pattern, flags);
Branch: 1 branch
BranchTrueFalseTotal
return Perl_re_compile(aTHX_ pattern, flags); 0 0 0
158 }
159  
160 0 ▶ REGEXP *rx_sv = (REGEXP*) newSV_type(SVt_REGEXP);
Branch: 1 branch
BranchTrueFalseTotal
REGEXP *rx_sv = (REGEXP*) newSV_type(SVt_REGEXP); 0 0 0
161 0 regexp *const rx = SvANY(rx_sv);
162 /* Zero the structure before we populate it. This future proofs us
163 * from any new fields or whatnot in the REGEXP structure */
164 0 Zero(rx,1,regexp);
165  
166 0 rx->extflags = extflags;
167 0 rx->engine = &re2_engine;
168  
169 0 rx->pre_prefix = SvCUR(wrapped) - plen - 1;
170  
171 0 ▶ RX_WRAPPED(rx_sv) = savepvn(SvPVX(wrapped), SvCUR(wrapped));
Branch: 1 branch
BranchTrueFalseTotal
RX_WRAPPED(rx_sv) = savepvn(SvPVX(wrapped), SvCUR(wrapped)); 0 0 0
172 0 RX_WRAPLEN(rx_sv) = SvCUR(wrapped);
173  
174 /* Store our private object */
175 0 rx->pprivate = (void *) ri;
176  
177 /* If named captures are defined make rx->paren_names */
178 0 ▶ const map<string, int> ncg(ri->NamedCapturingGroups());
Branch: 2 branches
BranchTrueFalseTotal
const map<string, int> ncg(ri->NamedCapturingGroups()); 0 0 0
const map<string, int> ncg(ri->NamedCapturingGroups()); 0 0 0
179 0 for(map<string, int>::const_iterator it = ncg.begin();
180 0 ▶ it != ncg.end();
Branch: 1 branch
BranchTrueFalseTotal
it != ncg.end(); 0 0 0
181 0 ++it) {
182 // This block assumes RE2 won't give us multiple named captures of the
183 // same name -- it currently doesn't support this.
184 0 ▶ SV* value = newSV_type(SVt_PVNV);
Branch: 1 branch
BranchTrueFalseTotal
SV* value = newSV_type(SVt_PVNV); 0 0 0
185  
186 0 SvIV_set(value, 1);
187 0 SvIOK_on(value);
188 0 I32 offsetp = it->second;
189 0 ▶ sv_setpvn(value, (char*)&offsetp, sizeof offsetp);
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvn(value, (char*)&offsetp, sizeof offsetp); 0 0 0
190  
191 0 ▶ if (!RXp_PAREN_NAMES(rx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!RXp_PAREN_NAMES(rx)) { 0 0 0
192 0 ▶ RXp_PAREN_NAMES(rx) = newHV();
Branch: 1 branch
BranchTrueFalseTotal
RXp_PAREN_NAMES(rx) = newHV(); 0 0 0
193 }
194  
195 0 ▶ hv_store(RXp_PAREN_NAMES(rx), it->first.data(), it->first.size(), value, 0);
Branch: 1 branch
BranchTrueFalseTotal
hv_store(RXp_PAREN_NAMES(rx), it->first.data(), it->first.size(), value, 0); 0 0 0
196 }
197  
198 /* these are run time properties, not compile time - and we zero the struct
199 above so there is no point in clearing them here.
200  
201 rx->lastparen = rx->lastcloseparen = 0;
202 */
203 0 rx->nparens = ri->NumberOfCapturingGroups();
204  
205 #if (PERL_VERSION >= 37 && PERL_SUBVERSION >=7) || PERL_VERSION > 37
206 0 rx->logical_nparens = rx->nparens;
207 #endif
208  
209  
210 0 ▶ Newxz(rx->offs, rx->nparens + 1, regexp_paren_pair);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(rx->offs, rx->nparens + 1, regexp_paren_pair); 0 0 0
211  
212 /* return the regexp */
213 0 return rx_sv;
214 0 }
215  
216 #if PERL_VERSION >= 19
217 I32
218 0 RE2_exec(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
219 char *strbeg, SSize_t minend, SV * sv,
220 void *data, U32 flags)
221 #else
222 I32
223 RE2_exec(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
224 char *strbeg, I32 minend, SV * sv,
225 void *data, U32 flags)
226 #endif
227 {
228 0 RE2 * ri = (RE2*) SvANY(rx)->pprivate;
229 0 regexp * re = SvANY(rx);
230  
231 0 ▶ re2::StringPiece res[re->nparens + 1];
Branch: 1 branch
BranchTrueFalseTotal
re2::StringPiece res[re->nparens + 1]; 0 0 0
232  
233 #ifdef RE2_DEBUG
234 Perl_warner(aTHX_ packWARN(WARN_MISC), "RE2: Matching '%s' (%p, %p) against '%s'", stringarg, strbeg, stringarg, RX_WRAPPED(rx));
235 #endif
236  
237 0 ▶ if(stringarg > strend) {
Branch: 1 branch
BranchTrueFalseTotal
if(stringarg > strend) { 0 0 0
238 0 re->offs[0].start = -1;
239 0 re->offs[0].end = -1;
240 0 return 0;
241 }
242  
243 0 ▶ bool ok = ri->Match(
Branch: 1 branch
BranchTrueFalseTotal
bool ok = ri->Match( 0 0 0
244 0 re2::StringPiece(strbeg, strend - strbeg),
245 0 stringarg - strbeg,
246 0 strend - strbeg,
247 RE2::UNANCHORED,
248 0 res, sizeof res / sizeof *res);
249  
250 /* Matching failed */
251 0 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 0 0
252 0 return 0;
253 }
254  
255 0 re->subbeg = strbeg;
256 0 re->sublen = strend - strbeg;
257  
258 0 ▶ for (U32 i = 0; i <= re->nparens; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (U32 i = 0; i <= re->nparens; i++) { 0 0 0
259 0 ▶ if(res[i].data()) {
Branch: 1 branch
BranchTrueFalseTotal
if(res[i].data()) { 0 0 0
260 0 re->offs[i].start = res[i].data() - strbeg;
261 0 re->offs[i].end = res[i].data() - strbeg + res[i].length();
262 /* lastparen is the last paren in the struct that matched,
263 * and is used as an optimization by perl.
264 * lastcloseparen is the last paren in the struct that was closed
265 * lastcloseparen is used to implement $^N */
266 0 re->lastparen = re->lastcloseparen = i;
267 } else {
268 0 re->offs[i].start = -1;
269 0 re->offs[i].end = -1;
270 }
271 }
272  
273 0 return 1;
274 0 }
275  
276 char *
277 0 RE2_intuit(pTHX_ REGEXP * const rx, SV * sv, const char *strbeg, char *strpos,
278 char *strend, U32 flags, re_scream_pos_data *data)
279 {
280 PERL_UNUSED_ARG(rx);
281 PERL_UNUSED_ARG(sv);
282 PERL_UNUSED_ARG(strbeg);
283 PERL_UNUSED_ARG(strpos);
284 PERL_UNUSED_ARG(strend);
285 PERL_UNUSED_ARG(flags);
286 PERL_UNUSED_ARG(data);
287 0 return NULL;
288 }
289  
290 SV *
291 0 RE2_checkstr(pTHX_ REGEXP * const rx)
292 {
293 PERL_UNUSED_ARG(rx);
294 0 return NULL;
295 }
296  
297 void
298 0 RE2_free(pTHX_ REGEXP * const rx)
299 {
300 0 ▶ delete (RE2 *) SvANY(rx)->pprivate;
Branch: 1 branch
BranchTrueFalseTotal
delete (RE2 *) SvANY(rx)->pprivate; 0 0 0
301 0 }
302  
303 // Perl polluting our namespace, again.
304 #undef Copy
305 void *
306 0 RE2_dupe(pTHX_ REGEXP * const rx, CLONE_PARAMS *param)
307 {
308 PERL_UNUSED_ARG(param);
309  
310 0 RE2 *previous = (RE2*) SvANY(rx)->pprivate;
311 0 RE2::Options options;
312 0 options.Copy(previous->options());
313  
314 0 ▶ return new RE2 (re2::StringPiece(RX_WRAPPED(rx), RX_WRAPLEN(rx)), options);
Branch: 3 branches
BranchTrueFalseTotal
return new RE2 (re2::StringPiece(RX_WRAPPED(rx), RX_WRAPLEN(rx)), options); 0 0 0
return new RE2 (re2::StringPiece(RX_WRAPPED(rx), RX_WRAPLEN(rx)), options); 0 0 0
return new RE2 (re2::StringPiece(RX_WRAPPED(rx), RX_WRAPLEN(rx)), options); 0 0 0
315 }
316  
317 SV *
318 0 RE2_package(pTHX_ REGEXP * const rx)
319 {
320 PERL_UNUSED_ARG(rx);
321 0 return newSVpvs("re::engine::RE2");
322 }
323  
324 };
325  
326 // Unnamespaced
327 0 extern "C" void RE2_possible_match_range(pTHX_ REGEXP* rx, STRLEN len, SV** min_sv, SV** max_sv)
328 {
329 0 const RE2 *const re2 = (RE2*) SvANY(rx)->pprivate;
330 0 string min, max;
331  
332 0 ▶ re2->PossibleMatchRange(&min, &max, (int)len);
Branch: 1 branch
BranchTrueFalseTotal
re2->PossibleMatchRange(&min, &max, (int)len); 0 0 0
333  
334 0 ▶ *min_sv = newSVpvn(min.data(), min.size());
Branch: 1 branch
BranchTrueFalseTotal
*min_sv = newSVpvn(min.data(), min.size()); 0 0 0
335 0 ▶ *max_sv = newSVpvn(max.data(), max.size());
Branch: 1 branch
BranchTrueFalseTotal
*max_sv = newSVpvn(max.data(), max.size()); 0 0 0
336  
337 0 return;
338 0 }
339  
340 0 extern "C" HV* RE2_named_captures(pTHX_ REGEXP* rx)
341 {
342 0 const RE2 *const re2 = (RE2*) SvANY(rx)->pprivate;
343 0 ▶ const map<string, int> ncg(re2->NamedCapturingGroups());
Branch: 2 branches
BranchTrueFalseTotal
const map<string, int> ncg(re2->NamedCapturingGroups()); 0 0 0
const map<string, int> ncg(re2->NamedCapturingGroups()); 0 0 0
344 0 ▶ HV* hv = newHV();
Branch: 1 branch
BranchTrueFalseTotal
HV* hv = newHV(); 0 0 0
345 0 for(map<string, int>::const_iterator it = ncg.begin();
346 0 ▶ it != ncg.end();
Branch: 1 branch
BranchTrueFalseTotal
it != ncg.end(); 0 0 0
347 0 ++it) {
348 0 ▶ hv_store(hv, it->first.data(), it->first.size(), newSViv(it->second), 0);
Branch: 2 branches
BranchTrueFalseTotal
hv_store(hv, it->first.data(), it->first.size(), newSViv(it->second), 0); 0 0 0
hv_store(hv, it->first.data(), it->first.size(), newSViv(it->second), 0); 0 0 0
349 }
350  
351 0 return hv;
352 0 }
353  
354 0 extern "C" int RE2_number_of_capture_groups(pTHX_ REGEXP* rx)
355 {
356 0 const RE2 *const re2 = (RE2*) SvANY(rx)->pprivate;
357 0 return re2->NumberOfCapturingGroups();
358 }
359  
360 // ex:sw=4 et:
« re2/re2/stringpiece.h ↑ Summary blib/lib/re/engine/RE2.pm »