lib/Syntax/Operator/Zip.xs

filter: Statementstmt 95.4% 84 / 88 Branchbran 76.5% 49 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.5% 133 / 152 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Syntax/Operator/Zip.pm ↑ Summary
1 /* You may distribute under the terms of either the GNU General Public License
2 * or the Artistic License (the same terms as Perl itself)
3 *
4 * (C) Paul Evans, 2021-2024 -- leonerd@leonerd.org.uk
5 */
6 #include "EXTERN.h"
7 #include "perl.h"
8 #include "XSUB.h"
9  
10 #define HAVE_PERL_VERSION(R, V, S) \
11 (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
12  
13 #include "XSParseInfix.h"
14  
15 #if !HAVE_PERL_VERSION(5, 16, 0)
16 # define true TRUE
17 # define false FALSE
18 #endif
19  
20 100010 static OP *pp_zip(pTHX)
21 {
22 100010 dSP;
23 100010 ▶ int nlists = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private;
Branch: 1 branch
BranchTrueFalseTotal
int nlists = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private; 0 100010 100010
24  
25 /* Most invocations will only have 2 lists. We'll account for up to 4 as
26 * local variables; anything bigger we'll allocate temporary SV buffers
27 */
28 U32 counts4[4];
29 SV **svp4[4];
30  
31 100010 U32 maxcount = 0;
32 100010 ▶ U32 *counts = nlists <= 4 ? counts4 : (U32 *)SvPVX(sv_2mortal(newSV(nlists * sizeof(U32))));
Branch: 1 branch
BranchTrueFalseTotal
U32 *counts = nlists <= 4 ? counts4 : (U32 *)SvPVX(sv_2mortal(newSV(nlists * sizeof(U32)))); 100010 0 100010
33 300032 ▶ for(int i = nlists; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for(int i = nlists; i > 0; i--) { 200022 100010 300032
34 200022 U32 mark = POPMARK;
35 200022 U32 count = SP - (PL_stack_base + mark);
36 200022 counts[i-1] = count;
37 200022 ▶ if(count > maxcount)
Branch: 1 branch
BranchTrueFalseTotal
if(count > maxcount) 100012 100010 200022
38 100012 maxcount = count;
39  
40 200022 SP = PL_stack_base + mark;
41 }
42  
43 100010 ▶ if(GIMME_V == G_VOID)
Branch: 1 branch
BranchTrueFalseTotal
if(GIMME_V == G_VOID) 0 100010 100010
44 0 return NORMAL;
45 100010 ▶ if(GIMME_V == G_SCALAR) {
Branch: 1 branch
BranchTrueFalseTotal
if(GIMME_V == G_SCALAR) { 3 100007 100010
46 3 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 3 3
47 3 mPUSHi(maxcount);
48 3 RETURN;
49 }
50  
51 /* known G_LIST */
52  
53 /* No need to EXTEND because we know the stack will be big enough */
54 100007 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 100007 100007
55  
56 100007 ▶ if(!maxcount)
Branch: 1 branch
BranchTrueFalseTotal
if(!maxcount) 0 100007 100007
57 0 RETURN;
58  
59 100007 ▶ SV ***svp = nlists <= 4 ? svp4 : (SV ***)SvPVX(sv_2mortal(newSV(nlists * sizeof(SV **))));
Branch: 1 branch
BranchTrueFalseTotal
SV ***svp = nlists <= 4 ? svp4 : (SV ***)SvPVX(sv_2mortal(newSV(nlists * sizeof(SV **)))); 100007 0 100007
60 100007 svp[0] = SP + 1;
61 200016 ▶ for(int i = 1; i < nlists; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(int i = 1; i < nlists; i++) 100009 100007 200016
62 100009 svp[i] = svp[i-1] + counts[i-1];
63  
64 100007 bool more = true;
65 do {
66 500019 more = false;
67 500019 AV *av = newAV();
68 1500061 ▶ for(int i = 0; i < nlists; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(int i = 0; i < nlists; i++) { 1000042 500019 1500061
69 1000042 ▶ if(counts[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if(counts[i]) { 1000040 2 1000042
70 1000040 av_push(av, newSVsv(*(svp[i])));
71 1000040 svp[i]++, counts[i]--;
72  
73 1000040 ▶ if(counts[i])
Branch: 1 branch
BranchTrueFalseTotal
if(counts[i]) 800024 200016 1000040
74 800024 more = true;
75 }
76 else
77 2 av_push(av, &PL_sv_undef);
78 }
79 500019 mPUSHs(newRV_noinc((SV *)av));
80 500019 ▶ } while(more);
Branch: 1 branch
BranchTrueFalseTotal
} while(more); 400012 100007 500019
81  
82 100007 RETURN;
83 }
84  
85 static const struct XSParseInfixHooks infix_zip = {
86 /* Parse this at ADD precedence, so that (LIST)xCOUNT can be used on RHS */
87 .cls = XPI_CLS_ADD_MISC,
88 .flags = XPI_FLAG_LISTASSOC,
89 .lhs_flags = XPI_OPERAND_TERM_LIST|XPI_OPERAND_ONLY_LOOK,
90 .rhs_flags = XPI_OPERAND_TERM_LIST|XPI_OPERAND_ONLY_LOOK,
91 .permit_hintkey = "Syntax::Operator::Zip/Z",
92  
93 .wrapper_func_name = "Syntax::Operator::Zip::zip",
94  
95 .ppaddr = &pp_zip,
96 };
97  
98 10 static OP *pp_mesh(pTHX)
99 {
100 10 dSP;
101 10 ▶ int nlists = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private;
Branch: 1 branch
BranchTrueFalseTotal
int nlists = (PL_op->op_flags & OPf_STACKED) ? POPu : PL_op->op_private; 0 10 10
102  
103 /* Most invocations will only have 2 lists. We'll account for up to 4 as
104 * local variables; anything bigger we'll allocate temporary SV buffers
105 */
106 U32 counts4[4];
107 SV **svp4[4];
108  
109 10 U32 maxcount = 0;
110 10 ▶ U32 *counts = nlists <= 4 ? counts4 : (U32 *)SvPVX(sv_2mortal(newSV(nlists * sizeof(U32))));
Branch: 1 branch
BranchTrueFalseTotal
U32 *counts = nlists <= 4 ? counts4 : (U32 *)SvPVX(sv_2mortal(newSV(nlists * sizeof(U32)))); 10 0 10
111 32 ▶ for(int i = nlists; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for(int i = nlists; i > 0; i--) { 22 10 32
112 22 U32 mark = POPMARK;
113 22 U32 count = SP - (PL_stack_base + mark);
114 22 counts[i-1] = count;
115 22 ▶ if(count > maxcount)
Branch: 1 branch
BranchTrueFalseTotal
if(count > maxcount) 12 10 22
116 12 maxcount = count;
117  
118 22 SP = PL_stack_base + mark;
119 }
120  
121 10 U32 retcount = maxcount * nlists;
122  
123 10 ▶ if(GIMME_V == G_VOID)
Branch: 1 branch
BranchTrueFalseTotal
if(GIMME_V == G_VOID) 0 10 10
124 0 return NORMAL;
125 10 ▶ if(GIMME_V == G_SCALAR) {
Branch: 1 branch
BranchTrueFalseTotal
if(GIMME_V == G_SCALAR) { 3 7 10
126 3 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 3 3
127 3 mPUSHi(retcount);
128 3 RETURN;
129 }
130  
131 /* known G_LIST */
132 7 ▶ EXTEND(SP, retcount);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, retcount); 0 7 7
133 7 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 7 7
134  
135 7 ▶ if(!retcount)
Branch: 1 branch
BranchTrueFalseTotal
if(!retcount) 0 7 7
136 0 RETURN;
137  
138 7 ▶ SV ***svp = nlists <= 4 ? svp4 : (SV ***)SvPVX(sv_2mortal(newSV(nlists * sizeof(SV **))));
Branch: 1 branch
BranchTrueFalseTotal
SV ***svp = nlists <= 4 ? svp4 : (SV ***)SvPVX(sv_2mortal(newSV(nlists * sizeof(SV **)))); 7 0 7
139 7 svp[0] = SP + 1;
140 16 ▶ for(int i = 1; i < nlists; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(int i = 1; i < nlists; i++) 9 7 16
141 9 svp[i] = svp[i-1] + counts[i-1];
142  
143 /* We can't easily do this inplace so we'll have to store the result in a
144 * temporary array
145 */
146 7 AV *tmpav = newAV();
147 7 sv_2mortal((SV *)tmpav);
148 7 av_extend(tmpav, retcount - 1);
149  
150 7 SV **result = AvARRAY(tmpav);
151  
152 7 bool more = true;
153 do {
154 19 more = false;
155 61 ▶ for(int i = 0; i < nlists; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(int i = 0; i < nlists; i++) { 42 19 61
156 42 ▶ if(counts[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if(counts[i]) { 40 2 42
157 40 *result = sv_mortalcopy(*(svp[i]));
158 40 svp[i]++, counts[i]--;
159  
160 40 ▶ if(counts[i])
Branch: 1 branch
BranchTrueFalseTotal
if(counts[i]) 24 16 40
161 24 more = true;
162 }
163 else
164 2 *result = &PL_sv_undef;
165 42 result++;
166 }
167 19 ▶ } while(more);
Branch: 1 branch
BranchTrueFalseTotal
} while(more); 12 7 19
168  
169 7 result = AvARRAY(tmpav);
170 49 ▶ for(U32 i = 0; i < retcount; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(U32 i = 0; i < retcount; i++) 42 7 49
171 42 PUSHs(*result++);
172  
173 7 AvREAL_off(tmpav); // AV shouldn't own the SVs
174 7 RETURN;
175 }
176  
177 static const struct XSParseInfixHooks infix_mesh = {
178 .cls = XPI_CLS_ADD_MISC,
179 .flags = XPI_FLAG_LISTASSOC,
180 .lhs_flags = XPI_OPERAND_TERM_LIST|XPI_OPERAND_ONLY_LOOK,
181 .rhs_flags = XPI_OPERAND_TERM_LIST|XPI_OPERAND_ONLY_LOOK,
182 .permit_hintkey = "Syntax::Operator::Zip/M",
183  
184 .wrapper_func_name = "Syntax::Operator::Zip::mesh",
185  
186 .ppaddr = &pp_mesh,
187 };
188  
189 MODULE = Syntax::Operator::Zip PACKAGE = Syntax::Operator::Zip
190  
191 BOOT:
192 5 boot_xs_parse_infix(0.40);
193  
194 5 register_xs_parse_infix("Z", &infix_zip, NULL);
195 5 register_xs_parse_infix("M", &infix_mesh, NULL);
« blib/lib/Syntax/Operator/Zip.pm ↑ Summary