lib/String/Base.xs

filter: Statementstmt 92.1% 106 / 115 Branchbran 68.1% 60 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.7% 166 / 203 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/String/Base.pm »
1 #define PERL_NO_GET_CONTEXT 1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
7 #define PERL_DECIMAL_VERSION \
8 PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
9 #define PERL_VERSION_GE(r,v,s) \
10 (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
11  
12 #ifndef cBOOL
13 # define cBOOL(x) ((bool)!!(x))
14 #endif /* !cBOOL */
15  
16 #ifndef C_ARRAY_LENGTH
17 # define C_ARRAY_LENGTH(a) (sizeof(a)/sizeof(*(a)))
18 #endif /* !C_ARRAY_LENGTH */
19  
20 #ifndef newSVpvs_share
21 # define newSVpvs_share(STR) newSVpvn_share(""STR"", sizeof(STR)-1, 0)
22 #endif /* !newSVpvs_share */
23  
24 #ifndef SvSHARED_HASH
25 # define SvSHARED_HASH(SV) SvUVX(SV)
26 #endif /* !SvSHARED_HASH */
27  
28 #ifndef OpMORESIB_set
29 # define OpMORESIB_set(o, sib) ((o)->op_sibling = (sib))
30 # define OpLASTSIB_set(o, parent) ((o)->op_sibling = NULL)
31 # define OpMAYBESIB_set(o, sib, parent) ((o)->op_sibling = (sib))
32 #endif /* !OpMORESIB_set */
33 #ifndef OpSIBLING
34 # define OpHAS_SIBLING(o) (cBOOL((o)->op_sibling))
35 # define OpSIBLING(o) (0 + (o)->op_sibling)
36 #endif /* !OpSIBLING */
37  
38 #ifndef op_contextualize
39 # define scalar(op) Perl_scalar(aTHX_ op)
40 # define list(op) Perl_list(aTHX_ op)
41 # define scalarvoid(op) Perl_scalarvoid(aTHX_ op)
42 # define op_contextualize(op, c) THX_op_contextualize(aTHX_ op, c)
43 static OP *THX_op_contextualize(pTHX_ OP *o, I32 context)
44 {
45 switch (context) {
46 case G_SCALAR: return scalar(o);
47 case G_ARRAY: return list(o);
48 case G_VOID: return scalarvoid(o);
49 default:
50 croak("panic: op_contextualize bad context");
51 return o;
52 }
53 }
54 #endif /* !op_contextualize */
55  
56 #if !PERL_VERSION_GE(5,9,3)
57 typedef OP *(*Perl_check_t)(pTHX_ OP *);
58 #endif /* <5.9.3 */
59  
60 #if !PERL_VERSION_GE(5,10,1)
61 typedef unsigned Optype;
62 #endif /* <5.10.1 */
63  
64 #ifndef wrap_op_checker
65 # define wrap_op_checker(c,n,o) THX_wrap_op_checker(aTHX_ c,n,o)
66 static void THX_wrap_op_checker(pTHX_ Optype opcode,
67 Perl_check_t new_checker, Perl_check_t *old_checker_p)
68 {
69 if(*old_checker_p) return;
70 OP_REFCNT_LOCK;
71 if(!*old_checker_p) {
72 *old_checker_p = PL_check[opcode];
73 PL_check[opcode] = new_checker;
74 }
75 OP_REFCNT_UNLOCK;
76 }
77 #endif /* !wrap_op_checker */
78  
79 #ifndef pad_alloc
80 # define pad_alloc(optype, tmptype) Perl_pad_alloc(aTHX_ optype, tmptype)
81 #endif /* !pad_alloc */
82  
83 static SV *base_hint_key_sv;
84 static U32 base_hint_key_hash;
85 static OP *(*THX_nxck_substr)(pTHX_ OP *o);
86 static OP *(*THX_nxck_index)(pTHX_ OP *o);
87 static OP *(*THX_nxck_rindex)(pTHX_ OP *o);
88 static OP *(*THX_nxck_pos)(pTHX_ OP *o);
89  
90 #define current_base() THX_current_base(aTHX)
91 177 static IV THX_current_base(pTHX)
92 {
93 177 HE *base_ent = hv_fetch_ent(GvHV(PL_hintgv), base_hint_key_sv, 0,
94 base_hint_key_hash);
95 177 ▶ return base_ent ? SvIV(HeVAL(base_ent)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return base_ent ? SvIV(HeVAL(base_ent)) : 0; 39 138 177
96 }
97  
98 119 static OP *THX_myck_substr(pTHX_ OP *op)
99 {
100 IV base;
101 119 ▶ if((base = current_base()) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if((base = current_base()) != 0) { 9 110 119
102 OP *pop, *sop, *iop, *rest;
103 9 ▶ if(!(op->op_flags & OPf_KIDS)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!(op->op_flags & OPf_KIDS)) { 0 9 9
104 0 bad_ops:
105 0 croak("strange op tree prevents applying string base");
106 }
107 9 pop = cLISTOPx(op)->op_first;
108 9 ▶ if(!(pop->op_type == OP_PUSHMARK ||
Branch: 1 branch
BranchTrueFalseTotal
if(!(pop->op_type == OP_PUSHMARK || 9 0 9
109 9 ▶ (pop->op_type == OP_NULL &&
Branch: 1 branch
BranchTrueFalseTotal
(pop->op_type == OP_NULL && 9 0 9
110 9 ▶ pop->op_targ == OP_PUSHMARK)))
Branch: 1 branch
BranchTrueFalseTotal
pop->op_targ == OP_PUSHMARK))) 0 9 9
111 0 goto bad_ops;
112 9 ▶ sop = OpSIBLING(pop);
Branch: 1 branch
BranchTrueFalseTotal
sop = OpSIBLING(pop); 9 0 9
113 9 ▶ if(!sop) goto bad_ops;
Branch: 1 branch
BranchTrueFalseTotal
if(!sop) goto bad_ops; 0 9 9
114 9 ▶ iop = OpSIBLING(sop);
Branch: 1 branch
BranchTrueFalseTotal
iop = OpSIBLING(sop); 9 0 9
115 9 ▶ if(!iop) goto bad_ops;
Branch: 1 branch
BranchTrueFalseTotal
if(!iop) goto bad_ops; 0 9 9
116 9 ▶ rest = OpSIBLING(iop);
Branch: 1 branch
BranchTrueFalseTotal
rest = OpSIBLING(iop); 8 1 9
117 9 ▶ OpMAYBESIB_set(sop, rest, op);
Branch: 1 branch
BranchTrueFalseTotal
OpMAYBESIB_set(sop, rest, op); 8 1 9
118 9 OpLASTSIB_set(iop, NULL);
119 9 ▶ if(!rest) cLISTOPx(op)->op_last = sop;
Branch: 1 branch
BranchTrueFalseTotal
if(!rest) cLISTOPx(op)->op_last = sop; 1 8 9
120 9 iop = newBINOP(OP_I_SUBTRACT, 0,
121 op_contextualize(iop, G_SCALAR),
122 newSVOP(OP_CONST, 0, newSViv(base)));
123 9 ▶ OpMAYBESIB_set(iop, rest, op);
Branch: 1 branch
BranchTrueFalseTotal
OpMAYBESIB_set(iop, rest, op); 8 1 9
124 9 OpMORESIB_set(sop, iop);
125 9 ▶ if(!rest) cLISTOPx(op)->op_last = iop;
Branch: 1 branch
BranchTrueFalseTotal
if(!rest) cLISTOPx(op)->op_last = iop; 1 8 9
126 }
127 119 return THX_nxck_substr(aTHX_ op);
128 }
129  
130 34 static OP *THX_myck_index(pTHX_ OP *op)
131 {
132 IV base;
133 34 ▶ if((base = current_base()) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if((base = current_base()) != 0) { 14 20 34
134 OP *pop, *hop, *nop, *iop;
135 14 ▶ if(!(op->op_flags & OPf_KIDS)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!(op->op_flags & OPf_KIDS)) { 0 14 14
136 0 bad_ops:
137 0 croak("strange op tree prevents applying string base");
138 }
139 14 pop = cLISTOPx(op)->op_first;
140 14 ▶ if(!(pop->op_type == OP_PUSHMARK ||
Branch: 1 branch
BranchTrueFalseTotal
if(!(pop->op_type == OP_PUSHMARK || 14 0 14
141 14 ▶ (pop->op_type == OP_NULL &&
Branch: 1 branch
BranchTrueFalseTotal
(pop->op_type == OP_NULL && 14 0 14
142 14 ▶ pop->op_targ == OP_PUSHMARK)))
Branch: 1 branch
BranchTrueFalseTotal
pop->op_targ == OP_PUSHMARK))) 0 14 14
143 0 goto bad_ops;
144 14 ▶ hop = OpSIBLING(pop);
Branch: 1 branch
BranchTrueFalseTotal
hop = OpSIBLING(pop); 14 0 14
145 14 ▶ if(!hop) goto bad_ops;
Branch: 1 branch
BranchTrueFalseTotal
if(!hop) goto bad_ops; 0 14 14
146 14 ▶ nop = OpSIBLING(hop);
Branch: 1 branch
BranchTrueFalseTotal
nop = OpSIBLING(hop); 14 0 14
147 14 ▶ if(!nop) goto bad_ops;
Branch: 1 branch
BranchTrueFalseTotal
if(!nop) goto bad_ops; 0 14 14
148 14 ▶ iop = OpSIBLING(nop);
Branch: 1 branch
BranchTrueFalseTotal
iop = OpSIBLING(nop); 10 4 14
149 14 ▶ if(iop) {
Branch: 1 branch
BranchTrueFalseTotal
if(iop) { 10 4 14
150 10 ▶ OP *rest = OpSIBLING(iop);
Branch: 1 branch
BranchTrueFalseTotal
OP *rest = OpSIBLING(iop); 0 10 10
151 10 ▶ OpMAYBESIB_set(nop, rest, op);
Branch: 1 branch
BranchTrueFalseTotal
OpMAYBESIB_set(nop, rest, op); 0 10 10
152 10 OpLASTSIB_set(iop, NULL);
153 10 ▶ if(!rest) cLISTOPx(op)->op_last = nop;
Branch: 1 branch
BranchTrueFalseTotal
if(!rest) cLISTOPx(op)->op_last = nop; 10 0 10
154 10 iop = newBINOP(OP_I_SUBTRACT, 0,
155 op_contextualize(iop, G_SCALAR),
156 newSVOP(OP_CONST, 0, newSViv(base)));
157 10 ▶ OpMAYBESIB_set(iop, rest, op);
Branch: 1 branch
BranchTrueFalseTotal
OpMAYBESIB_set(iop, rest, op); 0 10 10
158 10 OpMORESIB_set(nop, iop);
159 10 ▶ if(!rest) cLISTOPx(op)->op_last = iop;
Branch: 1 branch
BranchTrueFalseTotal
if(!rest) cLISTOPx(op)->op_last = iop; 10 0 10
160 }
161 14 ▶ op = (op->op_type == OP_INDEX ? THX_nxck_index :
Branch: 1 branch
BranchTrueFalseTotal
op = (op->op_type == OP_INDEX ? THX_nxck_index : 7 7 14
162 THX_nxck_rindex)
163 (aTHX_ op);
164 14 ▶ if((PL_opargs[op->op_type] & OA_TARGET) && !op->op_targ)
Branch: 2 branches
BranchTrueFalseTotal
if((PL_opargs[op->op_type] & OA_TARGET) && !op->op_targ) 14 0 14
if((PL_opargs[op->op_type] & OA_TARGET) && !op->op_targ) 14 0 14
165 14 op->op_targ = pad_alloc(op->op_type, SVs_PADTMP);
166 14 return newBINOP(OP_I_ADD, 0, op_contextualize(op, G_SCALAR),
167 newSVOP(OP_CONST, 0, newSViv(base)));
168 } else {
169 20 ▶ return (op->op_type == OP_INDEX ? THX_nxck_index :
Branch: 1 branch
BranchTrueFalseTotal
return (op->op_type == OP_INDEX ? THX_nxck_index : 16 4 20
170 THX_nxck_rindex)
171 (aTHX_ op);
172 }
173 }
174  
175 16 static OP *THX_pp_dup(pTHX)
176 {
177 16 dSP;
178 16 SV *val = TOPs;
179 16 ▶ XPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(val); 0 16 16
180 16 PUTBACK;
181 16 return PL_op->op_next;
182 }
183  
184 #define newUNOP_dup(argop) THX_newUNOP_dup(aTHX_ argop)
185 16 static OP *THX_newUNOP_dup(pTHX_ OP *argop)
186 {
187 OP *dupop;
188 16 NewOpSz(0, dupop, sizeof(UNOP));
189 #ifdef XopENTRY_set
190 16 dupop->op_type = OP_CUSTOM;
191 #else /* !XopENTRY_set */
192 dupop->op_type = OP_RAND;
193 #endif /* !XopENTRY_set */
194 16 dupop->op_ppaddr = THX_pp_dup;
195 16 cUNOPx(dupop)->op_flags = OPf_KIDS;
196 16 cUNOPx(dupop)->op_first = argop;
197 16 OpLASTSIB_set(argop, dupop);
198 16 return dupop;
199 }
200  
201 0 static OP *THX_pp_foldsafe_null(pTHX)
202 {
203 0 return PL_op->op_next;
204 }
205  
206 #ifdef XopENTRY_set
207 16 static void THX_cpeep_foldsafe_null(pTHX_ OP *o, OP *oldop)
208 {
209 PERL_UNUSED_ARG(oldop);
210 # if PERL_VERSION_GE(5,19,10)
211 16 op_null(o);
212 # else /* <5.19.10 */
213 PERL_UNUSED_ARG(o);
214 # endif /* <5.19.10 */
215 16 }
216 #endif /* XopENTRY_set */
217  
218 #define newOP_foldsafe_null() THX_newOP_foldsafe_null(aTHX)
219 16 static OP *THX_newOP_foldsafe_null(pTHX)
220 {
221 OP *op;
222 16 NewOpSz(0, op, sizeof(OP));
223 #ifdef XopENTRY_set
224 16 op->op_type = OP_CUSTOM;
225 #else /* !XopENTRY_set */
226 op->op_type = OP_RAND;
227 #endif /* !XopENTRY_set */
228 16 op->op_ppaddr = THX_pp_foldsafe_null;
229 16 op->op_next = op;
230 16 return op;
231 }
232  
233 24 static OP *THX_myck_pos(pTHX_ OP *op)
234 {
235 IV base;
236 24 ▶ if((base = current_base()) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if((base = current_base()) != 0) { 16 8 24
237 16 op = THX_nxck_pos(aTHX_ op);
238 16 ▶ if((PL_opargs[op->op_type] & OA_TARGET) && !op->op_targ)
Branch: 2 branches
BranchTrueFalseTotal
if((PL_opargs[op->op_type] & OA_TARGET) && !op->op_targ) 16 0 16
if((PL_opargs[op->op_type] & OA_TARGET) && !op->op_targ) 16 0 16
239 16 op->op_targ = pad_alloc(op->op_type, SVs_PADTMP);
240 16 return newCONDOP(0,
241 newUNOP(OP_DEFINED, 0,
242 newUNOP_dup(op_contextualize(op, G_SCALAR))),
243 newBINOP(OP_I_ADD, 0, newOP_foldsafe_null(),
244 newSVOP(OP_CONST, 0, newSViv(base))),
245 newOP(OP_NULL, 0));
246 } else {
247 8 return THX_nxck_pos(aTHX_ op);
248 }
249 }
250  
251 MODULE = String::Base PACKAGE = String::Base
252  
253 PROTOTYPES: DISABLE
254  
255 BOOT:
256 {
257 #ifdef XopENTRY_set
258 struct {
259 char const *name, *desc;
260 U32 class;
261 Perl_cpeep_t THX_cpeep;
262 Perl_ppaddr_t THX_pp;
263 5 } const ops_to_register[] = {
264 { "dup", "duplicate", OA_UNOP, (Perl_cpeep_t)0, THX_pp_dup },
265 { "foldsafe_null", "non-foldable null", OA_BASEOP,
266 THX_cpeep_foldsafe_null, THX_pp_foldsafe_null },
267 }, *otr;
268 int i;
269 15 ▶ for(i = C_ARRAY_LENGTH(ops_to_register); i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = C_ARRAY_LENGTH(ops_to_register); i--; ) { 10 5 15
270 XOP *xop;
271 10 Newxz(xop, 1, XOP);
272 10 otr = &ops_to_register[i];
273 10 XopENTRY_set(xop, xop_name, otr->name);
274 10 XopENTRY_set(xop, xop_desc, otr->desc);
275 10 XopENTRY_set(xop, xop_class, otr->class);
276 10 ▶ if(otr->THX_cpeep) XopENTRY_set(xop, xop_peep, otr->THX_cpeep);
Branch: 1 branch
BranchTrueFalseTotal
if(otr->THX_cpeep) XopENTRY_set(xop, xop_peep, otr->THX_cpeep); 5 5 10
277 10 Perl_custom_op_register(aTHX_ otr->THX_pp, xop);
278 }
279 #endif /* XopENTRY_set */
280 }
281  
282 BOOT:
283 {
284 5 base_hint_key_sv = newSVpvs_share("String::Base/base");
285 5 base_hint_key_hash = SvSHARED_HASH(base_hint_key_sv);
286 5 wrap_op_checker(OP_SUBSTR, THX_myck_substr, &THX_nxck_substr);
287 5 wrap_op_checker(OP_INDEX, THX_myck_index, &THX_nxck_index);
288 5 wrap_op_checker(OP_RINDEX, THX_myck_index, &THX_nxck_rindex);
289 5 wrap_op_checker(OP_POS, THX_myck_pos, &THX_nxck_pos);
290 }
291  
292 void
293 import(SV *classname, IV base)
294 CODE:
295 PERL_UNUSED_VAR(classname);
296 8 PL_hints |= HINT_LOCALIZE_HH;
297 8 gv_HVadd(PL_hintgv);
298 8 ▶ if(base == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(base == 0) { 1 7 8
299 1 (void) hv_delete_ent(GvHV(PL_hintgv), base_hint_key_sv,
300 G_DISCARD, base_hint_key_hash);
301 } else {
302 7 SV *base_sv = newSViv(base);
303 7 HE *he = hv_store_ent(GvHV(PL_hintgv), base_hint_key_sv,
304 base_sv, base_hint_key_hash);
305 7 ▶ if(he) {
Branch: 1 branch
BranchTrueFalseTotal
if(he) { 7 0 7
306 7 SV *val = HeVAL(he);
307 7 ▶ SvSETMAGIC(val);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(val); 7 0 7
308 } else {
309 0 SvREFCNT_dec(base_sv);
310 }
311 }
312  
313 void
314 unimport(SV *classname)
315 CODE:
316 PERL_UNUSED_VAR(classname);
317 1 PL_hints |= HINT_LOCALIZE_HH;
318 1 gv_HVadd(PL_hintgv);
319 1 (void) hv_delete_ent(GvHV(PL_hintgv), base_hint_key_sv,
320 G_DISCARD, base_hint_key_hash);
↑ Summary blib/lib/String/Base.pm »