XS.xs

filter: Statementstmt 98.6% 1645 / 1667 Branchbran 63.1% 2058 / 3260 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.1% 3703 / 4927 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« eg/streaming.pl ↑ Summary eg/csv_export.pl »
1  
2 #define PERL_NO_GET_CONTEXT
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6  
7 #ifndef av_count
8 #define av_count(av) (av_top_index(av) + 1)
9 #endif
10  
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <time.h>
16 #include <math.h>
17 #include <limits.h>
18 #include <stdarg.h>
19  
20 /* ---- escape/format helpers ---- */
21  
22 /* any byte >= 0x80? word-at-a-time; the mask is 0x80 in every byte of a UV */
23 #define TS_HIGH_MASK ((UV)~(UV)0 / 255 * 128)
24 675581 static int has_high_byte(const char *s, STRLEN len) {
25 675581 const unsigned char *p = (const unsigned char *)s;
26 675581 STRLEN i = 0;
27 2029756 ▶ while (i + sizeof(UV) <= len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i + sizeof(UV) <= len) { 1354176 675580 2029756
28 UV w;
29 1354176 memcpy(&w, p + i, sizeof(UV));
30 1354176 ▶ if (w & TS_HIGH_MASK) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (w & TS_HIGH_MASK) return 1; 1 1354175 1354176
31 1354175 i += sizeof(UV);
32 }
33 4188476 ▶ for (; i < len; i++) if (p[i] & 0x80) return 1;
Branch: 2 branches
BranchTrueFalseTotal
for (; i < len; i++) if (p[i] & 0x80) return 1; 409 3512896 3513305
for (; i < len; i++) if (p[i] & 0x80) return 1; 3513305 675171 4188476
34 675171 return 0;
35 }
36  
37 /* gmtime_r is not everywhere: perl leaves HAS_GMTIME_R undefined on plenty of
38 builds, MSVC has no such function, and under a strict -std= glibc hides it
39 (an error, not a warning, since gcc 14). Plain gmtime() is C89 and perl's
40 reentr.h already redirects it to the reentrant form on threaded builds, so
41 copy out of it straight away. */
42 5 static int ts_gmtime(const time_t *epoch, struct tm *out) {
43 #ifdef HAS_GMTIME_R
44 return gmtime_r(epoch, out) != NULL;
45 #else
46 5 struct tm *r = gmtime(epoch);
47 5 ▶ if (!r) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return 0; 1 4 5
48 4 *out = *r;
49 4 return 1;
50 #endif
51 }
52  
53 /* The digit-takers accept a sign anywhere in the text, not just in front. */
54 1065 PERL_STATIC_INLINE int ts_has_minus(const char *s, STRLEN len) {
55 STRLEN i;
56 4429 ▶ for (i = 0; i < len; i++) if (s[i] == '-') return 1;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < len; i++) if (s[i] == '-') return 1; 21 3364 3385
for (i = 0; i < len; i++) if (s[i] == '-') return 1; 3385 1044 4429
57 1044 return 0;
58 }
59  
60 /* Take a decimal out of text, saturating at the IV range. The magnitude is
61 accumulated in a UV because IV_MIN is one larger in magnitude than IV_MAX:
62 saturating an IV and negating afterwards lands one short of the most
63 negative value. `neg` is decided by the caller, since the transforms differ
64 on whether a '-' counts anywhere or only in front. */
65 1087 static IV ts_scan_iv(const char *s, STRLEN len, int neg) {
66 1087 const UV lim = (UV)IV_MAX + 1;
67 1087 UV uv = 0;
68 STRLEN i;
69 4898 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 3821 1077 4898
70 UV d;
71 3821 ▶ if (s[i] < '0' || s[i] > '9') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] < '0' || s[i] > '9') continue; 3799 22 3821
if (s[i] < '0' || s[i] > '9') continue; 3 3796 3799
72 3796 d = (UV)(s[i] - '0');
73 /* bound against the digit in hand, not a worst-case 9: that saturated
74 the top eight magnitudes although they fit */
75 3796 ▶ if (uv > (lim - d) / 10) { uv = lim; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (uv > (lim - d) / 10) { uv = lim; break; } 10 3786 3796
76 3786 uv = uv * 10 + d;
77 }
78 1087 ▶ if (neg) return (uv >= lim) ? IV_MIN : -(IV)uv;
Branch: 2 branches
BranchTrueFalseTotal
if (neg) return (uv >= lim) ? IV_MIN : -(IV)uv; 24 1063 1087
if (neg) return (uv >= lim) ? IV_MIN : -(IV)uv; 17 7 24
79 1063 ▶ return (uv > (UV)IV_MAX) ? IV_MAX : (IV)uv;
Branch: 1 branch
BranchTrueFalseTotal
return (uv > (UV)IV_MAX) ? IV_MAX : (IV)uv; 1055 8 1063
80 }
81  
82 11314 PERL_STATIC_INLINE int itoa_fast(char *buf, IV val) {
83 char tmp[20];
84 11314 int len = 0, neg = 0;
85 UV uval;
86 11314 ▶ if (val < 0) { neg = 1; uval = -(UV)val; } else { uval = (UV)val; }
Branch: 1 branch
BranchTrueFalseTotal
if (val < 0) { neg = 1; uval = -(UV)val; } else { uval = (UV)val; } 39 11275 11314
87 43413 ▶ do { tmp[len++] = '0' + (uval % 10); uval /= 10; } while (uval);
Branch: 1 branch
BranchTrueFalseTotal
do { tmp[len++] = '0' + (uval % 10); uval /= 10; } while (uval); 32099 11314 43413
88 11314 ▶ if (neg) tmp[len++] = '-';
Branch: 1 branch
BranchTrueFalseTotal
if (neg) tmp[len++] = '-'; 39 11275 11314
89 54766 ▶ for (int i = 0; i < len; i++) buf[i] = tmp[len - 1 - i];
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < len; i++) buf[i] = tmp[len - 1 - i]; 43452 11314 54766
90 11314 return len;
91 }
92  
93 1011 static int itoa_comma(char *buf, IV val) {
94 char digits[20];
95 1011 int dlen = 0, neg = 0;
96 UV uval;
97 1011 ▶ if (val < 0) { neg = 1; uval = -(UV)val; } else { uval = (UV)val; }
Branch: 1 branch
BranchTrueFalseTotal
if (val < 0) { neg = 1; uval = -(UV)val; } else { uval = (UV)val; } 4 1007 1011
98 3002 ▶ do { digits[dlen++] = '0' + (uval % 10); uval /= 10; } while (uval);
Branch: 1 branch
BranchTrueFalseTotal
do { digits[dlen++] = '0' + (uval % 10); uval /= 10; } while (uval); 1991 1011 3002
99 1011 int pos = 0;
100 1011 ▶ if (neg) buf[pos++] = '-';
Branch: 1 branch
BranchTrueFalseTotal
if (neg) buf[pos++] = '-'; 4 1007 1011
101 4013 ▶ for (int i = dlen - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = dlen - 1; i >= 0; i--) { 3002 1011 4013
102 3002 buf[pos++] = digits[i];
103 3002 ▶ if (i > 0 && i % 3 == 0) buf[pos++] = ',';
Branch: 2 branches
BranchTrueFalseTotal
if (i > 0 && i % 3 == 0) buf[pos++] = ','; 1991 1011 3002
if (i > 0 && i % 3 == 0) buf[pos++] = ','; 33 1958 1991
104 }
105 1011 return pos;
106 }
107  
108 /* Which bytes each escaper has to stop on. Compile-time constants: building
109 them at BOOT meant two interpreters could race on the first require, and a
110 reader that saw the "done" flag before the stores landed would escape
111 nothing. There is no initialisation left to get wrong. */
112 static const char html_special[256] = {
113 ['&']=1, ['<']=1, ['>']=1, ['"']=1, ['\'']=1
114 };
115 static const char html_br_special[256] = {
116 ['&']=1, ['<']=1, ['>']=1, ['"']=1, ['\'']=1, ['\n']=1
117 };
118 /* JSON must escape every control character, plus the quote and backslash */
119 static const char json_special[256] = {
120 [0x00]=1, [0x01]=1, [0x02]=1, [0x03]=1, [0x04]=1, [0x05]=1, [0x06]=1, [0x07]=1, [0x08]=1, [0x09]=1, [0x0a]=1, [0x0b]=1, [0x0c]=1, [0x0d]=1, [0x0e]=1, [0x0f]=1, [0x10]=1, [0x11]=1, [0x12]=1, [0x13]=1, [0x14]=1, [0x15]=1, [0x16]=1, [0x17]=1, [0x18]=1, [0x19]=1, [0x1a]=1, [0x1b]=1, [0x1c]=1, [0x1d]=1, [0x1e]=1, [0x1f]=1,
121 ['"']=1, ['\\']=1
122 };
123  
124 28 static STRLEN html_escape(char *dst, const char *src, STRLEN slen) {
125 28 char *out = dst;
126 28 STRLEN i = 0;
127 1084 ▶ while (i < slen) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < slen) { 1064 20 1084
128 /* scan for run of non-special bytes */
129 1064 STRLEN run = i;
130 1351 ▶ while (run < slen && !html_special[(unsigned char)src[run]]) run++;
Branch: 2 branches
BranchTrueFalseTotal
while (run < slen && !html_special[(unsigned char)src[run]]) run++; 1343 8 1351
while (run < slen && !html_special[(unsigned char)src[run]]) run++; 287 1056 1343
131 1064 ▶ if (run > i) {
Branch: 1 branch
BranchTrueFalseTotal
if (run > i) { 46 1018 1064
132 46 memcpy(out, src + i, run - i);
133 46 out += run - i;
134 46 i = run;
135 }
136 1064 ▶ if (i >= slen) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= slen) break; 8 1056 1064
137 1056 switch (src[i]) {
138 1006 case '&': memcpy(out, "&amp;", 5); out += 5; break;
139 21 case '<': memcpy(out, "&lt;", 4); out += 4; break;
140 21 case '>': memcpy(out, "&gt;", 4); out += 4; break;
141 6 case '"': memcpy(out, "&quot;", 6); out += 6; break;
142 2 case '\'': memcpy(out, "&#39;", 5); out += 5; break;
143 }
144 1056 i++;
145 }
146 28 return (STRLEN)(out - dst);
147 }
148  
149 7 static STRLEN html_br_escape(char *dst, const char *src, STRLEN slen) {
150 7 char *out = dst;
151 7 STRLEN i = 0;
152 17 ▶ while (i < slen) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < slen) { 10 7 17
153 10 STRLEN run = i;
154 23 ▶ while (run < slen && !html_br_special[(unsigned char)src[run]]) run++;
Branch: 2 branches
BranchTrueFalseTotal
while (run < slen && !html_br_special[(unsigned char)src[run]]) run++; 23 0 23
while (run < slen && !html_br_special[(unsigned char)src[run]]) run++; 13 10 23
155 10 ▶ if (run > i) {
Branch: 1 branch
BranchTrueFalseTotal
if (run > i) { 3 7 10
156 3 memcpy(out, src + i, run - i);
157 3 out += run - i;
158 3 i = run;
159 }
160 10 ▶ if (i >= slen) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= slen) break; 0 10 10
161 10 switch (src[i]) {
162 1 case '&': memcpy(out, "&amp;", 5); out += 5; break;
163 2 case '<': memcpy(out, "&lt;", 4); out += 4; break;
164 2 case '>': memcpy(out, "&gt;", 4); out += 4; break;
165 1 case '"': memcpy(out, "&quot;", 6); out += 6; break;
166 1 case '\'': memcpy(out, "&#39;", 5); out += 5; break;
167 3 case '\n': memcpy(out, "<br>", 4); out += 4; break;
168 }
169 10 i++;
170 }
171 7 return (STRLEN)(out - dst);
172 }
173  
174 2 static STRLEN url_escape(char *dst, const char *src, STRLEN slen) {
175 static const char hex[] = "0123456789ABCDEF";
176 2 char *out = dst;
177 1021 ▶ for (STRLEN i = 0; i < slen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i < slen; i++) { 1019 2 1021
178 1019 unsigned char c = (unsigned char)src[i];
179 1019 ▶ if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
Branch: 5 branches
BranchTrueFalseTotal
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 16 1003 1019
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 16 0 16
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 16 1003 1019
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 0 16 16
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 1 1002 1003
180 1003 ▶ (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~')
Branch: 5 branches
BranchTrueFalseTotal
(c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~') 1 0 1
(c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~') 1003 0 1003
(c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~') 1003 0 1003
(c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~') 1003 0 1003
(c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~') 0 1003 1003
181 16 *out++ = c;
182 1003 else { *out++ = '%'; *out++ = hex[c >> 4]; *out++ = hex[c & 0xf]; }
183 }
184 2 return (STRLEN)(out - dst);
185 }
186  
187 39 static STRLEN json_escape(char *dst, const char *src, STRLEN slen) {
188 static const char hex[] = "0123456789abcdef";
189 39 char *out = dst;
190 118 ▶ for (STRLEN i = 0; i < slen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i < slen; i++) { 79 39 118
191 79 unsigned char c = (unsigned char)src[i];
192 79 switch (c) {
193 7 case '"': *out++ = '\\'; *out++ = '"'; break;
194 1 case '\\': *out++ = '\\'; *out++ = '\\'; break;
195 1 case '\b': *out++ = '\\'; *out++ = 'b'; break;
196 1 case '\f': *out++ = '\\'; *out++ = 'f'; break;
197 3 case '\n': *out++ = '\\'; *out++ = 'n'; break;
198 1 case '\r': *out++ = '\\'; *out++ = 'r'; break;
199 1 case '\t': *out++ = '\\'; *out++ = 't'; break;
200 64 default:
201 64 ▶ if (c < 0x20) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x20) { 29 35 64
202 29 *out++ = '\\'; *out++ = 'u'; *out++ = '0'; *out++ = '0';
203 29 *out++ = hex[c >> 4]; *out++ = hex[c & 0xf];
204 35 } else *out++ = c;
205 }
206 }
207 39 return (STRLEN)(out - dst);
208 }
209  
210 2 static STRLEN hex_encode(char *dst, const char *src, STRLEN slen) {
211 static const char hex[] = "0123456789abcdef";
212 1005 ▶ for (STRLEN i = 0; i < slen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i < slen; i++) { 1003 2 1005
213 1003 unsigned char c = (unsigned char)src[i];
214 1003 dst[i*2] = hex[c >> 4]; dst[i*2+1] = hex[c & 0xf];
215 }
216 2 return slen * 2;
217 }
218  
219 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
220 static const char b64url[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
221  
222 10 static STRLEN base64_encode_with(char *dst, const unsigned char *src, STRLEN slen, const char *alpha, int pad) {
223 10 char *out = dst;
224 STRLEN i;
225 14 ▶ for (i = 0; i + 2 < slen; i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 2 < slen; i += 3) { 4 10 14
226 4 *out++ = alpha[src[i] >> 2];
227 4 *out++ = alpha[((src[i] & 3) << 4) | (src[i+1] >> 4)];
228 4 *out++ = alpha[((src[i+1] & 0xf) << 2) | (src[i+2] >> 6)];
229 4 *out++ = alpha[src[i+2] & 0x3f];
230 }
231 10 ▶ if (i < slen) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < slen) { 9 1 10
232 9 *out++ = alpha[src[i] >> 2];
233 9 ▶ if (i + 1 < slen) {
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 < slen) { 6 3 9
234 6 *out++ = alpha[((src[i] & 3) << 4) | (src[i+1] >> 4)];
235 6 *out++ = alpha[((src[i+1] & 0xf) << 2)];
236 } else {
237 3 *out++ = alpha[((src[i] & 3) << 4)];
238 3 ▶ if (pad) *out++ = '=';
Branch: 1 branch
BranchTrueFalseTotal
if (pad) *out++ = '='; 2 1 3
239 }
240 9 ▶ if (pad) *out++ = '=';
Branch: 1 branch
BranchTrueFalseTotal
if (pad) *out++ = '='; 6 3 9
241 }
242 10 return (STRLEN)(out - dst);
243 }
244 #define base64_encode(d,s,l) base64_encode_with(d,s,l,b64,1)
245 #define base64url_encode(d,s,l) base64_encode_with(d,s,l,b64url,0)
246  
247 /* ---- compiled template structures ---- */
248  
249 enum xform_type {
250 XF_INT, XF_INT_COMMA, XF_FLOAT, XF_HTML, XF_HTML_BR,
251 XF_RAW, XF_URL, XF_JSON, XF_TRIM, XF_UC, XF_LC,
252 XF_PAD, XF_RPAD, XF_TRUNC, XF_DEFAULT,
253 XF_HEX, XF_BASE64, XF_BASE64URL, XF_COUNT, XF_BOOL, XF_DATE,
254 XF_SPRINTF, XF_REPLACE, XF_SUBSTR, XF_PLURAL,
255 XF_IF, XF_UNLESS, XF_MAP, XF_WRAP,
256 XF_NUMBER_SI, XF_BYTES_SI, XF_ELAPSED, XF_AGO,
257 XF_MASK, XF_COALESCE, XF_LENGTH
258 };
259 enum row_mode { ROW_ARRAY, ROW_HASH };
260  
261 typedef struct {
262 enum xform_type type;
263 int param_int;
264 char *param_str;
265 STRLEN param_str_len;
266 char *param_str2; /* bool falsy, replace new, plural plural-form, wrap suffix */
267 STRLEN param_str2_len;
268 int param_int2; /* substr length, map entry count */
269 /* map entries: stored as parallel arrays of keys and values */
270 char **map_keys; STRLEN *map_key_lens;
271 char **map_vals; STRLEN *map_val_lens;
272 int map_count;
273 } tpl_xform;
274  
275 /* a bare field reference with no transform; a named constant rather than a
276 compound literal, which MSVC only accepted from VS2019 */
277 static const tpl_xform ts_raw_xform =
278 { XF_RAW, 0, NULL, 0, NULL, 0, 0, NULL, NULL, NULL, NULL, 0 };
279  
280 typedef struct {
281 /* static text (if chain is NULL) */
282 char *static_data; STRLEN static_len;
283 /* field ref */
284 int col;
285 char *key; STRLEN key_len; int key_utf8;
286 /* transform chain */
287 tpl_xform *chain;
288 int chain_len;
289 int is_rownum;
290 } tpl_op;
291  
292 typedef struct {
293 char *header; STRLEN header_len;
294 char *footer; STRLEN footer_len;
295 char *sep; STRLEN sep_len;
296 tpl_op *ops; int nops;
297 enum row_mode mode;
298 /* the template really contained a numeric {0} ref, as opposed to mode
299 defaulting to ROW_ARRAY because it contained no field refs at all */
300 int saw_num_ref;
301 SSize_t last_row_count;
302 char escape_char; /* delimiter char, default '{' */
303 char *render_buf; STRLEN render_buf_alloc;
304 /* skip_if / skip_unless */
305 int skip_if_col; char *skip_if_key; STRLEN skip_if_key_len; int has_skip_if; int skip_if_utf8;
306 int skip_unless_col; char *skip_unless_key; STRLEN skip_unless_key_len; int has_skip_unless; int skip_unless_utf8;
307 /* encoding: tpl_* fixed at compile time, r_* per render; see tpl_out_sv.
308 A mid-character slice is noticed where it happens (TS_NOTE_CUT).
309 tpl_utf8 is a bitmask of which pieces arrived flagged, so a flagged
310 separator cannot vouch for a row that came in as bytes. */
311 int tpl_utf8, tpl_high;
312 int r_utf8, r_high, r_skipped;
313 /* owner: the one SV allowed to reach this struct, so a deep copy that
314 duplicates the blessed IV cannot become a second owner and free it
315 twice. in_use/doomed: a DESTROY that lands while a method is running
316 defers the free instead of pulling the struct out from under it. */
317 SV *owner;
318 int in_use, doomed;
319 /* the buffer a render is filling right now; BUF_ENSURE keeps it current so
320 a croak out of user code can free it instead of leaking it */
321 char *inflight;
322 } tpl_compiled;
323  
324 #define TS_TPL_HDR 1
325 #define TS_TPL_ROW 2
326 #define TS_TPL_FTR 4
327 #define TS_TPL_SEP 8
328  
329 static void tpl_free(tpl_compiled *t);
330  
331 /* Our own magic is the proof that a blessed IV really is one of ours: reading
332 an arbitrary integer as a tpl_compiled * segfaults, and a deep copier that
333 duplicates the IV (Clone, and anything else that copies an SV verbatim)
334 would otherwise produce a second owner that frees the same struct. The
335 vtable is all-zero -- we free in DESTROY, not from magic. */
336 static MGVTBL ts_vtbl = { 0, 0, 0, 0, 0, 0, 0, 0 };
337  
338 /* Only a PVMG or richer body has a magic chain; mg_findext does not check, so
339 on anything smaller it walks whatever the union holds instead. A blessed
340 referent is always upgraded by sv_bless, which is why every method call is
341 safe -- but the function-call form, Text::Stencil::render(\"str", ...),
342 hands us an unblessed low-type referent and segfaulted once the heap was
343 warm enough for the garbage to be non-NULL. */
344 #define TS_HAS_OUR_MAGIC(sv) \
345 (SvROK(sv) && SvTYPE(SvRV(sv)) >= SVt_PVMG \
346 ? mg_findext(SvRV(sv), PERL_MAGIC_ext, &ts_vtbl) : NULL)
347  
348 328609 static tpl_compiled *ts_self(pTHX_ SV *sv) {
349 328609 ▶ MAGIC *mg = TS_HAS_OUR_MAGIC(sv);
Branch: 2 branches
BranchTrueFalseTotal
MAGIC *mg = TS_HAS_OUR_MAGIC(sv); 328600 9 328609
MAGIC *mg = TS_HAS_OUR_MAGIC(sv); 328596 4 328600
350 328609 ▶ tpl_compiled *t = mg ? (tpl_compiled *)mg->mg_ptr : NULL;
Branch: 1 branch
BranchTrueFalseTotal
tpl_compiled *t = mg ? (tpl_compiled *)mg->mg_ptr : NULL; 328590 19 328609
351 /* the owner test rejects a copy that carried the magic along with it */
352 328609 ▶ if (!t || t->owner != SvRV(sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!t || t->owner != SvRV(sv)) 328590 19 328609
if (!t || t->owner != SvRV(sv)) 0 328590 328590
353 19 croak("Text::Stencil: not a Text::Stencil object");
354 328590 return t;
355 }
356  
357 /* Frees whatever buffer the render was filling when it croaked. */
358 108533 static void ts_free_inflight(pTHX_ void *p) {
359 108533 tpl_compiled *t = (tpl_compiled *)p;
360 108533 ▶ if (t->inflight) { free(t->inflight); t->inflight = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (t->inflight) { free(t->inflight); t->inflight = NULL; } 26201 82332 108533
361 108533 }
362  
363 /* Released on scope exit, croak included. */
364 108572 static void ts_unuse(pTHX_ void *p) {
365 108572 tpl_compiled *t = (tpl_compiled *)p;
366 108572 ▶ if (--t->in_use <= 0 && t->doomed) tpl_free(t);
Branch: 2 branches
BranchTrueFalseTotal
if (--t->in_use <= 0 && t->doomed) tpl_free(t); 47967 60605 108572
if (--t->in_use <= 0 && t->doomed) tpl_free(t); 2 47965 47967
367 108572 }
368  
369 /* Validate before pinning: a comma expression would run SvRV and the refcount
370 bump first, and on a non-reference invocant -- Text::Stencil::render(undef,
371 ...) or an instance method called on the class -- SvRV hands back the IV or
372 the PV buffer, so we would increment and later decrement whatever that
373 address points at. Pinning the SV is not enough either: an explicit
374 ->DESTROY from inside a callback frees the struct while we hold it, so the
375 struct is marked in use for the duration of the call. */
376 108591 static tpl_compiled *ts_pin_self(pTHX_ SV *sv) {
377 108591 tpl_compiled *t = ts_self(aTHX_ sv);
378 108572 sv_2mortal(SvREFCNT_inc_simple_NN(SvRV(sv)));
379 108572 t->in_use++;
380 108572 SAVEDESTRUCTOR_X(ts_unuse, t);
381 108572 return t;
382 }
383 #define TS_PIN_SELF(sv) ts_pin_self(aTHX_ (sv))
384  
385 /* The rows AV arrives from the typemap with no reference held, and the same
386 mid-render Perl can drop the caller's last one -- then av_fetch reads a
387 freed body. */
388 #define TS_PIN_AV(av) sv_2mortal(SvREFCNT_inc_simple_NN((SV *)(av)))
389  
390 /* Compile errors travel in a caller-supplied buffer: croaking inside the
391 parser would abandon the half-built tpl_compiled. */
392 #define TS_ERRSZ 192
393 #define TS_REFSZ 32
394 /* DBL_MAX at 30 decimals is 341 bytes; sizing short truncated it into a
395 different number */
396 #define TS_FLTSZ 384
397  
398 /* Scratch for handing a field to strtod. At 64 it truncated a longer numeric
399 to 63 digits -- a wrong magnitude, not a rounding nit. A double overflows
400 past ~1.8e308, so 309 integer digits covers everything it can represent.
401 Stack only: no cost on the sort comparator, which runs this per compare. */
402 #define TS_NUMBUF 512
403  
404 113 PERL_STATIC_INLINE double ts_atof(const char *s, STRLEN len) {
405 char b[TS_NUMBUF];
406 113 STRLEN n = len < TS_NUMBUF - 1 ? len : TS_NUMBUF - 1;
407 113 ▶ if (n) memcpy(b, s, n);
Branch: 1 branch
BranchTrueFalseTotal
if (n) memcpy(b, s, n); 113 0 113
408 113 b[n] = 0;
409 113 return atof(b);
410 }
411 /* __attribute__ format so the compiler checks every call site too */
412 #ifdef __GNUC__
413 static void tpl_err(char *err, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
414 #endif
415 52057 static void tpl_err(char *err, const char *fmt, ...) {
416 va_list ap;
417 52057 ▶ if (err[0]) return; /* keep the first error */
Branch: 1 branch
BranchTrueFalseTotal
if (err[0]) return; /* keep the first error */ 0 52057 52057
418 52057 va_start(ap, fmt);
419 52057 vsnprintf(err, TS_ERRSZ, fmt, ap);
420 52057 va_end(ap);
421 /* a truncating vsnprintf does not terminate on every libc */
422 52057 err[TS_ERRSZ - 1] = 0;
423 }
424  
425 /* strict unsigned decimal; TS_NUM_BAD on a non-digit, TS_NUM_BIG if too big */
426 #define TS_NUM_BAD (-1)
427 #define TS_NUM_BIG (-2)
428 #define TS_NUM_MAX 100000000L
429 96 static int parse_uint_param(const char *p, int len) {
430 96 long v = 0;
431 96 ▶ if (len <= 0) return TS_NUM_BAD;
Branch: 1 branch
BranchTrueFalseTotal
if (len <= 0) return TS_NUM_BAD; 0 96 96
432 218 ▶ for (int i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < len; i++) { 136 82 218
433 136 ▶ if (p[i] < '0' || p[i] > '9') return TS_NUM_BAD;
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] < '0' || p[i] > '9') return TS_NUM_BAD; 129 7 136
if (p[i] < '0' || p[i] > '9') return TS_NUM_BAD; 4 125 129
434 125 v = v * 10 + (p[i] - '0');
435 125 ▶ if (v > TS_NUM_MAX) return TS_NUM_BIG;
Branch: 1 branch
BranchTrueFalseTotal
if (v > TS_NUM_MAX) return TS_NUM_BIG; 3 122 125
436 }
437 82 return (int)v;
438 }
439  
440 52697 static void tpl_free(tpl_compiled *t) {
441 52697 ▶ if (t->header) free(t->header);
Branch: 1 branch
BranchTrueFalseTotal
if (t->header) free(t->header); 29 52668 52697
442 52697 ▶ if (t->footer) free(t->footer);
Branch: 1 branch
BranchTrueFalseTotal
if (t->footer) free(t->footer); 22 52675 52697
443 52697 ▶ if (t->sep) free(t->sep);
Branch: 1 branch
BranchTrueFalseTotal
if (t->sep) free(t->sep); 54 52643 52697
444 52697 ▶ if (t->render_buf) free(t->render_buf);
Branch: 1 branch
BranchTrueFalseTotal
if (t->render_buf) free(t->render_buf); 576 52121 52697
445 52697 ▶ if (t->skip_if_key) free(t->skip_if_key);
Branch: 1 branch
BranchTrueFalseTotal
if (t->skip_if_key) free(t->skip_if_key); 6 52691 52697
446 52697 ▶ if (t->skip_unless_key) free(t->skip_unless_key);
Branch: 1 branch
BranchTrueFalseTotal
if (t->skip_unless_key) free(t->skip_unless_key); 5 52692 52697
447 105701 ▶ for (int i = 0; i < t->nops; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < t->nops; i++) { 53004 52697 105701
448 53004 ▶ if (t->ops[i].static_data) free(t->ops[i].static_data);
Branch: 1 branch
BranchTrueFalseTotal
if (t->ops[i].static_data) free(t->ops[i].static_data); 244 52760 53004
449 53004 ▶ if (t->ops[i].key) free(t->ops[i].key);
Branch: 1 branch
BranchTrueFalseTotal
if (t->ops[i].key) free(t->ops[i].key); 101 52903 53004
450 53004 ▶ if (t->ops[i].chain) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->ops[i].chain) { 52756 248 53004
451 157601 ▶ for (int j = 0; j < t->ops[i].chain_len; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int j = 0; j < t->ops[i].chain_len; j++) { 104845 52756 157601
452 104845 tpl_xform *x = &t->ops[i].chain[j];
453 104845 ▶ if (x->param_str) free(x->param_str);
Branch: 1 branch
BranchTrueFalseTotal
if (x->param_str) free(x->param_str); 156 104689 104845
454 104845 ▶ if (x->param_str2) free(x->param_str2);
Branch: 1 branch
BranchTrueFalseTotal
if (x->param_str2) free(x->param_str2); 38 104807 104845
455 /* Only the entry walk needs map_keys. Gating the four frees on
456 it too assumed the group is all-or-nothing: parse_xform
457 allocates them one at a time, so a failure on the FIRST left
458 the other three unreachable. free(NULL) is a no-op. */
459 104845 ▶ if (x->map_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->map_keys) { 11 104834 104845
460 37 ▶ for (int k = 0; k < x->map_count; k++) { free(x->map_keys[k]); free(x->map_vals[k]); }
Branch: 1 branch
BranchTrueFalseTotal
for (int k = 0; k < x->map_count; k++) { free(x->map_keys[k]); free(x->map_vals[k]); } 26 11 37
461 }
462 104845 free(x->map_keys); free(x->map_vals); free(x->map_key_lens); free(x->map_val_lens);
463 }
464 52756 free(t->ops[i].chain);
465 }
466 }
467 52697 ▶ if (t->ops) free(t->ops);
Branch: 1 branch
BranchTrueFalseTotal
if (t->ops) free(t->ops); 52696 1 52697
468 52697 free(t);
469 52697 }
470  
471 /* malloc + copy + NUL, the shape repeated all through the compile path.
472 Returns NULL and sets err on failure; every caller bails on NULL. Unchecked,
473 each of these was a memcpy through NULL, so a failed allocation while
474 compiling was a SIGSEGV. Compiling runs once per object, so the branch is
475 not on any path that matters. */
476 340 static char *ts_dup_n(const char *src, STRLEN n, char *err) {
477 340 char *p = (char *)malloc(n + 1);
478 340 ▶ if (!p) { tpl_err(err, "out of memory"); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!p) { tpl_err(err, "out of memory"); return NULL; } 0 340 340
479 340 ▶ if (n) memcpy(p, src, n);
Branch: 1 branch
BranchTrueFalseTotal
if (n) memcpy(p, src, n); 333 7 340
480 340 p[n] = '\0';
481 340 return p;
482 }
483  
484 /* parse "type" or "type:param" or "type:param1:param2" */
485 104677 static tpl_xform parse_xform(const char *s, int len, char *err, char *soft) {
486 104677 int known = 1; /* cleared below if the name matches no transform */
487 104677 tpl_xform x = {XF_RAW, 0, NULL, 0, NULL, 0, 0, NULL, NULL, NULL, NULL, 0};
488 104677 const char *colon = memchr(s, ':', len);
489 104677 ▶ int tlen = colon ? (int)(colon - s) : len;
Branch: 1 branch
BranchTrueFalseTotal
int tlen = colon ? (int)(colon - s) : len; 268 104409 104677
490 104677 ▶ const char *param = colon ? colon + 1 : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *param = colon ? colon + 1 : NULL; 268 104409 104677
491 104677 ▶ int plen = colon ? len - tlen - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
int plen = colon ? len - tlen - 1 : 0; 268 104409 104677
492  
493 104677 ▶ if (tlen == 3 && memcmp(s, "int", 3) == 0) x.type = XF_INT;
Branch: 2 branches
BranchTrueFalseTotal
if (tlen == 3 && memcmp(s, "int", 3) == 0) x.type = XF_INT; 267 104410 104677
if (tlen == 3 && memcmp(s, "int", 3) == 0) x.type = XF_INT; 111 156 267
494 104566 ▶ else if (tlen == 9 && memcmp(s, "int_comma", 9) == 0) x.type = XF_INT_COMMA;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 9 && memcmp(s, "int_comma", 9) == 0) x.type = XF_INT_COMMA; 23 104543 104566
else if (tlen == 9 && memcmp(s, "int_comma", 9) == 0) x.type = XF_INT_COMMA; 11 12 23
495 104555 ▶ else if (tlen == 5 && memcmp(s, "float", 5) == 0) { x.type = XF_FLOAT; x.param_int = 2; }
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 5 && memcmp(s, "float", 5) == 0) { x.type = XF_FLOAT; x.param_int = 2; } 52068 52487 104555
else if (tlen == 5 && memcmp(s, "float", 5) == 0) { x.type = XF_FLOAT; x.param_int = 2; } 27 52041 52068
496 104528 ▶ else if (tlen == 4 && memcmp(s, "html", 4) == 0) x.type = XF_HTML;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 4 && memcmp(s, "html", 4) == 0) x.type = XF_HTML; 107 104421 104528
else if (tlen == 4 && memcmp(s, "html", 4) == 0) x.type = XF_HTML; 31 76 107
497 104497 ▶ else if (tlen == 7 && memcmp(s, "html_br", 7) == 0) x.type = XF_HTML_BR;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 7 && memcmp(s, "html_br", 7) == 0) x.type = XF_HTML_BR; 114 104383 104497
else if (tlen == 7 && memcmp(s, "html_br", 7) == 0) x.type = XF_HTML_BR; 3 111 114
498 104494 ▶ else if (tlen == 3 && memcmp(s, "raw", 3) == 0) x.type = XF_RAW;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 3 && memcmp(s, "raw", 3) == 0) x.type = XF_RAW; 156 104338 104494
else if (tlen == 3 && memcmp(s, "raw", 3) == 0) x.type = XF_RAW; 120 36 156
499 104374 ▶ else if (tlen == 3 && memcmp(s, "url", 3) == 0) x.type = XF_URL;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 3 && memcmp(s, "url", 3) == 0) x.type = XF_URL; 36 104338 104374
else if (tlen == 3 && memcmp(s, "url", 3) == 0) x.type = XF_URL; 3 33 36
500 104371 ▶ else if (tlen == 4 && memcmp(s, "json", 4) == 0) x.type = XF_JSON;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 4 && memcmp(s, "json", 4) == 0) x.type = XF_JSON; 76 104295 104371
else if (tlen == 4 && memcmp(s, "json", 4) == 0) x.type = XF_JSON; 8 68 76
501 104363 ▶ else if (tlen == 4 && memcmp(s, "trim", 4) == 0) x.type = XF_TRIM;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 4 && memcmp(s, "trim", 4) == 0) x.type = XF_TRIM; 68 104295 104363
else if (tlen == 4 && memcmp(s, "trim", 4) == 0) x.type = XF_TRIM; 19 49 68
502 104344 ▶ else if (tlen == 2 && memcmp(s, "uc", 2) == 0) x.type = XF_UC;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 2 && memcmp(s, "uc", 2) == 0) x.type = XF_UC; 52028 52316 104344
else if (tlen == 2 && memcmp(s, "uc", 2) == 0) x.type = XF_UC; 52014 14 52028
503 52330 ▶ else if (tlen == 2 && memcmp(s, "lc", 2) == 0) x.type = XF_LC;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 2 && memcmp(s, "lc", 2) == 0) x.type = XF_LC; 14 52316 52330
else if (tlen == 2 && memcmp(s, "lc", 2) == 0) x.type = XF_LC; 8 6 14
504 52322 ▶ else if (tlen == 3 && memcmp(s, "pad", 3) == 0) x.type = XF_PAD;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 3 && memcmp(s, "pad", 3) == 0) x.type = XF_PAD; 33 52289 52322
else if (tlen == 3 && memcmp(s, "pad", 3) == 0) x.type = XF_PAD; 13 20 33
505 52309 ▶ else if (tlen == 4 && memcmp(s, "rpad", 4) == 0) x.type = XF_RPAD;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 4 && memcmp(s, "rpad", 4) == 0) x.type = XF_RPAD; 49 52260 52309
else if (tlen == 4 && memcmp(s, "rpad", 4) == 0) x.type = XF_RPAD; 3 46 49
506 52306 ▶ else if (tlen == 5 && memcmp(s, "trunc", 5) == 0) x.type = XF_TRUNC;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 5 && memcmp(s, "trunc", 5) == 0) x.type = XF_TRUNC; 52041 265 52306
else if (tlen == 5 && memcmp(s, "trunc", 5) == 0) x.type = XF_TRUNC; 17 52024 52041
507 52289 ▶ else if (tlen == 7 && memcmp(s, "default", 7) == 0) x.type = XF_DEFAULT;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 7 && memcmp(s, "default", 7) == 0) x.type = XF_DEFAULT; 111 52178 52289
else if (tlen == 7 && memcmp(s, "default", 7) == 0) x.type = XF_DEFAULT; 14 97 111
508 52275 ▶ else if (tlen == 3 && memcmp(s, "hex", 3) == 0) x.type = XF_HEX;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 3 && memcmp(s, "hex", 3) == 0) x.type = XF_HEX; 20 52255 52275
else if (tlen == 3 && memcmp(s, "hex", 3) == 0) x.type = XF_HEX; 4 16 20
509 52271 ▶ else if (tlen == 9 && memcmp(s, "base64url", 9) == 0) x.type = XF_BASE64URL;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 9 && memcmp(s, "base64url", 9) == 0) x.type = XF_BASE64URL; 12 52259 52271
else if (tlen == 9 && memcmp(s, "base64url", 9) == 0) x.type = XF_BASE64URL; 4 8 12
510 52267 ▶ else if (tlen == 6 && memcmp(s, "base64", 6) == 0) x.type = XF_BASE64;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 6 && memcmp(s, "base64", 6) == 0) x.type = XF_BASE64; 41 52226 52267
else if (tlen == 6 && memcmp(s, "base64", 6) == 0) x.type = XF_BASE64; 5 36 41
511 52262 ▶ else if (tlen == 5 && memcmp(s, "count", 5) == 0) x.type = XF_COUNT;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 5 && memcmp(s, "count", 5) == 0) x.type = XF_COUNT; 52024 238 52262
else if (tlen == 5 && memcmp(s, "count", 5) == 0) x.type = XF_COUNT; 52019 5 52024
512 243 ▶ else if (tlen == 4 && memcmp(s, "bool", 4) == 0) x.type = XF_BOOL;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 4 && memcmp(s, "bool", 4) == 0) x.type = XF_BOOL; 46 197 243
else if (tlen == 4 && memcmp(s, "bool", 4) == 0) x.type = XF_BOOL; 9 37 46
513 234 ▶ else if (tlen == 4 && memcmp(s, "date", 4) == 0) x.type = XF_DATE;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 4 && memcmp(s, "date", 4) == 0) x.type = XF_DATE; 37 197 234
else if (tlen == 4 && memcmp(s, "date", 4) == 0) x.type = XF_DATE; 5 32 37
514 229 ▶ else if (tlen == 7 && memcmp(s, "sprintf", 7) == 0) x.type = XF_SPRINTF;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 7 && memcmp(s, "sprintf", 7) == 0) x.type = XF_SPRINTF; 97 132 229
else if (tlen == 7 && memcmp(s, "sprintf", 7) == 0) x.type = XF_SPRINTF; 74 23 97
515 155 ▶ else if (tlen == 7 && memcmp(s, "replace", 7) == 0) x.type = XF_REPLACE;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 7 && memcmp(s, "replace", 7) == 0) x.type = XF_REPLACE; 23 132 155
else if (tlen == 7 && memcmp(s, "replace", 7) == 0) x.type = XF_REPLACE; 11 12 23
516 /* -1 length means "to the end", so a bare "substr" is the whole value */
517 144 ▶ else if (tlen == 6 && memcmp(s, "substr", 6) == 0) { x.type = XF_SUBSTR; x.param_int2 = -1; }
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 6 && memcmp(s, "substr", 6) == 0) { x.type = XF_SUBSTR; x.param_int2 = -1; } 36 108 144
else if (tlen == 6 && memcmp(s, "substr", 6) == 0) { x.type = XF_SUBSTR; x.param_int2 = -1; } 17 19 36
518 127 ▶ else if (tlen == 6 && memcmp(s, "plural", 6) == 0) x.type = XF_PLURAL;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 6 && memcmp(s, "plural", 6) == 0) x.type = XF_PLURAL; 19 108 127
else if (tlen == 6 && memcmp(s, "plural", 6) == 0) x.type = XF_PLURAL; 12 7 19
519 115 ▶ else if (tlen == 2 && memcmp(s, "if", 2) == 0) x.type = XF_IF;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 2 && memcmp(s, "if", 2) == 0) x.type = XF_IF; 6 109 115
else if (tlen == 2 && memcmp(s, "if", 2) == 0) x.type = XF_IF; 6 0 6
520 109 ▶ else if (tlen == 6 && memcmp(s, "unless", 6) == 0) x.type = XF_UNLESS;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 6 && memcmp(s, "unless", 6) == 0) x.type = XF_UNLESS; 7 102 109
else if (tlen == 6 && memcmp(s, "unless", 6) == 0) x.type = XF_UNLESS; 3 4 7
521 106 ▶ else if (tlen == 3 && memcmp(s, "map", 3) == 0) x.type = XF_MAP;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 3 && memcmp(s, "map", 3) == 0) x.type = XF_MAP; 16 90 106
else if (tlen == 3 && memcmp(s, "map", 3) == 0) x.type = XF_MAP; 11 5 16
522 95 ▶ else if (tlen == 4 && memcmp(s, "wrap", 4) == 0) x.type = XF_WRAP;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 4 && memcmp(s, "wrap", 4) == 0) x.type = XF_WRAP; 32 63 95
else if (tlen == 4 && memcmp(s, "wrap", 4) == 0) x.type = XF_WRAP; 12 20 32
523 83 ▶ else if (tlen == 9 && memcmp(s, "number_si", 9) == 0) x.type = XF_NUMBER_SI;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 9 && memcmp(s, "number_si", 9) == 0) x.type = XF_NUMBER_SI; 8 75 83
else if (tlen == 9 && memcmp(s, "number_si", 9) == 0) x.type = XF_NUMBER_SI; 8 0 8
524 75 ▶ else if (tlen == 8 && memcmp(s, "bytes_si", 8) == 0) x.type = XF_BYTES_SI;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 8 && memcmp(s, "bytes_si", 8) == 0) x.type = XF_BYTES_SI; 26 49 75
else if (tlen == 8 && memcmp(s, "bytes_si", 8) == 0) x.type = XF_BYTES_SI; 9 17 26
525 66 ▶ else if (tlen == 7 && memcmp(s, "elapsed", 7) == 0) x.type = XF_ELAPSED;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 7 && memcmp(s, "elapsed", 7) == 0) x.type = XF_ELAPSED; 12 54 66
else if (tlen == 7 && memcmp(s, "elapsed", 7) == 0) x.type = XF_ELAPSED; 11 1 12
526 55 ▶ else if (tlen == 3 && memcmp(s, "ago", 3) == 0) x.type = XF_AGO;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 3 && memcmp(s, "ago", 3) == 0) x.type = XF_AGO; 5 50 55
else if (tlen == 3 && memcmp(s, "ago", 3) == 0) x.type = XF_AGO; 5 0 5
527 50 ▶ else if (tlen == 4 && memcmp(s, "mask", 4) == 0) { x.type = XF_MASK; x.param_int = 4; }
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 4 && memcmp(s, "mask", 4) == 0) { x.type = XF_MASK; x.param_int = 4; } 20 30 50
else if (tlen == 4 && memcmp(s, "mask", 4) == 0) { x.type = XF_MASK; x.param_int = 4; } 17 3 20
528 33 ▶ else if (tlen == 6 && memcmp(s, "length", 6) == 0) x.type = XF_LENGTH;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 6 && memcmp(s, "length", 6) == 0) x.type = XF_LENGTH; 4 29 33
else if (tlen == 6 && memcmp(s, "length", 6) == 0) x.type = XF_LENGTH; 4 0 4
529 29 ▶ else if (tlen == 8 && memcmp(s, "coalesce", 8) == 0) x.type = XF_COALESCE;
Branch: 2 branches
BranchTrueFalseTotal
else if (tlen == 8 && memcmp(s, "coalesce", 8) == 0) x.type = XF_COALESCE; 17 12 29
else if (tlen == 8 && memcmp(s, "coalesce", 8) == 0) x.type = XF_COALESCE; 17 0 17
530 /* An unknown name used to fall through as XF_RAW, so a typo in an escaping
531 transform ("{0:hmtl}") silently emitted the value unescaped. */
532 else {
533 /* Deferred: a stray literal brace (".cls { color: red }") also lands here,
534 and the mixed-mode check downstream explains that far better, so only
535 surface this if nothing more specific goes wrong. */
536 12 known = 0;
537 12 ▶ if (tlen == 0) tpl_err(soft, "empty transform in chain (stray '|' or ':')");
Branch: 1 branch
BranchTrueFalseTotal
if (tlen == 0) tpl_err(soft, "empty transform in chain (stray '|' or ':')"); 3 9 12
538 9 else tpl_err(soft, "unknown transform '%.*s'", tlen > TS_REFSZ ? TS_REFSZ : tlen, s);
539 }
540  
541 104677 ▶ if (param && plen == 0 && x.type == XF_DEFAULT) {
Branch: 3 branches
BranchTrueFalseTotal
if (param && plen == 0 && x.type == XF_DEFAULT) { 268 104409 104677
if (param && plen == 0 && x.type == XF_DEFAULT) { 1 267 268
if (param && plen == 0 && x.type == XF_DEFAULT) { 1 0 1
542 1 x.param_str = ts_dup_n("", 0, err);
543 1 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 1 1
544 1 x.param_str_len = 0;
545 104676 ▶ } else if (param && plen > 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (param && plen > 0) { 267 104409 104676
} else if (param && plen > 0) { 267 0 267
546 267 ▶ if (x.type == XF_FLOAT || x.type == XF_PAD || x.type == XF_RPAD || x.type == XF_TRUNC || x.type == XF_MASK) {
Branch: 5 branches
BranchTrueFalseTotal
if (x.type == XF_FLOAT || x.type == XF_PAD || x.type == XF_RPAD || x.type == XF_TRUNC || x.type == XF_MASK) { 242 25 267
if (x.type == XF_FLOAT || x.type == XF_PAD || x.type == XF_RPAD || x.type == XF_TRUNC || x.type == XF_MASK) { 229 13 242
if (x.type == XF_FLOAT || x.type == XF_PAD || x.type == XF_RPAD || x.type == XF_TRUNC || x.type == XF_MASK) { 226 3 229
if (x.type == XF_FLOAT || x.type == XF_PAD || x.type == XF_RPAD || x.type == XF_TRUNC || x.type == XF_MASK) { 209 17 226
if (x.type == XF_FLOAT || x.type == XF_PAD || x.type == XF_RPAD || x.type == XF_TRUNC || x.type == XF_MASK) { 15 194 209
547 73 int v = parse_uint_param(param, plen);
548 73 ▶ if (v == TS_NUM_BAD) {
Branch: 1 branch
BranchTrueFalseTotal
if (v == TS_NUM_BAD) { 8 65 73
549 8 tpl_err(err, "'%.*s' takes a non-negative integer, got '%.*s'", tlen, s, plen, param);
550 8 return x;
551 }
552 65 ▶ if (v == TS_NUM_BIG) {
Branch: 1 branch
BranchTrueFalseTotal
if (v == TS_NUM_BIG) { 1 64 65
553 1 tpl_err(err, "'%.*s' parameter is too large (max %ld)", tlen, s, TS_NUM_MAX);
554 1 return x;
555 }
556 /* an absurd precision only makes snprintf build a string to truncate */
557 64 ▶ if (x.type == XF_FLOAT && v > 30) v = 30;
Branch: 2 branches
BranchTrueFalseTotal
if (x.type == XF_FLOAT && v > 30) v = 30; 24 40 64
if (x.type == XF_FLOAT && v > 30) v = 30; 0 24 24
558 64 x.param_int = v;
559 194 ▶ } else if (x.type == XF_SPRINTF) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (x.type == XF_SPRINTF) { 73 121 194
560 73 x.param_str = ts_dup_n(param, plen, err);
561 73 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 73 73
562 73 x.param_str_len = plen;
563 121 ▶ } else if (x.type == XF_REPLACE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (x.type == XF_REPLACE) { 11 110 121
564 11 const char *c2 = memchr(param, ':', plen);
565 /* "replace:OLD" deletes OLD; it used to set no needle and no-op */
566 11 ▶ if (!c2) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c2) { 1 10 11
567 1 x.param_str = ts_dup_n(param, plen, err);
568 1 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 1 1
569 1 x.param_str_len = plen;
570 1 x.param_str2 = ts_dup_n("", 0, err);
571 1 ▶ if (!x.param_str2) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str2) return x; 0 1 1
572 1 x.param_str2_len = 0;
573 } else {
574 10 int p1len = (int)(c2 - param);
575 10 int p2len = plen - p1len - 1;
576 10 ▶ if (p1len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p1len > 0) { 9 1 10
577 9 x.param_str = ts_dup_n(param, p1len, err);
578 9 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 9 9
579 9 x.param_str_len = p1len;
580 9 x.param_str2 = ts_dup_n(c2 + 1, p2len, err);
581 9 ▶ if (!x.param_str2) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str2) return x; 0 9 9
582 9 x.param_str2_len = p2len;
583 }
584 }
585 110 ▶ } else if (x.type == XF_SUBSTR) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (x.type == XF_SUBSTR) { 16 94 110
586 16 const char *c2 = memchr(param, ':', plen);
587 16 ▶ int p1len = c2 ? (int)(c2 - param) : plen;
Branch: 1 branch
BranchTrueFalseTotal
int p1len = c2 ? (int)(c2 - param) : plen; 9 7 16
588 16 int v = 0;
589 16 ▶ if (p1len > 0) { /* "substr::N" means offset 0 */
Branch: 1 branch
BranchTrueFalseTotal
if (p1len > 0) { /* "substr::N" means offset 0 */ 14 2 16
590 14 v = parse_uint_param(param, p1len);
591 14 ▶ if (v == TS_NUM_BIG) {
Branch: 1 branch
BranchTrueFalseTotal
if (v == TS_NUM_BIG) { 1 13 14
592 1 tpl_err(err, "'substr' offset is too large (max %ld)", TS_NUM_MAX);
593 1 return x;
594 }
595 13 ▶ if (v < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) { 2 11 13
596 2 tpl_err(err, "'substr' offset must be a non-negative integer, got '%.*s'", p1len, param);
597 2 return x;
598 }
599 }
600 13 x.param_int = v;
601 13 x.param_int2 = -1;
602 13 ▶ if (c2) {
Branch: 1 branch
BranchTrueFalseTotal
if (c2) { 9 4 13
603 9 int p2len = plen - p1len - 1;
604 9 ▶ if (p2len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p2len > 0) { 9 0 9
605 9 v = parse_uint_param(c2 + 1, p2len);
606 9 ▶ if (v == TS_NUM_BIG) {
Branch: 1 branch
BranchTrueFalseTotal
if (v == TS_NUM_BIG) { 1 8 9
607 1 tpl_err(err, "'substr' length is too large (max %ld)", TS_NUM_MAX);
608 1 return x;
609 }
610 8 ▶ if (v < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) { 1 7 8
611 1 tpl_err(err, "'substr' length must be a non-negative integer, got '%.*s'", p2len, c2 + 1);
612 1 return x;
613 }
614 7 x.param_int2 = v;
615 }
616 }
617 94 ▶ } else if (x.type == XF_PLURAL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (x.type == XF_PLURAL) { 12 82 94
618 12 const char *c2 = memchr(param, ':', plen);
619 12 ▶ int p1len = c2 ? (int)(c2 - param) : plen;
Branch: 1 branch
BranchTrueFalseTotal
int p1len = c2 ? (int)(c2 - param) : plen; 5 7 12
620 12 ▶ int p2len = c2 ? plen - p1len - 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
int p2len = c2 ? plen - p1len - 1 : -1; 5 7 12
621 12 x.param_str = ts_dup_n(param, p1len, err);
622 12 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 12 12
623 12 x.param_str_len = p1len;
624 12 ▶ if (c2) {
Branch: 1 branch
BranchTrueFalseTotal
if (c2) { 5 7 12
625 5 x.param_str2 = ts_dup_n(c2 + 1, p2len, err);
626 5 ▶ if (!x.param_str2) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str2) return x; 0 5 5
627 5 x.param_str2_len = p2len;
628 } else {
629 /* one form given: plural is that form plus "s" */
630 7 x.param_str2 = (char *)malloc(p1len + 2);
631 7 ▶ if (!x.param_str2) { tpl_err(err, "out of memory"); return x; }
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str2) { tpl_err(err, "out of memory"); return x; } 0 7 7
632 7 memcpy(x.param_str2, param, p1len);
633 7 x.param_str2[p1len] = 's';
634 7 x.param_str2[p1len + 1] = '\0';
635 7 x.param_str2_len = p1len + 1;
636 }
637 82 ▶ } else if (x.type == XF_IF || x.type == XF_UNLESS) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (x.type == XF_IF || x.type == XF_UNLESS) { 76 6 82
} else if (x.type == XF_IF || x.type == XF_UNLESS) { 3 73 76
638 9 x.param_str = ts_dup_n(param, plen, err);
639 9 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 9 9
640 9 x.param_str_len = plen;
641 73 ▶ } else if (x.type == XF_WRAP) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (x.type == XF_WRAP) { 11 62 73
642 11 const char *c2 = memchr(param, ':', plen);
643 11 ▶ if (c2) {
Branch: 1 branch
BranchTrueFalseTotal
if (c2) { 9 2 11
644 9 int p1len = (int)(c2 - param);
645 9 int p2len = plen - p1len - 1;
646 9 x.param_str = ts_dup_n(param, p1len, err);
647 9 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 9 9
648 9 x.param_str_len = p1len;
649 9 x.param_str2 = ts_dup_n(c2 + 1, p2len, err);
650 9 ▶ if (!x.param_str2) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str2) return x; 0 9 9
651 9 x.param_str2_len = p2len;
652 } else {
653 2 x.param_str = ts_dup_n(param, plen, err);
654 2 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 2 2
655 2 x.param_str_len = plen;
656 }
657 62 ▶ } else if (x.type == XF_MAP) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (x.type == XF_MAP) { 11 51 62
658 11 int cnt = 1;
659 186 ▶ for (int i = 0; i < plen; i++) if (param[i] == ':') cnt++;
Branch: 2 branches
BranchTrueFalseTotal
for (int i = 0; i < plen; i++) if (param[i] == ':') cnt++; 15 160 175
for (int i = 0; i < plen; i++) if (param[i] == ':') cnt++; 175 11 186
660 11 x.map_keys = (char **)calloc(cnt, sizeof(char *));
661 11 x.map_vals = (char **)calloc(cnt, sizeof(char *));
662 11 x.map_key_lens = (STRLEN *)calloc(cnt, sizeof(STRLEN));
663 11 x.map_val_lens = (STRLEN *)calloc(cnt, sizeof(STRLEN));
664 /* all four or none: the loop below indexes them in lockstep, and
665 tpl_free only walks them when map_count says there is something
666 to walk, so leaving map_count at 0 here is safe to free */
667 11 ▶ if (!x.map_keys || !x.map_vals || !x.map_key_lens || !x.map_val_lens) {
Branch: 4 branches
BranchTrueFalseTotal
if (!x.map_keys || !x.map_vals || !x.map_key_lens || !x.map_val_lens) { 11 0 11
if (!x.map_keys || !x.map_vals || !x.map_key_lens || !x.map_val_lens) { 11 0 11
if (!x.map_keys || !x.map_vals || !x.map_key_lens || !x.map_val_lens) { 11 0 11
if (!x.map_keys || !x.map_vals || !x.map_key_lens || !x.map_val_lens) { 0 11 11
668 0 tpl_err(err, "out of memory");
669 0 return x;
670 }
671 11 x.map_count = 0;
672 11 const char *p2 = param, *pe = param + plen;
673 37 ▶ while (p2 < pe) {
Branch: 1 branch
BranchTrueFalseTotal
while (p2 < pe) { 26 11 37
674 26 const char *next = memchr(p2, ':', pe - p2);
675 26 ▶ if (!next) next = pe;
Branch: 1 branch
BranchTrueFalseTotal
if (!next) next = pe; 11 15 26
676 26 const char *eq = memchr(p2, '=', next - p2);
677 26 ▶ if (eq) {
Branch: 1 branch
BranchTrueFalseTotal
if (eq) { 26 0 26
678 26 int kl = (int)(eq - p2), vl = (int)(next - eq - 1);
679 26 int idx = x.map_count;
680 26 x.map_keys[idx] = ts_dup_n(p2, kl, err);
681 26 ▶ if (!x.map_keys[idx]) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.map_keys[idx]) return x; 0 26 26
682 26 x.map_key_lens[idx] = kl;
683 26 x.map_vals[idx] = ts_dup_n(eq + 1, vl, err);
684 26 ▶ if (!x.map_vals[idx]) { free(x.map_keys[idx]); x.map_keys[idx] = NULL; return x; }
Branch: 1 branch
BranchTrueFalseTotal
if (!x.map_vals[idx]) { free(x.map_keys[idx]); x.map_keys[idx] = NULL; return x; } 0 26 26
685 26 x.map_val_lens[idx] = vl;
686 /* bumped only once the pair is complete, so a half-built
687 entry is never handed to tpl_free */
688 26 x.map_count++;
689 }
690 26 p2 = next + 1;
691 }
692 51 ▶ } else if (x.type == XF_COALESCE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (x.type == XF_COALESCE) { 17 34 51
693 17 x.param_str = ts_dup_n(param, plen, err);
694 17 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 17 17
695 17 x.param_str_len = plen;
696 34 ▶ } else if (x.type == XF_DEFAULT || x.type == XF_DATE) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (x.type == XF_DEFAULT || x.type == XF_DATE) { 21 13 34
} else if (x.type == XF_DEFAULT || x.type == XF_DATE) { 3 18 21
697 16 x.param_str = ts_dup_n(param, plen, err);
698 16 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 16 16
699 16 x.param_str_len = plen;
700 18 ▶ } else if (x.type == XF_BOOL) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (x.type == XF_BOOL) { 7 11 18
701 7 const char *c2 = memchr(param, ':', plen);
702 7 ▶ if (c2) {
Branch: 1 branch
BranchTrueFalseTotal
if (c2) { 7 0 7
703 7 int p1len = (int)(c2 - param);
704 7 int p2len = plen - p1len - 1;
705 7 x.param_str = ts_dup_n(param, p1len, err);
706 7 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 7 7
707 7 x.param_str_len = p1len;
708 7 x.param_str2 = ts_dup_n(c2 + 1, p2len, err);
709 7 ▶ if (!x.param_str2) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str2) return x; 0 7 7
710 7 x.param_str2_len = p2len;
711 } else {
712 0 x.param_str = ts_dup_n(param, plen, err);
713 0 ▶ if (!x.param_str) return x;
Branch: 1 branch
BranchTrueFalseTotal
if (!x.param_str) return x; 0 0 0
714 0 x.param_str_len = plen;
715 }
716 }
717 /* Nothing above claimed the parameter, so this transform does not take
718 one and it was about to be dropped on the floor -- the same silent
719 no-op an unknown transform name and an unknown option are rejected
720 for. Only for a name we recognise, though: an unknown one already has
721 a better message waiting in `soft`, and a hard error here would
722 preempt it and blame a transform that does not exist. */
723 11 ▶ else if (known) {
Branch: 1 branch
BranchTrueFalseTotal
else if (known) { 9 2 11
724 9 tpl_err(err, "'%.*s' takes no parameter, got '%.*s'",
725 tlen, s, plen, param);
726 9 return x;
727 }
728 }
729 104654 return x;
730 }
731  
732 /* Parse "{field:type1:p1|type2:p2}" into chain */
733 52760 static void parse_field_spec(const char *spec, int spec_len,
734 tpl_op *op, enum row_mode *mode,
735 char *num_ref, char *key_ref, char *err, char *soft_err,
736 int tpl_key_utf8) {
737 /* split field name from transform chain at first : or | */
738 52760 const char *sep = NULL;
739 105742 ▶ for (int i = 0; i < spec_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < spec_len; i++) { 105571 171 105742
740 105571 ▶ if (spec[i] == ':' || spec[i] == '|') { sep = spec + i; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (spec[i] == ':' || spec[i] == '|') { sep = spec + i; break; } 52983 52588 105571
if (spec[i] == ':' || spec[i] == '|') { sep = spec + i; break; } 1 52982 52983
741 }
742  
743 52760 const char *field = spec;
744 52760 ▶ int field_len = sep ? (int)(sep - spec) : spec_len;
Branch: 1 branch
BranchTrueFalseTotal
int field_len = sep ? (int)(sep - spec) : spec_len; 52589 171 52760
745  
746 /* check for row number placeholder {#} */
747 52760 ▶ if (field_len == 1 && field[0] == '#') {
Branch: 2 branches
BranchTrueFalseTotal
if (field_len == 1 && field[0] == '#') { 52699 61 52760
if (field_len == 1 && field[0] == '#') { 8 52691 52699
748 8 op->is_rownum = 1;
749 /* parse transform chain if any */
750 8 ▶ if (!sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sep) { 4 4 8
751 4 op->chain = (tpl_xform *)malloc(sizeof(tpl_xform));
752 4 ▶ if (!op->chain) { tpl_err(err, "out of memory"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!op->chain) { tpl_err(err, "out of memory"); return; } 0 4 4
753 4 op->chain[0] = ts_raw_xform;
754 4 op->chain_len = 1;
755 4 return;
756 }
757 4 goto parse_chain;
758 }
759  
760 /* an empty name would otherwise become a "" hash key */
761 52752 ▶ if (field_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (field_len == 0) { 1 52751 52752
762 1 tpl_err(err, "empty field reference in template");
763 1 return;
764 }
765  
766 /* detect array vs hash mode */
767 52751 int is_num = 1, is_neg = 0, start_idx = 0;
768 52751 ▶ if (field[0] == '-') { is_neg = 1; start_idx = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (field[0] == '-') { is_neg = 1; start_idx = 1; } 7 52744 52751
769 105455 ▶ for (int i = start_idx; i < field_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (int i = start_idx; i < field_len; i++) 52805 52650 105455
770 52805 ▶ if (field[i] < '0' || field[i] > '9') { is_num = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (field[i] < '0' || field[i] > '9') { is_num = 0; break; } 52802 3 52805
if (field[i] < '0' || field[i] > '9') { is_num = 0; break; } 98 52704 52802
771  
772 105398 ▶ if (is_num && field_len > start_idx) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_num && field_len > start_idx) { 52650 101 52751
if (is_num && field_len > start_idx) { 52650 0 52650
773 /* Check before multiplying, and in int: testing `long col > INT_MAX`
774 afterwards never fires where long is 32 bits (ILP32, and Win64's
775 LLP64), so the accumulation just wrapped to a wrong column. */
776 /* Accumulate the magnitude unsigned: a negative index reaches one
777 further than a positive one, and bounding both at INT_MAX rejected
778 exactly INT_MIN -- which skip_if and the sort spec accept, so the
779 same index was valid in one place and not another. */
780 52650 ▶ unsigned lim = (unsigned)INT_MAX + (is_neg ? 1u : 0u);
Branch: 1 branch
BranchTrueFalseTotal
unsigned lim = (unsigned)INT_MAX + (is_neg ? 1u : 0u); 7 52643 52650
781 52650 unsigned col = 0;
782 105351 ▶ for (int i = start_idx; i < field_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = start_idx; i < field_len; i++) { 52704 52647 105351
783 52704 unsigned d = (unsigned)(field[i] - '0');
784 52704 ▶ if (col > (lim - d) / 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (col > (lim - d) / 10) { 3 52701 52704
785 3 tpl_err(err, "column index '%.*s' is out of range", field_len, field);
786 3 return;
787 }
788 52701 col = col * 10 + d;
789 }
790 /* negate as unsigned, then convert: -(int)2147483648 would be UB */
791 52647 ▶ op->col = is_neg ? (int)(0u - col) : (int)col;
Branch: 1 branch
BranchTrueFalseTotal
op->col = is_neg ? (int)(0u - col) : (int)col; 6 52641 52647
792 52647 ▶ if (!num_ref[0]) snprintf(num_ref, TS_REFSZ, "%.*s", field_len, field);
Branch: 1 branch
BranchTrueFalseTotal
if (!num_ref[0]) snprintf(num_ref, TS_REFSZ, "%.*s", field_len, field); 52600 47 52647
793 } else {
794 101 *mode = ROW_HASH;
795 101 ▶ if (!key_ref[0]) snprintf(key_ref, TS_REFSZ, "%.*s", field_len, field);
Branch: 1 branch
BranchTrueFalseTotal
if (!key_ref[0]) snprintf(key_ref, TS_REFSZ, "%.*s", field_len, field); 83 18 101
796 101 op->key = ts_dup_n(field, field_len, err);
797 101 ▶ if (!op->key) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!op->key) return; 0 101 101
798 101 op->key_len = field_len;
799 101 op->key_utf8 = tpl_key_utf8;
800 }
801  
802 /* parse transform chain */
803 52748 ▶ if (!sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sep) { 164 52584 52748
804 164 op->chain = (tpl_xform *)malloc(sizeof(tpl_xform));
805 164 ▶ if (!op->chain) { tpl_err(err, "out of memory"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!op->chain) { tpl_err(err, "out of memory"); return; } 0 164 164
806 164 op->chain[0] = ts_raw_xform;
807 164 op->chain_len = 1;
808 164 return;
809 }
810  
811 52584 parse_chain:;
812 52588 const char *xforms_start = spec + field_len;
813 52588 ▶ if (*xforms_start == ':' || *xforms_start == '|') xforms_start++;
Branch: 2 branches
BranchTrueFalseTotal
if (*xforms_start == ':' || *xforms_start == '|') xforms_start++; 1 52587 52588
if (*xforms_start == ':' || *xforms_start == '|') xforms_start++; 1 0 1
814 52588 int xforms_len = spec_len - (int)(xforms_start - spec);
815  
816 /* count pipes to size the chain */
817 52588 int nxforms = 1;
818 473450 ▶ for (int i = 0; i < xforms_len; i++) if (xforms_start[i] == '|') nxforms++;
Branch: 2 branches
BranchTrueFalseTotal
for (int i = 0; i < xforms_len; i++) if (xforms_start[i] == '|') nxforms++; 52093 368769 420862
for (int i = 0; i < xforms_len; i++) if (xforms_start[i] == '|') nxforms++; 420862 52588 473450
819  
820 52588 op->chain = (tpl_xform *)malloc(nxforms * sizeof(tpl_xform));
821 52588 ▶ if (!op->chain) { tpl_err(err, "out of memory"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!op->chain) { tpl_err(err, "out of memory"); return; } 0 52588 52588
822 52588 op->chain_len = 0;
823  
824 52588 const char *p = xforms_start;
825 52588 const char *xend = xforms_start + xforms_len;
826 157242 ▶ while (p < xend) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < xend) { 104677 52565 157242
827 104677 const char *pipe = memchr(p, '|', xend - p);
828 104677 ▶ if (!pipe) pipe = xend;
Branch: 1 branch
BranchTrueFalseTotal
if (!pipe) pipe = xend; 52584 52093 104677
829 /* stored even on error so tpl_free reclaims it */
830 104677 op->chain[op->chain_len++] = parse_xform(p, (int)(pipe - p), err, soft_err);
831 104677 ▶ if (err[0]) return;
Branch: 1 branch
BranchTrueFalseTotal
if (err[0]) return; 23 104654 104677
832 104654 p = pipe + 1;
833 }
834  
835 /* count/coalesce operate on the raw field value (array/hash size, or the
836 first truthy field), so they are only meaningful as the first transform.
837 Mid-chain they would silently emit "0"/nothing; reject at compile time. */
838 52654 ▶ for (int i = 1; i < op->chain_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 1; i < op->chain_len; i++) { 52091 563 52654
839 52091 ▶ if (op->chain[i].type == XF_COUNT) {
Branch: 1 branch
BranchTrueFalseTotal
if (op->chain[i].type == XF_COUNT) { 52001 90 52091
840 52001 tpl_err(err, "'count' must be the first transform in a chain");
841 52001 return;
842 }
843 90 ▶ if (op->chain[i].type == XF_COALESCE) {
Branch: 1 branch
BranchTrueFalseTotal
if (op->chain[i].type == XF_COALESCE) { 1 89 90
844 1 tpl_err(err, "'coalesce' must be the first transform in a chain");
845 1 return;
846 }
847 }
848 }
849  
850 52697 static tpl_compiled *tpl_compile(pTHX_ const char *header, STRLEN hlen,
851 const char *row, STRLEN rlen,
852 const char *footer, STRLEN flen,
853 const char *sep, STRLEN slen,
854 char esc_char, int tpl_utf8, char *err) {
855 52697 tpl_compiled *t = (tpl_compiled *)calloc(1, sizeof(tpl_compiled));
856 52697 ▶ if (!t) { tpl_err(err, "out of memory"); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!t) { tpl_err(err, "out of memory"); return NULL; } 0 52697 52697
857 52697 char soft_err[TS_ERRSZ]; soft_err[0] = '\0';
858 /* these three are length-counted, not NUL-terminated, so they allocate
859 exactly hlen/flen/slen rather than going through ts_dup_n */
860 52697 ▶ if (hlen) { t->header = (char *)malloc(hlen);
Branch: 1 branch
BranchTrueFalseTotal
if (hlen) { t->header = (char *)malloc(hlen); 29 52668 52697
861 29 ▶ if (!t->header) { tpl_err(err, "out of memory"); return t; }
Branch: 1 branch
BranchTrueFalseTotal
if (!t->header) { tpl_err(err, "out of memory"); return t; } 0 29 29
862 29 memcpy(t->header, header, hlen); } t->header_len = hlen;
863 52697 ▶ if (flen) { t->footer = (char *)malloc(flen);
Branch: 1 branch
BranchTrueFalseTotal
if (flen) { t->footer = (char *)malloc(flen); 22 52675 52697
864 22 ▶ if (!t->footer) { tpl_err(err, "out of memory"); return t; }
Branch: 1 branch
BranchTrueFalseTotal
if (!t->footer) { tpl_err(err, "out of memory"); return t; } 0 22 22
865 22 memcpy(t->footer, footer, flen); } t->footer_len = flen;
866 52697 ▶ if (slen) { t->sep = (char *)malloc(slen);
Branch: 1 branch
BranchTrueFalseTotal
if (slen) { t->sep = (char *)malloc(slen); 54 52643 52697
867 54 ▶ if (!t->sep) { tpl_err(err, "out of memory"); return t; }
Branch: 1 branch
BranchTrueFalseTotal
if (!t->sep) { tpl_err(err, "out of memory"); return t; } 0 54 54
868 54 memcpy(t->sep, sep, slen); t->sep_len = slen; }
869 52697 t->mode = ROW_ARRAY;
870 52697 ▶ t->escape_char = esc_char ? esc_char : '{';
Branch: 1 branch
BranchTrueFalseTotal
t->escape_char = esc_char ? esc_char : '{'; 19 52678 52697
871  
872 /* The row is split on raw bytes, so a delimiter above 0x7F cuts inside a
873 multi-byte character of a decoded template -- from_file always decodes --
874 and every piece stays flagged: render() and columns() then return SvUTF8
875 strings whose bytes are not UTF-8, and uc() on them dies. Flagging the
876 output as bytes would still leave columns() broken, so refuse instead.
877 Byte templates are unaffected. */
878 52697 ▶ if ((unsigned char)t->escape_char >= 0x80 && (tpl_utf8 & TS_TPL_ROW)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((unsigned char)t->escape_char >= 0x80 && (tpl_utf8 & TS_TPL_ROW)) { 3 52694 52697
if ((unsigned char)t->escape_char >= 0x80 && (tpl_utf8 & TS_TPL_ROW)) { 1 2 3
879 1 tpl_err(err, "escape_char must be an ASCII byte when the row template "
880 "is a character string");
881 1 return t;
882 }
883  
884 52696 int cap = 16;
885 52696 t->ops = (tpl_op *)calloc(cap, sizeof(tpl_op));
886 52696 ▶ if (!t->ops) { tpl_err(err, "out of memory"); return t; }
Branch: 1 branch
BranchTrueFalseTotal
if (!t->ops) { tpl_err(err, "out of memory"); return t; } 0 52696 52696
887 52696 t->nops = 0;
888  
889 52696 ▶ char close_char = (t->escape_char == '{') ? '}' : t->escape_char;
Branch: 1 branch
BranchTrueFalseTotal
char close_char = (t->escape_char == '{') ? '}' : t->escape_char; 9 52687 52696
890 52696 ▶ if (t->escape_char == '[') close_char = ']';
Branch: 1 branch
BranchTrueFalseTotal
if (t->escape_char == '[') close_char = ']'; 5 52691 52696
891 52696 ▶ if (t->escape_char == '(') close_char = ')';
Branch: 1 branch
BranchTrueFalseTotal
if (t->escape_char == '(') close_char = ')'; 0 52696 52696
892 52696 ▶ if (t->escape_char == '<') close_char = '>';
Branch: 1 branch
BranchTrueFalseTotal
if (t->escape_char == '<') close_char = '>'; 1 52695 52696
893  
894 /* on failure t still owns everything built so far; the caller frees it */
895 #define GROW_OPS() do { \
896 if (t->nops >= cap) { \
897 tpl_op *no = (tpl_op *)realloc(t->ops, (size_t)cap * 2 * sizeof(tpl_op)); \
898 if (!no) { tpl_err(err, "out of memory"); return t; } \
899 t->ops = no; memset(&t->ops[cap], 0, (size_t)cap * sizeof(tpl_op)); cap *= 2; \
900 } \
901 } while(0)
902  
903 52696 char num_ref[TS_REFSZ] = "", key_ref[TS_REFSZ] = "";
904 52696 const char *p = row, *end = row + rlen;
905 53432 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 52886 546 53432
906 52886 const char *brace = memchr(p, t->escape_char, end - p);
907 52886 ▶ if (!brace) brace = end;
Branch: 1 branch
BranchTrueFalseTotal
if (!brace) brace = end; 119 52767 52886
908 52886 ▶ if (brace > p) {
Branch: 1 branch
BranchTrueFalseTotal
if (brace > p) { 239 52647 52886
909 239 ▶ GROW_OPS();
Branch: 2 branches
BranchTrueFalseTotal
GROW_OPS(); 0 239 239
GROW_OPS(); 0 0 0
910 239 tpl_op *op = &t->ops[t->nops++];
911 239 memset(op, 0, sizeof(*op));
912 239 op->static_data = (char *)malloc(brace - p);
913 239 ▶ if (!op->static_data) { tpl_err(err, "out of memory"); return t; }
Branch: 1 branch
BranchTrueFalseTotal
if (!op->static_data) { tpl_err(err, "out of memory"); return t; } 0 239 239
914 239 memcpy(op->static_data, p, brace - p);
915 239 op->static_len = brace - p;
916 }
917 52886 ▶ if (brace >= end) break;
Branch: 1 branch
BranchTrueFalseTotal
if (brace >= end) break; 119 52767 52886
918  
919 /* doubled escape char (e.g. {{ ) = literal */
920 52767 ▶ if (brace + 1 < end && brace[1] == t->escape_char) {
Branch: 2 branches
BranchTrueFalseTotal
if (brace + 1 < end && brace[1] == t->escape_char) { 52766 1 52767
if (brace + 1 < end && brace[1] == t->escape_char) { 5 52761 52766
921 5 ▶ GROW_OPS();
Branch: 2 branches
BranchTrueFalseTotal
GROW_OPS(); 0 5 5
GROW_OPS(); 0 0 0
922 5 tpl_op *op = &t->ops[t->nops++];
923 5 memset(op, 0, sizeof(*op));
924 5 op->static_data = (char *)malloc(1);
925 5 ▶ if (!op->static_data) { tpl_err(err, "out of memory"); return t; }
Branch: 1 branch
BranchTrueFalseTotal
if (!op->static_data) { tpl_err(err, "out of memory"); return t; } 0 5 5
926 5 op->static_data[0] = t->escape_char;
927 5 op->static_len = 1;
928 5 p = brace + 2;
929 5 continue;
930 }
931  
932 52762 const char *close = memchr(brace + 1, close_char, end - brace - 1);
933 52762 ▶ if (!close) {
Branch: 1 branch
BranchTrueFalseTotal
if (!close) { 2 52760 52762
934 2 tpl_err(err, "unclosed '%c' in row template"
935 " (offset %d; use '%c%c' for a literal delimiter)",
936 2 t->escape_char, (int)(brace - row),
937 2 t->escape_char, t->escape_char);
938 2 return t;
939 }
940  
941 52760 ▶ GROW_OPS();
Branch: 2 branches
BranchTrueFalseTotal
GROW_OPS(); 0 52760 52760
GROW_OPS(); 0 0 0
942 52760 tpl_op *op = &t->ops[t->nops++];
943 52760 memset(op, 0, sizeof(*op));
944 52760 parse_field_spec(brace + 1, (int)(close - brace - 1), op, &t->mode,
945 num_ref, key_ref, err, soft_err,
946 52760 (tpl_utf8 & TS_TPL_ROW) ? 1 : 0);
947 52760 ▶ if (err[0]) return t;
Branch: 1 branch
BranchTrueFalseTotal
if (err[0]) return t; 52029 731 52760
948 731 p = close + 1;
949 }
950 #undef GROW_OPS
951  
952 /* A flagged piece's high bytes are its own encoding, so only the pieces
953 that arrived as bytes can rule out a character result */
954 665 t->tpl_utf8 = tpl_utf8;
955 665 t->tpl_high =
956 660 ▶ (!(tpl_utf8 & TS_TPL_HDR) && t->header && has_high_byte(t->header, t->header_len))
Branch: 2 branches
BranchTrueFalseTotal
(!(tpl_utf8 & TS_TPL_HDR) && t->header && has_high_byte(t->header, t->header_len)) 24 636 660
(!(tpl_utf8 & TS_TPL_HDR) && t->header && has_high_byte(t->header, t->header_len)) 22 2 24
957 663 ▶ || (!(tpl_utf8 & TS_TPL_FTR) && t->footer && has_high_byte(t->footer, t->footer_len))
Branch: 3 branches
BranchTrueFalseTotal
|| (!(tpl_utf8 & TS_TPL_FTR) && t->footer && has_high_byte(t->footer, t->footer_len)) 661 2 663
|| (!(tpl_utf8 & TS_TPL_FTR) && t->footer && has_high_byte(t->footer, t->footer_len)) 20 641 661
|| (!(tpl_utf8 & TS_TPL_FTR) && t->footer && has_high_byte(t->footer, t->footer_len)) 18 2 20
958 661 ▶ || (!(tpl_utf8 & TS_TPL_SEP) && t->sep && has_high_byte(t->sep, t->sep_len))
Branch: 3 branches
BranchTrueFalseTotal
|| (!(tpl_utf8 & TS_TPL_SEP) && t->sep && has_high_byte(t->sep, t->sep_len)) 659 2 661
|| (!(tpl_utf8 & TS_TPL_SEP) && t->sep && has_high_byte(t->sep, t->sep_len)) 51 608 659
|| (!(tpl_utf8 & TS_TPL_SEP) && t->sep && has_high_byte(t->sep, t->sep_len)) 50 1 51
959 1330 ▶ || (!(tpl_utf8 & TS_TPL_ROW) && has_high_byte(row, rlen));
Branch: 3 branches
BranchTrueFalseTotal
|| (!(tpl_utf8 & TS_TPL_ROW) && has_high_byte(row, rlen)); 660 5 665
|| (!(tpl_utf8 & TS_TPL_ROW) && has_high_byte(row, rlen)); 638 22 660
|| (!(tpl_utf8 & TS_TPL_ROW) && has_high_byte(row, rlen)); 5 633 638
960  
961 665 t->saw_num_ref = num_ref[0] ? 1 : 0;
962  
963 /* one row mode per template; name both offenders, since a stray literal
964 delimiter in the text lands here */
965 665 ▶ if (num_ref[0] && key_ref[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (num_ref[0] && key_ref[0]) { 573 92 665
if (num_ref[0] && key_ref[0]) { 4 569 573
966 4 tpl_err(err, "template mixes numeric {%s} and named {%s} field references"
967 " (use '%c%c' for a literal delimiter)",
968 4 num_ref, key_ref, t->escape_char, t->escape_char);
969 4 return t;
970 }
971  
972 /* nothing more specific went wrong, so an unknown transform name stands */
973 661 ▶ if (!err[0] && soft_err[0]) tpl_err(err, "%s", soft_err);
Branch: 2 branches
BranchTrueFalseTotal
if (!err[0] && soft_err[0]) tpl_err(err, "%s", soft_err); 661 0 661
if (!err[0] && soft_err[0]) tpl_err(err, "%s", soft_err); 9 652 661
974 661 return t;
975 }
976  
977 /* ---- render ---- */
978  
979 /* clamp rather than double past the top: where STRLEN is 32 bits the
980 doubling wraps and under-reserves the buffer it is meant to grow */
981 #define TS_GROW(want) ((want) <= (STRLEN)-1 / 2 ? (want) * 2 : (STRLEN)-1)
982 #define BUF_ENSURE(need) do { if (pos + (need) > alloc) { alloc = TS_GROW(pos + (need)); buf = (char *)realloc(buf, alloc); if (!buf) croak("Text::Stencil: out of memory"); t->inflight = buf; } } while(0)
983 /* Say so rather than drop rows on a full disk or closed pipe; both callers
984 keep the render buffer in `buf` */
985 #define FH_WRITE(fh, b, n) do { \
986 SSize_t wrote_ = PerlIO_write(fh, (b), (n)); \
987 if (wrote_ < 0 || (STRLEN)wrote_ != (STRLEN)(n) || PerlIO_error(fh)) { \
988 int err_ = errno; \
989 TS_INFLIGHT_DISARM(t); \
990 free(buf); \
991 croak("Text::Stencil: write failed: %s", Strerror(err_)); \
992 } \
993 } while(0)
994  
995 /* the length guard keeps memcpy(dst, NULL, 0) out of the empty header/footer paths */
996 #define BUF_WRITE(s, l) do { if (l) { BUF_ENSURE(l); memcpy(buf + pos, s, l); pos += l; } } while(0)
997  
998 /* Pin a row for the duration of its render: skip tests and field rendering both
999 run Perl (tie, overload, DESTROY) that can clear the slot, realloc AvARRAY or
1000 drop the caller's last reference. PL_sv_undef is exempt -- it is immortal, so
1001 it needs no pin, and sv_2mortal returns early for an immortal without
1002 registering it, which left the increment unmatched and leaked a reference per
1003 undef row. */
1004 #define TS_PIN_ROW(sv) STMT_START { \
1005 if (LIKELY((sv) != &PL_sv_undef)) sv_2mortal(SvREFCNT_inc_simple_NN(sv)); \
1006 } STMT_END
1007  
1008 /* reusable buffer macros for render_buf */
1009 #define TS_INFLIGHT_ARM(t) do { \
1010 SAVEPPTR((t)->inflight); \
1011 (t)->inflight = buf; \
1012 SAVEDESTRUCTOR_X(ts_free_inflight, (t)); \
1013 } while(0)
1014 /* the buffer is ours again (re-attached or already freed): stop tracking it */
1015 #define TS_INFLIGHT_DISARM(t) ((t)->inflight = NULL)
1016  
1017 #define RBUF_INIT(t, est) do { \
1018 if (t->render_buf && t->render_buf_alloc >= (est)) { \
1019 buf = t->render_buf; alloc = t->render_buf_alloc; \
1020 } else { \
1021 alloc = (est); buf = (char *)realloc(t->render_buf, alloc); \
1022 if (!buf) croak("Text::Stencil: out of memory"); \
1023 t->render_buf = buf; t->render_buf_alloc = alloc; \
1024 } \
1025 /* Detach the reusable buffer for the render's duration. The live buffer \
1026 lives only in the local `buf`; a BUF_ENSURE/render_field realloc can \
1027 move it, and a render path can croak (e.g. a row callback dies). \
1028 Leaving t->render_buf NULL until RBUF_FINISH guarantees DESTROY and the \
1029 next render never free/realloc a stale pointer. The detached buffer is \
1030 not orphaned by a croak: TS_INFLIGHT_ARM tracks it on the savestack, so \
1031 a tie, overload or callback that dies frees it on the way out. */ \
1032 t->render_buf = NULL; t->render_buf_alloc = 0; \
1033 pos = 0; \
1034 } while(0)
1035 /* a nested render may have re-attached its own buffer; free it, do not orphan it */
1036 /* Hold on to the buffer for the next render, but not at any size: a one-off
1037 huge render would otherwise park hundreds of MB on the object for the life
1038 of the process. Give back anything wildly larger than the render actually
1039 used; a failed shrink is not an error, we simply keep what we have. */
1040 #define TS_RBUF_KEEP (1u << 20)
1041  
1042 /* An up-front guess, not a commitment: 300 bytes a row overshoots badly on
1043 short rows (a 7.5MB render reserved 294MB), and BUF_ENSURE grows the buffer
1044 anyway. Cap the guess and let it grow into whatever the render really needs. */
1045 2098 static STRLEN ts_est(STRLEN fixed, SSize_t nrows) {
1046 2098 ▶ STRLEN est = nrows > 0 ? (STRLEN)nrows * 300 : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN est = nrows > 0 ? (STRLEN)nrows * 300 : 0; 2094 4 2098
1047 2098 ▶ if (nrows > 0 && est / 300 != (STRLEN)nrows) est = TS_RBUF_KEEP; /* overflowed */
Branch: 2 branches
BranchTrueFalseTotal
if (nrows > 0 && est / 300 != (STRLEN)nrows) est = TS_RBUF_KEEP; /* overflowed */ 2094 4 2098
if (nrows > 0 && est / 300 != (STRLEN)nrows) est = TS_RBUF_KEEP; /* overflowed */ 0 2094 2094
1048 2098 ▶ if (est > TS_RBUF_KEEP) est = TS_RBUF_KEEP;
Branch: 1 branch
BranchTrueFalseTotal
if (est > TS_RBUF_KEEP) est = TS_RBUF_KEEP; 4 2094 2098
1049 2098 return fixed + est + 1;
1050 }
1051 #define RBUF_FINISH(t) do { \
1052 if (t->render_buf) free(t->render_buf); \
1053 if (alloc > TS_RBUF_KEEP && alloc / 4 > pos + 1) { \
1054 STRLEN want_ = (pos + 1) < TS_RBUF_KEEP ? TS_RBUF_KEEP : (pos + 1); \
1055 char *small_ = (char *)realloc(buf, want_); \
1056 if (small_) { buf = small_; alloc = want_; } \
1057 } \
1058 t->render_buf = buf; t->render_buf_alloc = alloc; \
1059 } while(0)
1060  
1061 /* AvARRAY is NULL on a magical AV (tied, @-/@+) even though av_top_index() is
1062 the real length, so those must go through av_fetch. */
1063 684671 PERL_STATIC_INLINE SV *av_element(pTHX_ AV *av, int col) {
1064 684671 SSize_t top = av_top_index(av);
1065 684671 ▶ if (col < 0) col = (int)(top + 1) + col;
Branch: 1 branch
BranchTrueFalseTotal
if (col < 0) col = (int)(top + 1) + col; 11 684660 684671
1066 684671 ▶ if (col < 0 || col > (int)top) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (col < 0 || col > (int)top) return NULL; 684671 0 684671
if (col < 0 || col > (int)top) return NULL; 1 684670 684671
1067 684670 ▶ if (UNLIKELY(SvRMAGICAL((SV *)av))) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(SvRMAGICAL((SV *)av))) { 5 684665 684670
1068 /* a tied array hands back an unmaterialised PVLV */
1069 5 SV **s = av_fetch(av, col, 0);
1070 5 ▶ if (!s || !*s) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !*s) return NULL; 5 0 5
if (!s || !*s) return NULL; 0 5 5
1071 5 SvGETMAGIC(*s);
1072 5 return *s;
1073 }
1074 {
1075 /* the AV can be plain while an element is not: `tie $row[0], ...` puts
1076 the magic on the element, and reading it raw skips FETCH */
1077 684665 SV **ary = AvARRAY(av);
1078 684665 ▶ SV *e = ary ? ary[col] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *e = ary ? ary[col] : NULL; 684665 0 684665
1079 684665 ▶ if (e) SvGETMAGIC(e);
Branch: 1 branch
BranchTrueFalseTotal
if (e) SvGETMAGIC(e); 684665 0 684665
1080 684665 return e;
1081 }
1082 }
1083  
1084 /* Buffer the format is assembled in; a format that will not fit is one we do
1085 not handle, so the size belongs with the check that rejects it. */
1086 #define TS_FMTBUF 64
1087  
1088 /* The conversion character of a sprintf format we actually support, or 0 for
1089 anything else. Both the formatter and the caller that decides how to numify
1090 the value ask this, so they can never disagree about what a format means.
1091 Walk %[flags][width][.precision]<conv>, insisting the conversion is last: the
1092 pushed argument is chosen from that character, so "%sx" or "%d_s" would hand
1093 printf the wrong type. '$', '*' and length modifiers fail the same test. */
1094 186 static char ts_sprintf_conv(const char *f) {
1095 STRLEN fi, flen;
1096 186 int wdigits = 0, pdigits = 0;
1097 char last;
1098 186 ▶ if (!f) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!f) return 0; 1 185 186
1099 /* measure it the way the formatter does -- it builds the format with
1100 snprintf("%s"), so the NUL is what bounds it, not any stored length */
1101 185 flen = strlen(f);
1102 185 ▶ if (flen < 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (flen < 1) return 0; 0 185 185
1103 /* a format that will not fit in the assembly buffer is one we do not
1104 handle; the .%. is added when absent, so count it either way */
1105 185 ▶ if ((f[0] == '%' ? flen : flen + 1) + 3 > (STRLEN)TS_FMTBUF) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if ((f[0] == '%' ? flen : flen + 1) + 3 > (STRLEN)TS_FMTBUF) return 0; 0 185 185
if ((f[0] == '%' ? flen : flen + 1) + 3 > (STRLEN)TS_FMTBUF) return 0; 8 177 185
1106 177 fi = (f[0] == '%') ? 1 : 0;
1107 558 ▶ while (fi < flen && (f[fi] == '-' || f[fi] == '+' ||
Branch: 3 branches
BranchTrueFalseTotal
while (fi < flen && (f[fi] == '-' || f[fi] == '+' || 558 0 558
while (fi < flen && (f[fi] == '-' || f[fi] == '+' || 360 198 558
while (fi < flen && (f[fi] == '-' || f[fi] == '+' || 3 195 198
1108 558 ▶ f[fi] == ' ' || f[fi] == '#' || f[fi] == '0')) fi++;
Branch: 3 branches
BranchTrueFalseTotal
f[fi] == ' ' || f[fi] == '#' || f[fi] == '0')) fi++; 0 195 195
f[fi] == ' ' || f[fi] == '#' || f[fi] == '0')) fi++; 3 192 195
f[fi] == ' ' || f[fi] == '#' || f[fi] == '0')) fi++; 15 177 192
1109 251 ▶ while (fi < flen && f[fi] >= '0' && f[fi] <= '9') { fi++; wdigits++; }
Branch: 3 branches
BranchTrueFalseTotal
while (fi < flen && f[fi] >= '0' && f[fi] <= '9') { fi++; wdigits++; } 251 0 251
while (fi < flen && f[fi] >= '0' && f[fi] <= '9') { fi++; wdigits++; } 202 49 251
while (fi < flen && f[fi] >= '0' && f[fi] <= '9') { fi++; wdigits++; } 74 128 202
1110 177 ▶ if (fi < flen && f[fi] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (fi < flen && f[fi] == '.') { 177 0 177
if (fi < flen && f[fi] == '.') { 37 140 177
1111 37 fi++;
1112 72 ▶ while (fi < flen && f[fi] >= '0' && f[fi] <= '9') { fi++; pdigits++; }
Branch: 3 branches
BranchTrueFalseTotal
while (fi < flen && f[fi] >= '0' && f[fi] <= '9') { fi++; pdigits++; } 72 0 72
while (fi < flen && f[fi] >= '0' && f[fi] <= '9') { fi++; pdigits++; } 70 2 72
while (fi < flen && f[fi] >= '0' && f[fi] <= '9') { fi++; pdigits++; } 35 35 70
1113 }
1114 177 ▶ if (fi != flen - 1 || wdigits > 8 || pdigits > 8) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (fi != flen - 1 || wdigits > 8 || pdigits > 8) return 0; 131 46 177
if (fi != flen - 1 || wdigits > 8 || pdigits > 8) return 0; 129 2 131
if (fi != flen - 1 || wdigits > 8 || pdigits > 8) return 0; 0 129 129
1115 129 last = f[fi];
1116 129 ▶ if (last == 'd' || last == 'i' || last == 'x' || last == 'X' ||
Branch: 5 branches
BranchTrueFalseTotal
if (last == 'd' || last == 'i' || last == 'x' || last == 'X' || 77 52 129
if (last == 'd' || last == 'i' || last == 'x' || last == 'X' || 74 3 77
if (last == 'd' || last == 'i' || last == 'x' || last == 'X' || 62 12 74
if (last == 'd' || last == 'i' || last == 'x' || last == 'X' || 59 3 62
if (last == 'd' || last == 'i' || last == 'x' || last == 'X' || 53 6 59
1117 53 ▶ last == 'o' || last == 'u' || last == 'f' || last == 'e' ||
Branch: 4 branches
BranchTrueFalseTotal
last == 'o' || last == 'u' || last == 'f' || last == 'e' || 50 3 53
last == 'o' || last == 'u' || last == 'f' || last == 'e' || 38 12 50
last == 'o' || last == 'u' || last == 'f' || last == 'e' || 35 3 38
last == 'o' || last == 'u' || last == 'f' || last == 'e' || 35 0 35
1118 129 ▶ last == 'g' || last == 's') return last;
Branch: 1 branch
BranchTrueFalseTotal
last == 'g' || last == 's') return last; 33 2 35
1119 2 return 0;
1120 }
1121  
1122 /* Own the bytes of a template piece. Pinning the argument SV keeps it alive
1123 but not its buffer: stringifying a later argument can run an overload that
1124 grows an earlier one in place, freeing the PV we were still holding. */
1125 116887 static const char *ts_own_pv(pTHX_ SV *sv, STRLEN *lenp, int *utf8p) {
1126 STRLEN l;
1127 84885 const char *p = SvPV(sv, l);
1128 /* take the encoding from the same call that produced the bytes: an overload
1129 returns its own string, whose flag has nothing to do with the RV's */
1130 84885 ▶ if (utf8p) *utf8p = SvUTF8(sv) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
if (utf8p) *utf8p = SvUTF8(sv) ? 1 : 0; 84866 19 84885
1131 84885 SV *owned = sv_2mortal(newSVpvn(p, l));
1132 84885 *lenp = l;
1133 84885 return SvPVX(owned);
1134 }
1135  
1136 /* Install a skip-condition key on a template that no SV owns yet: the croak
1137 has to reclaim it, or a failed allocation strands the whole thing. All four
1138 call sites -- new's two and clone's two -- were this same sequence. */
1139 11 static void ts_set_skip_key(pTHX_ tpl_compiled *t, char **dst, STRLEN *lenp,
1140 int *utf8p, const char *src, STRLEN len, int utf8) {
1141 11 char *p = (char *)malloc(len + 1);
1142 11 ▶ if (!p) { tpl_free(t); croak("Text::Stencil: out of memory"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!p) { tpl_free(t); croak("Text::Stencil: out of memory"); } 0 11 11
1143 11 ▶ if (len) memcpy(p, src, len);
Branch: 1 branch
BranchTrueFalseTotal
if (len) memcpy(p, src, len); 11 0 11
1144 11 p[len] = '\0';
1145 11 *dst = p; *lenp = len; *utf8p = utf8;
1146 11 }
1147  
1148 /* Column indices are ints. A wider IV would narrow onto some unrelated valid
1149 column, so reject it the way an out-of-range template index is rejected.
1150 Every caller resolves its columns before tpl_compile runs, so there is never
1151 a half-built template for the croak to strand. */
1152 59 static int ts_col_iv(pTHX_ IV v, const char *what) {
1153 59 ▶ if (v > INT_MAX || v < INT_MIN) {
Branch: 2 branches
BranchTrueFalseTotal
if (v > INT_MAX || v < INT_MIN) { 59 0 59
if (v > INT_MAX || v < INT_MIN) { 0 59 59
1154 0 croak("Text::Stencil: %s column index %" IVdf " is out of range", what, v);
1155 }
1156 59 return (int)v;
1157 }
1158  
1159 /* Same check, but from the SV: SvIV has already saturated or wrapped by the
1160 time we see it, so "99999999999999999999" arrives as -1 and would quietly
1161 become the last column. The NV still knows how big the value really was. */
1162 71 static int ts_col_sv(pTHX_ SV *sv, const char *what) {
1163 71 NV nv = SvNV(sv);
1164 71 ▶ if (!(nv >= (NV)INT_MIN && nv <= (NV)INT_MAX)) { /* false for NaN too */
Branch: 2 branches
BranchTrueFalseTotal
if (!(nv >= (NV)INT_MIN && nv <= (NV)INT_MAX)) { /* false for NaN too */ 68 3 71
if (!(nv >= (NV)INT_MIN && nv <= (NV)INT_MAX)) { /* false for NaN too */ 9 59 68
1165 12 croak("Text::Stencil: %s column index '%s' is out of range",
1166 what, SvPV_nolen(sv));
1167 }
1168 59 return ts_col_iv(aTHX_ SvIV(sv), what);
1169 }
1170  
1171 713050 PERL_STATIC_INLINE SV *fetch_field(pTHX_ SV *row_sv, tpl_op *op, enum row_mode mode) {
1172 713050 SvGETMAGIC(row_sv); /* the row itself may come from a magical array */
1173 713050 ▶ if (!SvROK(row_sv)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(row_sv)) return NULL; 1603 711447 713050
1174 711447 ▶ if (mode == ROW_HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode == ROW_HASH) { 26814 684633 711447
1175 26814 ▶ if (SvTYPE(SvRV(row_sv)) != SVt_PVHV || !op->key) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(SvRV(row_sv)) != SVt_PVHV || !op->key) return NULL; 26814 0 26814
if (SvTYPE(SvRV(row_sv)) != SVt_PVHV || !op->key) return NULL; 0 26814 26814
1176 /* a positive klen only matches non-UTF8 HEKs, so a key taken from a
1177 flagged template never matched a flagged hash key */
1178 26814 ▶ SV **sv = hv_fetch((HV *)SvRV(row_sv), op->key,
Branch: 1 branch
BranchTrueFalseTotal
SV **sv = hv_fetch((HV *)SvRV(row_sv), op->key, 13 26801 26814
1179 op->key_utf8 ? -(I32)op->key_len : (I32)op->key_len, 0);
1180 26814 ▶ if (!sv || !*sv) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !*sv) return NULL; 26811 3 26814
if (!sv || !*sv) return NULL; 0 26811 26811
1181 26811 SvGETMAGIC(*sv);
1182 611 return *sv;
1183 } else {
1184 684633 ▶ if (SvTYPE(SvRV(row_sv)) != SVt_PVAV) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(row_sv)) != SVt_PVAV) return NULL; 0 684633 684633
1185 684633 return av_element(aTHX_ (AV *)SvRV(row_sv), op->col);
1186 }
1187 }
1188  
1189 /* Apply a single transform, writing result to buf+pos or tmp */
1190 /* a slice at byte offset `at` splits a character if a continuation byte lands
1191 there -- O(1), and pure-ASCII values can never trip it */
1192 #define TS_NOTE_CUT(t, s, len, at) do { \
1193 STRLEN a_ = (STRLEN)(at); \
1194 if (a_ < (len) && ((unsigned char)(s)[a_] & 0xC0) == 0x80) (t)->r_high = 1; \
1195 } while(0)
1196  
1197 /* SI tiers, index 0 the bare unit. Choosing by magnitude alone let rounding
1198 spill out of the tier: "999.6" printed 1000 with no suffix */
1199 static const double si_dec[] = {1.0, 1e3, 1e6, 1e9, 1e12, 1e15};
1200 static const char *si_dec_suf[] = {"", "K", "M", "G", "T", "P"};
1201 static const double si_bin[] = {1.0, 1024.0, 1048576.0, 1073741824.0,
1202 1099511627776.0, 1125899906842624.0,
1203 1152921504606846976.0};
1204 static const char *si_bin_suf[] = {"B", "KB", "MB", "GB", "TB", "PB", "EB"};
1205  
1206 16 static int si_tier(double v, const double *div, int top) {
1207 16 ▶ double av = v < 0 ? -v : v;
Branch: 1 branch
BranchTrueFalseTotal
double av = v < 0 ? -v : v; 1 15 16
1208 16 int ti = 0;
1209 43 ▶ while (ti < top && av >= div[ti + 1]) ti++;
Branch: 2 branches
BranchTrueFalseTotal
while (ti < top && av >= div[ti + 1]) ti++; 41 2 43
while (ti < top && av >= div[ti + 1]) ti++; 27 14 41
1210 16 ▶ if (ti < top) {
Branch: 1 branch
BranchTrueFalseTotal
if (ti < top) { 14 2 16
1211 /* tier 0 prints %.0f, the rest %.1f, so they round at different edges */
1212 14 double ratio = div[ti + 1] / div[ti];
1213 14 ▶ if (av / div[ti] >= ratio - (ti ? 0.05 : 0.5)) ti++;
Branch: 2 branches
BranchTrueFalseTotal
if (av / div[ti] >= ratio - (ti ? 0.05 : 0.5)) ti++; 8 6 14
if (av / div[ti] >= ratio - (ti ? 0.05 : 0.5)) ti++; 2 12 14
1214 }
1215 16 return ti;
1216 }
1217  
1218 676021 static void apply_xform(tpl_compiled *t, tpl_xform *xf, const char *src, STRLEN slen,
1219 char **bufp, STRLEN *posp, STRLEN *allocp,
1220 char **tmpp, STRLEN *tmp_lenp, STRLEN *tmp_allocp,
1221 int to_output) {
1222 676021 char *buf = *bufp; STRLEN pos = *posp; STRLEN alloc = *allocp;
1223 676021 char *tmp = *tmpp; STRLEN tmp_len = *tmp_lenp; STRLEN tmp_alloc = *tmp_allocp;
1224  
1225 /* macro to write to either output or temp */
1226 #define OUT_ENSURE(n) do { \
1227 if (to_output) { BUF_ENSURE(n); } \
1228 else { if ((STRLEN)(n) > tmp_alloc || !tmp) { tmp_alloc = (n) < 1 ? 1 : TS_GROW((STRLEN)(n)); tmp = (char *)realloc(tmp, tmp_alloc); if (!tmp) croak("Text::Stencil: out of memory"); } } \
1229 } while(0)
1230 /* A transform whose output is a multiple of its input must reserve that
1231 multiple. Where STRLEN is 32 bits the product can wrap while the input
1232 is still under the 2GB passthrough limit, which would under-reserve the
1233 buffer and let the transform write past it -- so refuse instead. */
1234 #define TS_OUT_ENSURE_MUL(n, m) do { \
1235 STRLEN n_ = (STRLEN)(n), m_ = (STRLEN)(m); \
1236 if (m_ && n_ > (STRLEN)-1 / m_) croak("Text::Stencil: value too large to transform"); \
1237 OUT_ENSURE(n_ * m_); \
1238 } while(0)
1239 #define OUT_PTR (to_output ? buf + pos : tmp)
1240 /* Every transform ends by publishing how much it wrote, to whichever of the
1241 two destinations OUT_PTR selected. It was spelled out 46 times. */
1242 #define OUT_COMMIT(n) do { if (to_output) pos += (n); else tmp_len = (n); } while (0)
1243 /* and nine of them are exactly "emit the input unchanged" */
1244 #define OUT_PASSTHRU() do { OUT_ENSURE(slen); memcpy(OUT_PTR, src, slen); OUT_COMMIT(slen); } while (0)
1245  
1246 /* the widths below are ints; past INT_MAX one wraps negative and corrupts
1247 the write offset, and no transform has a useful answer for 2GB anyway */
1248 676021 ▶ if (UNLIKELY(slen > (STRLEN)INT_MAX)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(slen > (STRLEN)INT_MAX)) { 0 676021 676021
1249 0 ▶ OUT_PASSTHRU();
Branch: 11 branches
BranchTrueFalseTotal
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
1250 0 goto done;
1251 }
1252  
1253 676021 switch (xf->type) {
1254 35 case XF_INT: {
1255 35 ▶ OUT_ENSURE(20);
Branch: 7 branches
BranchTrueFalseTotal
OUT_ENSURE(20); 35 0 35
OUT_ENSURE(20); 0 35 35
OUT_ENSURE(20); 0 0 0
OUT_ENSURE(20); 0 0 0
OUT_ENSURE(20); 0 0 0
OUT_ENSURE(20); 0 0 0
OUT_ENSURE(20); 0 0 0
1256 35 int neg = ts_has_minus(src, slen);
1257 35 IV v = ts_scan_iv(src, slen, neg);
1258 35 ▶ int w = itoa_fast(OUT_PTR, v);
Branch: 1 branch
BranchTrueFalseTotal
int w = itoa_fast(OUT_PTR, v); 35 0 35
1259 35 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 35 0 35
1260 35 break;
1261 }
1262 1003 case XF_INT_COMMA: {
1263 1003 ▶ OUT_ENSURE(28);
Branch: 7 branches
BranchTrueFalseTotal
OUT_ENSURE(28); 1003 0 1003
OUT_ENSURE(28); 0 1003 1003
OUT_ENSURE(28); 0 0 0
OUT_ENSURE(28); 0 0 0
OUT_ENSURE(28); 0 0 0
OUT_ENSURE(28); 0 0 0
OUT_ENSURE(28); 0 0 0
1264 1003 int neg = ts_has_minus(src, slen);
1265 1003 IV v = ts_scan_iv(src, slen, neg);
1266 1003 ▶ int w = itoa_comma(OUT_PTR, v);
Branch: 1 branch
BranchTrueFalseTotal
int w = itoa_comma(OUT_PTR, v); 1003 0 1003
1267 1003 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 1003 0 1003
1268 1003 break;
1269 }
1270 2 case XF_FLOAT: {
1271 2 ▶ OUT_ENSURE(TS_FLTSZ);
Branch: 7 branches
BranchTrueFalseTotal
OUT_ENSURE(TS_FLTSZ); 2 0 2
OUT_ENSURE(TS_FLTSZ); 0 2 2
OUT_ENSURE(TS_FLTSZ); 0 0 0
OUT_ENSURE(TS_FLTSZ); 0 0 0
OUT_ENSURE(TS_FLTSZ); 0 0 0
OUT_ENSURE(TS_FLTSZ); 0 0 0
OUT_ENSURE(TS_FLTSZ); 0 0 0
1272 2 double fv = ts_atof(src, slen);
1273 2 ▶ int w = snprintf(OUT_PTR, TS_FLTSZ, "%.*f", xf->param_int, fv);
Branch: 1 branch
BranchTrueFalseTotal
int w = snprintf(OUT_PTR, TS_FLTSZ, "%.*f", xf->param_int, fv); 2 0 2
1274 2 ▶ if (w > TS_FLTSZ - 1) w = TS_FLTSZ - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (w > TS_FLTSZ - 1) w = TS_FLTSZ - 1; 0 2 2
1275 2 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 2 0 2
1276 2 break;
1277 }
1278 296 case XF_HTML: {
1279 296 ▶ TS_OUT_ENSURE_MUL(slen, 6);
Branch: 11 branches
BranchTrueFalseTotal
TS_OUT_ENSURE_MUL(slen, 6); 296 0 296
TS_OUT_ENSURE_MUL(slen, 6); 0 296 296
TS_OUT_ENSURE_MUL(slen, 6); 296 0 296
TS_OUT_ENSURE_MUL(slen, 6); 2 294 296
TS_OUT_ENSURE_MUL(slen, 6); 2 0 2
TS_OUT_ENSURE_MUL(slen, 6); 0 2 2
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
1280 296 int needs_escape = 0;
1281 101230 ▶ for (STRLEN i = 0; i < slen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i < slen; i++) 100962 268 101230
1282 100962 ▶ if (html_special[(unsigned char)src[i]]) { needs_escape = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (html_special[(unsigned char)src[i]]) { needs_escape = 1; break; } 28 100934 100962
1283 296 ▶ if (!needs_escape) {
Branch: 1 branch
BranchTrueFalseTotal
if (!needs_escape) { 268 28 296
1284 268 ▶ memcpy(OUT_PTR, src, slen);
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR, src, slen); 268 0 268
1285 268 ▶ OUT_COMMIT(slen);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(slen); 268 0 268
1286 } else {
1287 28 ▶ STRLEN w = html_escape(OUT_PTR, src, slen);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN w = html_escape(OUT_PTR, src, slen); 28 0 28
1288 28 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 28 0 28
1289 }
1290 296 break;
1291 }
1292 257 case XF_HTML_BR: {
1293 257 ▶ TS_OUT_ENSURE_MUL(slen, 6);
Branch: 11 branches
BranchTrueFalseTotal
TS_OUT_ENSURE_MUL(slen, 6); 257 0 257
TS_OUT_ENSURE_MUL(slen, 6); 0 257 257
TS_OUT_ENSURE_MUL(slen, 6); 257 0 257
TS_OUT_ENSURE_MUL(slen, 6); 0 257 257
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
1294 257 int needs_escape = 0;
1295 512 ▶ for (STRLEN i = 0; i < slen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i < slen; i++) 262 250 512
1296 262 ▶ if (html_br_special[(unsigned char)src[i]]) { needs_escape = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (html_br_special[(unsigned char)src[i]]) { needs_escape = 1; break; } 7 255 262
1297 257 ▶ if (!needs_escape) {
Branch: 1 branch
BranchTrueFalseTotal
if (!needs_escape) { 250 7 257
1298 250 ▶ memcpy(OUT_PTR, src, slen);
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR, src, slen); 250 0 250
1299 250 ▶ OUT_COMMIT(slen);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(slen); 250 0 250
1300 } else {
1301 7 ▶ STRLEN w = html_br_escape(OUT_PTR, src, slen);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN w = html_br_escape(OUT_PTR, src, slen); 7 0 7
1302 7 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 7 0 7
1303 }
1304 257 break;
1305 }
1306 2 case XF_URL: {
1307 2 ▶ TS_OUT_ENSURE_MUL(slen, 3);
Branch: 11 branches
BranchTrueFalseTotal
TS_OUT_ENSURE_MUL(slen, 3); 2 0 2
TS_OUT_ENSURE_MUL(slen, 3); 0 2 2
TS_OUT_ENSURE_MUL(slen, 3); 2 0 2
TS_OUT_ENSURE_MUL(slen, 3); 1 1 2
TS_OUT_ENSURE_MUL(slen, 3); 1 0 1
TS_OUT_ENSURE_MUL(slen, 3); 0 1 1
TS_OUT_ENSURE_MUL(slen, 3); 0 0 0
TS_OUT_ENSURE_MUL(slen, 3); 0 0 0
TS_OUT_ENSURE_MUL(slen, 3); 0 0 0
TS_OUT_ENSURE_MUL(slen, 3); 0 0 0
TS_OUT_ENSURE_MUL(slen, 3); 0 0 0
1308 2 ▶ STRLEN w = url_escape(OUT_PTR, src, slen);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN w = url_escape(OUT_PTR, src, slen); 2 0 2
1309 2 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 2 0 2
1310 2 break;
1311 }
1312 263 case XF_JSON: {
1313 263 ▶ TS_OUT_ENSURE_MUL(slen, 6);
Branch: 11 branches
BranchTrueFalseTotal
TS_OUT_ENSURE_MUL(slen, 6); 263 0 263
TS_OUT_ENSURE_MUL(slen, 6); 0 263 263
TS_OUT_ENSURE_MUL(slen, 6); 263 0 263
TS_OUT_ENSURE_MUL(slen, 6); 0 263 263
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
TS_OUT_ENSURE_MUL(slen, 6); 0 0 0
1314 263 int needs_escape = 0;
1315 522 ▶ for (STRLEN i = 0; i < slen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i < slen; i++) 298 224 522
1316 298 ▶ if (json_special[(unsigned char)src[i]]) { needs_escape = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (json_special[(unsigned char)src[i]]) { needs_escape = 1; break; } 39 259 298
1317 263 ▶ if (!needs_escape) {
Branch: 1 branch
BranchTrueFalseTotal
if (!needs_escape) { 224 39 263
1318 224 ▶ memcpy(OUT_PTR, src, slen);
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR, src, slen); 224 0 224
1319 224 ▶ OUT_COMMIT(slen);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(slen); 224 0 224
1320 } else {
1321 39 ▶ STRLEN w = json_escape(OUT_PTR, src, slen);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN w = json_escape(OUT_PTR, src, slen); 39 0 39
1322 39 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 39 0 39
1323 }
1324 263 break;
1325 }
1326 14 case XF_TRIM: {
1327 14 const char *s = src; STRLEN l = slen;
1328 31 ▶ while (l > 0 && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')) { s++; l--; }
Branch: 5 branches
BranchTrueFalseTotal
while (l > 0 && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')) { s++; l--; } 31 0 31
while (l > 0 && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')) { s++; l--; } 17 14 31
while (l > 0 && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')) { s++; l--; } 0 14 14
while (l > 0 && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')) { s++; l--; } 0 14 14
while (l > 0 && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')) { s++; l--; } 0 14 14
1329 32 ▶ while (l > 0 && (s[l-1] == ' ' || s[l-1] == '\t' || s[l-1] == '\n' || s[l-1] == '\r')) l--;
Branch: 5 branches
BranchTrueFalseTotal
while (l > 0 && (s[l-1] == ' ' || s[l-1] == '\t' || s[l-1] == '\n' || s[l-1] == '\r')) l--; 32 0 32
while (l > 0 && (s[l-1] == ' ' || s[l-1] == '\t' || s[l-1] == '\n' || s[l-1] == '\r')) l--; 17 15 32
while (l > 0 && (s[l-1] == ' ' || s[l-1] == '\t' || s[l-1] == '\n' || s[l-1] == '\r')) l--; 0 15 15
while (l > 0 && (s[l-1] == ' ' || s[l-1] == '\t' || s[l-1] == '\n' || s[l-1] == '\r')) l--; 1 14 15
while (l > 0 && (s[l-1] == ' ' || s[l-1] == '\t' || s[l-1] == '\n' || s[l-1] == '\r')) l--; 0 14 14
1330 14 ▶ OUT_ENSURE(l);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(l); 2 12 14
OUT_ENSURE(l); 0 2 2
OUT_ENSURE(l); 0 0 0
OUT_ENSURE(l); 0 0 0
OUT_ENSURE(l); 2 10 12
OUT_ENSURE(l); 0 2 2
OUT_ENSURE(l); 10 0 10
OUT_ENSURE(l); 10 0 10
OUT_ENSURE(l); 0 10 10
1331 14 ▶ memcpy(OUT_PTR, s, l);
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR, s, l); 2 12 14
1332 14 ▶ OUT_COMMIT(l);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(l); 2 12 14
1333 14 break;
1334 }
1335 /* ASCII-only by hand: toupper/tolower follow LC_CTYPE, and in an 8-bit
1336 locale glibc maps 0xE0-0xFE onto 0xC0-0xDE -- UTF-8 lead bytes -- which
1337 turned a flagged value into malformed UTF-8 */
1338 9 case XF_UC: {
1339 9 ▶ OUT_ENSURE(slen);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(slen); 8 1 9
OUT_ENSURE(slen); 0 8 8
OUT_ENSURE(slen); 0 0 0
OUT_ENSURE(slen); 0 0 0
OUT_ENSURE(slen); 0 1 1
OUT_ENSURE(slen); 0 0 0
OUT_ENSURE(slen); 1 0 1
OUT_ENSURE(slen); 1 0 1
OUT_ENSURE(slen); 0 1 1
1340 43 ▶ for (STRLEN i = 0; i < slen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i < slen; i++) { 34 9 43
1341 34 unsigned char c = (unsigned char)src[i];
1342 34 ▶ OUT_PTR[i] = (c >= 'a' && c <= 'z') ? (char)(c - 32) : (char)c;
Branch: 3 branches
BranchTrueFalseTotal
OUT_PTR[i] = (c >= 'a' && c <= 'z') ? (char)(c - 32) : (char)c; 26 8 34
OUT_PTR[i] = (c >= 'a' && c <= 'z') ? (char)(c - 32) : (char)c; 23 3 26
OUT_PTR[i] = (c >= 'a' && c <= 'z') ? (char)(c - 32) : (char)c; 25 9 34
1343 }
1344 9 ▶ OUT_COMMIT(slen);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(slen); 8 1 9
1345 9 break;
1346 }
1347 6 case XF_LC: {
1348 6 ▶ OUT_ENSURE(slen);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(slen); 3 3 6
OUT_ENSURE(slen); 0 3 3
OUT_ENSURE(slen); 0 0 0
OUT_ENSURE(slen); 0 0 0
OUT_ENSURE(slen); 2 1 3
OUT_ENSURE(slen); 0 2 2
OUT_ENSURE(slen); 1 0 1
OUT_ENSURE(slen); 1 0 1
OUT_ENSURE(slen); 0 1 1
1349 40 ▶ for (STRLEN i = 0; i < slen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i < slen; i++) { 34 6 40
1350 34 unsigned char c = (unsigned char)src[i];
1351 34 ▶ OUT_PTR[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : (char)c;
Branch: 3 branches
BranchTrueFalseTotal
OUT_PTR[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : (char)c; 19 15 34
OUT_PTR[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : (char)c; 6 13 19
OUT_PTR[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : (char)c; 10 24 34
1352 }
1353 6 ▶ OUT_COMMIT(slen);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(slen); 3 3 6
1354 6 break;
1355 }
1356 16 case XF_PAD: {
1357 16 int w = xf->param_int; /* compile-time validated non-negative */
1358 16 ▶ OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen));
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 16 0 16
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 16 16
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
1359 16 int pad = w - (int)slen;
1360 16 ▶ if (pad > 0) { memset(OUT_PTR, ' ', pad); memcpy(OUT_PTR + pad, src, slen); }
Branch: 3 branches
BranchTrueFalseTotal
if (pad > 0) { memset(OUT_PTR, ' ', pad); memcpy(OUT_PTR + pad, src, slen); } 15 1 16
if (pad > 0) { memset(OUT_PTR, ' ', pad); memcpy(OUT_PTR + pad, src, slen); } 15 0 15
if (pad > 0) { memset(OUT_PTR, ' ', pad); memcpy(OUT_PTR + pad, src, slen); } 15 0 15
1361 1 ▶ else memcpy(OUT_PTR, src, slen);
Branch: 1 branch
BranchTrueFalseTotal
else memcpy(OUT_PTR, src, slen); 1 0 1
1362 16 ▶ int total = pad > 0 ? w : (int)slen;
Branch: 1 branch
BranchTrueFalseTotal
int total = pad > 0 ? w : (int)slen; 1 15 16
1363 16 ▶ OUT_COMMIT(total);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(total); 16 0 16
1364 16 break;
1365 }
1366 9 case XF_RPAD: {
1367 9 int w = xf->param_int; /* compile-time validated non-negative */
1368 9 ▶ OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen));
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 9 0 9
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 9 9
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
OUT_ENSURE((STRLEN)(w > (int)slen ? w : (int)slen)); 0 0 0
1369 9 ▶ memcpy(OUT_PTR, src, slen);
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR, src, slen); 9 0 9
1370 9 int pad = w - (int)slen;
1371 9 ▶ if (pad > 0) memset(OUT_PTR + slen, ' ', pad);
Branch: 2 branches
BranchTrueFalseTotal
if (pad > 0) memset(OUT_PTR + slen, ' ', pad); 8 1 9
if (pad > 0) memset(OUT_PTR + slen, ' ', pad); 8 0 8
1372 9 ▶ int total = pad > 0 ? w : (int)slen;
Branch: 1 branch
BranchTrueFalseTotal
int total = pad > 0 ? w : (int)slen; 1 8 9
1373 9 ▶ OUT_COMMIT(total);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(total); 9 0 9
1374 9 break;
1375 }
1376 18 case XF_TRUNC: {
1377 18 int mx = xf->param_int;
1378 18 ▶ if (mx < 0) mx = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (mx < 0) mx = 0; 0 18 18
1379 18 ▶ if ((int)slen <= mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ((int)slen <= mx) { 3 15 18
1380 3 ▶ OUT_PASSTHRU();
Branch: 11 branches
BranchTrueFalseTotal
OUT_PASSTHRU(); 2 1 3
OUT_PASSTHRU(); 0 2 2
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 1 1
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 1 0 1
OUT_PASSTHRU(); 1 0 1
OUT_PASSTHRU(); 0 1 1
OUT_PASSTHRU(); 2 1 3
OUT_PASSTHRU(); 2 1 3
1381 15 ▶ } else if (mx <= 3) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (mx <= 3) { 10 5 15
1382 /* no room for the ellipsis: hard cut, never longer than asked */
1383 10 ▶ TS_NOTE_CUT(t, src, slen, mx);
Branch: 2 branches
BranchTrueFalseTotal
TS_NOTE_CUT(t, src, slen, mx); 10 0 10
TS_NOTE_CUT(t, src, slen, mx); 0 10 10
1384 10 ▶ OUT_ENSURE(mx); memcpy(OUT_PTR, src, mx);
Branch: 10 branches
BranchTrueFalseTotal
OUT_ENSURE(mx); memcpy(OUT_PTR, src, mx); 10 0 10
OUT_ENSURE(mx); memcpy(OUT_PTR, src, mx); 0 10 10
OUT_ENSURE(mx); memcpy(OUT_PTR, src, mx); 0 0 0
OUT_ENSURE(mx); memcpy(OUT_PTR, src, mx); 0 0 0
OUT_ENSURE(mx); memcpy(OUT_PTR, src, mx); 0 0 0
OUT_ENSURE(mx); memcpy(OUT_PTR, src, mx); 0 0 0
OUT_ENSURE(mx); memcpy(OUT_PTR, src, mx); 0 0 0
OUT_ENSURE(mx); memcpy(OUT_PTR, src, mx); 0 0 0
OUT_ENSURE(mx); memcpy(OUT_PTR, src, mx); 0 0 0
OUT_ENSURE(mx); memcpy(OUT_PTR, src, mx); 10 0 10
1385 10 ▶ OUT_COMMIT(mx);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(mx); 10 0 10
1386 } else {
1387 5 int tl = mx - 3;
1388 5 ▶ TS_NOTE_CUT(t, src, slen, tl);
Branch: 2 branches
BranchTrueFalseTotal
TS_NOTE_CUT(t, src, slen, tl); 5 0 5
TS_NOTE_CUT(t, src, slen, tl); 1 4 5
1389 5 ▶ OUT_ENSURE(tl + 3);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(tl + 3); 3 2 5
OUT_ENSURE(tl + 3); 0 3 3
OUT_ENSURE(tl + 3); 0 0 0
OUT_ENSURE(tl + 3); 0 0 0
OUT_ENSURE(tl + 3); 0 2 2
OUT_ENSURE(tl + 3); 0 0 0
OUT_ENSURE(tl + 3); 2 0 2
OUT_ENSURE(tl + 3); 2 0 2
OUT_ENSURE(tl + 3); 0 2 2
1390 5 ▶ memcpy(OUT_PTR, src, tl);
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR, src, tl); 3 2 5
1391 5 ▶ memcpy(OUT_PTR + tl, "...", 3);
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR + tl, "...", 3); 3 2 5
1392 5 ▶ OUT_COMMIT(tl + 3);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(tl + 3); 3 2 5
1393 }
1394 18 break;
1395 }
1396 2 case XF_HEX: {
1397 2 ▶ TS_OUT_ENSURE_MUL(slen, 2);
Branch: 11 branches
BranchTrueFalseTotal
TS_OUT_ENSURE_MUL(slen, 2); 2 0 2
TS_OUT_ENSURE_MUL(slen, 2); 0 2 2
TS_OUT_ENSURE_MUL(slen, 2); 2 0 2
TS_OUT_ENSURE_MUL(slen, 2); 1 1 2
TS_OUT_ENSURE_MUL(slen, 2); 1 0 1
TS_OUT_ENSURE_MUL(slen, 2); 0 1 1
TS_OUT_ENSURE_MUL(slen, 2); 0 0 0
TS_OUT_ENSURE_MUL(slen, 2); 0 0 0
TS_OUT_ENSURE_MUL(slen, 2); 0 0 0
TS_OUT_ENSURE_MUL(slen, 2); 0 0 0
TS_OUT_ENSURE_MUL(slen, 2); 0 0 0
1398 2 ▶ STRLEN w = hex_encode(OUT_PTR, src, slen);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN w = hex_encode(OUT_PTR, src, slen); 2 0 2
1399 2 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 2 0 2
1400 2 break;
1401 }
1402 7 case XF_BASE64: {
1403 7 ▶ OUT_ENSURE(((slen + 2) / 3) * 4);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(((slen + 2) / 3) * 4); 7 0 7
OUT_ENSURE(((slen + 2) / 3) * 4); 0 7 7
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
1404 7 ▶ STRLEN w = base64_encode(OUT_PTR, (const unsigned char *)src, slen);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN w = base64_encode(OUT_PTR, (const unsigned char *)src, slen); 7 0 7
1405 7 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 7 0 7
1406 7 break;
1407 }
1408 3 case XF_BASE64URL: {
1409 3 ▶ OUT_ENSURE(((slen + 2) / 3) * 4);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(((slen + 2) / 3) * 4); 3 0 3
OUT_ENSURE(((slen + 2) / 3) * 4); 0 3 3
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
OUT_ENSURE(((slen + 2) / 3) * 4); 0 0 0
1410 3 ▶ STRLEN w = base64url_encode(OUT_PTR, (const unsigned char *)src, slen);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN w = base64url_encode(OUT_PTR, (const unsigned char *)src, slen); 3 0 3
1411 3 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 3 0 3
1412 3 break;
1413 }
1414 2 case XF_COUNT: {
1415 /* Reached by {#:count}: render_field handles count itself, but that
1416 handler is gated on !is_rownum, and param_int is 0 -- which is what
1417 count documents for a scalar, so the row number lands right. An
1418 undef field used to arrive here too, via use_default, and printed 0
1419 over whatever default had supplied; render_field now drops the
1420 count stage in that case instead. */
1421 2 ▶ OUT_ENSURE(12);
Branch: 7 branches
BranchTrueFalseTotal
OUT_ENSURE(12); 2 0 2
OUT_ENSURE(12); 0 2 2
OUT_ENSURE(12); 0 0 0
OUT_ENSURE(12); 0 0 0
OUT_ENSURE(12); 0 0 0
OUT_ENSURE(12); 0 0 0
OUT_ENSURE(12); 0 0 0
1422 2 ▶ int w = itoa_fast(OUT_PTR, xf->param_int);
Branch: 1 branch
BranchTrueFalseTotal
int w = itoa_fast(OUT_PTR, xf->param_int); 2 0 2
1423 2 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 2 0 2
1424 2 break;
1425 }
1426 10 case XF_BOOL: {
1427 10 int truthy = (slen > 0);
1428 10 ▶ if (slen == 1 && src[0] == '0') truthy = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (slen == 1 && src[0] == '0') truthy = 0; 7 3 10
if (slen == 1 && src[0] == '0') truthy = 0; 4 3 7
1429 const char *val; STRLEN vlen;
1430 10 ▶ if (truthy) {
Branch: 1 branch
BranchTrueFalseTotal
if (truthy) { 5 5 10
1431 5 ▶ val = xf->param_str ? xf->param_str : "true";
Branch: 1 branch
BranchTrueFalseTotal
val = xf->param_str ? xf->param_str : "true"; 4 1 5
1432 5 ▶ vlen = xf->param_str ? xf->param_str_len : 4;
Branch: 1 branch
BranchTrueFalseTotal
vlen = xf->param_str ? xf->param_str_len : 4; 4 1 5
1433 } else {
1434 5 ▶ val = xf->param_str2 ? xf->param_str2 : "false";
Branch: 1 branch
BranchTrueFalseTotal
val = xf->param_str2 ? xf->param_str2 : "false"; 4 1 5
1435 5 ▶ vlen = xf->param_str2 ? xf->param_str2_len : 5;
Branch: 1 branch
BranchTrueFalseTotal
vlen = xf->param_str2 ? xf->param_str2_len : 5; 4 1 5
1436 }
1437 10 ▶ OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen);
Branch: 10 branches
BranchTrueFalseTotal
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 10 0 10
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 10 10
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 10 0 10
1438 10 ▶ OUT_COMMIT(vlen);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(vlen); 10 0 10
1439 10 break;
1440 }
1441 7 case XF_DATE: {
1442 /* gmtime_r leaves tm untouched when it fails, so bound the epoch and
1443 check the result before strftime reads it */
1444 7 long long e = 0; int bad = 0;
1445 83 ▶ for (STRLEN i = 0; i < slen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i < slen; i++) { 78 5 83
1446 78 ▶ if (src[i] >= '0' && src[i] <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (src[i] >= '0' && src[i] <= '9') { 78 0 78
if (src[i] >= '0' && src[i] <= '9') { 78 0 78
1447 78 ▶ if (e > (LLONG_MAX - 9) / 10) { bad = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (e > (LLONG_MAX - 9) / 10) { bad = 1; break; } 2 76 78
1448 76 e = e * 10 + (src[i] - '0');
1449 }
1450 }
1451 struct tm tm;
1452 7 time_t epoch = (time_t)e;
1453 7 ▶ if (bad || (long long)epoch != e || !ts_gmtime(&epoch, &tm)) {
Branch: 3 branches
BranchTrueFalseTotal
if (bad || (long long)epoch != e || !ts_gmtime(&epoch, &tm)) { 5 2 7
if (bad || (long long)epoch != e || !ts_gmtime(&epoch, &tm)) { 5 0 5
if (bad || (long long)epoch != e || !ts_gmtime(&epoch, &tm)) { 1 4 5
1454 3 ▶ if (!to_output) tmp_len = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!to_output) tmp_len = 0; 0 3 3
1455 3 break;
1456 }
1457 4 ▶ const char *fmt = xf->param_str ? xf->param_str : "%Y-%m-%d %H:%M:%S";
Branch: 1 branch
BranchTrueFalseTotal
const char *fmt = xf->param_str ? xf->param_str : "%Y-%m-%d %H:%M:%S"; 2 2 4
1458 /* strftime reports "too big" as 0, which is also a legitimately empty
1459 result, so grow rather than render nothing */
1460 4 STRLEN cap = 256;
1461 4 size_t w = 0;
1462 while (1) {
1463 5 ▶ OUT_ENSURE(cap);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(cap); 5 0 5
OUT_ENSURE(cap); 1 4 5
OUT_ENSURE(cap); 1 0 1
OUT_ENSURE(cap); 0 1 1
OUT_ENSURE(cap); 0 0 0
OUT_ENSURE(cap); 0 0 0
OUT_ENSURE(cap); 0 0 0
OUT_ENSURE(cap); 0 0 0
OUT_ENSURE(cap); 0 0 0
1464 5 ▶ w = strftime(OUT_PTR, cap, fmt, &tm);
Branch: 1 branch
BranchTrueFalseTotal
w = strftime(OUT_PTR, cap, fmt, &tm); 5 0 5
1465 5 ▶ if (w || cap >= 8192) break;
Branch: 2 branches
BranchTrueFalseTotal
if (w || cap >= 8192) break; 1 4 5
if (w || cap >= 8192) break; 1 0 1
1466 1 cap *= 4;
1467 }
1468 /* %B/%A/%c/%p emit locale text, which in an 8-bit LC_TIME is not UTF-8;
1469 we cannot judge it here, so make the final validation settle it */
1470 4 ▶ if (w) t->r_skipped = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (w) t->r_skipped = 1; 4 0 4
1471 4 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 4 0 4
1472 4 break;
1473 }
1474 73 case XF_SPRINTF: {
1475  
1476 73 ▶ if (!xf->param_str || xf->param_str_len == 0) { /* no format: passthrough */
Branch: 2 branches
BranchTrueFalseTotal
if (!xf->param_str || xf->param_str_len == 0) { /* no format: passthrough */ 72 1 73
if (!xf->param_str || xf->param_str_len == 0) { /* no format: passthrough */ 0 72 72
1477 1 ▶ OUT_PASSTHRU();
Branch: 11 branches
BranchTrueFalseTotal
OUT_PASSTHRU(); 1 0 1
OUT_PASSTHRU(); 0 1 1
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 1 0 1
OUT_PASSTHRU(); 1 0 1
1478 } else {
1479 /* One definition decides what a format means. render_field asks the
1480 same question before numifying the value, and if the two answers
1481 ever differed the value it passes through here would arrive
1482 already converted. */
1483 72 char last = ts_sprintf_conv(xf->param_str);
1484 72 ▶ if (!last) {
Branch: 1 branch
BranchTrueFalseTotal
if (!last) { 29 43 72
1485 29 ▶ OUT_PASSTHRU();
Branch: 11 branches
BranchTrueFalseTotal
OUT_PASSTHRU(); 29 0 29
OUT_PASSTHRU(); 0 29 29
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 29 0 29
OUT_PASSTHRU(); 29 0 29
1486 29 break;
1487 }
1488 char fmtbuf[TS_FMTBUF];
1489 43 int fmtlen = snprintf(fmtbuf, sizeof(fmtbuf), "%s%s",
1490 43 ▶ xf->param_str[0] == '%' ? "" : "%", xf->param_str);
Branch: 1 branch
BranchTrueFalseTotal
xf->param_str[0] == '%' ? "" : "%", xf->param_str); 43 0 43
1491 43 ▶ if (fmtlen < 0 || fmtlen + 2 > (int)sizeof(fmtbuf)) {
Branch: 2 branches
BranchTrueFalseTotal
if (fmtlen < 0 || fmtlen + 2 > (int)sizeof(fmtbuf)) { 43 0 43
if (fmtlen < 0 || fmtlen + 2 > (int)sizeof(fmtbuf)) { 0 43 43
1492 0 ▶ OUT_PASSTHRU();
Branch: 11 branches
BranchTrueFalseTotal
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
1493 0 break;
1494 }
1495 25 ▶ int is_int = (last == 'd' || last == 'i' || last == 'x' ||
Branch: 3 branches
BranchTrueFalseTotal
int is_int = (last == 'd' || last == 'i' || last == 'x' || 24 1 25
int is_int = (last == 'd' || last == 'i' || last == 'x' || 20 4 24
int is_int = (last == 'd' || last == 'i' || last == 'x' || 19 1 20
1496 68 ▶ last == 'X' || last == 'o' || last == 'u');
Branch: 3 branches
BranchTrueFalseTotal
last == 'X' || last == 'o' || last == 'u'); 25 18 43
last == 'X' || last == 'o' || last == 'u'); 17 2 19
last == 'X' || last == 'o' || last == 'u'); 1 16 17
1497 43 ▶ if (is_int) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_int) { 27 16 43
1498 /* Use perl's own length modifier rather than a bare 'l': an IV
1499 is not always a C long (Win64, ILP32 with 64-bit ints), and
1500 there %ld would print half the value we pass. */
1501 27 ▶ const char *conv = (last == 'd' || last == 'i') ? IVdf
Branch: 5 branches
BranchTrueFalseTotal
const char *conv = (last == 'd' || last == 'i') ? IVdf 9 18 27
const char *conv = (last == 'd' || last == 'i') ? IVdf 8 1 9
const char *conv = (last == 'd' || last == 'i') ? IVdf 7 1 8
const char *conv = (last == 'd' || last == 'i') ? IVdf 5 2 7
const char *conv = (last == 'd' || last == 'i') ? IVdf 1 4 5
1502 : (last == 'u') ? UVuf
1503 : (last == 'o') ? UVof
1504 : (last == 'X') ? UVXf : UVxf;
1505 27 size_t clen = strlen(conv);
1506 27 ▶ if ((size_t)fmtlen + clen >= sizeof(fmtbuf)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)fmtlen + clen >= sizeof(fmtbuf)) { 0 27 27
1507 0 ▶ OUT_PASSTHRU();
Branch: 11 branches
BranchTrueFalseTotal
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
1508 0 break;
1509 }
1510 27 memcpy(fmtbuf + fmtlen - 1, conv, clen);
1511 27 fmtlen = fmtlen - 1 + (int)clen;
1512 27 fmtbuf[fmtlen] = '\0';
1513 }
1514 /* %s needs a NUL-terminated copy; only a long value takes the heap */
1515 char stackbuf[256];
1516 43 IV lv = 0; double dv = 0;
1517 43 char *sv_heap = NULL; const char *sv_tb = NULL;
1518 43 ▶ if (is_int) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_int) { 27 16 43
1519 27 int neg = ts_has_minus(src, slen);
1520 27 lv = ts_scan_iv(src, slen, neg);
1521 16 ▶ } else if (last == 'f' || last == 'e' || last == 'g') {
Branch: 3 branches
BranchTrueFalseTotal
} else if (last == 'f' || last == 'e' || last == 'g') { 12 4 16
} else if (last == 'f' || last == 'e' || last == 'g') { 11 1 12
} else if (last == 'f' || last == 'e' || last == 'g') { 0 11 11
1522 5 dv = ts_atof(src, slen);
1523 11 ▶ } else if (slen < sizeof(stackbuf)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (slen < sizeof(stackbuf)) { 10 1 11
1524 /* a precision on %s cuts bytes, and the padded output width is
1525 not the cut offset, so leave it to the final validation */
1526 10 t->r_skipped = 1;
1527 10 memcpy(stackbuf, src, slen); stackbuf[slen] = 0;
1528 10 sv_tb = stackbuf;
1529 } else {
1530 1 t->r_skipped = 1;
1531 1 sv_heap = (char *)malloc(slen + 1);
1532 1 ▶ if (!sv_heap) croak("Text::Stencil: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_heap) croak("Text::Stencil: out of memory"); 0 1 1
1533 1 memcpy(sv_heap, src, slen); sv_heap[slen] = 0;
1534 1 sv_tb = sv_heap;
1535 }
1536 /* size the output from the formatted length */
1537 #define TS_SPRINTF(dst, cap) (is_int ? snprintf(dst, cap, fmtbuf, lv) \
1538 : sv_tb ? snprintf(dst, cap, fmtbuf, sv_tb) : snprintf(dst, cap, fmtbuf, dv))
1539 43 ▶ int w = TS_SPRINTF((char *)NULL, 0);
Branch: 2 branches
BranchTrueFalseTotal
int w = TS_SPRINTF((char *)NULL, 0); 27 16 43
int w = TS_SPRINTF((char *)NULL, 0); 11 5 16
1540 43 ▶ if (w < 0) w = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) w = 0; 0 43 43
1541 43 ▶ OUT_ENSURE((STRLEN)w + 1);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE((STRLEN)w + 1); 42 1 43
OUT_ENSURE((STRLEN)w + 1); 1 41 42
OUT_ENSURE((STRLEN)w + 1); 1 0 1
OUT_ENSURE((STRLEN)w + 1); 0 1 1
OUT_ENSURE((STRLEN)w + 1); 0 1 1
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 1 0 1
OUT_ENSURE((STRLEN)w + 1); 1 0 1
OUT_ENSURE((STRLEN)w + 1); 0 1 1
1542 43 ▶ w = TS_SPRINTF(OUT_PTR, (size_t)w + 1);
Branch: 5 branches
BranchTrueFalseTotal
w = TS_SPRINTF(OUT_PTR, (size_t)w + 1); 27 16 43
w = TS_SPRINTF(OUT_PTR, (size_t)w + 1); 27 0 27
w = TS_SPRINTF(OUT_PTR, (size_t)w + 1); 11 5 16
w = TS_SPRINTF(OUT_PTR, (size_t)w + 1); 10 1 11
w = TS_SPRINTF(OUT_PTR, (size_t)w + 1); 5 0 5
1543 43 ▶ if (w < 0) w = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) w = 0; 0 43 43
1544 #undef TS_SPRINTF
1545 43 ▶ if (sv_heap) free(sv_heap);
Branch: 1 branch
BranchTrueFalseTotal
if (sv_heap) free(sv_heap); 1 42 43
1546 43 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 42 1 43
1547 }
1548 44 break;
1549 }
1550 14 case XF_REPLACE: {
1551 14 ▶ if (!xf->param_str) {
Branch: 1 branch
BranchTrueFalseTotal
if (!xf->param_str) { 1 13 14
1552 1 ▶ OUT_PASSTHRU();
Branch: 11 branches
BranchTrueFalseTotal
OUT_PASSTHRU(); 1 0 1
OUT_PASSTHRU(); 0 1 1
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 1 0 1
OUT_PASSTHRU(); 1 0 1
1553 1 break;
1554 }
1555 13 const char *needle = xf->param_str;
1556 13 STRLEN nlen = xf->param_str_len;
1557 13 ▶ const char *repl = xf->param_str2 ? xf->param_str2 : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *repl = xf->param_str2 ? xf->param_str2 : ""; 13 0 13
1558 13 ▶ STRLEN rlen = xf->param_str2 ? xf->param_str2_len : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN rlen = xf->param_str2 ? xf->param_str2_len : 0; 13 0 13
1559 13 ▶ TS_OUT_ENSURE_MUL(slen, rlen + 1);
Branch: 11 branches
BranchTrueFalseTotal
TS_OUT_ENSURE_MUL(slen, rlen + 1); 13 0 13
TS_OUT_ENSURE_MUL(slen, rlen + 1); 0 13 13
TS_OUT_ENSURE_MUL(slen, rlen + 1); 13 0 13
TS_OUT_ENSURE_MUL(slen, rlen + 1); 1 12 13
TS_OUT_ENSURE_MUL(slen, rlen + 1); 1 0 1
TS_OUT_ENSURE_MUL(slen, rlen + 1); 0 1 1
TS_OUT_ENSURE_MUL(slen, rlen + 1); 0 0 0
TS_OUT_ENSURE_MUL(slen, rlen + 1); 0 0 0
TS_OUT_ENSURE_MUL(slen, rlen + 1); 0 0 0
TS_OUT_ENSURE_MUL(slen, rlen + 1); 0 0 0
TS_OUT_ENSURE_MUL(slen, rlen + 1); 0 0 0
1560 13 ▶ char *out = OUT_PTR;
Branch: 1 branch
BranchTrueFalseTotal
char *out = OUT_PTR; 13 0 13
1561 13 STRLEN opos = 0;
1562 13 STRLEN i = 0;
1563 614 ▶ while (i < slen) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < slen) { 601 13 614
1564 601 ▶ if (i + nlen <= slen && memcmp(src + i, needle, nlen) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (i + nlen <= slen && memcmp(src + i, needle, nlen) == 0) { 600 1 601
if (i + nlen <= slen && memcmp(src + i, needle, nlen) == 0) { 513 87 600
1565 513 memcpy(out + opos, repl, rlen); opos += rlen;
1566 513 i += nlen;
1567 } else {
1568 88 out[opos++] = src[i++];
1569 }
1570 }
1571 13 ▶ OUT_COMMIT(opos);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(opos); 13 0 13
1572 13 break;
1573 }
1574 11 case XF_SUBSTR: {
1575 11 int start = xf->param_int;
1576 11 int maxlen = xf->param_int2;
1577 11 ▶ if (start < 0 || start >= (int)slen) {
Branch: 2 branches
BranchTrueFalseTotal
if (start < 0 || start >= (int)slen) { 11 0 11
if (start < 0 || start >= (int)slen) { 1 10 11
1578 1 ▶ if (!to_output) tmp_len = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!to_output) tmp_len = 0; 0 1 1
1579 1 break;
1580 }
1581 10 const char *s = src + start;
1582 10 STRLEN l = slen - start;
1583 10 ▶ if (maxlen >= 0 && (int)l > maxlen) l = maxlen;
Branch: 2 branches
BranchTrueFalseTotal
if (maxlen >= 0 && (int)l > maxlen) l = maxlen; 6 4 10
if (maxlen >= 0 && (int)l > maxlen) l = maxlen; 6 0 6
1584 10 ▶ if (l) { /* nothing emitted, nothing cut */
Branch: 1 branch
BranchTrueFalseTotal
if (l) { /* nothing emitted, nothing cut */ 9 1 10
1585 9 ▶ TS_NOTE_CUT(t, src, slen, start); /* starts mid-character? */
Branch: 2 branches
BranchTrueFalseTotal
TS_NOTE_CUT(t, src, slen, start); /* starts mid-character? */ 9 0 9
TS_NOTE_CUT(t, src, slen, start); /* starts mid-character? */ 1 8 9
1586 9 ▶ TS_NOTE_CUT(t, src, slen, start + l); /* ends mid-character? */
Branch: 2 branches
BranchTrueFalseTotal
TS_NOTE_CUT(t, src, slen, start + l); /* ends mid-character? */ 5 4 9
TS_NOTE_CUT(t, src, slen, start + l); /* ends mid-character? */ 1 4 5
1587 }
1588 10 ▶ OUT_ENSURE(l); memcpy(OUT_PTR, s, l);
Branch: 10 branches
BranchTrueFalseTotal
OUT_ENSURE(l); memcpy(OUT_PTR, s, l); 10 0 10
OUT_ENSURE(l); memcpy(OUT_PTR, s, l); 0 10 10
OUT_ENSURE(l); memcpy(OUT_PTR, s, l); 0 0 0
OUT_ENSURE(l); memcpy(OUT_PTR, s, l); 0 0 0
OUT_ENSURE(l); memcpy(OUT_PTR, s, l); 0 0 0
OUT_ENSURE(l); memcpy(OUT_PTR, s, l); 0 0 0
OUT_ENSURE(l); memcpy(OUT_PTR, s, l); 0 0 0
OUT_ENSURE(l); memcpy(OUT_PTR, s, l); 0 0 0
OUT_ENSURE(l); memcpy(OUT_PTR, s, l); 0 0 0
OUT_ENSURE(l); memcpy(OUT_PTR, s, l); 10 0 10
1589 10 ▶ OUT_COMMIT(l);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(l); 10 0 10
1590 10 break;
1591 }
1592 11 case XF_PLURAL: {
1593 /* a leading .-. is the sign; -1 is plural, and itoa keeps the .-. */
1594 11 ▶ STRLEN pi = (slen > 0 && src[0] == '-') ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
STRLEN pi = (slen > 0 && src[0] == '-') ? 1 : 0; 11 0 11
STRLEN pi = (slen > 0 && src[0] == '-') ? 1 : 0; 3 8 11
1595 11 IV v = ts_scan_iv(src + pi, slen - pi, pi ? 1 : 0);
1596 const char *form; STRLEN flen;
1597 11 ▶ if (v == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (v == 1) { 4 7 11
1598 4 ▶ form = xf->param_str ? xf->param_str : ""; flen = xf->param_str_len;
Branch: 1 branch
BranchTrueFalseTotal
form = xf->param_str ? xf->param_str : ""; flen = xf->param_str_len; 4 0 4
1599 } else {
1600 7 ▶ form = xf->param_str2 ? xf->param_str2 : "s"; flen = xf->param_str2 ? xf->param_str2_len : 1;
Branch: 2 branches
BranchTrueFalseTotal
form = xf->param_str2 ? xf->param_str2 : "s"; flen = xf->param_str2 ? xf->param_str2_len : 1; 7 0 7
form = xf->param_str2 ? xf->param_str2 : "s"; flen = xf->param_str2 ? xf->param_str2_len : 1; 7 0 7
1601 }
1602 11 ▶ OUT_ENSURE(20 + 1 + flen);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(20 + 1 + flen); 11 0 11
OUT_ENSURE(20 + 1 + flen); 0 11 11
OUT_ENSURE(20 + 1 + flen); 0 0 0
OUT_ENSURE(20 + 1 + flen); 0 0 0
OUT_ENSURE(20 + 1 + flen); 0 0 0
OUT_ENSURE(20 + 1 + flen); 0 0 0
OUT_ENSURE(20 + 1 + flen); 0 0 0
OUT_ENSURE(20 + 1 + flen); 0 0 0
OUT_ENSURE(20 + 1 + flen); 0 0 0
1603 11 ▶ int nw = itoa_fast(OUT_PTR, v);
Branch: 1 branch
BranchTrueFalseTotal
int nw = itoa_fast(OUT_PTR, v); 11 0 11
1604 11 ▶ OUT_PTR[nw] = ' ';
Branch: 1 branch
BranchTrueFalseTotal
OUT_PTR[nw] = ' '; 11 0 11
1605 11 ▶ memcpy(OUT_PTR + nw + 1, form, flen);
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR + nw + 1, form, flen); 11 0 11
1606 11 STRLEN total = (STRLEN)nw + 1 + flen;
1607 11 ▶ OUT_COMMIT(total);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(total); 11 0 11
1608 11 break;
1609 }
1610 7 case XF_IF: {
1611 7 ▶ int truthy = (slen > 0 && !(slen == 1 && src[0] == '0'));
Branch: 3 branches
BranchTrueFalseTotal
int truthy = (slen > 0 && !(slen == 1 && src[0] == '0')); 5 2 7
int truthy = (slen > 0 && !(slen == 1 && src[0] == '0')); 5 0 5
int truthy = (slen > 0 && !(slen == 1 && src[0] == '0')); 3 2 5
1612 7 ▶ if (truthy && xf->param_str) {
Branch: 2 branches
BranchTrueFalseTotal
if (truthy && xf->param_str) { 3 4 7
if (truthy && xf->param_str) { 3 0 3
1613 3 ▶ OUT_ENSURE(xf->param_str_len);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(xf->param_str_len); 3 0 3
OUT_ENSURE(xf->param_str_len); 0 3 3
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
1614 3 ▶ memcpy(OUT_PTR, xf->param_str, xf->param_str_len);
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR, xf->param_str, xf->param_str_len); 3 0 3
1615 3 ▶ OUT_COMMIT(xf->param_str_len);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(xf->param_str_len); 3 0 3
1616 } else {
1617 4 ▶ if (!to_output) tmp_len = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!to_output) tmp_len = 0; 0 4 4
1618 }
1619 7 break;
1620 }
1621 2 case XF_UNLESS: {
1622 2 ▶ int truthy = (slen > 0 && !(slen == 1 && src[0] == '0'));
Branch: 3 branches
BranchTrueFalseTotal
int truthy = (slen > 0 && !(slen == 1 && src[0] == '0')); 1 1 2
int truthy = (slen > 0 && !(slen == 1 && src[0] == '0')); 1 0 1
int truthy = (slen > 0 && !(slen == 1 && src[0] == '0')); 1 0 1
1623 2 ▶ if (!truthy && xf->param_str) {
Branch: 2 branches
BranchTrueFalseTotal
if (!truthy && xf->param_str) { 1 1 2
if (!truthy && xf->param_str) { 1 0 1
1624 1 ▶ OUT_ENSURE(xf->param_str_len);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(xf->param_str_len); 1 0 1
OUT_ENSURE(xf->param_str_len); 0 1 1
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
OUT_ENSURE(xf->param_str_len); 0 0 0
1625 1 ▶ memcpy(OUT_PTR, xf->param_str, xf->param_str_len);
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR, xf->param_str, xf->param_str_len); 1 0 1
1626 1 ▶ OUT_COMMIT(xf->param_str_len);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(xf->param_str_len); 1 0 1
1627 } else {
1628 1 ▶ if (!to_output) tmp_len = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!to_output) tmp_len = 0; 0 1 1
1629 }
1630 2 break;
1631 }
1632 12 case XF_MAP: {
1633 12 const char *val = src; STRLEN vlen = slen;
1634 31 ▶ for (int mi = 0; mi < xf->map_count; mi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int mi = 0; mi < xf->map_count; mi++) { 25 6 31
1635 25 ▶ if ((xf->map_key_lens[mi] == slen && memcmp(xf->map_keys[mi], src, slen) == 0) ||
Branch: 2 branches
BranchTrueFalseTotal
if ((xf->map_key_lens[mi] == slen && memcmp(xf->map_keys[mi], src, slen) == 0) || 21 4 25
if ((xf->map_key_lens[mi] == slen && memcmp(xf->map_keys[mi], src, slen) == 0) || 15 6 21
1636 19 ▶ (xf->map_key_lens[mi] == 1 && xf->map_keys[mi][0] == '*')) {
Branch: 2 branches
BranchTrueFalseTotal
(xf->map_key_lens[mi] == 1 && xf->map_keys[mi][0] == '*')) { 19 0 19
(xf->map_key_lens[mi] == 1 && xf->map_keys[mi][0] == '*')) { 6 13 19
1637 12 val = xf->map_vals[mi]; vlen = xf->map_val_lens[mi];
1638 12 ▶ if (xf->map_key_lens[mi] != 1 || xf->map_keys[mi][0] != '*') break;
Branch: 2 branches
BranchTrueFalseTotal
if (xf->map_key_lens[mi] != 1 || xf->map_keys[mi][0] != '*') break; 12 0 12
if (xf->map_key_lens[mi] != 1 || xf->map_keys[mi][0] != '*') break; 6 6 12
1639 }
1640 }
1641 12 ▶ OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen);
Branch: 10 branches
BranchTrueFalseTotal
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 12 0 12
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 12 12
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 0 0 0
OUT_ENSURE(vlen); memcpy(OUT_PTR, val, vlen); 12 0 12
1642 12 ▶ OUT_COMMIT(vlen);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(vlen); 12 0 12
1643 12 break;
1644 }
1645 13 case XF_WRAP: {
1646 13 ▶ if (slen > 0 && !xf->param_str) {
Branch: 2 branches
BranchTrueFalseTotal
if (slen > 0 && !xf->param_str) { 8 5 13
if (slen > 0 && !xf->param_str) { 1 7 8
1647 /* no prefix/suffix given: pass the value through */
1648 1 ▶ OUT_PASSTHRU();
Branch: 11 branches
BranchTrueFalseTotal
OUT_PASSTHRU(); 1 0 1
OUT_PASSTHRU(); 0 1 1
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 1 0 1
OUT_PASSTHRU(); 1 0 1
1649 19 ▶ } else if (slen > 0 && xf->param_str) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (slen > 0 && xf->param_str) { 7 5 12
} else if (slen > 0 && xf->param_str) { 7 0 7
1650 7 ▶ STRLEN plen2 = xf->param_str_len + slen + (xf->param_str2 ? xf->param_str2_len : 0);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN plen2 = xf->param_str_len + slen + (xf->param_str2 ? xf->param_str2_len : 0); 5 2 7
1651 7 ▶ OUT_ENSURE(plen2);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(plen2); 7 0 7
OUT_ENSURE(plen2); 1 6 7
OUT_ENSURE(plen2); 1 0 1
OUT_ENSURE(plen2); 0 1 1
OUT_ENSURE(plen2); 0 0 0
OUT_ENSURE(plen2); 0 0 0
OUT_ENSURE(plen2); 0 0 0
OUT_ENSURE(plen2); 0 0 0
OUT_ENSURE(plen2); 0 0 0
1652 7 ▶ memcpy(OUT_PTR, xf->param_str, xf->param_str_len);
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR, xf->param_str, xf->param_str_len); 7 0 7
1653 /* STRLEN, not int: the guard above caps slen at INT_MAX, but this
1654 is the one accumulator that ADDS to slen, so a prefix on a value
1655 near the cap overflowed to negative and turned the length into
1656 ~SIZE_MAX. Reachable without a 2GB scalar in hand: hex doubles a
1657 1GB field to just under INT_MAX before wrap ever sees it. */
1658 7 STRLEN wpos = xf->param_str_len;
1659 7 ▶ memcpy(OUT_PTR + wpos, src, slen); wpos += slen;
Branch: 1 branch
BranchTrueFalseTotal
memcpy(OUT_PTR + wpos, src, slen); wpos += slen; 7 0 7
1660 7 ▶ if (xf->param_str2) { memcpy(OUT_PTR + wpos, xf->param_str2, xf->param_str2_len); wpos += xf->param_str2_len; }
Branch: 2 branches
BranchTrueFalseTotal
if (xf->param_str2) { memcpy(OUT_PTR + wpos, xf->param_str2, xf->param_str2_len); wpos += xf->param_str2_len; } 5 2 7
if (xf->param_str2) { memcpy(OUT_PTR + wpos, xf->param_str2, xf->param_str2_len); wpos += xf->param_str2_len; } 5 0 5
1661 7 ▶ OUT_COMMIT(wpos);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(wpos); 7 0 7
1662 } else {
1663 5 ▶ if (!to_output) tmp_len = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!to_output) tmp_len = 0; 0 5 5
1664 }
1665 13 break;
1666 }
1667 7 case XF_NUMBER_SI: {
1668 7 double v = ts_atof(src, slen);
1669 { /* a 32-byte cap chopped huge values mid-digit and dropped the suffix */
1670 7 int ti = si_tier(v, si_dec, 5);
1671 5 int w = ti ? snprintf(NULL, 0, "%.1f%s", v / si_dec[ti], si_dec_suf[ti])
1672 7 ▶ : snprintf(NULL, 0, "%.0f", v);
Branch: 1 branch
BranchTrueFalseTotal
: snprintf(NULL, 0, "%.0f", v); 5 2 7
1673 7 ▶ if (w < 0) w = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) w = 0; 0 7 7
1674 7 ▶ OUT_ENSURE((STRLEN)w + 1);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE((STRLEN)w + 1); 7 0 7
OUT_ENSURE((STRLEN)w + 1); 0 7 7
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
1675 5 ▶ w = ti ? snprintf(OUT_PTR, (size_t)w + 1, "%.1f%s", v / si_dec[ti], si_dec_suf[ti])
Branch: 1 branch
BranchTrueFalseTotal
w = ti ? snprintf(OUT_PTR, (size_t)w + 1, "%.1f%s", v / si_dec[ti], si_dec_suf[ti]) 5 0 5
1676 7 ▶ : snprintf(OUT_PTR, (size_t)w + 1, "%.0f", v);
Branch: 2 branches
BranchTrueFalseTotal
: snprintf(OUT_PTR, (size_t)w + 1, "%.0f", v); 5 2 7
: snprintf(OUT_PTR, (size_t)w + 1, "%.0f", v); 2 0 2
1677 7 ▶ if (w < 0) w = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) w = 0; 0 7 7
1678 7 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 7 0 7
1679 }
1680 7 break;
1681 }
1682 9 case XF_BYTES_SI: {
1683 9 double v = ts_atof(src, slen);
1684 {
1685 9 int ti = si_tier(v, si_bin, 6);
1686 7 int w = ti ? snprintf(NULL, 0, "%.1f %s", v / si_bin[ti], si_bin_suf[ti])
1687 9 ▶ : snprintf(NULL, 0, "%.0f B", v);
Branch: 1 branch
BranchTrueFalseTotal
: snprintf(NULL, 0, "%.0f B", v); 7 2 9
1688 9 ▶ if (w < 0) w = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) w = 0; 0 9 9
1689 9 ▶ OUT_ENSURE((STRLEN)w + 1);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE((STRLEN)w + 1); 9 0 9
OUT_ENSURE((STRLEN)w + 1); 0 9 9
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
OUT_ENSURE((STRLEN)w + 1); 0 0 0
1690 7 ▶ w = ti ? snprintf(OUT_PTR, (size_t)w + 1, "%.1f %s", v / si_bin[ti], si_bin_suf[ti])
Branch: 1 branch
BranchTrueFalseTotal
w = ti ? snprintf(OUT_PTR, (size_t)w + 1, "%.1f %s", v / si_bin[ti], si_bin_suf[ti]) 7 0 7
1691 9 ▶ : snprintf(OUT_PTR, (size_t)w + 1, "%.0f B", v);
Branch: 2 branches
BranchTrueFalseTotal
: snprintf(OUT_PTR, (size_t)w + 1, "%.0f B", v); 7 2 9
: snprintf(OUT_PTR, (size_t)w + 1, "%.0f B", v); 2 0 2
1692 9 ▶ if (w < 0) w = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) w = 0; 0 9 9
1693 9 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 9 0 9
1694 }
1695 9 break;
1696 }
1697 11 case XF_ELAPSED: {
1698 /* junk in, but not negative out */
1699 11 IV v = ts_scan_iv(src, slen, 0);
1700 11 ▶ OUT_ENSURE(64);
Branch: 7 branches
BranchTrueFalseTotal
OUT_ENSURE(64); 11 0 11
OUT_ENSURE(64); 0 11 11
OUT_ENSURE(64); 0 0 0
OUT_ENSURE(64); 0 0 0
OUT_ENSURE(64); 0 0 0
OUT_ENSURE(64); 0 0 0
OUT_ENSURE(64); 0 0 0
1701 11 int w = 0;
1702 11 ▶ if (v >= 86400 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "d ", v / 86400); if (n > 0) w += n; v %= 86400; }
Branch: 4 branches
BranchTrueFalseTotal
if (v >= 86400 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "d ", v / 86400); if (n > 0) w += n; v %= 86400; } 6 5 11
if (v >= 86400 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "d ", v / 86400); if (n > 0) w += n; v %= 86400; } 6 0 6
if (v >= 86400 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "d ", v / 86400); if (n > 0) w += n; v %= 86400; } 6 0 6
if (v >= 86400 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "d ", v / 86400); if (n > 0) w += n; v %= 86400; } 6 0 6
1703 11 ▶ if (v >= 3600 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "h ", v / 3600); if (n > 0) w += n; v %= 3600; }
Branch: 4 branches
BranchTrueFalseTotal
if (v >= 3600 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "h ", v / 3600); if (n > 0) w += n; v %= 3600; } 7 4 11
if (v >= 3600 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "h ", v / 3600); if (n > 0) w += n; v %= 3600; } 7 0 7
if (v >= 3600 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "h ", v / 3600); if (n > 0) w += n; v %= 3600; } 7 0 7
if (v >= 3600 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "h ", v / 3600); if (n > 0) w += n; v %= 3600; } 7 0 7
1704 11 ▶ if (v >= 60 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "m ", v / 60); if (n > 0) w += n; v %= 60; }
Branch: 4 branches
BranchTrueFalseTotal
if (v >= 60 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "m ", v / 60); if (n > 0) w += n; v %= 60; } 7 4 11
if (v >= 60 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "m ", v / 60); if (n > 0) w += n; v %= 60; } 7 0 7
if (v >= 60 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "m ", v / 60); if (n > 0) w += n; v %= 60; } 7 0 7
if (v >= 60 && w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "m ", v / 60); if (n > 0) w += n; v %= 60; } 7 0 7
1705 11 ▶ if (w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "s", v); if (n > 0) w += n; }
Branch: 3 branches
BranchTrueFalseTotal
if (w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "s", v); if (n > 0) w += n; } 11 0 11
if (w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "s", v); if (n > 0) w += n; } 11 0 11
if (w < 63) { int n = snprintf(OUT_PTR + w, 64 - w, "%" IVdf "s", v); if (n > 0) w += n; } 11 0 11
1706 11 ▶ if (w > 63) w = 63;
Branch: 1 branch
BranchTrueFalseTotal
if (w > 63) w = 63; 0 11 11
1707 11 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 11 0 11
1708 11 break;
1709 }
1710 4 case XF_AGO: {
1711 4 time_t now = time(NULL);
1712 4 long long e = 0;
1713 35 ▶ for (STRLEN i = 0; i < slen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN i = 0; i < slen; i++) 31 4 35
1714 31 ▶ if (src[i] >= '0' && src[i] <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (src[i] >= '0' && src[i] <= '9') { 31 0 31
if (src[i] >= '0' && src[i] <= '9') { 31 0 31
1715 31 ▶ if (e > (LLONG_MAX - 9) / 10) break;
Branch: 1 branch
BranchTrueFalseTotal
if (e > (LLONG_MAX - 9) / 10) break; 0 31 31
1716 31 e = e * 10 + (src[i] - '0');
1717 }
1718 4 time_t epoch = (time_t)e;
1719 /* the same round-trip date does: where time_t is 32 bits an epoch past
1720 2038 silently wraps negative, and "124y ago" is a worse answer than
1721 admitting the value is out of range */
1722 4 ▶ if ((long long)epoch != e) {
Branch: 1 branch
BranchTrueFalseTotal
if ((long long)epoch != e) { 0 4 4
1723 0 ▶ if (!to_output) tmp_len = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!to_output) tmp_len = 0; 0 0 0
1724 0 break;
1725 }
1726 4 IV diff = (IV)(now - epoch);
1727 4 ▶ OUT_ENSURE(32);
Branch: 7 branches
BranchTrueFalseTotal
OUT_ENSURE(32); 4 0 4
OUT_ENSURE(32); 0 4 4
OUT_ENSURE(32); 0 0 0
OUT_ENSURE(32); 0 0 0
OUT_ENSURE(32); 0 0 0
OUT_ENSURE(32); 0 0 0
OUT_ENSURE(32); 0 0 0
1728 int w;
1729 4 ▶ if (diff < 0) w = snprintf(OUT_PTR, 32, "in the future");
Branch: 2 branches
BranchTrueFalseTotal
if (diff < 0) w = snprintf(OUT_PTR, 32, "in the future"); 1 3 4
if (diff < 0) w = snprintf(OUT_PTR, 32, "in the future"); 1 0 1
1730 3 ▶ else if (diff < 60) w = snprintf(OUT_PTR, 32, "%" IVdf "s ago", diff);
Branch: 2 branches
BranchTrueFalseTotal
else if (diff < 60) w = snprintf(OUT_PTR, 32, "%" IVdf "s ago", diff); 0 3 3
else if (diff < 60) w = snprintf(OUT_PTR, 32, "%" IVdf "s ago", diff); 0 0 0
1731 3 ▶ else if (diff < 3600) w = snprintf(OUT_PTR, 32, "%" IVdf "m ago", diff / 60);
Branch: 2 branches
BranchTrueFalseTotal
else if (diff < 3600) w = snprintf(OUT_PTR, 32, "%" IVdf "m ago", diff / 60); 1 2 3
else if (diff < 3600) w = snprintf(OUT_PTR, 32, "%" IVdf "m ago", diff / 60); 1 0 1
1732 2 ▶ else if (diff < 86400) w = snprintf(OUT_PTR, 32, "%" IVdf "h ago", diff / 3600);
Branch: 2 branches
BranchTrueFalseTotal
else if (diff < 86400) w = snprintf(OUT_PTR, 32, "%" IVdf "h ago", diff / 3600); 1 1 2
else if (diff < 86400) w = snprintf(OUT_PTR, 32, "%" IVdf "h ago", diff / 3600); 1 0 1
1733 1 ▶ else if (diff < 2592000) w = snprintf(OUT_PTR, 32, "%" IVdf "d ago", diff / 86400);
Branch: 2 branches
BranchTrueFalseTotal
else if (diff < 2592000) w = snprintf(OUT_PTR, 32, "%" IVdf "d ago", diff / 86400); 0 1 1
else if (diff < 2592000) w = snprintf(OUT_PTR, 32, "%" IVdf "d ago", diff / 86400); 0 0 0
1734 1 ▶ else if (diff < 31536000) w = snprintf(OUT_PTR, 32, "%" IVdf "mo ago", diff / 2592000);
Branch: 2 branches
BranchTrueFalseTotal
else if (diff < 31536000) w = snprintf(OUT_PTR, 32, "%" IVdf "mo ago", diff / 2592000); 0 1 1
else if (diff < 31536000) w = snprintf(OUT_PTR, 32, "%" IVdf "mo ago", diff / 2592000); 0 0 0
1735 1 ▶ else w = snprintf(OUT_PTR, 32, "%" IVdf "y ago", diff / 31536000);
Branch: 1 branch
BranchTrueFalseTotal
else w = snprintf(OUT_PTR, 32, "%" IVdf "y ago", diff / 31536000); 1 0 1
1736 4 ▶ if (w > 31) w = 31;
Branch: 1 branch
BranchTrueFalseTotal
if (w > 31) w = 31; 0 4 4
1737 4 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 4 0 4
1738 4 break;
1739 }
1740 11 case XF_MASK: {
1741 /* clamp so the memset can never exceed the slen bytes reserved above */
1742 11 int keep = xf->param_int;
1743 11 ▶ if (keep < 0) keep = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (keep < 0) keep = 0; 0 11 11
1744 11 ▶ if (keep > (int)slen) keep = (int)slen;
Branch: 1 branch
BranchTrueFalseTotal
if (keep > (int)slen) keep = (int)slen; 1 10 11
1745 11 ▶ OUT_ENSURE(slen);
Branch: 9 branches
BranchTrueFalseTotal
OUT_ENSURE(slen); 10 1 11
OUT_ENSURE(slen); 0 10 10
OUT_ENSURE(slen); 0 0 0
OUT_ENSURE(slen); 0 0 0
OUT_ENSURE(slen); 0 1 1
OUT_ENSURE(slen); 0 0 0
OUT_ENSURE(slen); 1 0 1
OUT_ENSURE(slen); 1 0 1
OUT_ENSURE(slen); 0 1 1
1746 11 int mask_len = (int)slen - keep;
1747 11 ▶ if (mask_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mask_len > 0) { 10 1 11
1748 10 ▶ TS_NOTE_CUT(t, src, slen, mask_len);
Branch: 2 branches
BranchTrueFalseTotal
TS_NOTE_CUT(t, src, slen, mask_len); 9 1 10
TS_NOTE_CUT(t, src, slen, mask_len); 1 8 9
1749 10 ▶ memset(OUT_PTR, '*', mask_len);
Branch: 1 branch
BranchTrueFalseTotal
memset(OUT_PTR, '*', mask_len); 9 1 10
1750 }
1751 11 ▶ if (keep > 0) memcpy(OUT_PTR + mask_len, src + slen - keep, keep);
Branch: 2 branches
BranchTrueFalseTotal
if (keep > 0) memcpy(OUT_PTR + mask_len, src + slen - keep, keep); 10 1 11
if (keep > 0) memcpy(OUT_PTR + mask_len, src + slen - keep, keep); 9 1 10
1752 11 int total = (int)slen;
1753 11 ▶ OUT_COMMIT(total);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(total); 10 1 11
1754 11 break;
1755 }
1756 3 case XF_LENGTH: {
1757 3 ▶ OUT_ENSURE(20);
Branch: 7 branches
BranchTrueFalseTotal
OUT_ENSURE(20); 3 0 3
OUT_ENSURE(20); 0 3 3
OUT_ENSURE(20); 0 0 0
OUT_ENSURE(20); 0 0 0
OUT_ENSURE(20); 0 0 0
OUT_ENSURE(20); 0 0 0
OUT_ENSURE(20); 0 0 0
1758 3 ▶ int w = itoa_fast(OUT_PTR, (IV)slen);
Branch: 1 branch
BranchTrueFalseTotal
int w = itoa_fast(OUT_PTR, (IV)slen); 3 0 3
1759 3 ▶ OUT_COMMIT(w);
Branch: 1 branch
BranchTrueFalseTotal
OUT_COMMIT(w); 3 0 3
1760 3 break;
1761 }
1762 673872 case XF_COALESCE: /* handled in render_field, fallthrough to raw */
1763 case XF_DEFAULT:
1764 case XF_RAW: {
1765 673872 ▶ OUT_PASSTHRU();
Branch: 11 branches
BranchTrueFalseTotal
OUT_PASSTHRU(); 673831 41 673872
OUT_PASSTHRU(); 12 673819 673831
OUT_PASSTHRU(); 12 0 12
OUT_PASSTHRU(); 0 12 12
OUT_PASSTHRU(); 0 41 41
OUT_PASSTHRU(); 0 0 0
OUT_PASSTHRU(); 41 0 41
OUT_PASSTHRU(); 41 0 41
OUT_PASSTHRU(); 0 41 41
OUT_PASSTHRU(); 673831 41 673872
OUT_PASSTHRU(); 673831 41 673872
1766 673872 break;
1767 }
1768 }
1769  
1770 676021 done:
1771 #undef OUT_ENSURE
1772 #undef OUT_PTR
1773 676021 *bufp = buf; *posp = pos; *allocp = alloc;
1774 676021 *tmpp = tmp; *tmp_lenp = tmp_len; *tmp_allocp = tmp_alloc;
1775 676021 }
1776  
1777 /* How a field value constrains the output encoding. Scanning an unflagged
1778 value only tells us anything once something character-ish is in play, so
1779 until then just note that one went by; tpl_out_sv settles the remainder. */
1780 #define TS_NOTE_SV(t, sv, s, l) do { \
1781 if (SvUTF8(sv)) (t)->r_utf8 = 1; \
1782 else if (!(t)->r_high && (l) && has_high_byte(s, l)) { \
1783 /* an all-ASCII value cannot make the output malformed, so only a high \
1784 byte is worth remembering -- deferring on every value instead cost a \
1785 whole-output is_utf8_string scan per render on mixed data */ \
1786 if ((t)->r_utf8 || (t)->tpl_utf8) (t)->r_high = 1; \
1787 else (t)->r_skipped = 1; \
1788 } \
1789 } while(0)
1790  
1791 /* r_utf8/r_high/r_skipped describe one render, but they live on the object, so
1792 a nested render -- an overloaded "", a tied FETCH, a callback that renders
1793 again on the same object -- used to zero the outer render's evidence and let
1794 it flag malformed output. Stack the state on the C frame instead. */
1795 /* On the savestack, not the C frame: a nested render that croaks and is caught
1796 by the Perl that called it would otherwise leave the outer render looking at
1797 the inner one's zeroed state, and flag malformed output as characters. */
1798 #define TS_ENC_ENTER(t) do { \
1799 ENTER; \
1800 SAVEINT((t)->r_utf8); SAVEINT((t)->r_high); SAVEINT((t)->r_skipped); \
1801 (t)->r_utf8 = 0; (t)->r_high = 0; (t)->r_skipped = 0; \
1802 } while(0)
1803 #define TS_ENC_LEAVE(t) LEAVE
1804  
1805 /* Flag the result UTF-8 only if something character-ish contributed and the
1806 assembled bytes really are well-formed: byte-level trunc/substr/mask can cut
1807 a sequence in half, and byte-string fields must survive a render unchanged. */
1808 82305 static SV *tpl_out_sv(pTHX_ tpl_compiled *t, const char *buf, STRLEN len) {
1809 /* r_high means something ruled a character result out: a high byte from an
1810 unflagged value, or a transform that sliced a character in half */
1811 82305 ▶ int utf8 = (t->r_utf8 || t->tpl_utf8) && !(t->r_high || t->tpl_high);
Branch: 4 branches
BranchTrueFalseTotal
int utf8 = (t->r_utf8 || t->tpl_utf8) && !(t->r_high || t->tpl_high); 82267 38 82305
int utf8 = (t->r_utf8 || t->tpl_utf8) && !(t->r_high || t->tpl_high); 27 82240 82267
int utf8 = (t->r_utf8 || t->tpl_utf8) && !(t->r_high || t->tpl_high); 55 10 65
int utf8 = (t->r_utf8 || t->tpl_utf8) && !(t->r_high || t->tpl_high); 51 4 55
1812 /* values went past unexamined; settle it once here. The length guard is
1813 load-bearing: is_utf8_string() reads len == 0 as "call strlen()", and
1814 the buffer is not terminated at pos. */
1815 82305 ▶ if (utf8 && t->r_skipped && len) utf8 = is_utf8_string((const U8 *)buf, len);
Branch: 3 branches
BranchTrueFalseTotal
if (utf8 && t->r_skipped && len) utf8 = is_utf8_string((const U8 *)buf, len); 51 82254 82305
if (utf8 && t->r_skipped && len) utf8 = is_utf8_string((const U8 *)buf, len); 12 39 51
if (utf8 && t->r_skipped && len) utf8 = is_utf8_string((const U8 *)buf, len); 10 2 12
1816 82305 ▶ return newSVpvn_utf8(buf, len, utf8);
Branch: 1 branch
BranchTrueFalseTotal
return newSVpvn_utf8(buf, len, utf8); 43 82262 82305
1817 }
1818  
1819 /* A field selector of the wrong kind for the template fetches nothing from any
1820 row: the sort comes back in input order, skip_if never fires, and
1821 skip_unless drops everything and returns "". saw_num_ref distinguishes
1822 "ROW_ARRAY because {0} was seen" from "ROW_ARRAY because there are no field
1823 references at all", where either kind is meaningless and neither is refused.
1824 `own` is the template when nothing owns it yet: new() and clone() check after
1825 tpl_compile built it, so croaking without freeing would strand it.
1826 render_sorted passes NULL -- by then the object owns it. */
1827 #define TS_CHECK_KIND(own, what, by_name) STMT_START { \
1828 if ((by_name) && t->saw_num_ref) { \
1829 if (own) tpl_free(own); \
1830 croak("Text::Stencil: %s was given a field name but the template uses" \
1831 " numeric field references", (what)); \
1832 } \
1833 if (!(by_name) && t->mode == ROW_HASH) { \
1834 if (own) tpl_free(own); \
1835 croak("Text::Stencil: %s was given a column index but the template uses" \
1836 " named field references", (what)); \
1837 } \
1838 } STMT_END
1839 #define TS_CHECK_SORT_KIND(by_name) TS_CHECK_KIND(NULL, "render_sorted", (by_name))
1840  
1841 /* Fetch row i, or decide why the slot is empty. A slot past the end means
1842 user code shrank the array mid-render, so the caller stops; within the
1843 array it is a hole, which Perl reads as undef and which renders as an empty
1844 row like any other non-arrayref. Only consulted when the slot is empty, so
1845 the common path is free. Returns 0 to tell the caller to stop. */
1846 565612 PERL_STATIC_INLINE int ts_row_at(pTHX_ AV *rows, SSize_t i, SV **out) {
1847 565612 SV **rowref = av_fetch(rows, i, 0);
1848 565612 ▶ if (LIKELY(rowref && *rowref)) { *out = *rowref; return 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (LIKELY(rowref && *rowref)) { *out = *rowref; return 1; } 564609 1003 565612
if (LIKELY(rowref && *rowref)) { *out = *rowref; return 1; } 564609 0 564609
1849 1003 ▶ if (i > av_top_index(rows)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i > av_top_index(rows)) return 0; 3 1000 1003
1850 1000 *out = &PL_sv_undef;
1851 1000 return 1;
1852 }
1853  
1854 /* Emit one row through the compiled ops. */
1855 #define TS_EMIT_OPS(row_sv, idx) do { \
1856 for (int j_ = 0; j_ < t->nops; j_++) { \
1857 tpl_op *op_ = &t->ops[j_]; \
1858 if (op_->static_data) BUF_WRITE(op_->static_data, op_->static_len); \
1859 else render_field(aTHX_ t, op_, (row_sv), &buf, &pos, &alloc, (idx)); \
1860 } \
1861 } while (0)
1862  
1863 /* Write straight to the render buffer and hand the moved pointers back. The
1864 four fast-path exits in render_field all did this by hand. */
1865 #define EMIT_DIRECT(s, n) do { \
1866 char *buf = *bufp; STRLEN pos = *posp; STRLEN alloc = *allocp; \
1867 BUF_ENSURE(n); memcpy(buf + pos, (s), (n)); pos += (n); \
1868 *bufp = buf; *posp = pos; *allocp = alloc; \
1869 } while (0)
1870  
1871 714047 static void render_field(pTHX_ tpl_compiled *t, tpl_op *op, SV *row_sv,
1872 char **bufp, STRLEN *posp, STRLEN *allocp,
1873 SSize_t row_idx) {
1874 714047 enum row_mode mode = t->mode;
1875 714047 const char *src = NULL; STRLEN slen = 0;
1876 714047 int use_default = 0;
1877 /* src is read after the blocks that fill these, so they need whole-function
1878 scope -- declared inline they were a use-after-scope */
1879 714047 char rownum_buf[20]; int rownum_len = 0;
1880 char cbuf[12];
1881 char ibuf[28]; /* itoa_comma needs up to 26 ("-9,223,372,036,854,775,808") */
1882 char fbuf[TS_FLTSZ];
1883  
1884 714047 ▶ if (op->is_rownum) {
Branch: 1 branch
BranchTrueFalseTotal
if (op->is_rownum) { 1015 713032 714047
1885 1015 rownum_len = itoa_fast(rownum_buf, (IV)row_idx);
1886 1015 src = rownum_buf; slen = rownum_len;
1887 1015 use_default = 1; /* skip fetch_field path, go straight to chain */
1888 }
1889  
1890 714047 SV *sv = NULL;
1891 714047 ▶ if (!op->is_rownum) {
Branch: 1 branch
BranchTrueFalseTotal
if (!op->is_rownum) { 713032 1015 714047
1892 713032 sv = fetch_field(aTHX_ row_sv, op, mode);
1893  
1894 /* handle default transform */
1895 686832 ▶ if (!sv || !SvOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) { 685226 1606 686832
if (!sv || !SvOK(sv)) { 35 685191 685226
1896 3271 ▶ for (int i = 0; i < op->chain_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < op->chain_len; i++) { 1645 1626 3271
1897 1645 ▶ if (op->chain[i].type == XF_DEFAULT && op->chain[i].param_str) {
Branch: 2 branches
BranchTrueFalseTotal
if (op->chain[i].type == XF_DEFAULT && op->chain[i].param_str) { 7 1638 1645
if (op->chain[i].type == XF_DEFAULT && op->chain[i].param_str) { 7 0 7
1898 7 src = op->chain[i].param_str;
1899 7 slen = op->chain[i].param_str_len;
1900 7 use_default = 1;
1901 7 break;
1902 }
1903 1638 ▶ if (op->chain[i].type == XF_COALESCE) {
Branch: 1 branch
BranchTrueFalseTotal
if (op->chain[i].type == XF_COALESCE) { 2 1636 1638
1904 2 use_default = 1; /* coalesce will handle it */
1905 2 break;
1906 }
1907 1636 ▶ if (op->chain[i].type == XF_BOOL || op->chain[i].type == XF_IF ||
Branch: 2 branches
BranchTrueFalseTotal
if (op->chain[i].type == XF_BOOL || op->chain[i].type == XF_IF || 1635 1 1636
if (op->chain[i].type == XF_BOOL || op->chain[i].type == XF_IF || 1634 1 1635
1908 1634 ▶ op->chain[i].type == XF_UNLESS || op->chain[i].type == XF_MAP ||
Branch: 2 branches
BranchTrueFalseTotal
op->chain[i].type == XF_UNLESS || op->chain[i].type == XF_MAP || 1634 0 1634
op->chain[i].type == XF_UNLESS || op->chain[i].type == XF_MAP || 1632 2 1634
1909 1632 ▶ op->chain[i].type == XF_WRAP) {
Branch: 1 branch
BranchTrueFalseTotal
op->chain[i].type == XF_WRAP) { 2 1630 1632
1910 6 src = ""; slen = 0;
1911 6 use_default = 1;
1912 6 break;
1913 }
1914 }
1915 687807 ▶ if (!use_default) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!use_default) return; 1626 15 1641
1916 }
1917 }
1918  
1919 /* handle coalesce: try fallback fields, then literal default */
1920 686221 ▶ if (!op->is_rownum && op->chain_len > 0 && op->chain[0].type == XF_COALESCE) {
Branch: 3 branches
BranchTrueFalseTotal
if (!op->is_rownum && op->chain_len > 0 && op->chain[0].type == XF_COALESCE) { 685206 1015 686221
if (!op->is_rownum && op->chain_len > 0 && op->chain[0].type == XF_COALESCE) { 685204 2 685206
if (!op->is_rownum && op->chain_len > 0 && op->chain[0].type == XF_COALESCE) { 19 685185 685204
1921 19 int primary_ok = 0;
1922 19 ▶ if (sv && SvOK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK(sv)) { 18 1 19
if (sv && SvOK(sv)) { 17 1 18
1923 STRLEN plen;
1924 17 const char *pstr = SvPV_nomg(sv, plen);
1925 17 ▶ if (plen > 0) { primary_ok = 1; src = pstr; slen = plen; use_default = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (plen > 0) { primary_ok = 1; src = pstr; slen = plen; use_default = 0; 3 14 17
1926 3 ▶ TS_NOTE_SV(t, sv, src, slen); }
Branch: 6 branches
BranchTrueFalseTotal
TS_NOTE_SV(t, sv, src, slen); } 0 3 3
TS_NOTE_SV(t, sv, src, slen); } 3 0 3
TS_NOTE_SV(t, sv, src, slen); } 3 0 3
TS_NOTE_SV(t, sv, src, slen); } 0 3 3
TS_NOTE_SV(t, sv, src, slen); } 0 0 0
TS_NOTE_SV(t, sv, src, slen); } 0 0 0
1927 }
1928 19 ▶ if (!primary_ok && !op->chain[0].param_str) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!primary_ok && !op->chain[0].param_str) return; 16 3 19
if (!primary_ok && !op->chain[0].param_str) return; 0 16 16
1929 19 ▶ if (!primary_ok && op->chain[0].param_str) {
Branch: 2 branches
BranchTrueFalseTotal
if (!primary_ok && op->chain[0].param_str) { 16 3 19
if (!primary_ok && op->chain[0].param_str) { 16 0 16
1930 16 const char *params = op->chain[0].param_str;
1931 16 STRLEN params_len = op->chain[0].param_str_len;
1932 16 const char *p = params, *pe = params + params_len;
1933 16 const char *last_param = NULL; STRLEN last_param_len = 0;
1934 /* The literal default is whatever follows the final ':', including
1935 nothing. Walking forwards never visited the empty tail of
1936 `coalesce:FIELD:`, so it stopped on FIELD and emitted that
1937 field's NAME. Scanning back makes the tail empty by
1938 construction. */
1939 16 { const char *q = pe;
1940 86 ▶ while (q > params && q[-1] != ':') q--;
Branch: 2 branches
BranchTrueFalseTotal
while (q > params && q[-1] != ':') q--; 86 0 86
while (q > params && q[-1] != ':') q--; 70 16 86
1941 16 last_param = q; last_param_len = (STRLEN)(pe - q);
1942 }
1943 /* try each fallback field (all params except the last) */
1944 16 int found = 0;
1945 16 p = params;
1946 27 ▶ while (p < pe) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < pe) { 24 3 27
1947 24 const char *next = memchr(p, ':', pe - p);
1948 24 ▶ STRLEN seg_len = next ? (STRLEN)(next - p) : (STRLEN)(pe - p);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN seg_len = next ? (STRLEN)(next - p) : (STRLEN)(pe - p); 19 5 24
1949 32 ▶ if (!next && p == last_param) break; /* this is the literal default */
Branch: 2 branches
BranchTrueFalseTotal
if (!next && p == last_param) break; /* this is the literal default */ 5 19 24
if (!next && p == last_param) break; /* this is the literal default */ 5 0 5
1950 /* try to fetch this field from the row */
1951 19 tpl_op tmp_op = {0};
1952 19 ▶ if (mode == ROW_HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode == ROW_HASH) { 12 7 19
1953 12 tmp_op.key = (char *)p; tmp_op.key_len = seg_len;
1954 /* the fallback name is a slice of the template, so it
1955 carries the template's own encoding */
1956 12 tmp_op.key_utf8 = op->key_utf8;
1957 } else {
1958 7 int is_neg = 0, si = 0;
1959 7 ▶ if (seg_len > 0 && p[0] == '-') { is_neg = 1; si = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (seg_len > 0 && p[0] == '-') { is_neg = 1; si = 1; } 7 0 7
if (seg_len > 0 && p[0] == '-') { is_neg = 1; si = 1; } 0 7 7
1960 7 int is_num = 1;
1961 23 ▶ for (STRLEN fi = si; fi < seg_len; fi++)
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN fi = si; fi < seg_len; fi++) 16 7 23
1962 16 ▶ if (p[fi] < '0' || p[fi] > '9') { is_num = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (p[fi] < '0' || p[fi] > '9') { is_num = 0; break; } 16 0 16
if (p[fi] < '0' || p[fi] > '9') { is_num = 0; break; } 0 16 16
1963 7 ▶ if (is_num && seg_len > (STRLEN)si) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_num && seg_len > (STRLEN)si) { 7 0 7
if (is_num && seg_len > (STRLEN)si) { 7 0 7
1964 7 tmp_op.col = 0;
1965 { /* bound it like parse_field_spec does: an unchecked
1966 accumulate wrapped 2**32 onto a valid column */
1967 7 int ovf = 0;
1968 22 ▶ for (STRLEN fi = si; fi < seg_len; fi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (STRLEN fi = si; fi < seg_len; fi++) { 16 6 22
1969 16 int d = p[fi] - '0';
1970 16 ▶ if (tmp_op.col > (INT_MAX - d) / 10) { ovf = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (tmp_op.col > (INT_MAX - d) / 10) { ovf = 1; break; } 1 15 16
1971 15 tmp_op.col = tmp_op.col * 10 + d;
1972 }
1973 7 ▶ if (ovf) { if (!next) break; p = next + 1; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (ovf) { if (!next) break; p = next + 1; continue; } 1 6 7
if (ovf) { if (!next) break; p = next + 1; continue; } 0 1 1
1974 }
1975 6 ▶ if (is_neg) tmp_op.col = -tmp_op.col;
Branch: 1 branch
BranchTrueFalseTotal
if (is_neg) tmp_op.col = -tmp_op.col; 0 6 6
1976 } else {
1977 0 ▶ if (!next) break; /* non-numeric in array mode = treat as literal default */
Branch: 1 branch
BranchTrueFalseTotal
if (!next) break; /* non-numeric in array mode = treat as literal default */ 0 0 0
1978 0 p = next + 1; continue;
1979 }
1980 }
1981 18 SV *fallback = fetch_field(aTHX_ row_sv, &tmp_op, mode);
1982 18 ▶ if (fallback && SvOK(fallback)) {
Branch: 2 branches
BranchTrueFalseTotal
if (fallback && SvOK(fallback)) { 18 0 18
if (fallback && SvOK(fallback)) { 17 1 18
1983 STRLEN flen;
1984 17 const char *fstr = SvPV_nomg(fallback, flen);
1985 17 ▶ if (flen > 0) { sv = fallback; src = fstr; slen = flen; use_default = 0; found = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (flen > 0) { sv = fallback; src = fstr; slen = flen; use_default = 0; found = 1; 8 9 17
1986 8 ▶ TS_NOTE_SV(t, fallback, src, slen); break; }
Branch: 6 branches
BranchTrueFalseTotal
TS_NOTE_SV(t, fallback, src, slen); break; } 0 8 8
TS_NOTE_SV(t, fallback, src, slen); break; } 8 0 8
TS_NOTE_SV(t, fallback, src, slen); break; } 8 0 8
TS_NOTE_SV(t, fallback, src, slen); break; } 0 8 8
TS_NOTE_SV(t, fallback, src, slen); break; } 0 0 0
TS_NOTE_SV(t, fallback, src, slen); break; } 0 0 0
1987 }
1988 10 ▶ if (!next) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!next) break; 0 10 10
1989 10 p = next + 1;
1990 }
1991 16 ▶ if (!found) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found) { 8 8 16
1992 8 src = last_param; slen = last_param_len;
1993 8 use_default = 1;
1994 }
1995 }
1996 }
1997  
1998 /* handle count type: count elements of array/hash ref */
1999 686221 ▶ if (!use_default && !op->is_rownum && op->chain_len > 0 && op->chain[0].type == XF_COUNT) {
Branch: 4 branches
BranchTrueFalseTotal
if (!use_default && !op->is_rownum && op->chain_len > 0 && op->chain[0].type == XF_COUNT) { 685185 1036 686221
if (!use_default && !op->is_rownum && op->chain_len > 0 && op->chain[0].type == XF_COUNT) { 685185 0 685185
if (!use_default && !op->is_rownum && op->chain_len > 0 && op->chain[0].type == XF_COUNT) { 685183 2 685185
if (!use_default && !op->is_rownum && op->chain_len > 0 && op->chain[0].type == XF_COUNT) { 10 685173 685183
2000 10 int cnt = 0;
2001 10 ▶ if (sv && SvROK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvROK(sv)) { 10 0 10
if (sv && SvROK(sv)) { 9 1 10
2002 9 SV *inner = SvRV(sv);
2003 9 ▶ if (SvTYPE(inner) == SVt_PVAV) cnt = (int)av_count((AV *)inner);
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(inner) == SVt_PVAV) cnt = (int)av_count((AV *)inner); 6 3 9
2004 3 ▶ else if (SvTYPE(inner) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTYPE(inner) == SVt_PVHV) { 3 0 3
2005 3 HV *hv = (HV *)inner;
2006 /* HvUSEDKEYS reads the backing store, empty for a tied hash */
2007 3 ▶ if (UNLIKELY(SvRMAGICAL(inner))) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(SvRMAGICAL(inner))) { 1 2 3
2008 1 hv_iterinit(hv);
2009 4 ▶ while (hv_iternext(hv)) cnt++;
Branch: 1 branch
BranchTrueFalseTotal
while (hv_iternext(hv)) cnt++; 3 1 4
2010 2 ▶ } else cnt = (int)HvUSEDKEYS(hv);
Branch: 1 branch
BranchTrueFalseTotal
} else cnt = (int)HvUSEDKEYS(hv); 0 2 2
2011 }
2012 }
2013 10 int clen = itoa_fast(cbuf, cnt);
2014 10 ▶ if (op->chain_len == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (op->chain_len == 1) { 5 5 10
2015 5 ▶ EMIT_DIRECT(cbuf, clen);
Branch: 3 branches
BranchTrueFalseTotal
EMIT_DIRECT(cbuf, clen); 0 5 5
EMIT_DIRECT(cbuf, clen); 0 0 0
EMIT_DIRECT(cbuf, clen); 0 0 0
2016 5 return;
2017 }
2018 5 src = cbuf; slen = clen;
2019 }
2020  
2021 /* get initial string value */
2022 686216 ▶ if (!use_default && !op->is_rownum && !(op->chain_len > 0 &&
Branch: 3 branches
BranchTrueFalseTotal
if (!use_default && !op->is_rownum && !(op->chain_len > 0 && 685180 1036 686216
if (!use_default && !op->is_rownum && !(op->chain_len > 0 && 685180 0 685180
if (!use_default && !op->is_rownum && !(op->chain_len > 0 && 685178 2 685180
2023 685178 ▶ (op->chain[0].type == XF_COUNT || op->chain[0].type == XF_COALESCE))) {
Branch: 2 branches
BranchTrueFalseTotal
(op->chain[0].type == XF_COUNT || op->chain[0].type == XF_COALESCE))) { 685173 5 685178
(op->chain[0].type == XF_COUNT || op->chain[0].type == XF_COALESCE))) { 685162 11 685173
2024 /* for int/float types as first transform, use numeric conversion */
2025 685164 ▶ if (op->chain_len > 0 && (op->chain[0].type == XF_INT || op->chain[0].type == XF_INT_COMMA)) {
Branch: 3 branches
BranchTrueFalseTotal
if (op->chain_len > 0 && (op->chain[0].type == XF_INT || op->chain[0].type == XF_INT_COMMA)) { 685162 2 685164
if (op->chain_len > 0 && (op->chain[0].type == XF_INT || op->chain[0].type == XF_INT_COMMA)) { 674950 10212 685162
if (op->chain_len > 0 && (op->chain[0].type == XF_INT || op->chain[0].type == XF_INT_COMMA)) { 8 674942 674950
2026 10220 ▶ int ilen = (op->chain[0].type == XF_INT) ? itoa_fast(ibuf, SvIV_nomg(sv)) : itoa_comma(ibuf, SvIV_nomg(sv));
Branch: 1 branch
BranchTrueFalseTotal
int ilen = (op->chain[0].type == XF_INT) ? itoa_fast(ibuf, SvIV_nomg(sv)) : itoa_comma(ibuf, SvIV_nomg(sv)); 10212 8 10220
2027 10220 ▶ if (op->chain_len == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (op->chain_len == 1) { 10219 1 10220
2028 10219 ▶ EMIT_DIRECT(ibuf, ilen);
Branch: 3 branches
BranchTrueFalseTotal
EMIT_DIRECT(ibuf, ilen); 0 10219 10219
EMIT_DIRECT(ibuf, ilen); 0 0 0
EMIT_DIRECT(ibuf, ilen); 0 0 0
2029 10219 return;
2030 }
2031 1 src = ibuf; slen = ilen;
2032 674944 ▶ } else if (op->chain_len > 0 && op->chain[0].type == XF_FLOAT) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (op->chain_len > 0 && op->chain[0].type == XF_FLOAT) { 674942 2 674944
} else if (op->chain_len > 0 && op->chain[0].type == XF_FLOAT) { 24 674918 674942
2033 /* the cast is load-bearing: NV is long double under -Duselongdouble */
2034 24 int flen = snprintf(fbuf, TS_FLTSZ, "%.*f",
2035 24 op->chain[0].param_int, (double)SvNV_nomg(sv));
2036 24 ▶ if (flen > TS_FLTSZ - 1) flen = TS_FLTSZ - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (flen > TS_FLTSZ - 1) flen = TS_FLTSZ - 1; 0 24 24
2037 24 ▶ if (op->chain_len == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (op->chain_len == 1) { 15 9 24
2038 15 ▶ EMIT_DIRECT(fbuf, flen);
Branch: 3 branches
BranchTrueFalseTotal
EMIT_DIRECT(fbuf, flen); 2 13 15
EMIT_DIRECT(fbuf, flen); 2 0 2
EMIT_DIRECT(fbuf, flen); 0 2 2
2039 15 return;
2040 }
2041 9 src = fbuf; slen = flen;
2042 674992 ▶ } else if (op->chain_len > 0 && op->chain[0].type == XF_SPRINTF &&
Branch: 1 branch
BranchTrueFalseTotal
} else if (op->chain_len > 0 && op->chain[0].type == XF_SPRINTF && 674918 2 674920
2043 114 ts_sprintf_conv(op->chain[0].param_str)) {
2044 /* Numify first, like int and float do. The formatter itself only
2045 scrapes digits out of text, so handing it the raw string made
2046 sprintf:%d on 3.9 print 39 -- and disagree with int on the same
2047 value. Mid-chain there is no SV left to numify, which is why the
2048 two still part company there. A format the formatter will reject
2049 must not be numified at all, or the value it passes through
2050 unchanged would arrive already mangled. */
2051 42 char conv = ts_sprintf_conv(op->chain[0].param_str);
2052 42 ▶ if (conv == 'd' || conv == 'i' || conv == 'x' ||
Branch: 4 branches
BranchTrueFalseTotal
if (conv == 'd' || conv == 'i' || conv == 'x' || 25 17 42
if (conv == 'd' || conv == 'i' || conv == 'x' || 24 1 25
if (conv == 'd' || conv == 'i' || conv == 'x' || 20 4 24
if (conv == 'd' || conv == 'i' || conv == 'x' || 19 1 20
2053 19 ▶ conv == 'X' || conv == 'o' || conv == 'u') {
Branch: 2 branches
BranchTrueFalseTotal
conv == 'X' || conv == 'o' || conv == 'u') { 17 2 19
conv == 'X' || conv == 'o' || conv == 'u') { 1 16 17
2054 26 slen = (STRLEN)itoa_fast(ibuf, SvIV_nomg(sv));
2055 26 src = ibuf;
2056 16 ▶ } else if (conv == 'f' || conv == 'e' || conv == 'g') {
Branch: 3 branches
BranchTrueFalseTotal
} else if (conv == 'f' || conv == 'e' || conv == 'g') { 12 4 16
} else if (conv == 'f' || conv == 'e' || conv == 'g') { 11 1 12
} else if (conv == 'f' || conv == 'e' || conv == 'g') { 0 11 11
2057 5 int nlen = snprintf(fbuf, TS_FLTSZ, "%.17g", (double)SvNV_nomg(sv));
2058 5 ▶ if (nlen < 0) nlen = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nlen < 0) nlen = 0; 0 5 5
2059 5 ▶ if (nlen > TS_FLTSZ - 1) nlen = TS_FLTSZ - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (nlen > TS_FLTSZ - 1) nlen = TS_FLTSZ - 1; 0 5 5
2060 5 src = fbuf; slen = (STRLEN)nlen;
2061 } else {
2062 11 src = SvPV_nomg(sv, slen);
2063 11 ▶ TS_NOTE_SV(t, sv, src, slen);
Branch: 6 branches
BranchTrueFalseTotal
TS_NOTE_SV(t, sv, src, slen); 7 4 11
TS_NOTE_SV(t, sv, src, slen); 4 0 4
TS_NOTE_SV(t, sv, src, slen); 4 0 4
TS_NOTE_SV(t, sv, src, slen); 0 4 4
TS_NOTE_SV(t, sv, src, slen); 0 0 0
TS_NOTE_SV(t, sv, src, slen); 0 0 0
2064 }
2065 } else {
2066 674877 src = SvPV_nomg(sv, slen);
2067 674877 ▶ TS_NOTE_SV(t, sv, src, slen);
Branch: 6 branches
BranchTrueFalseTotal
TS_NOTE_SV(t, sv, src, slen); 36 674841 674877
TS_NOTE_SV(t, sv, src, slen); 674839 2 674841
TS_NOTE_SV(t, sv, src, slen); 674833 6 674839
TS_NOTE_SV(t, sv, src, slen); 400 674433 674833
TS_NOTE_SV(t, sv, src, slen); 396 4 400
TS_NOTE_SV(t, sv, src, slen); 2 394 396
2068 }
2069 }
2070  
2071 /* single transform fast path (most common) */
2072 675981 int start = 0;
2073 675981 ▶ if (!use_default && !op->is_rownum && op->chain_len > 0 &&
Branch: 3 branches
BranchTrueFalseTotal
if (!use_default && !op->is_rownum && op->chain_len > 0 && 674945 1036 675981
if (!use_default && !op->is_rownum && op->chain_len > 0 && 674945 0 674945
if (!use_default && !op->is_rownum && op->chain_len > 0 && 674943 2 674945
2074 674943 ▶ (op->chain[0].type == XF_INT || op->chain[0].type == XF_INT_COMMA ||
Branch: 2 branches
BranchTrueFalseTotal
(op->chain[0].type == XF_INT || op->chain[0].type == XF_INT_COMMA || 674942 1 674943
(op->chain[0].type == XF_INT || op->chain[0].type == XF_INT_COMMA || 674942 0 674942
2075 674942 ▶ op->chain[0].type == XF_FLOAT || op->chain[0].type == XF_COUNT ||
Branch: 2 branches
BranchTrueFalseTotal
op->chain[0].type == XF_FLOAT || op->chain[0].type == XF_COUNT || 674933 9 674942
op->chain[0].type == XF_FLOAT || op->chain[0].type == XF_COUNT || 674928 5 674933
2076 674928 ▶ op->chain[0].type == XF_COALESCE))
Branch: 1 branch
BranchTrueFalseTotal
op->chain[0].type == XF_COALESCE)) 11 674917 674928
2077 26 start = 1;
2078 /* use_default skipped the count handler, and apply_xform then wrote 0 over
2079 the value default had supplied: {items:count|default:none} on a missing
2080 field printed 0. count on undef is empty, so drop the stage. The rownum
2081 is a scalar, where 0 is right, so leave {#:count} alone. */
2082 675981 ▶ if (use_default && !op->is_rownum && op->chain_len > 0 &&
Branch: 3 branches
BranchTrueFalseTotal
if (use_default && !op->is_rownum && op->chain_len > 0 && 1036 674945 675981
if (use_default && !op->is_rownum && op->chain_len > 0 && 21 1015 1036
if (use_default && !op->is_rownum && op->chain_len > 0 && 21 0 21
2083 21 ▶ op->chain[0].type == XF_COUNT)
Branch: 1 branch
BranchTrueFalseTotal
op->chain[0].type == XF_COUNT) 4 17 21
2084 4 start = 1;
2085  
2086 /* the overwhelmingly common shape: one transform, and it emits */
2087 675981 ▶ if (op->chain_len - start == 1 && op->chain[start].type != XF_DEFAULT) {
Branch: 2 branches
BranchTrueFalseTotal
if (op->chain_len - start == 1 && op->chain[start].type != XF_DEFAULT) { 675911 70 675981
if (op->chain_len - start == 1 && op->chain[start].type != XF_DEFAULT) { 675904 7 675911
2088 675904 char *tmp = NULL; STRLEN tmp_len = 0, tmp_alloc = 0;
2089 675904 apply_xform(t, &op->chain[start], src, slen, bufp, posp, allocp,
2090 &tmp, &tmp_len, &tmp_alloc, 1);
2091 675904 ▶ if (tmp) free(tmp);
Branch: 1 branch
BranchTrueFalseTotal
if (tmp) free(tmp); 0 675904 675904
2092 675904 return;
2093 }
2094  
2095 /* DEFAULT only supplies a fallback value, it emits nothing itself, so count
2096 the transforms that actually produce output */
2097 77 int effective = 0;
2098 77 tpl_xform *only = NULL;
2099 207 ▶ for (int i = start; i < op->chain_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (int i = start; i < op->chain_len; i++) 130 77 207
2100 130 ▶ if (op->chain[i].type != XF_DEFAULT) { if (!effective) only = &op->chain[i]; effective++; }
Branch: 2 branches
BranchTrueFalseTotal
if (op->chain[i].type != XF_DEFAULT) { if (!effective) only = &op->chain[i]; effective++; } 117 13 130
if (op->chain[i].type != XF_DEFAULT) { if (!effective) only = &op->chain[i]; effective++; } 55 62 117
2101  
2102 77 ▶ if (effective == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (effective == 0) { 22 55 77
2103 22 ▶ EMIT_DIRECT(src, slen);
Branch: 3 branches
BranchTrueFalseTotal
EMIT_DIRECT(src, slen); 0 22 22
EMIT_DIRECT(src, slen); 0 0 0
EMIT_DIRECT(src, slen); 0 0 0
2104 22 return;
2105 }
2106  
2107 55 ▶ if (effective == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (effective == 1) { 1 54 55
2108 1 char *tmp = NULL; STRLEN tmp_len = 0, tmp_alloc = 0;
2109 1 apply_xform(t, only, src, slen, bufp, posp, allocp, &tmp, &tmp_len, &tmp_alloc, 1);
2110 1 ▶ if (tmp) free(tmp);
Branch: 1 branch
BranchTrueFalseTotal
if (tmp) free(tmp); 0 1 1
2111 1 return;
2112 }
2113  
2114 /* chain: apply transforms with ping-pong buffers.
2115 Deliberate gap: plain malloc, not savestack-tracked, so a croak before
2116 the free below leaks the block apply_xform is growing -- likewise
2117 sprintf's sv_heap. Only the "out of memory" croaks reach either window
2118 on 64-bit and no user code runs there; plugging it costs a signature
2119 change plus stores in the per-stage loop. Revisit if a transform ever
2120 grows a croak of its own. */
2121 54 char *tmp_a = NULL, *tmp_b = NULL;
2122 54 STRLEN tmp_a_len = 0, tmp_a_alloc = 0, tmp_b_len = 0, tmp_b_alloc = 0;
2123 54 const char *cur = src; STRLEN cur_len = slen;
2124 54 int use_a = 1;
2125  
2126 /* which stage emits: found once here rather than rescanned per stage */
2127 54 int last_eff = -1;
2128 171 ▶ for (int i = start; i < op->chain_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (int i = start; i < op->chain_len; i++) 117 54 171
2129 117 ▶ if (op->chain[i].type != XF_DEFAULT) last_eff = i;
Branch: 1 branch
BranchTrueFalseTotal
if (op->chain[i].type != XF_DEFAULT) last_eff = i; 116 1 117
2130  
2131 171 ▶ for (int i = start; i < op->chain_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = start; i < op->chain_len; i++) { 117 54 171
2132 117 ▶ if (op->chain[i].type == XF_DEFAULT) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (op->chain[i].type == XF_DEFAULT) continue; 1 116 117
2133 116 int is_last = (i == last_eff);
2134  
2135 116 ▶ if (is_last) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_last) { 54 62 116
2136 54 char *dummy = NULL; STRLEN dummy_len = 0, dummy_alloc = 0;
2137 54 apply_xform(t, &op->chain[i], cur, cur_len, bufp, posp, allocp, &dummy, &dummy_len, &dummy_alloc, 1);
2138 54 ▶ if (dummy) free(dummy);
Branch: 1 branch
BranchTrueFalseTotal
if (dummy) free(dummy); 0 54 54
2139 } else {
2140 62 ▶ if (use_a) {
Branch: 1 branch
BranchTrueFalseTotal
if (use_a) { 56 6 62
2141 56 tmp_a_len = 0;
2142 56 apply_xform(t, &op->chain[i], cur, cur_len, bufp, posp, allocp, &tmp_a, &tmp_a_len, &tmp_a_alloc, 0);
2143 /* a stage that emitted nothing leaves the buffer NULL; keep NULL
2144 out of the next stage's memcpy */
2145 56 ▶ cur = tmp_a ? tmp_a : ""; cur_len = tmp_a_len;
Branch: 1 branch
BranchTrueFalseTotal
cur = tmp_a ? tmp_a : ""; cur_len = tmp_a_len; 56 0 56
2146 56 use_a = 0;
2147 } else {
2148 6 tmp_b_len = 0;
2149 6 apply_xform(t, &op->chain[i], cur, cur_len, bufp, posp, allocp, &tmp_b, &tmp_b_len, &tmp_b_alloc, 0);
2150 6 ▶ cur = tmp_b ? tmp_b : ""; cur_len = tmp_b_len;
Branch: 1 branch
BranchTrueFalseTotal
cur = tmp_b ? tmp_b : ""; cur_len = tmp_b_len; 6 0 6
2151 6 use_a = 1;
2152 }
2153 }
2154 }
2155 54 ▶ if (tmp_a) free(tmp_a);
Branch: 1 branch
BranchTrueFalseTotal
if (tmp_a) free(tmp_a); 54 0 54
2156 54 ▶ if (tmp_b) free(tmp_b);
Branch: 1 branch
BranchTrueFalseTotal
if (tmp_b) free(tmp_b); 4 50 54
2157 }
2158  
2159 /* check if a field value in a row is truthy */
2160 157 static int is_field_truthy(pTHX_ SV *row_sv, tpl_compiled *t, int is_skip_if) {
2161 int col; char *key; STRLEN key_len; int key_utf8;
2162 157 ▶ if (is_skip_if) { col = t->skip_if_col; key = t->skip_if_key;
Branch: 1 branch
BranchTrueFalseTotal
if (is_skip_if) { col = t->skip_if_col; key = t->skip_if_key; 136 21 157
2163 136 key_len = t->skip_if_key_len; key_utf8 = t->skip_if_utf8; }
2164 21 else { col = t->skip_unless_col; key = t->skip_unless_key;
2165 21 key_len = t->skip_unless_key_len; key_utf8 = t->skip_unless_utf8; }
2166  
2167 157 SV *field = NULL;
2168 157 SvGETMAGIC(row_sv);
2169 157 ▶ if (key) {
Branch: 1 branch
BranchTrueFalseTotal
if (key) { 119 38 157
2170 119 ▶ if (SvROK(row_sv) && SvTYPE(SvRV(row_sv)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(row_sv) && SvTYPE(SvRV(row_sv)) == SVt_PVHV) { 119 0 119
if (SvROK(row_sv) && SvTYPE(SvRV(row_sv)) == SVt_PVHV) { 119 0 119
2171 119 ▶ SV **sv = hv_fetch((HV *)SvRV(row_sv), key,
Branch: 1 branch
BranchTrueFalseTotal
SV **sv = hv_fetch((HV *)SvRV(row_sv), key, 2 117 119
2172 key_utf8 ? -(I32)key_len : (I32)key_len, 0);
2173 /* a tied hash yields an unmaterialised PVLV, and SvOK is false on it
2174 until get-magic runs -- the row looked untruthy without FETCH */
2175 119 ▶ if (sv && *sv) { SvGETMAGIC(*sv); field = *sv; }
Branch: 2 branches
BranchTrueFalseTotal
if (sv && *sv) { SvGETMAGIC(*sv); field = *sv; } 119 0 119
if (sv && *sv) { SvGETMAGIC(*sv); field = *sv; } 119 0 119
2176 }
2177 } else {
2178 38 ▶ if (SvROK(row_sv) && SvTYPE(SvRV(row_sv)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(row_sv) && SvTYPE(SvRV(row_sv)) == SVt_PVAV) 38 0 38
if (SvROK(row_sv) && SvTYPE(SvRV(row_sv)) == SVt_PVAV) 38 0 38
2179 38 field = av_element(aTHX_ (AV *)SvRV(row_sv), col);
2180 }
2181 157 ▶ if (!field || !SvOK(field)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!field || !SvOK(field)) return 0; 156 1 157
if (!field || !SvOK(field)) return 0; 3 153 156
2182 STRLEN flen;
2183 /* get-magic already ran above, on both branches. Plain SvPV re-ran it, and
2184 a tied element's second FETCH can differ -- the skip decision was taken
2185 on a value nothing else in the render sees. fetch_field is spelled the
2186 same way. */
2187 153 const char *fstr = SvPV_nomg(field, flen);
2188 153 ▶ if (flen == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (flen == 0) return 0; 11 142 153
2189 142 ▶ if (flen == 1 && fstr[0] == '0') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (flen == 1 && fstr[0] == '0') return 0; 35 107 142
if (flen == 1 && fstr[0] == '0') return 0; 11 24 35
2190 131 return 1;
2191 }
2192  
2193 713944 static int should_skip_row(pTHX_ SV *row_sv, tpl_compiled *t) {
2194 713944 ▶ if (t->has_skip_if && is_field_truthy(aTHX_ row_sv, t, 1)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (t->has_skip_if && is_field_truthy(aTHX_ row_sv, t, 1)) return 1; 136 713808 713944
if (t->has_skip_if && is_field_truthy(aTHX_ row_sv, t, 1)) return 1; 120 16 136
2195 713824 ▶ if (t->has_skip_unless && !is_field_truthy(aTHX_ row_sv, t, 0)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (t->has_skip_unless && !is_field_truthy(aTHX_ row_sv, t, 0)) return 1; 21 713803 713824
if (t->has_skip_unless && !is_field_truthy(aTHX_ row_sv, t, 0)) return 1; 10 11 21
2196 713814 return 0;
2197 }
2198  
2199 1603 static SV *tpl_render(pTHX_ tpl_compiled *t, AV *rows) {
2200 1603 TS_ENC_ENTER(t);
2201 1603 TS_PIN_AV(rows);
2202 1603 SSize_t nrows = av_count(rows);
2203 1603 t->last_row_count = nrows;
2204 STRLEN alloc, pos;
2205 char *buf;
2206 1603 ▶ RBUF_INIT(t, ts_est(t->header_len + t->footer_len, nrows));
Branch: 3 branches
BranchTrueFalseTotal
RBUF_INIT(t, ts_est(t->header_len + t->footer_len, nrows)); 786 817 1603
RBUF_INIT(t, ts_est(t->header_len + t->footer_len, nrows)); 785 1 786
RBUF_INIT(t, ts_est(t->header_len + t->footer_len, nrows)); 0 818 818
2207 1603 TS_INFLIGHT_ARM(t);
2208  
2209 1603 ▶ BUF_WRITE(t->header, t->header_len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_WRITE(t->header, t->header_len); 17 1586 1603
BUF_WRITE(t->header, t->header_len); 0 17 17
BUF_WRITE(t->header, t->header_len); 0 0 0
BUF_WRITE(t->header, t->header_len); 0 0 0
2210 1603 int first = 1;
2211 364893 ▶ for (SSize_t i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (SSize_t i = 0; i < nrows; i++) { 363693 1200 364893
2212 SV *row_sv;
2213 363693 ▶ if (!ts_row_at(aTHX_ rows, i, &row_sv)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!ts_row_at(aTHX_ rows, i, &row_sv)) break; 2 363691 363693
2214 363691 ▶ TS_PIN_ROW(row_sv);
Branch: 1 branch
BranchTrueFalseTotal
TS_PIN_ROW(row_sv); 363191 500 363691
2215 363691 ▶ if (should_skip_row(aTHX_ row_sv, t)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (should_skip_row(aTHX_ row_sv, t)) continue; 22 363669 363691
2216 363669 ▶ if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len);
Branch: 6 branches
BranchTrueFalseTotal
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 362079 1590 363669
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 12036 350043 362079
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 12036 0 12036
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 1 12035 12036
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 1 0 1
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 0 1 1
2217 363669 first = 0;
2218 1079258 ▶ TS_EMIT_OPS(row_sv, i);
Branch: 6 branches
BranchTrueFalseTotal
TS_EMIT_OPS(row_sv, i); 352242 363748 715990
TS_EMIT_OPS(row_sv, i); 352242 0 352242
TS_EMIT_OPS(row_sv, i); 0 352242 352242
TS_EMIT_OPS(row_sv, i); 0 0 0
TS_EMIT_OPS(row_sv, i); 0 0 0
TS_EMIT_OPS(row_sv, i); 715990 363268 1079258
2219 }
2220 1202 t->last_row_count = nrows;
2221 1202 ▶ BUF_WRITE(t->footer, t->footer_len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_WRITE(t->footer, t->footer_len); 13 1189 1202
BUF_WRITE(t->footer, t->footer_len); 0 13 13
BUF_WRITE(t->footer, t->footer_len); 0 0 0
BUF_WRITE(t->footer, t->footer_len); 0 0 0
2222 1202 TS_INFLIGHT_DISARM(t);
2223 1202 ▶ RBUF_FINISH(t);
Branch: 4 branches
BranchTrueFalseTotal
RBUF_FINISH(t); 2 1200 1202
RBUF_FINISH(t); 5 1197 1202
RBUF_FINISH(t); 3 2 5
RBUF_FINISH(t); 3 0 3
2224 1202 { SV *res_ = tpl_out_sv(aTHX_ t, buf, pos); TS_ENC_LEAVE(t); return res_; }
2225 }
2226  
2227 /* ---- sorted render ---- */
2228  
2229 /* Stringifying this SV would run Perl -- get-magic, an overload, or a
2230 reference whose stringification could be overloaded. Nothing here is a
2231 guess: for a plain scalar all three are false and SvPV is a pure read. */
2232 /* Any magic at all means stringifying this SV may re-enter perl. Testing the
2233 individual flavours got it wrong three times: get-magic missed a magical
2234 row slot, then undef reaching report_uninit, then PERL_MAGIC_uvar -- which
2235 hv_fetch dispatches on SvSMAGICAL && SvGMAGICAL and which is not RMAGICAL,
2236 so a fieldhash or a Variable::Magic fetch wizard slipped straight past.
2237 One predicate, no flavours: it costs the same test and cannot drift. */
2238 #define TS_RUNS_PERL(sv) (SvMAGICAL(sv) || SvROK(sv))
2239  
2240 /* Take ownership of the first `n` collected sort keys, which until now pointed
2241 into the fields themselves. Called the first time something appears that
2242 could move those buffers, while they are all still valid. */
2243 14 static void ts_own_keys(pTHX_ const char **keys, STRLEN *lens, SSize_t n) {
2244 SSize_t i;
2245 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 14 24
2246 10 ▶ if (!lens[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!lens[i]) continue; 0 10 10
2247 10 keys[i] = SvPVX(sv_2mortal(newSVpvn(keys[i], lens[i])));
2248 }
2249 14 }
2250  
2251 /* The comparator gets no context argument from qsort, so what it needs to
2252 know travels in the entries themselves. Holding it in file-scope statics
2253 instead meant two interpreters sorting at once could each see the other.s
2254 key count and read past its own key arrays. */
2255 typedef struct {
2256 SV *sv; const char **keys; STRLEN *key_lens;
2257 int nsort; int numeric;
2258 } sort_entry;
2259  
2260 7769 static int sort_cmp_multi(const sort_entry *ea, const sort_entry *eb) {
2261 9991 ▶ for (int k = 0; k < ea->nsort; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int k = 0; k < ea->nsort; k++) { 7774 2217 9991
2262 7774 ▶ if (ea->numeric) {
Branch: 1 branch
BranchTrueFalseTotal
if (ea->numeric) { 45 7729 7774
2263 45 double da = ts_atof(ea->keys[k], ea->key_lens[k]);
2264 45 double db = ts_atof(eb->keys[k], eb->key_lens[k]);
2265 /* NaN is neither < nor > anything, so it compared equal to every
2266 key and the comparator stopped being a weak ordering: garbage
2267 order, plus an out-of-bounds read in glibc qsort before 2.39.
2268 Treat it as the zero other unparseable values become. */
2269 45 ▶ if (UNLIKELY(da != da)) da = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(da != da)) da = 0; 6 39 45
2270 45 ▶ if (UNLIKELY(db != db)) db = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(db != db)) db = 0; 7 38 45
2271 45 ▶ if (da < db) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (da < db) return -1; 16 29 45
2272 29 ▶ if (da > db) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (da > db) return 1; 27 2 29
2273 } else {
2274 7729 STRLEN minlen = ea->key_lens[k] < eb->key_lens[k] ? ea->key_lens[k] : eb->key_lens[k];
2275 7729 int r = memcmp(ea->keys[k], eb->keys[k], minlen);
2276 7729 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 2691 5038 7729
2277 5038 ▶ if (ea->key_lens[k] != eb->key_lens[k])
Branch: 1 branch
BranchTrueFalseTotal
if (ea->key_lens[k] != eb->key_lens[k]) 2818 2220 5038
2278 2818 ▶ return ea->key_lens[k] < eb->key_lens[k] ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
return ea->key_lens[k] < eb->key_lens[k] ? -1 : 1; 322 2496 2818
2279 }
2280 }
2281 2217 return 0;
2282 }
2283  
2284 7727 static int sort_cmp_asc(const void *a, const void *b) {
2285 7727 return sort_cmp_multi((const sort_entry *)a, (const sort_entry *)b);
2286 }
2287  
2288 42 static int sort_cmp_desc(const void *a, const void *b) {
2289 42 return sort_cmp_multi((const sort_entry *)b, (const sort_entry *)a);
2290 }
2291  
2292 /* libc free() as a SAVEDESTRUCTOR_X callback. The sort scratch is malloc'd,
2293 not Newx'd, so SAVEFREEPV/Safefree would mismatch on DEBUGGING perls. */
2294 283 static void ts_free(pTHX_ void *p) { free(p); }
2295  
2296 79 static SV *tpl_render_sorted(pTHX_ tpl_compiled *t, AV *rows,
2297 int *sort_cols, const char **sort_keys, STRLEN *sort_key_lens,
2298 const int *sort_key_utf8,
2299 int nsort, int descending, int numeric) {
2300 79 TS_ENC_ENTER(t);
2301 79 TS_PIN_AV(rows);
2302 79 SSize_t nrows = av_count(rows);
2303 79 t->last_row_count = nrows;
2304  
2305 79 ▶ sort_entry *entries = nrows > 0 ? (sort_entry *)malloc(nrows * sizeof(sort_entry)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
sort_entry *entries = nrows > 0 ? (sort_entry *)malloc(nrows * sizeof(sort_entry)) : NULL; 78 1 79
2306 79 ▶ if (nrows > 0 && !entries) croak("Text::Stencil: out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (nrows > 0 && !entries) croak("Text::Stencil: out of memory"); 78 1 79
if (nrows > 0 && !entries) croak("Text::Stencil: out of memory"); 0 78 78
2307 79 ▶ const char **all_keys = nrows > 0 ? (const char **)calloc(nrows * nsort, sizeof(char *)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char **all_keys = nrows > 0 ? (const char **)calloc(nrows * nsort, sizeof(char *)) : NULL; 78 1 79
2308 79 ▶ STRLEN *all_lens = nrows > 0 ? (STRLEN *)calloc(nrows * nsort, sizeof(STRLEN)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN *all_lens = nrows > 0 ? (STRLEN *)calloc(nrows * nsort, sizeof(STRLEN)) : NULL; 78 1 79
2309 /* Free the sort scratch on scope exit, so a croak mid-render (e.g. an OOM
2310 realloc in BUF_ENSURE) frees it on the unwind instead of leaking it;
2311 LEAVE frees it on the normal path. */
2312 79 ENTER;
2313 79 ▶ if (entries) SAVEDESTRUCTOR_X(ts_free, entries);
Branch: 1 branch
BranchTrueFalseTotal
if (entries) SAVEDESTRUCTOR_X(ts_free, entries); 78 1 79
2314 79 ▶ if (all_keys) SAVEDESTRUCTOR_X(ts_free, all_keys);
Branch: 1 branch
BranchTrueFalseTotal
if (all_keys) SAVEDESTRUCTOR_X(ts_free, all_keys); 78 1 79
2315 79 ▶ if (all_lens) SAVEDESTRUCTOR_X(ts_free, all_lens);
Branch: 1 branch
BranchTrueFalseTotal
if (all_lens) SAVEDESTRUCTOR_X(ts_free, all_lens); 78 1 79
2316 /* checked after the SAVEDESTRUCTORs so the unwind frees entries; an
2317 unchecked NULL here would segfault at entries[i].keys below */
2318 79 ▶ if (nrows > 0 && (!all_keys || !all_lens)) croak("Text::Stencil: out of memory");
Branch: 3 branches
BranchTrueFalseTotal
if (nrows > 0 && (!all_keys || !all_lens)) croak("Text::Stencil: out of memory"); 78 1 79
if (nrows > 0 && (!all_keys || !all_lens)) croak("Text::Stencil: out of memory"); 78 0 78
if (nrows > 0 && (!all_keys || !all_lens)) croak("Text::Stencil: out of memory"); 0 78 78
2319 /* a magical rows array runs Perl on every fetch, so never borrow there */
2320 79 int borrow = !SvMAGICAL((SV *)rows);
2321 1300 ▶ for (SSize_t i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (SSize_t i = 0; i < nrows; i++) { 1221 79 1300
2322 1221 SV **rowref = av_fetch(rows, i, 0);
2323 /* A plain rows array can still hold a magical element -- tie $rows[1] --
2324 and materialising it runs Perl just as a tied container would. Give
2325 up borrowing before that happens, not after. (Only get-magic matters
2326 here: the row is a reference we never stringify, so SvROK/SvAMAGIC
2327 would disqualify every ordinary row for nothing.) */
2328 1221 ▶ if (borrow && rowref && *rowref && SvMAGICAL(*rowref)) {
Branch: 4 branches
BranchTrueFalseTotal
if (borrow && rowref && *rowref && SvMAGICAL(*rowref)) { 1190 31 1221
if (borrow && rowref && *rowref && SvMAGICAL(*rowref)) { 1190 0 1190
if (borrow && rowref && *rowref && SvMAGICAL(*rowref)) { 1190 0 1190
if (borrow && rowref && *rowref && SvMAGICAL(*rowref)) { 1 1189 1190
2329 1 ts_own_keys(aTHX_ all_keys, all_lens, i * nsort);
2330 1 borrow = 0;
2331 }
2332 /* a magical rows array yields unmaterialised PVLVs, and the SvROK test
2333 below would leave every sort key empty */
2334 1221 ▶ if (rowref) SvGETMAGIC(*rowref);
Branch: 1 branch
BranchTrueFalseTotal
if (rowref) SvGETMAGIC(*rowref); 1221 0 1221
2335 /* the comparator reads these PVs long after collection, so the key SVs
2336 below must outlive it too -- that was a use-after-free in qsort */
2337 1221 ▶ SV *row_sv = (rowref && *rowref) ? *rowref : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *row_sv = (rowref && *rowref) ? *rowref : &PL_sv_undef; 1221 0 1221
SV *row_sv = (rowref && *rowref) ? *rowref : &PL_sv_undef; 1221 0 1221
2338 1221 ▶ TS_PIN_ROW(row_sv);
Branch: 1 branch
BranchTrueFalseTotal
TS_PIN_ROW(row_sv); 1221 0 1221
2339 1221 entries[i].sv = row_sv;
2340 1221 entries[i].keys = all_keys + i * nsort;
2341 1221 entries[i].key_lens = all_lens + i * nsort;
2342 1221 entries[i].nsort = nsort;
2343 1221 entries[i].numeric = numeric;
2344 1221 ▶ if (borrow && SvROK(row_sv) && SvMAGICAL(SvRV(row_sv))) {
Branch: 3 branches
BranchTrueFalseTotal
if (borrow && SvROK(row_sv) && SvMAGICAL(SvRV(row_sv))) { 1189 32 1221
if (borrow && SvROK(row_sv) && SvMAGICAL(SvRV(row_sv))) { 688 501 1189
if (borrow && SvROK(row_sv) && SvMAGICAL(SvRV(row_sv))) { 4 684 688
2345 4 ts_own_keys(aTHX_ all_keys, all_lens, i * nsort);
2346 4 borrow = 0;
2347 }
2348 2466 ▶ for (int k = 0; k < nsort; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int k = 0; k < nsort; k++) { 1245 1221 2466
2349 1245 entries[i].keys[k] = ""; entries[i].key_lens[k] = 0;
2350 1245 ▶ if (SvROK(row_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(row_sv)) { 744 501 1245
2351 744 SV *field = NULL;
2352 744 ▶ if (sort_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (sort_keys) { 159 585 744
2353 159 ▶ if (SvTYPE(SvRV(row_sv)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(row_sv)) == SVt_PVHV) { 159 0 159
2354 159 ▶ SV **sv = hv_fetch((HV *)SvRV(row_sv), sort_keys[k],
Branch: 1 branch
BranchTrueFalseTotal
SV **sv = hv_fetch((HV *)SvRV(row_sv), sort_keys[k], 3 156 159
2355 sort_key_utf8[k] ? -(I32)sort_key_lens[k]
2356 : (I32)sort_key_lens[k], 0);
2357 159 ▶ if (sv) field = *sv;
Branch: 1 branch
BranchTrueFalseTotal
if (sv) field = *sv; 159 0 159
2358 }
2359 } else {
2360 585 ▶ if (SvTYPE(SvRV(row_sv)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(row_sv)) == SVt_PVAV) { 585 0 585
2361 585 SV **sv = av_fetch((AV *)SvRV(row_sv), sort_cols[k], 0);
2362 585 ▶ if (sv) field = *sv;
Branch: 1 branch
BranchTrueFalseTotal
if (sv) field = *sv; 585 0 585
2363 }
2364 }
2365 744 ▶ if (field) {
Branch: 1 branch
BranchTrueFalseTotal
if (field) { 744 0 744
2366 /* Borrowing the field's own buffer is only safe while
2367 nothing in this loop can run Perl -- a later key that
2368 stringifies through an overload could grow an earlier one
2369 in place and free the PV the comparator still points at.
2370 For plain scalars nothing can, which is the common case
2371 and the one worth keeping fast; the moment something
2372 could, take ownership of this key and of every key
2373 already borrowed (still valid: no Perl has run yet). */
2374 744 ▶ if (borrow && TS_RUNS_PERL(field)) {
Branch: 3 branches
BranchTrueFalseTotal
if (borrow && TS_RUNS_PERL(field)) { 708 36 744
if (borrow && TS_RUNS_PERL(field)) { 706 2 708
if (borrow && TS_RUNS_PERL(field)) { 7 699 706
2375 9 ts_own_keys(aTHX_ all_keys, all_lens, i * nsort + k);
2376 9 borrow = 0;
2377 }
2378 /* Stringifying undef is not free: perl reports it, and a
2379 __WARN__ handler is Perl running inside the borrow
2380 window. The other render paths already skip undef rather
2381 than warn, so match them and leave the key empty. */
2382 744 ▶ if (!borrow) SvGETMAGIC(field);
Branch: 1 branch
BranchTrueFalseTotal
if (!borrow) SvGETMAGIC(field); 45 699 744
2383 744 ▶ if (!SvOK(field)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(field)) continue; 2 742 744
2384 STRLEN flen;
2385 742 const char *fp = SvPV_nomg(field, flen);
2386 742 ▶ if (!borrow) {
Branch: 1 branch
BranchTrueFalseTotal
if (!borrow) { 45 697 742
2387 45 SV *owned = sv_2mortal(newSVpvn(fp, flen));
2388 45 fp = SvPVX(owned);
2389 }
2390 742 entries[i].keys[k] = fp;
2391 742 entries[i].key_lens[k] = flen;
2392 }
2393 }
2394 }
2395 }
2396  
2397 79 ▶ int (*cmp)(const void *, const void *) = descending ? sort_cmp_desc : sort_cmp_asc;
Branch: 1 branch
BranchTrueFalseTotal
int (*cmp)(const void *, const void *) = descending ? sort_cmp_desc : sort_cmp_asc; 15 64 79
2398 79 ▶ if (nrows > 1) qsort(entries, nrows, sizeof(sort_entry), cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (nrows > 1) qsort(entries, nrows, sizeof(sort_entry), cmp); 75 4 79
2399  
2400 STRLEN alloc, pos;
2401 char *buf;
2402 79 ▶ RBUF_INIT(t, ts_est(t->header_len + t->footer_len, nrows));
Branch: 3 branches
BranchTrueFalseTotal
RBUF_INIT(t, ts_est(t->header_len + t->footer_len, nrows)); 28 51 79
RBUF_INIT(t, ts_est(t->header_len + t->footer_len, nrows)); 28 0 28
RBUF_INIT(t, ts_est(t->header_len + t->footer_len, nrows)); 0 51 51
2403 79 TS_INFLIGHT_ARM(t);
2404  
2405 79 ▶ BUF_WRITE(t->header, t->header_len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_WRITE(t->header, t->header_len); 1 78 79
BUF_WRITE(t->header, t->header_len); 0 1 1
BUF_WRITE(t->header, t->header_len); 0 0 0
BUF_WRITE(t->header, t->header_len); 0 0 0
2406 79 int first = 1;
2407 1300 ▶ for (SSize_t i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (SSize_t i = 0; i < nrows; i++) { 1221 79 1300
2408 1221 SV *row_sv = entries[i].sv;
2409 1221 ▶ if (should_skip_row(aTHX_ row_sv, t)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (should_skip_row(aTHX_ row_sv, t)) continue; 4 1217 1221
2410 1217 ▶ if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len);
Branch: 6 branches
BranchTrueFalseTotal
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 1139 78 1217
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 1029 110 1139
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 1029 0 1029
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 0 1029 1029
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 0 0 0
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 0 0 0
2411 1217 first = 0;
2412 4706 ▶ TS_EMIT_OPS(row_sv, i);
Branch: 6 branches
BranchTrueFalseTotal
TS_EMIT_OPS(row_sv, i); 2232 1257 3489
TS_EMIT_OPS(row_sv, i); 2232 0 2232
TS_EMIT_OPS(row_sv, i); 0 2232 2232
TS_EMIT_OPS(row_sv, i); 0 0 0
TS_EMIT_OPS(row_sv, i); 0 0 0
TS_EMIT_OPS(row_sv, i); 3489 1217 4706
2413 }
2414 79 t->last_row_count = nrows;
2415 79 ▶ BUF_WRITE(t->footer, t->footer_len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_WRITE(t->footer, t->footer_len); 0 79 79
BUF_WRITE(t->footer, t->footer_len); 0 0 0
BUF_WRITE(t->footer, t->footer_len); 0 0 0
BUF_WRITE(t->footer, t->footer_len); 0 0 0
2416 79 TS_INFLIGHT_DISARM(t);
2417 79 ▶ RBUF_FINISH(t);
Branch: 4 branches
BranchTrueFalseTotal
RBUF_FINISH(t); 0 79 79
RBUF_FINISH(t); 0 79 79
RBUF_FINISH(t); 0 0 0
RBUF_FINISH(t); 0 0 0
2418 79 SV *result = tpl_out_sv(aTHX_ t, buf, pos);
2419 79 LEAVE; /* the sort scratch: entries/all_keys/all_lens */
2420 79 TS_ENC_LEAVE(t); /* then the encoding state this render saved */
2421 79 return result;
2422 }
2423  
2424 81005 static SV *tpl_render_one(pTHX_ tpl_compiled *t, SV *row_sv) {
2425 81005 TS_ENC_ENTER(t);
2426 81005 ▶ TS_PIN_ROW(row_sv);
Branch: 1 branch
BranchTrueFalseTotal
TS_PIN_ROW(row_sv); 80905 100 81005
2427 81005 ▶ if (should_skip_row(aTHX_ row_sv, t)) {
Branch: 1 branch
BranchTrueFalseTotal
if (should_skip_row(aTHX_ row_sv, t)) { 2 81003 81005
2428 2 TS_ENC_LEAVE(t); /* a skipped row must not eat an outer render's state */
2429 2 return newSVpvn("", 0);
2430 }
2431 STRLEN alloc, pos;
2432 char *buf;
2433 81003 ▶ RBUF_INIT(t, t->header_len + t->footer_len + 512);
Branch: 3 branches
BranchTrueFalseTotal
RBUF_INIT(t, t->header_len + t->footer_len + 512); 40503 40500 81003
RBUF_INIT(t, t->header_len + t->footer_len + 512); 40503 0 40503
RBUF_INIT(t, t->header_len + t->footer_len + 512); 0 40500 40500
2434 81003 TS_INFLIGHT_ARM(t);
2435 81003 ▶ BUF_WRITE(t->header, t->header_len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_WRITE(t->header, t->header_len); 3 81000 81003
BUF_WRITE(t->header, t->header_len); 0 3 3
BUF_WRITE(t->header, t->header_len); 0 0 0
BUF_WRITE(t->header, t->header_len); 0 0 0
2436 202628 ▶ TS_EMIT_OPS(row_sv, 0);
Branch: 6 branches
BranchTrueFalseTotal
TS_EMIT_OPS(row_sv, 0); 60820 81005 141825
TS_EMIT_OPS(row_sv, 0); 60820 0 60820
TS_EMIT_OPS(row_sv, 0); 0 60820 60820
TS_EMIT_OPS(row_sv, 0); 0 0 0
TS_EMIT_OPS(row_sv, 0); 0 0 0
TS_EMIT_OPS(row_sv, 0); 141825 60803 202628
2437 60803 ▶ BUF_WRITE(t->footer, t->footer_len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_WRITE(t->footer, t->footer_len); 2 60801 60803
BUF_WRITE(t->footer, t->footer_len); 0 2 2
BUF_WRITE(t->footer, t->footer_len); 0 0 0
BUF_WRITE(t->footer, t->footer_len); 0 0 0
2438 60803 TS_INFLIGHT_DISARM(t);
2439 60803 ▶ RBUF_FINISH(t);
Branch: 4 branches
BranchTrueFalseTotal
RBUF_FINISH(t); 0 60803 60803
RBUF_FINISH(t); 0 60803 60803
RBUF_FINISH(t); 0 0 0
RBUF_FINISH(t); 0 0 0
2440 60803 { SV *res_ = tpl_out_sv(aTHX_ t, buf, pos); TS_ENC_LEAVE(t); return res_; }
2441 }
2442  
2443 415 static void tpl_render_to_fh(pTHX_ tpl_compiled *t, AV *rows, PerlIO *fh) {
2444 415 TS_ENC_ENTER(t);
2445 415 TS_PIN_AV(rows);
2446 415 SAVETMPS;
2447 415 SSize_t nrows = av_count(rows);
2448 415 t->last_row_count = nrows;
2449 /* This streams, flushing every 64KB, so it never needs the whole output
2450 resident -- sizing it from the row count made a million-row render
2451 reserve hundreds of MB and hit an address-space limit that render_cb,
2452 which uses a fixed buffer, sails through. */
2453 415 STRLEN est = ts_est(t->header_len + t->footer_len, nrows);
2454 415 STRLEN cap = t->header_len + t->footer_len + 65536 + 1;
2455 415 STRLEN alloc = est < cap ? est : cap;
2456 415 char *buf = (char *)malloc(alloc);
2457 415 ▶ if (!buf) croak("Text::Stencil: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) croak("Text::Stencil: out of memory"); 0 415 415
2458 415 TS_INFLIGHT_ARM(t);
2459 415 STRLEN pos = 0;
2460 415 ▶ BUF_WRITE(t->header, t->header_len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_WRITE(t->header, t->header_len); 1 414 415
BUF_WRITE(t->header, t->header_len); 0 1 1
BUF_WRITE(t->header, t->header_len); 0 0 0
BUF_WRITE(t->header, t->header_len); 0 0 0
2461 415 int first = 1;
2462 201933 ▶ for (SSize_t i = 0; i < nrows; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (SSize_t i = 0; i < nrows; i++) { 201919 14 201933
2463 SV *row_sv;
2464 201919 ▶ if (!ts_row_at(aTHX_ rows, i, &row_sv)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!ts_row_at(aTHX_ rows, i, &row_sv)) break; 1 201918 201919
2465 201918 ▶ TS_PIN_ROW(row_sv);
Branch: 1 branch
BranchTrueFalseTotal
TS_PIN_ROW(row_sv); 201418 500 201918
2466 201918 ▶ if (!should_skip_row(aTHX_ row_sv, t)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!should_skip_row(aTHX_ row_sv, t)) { 201818 100 201918
2467 201818 ▶ if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len);
Branch: 6 branches
BranchTrueFalseTotal
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 201404 414 201818
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 1302 200102 201404
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 1302 0 1302
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 0 1302 1302
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 0 0 0
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 0 0 0
2468 201818 first = 0;
2469 605464 ▶ TS_EMIT_OPS(row_sv, i);
Branch: 6 branches
BranchTrueFalseTotal
TS_EMIT_OPS(row_sv, i); 202124 201922 404046
TS_EMIT_OPS(row_sv, i); 202124 0 202124
TS_EMIT_OPS(row_sv, i); 1 202123 202124
TS_EMIT_OPS(row_sv, i); 1 0 1
TS_EMIT_OPS(row_sv, i); 0 1 1
TS_EMIT_OPS(row_sv, i); 404046 201418 605464
2470 201418 ▶ if (pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; FREETMPS; }
Branch: 5 branches
BranchTrueFalseTotal
if (pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; FREETMPS; } 22 201396 201418
if (pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; FREETMPS; } 22 0 22
if (pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; FREETMPS; } 22 0 22
if (pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; FREETMPS; } 0 22 22
if (pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; FREETMPS; } 22 0 22
2471 }
2472 201518 ▶ if (UNLIKELY((i & 63) == 63)) FREETMPS;
Branch: 2 branches
BranchTrueFalseTotal
if (UNLIKELY((i & 63) == 63)) FREETMPS; 3146 198372 201518
if (UNLIKELY((i & 63) == 63)) FREETMPS; 3145 1 3146
2473 }
2474 15 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 14 1 15
2475 15 t->last_row_count = nrows;
2476 15 ▶ BUF_WRITE(t->footer, t->footer_len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_WRITE(t->footer, t->footer_len); 1 14 15
BUF_WRITE(t->footer, t->footer_len); 0 1 1
BUF_WRITE(t->footer, t->footer_len); 0 0 0
BUF_WRITE(t->footer, t->footer_len); 0 0 0
2477 15 ▶ if (pos) FH_WRITE(fh, buf, pos);
Branch: 4 branches
BranchTrueFalseTotal
if (pos) FH_WRITE(fh, buf, pos); 14 1 15
if (pos) FH_WRITE(fh, buf, pos); 13 1 14
if (pos) FH_WRITE(fh, buf, pos); 12 1 13
if (pos) FH_WRITE(fh, buf, pos); 0 12 12
2478 13 TS_INFLIGHT_DISARM(t);
2479 13 free(buf);
2480 13 TS_ENC_LEAVE(t);
2481 13 }
2482  
2483 25433 static SV *tpl_render_cb(pTHX_ tpl_compiled *t, SV *cb, PerlIO *fh) {
2484 25433 TS_ENC_ENTER(t);
2485 /* The next callback's FREETMPS also releases the previous row's fields. */
2486 25433 SAVETMPS;
2487 STRLEN alloc, pos;
2488 char *buf;
2489 25433 int use_fh = (fh != NULL);
2490  
2491 25433 ▶ if (use_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (use_fh) { 5206 20227 25433
2492 5206 alloc = 65536;
2493 5206 buf = (char *)malloc(alloc);
2494 5206 ▶ if (!buf) croak("Text::Stencil: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) croak("Text::Stencil: out of memory"); 0 5206 5206
2495 } else {
2496 20227 ▶ RBUF_INIT(t, 4096);
Branch: 3 branches
BranchTrueFalseTotal
RBUF_INIT(t, 4096); 20209 18 20227
RBUF_INIT(t, 4096); 20207 2 20209
RBUF_INIT(t, 4096); 0 20 20
2497 }
2498 25433 TS_INFLIGHT_ARM(t);
2499 /* Where our savestack frame starts. Nothing should reach it now that the
2500 callback runs on its own stackinfo, but a mis-guessed unwind here costs
2501 a heap corruption, so the check stays as a backstop. */
2502 25433 I32 ts_floor = PL_savestack_ix;
2503 25433 pos = 0;
2504  
2505 25433 ▶ BUF_WRITE(t->header, t->header_len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_WRITE(t->header, t->header_len); 1 25432 25433
BUF_WRITE(t->header, t->header_len); 0 1 1
BUF_WRITE(t->header, t->header_len); 0 0 0
BUF_WRITE(t->header, t->header_len); 0 0 0
2506 25433 SSize_t row_idx = 0;
2507 25433 int first = 1;
2508 25433 t->last_row_count = 0;
2509  
2510 /* Run the callback on a stackinfo of its own. last/next/goto out of a sub
2511 unwinds by scanning the current stackinfo's context stack; without this
2512 it walks straight through our C frame, panicking in pp_iter or resuming
2513 in our caller with the render half-done. A fresh stackinfo has no context
2514 to find, so the jump becomes a "Label not found" die that G_EVAL catches.
2515 Core contains a user sub called from C the same way (magic_methcall,
2516 amagic_call, the warn hook). Hoisted out of the loop: per-iteration it
2517 costs ~5%, here one push per render_cb call. */
2518 25433 dSP;
2519 25433 PUSHSTACKi(PERLSI_MAGIC);
2520  
2521 60909 while (1) {
2522 86342 SPAGAIN;
2523 86342 ENTER;
2524 86342 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 86342 86342
2525 86342 PUTBACK;
2526 86342 int count = call_sv(cb, G_SCALAR | G_EVAL);
2527 86342 SPAGAIN;
2528 /* Backstop: the stackinfo above contains the jumps that used to get
2529 here. If it ever fires our frame is already gone and `buf` dangles,
2530 so touching it -- or running LEAVE -- would corrupt the heap. Return
2531 and let perl finish its unwind; the stackinfo is left for die_unwind,
2532 since getting here means perl is already mid-longjmp. */
2533 86342 ▶ if (UNLIKELY(PL_savestack_ix < ts_floor)) return &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY(PL_savestack_ix < ts_floor)) return &PL_sv_undef; 0 86342 86342
2534 86342 ▶ if (SvROK(ERRSV) || (SvPOK(ERRSV) && SvCUR(ERRSV))) {
Branch: 6 branches
BranchTrueFalseTotal
if (SvROK(ERRSV) || (SvPOK(ERRSV) && SvCUR(ERRSV))) { 86342 0 86342
if (SvROK(ERRSV) || (SvPOK(ERRSV) && SvCUR(ERRSV))) { 86340 2 86342
if (SvROK(ERRSV) || (SvPOK(ERRSV) && SvCUR(ERRSV))) { 86340 0 86340
if (SvROK(ERRSV) || (SvPOK(ERRSV) && SvCUR(ERRSV))) { 86340 0 86340
if (SvROK(ERRSV) || (SvPOK(ERRSV) && SvCUR(ERRSV))) { 86340 0 86340
if (SvROK(ERRSV) || (SvPOK(ERRSV) && SvCUR(ERRSV))) { 6 86334 86340
2535 /* The row callback died: free our render buffer (the fh path's
2536 own malloc, or the detached render_buf) before propagating, so
2537 a dying callback can't leak it. */
2538 8 ▶ SV *err = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
SV *err = newSVsv(ERRSV); 8 0 8
2539 8 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 8 0 8
2540 8 POPSTACK;
2541 8 TS_INFLIGHT_DISARM(t);
2542 8 free(buf);
2543 8 croak_sv(sv_2mortal(err));
2544 }
2545 86334 SV *row_sv = NULL;
2546 86334 ▶ if (count > 0) row_sv = POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) row_sv = POPs; 86334 0 86334
2547 /* only an arrayref or hashref is a row; accepting any reference let a
2548 stray scalarref render an empty row forever */
2549 86334 ▶ if (!row_sv || !SvOK(row_sv) || !SvROK(row_sv) ||
Branch: 3 branches
BranchTrueFalseTotal
if (!row_sv || !SvOK(row_sv) || !SvROK(row_sv) || 86334 0 86334
if (!row_sv || !SvOK(row_sv) || !SvROK(row_sv) || 66118 20216 86334
if (!row_sv || !SvOK(row_sv) || !SvROK(row_sv) || 66116 2 66118
2550 66116 ▶ (SvTYPE(SvRV(row_sv)) != SVt_PVAV && SvTYPE(SvRV(row_sv)) != SVt_PVHV)) {
Branch: 2 branches
BranchTrueFalseTotal
(SvTYPE(SvRV(row_sv)) != SVt_PVAV && SvTYPE(SvRV(row_sv)) != SVt_PVHV)) { 5472 60644 66116
(SvTYPE(SvRV(row_sv)) != SVt_PVAV && SvTYPE(SvRV(row_sv)) != SVt_PVHV)) { 7 5465 5472
2551 20225 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 20225 0 20225
2552 20225 break;
2553 }
2554 66109 SvREFCNT_inc_simple_void_NN(row_sv);
2555 66109 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 66109 0 66109
2556  
2557 66109 ENTER;
2558 66109 SAVEFREESV(row_sv);
2559 66109 ▶ if (!should_skip_row(aTHX_ row_sv, t)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!should_skip_row(aTHX_ row_sv, t)) { 66107 2 66109
2560 66107 ▶ if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len);
Branch: 6 branches
BranchTrueFalseTotal
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 40682 25425 66107
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 207 40475 40682
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 207 0 207
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 0 207 207
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 0 0 0
if (!first && t->sep_len) BUF_WRITE(t->sep, t->sep_len); 0 0 0
2561 66107 first = 0;
2562 187786 ▶ TS_EMIT_OPS(row_sv, row_idx);
Branch: 6 branches
BranchTrueFalseTotal
TS_EMIT_OPS(row_sv, row_idx); 60764 66115 126879
TS_EMIT_OPS(row_sv, row_idx); 60764 0 60764
TS_EMIT_OPS(row_sv, row_idx); 3 60761 60764
TS_EMIT_OPS(row_sv, row_idx); 3 0 3
TS_EMIT_OPS(row_sv, row_idx); 0 3 3
TS_EMIT_OPS(row_sv, row_idx); 126879 60907 187786
2563 60907 ▶ if (use_fh && pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; }
Branch: 5 branches
BranchTrueFalseTotal
if (use_fh && pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; } 107 60800 60907
if (use_fh && pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; } 0 107 107
if (use_fh && pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; } 0 0 0
if (use_fh && pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; } 0 0 0
if (use_fh && pos > 65536) { FH_WRITE(fh, buf, pos); pos = 0; } 0 0 0
2564 }
2565 60909 LEAVE;
2566 60909 row_idx++;
2567 60909 t->last_row_count = row_idx;
2568 }
2569  
2570 20225 POPSTACK;
2571  
2572 20225 t->last_row_count = row_idx;
2573 20225 ▶ BUF_WRITE(t->footer, t->footer_len);
Branch: 4 branches
BranchTrueFalseTotal
BUF_WRITE(t->footer, t->footer_len); 1 20224 20225
BUF_WRITE(t->footer, t->footer_len); 0 1 1
BUF_WRITE(t->footer, t->footer_len); 0 0 0
BUF_WRITE(t->footer, t->footer_len); 0 0 0
2574  
2575 20225 ▶ if (use_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (use_fh) { 4 20221 20225
2576 4 ▶ if (pos) FH_WRITE(fh, buf, pos);
Branch: 4 branches
BranchTrueFalseTotal
if (pos) FH_WRITE(fh, buf, pos); 4 0 4
if (pos) FH_WRITE(fh, buf, pos); 4 0 4
if (pos) FH_WRITE(fh, buf, pos); 4 0 4
if (pos) FH_WRITE(fh, buf, pos); 0 4 4
2577 4 TS_INFLIGHT_DISARM(t);
2578 4 free(buf);
2579 4 TS_ENC_LEAVE(t);
2580 4 return &PL_sv_undef;
2581 } else {
2582 20221 TS_INFLIGHT_DISARM(t);
2583 20221 ▶ RBUF_FINISH(t);
Branch: 4 branches
BranchTrueFalseTotal
RBUF_FINISH(t); 20201 20 20221
RBUF_FINISH(t); 0 20221 20221
RBUF_FINISH(t); 0 0 0
RBUF_FINISH(t); 0 0 0
2584 20221 { SV *res_ = tpl_out_sv(aTHX_ t, buf, pos); TS_ENC_LEAVE(t); return res_; }
2585 }
2586 }
2587  
2588 /* columns introspection; returns an owning RV, since the AV* OUTPUT typemap
2589 would wrap RETVAL with the refcount-incrementing newRV() and leak the AV */
2590 220007 static SV *tpl_columns(pTHX_ tpl_compiled *t) {
2591 220007 AV *cols = newAV();
2592 /* each field once, in first-appearance order */
2593 1320030 ▶ for (int i = 0; i < t->nops; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < t->nops; i++) { 1100023 220007 1320030
2594 1100023 tpl_op *op = &t->ops[i];
2595 1100023 ▶ if (!op->chain || op->is_rownum) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!op->chain || op->is_rownum) continue; 660016 440007 1100023
if (!op->chain || op->is_rownum) continue; 1 660015 660016
2596 660015 int seen = 0;
2597 1980033 ▶ for (int j = 0; j < i && !seen; j++) {
Branch: 2 branches
BranchTrueFalseTotal
for (int j = 0; j < i && !seen; j++) { 1320021 660012 1980033
for (int j = 0; j < i && !seen; j++) { 1320018 3 1320021
2598 1320018 tpl_op *p = &t->ops[j];
2599 1320018 ▶ if (!p->chain || p->is_rownum) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!p->chain || p->is_rownum) continue; 660011 660007 1320018
if (!p->chain || p->is_rownum) continue; 2 660009 660011
2600 660009 ▶ if (op->key)
Branch: 1 branch
BranchTrueFalseTotal
if (op->key) 3 660006 660009
2601 5 ▶ seen = p->key && p->key_len == op->key_len &&
Branch: 2 branches
BranchTrueFalseTotal
seen = p->key && p->key_len == op->key_len && 3 0 3
seen = p->key && p->key_len == op->key_len && 2 1 3
2602 2 ▶ memcmp(p->key, op->key, op->key_len) == 0;
Branch: 1 branch
BranchTrueFalseTotal
memcmp(p->key, op->key, op->key_len) == 0; 1 1 2
2603 else
2604 660006 ▶ seen = !p->key && p->col == op->col;
Branch: 2 branches
BranchTrueFalseTotal
seen = !p->key && p->col == op->col; 660006 0 660006
seen = !p->key && p->col == op->col; 2 660004 660006
2605 }
2606 660015 ▶ if (seen) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (seen) continue; 3 660012 660015
2607 /* the flag matters: callers index their own rows with these, and a
2608 flagged key and its bytes are not the same hash key */
2609 660012 ▶ if (op->key) av_push(cols, newSVpvn_utf8(op->key, op->key_len, op->key_utf8));
Branch: 2 branches
BranchTrueFalseTotal
if (op->key) av_push(cols, newSVpvn_utf8(op->key, op->key_len, op->key_utf8)); 6 660006 660012
if (op->key) av_push(cols, newSVpvn_utf8(op->key, op->key_len, op->key_utf8)); 2 4 6
2610 660006 else av_push(cols, newSViv(op->col));
2611 }
2612 220007 return newRV_noinc((SV *)cols);
2613 }
2614  
2615  
2616 MODULE = Text::Stencil PACKAGE = Text::Stencil
2617  
2618 SV *
2619 new(class, ...)
2620 const char *class
2621 CODE:
2622 {
2623 84710 const char *header = "", *row = "", *footer = "", *sep = "";
2624 84710 STRLEN hlen = 0, rlen = 0, flen = 0, slen = 0;
2625 84710 char esc = 0;
2626 84710 int tpl_utf8 = 0;
2627 /* `class` is a raw PV borrowed from ST(0); an overloaded "" among the
2628 arguments can free it before we get here, so resolve the stash now */
2629 /* called as an instance method, `class` stringifies to
2630 "Text::Stencil=SCALAR(0x...)" and gv_stashpv would mint that stash */
2631 1 ▶ HV *class_stash = (SvROK(ST(0)) && SvOBJECT(SvRV(ST(0))))
Branch: 1 branch
BranchTrueFalseTotal
HV *class_stash = (SvROK(ST(0)) && SvOBJECT(SvRV(ST(0)))) 1 0 1
2632 84711 ▶ ? SvSTASH(SvRV(ST(0))) : gv_stashpv(class, GV_ADD);
Branch: 1 branch
BranchTrueFalseTotal
? SvSTASH(SvRV(ST(0))) : gv_stashpv(class, GV_ADD); 1 84709 84710
2633 84710 char err[TS_ERRSZ]; err[0] = '\0';
2634 84710 SV *skip_if_sv = NULL, *skip_unless_sv = NULL;
2635 /* shorthand: Text::Stencil->new($row_template) */
2636 84710 ▶ if (items == 2 && SvOK(ST(1)) && !SvROK(ST(1))) {
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && SvOK(ST(1)) && !SvROK(ST(1))) { 9 84701 84710
if (items == 2 && SvOK(ST(1)) && !SvROK(ST(1))) { 8 1 9
if (items == 2 && SvOK(ST(1)) && !SvROK(ST(1))) { 6 2 8
2637 6 row = ts_own_pv(aTHX_ ST(1), &rlen, NULL);
2638 6 ▶ if (SvUTF8(ST(1))) tpl_utf8 |= TS_TPL_ROW;
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(ST(1))) tpl_utf8 |= TS_TPL_ROW; 0 6 6
2639 } else {
2640 /* one argument that is not a plain string is neither shorthand nor an
2641 option list; "odd number of arguments" described the wrong mistake */
2642 84704 ▶ if (items == 2) croak("Text::Stencil: new takes a template string or an"
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) croak("Text::Stencil: new takes a template string or an" 3 84701 84704
2643 " option list");
2644 84701 ▶ if (items % 2 == 0) croak("Text::Stencil: odd number of arguments");
Branch: 1 branch
BranchTrueFalseTotal
if (items % 2 == 0) croak("Text::Stencil: odd number of arguments"); 1 84700 84701
2645 201557 ▶ for (int i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 1; i < items; i += 2) { 116866 84691 201557
2646 116866 const char *key = SvPV_nolen(ST(i));
2647 116866 SV *val = ST(i + 1);
2648 /* an overloaded "" on a later argument can free an earlier one, and we
2649 keep raw PVs until tpl_compile copies them */
2650 116866 sv_2mortal(SvREFCNT_inc_simple_NN(val));
2651 116866 ▶ if (strcmp(key, "header") == 0) { int u_; header = ts_own_pv(aTHX_ val, &hlen, &u_); if (u_) tpl_utf8 |= TS_TPL_HDR; else tpl_utf8 &= ~TS_TPL_HDR; }
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(key, "header") == 0) { int u_; header = ts_own_pv(aTHX_ val, &hlen, &u_); if (u_) tpl_utf8 |= TS_TPL_HDR; else tpl_utf8 &= ~TS_TPL_HDR; } 25 116841 116866
if (strcmp(key, "header") == 0) { int u_; header = ts_own_pv(aTHX_ val, &hlen, &u_); if (u_) tpl_utf8 |= TS_TPL_HDR; else tpl_utf8 &= ~TS_TPL_HDR; } 6 19 25
2652 116841 ▶ else if (strcmp(key, "row") == 0) { int u_; row = ts_own_pv(aTHX_ val, &rlen, &u_); if (u_) tpl_utf8 |= TS_TPL_ROW; else tpl_utf8 &= ~TS_TPL_ROW; }
Branch: 2 branches
BranchTrueFalseTotal
else if (strcmp(key, "row") == 0) { int u_; row = ts_own_pv(aTHX_ val, &rlen, &u_); if (u_) tpl_utf8 |= TS_TPL_ROW; else tpl_utf8 &= ~TS_TPL_ROW; } 84702 32139 116841
else if (strcmp(key, "row") == 0) { int u_; row = ts_own_pv(aTHX_ val, &rlen, &u_); if (u_) tpl_utf8 |= TS_TPL_ROW; else tpl_utf8 &= ~TS_TPL_ROW; } 25 84677 84702
2653 32139 ▶ else if (strcmp(key, "footer") == 0) { int u_; footer = ts_own_pv(aTHX_ val, &flen, &u_); if (u_) tpl_utf8 |= TS_TPL_FTR; else tpl_utf8 &= ~TS_TPL_FTR; }
Branch: 2 branches
BranchTrueFalseTotal
else if (strcmp(key, "footer") == 0) { int u_; footer = ts_own_pv(aTHX_ val, &flen, &u_); if (u_) tpl_utf8 |= TS_TPL_FTR; else tpl_utf8 &= ~TS_TPL_FTR; } 21 32118 32139
else if (strcmp(key, "footer") == 0) { int u_; footer = ts_own_pv(aTHX_ val, &flen, &u_); if (u_) tpl_utf8 |= TS_TPL_FTR; else tpl_utf8 &= ~TS_TPL_FTR; } 3 18 21
2654 32118 ▶ else if (strcmp(key, "separator") == 0) { int u_; sep = ts_own_pv(aTHX_ val, &slen, &u_); if (u_) tpl_utf8 |= TS_TPL_SEP; else tpl_utf8 &= ~TS_TPL_SEP; }
Branch: 2 branches
BranchTrueFalseTotal
else if (strcmp(key, "separator") == 0) { int u_; sep = ts_own_pv(aTHX_ val, &slen, &u_); if (u_) tpl_utf8 |= TS_TPL_SEP; else tpl_utf8 &= ~TS_TPL_SEP; } 52 32066 32118
else if (strcmp(key, "separator") == 0) { int u_; sep = ts_own_pv(aTHX_ val, &slen, &u_); if (u_) tpl_utf8 |= TS_TPL_SEP; else tpl_utf8 &= ~TS_TPL_SEP; } 3 49 52
2655 32066 ▶ else if (strcmp(key, "escape_char") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(key, "escape_char") == 0) { 17 32049 32066
2656 /* one byte, or the template silently stops substituting entirely */
2657 17 STRLEN el; const char *ev = SvPV(val, el);
2658 17 ▶ if (el != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (el != 1) 6 11 17
2659 6 croak("Text::Stencil: escape_char must be a single byte, got %" UVuf,
2660 (UV)el);
2661 /* a NUL fell through the `esc_char ? esc_char : '{'` default below
2662 and silently meant '{', which is the quiet no-op this option's
2663 single-byte check exists to prevent */
2664 11 ▶ if (ev[0] == '\0')
Branch: 1 branch
BranchTrueFalseTotal
if (ev[0] == '\0') 1 10 11
2665 1 croak("Text::Stencil: escape_char must not be NUL");
2666 10 esc = ev[0];
2667 }
2668 32049 ▶ else if (strcmp(key, "skip_if") == 0) skip_if_sv = val;
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(key, "skip_if") == 0) skip_if_sv = val; 32035 14 32049
2669 14 ▶ else if (strcmp(key, "skip_unless") == 0) skip_unless_sv = val;
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(key, "skip_unless") == 0) skip_unless_sv = val; 12 2 14
2670 /* a silently dropped option is how a typo becomes an empty table */
2671 2 else croak("Text::Stencil: unknown option '%s'", key);
2672 }
2673 }
2674 /* Resolve the skip conditions before compiling anything. Deciding whether
2675 one is a column or a key, and stringifying it, both run user Perl that
2676 can die -- and a die after tpl_compile strands the whole template, which
2677 is not yet owned by any SV that could free it. */
2678 84697 int skip_if_isnum = 0, skip_if_u = 0, skip_unless_isnum = 0, skip_unless_u = 0;
2679 84697 int skip_if_col = 0, skip_unless_col = 0;
2680 84697 const char *skip_if_ks = NULL, *skip_unless_ks = NULL;
2681 84697 STRLEN skip_if_kl = 0, skip_unless_kl = 0;
2682 84697 ▶ if (skip_if_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (skip_if_sv) { 32035 52662 84697
2683 32035 ▶ if (SvIOK(skip_if_sv) || looks_like_number(skip_if_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(skip_if_sv) || looks_like_number(skip_if_sv)) { 32010 25 32035
if (SvIOK(skip_if_sv) || looks_like_number(skip_if_sv)) { 3 32007 32010
2684 28 skip_if_isnum = 1;
2685 28 skip_if_col = ts_col_sv(aTHX_ skip_if_sv, "skip_if");
2686 32007 } else skip_if_ks = ts_own_pv(aTHX_ skip_if_sv, &skip_if_kl, &skip_if_u);
2687 }
2688 52690 ▶ if (skip_unless_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (skip_unless_sv) { 12 52678 52690
2689 12 ▶ if (SvIOK(skip_unless_sv) || looks_like_number(skip_unless_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(skip_unless_sv) || looks_like_number(skip_unless_sv)) { 7 5 12
if (SvIOK(skip_unless_sv) || looks_like_number(skip_unless_sv)) { 0 7 7
2690 5 skip_unless_isnum = 1;
2691 5 skip_unless_col = ts_col_sv(aTHX_ skip_unless_sv, "skip_unless");
2692 7 } else skip_unless_ks = ts_own_pv(aTHX_ skip_unless_sv, &skip_unless_kl, &skip_unless_u);
2693 }
2694  
2695 52688 tpl_compiled *t = tpl_compile(aTHX_ header, hlen, row, rlen, footer, flen, sep, slen, esc, tpl_utf8, err);
2696 52688 ▶ if (!t || err[0]) { if (t) tpl_free(t); croak("Text::Stencil: %s", err); }
Branch: 3 branches
BranchTrueFalseTotal
if (!t || err[0]) { if (t) tpl_free(t); croak("Text::Stencil: %s", err); } 52688 0 52688
if (!t || err[0]) { if (t) tpl_free(t); croak("Text::Stencil: %s", err); } 52045 643 52688
if (!t || err[0]) { if (t) tpl_free(t); croak("Text::Stencil: %s", err); } 52045 0 52045
2697 643 ▶ if (skip_if_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (skip_if_sv) { 28 615 643
2698 28 ▶ TS_CHECK_KIND(t, "skip_if", !skip_if_isnum);
Branch: 6 branches
BranchTrueFalseTotal
TS_CHECK_KIND(t, "skip_if", !skip_if_isnum); 6 22 28
TS_CHECK_KIND(t, "skip_if", !skip_if_isnum); 0 6 6
TS_CHECK_KIND(t, "skip_if", !skip_if_isnum); 0 0 0
TS_CHECK_KIND(t, "skip_if", !skip_if_isnum); 22 6 28
TS_CHECK_KIND(t, "skip_if", !skip_if_isnum); 1 21 22
TS_CHECK_KIND(t, "skip_if", !skip_if_isnum); 1 0 1
2699 27 t->has_skip_if = 1;
2700 27 ▶ if (skip_if_isnum) t->skip_if_col = skip_if_col;
Branch: 1 branch
BranchTrueFalseTotal
if (skip_if_isnum) t->skip_if_col = skip_if_col; 21 6 27
2701 else {
2702 6 ts_set_skip_key(aTHX_ t, &t->skip_if_key, &t->skip_if_key_len,
2703 &t->skip_if_utf8, skip_if_ks, skip_if_kl, skip_if_u);
2704 }
2705 }
2706 642 ▶ if (skip_unless_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (skip_unless_sv) { 10 632 642
2707 10 ▶ TS_CHECK_KIND(t, "skip_unless", !skip_unless_isnum);
Branch: 6 branches
BranchTrueFalseTotal
TS_CHECK_KIND(t, "skip_unless", !skip_unless_isnum); 6 4 10
TS_CHECK_KIND(t, "skip_unless", !skip_unless_isnum); 1 5 6
TS_CHECK_KIND(t, "skip_unless", !skip_unless_isnum); 1 0 1
TS_CHECK_KIND(t, "skip_unless", !skip_unless_isnum); 4 5 9
TS_CHECK_KIND(t, "skip_unless", !skip_unless_isnum); 1 3 4
TS_CHECK_KIND(t, "skip_unless", !skip_unless_isnum); 1 0 1
2708 8 t->has_skip_unless = 1;
2709 8 ▶ if (skip_unless_isnum) t->skip_unless_col = skip_unless_col;
Branch: 1 branch
BranchTrueFalseTotal
if (skip_unless_isnum) t->skip_unless_col = skip_unless_col; 3 5 8
2710 else {
2711 5 ts_set_skip_key(aTHX_ t, &t->skip_unless_key, &t->skip_unless_key_len,
2712 &t->skip_unless_utf8, skip_unless_ks, skip_unless_kl,
2713 skip_unless_u);
2714 }
2715 }
2716 640 SV *obj = newSViv(PTR2IV(t));
2717 640 t->owner = obj;
2718 640 sv_magicext(obj, NULL, PERL_MAGIC_ext, &ts_vtbl, (const char *)t, 0);
2719 640 SV *ref = newRV_noinc(obj);
2720 640 sv_bless(ref, class_stash);
2721 640 RETVAL = ref;
2722 }
2723 OUTPUT:
2724 RETVAL
2725  
2726 SV *
2727 render(self, rows)
2728 SV *self
2729 AV *rows
2730 CODE:
2731 {
2732 1620 tpl_compiled *t = TS_PIN_SELF(self);
2733 1603 RETVAL = tpl_render(aTHX_ t, rows);
2734 }
2735 OUTPUT:
2736 RETVAL
2737  
2738 SV *
2739 render_sorted(self, rows, sort_by, ...)
2740 SV *self
2741 AV *rows
2742 SV *sort_by
2743 CODE:
2744 {
2745 102 tpl_compiled *t = TS_PIN_SELF(self);
2746 102 int descending = 0, numeric = 0;
2747 102 ▶ if (items > 4) croak("Text::Stencil: render_sorted takes at most three arguments");
Branch: 1 branch
BranchTrueFalseTotal
if (items > 4) croak("Text::Stencil: render_sorted takes at most three arguments"); 0 102 102
2748 102 ▶ if (items > 3 && SvOK(ST(3))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 3 && SvOK(ST(3))) { 20 82 102
if (items > 3 && SvOK(ST(3))) { 20 0 20
2749 20 ▶ if (!SvROK(ST(3)) || SvTYPE(SvRV(ST(3))) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(3)) || SvTYPE(SvRV(ST(3))) != SVt_PVHV) 19 1 20
if (!SvROK(ST(3)) || SvTYPE(SvRV(ST(3))) != SVt_PVHV) 0 19 19
2750 1 croak("Text::Stencil: render_sorted options must be a hashref");
2751 19 HV *opts = (HV *)SvRV(ST(3));
2752 SV **sv;
2753 19 sv = hv_fetchs(opts, "descending", 0);
2754 19 ▶ if (sv && SvTRUE(*sv)) descending = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvTRUE(*sv)) descending = 1; 9 10 19
if (sv && SvTRUE(*sv)) descending = 1; 8 1 9
2755 19 sv = hv_fetchs(opts, "numeric", 0);
2756 19 ▶ if (sv && SvTRUE(*sv)) numeric = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvTRUE(*sv)) numeric = 1; 10 9 19
if (sv && SvTRUE(*sv)) numeric = 1; 10 0 10
2757 /* a dropped 'decending' would just sort the other way with nothing to
2758 explain it, the same trap new() and clone() already refuse */
2759 {
2760 char *k; I32 klen; HE *he;
2761 19 hv_iterinit(opts);
2762 38 ▶ while ((he = hv_iternext(opts))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(opts))) { 21 17 38
2763 21 k = hv_iterkey(he, &klen);
2764 21 ▶ if (strcmp(k, "descending") && strcmp(k, "numeric"))
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(k, "descending") && strcmp(k, "numeric")) 12 9 21
if (strcmp(k, "descending") && strcmp(k, "numeric")) 2 10 12
2765 2 croak("Text::Stencil: unknown render_sorted option '%s'", k);
2766 }
2767 }
2768 }
2769 /* Anything else would be stringified into a field name like "HASH(0x...)",
2770 match nothing, and come back unsorted with no complaint -- and a hashref
2771 is an easy mistake to make when the *third* argument really is one. */
2772 99 ▶ if (!SvOK(sort_by))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sort_by)) 1 98 99
2773 1 croak("Text::Stencil: render_sorted needs a column index, a field name, "
2774 "or an arrayref of them");
2775 98 ▶ if (SvROK(sort_by) && !SvAMAGIC(sort_by) && SvTYPE(SvRV(sort_by)) != SVt_PVAV)
Branch: 5 branches
BranchTrueFalseTotal
if (SvROK(sort_by) && !SvAMAGIC(sort_by) && SvTYPE(SvRV(sort_by)) != SVt_PVAV) 32 66 98
if (SvROK(sort_by) && !SvAMAGIC(sort_by) && SvTYPE(SvRV(sort_by)) != SVt_PVAV) 32 0 32
if (SvROK(sort_by) && !SvAMAGIC(sort_by) && SvTYPE(SvRV(sort_by)) != SVt_PVAV) 1 31 32
if (SvROK(sort_by) && !SvAMAGIC(sort_by) && SvTYPE(SvRV(sort_by)) != SVt_PVAV) 0 1 1
if (SvROK(sort_by) && !SvAMAGIC(sort_by) && SvTYPE(SvRV(sort_by)) != SVt_PVAV) 3 28 31
2776 3 croak("Text::Stencil: render_sorted sort spec must be an arrayref, "
2777 "not a %s reference", sv_reftype(SvRV(sort_by), 0));
2778 113 ▶ if (SvROK(sort_by) && SvTYPE(SvRV(sort_by)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sort_by) && SvTYPE(SvRV(sort_by)) == SVt_PVAV) { 29 66 95
if (SvROK(sort_by) && SvTYPE(SvRV(sort_by)) == SVt_PVAV) { 28 1 29
2779 28 AV *sort_av = (AV *)SvRV(sort_by);
2780 28 SSize_t nsort_ = av_count(sort_av);
2781 /* bound it so the element-count arithmetic below cannot wrap */
2782 28 ▶ if (nsort_ < 0 || nsort_ > 1024)
Branch: 2 branches
BranchTrueFalseTotal
if (nsort_ < 0 || nsort_ > 1024) 28 0 28
if (nsort_ < 0 || nsort_ > 1024) 1 27 28
2783 1 croak("Text::Stencil: render_sorted sort spec has too many fields");
2784 27 int nsort = (int)nsort_;
2785 27 ▶ if (nsort == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nsort == 0) { 0 27 27
2786 0 RETVAL = tpl_render(aTHX_ t, rows);
2787 } else {
2788 /* Each element has to be usable on its own. Rejecting only the
2789 scalar form left [undef] and [{}] to be stringified into a field
2790 name nothing matches, coming back unsorted with no complaint --
2791 the very thing the scalar check exists to prevent. */
2792 {
2793 int i_;
2794 61 ▶ for (i_ = 0; i_ < nsort; i_++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i_ = 0; i_ < nsort; i_++) { 38 23 61
2795 38 SV **el_ = av_fetch(sort_av, i_, 0);
2796 38 ▶ if (!el_ || !*el_ || !SvOK(*el_))
Branch: 3 branches
BranchTrueFalseTotal
if (!el_ || !*el_ || !SvOK(*el_)) 38 0 38
if (!el_ || !*el_ || !SvOK(*el_)) 38 0 38
if (!el_ || !*el_ || !SvOK(*el_)) 2 36 38
2797 2 croak("Text::Stencil: render_sorted sort spec element %d "
2798 "is undef", i_);
2799 /* an overloaded object stringifies to a real name, so it is
2800 fine; a plain ref would become "HASH(0x...)" */
2801 36 ▶ if (SvROK(*el_) && !SvAMAGIC(*el_))
Branch: 4 branches
BranchTrueFalseTotal
if (SvROK(*el_) && !SvAMAGIC(*el_)) 4 32 36
if (SvROK(*el_) && !SvAMAGIC(*el_)) 4 0 4
if (SvROK(*el_) && !SvAMAGIC(*el_)) 2 2 4
if (SvROK(*el_) && !SvAMAGIC(*el_)) 0 2 2
2802 2 croak("Text::Stencil: render_sorted sort spec element %d "
2803 "is a %s reference, not a column or field name",
2804 i_, sv_reftype(SvRV(*el_), 0));
2805 }
2806 }
2807 23 SV **first = av_fetch(sort_av, 0, 0);
2808 23 ▶ int use_keys = first && !SvIOK(*first) && !looks_like_number(*first);
Branch: 3 branches
BranchTrueFalseTotal
int use_keys = first && !SvIOK(*first) && !looks_like_number(*first); 23 0 23
int use_keys = first && !SvIOK(*first) && !looks_like_number(*first); 17 6 23
int use_keys = first && !SvIOK(*first) && !looks_like_number(*first); 17 0 17
2809 /* The whole spec is read one way or the other, decided by its first
2810 element. A mixed spec used to push the odd one through the other
2811 path and quietly sort by the wrong field. */
2812 {
2813 int i_;
2814 31 ▶ for (i_ = 1; i_ < nsort; i_++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i_ = 1; i_ < nsort; i_++) { 10 21 31
2815 10 SV **el_ = av_fetch(sort_av, i_, 0);
2816 10 ▶ int k_ = el_ && !SvIOK(*el_) && !looks_like_number(*el_);
Branch: 3 branches
BranchTrueFalseTotal
int k_ = el_ && !SvIOK(*el_) && !looks_like_number(*el_); 10 0 10
int k_ = el_ && !SvIOK(*el_) && !looks_like_number(*el_); 7 3 10
int k_ = el_ && !SvIOK(*el_) && !looks_like_number(*el_); 7 0 7
2817 10 ▶ if (k_ != use_keys)
Branch: 1 branch
BranchTrueFalseTotal
if (k_ != use_keys) 2 8 10
2818 2 croak("Text::Stencil: render_sorted sort spec mixes column "
2819 "indices and field names");
2820 }
2821 }
2822 21 ▶ TS_CHECK_SORT_KIND(use_keys);
Branch: 4 branches
BranchTrueFalseTotal
TS_CHECK_SORT_KIND(use_keys); 16 5 21
TS_CHECK_SORT_KIND(use_keys); 1 15 16
TS_CHECK_SORT_KIND(use_keys); 5 15 20
TS_CHECK_SORT_KIND(use_keys); 1 4 5
2823 19 ▶ if (use_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (use_keys) { 15 4 19
2824 15 const char **skeys = (const char **)malloc(nsort * sizeof(char *));
2825 15 STRLEN *sklens = (STRLEN *)malloc(nsort * sizeof(STRLEN));
2826 15 int *skutf8 = (int *)malloc(nsort * sizeof(int));
2827 15 ▶ if (!skeys || !sklens || !skutf8) { free(skeys); free(sklens); free(skutf8);
Branch: 3 branches
BranchTrueFalseTotal
if (!skeys || !sklens || !skutf8) { free(skeys); free(sklens); free(skutf8); 15 0 15
if (!skeys || !sklens || !skutf8) { free(skeys); free(sklens); free(skutf8); 15 0 15
if (!skeys || !sklens || !skutf8) { free(skeys); free(sklens); free(skutf8); 0 15 15
2828 0 croak("Text::Stencil: out of memory"); }
2829 /* a dying overload during key collection would otherwise leak these */
2830 15 SAVEDESTRUCTOR_X(ts_free, skeys);
2831 15 SAVEDESTRUCTOR_X(ts_free, sklens);
2832 15 SAVEDESTRUCTOR_X(ts_free, skutf8);
2833 36 ▶ for (int i = 0; i < nsort; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < nsort; i++) { 21 15 36
2834 21 SV **el = av_fetch(sort_av, i, 0);
2835 21 skeys[i] = ""; sklens[i] = 0; skutf8[i] = 0;
2836 21 ▶ if (el && *el) {
Branch: 2 branches
BranchTrueFalseTotal
if (el && *el) { 21 0 21
if (el && *el) { 21 0 21
2837 /* Pin the element and read it once. Stringifying it can
2838 run an overload that pushes to this very array, and
2839 av_extend would then realloc AvARRAY out from under
2840 `el` -- so nothing may dereference the slot again. */
2841 21 SV *e = *el;
2842 21 sv_2mortal(SvREFCNT_inc_simple_NN(e));
2843 21 skeys[i] = ts_own_pv(aTHX_ e, &sklens[i], &skutf8[i]);
2844 }
2845 /* same '-' shorthand the scalar form takes; direction is a
2846 property of the whole sort, so any '-' turns it around */
2847 21 ▶ if (sklens[i] > 1 && skeys[i][0] == '-') {
Branch: 2 branches
BranchTrueFalseTotal
if (sklens[i] > 1 && skeys[i][0] == '-') { 7 14 21
if (sklens[i] > 1 && skeys[i][0] == '-') { 2 5 7
2848 2 skeys[i]++; sklens[i]--; descending = 1;
2849 }
2850 }
2851 15 RETVAL = tpl_render_sorted(aTHX_ t, rows, NULL, skeys, sklens, skutf8, nsort, descending, numeric);
2852 } else {
2853 4 int *scols = (int *)malloc(nsort * sizeof(int));
2854 4 ▶ if (!scols) croak("Text::Stencil: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!scols) croak("Text::Stencil: out of memory"); 0 4 4
2855 4 SAVEDESTRUCTOR_X(ts_free, scols);
2856 9 ▶ for (int i = 0; i < nsort; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < nsort; i++) { 6 3 9
2857 6 SV **el = av_fetch(sort_av, i, 0);
2858 6 ▶ scols[i] = el ? ts_col_sv(aTHX_ *el, "sort") : 0;
Branch: 1 branch
BranchTrueFalseTotal
scols[i] = el ? ts_col_sv(aTHX_ *el, "sort") : 0; 6 0 6
2859 }
2860 3 RETVAL = tpl_render_sorted(aTHX_ t, rows, scols, NULL, NULL, NULL, nsort, descending, numeric);
2861 }
2862 }
2863 95 ▶ } else if (SvIOK(sort_by) || looks_like_number(sort_by)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (SvIOK(sort_by) || looks_like_number(sort_by)) { 37 30 67
} else if (SvIOK(sort_by) || looks_like_number(sort_by)) { 3 34 37
2864 33 ▶ TS_CHECK_SORT_KIND(0);
Branch: 1 branch
BranchTrueFalseTotal
TS_CHECK_SORT_KIND(0); 1 32 33
2865 32 int col = ts_col_sv(aTHX_ sort_by, "sort");
2866 28 RETVAL = tpl_render_sorted(aTHX_ t, rows, &col, NULL, NULL, NULL, 1, descending, numeric);
2867 } else {
2868 STRLEN klen;
2869 int kutf8;
2870 34 ▶ TS_CHECK_SORT_KIND(1);
Branch: 1 branch
BranchTrueFalseTotal
TS_CHECK_SORT_KIND(1); 1 33 34
2871 33 const char *key = ts_own_pv(aTHX_ sort_by, &klen, &kutf8);
2872 33 ▶ if (klen > 1 && key[0] == '-') { key++; klen--; descending = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (klen > 1 && key[0] == '-') { key++; klen--; descending = 1; } 10 23 33
if (klen > 1 && key[0] == '-') { key++; klen--; descending = 1; } 5 5 10
2873 33 RETVAL = tpl_render_sorted(aTHX_ t, rows, NULL, &key, &klen, &kutf8, 1, descending, numeric);
2874 }
2875 }
2876 OUTPUT:
2877 RETVAL
2878  
2879 SV *
2880 render_one(self, row)
2881 SV *self
2882 SV *row
2883 CODE:
2884 {
2885 81005 tpl_compiled *t = TS_PIN_SELF(self);
2886 81005 RETVAL = tpl_render_one(aTHX_ t, row);
2887 }
2888 OUTPUT:
2889 RETVAL
2890  
2891 void
2892 render_to_fh(self, fh, rows)
2893 SV *self
2894 PerlIO *fh
2895 AV *rows
2896 CODE:
2897 {
2898 415 tpl_compiled *t = TS_PIN_SELF(self);
2899 415 tpl_render_to_fh(aTHX_ t, rows, fh);
2900 }
2901  
2902 SV *
2903 render_cb(self, cb, ...)
2904 SV *self
2905 SV *cb
2906 CODE:
2907 {
2908 25437 tpl_compiled *t = TS_PIN_SELF(self);
2909 /* like render_sorted: a silently swallowed extra argument is how a typo
2910 becomes output nobody can explain */
2911 25436 ▶ if (items > 3) croak("Text::Stencil: render_cb takes at most two arguments");
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) croak("Text::Stencil: render_cb takes at most two arguments"); 1 25435 25436
2912 25435 ▶ if (!SvROK(cb) || SvTYPE(SvRV(cb)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(cb) || SvTYPE(SvRV(cb)) != SVt_PVCV) 25434 1 25435
if (!SvROK(cb) || SvTYPE(SvRV(cb)) != SVt_PVCV) 0 25434 25434
2913 1 croak("Text::Stencil: render_cb second argument must be a coderef");
2914 25434 PerlIO *fh = NULL;
2915 25434 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 5207 20227 25434
2916 /* a closed handle yields a NULL IoIFP; silently buffering to a string the
2917 caller then discards loses the output */
2918 5207 fh = IoIFP(sv_2io(ST(2)));
2919 5207 ▶ if (!fh) croak("Text::Stencil: render_cb: filehandle is not open for writing");
Branch: 1 branch
BranchTrueFalseTotal
if (!fh) croak("Text::Stencil: render_cb: filehandle is not open for writing"); 1 5206 5207
2920 }
2921 25433 RETVAL = tpl_render_cb(aTHX_ t, cb, fh);
2922 }
2923 OUTPUT:
2924 RETVAL
2925  
2926 SV *
2927 columns(self)
2928 SV *self
2929 CODE:
2930 {
2931 220007 tpl_compiled *t = ts_self(aTHX_ self);
2932 220007 RETVAL = tpl_columns(aTHX_ t);
2933 }
2934 OUTPUT:
2935 RETVAL
2936  
2937 IV
2938 row_count(self)
2939 SV *self
2940 CODE:
2941 {
2942 11 tpl_compiled *t = ts_self(aTHX_ self);
2943 11 ▶ RETVAL = (IV)t->last_row_count;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (IV)t->last_row_count; 2 9 11
2944 }
2945 OUTPUT:
2946 RETVAL
2947  
2948 SV *
2949 clone(self, ...)
2950 SV *self
2951 CODE:
2952 {
2953 /* An overloaded "" among the arguments can DESTROY us mid-call, and it can
2954 clear the caller's reference too -- so grab the stash while self is
2955 still one. */
2956 12 tpl_compiled *orig = TS_PIN_SELF(self);
2957 11 HV *self_stash = SvSTASH(SvRV(self));
2958 11 ▶ if (items % 2 == 0) croak("Text::Stencil: odd number of arguments");
Branch: 1 branch
BranchTrueFalseTotal
if (items % 2 == 0) croak("Text::Stencil: odd number of arguments"); 0 11 11
2959 11 const char *row = NULL; STRLEN rlen = 0;
2960 11 const char *sep = NULL; STRLEN slen = 0;
2961 11 int tpl_utf8 = orig->tpl_utf8;
2962 11 char err[TS_ERRSZ]; err[0] = '\0';
2963 24 ▶ for (int i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 1; i < items; i += 2) { 14 10 24
2964 14 const char *key = SvPV_nolen(ST(i));
2965 14 SV *val = ST(i + 1);
2966 /* an overloaded "" on a later argument can free an earlier one, and we
2967 keep raw PVs until tpl_compile copies them */
2968 14 sv_2mortal(SvREFCNT_inc_simple_NN(val));
2969 /* a replaced piece re-decides its own bit; it must not inherit one */
2970 14 ▶ if (strcmp(key, "row") == 0) { row = ts_own_pv(aTHX_ val, &rlen, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "row") == 0) { row = ts_own_pv(aTHX_ val, &rlen, NULL); 10 4 14
2971 10 ▶ if (SvUTF8(val)) tpl_utf8 |= TS_TPL_ROW; else tpl_utf8 &= ~TS_TPL_ROW; }
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(val)) tpl_utf8 |= TS_TPL_ROW; else tpl_utf8 &= ~TS_TPL_ROW; } 0 10 10
2972 4 ▶ else if (strcmp(key, "separator") == 0) { sep = ts_own_pv(aTHX_ val, &slen, NULL);
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(key, "separator") == 0) { sep = ts_own_pv(aTHX_ val, &slen, NULL); 3 1 4
2973 3 ▶ if (SvUTF8(val)) tpl_utf8 |= TS_TPL_SEP; else tpl_utf8 &= ~TS_TPL_SEP; }
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(val)) tpl_utf8 |= TS_TPL_SEP; else tpl_utf8 &= ~TS_TPL_SEP; } 0 3 3
2974 /* everything else is inherited, so taking it here would throw
2975 the caller's value silently away */
2976 1 else croak("Text::Stencil: clone does not take a '%s' option"
2977 " (only 'row' and 'separator'; the rest is inherited)", key);
2978 }
2979 10 ▶ if (!row) croak("Text::Stencil: clone requires a 'row' argument");
Branch: 1 branch
BranchTrueFalseTotal
if (!row) croak("Text::Stencil: clone requires a 'row' argument"); 1 9 10
2980 9 ▶ tpl_compiled *t = tpl_compile(aTHX_
Branch: 1 branch
BranchTrueFalseTotal
tpl_compiled *t = tpl_compile(aTHX_ 7 2 9
2981 9 orig->header, orig->header_len,
2982 row, rlen,
2983 9 orig->footer, orig->footer_len,
2984 sep ? sep : orig->sep, sep ? slen : orig->sep_len,
2985 9 ▶ orig->escape_char, tpl_utf8, err);
Branch: 1 branch
BranchTrueFalseTotal
orig->escape_char, tpl_utf8, err); 7 2 9
2986 9 ▶ if (!t || err[0]) { if (t) tpl_free(t); croak("Text::Stencil: %s", err); }
Branch: 3 branches
BranchTrueFalseTotal
if (!t || err[0]) { if (t) tpl_free(t); croak("Text::Stencil: %s", err); } 9 0 9
if (!t || err[0]) { if (t) tpl_free(t); croak("Text::Stencil: %s", err); } 0 9 9
if (!t || err[0]) { if (t) tpl_free(t); croak("Text::Stencil: %s", err); } 0 0 0
2987 /* Copy the skip conditions -- but a replacement row can change the row
2988 mode, leaving an inherited skip of the wrong kind fetching nothing from
2989 any row. Re-check against the template we just built, as new() does. */
2990 9 ▶ if (orig->has_skip_if) TS_CHECK_KIND(t, "skip_if", orig->skip_if_key != NULL);
Branch: 7 branches
BranchTrueFalseTotal
if (orig->has_skip_if) TS_CHECK_KIND(t, "skip_if", orig->skip_if_key != NULL); 3 6 9
if (orig->has_skip_if) TS_CHECK_KIND(t, "skip_if", orig->skip_if_key != NULL); 0 3 3
if (orig->has_skip_if) TS_CHECK_KIND(t, "skip_if", orig->skip_if_key != NULL); 0 0 0
if (orig->has_skip_if) TS_CHECK_KIND(t, "skip_if", orig->skip_if_key != NULL); 0 0 0
if (orig->has_skip_if) TS_CHECK_KIND(t, "skip_if", orig->skip_if_key != NULL); 3 0 3
if (orig->has_skip_if) TS_CHECK_KIND(t, "skip_if", orig->skip_if_key != NULL); 1 2 3
if (orig->has_skip_if) TS_CHECK_KIND(t, "skip_if", orig->skip_if_key != NULL); 1 0 1
2991 8 ▶ if (orig->has_skip_unless) TS_CHECK_KIND(t, "skip_unless", orig->skip_unless_key != NULL);
Branch: 7 branches
BranchTrueFalseTotal
if (orig->has_skip_unless) TS_CHECK_KIND(t, "skip_unless", orig->skip_unless_key != NULL); 0 8 8
if (orig->has_skip_unless) TS_CHECK_KIND(t, "skip_unless", orig->skip_unless_key != NULL); 0 0 0
if (orig->has_skip_unless) TS_CHECK_KIND(t, "skip_unless", orig->skip_unless_key != NULL); 0 0 0
if (orig->has_skip_unless) TS_CHECK_KIND(t, "skip_unless", orig->skip_unless_key != NULL); 0 0 0
if (orig->has_skip_unless) TS_CHECK_KIND(t, "skip_unless", orig->skip_unless_key != NULL); 0 0 0
if (orig->has_skip_unless) TS_CHECK_KIND(t, "skip_unless", orig->skip_unless_key != NULL); 0 0 0
if (orig->has_skip_unless) TS_CHECK_KIND(t, "skip_unless", orig->skip_unless_key != NULL); 0 0 0
2992 8 t->has_skip_if = orig->has_skip_if;
2993 8 t->skip_if_col = orig->skip_if_col;
2994 8 ▶ if (orig->skip_if_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (orig->skip_if_key) { 0 8 8
2995 0 ts_set_skip_key(aTHX_ t, &t->skip_if_key, &t->skip_if_key_len,
2996 0 &t->skip_if_utf8, orig->skip_if_key,
2997 orig->skip_if_key_len, orig->skip_if_utf8);
2998 }
2999 8 t->has_skip_unless = orig->has_skip_unless;
3000 8 t->skip_unless_col = orig->skip_unless_col;
3001 8 ▶ if (orig->skip_unless_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (orig->skip_unless_key) { 0 8 8
3002 0 ts_set_skip_key(aTHX_ t, &t->skip_unless_key, &t->skip_unless_key_len,
3003 0 &t->skip_unless_utf8, orig->skip_unless_key,
3004 orig->skip_unless_key_len, orig->skip_unless_utf8);
3005 }
3006 8 SV *obj = newSViv(PTR2IV(t));
3007 8 t->owner = obj;
3008 8 sv_magicext(obj, NULL, PERL_MAGIC_ext, &ts_vtbl, (const char *)t, 0);
3009 8 SV *ref = newRV_noinc(obj);
3010 8 sv_bless(ref, self_stash);
3011 8 RETVAL = ref;
3012 }
3013 OUTPUT:
3014 RETVAL
3015  
3016 void
3017 DESTROY(self)
3018 SV *self
3019 CODE:
3020 {
3021 655 ▶ MAGIC *mg = TS_HAS_OUR_MAGIC(self);
Branch: 2 branches
BranchTrueFalseTotal
MAGIC *mg = TS_HAS_OUR_MAGIC(self); 655 0 655
MAGIC *mg = TS_HAS_OUR_MAGIC(self); 654 1 655
3022 655 ▶ tpl_compiled *t = mg ? (tpl_compiled *)mg->mg_ptr : NULL;
Branch: 1 branch
BranchTrueFalseTotal
tpl_compiled *t = mg ? (tpl_compiled *)mg->mg_ptr : NULL; 651 4 655
3023 /* only the owner frees, and only once nothing is still inside a call */
3024 655 ▶ if (t && t->owner == SvRV(self)) {
Branch: 2 branches
BranchTrueFalseTotal
if (t && t->owner == SvRV(self)) { 648 7 655
if (t && t->owner == SvRV(self)) { 648 0 648
3025 648 mg->mg_ptr = NULL;
3026 648 t->owner = NULL;
3027 648 ▶ if (t->in_use > 0) t->doomed = 1; else tpl_free(t);
Branch: 1 branch
BranchTrueFalseTotal
if (t->in_use > 0) t->doomed = 1; else tpl_free(t); 2 646 648
3028 }
3029 }
« eg/streaming.pl ↑ Summary eg/csv_export.pl »