src/stencil_filters.c

filter: Statementstmt 98.1% 163 / 166 Branchbran 76.1% 131 / 172 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.9% 294 / 338 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/stencil_compile.c ↑ Summary src/stencil_vm.inc »
1 /* utf8_to_uvchr_buf is 5.16; ask ppport.h (included from stencil.h) to
2 * emit its back-compat implementation into this TU, the only user. */
3 #define NEED_utf8_to_uvchr_buf
4  
5 #include "stencil.h"
6 #include "stencil_expfix.h"
7  
8 50936 void stencil_filt_case(pTHX_ SV *in, SV *out, int to_upper)
9 {
10 STRLEN len;
11 50936 const char *p = SvPV(in, len);
12  
13 50936 ▶ if (!SvUTF8(in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(in)) { 50931 5 50936
14 char *w;
15 STRLEN i;
16 50931 ▶ SvUPGRADE(out, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(out, SVt_PV); 0 50931 50931
17 50931 ▶ SvGROW(out, len + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out, len + 1); 50931 0 50931
SvGROW(out, len + 1); 0 50931 50931
18 50931 w = SvPVX(out);
19 50931 ▶ if (to_upper) {
Branch: 1 branch
BranchTrueFalseTotal
if (to_upper) { 50616 315 50931
20 1006925 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 956309 50616 1006925
21 956309 char c = p[i];
22 956309 ▶ w[i] = (c >= 'a' && c <= 'z') ? (char)(c - 32) : c;
Branch: 2 branches
BranchTrueFalseTotal
w[i] = (c >= 'a' && c <= 'z') ? (char)(c - 32) : c; 554132 402177 956309
w[i] = (c >= 'a' && c <= 'z') ? (char)(c - 32) : c; 554132 0 554132
23 }
24 } else {
25 4052 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 3737 315 4052
26 3737 char c = p[i];
27 3737 ▶ w[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c;
Branch: 2 branches
BranchTrueFalseTotal
w[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c; 2854 883 3737
w[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c; 2781 73 2854
28 }
29 }
30 50931 w[len] = '\0';
31 50931 SvCUR_set(out, len);
32 50931 SvPOK_only(out);
33 50931 SvUTF8_off(out);
34 50931 return;
35 }
36  
37 {
38 /* to_uni_upper/lower write the full (possibly multi-char)
39 * mapping as UTF-8 into a scratch buffer */
40 5 const U8 *s = (const U8 *)p;
41 5 const U8 *end = s + len;
42 5 STRLEN used = 0;
43 U8 tmp[UTF8_MAXBYTES_CASE + 1];
44 5 ▶ SvUPGRADE(out, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(out, SVt_PV); 0 5 5
45 5 ▶ SvGROW(out, len + UTF8_MAXBYTES_CASE + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out, len + UTF8_MAXBYTES_CASE + 1); 5 0 5
SvGROW(out, len + UTF8_MAXBYTES_CASE + 1); 0 5 5
46 69 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 64 5 69
47 STRLEN clen, tlen;
48 64 UV uv = utf8_to_uvchr_buf(s, end, &clen);
49 64 ▶ if (to_upper)
Branch: 1 branch
BranchTrueFalseTotal
if (to_upper) 38 26 64
50 38 (void)to_uni_upper(uv, tmp, &tlen);
51 else
52 26 (void)to_uni_lower(uv, tmp, &tlen);
53 64 ▶ if (SvLEN(out) < used + tlen + 1)
Branch: 1 branch
BranchTrueFalseTotal
if (SvLEN(out) < used + tlen + 1) 0 64 64
54 0 ▶ SvGROW(out, (used + tlen + 1) * 2);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out, (used + tlen + 1) * 2); 0 0 0
SvGROW(out, (used + tlen + 1) * 2); 0 0 0
55 64 memcpy(SvPVX(out) + used, tmp, tlen);
56 64 used += tlen;
57 64 ▶ s += clen ? clen : 1;
Branch: 1 branch
BranchTrueFalseTotal
s += clen ? clen : 1; 64 0 64
58 }
59 5 SvPVX(out)[used] = '\0';
60 5 SvCUR_set(out, used);
61 5 SvPOK_only(out);
62 5 SvUTF8_on(out);
63 }
64 }
65  
66 344 void stencil_filt_trim(pTHX_ SV *in, SV *out)
67 {
68 STRLEN len;
69 344 const char *p = SvPV(in, len);
70 344 const char *e = p + len;
71 592 ▶ while (p < e && isSPACE(*p))
Branch: 2 branches
BranchTrueFalseTotal
while (p < e && isSPACE(*p)) 591 1 592
while (p < e && isSPACE(*p)) 248 343 591
72 248 p++;
73 589 ▶ while (e > p && isSPACE(e[-1]))
Branch: 2 branches
BranchTrueFalseTotal
while (e > p && isSPACE(e[-1])) 588 1 589
while (e > p && isSPACE(e[-1])) 245 343 588
74 245 e--;
75 344 sv_setpvn(out, p, (STRLEN)(e - p));
76 344 ▶ if (SvUTF8(in))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(in)) 1 343 344
77 1 SvUTF8_on(out);
78 else
79 343 SvUTF8_off(out);
80 344 }
81  
82 12 void stencil_filt_html(pTHX_ SV *in, SV *out)
83 {
84 STRLEN len;
85 12 const char *p = SvPV(in, len);
86 stencil_buf b;
87  
88 /* attach the buffer machinery to the scratch SV so the escaper's
89 * reserve/grow reuses its allocation */
90 12 ▶ SvUPGRADE(out, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(out, SVt_PV); 0 12 12
91 12 ▶ SvGROW(out, len + 32);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out, len + 32); 12 0 12
SvGROW(out, len + 32); 0 12 12
92 12 SvPOK_only(out);
93 12 b.sv = out;
94 12 b.cur = SvPVX(out);
95 12 b.end = SvPVX(out) + SvLEN(out) - 1;
96 12 b.utf8 = 0;
97 #ifdef PERL_IMPLICIT_CONTEXT
98 b.perl = aTHX;
99 #endif
100 12 stencil_dispatch.escape(&b, p, len);
101 12 (void)stencil_buf_done(&b);
102 12 ▶ if (SvUTF8(in))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(in)) 0 12 12
103 0 SvUTF8_on(out);
104 else
105 12 SvUTF8_off(out);
106 12 }
107  
108 10 void stencil_filt_uri(pTHX_ SV *in, SV *out)
109 {
110 static const char hex[] = "0123456789ABCDEF";
111 STRLEN len, i;
112 10 const char *p = SvPV(in, len);
113 char *w;
114  
115 10 ▶ SvUPGRADE(out, SVt_PV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(out, SVt_PV); 0 10 10
116 10 ▶ SvGROW(out, len * 3 + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out, len * 3 + 1); 10 0 10
SvGROW(out, len * 3 + 1); 0 10 10
117 10 SvPOK_only(out);
118 /* latin-1-repped high bytes are UTF-8 encoded before percent
119 * escaping, so %-sequences are always UTF-8 regardless of the
120 * input SV's internal form */
121 10 ▶ SvGROW(out, len * 6 + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out, len * 6 + 1); 10 0 10
SvGROW(out, len * 6 + 1); 1 9 10
122 10 w = SvPVX(out);
123 91 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 81 10 91
124 81 unsigned char c = (unsigned char)p[i];
125 81 ▶ if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
Branch: 4 branches
BranchTrueFalseTotal
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 56 25 81
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 54 2 56
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 53 26 79
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') 7 46 53
126 33 ▶ || (c >= '0' && c <= '9') || c == '-' || c == '.'
Branch: 4 branches
BranchTrueFalseTotal
|| (c >= '0' && c <= '9') || c == '-' || c == '.' 18 15 33
|| (c >= '0' && c <= '9') || c == '-' || c == '.' 16 2 18
|| (c >= '0' && c <= '9') || c == '-' || c == '.' 30 1 31
|| (c >= '0' && c <= '9') || c == '-' || c == '.' 27 3 30
127 27 ▶ || c == '_' || c == '~') {
Branch: 2 branches
BranchTrueFalseTotal
|| c == '_' || c == '~') { 26 1 27
|| c == '_' || c == '~') { 1 25 26
128 56 *w++ = (char)c;
129 25 ▶ } else if (c >= 0x80 && !SvUTF8(in)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c >= 0x80 && !SvUTF8(in)) { 6 19 25
} else if (c >= 0x80 && !SvUTF8(in)) { 2 4 6
130 2 unsigned char u1 = (unsigned char)(0xC0 | (c >> 6));
131 2 unsigned char u2 = (unsigned char)(0x80 | (c & 0x3F));
132 2 *w++ = '%'; *w++ = hex[u1 >> 4]; *w++ = hex[u1 & 0xF];
133 2 *w++ = '%'; *w++ = hex[u2 >> 4]; *w++ = hex[u2 & 0xF];
134 } else {
135 23 *w++ = '%';
136 23 *w++ = hex[c >> 4];
137 23 *w++ = hex[c & 0xF];
138 }
139 }
140 10 *w = '\0';
141 10 SvCUR_set(out, (STRLEN)(w - SvPVX(out)));
142 10 SvUTF8_off(out); /* pure ASCII by construction */
143 10 }
144  
145 /* fmt: sprintf with exactly one conversion, the shape fmt_check
146 * (stencil_compile.c) guaranteed at template compile - so this can scan
147 * blind. The format is rewritten on the way through: perl's own length
148 * modifiers (IVdf and friends) are spliced in so the value passed always
149 * matches the conversion's width on every platform; floats go through a
150 * plain double, which is what display formatting wants. */
151 35 void stencil_filt_fmt(pTHX_ SV *in, SV *out, const char *fmt, STRLEN flen,
152 int src_utf8)
153 {
154 char rf[96]; /* rewritten format */
155 char obuf[640]; /* capped by STENCIL_FMT_MAXWID */
156 35 const char *p = fmt, *end = fmt + flen;
157 35 char *w = rf;
158 35 char conv = 's';
159 35 int has_prec = 0;
160 35 int fmt_hi = 0; /* a high-bit byte in the literals */
161 int want_utf8;
162 35 int olen = 0;
163  
164 91 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 56 35 91
165 56 ▶ if (((unsigned char)*p) & 0x80) fmt_hi = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (((unsigned char)*p) & 0x80) fmt_hi = 1; 0 56 56
166 56 ▶ if (*p != '%') { *w++ = *p++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '%') { *w++ = *p++; continue; } 20 36 56
167 36 ▶ if (p + 1 < end && p[1] == '%') { *w++ = '%'; *w++ = '%'; p += 2; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (p + 1 < end && p[1] == '%') { *w++ = '%'; *w++ = '%'; p += 2; continue; } 36 0 36
if (p + 1 < end && p[1] == '%') { *w++ = '%'; *w++ = '%'; p += 2; continue; } 1 35 36
168 35 *w++ = *p++; /* '%' */
169 39 ▶ while (p < end && (*p == '-' || *p == '+' || *p == ' '
Branch: 4 branches
BranchTrueFalseTotal
while (p < end && (*p == '-' || *p == '+' || *p == ' ' 39 0 39
while (p < end && (*p == '-' || *p == '+' || *p == ' ' 1 38 39
while (p < end && (*p == '-' || *p == '+' || *p == ' ' 1 37 38
while (p < end && (*p == '-' || *p == '+' || *p == ' ' 0 37 37
170 37 ▶ || *p == '0' || *p == '#'))
Branch: 2 branches
BranchTrueFalseTotal
|| *p == '0' || *p == '#')) 1 36 37
|| *p == '0' || *p == '#')) 1 35 36
171 4 *w++ = *p++;
172 40 ▶ while (p < end && *p >= '0' && *p <= '9')
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p >= '0' && *p <= '9') 40 0 40
while (p < end && *p >= '0' && *p <= '9') 27 13 40
while (p < end && *p >= '0' && *p <= '9') 5 22 27
173 5 *w++ = *p++;
174 35 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 35 0 35
if (p < end && *p == '.') { 13 22 35
175 13 has_prec = 1;
176 13 *w++ = *p++;
177 26 ▶ while (p < end && *p >= '0' && *p <= '9')
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p >= '0' && *p <= '9') 26 0 26
while (p < end && *p >= '0' && *p <= '9') 26 0 26
while (p < end && *p >= '0' && *p <= '9') 13 13 26
178 13 *w++ = *p++;
179 }
180 35 conv = *p++;
181 35 switch (conv) {
182 8 case 'd': case 'i':
183 24 ▶ { const char *m = IVdf; while (*m) *w++ = *m++; }
Branch: 1 branch
BranchTrueFalseTotal
{ const char *m = IVdf; while (*m) *w++ = *m++; } 16 8 24
184 8 break;
185 1 case 'o':
186 3 ▶ { const char *m = UVof; while (*m) *w++ = *m++; }
Branch: 1 branch
BranchTrueFalseTotal
{ const char *m = UVof; while (*m) *w++ = *m++; } 2 1 3
187 1 break;
188 1 case 'u':
189 3 ▶ { const char *m = UVuf; while (*m) *w++ = *m++; }
Branch: 1 branch
BranchTrueFalseTotal
{ const char *m = UVuf; while (*m) *w++ = *m++; } 2 1 3
190 1 break;
191 2 case 'x':
192 6 ▶ { const char *m = UVxf; while (*m) *w++ = *m++; }
Branch: 1 branch
BranchTrueFalseTotal
{ const char *m = UVxf; while (*m) *w++ = *m++; } 4 2 6
193 2 break;
194 1 case 'X':
195 3 ▶ { const char *m = UVXf; while (*m) *w++ = *m++; }
Branch: 1 branch
BranchTrueFalseTotal
{ const char *m = UVXf; while (*m) *w++ = *m++; } 2 1 3
196 1 break;
197 22 default: /* eEfgGs: as written */
198 22 *w++ = conv;
199 22 break;
200 }
201 }
202 35 *w = '\0';
203  
204 35 ▶ want_utf8 = fmt_hi && src_utf8;
Branch: 2 branches
BranchTrueFalseTotal
want_utf8 = fmt_hi && src_utf8; 0 35 35
want_utf8 = fmt_hi && src_utf8; 0 0 0
205 35 switch (conv) {
206 8 case 'd': case 'i':
207 8 olen = my_snprintf(obuf, sizeof obuf, rf, SvIV(in));
208 8 break;
209 5 case 'o': case 'u': case 'x': case 'X':
210 5 olen = my_snprintf(obuf, sizeof obuf, rf, SvUV(in));
211 5 break;
212 8 case 's': {
213 const char *s;
214 STRLEN sl;
215 8 ▶ if (SvUTF8(in) || want_utf8) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvUTF8(in) || want_utf8) { 6 2 8
if (SvUTF8(in) || want_utf8) { 0 6 6
216 2 s = SvPVutf8(in, sl);
217 2 want_utf8 = 1;
218 }
219 6 else s = SvPV(in, sl);
220 8 olen = my_snprintf(obuf, sizeof obuf, rf, s);
221 8 ▶ if (want_utf8 && has_prec) {
Branch: 2 branches
BranchTrueFalseTotal
if (want_utf8 && has_prec) { 2 6 8
if (want_utf8 && has_prec) { 1 1 2
222 /* a byte-counted precision may have cut the final UTF-8
223 * sequence short; strip a trailing incomplete sequence */
224 1 size_t ol = (size_t)(olen < 0 ? 0 : olen);
225 size_t st;
226 1 ▶ if (ol >= sizeof obuf) ol = strlen(obuf);
Branch: 1 branch
BranchTrueFalseTotal
if (ol >= sizeof obuf) ol = strlen(obuf); 0 1 1
227 1 st = ol;
228 1 ▶ while (st > 0 && (((unsigned char)obuf[st - 1]) & 0xC0) == 0x80)
Branch: 2 branches
BranchTrueFalseTotal
while (st > 0 && (((unsigned char)obuf[st - 1]) & 0xC0) == 0x80) 1 0 1
while (st > 0 && (((unsigned char)obuf[st - 1]) & 0xC0) == 0x80) 0 1 1
229 0 st--;
230 1 ▶ if (st > 0 && (((unsigned char)obuf[st - 1]) & 0x80)
Branch: 2 branches
BranchTrueFalseTotal
if (st > 0 && (((unsigned char)obuf[st - 1]) & 0x80) 1 0 1
if (st > 0 && (((unsigned char)obuf[st - 1]) & 0x80) 1 0 1
231 1 ▶ && (size_t)UTF8SKIP(&obuf[st - 1]) != ol - (st - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
&& (size_t)UTF8SKIP(&obuf[st - 1]) != ol - (st - 1)) { 1 0 1
232 1 obuf[st - 1] = '\0';
233 1 olen = (int)(st - 1);
234 }
235 }
236 8 break;
237 }
238 14 default: /* eEfgG */
239 /* snprintf, not my_snprintf: on a quadmath perl my_snprintf hands any
240 * format that is one bare float spec ("%.2f") to quadmath_snprintf
241 * and reads an __float128 from the varargs, so passing a double there
242 * panics ("quadmath_snprintf failed") or prints rubbish. Splicing in
243 * NVff to match would only move the problem, since the same perl
244 * routes "$%.2Qf" - a spec with literal text around it - to vsnprintf,
245 * which has no Q modifier. A double and a plain format are well
246 * defined everywhere, and display formatting is what this is for. */
247 14 olen = snprintf(obuf, sizeof obuf, rf, (double)SvNV(in));
248 #ifdef _WIN32
249 /* The Windows CRT prints 1.5e+003; make it 1.5e+03 like everyone. */
250 if (olen > 0 && (size_t)olen < sizeof obuf)
251 olen = stencil_expfix(obuf, olen);
252 #endif
253 14 break;
254 }
255  
256 35 ▶ if (olen < 0) olen = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (olen < 0) olen = 0; 0 35 35
257 35 ▶ if ((size_t)olen >= sizeof obuf) olen = (int)(sizeof obuf) - 1;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)olen >= sizeof obuf) olen = (int)(sizeof obuf) - 1; 0 35 35
258 35 sv_setpvn(out, obuf, (STRLEN)olen);
259 35 ▶ if (want_utf8) SvUTF8_on(out);
Branch: 1 branch
BranchTrueFalseTotal
if (want_utf8) SvUTF8_on(out); 2 33 35
260 33 else SvUTF8_off(out);
261 35 }
« src/stencil_compile.c ↑ Summary src/stencil_vm.inc »