dds_format.c

filter: Statementstmt 94.7% 491 / 518 Branchbran 84.8% 487 / 574 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.5% 978 / 1092 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« dds_emit.c ↑ Summary dds_engine.c »
1 #define NEED_utf8_to_uvchr_buf
2 #include "dds_engine.h"
3  
4 /* Recognize decimal spellings accepted for unquoted output, excluding
5 * leading-zero integers and trailing-zero fractions.
6 */
7 74369 static bool dds_decimal(const char *p, STRLEN len)
8 {
9 74369 STRLEN i = 0;
10  
11 74369 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 1 74368 74369
12 1 return FALSE;
13 74368 ▶ if (p[i] == '-' && ++i == len)
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] == '-' && ++i == len) 32 74336 74368
if (p[i] == '-' && ++i == len) 9 23 32
14 9 return FALSE;
15  
16 74359 ▶ if (p[i] == '0')
Branch: 1 branch
BranchTrueFalseTotal
if (p[i] == '0') 329 74030 74359
17 329 i++;
18 74030 ▶ else if (p[i] >= '1' && p[i] <= '9')
Branch: 2 branches
BranchTrueFalseTotal
else if (p[i] >= '1' && p[i] <= '9') 73812 218 74030
else if (p[i] >= '1' && p[i] <= '9') 15033 58779 73812
19 40896 ▶ while (i < len && p[i] >= '0' && p[i] <= '9')
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && p[i] >= '0' && p[i] <= '9') 26402 14494 40896
while (i < len && p[i] >= '0' && p[i] <= '9') 26397 5 26402
while (i < len && p[i] >= '0' && p[i] <= '9') 25863 534 26397
20 25863 i++;
21 else
22 58997 return FALSE;
23 15362 ▶ if (i == len)
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) 14797 565 15362
24 14797 return TRUE;
25  
26 565 ▶ if (p[i++] != '.' || i == len)
Branch: 2 branches
BranchTrueFalseTotal
if (p[i++] != '.' || i == len) 9 556 565
if (p[i++] != '.' || i == len) 0 9 9
27 556 return FALSE;
28 50 ▶ while (i < len && p[i] >= '0' && p[i] <= '9')
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && p[i] >= '0' && p[i] <= '9') 41 9 50
while (i < len && p[i] >= '0' && p[i] <= '9') 41 0 41
while (i < len && p[i] >= '0' && p[i] <= '9') 41 0 41
29 41 i++;
30  
31 9 ▶ return i == len && p[len - 1] != '0';
Branch: 2 branches
BranchTrueFalseTotal
return i == len && p[len - 1] != '0'; 9 0 9
return i == len && p[len - 1] != '0'; 5 4 9
32 }
33  
34 /* Build a mortal double-quoted Perl expression, escaping interpolation and
35 * controls while preserving the input's UTF-8 flag.
36 */
37 1055 SV *dds_qquote(pTHX_ SV *value)
38 {
39 1055 STRLEN len, offset = 0, chars = 0;
40 1055 const U8 *p = (const U8 *)SvPV(value, len);
41 1055 SV *out = newSVpvs("\"");
42 /* Older Perls use a char-sized bool that cannot hold SVf_UTF8. */
43 1055 bool force_unicode = cBOOL(SvUTF8(value));
44  
45 /* A Latin-1-only UTF-8 value needs a Unicode escape to keep its flag
46 * when the generated literal is evaluated. Empty strings need a seed. */
47 1055 ▶ if (force_unicode) {
Branch: 1 branch
BranchTrueFalseTotal
if (force_unicode) { 326 729 1055
48 STRLEN i;
49  
50 1433 ▶ for (i = 0; i < len;) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len;) { 1160 273 1433
51 STRLEN width;
52 1160 UV ch = utf8_to_uvchr_buf(p + i, p + len, &width);
53  
54 1160 ▶ if (ch > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch > 255) { 53 1107 1160
55 53 force_unicode = FALSE;
56 53 break;
57 }
58 1107 i += width;
59 }
60 326 ▶ if (!len) {
Branch: 1 branch
BranchTrueFalseTotal
if (!len) { 1 325 326
61 1 SvREFCNT_dec(out);
62 1 return DDS_TEMP(newSVpvs("substr(\"\\N{U+100}\",0,0)"));
63 }
64 }
65  
66 4345 ▶ while (offset < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (offset < len) { 3291 1054 4345
67 3291 STRLEN width = 1;
68 3291 UV ch = p[offset];
69  
70 3291 ▶ if (SvUTF8(value))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(value)) 1230 2061 3291
71 1230 ch = utf8_to_uvchr_buf(p + offset, p + len, &width);
72 3291 ▶ if (chars == 72) {
Branch: 1 branch
BranchTrueFalseTotal
if (chars == 72) { 8 3283 3291
73 8 sv_catpvs(out, "\".\n\t\"");
74 8 chars = 0;
75 }
76  
77 3291 ▶ if (force_unicode && !offset) {
Branch: 2 branches
BranchTrueFalseTotal
if (force_unicode && !offset) { 832 2459 3291
if (force_unicode && !offset) { 272 560 832
78 272 sv_catpvf(out, "\\N{U+%" UVXf "}", ch);
79 3019 ▶ } else if (ch == '\\' || ch == '"' || ch == '@' || ch == '$') {
Branch: 4 branches
BranchTrueFalseTotal
} else if (ch == '\\' || ch == '"' || ch == '@' || ch == '$') { 3016 3 3019
} else if (ch == '\\' || ch == '"' || ch == '@' || ch == '$') { 3012 4 3016
} else if (ch == '\\' || ch == '"' || ch == '@' || ch == '$') { 3009 3 3012
} else if (ch == '\\' || ch == '"' || ch == '@' || ch == '$') { 8 3001 3009
80 18 sv_catpvs(out, "\\");
81 18 sv_catpvn(out, (const char *)p + offset, width);
82 3001 ▶ } else if (ch == '\a')
Branch: 1 branch
BranchTrueFalseTotal
} else if (ch == '\a') 3 2998 3001
83 3 sv_catpvs(out, "\\a");
84 2998 ▶ else if (ch == '\b')
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == '\b') 3 2995 2998
85 3 sv_catpvs(out, "\\b");
86 2995 ▶ else if (ch == '\t')
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == '\t') 6 2989 2995
87 6 sv_catpvs(out, "\\t");
88 2989 ▶ else if (ch == '\n')
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == '\n') 46 2943 2989
89 46 sv_catpvs(out, "\\n");
90 2943 ▶ else if (ch == '\f')
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == '\f') 3 2940 2943
91 3 sv_catpvs(out, "\\f");
92 2940 ▶ else if (ch == '\r')
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == '\r') 4 2936 2940
93 4 sv_catpvs(out, "\\r");
94 2936 ▶ else if (ch == 27)
Branch: 1 branch
BranchTrueFalseTotal
else if (ch == 27) 3 2933 2936
95 3 sv_catpvs(out, "\\e");
96 3431 ▶ else if (ch < 32 || ch == 127 || (ch >= 128 && ch <= 255)) {
Branch: 4 branches
BranchTrueFalseTotal
else if (ch < 32 || ch == 127 || (ch >= 128 && ch <= 255)) { 2856 77 2933
else if (ch < 32 || ch == 127 || (ch >= 128 && ch <= 255)) { 2853 3 2856
else if (ch < 32 || ch == 127 || (ch >= 128 && ch <= 255)) { 477 2376 2853
else if (ch < 32 || ch == 127 || (ch >= 128 && ch <= 255)) { 418 59 477
97 498 ▶ bool next_digit = offset + width < len && isDIGIT_A(p[offset + width]);
Branch: 2 branches
BranchTrueFalseTotal
bool next_digit = offset + width < len && isDIGIT_A(p[offset + width]); 318 180 498
bool next_digit = offset + width < len && isDIGIT_A(p[offset + width]); 2 316 318
98  
99 498 ▶ if (ch < 32 && !next_digit)
Branch: 2 branches
BranchTrueFalseTotal
if (ch < 32 && !next_digit) 77 421 498
if (ch < 32 && !next_digit) 77 0 77
100 77 sv_catpvf(out, "\\%o", (unsigned)ch);
101 else
102 421 sv_catpvf(out, "\\%03o", (unsigned)ch);
103 2435 ▶ } else if (ch > 126)
Branch: 1 branch
BranchTrueFalseTotal
} else if (ch > 126) 59 2376 2435
104 59 sv_catpvf(out, "\\x{%" UVxf "}", ch);
105 else
106 2376 sv_catpvn(out, (const char *)p + offset, width);
107  
108 3291 offset += width;
109 3291 chars++;
110 }
111  
112 1054 sv_catpvs(out, "\"");
113 1054 return DDS_TEMP(out);
114 }
115  
116 /* Choose a numeric, bareword, single-quoted or double-quoted representation
117 * suitable for a hash key or scalar value.
118 */
119 74669 SV *dds_quote(pTHX_ SV *value, bool key)
120 {
121 STRLEN len, i;
122 149338 ▶ const char *p = SvOK(value) ? SvPV(value, len) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *p = SvOK(value) ? SvPV(value, len) : ""; 74669 0 74669
123 SV *out;
124  
125 74669 ▶ if (!SvOK(value))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(value)) 0 74669 74669
126 0 len = 0;
127 74669 ▶ if (key && !len)
Branch: 2 branches
BranchTrueFalseTotal
if (key && !len) 53672 20997 74669
if (key && !len) 0 53672 53672
128 0 return DDS_TEMP(newSVpvs("\"\""));
129 74669 ▶ if (!key && SvUTF8(value))
Branch: 2 branches
BranchTrueFalseTotal
if (!key && SvUTF8(value)) 20997 53672 74669
if (!key && SvUTF8(value)) 300 20697 20997
130 300 return dds_qquote(aTHX_ value);
131 74369 ▶ if (dds_decimal(p, len))
Branch: 1 branch
BranchTrueFalseTotal
if (dds_decimal(p, len)) 14802 59567 74369
132 14802 return DDS_TEMP(newSVpvn(p, len));
133  
134 /* Hash keys may use barewords; values need a quoted string literal. */
135 59567 ▶ if (key) {
Branch: 1 branch
BranchTrueFalseTotal
if (key) { 52892 6675 59567
136 52892 i = p[0] == '-' ? 1 : 0;
137 52892 ▶ if (i < len && (isALPHA_A(p[i]) || p[i] == '_')) {
Branch: 3 branches
BranchTrueFalseTotal
if (i < len && (isALPHA_A(p[i]) || p[i] == '_')) { 52884 8 52892
if (i < len && (isALPHA_A(p[i]) || p[i] == '_')) { 504 52380 52884
if (i < len && (isALPHA_A(p[i]) || p[i] == '_')) { 0 504 504
138 226903 ▶ for (i++; i < len && (isALNUMC_A(p[i]) || p[i] == '_'); i++)
Branch: 3 branches
BranchTrueFalseTotal
for (i++; i < len && (isALNUMC_A(p[i]) || p[i] == '_'); i++) 174575 52328 226903
for (i++; i < len && (isALNUMC_A(p[i]) || p[i] == '_'); i++) 174523 52 174575
for (i++; i < len && (isALNUMC_A(p[i]) || p[i] == '_'); i++) 0 52 52
139 ;
140 52380 ▶ if (i == len)
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) 52328 52 52380
141 52328 return DDS_TEMP(newSVpvn(p, len));
142 }
143 564 return dds_qquote(aTHX_ value);
144 }
145  
146 88454 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 81956 6498 88454
147 81956 ▶ if ((U8)p[i] < 32 || (U8)p[i] > 126)
Branch: 2 branches
BranchTrueFalseTotal
if ((U8)p[i] < 32 || (U8)p[i] > 126) 81921 35 81956
if ((U8)p[i] < 32 || (U8)p[i] > 126) 142 81779 81921
148 177 return dds_qquote(aTHX_ value);
149  
150 6498 out = newSVpvs("'");
151 88057 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 81559 6498 88057
152 81559 ▶ if (p[i] == '\\' || p[i] == '\'')
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] == '\\' || p[i] == '\'') 81557 2 81559
if (p[i] == '\\' || p[i] == '\'') 1 81556 81557
153 3 sv_catpvs(out, "\\");
154 81559 sv_catpvn(out, p + i, 1);
155 }
156  
157 6498 sv_catpvs(out, "'");
158 6498 return DDS_TEMP(out);
159 }
160  
161 /* Return a mortal byte slice while retaining the input string's UTF-8 flag. */
162 1582 static SV *dds_string_slice(pTHX_ SV *name, STRLEN start, STRLEN length)
163 {
164 STRLEN len;
165 1582 const char *p = SvPV(name, len);
166 1582 SV *slice = DDS_TEMP(newSVpvn(p + start, length));
167  
168 1582 ▶ if (SvUTF8(name))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(name)) 83 1499 1582
169 83 SvUTF8_on(slice);
170 1582 return slice;
171 }
172  
173 /* Find the byte offset after a run of identifier characters, decoding UTF-8
174 * when the name carries that flag.
175 */
176 33566 static STRLEN dds_word_end(pTHX_ SV *name, STRLEN start)
177 {
178 33566 STRLEN len, at = start;
179 33566 const U8 *p = (const U8 *)SvPV(name, len);
180  
181 200218 ▶ while (at < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (at < len) { 182184 18034 200218
182 182184 STRLEN width = 1;
183 182184 UV ch = p[at];
184  
185 182184 ▶ if (SvUTF8(name))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(name)) 351 181833 182184
186 351 ch = utf8_to_uvchr_buf(p + at, p + len, &width);
187 182184 ▶ if (!isWORDCHAR_uni(ch))
Branch: 4 branches
BranchTrueFalseTotal
if (!isWORDCHAR_uni(ch)) 182167 17 182184
if (!isWORDCHAR_uni(ch)) 182167 0 182167
if (!isWORDCHAR_uni(ch)) 15532 166635 182167
if (!isWORDCHAR_uni(ch)) 15532 166652 182184
188 15532 break;
189 166652 at += width;
190 }
191  
192 33566 return at;
193 }
194  
195 /* Recover a mortal inner expression from a simple or braced scalar
196 * dereference, returning NULL when it cannot be safely unwrapped.
197 */
198 33661 SV *dds_underef(pTHX_ SV *name)
199 {
200 STRLEN len;
201 33661 const char *p = SvPV(name, len);
202  
203 33661 ▶ if (len > 1 && strchr("@%$", p[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (len > 1 && strchr("@%$", p[0])) { 33661 0 33661
if (len > 1 && strchr("@%$", p[0])) { 33299 362 33661
204 33299 ▶ if (p[1] == '$')
Branch: 1 branch
BranchTrueFalseTotal
if (p[1] == '$') 177 33122 33299
205 177 return dds_string_slice(aTHX_ name, 1, len - 1);
206 33122 ▶ if (p[1] == '{' && p[len - 1] == '}') {
Branch: 2 branches
BranchTrueFalseTotal
if (p[1] == '{' && p[len - 1] == '}') { 436 32686 33122
if (p[1] == '{' && p[len - 1] == '}') { 352 84 436
207 STRLEN i;
208 352 IV depth = 0;
209 352 char quote = 0;
210  
211 5202 ▶ for (i = 1; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < len; i++) { 5202 0 5202
212 5202 char c = p[i];
213  
214 5202 ▶ if (quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (quote) { 0 5202 5202
215 0 ▶ if (c == '\\')
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\\') 0 0 0
216 0 i++;
217 0 ▶ else if (c == quote)
Branch: 1 branch
BranchTrueFalseTotal
else if (c == quote) 0 0 0
218 0 quote = 0;
219 5202 ▶ } else if (c == '\'' || c == '"')
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == '\'' || c == '"') 5202 0 5202
} else if (c == '\'' || c == '"') 0 5202 5202
220 0 quote = c;
221 5202 ▶ else if (c == '{')
Branch: 1 branch
BranchTrueFalseTotal
else if (c == '{') 616 4586 5202
222 616 depth++;
223 4586 ▶ else if (c == '}' && --depth == 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (c == '}' && --depth == 0) { 616 3970 4586
else if (c == '}' && --depth == 0) { 352 264 616
224 352 ▶ if (i == len - 1)
Branch: 1 branch
BranchTrueFalseTotal
if (i == len - 1) 352 0 352
225 352 return dds_string_slice(aTHX_ name, 2, len - 3);
226 0 break;
227 }
228 }
229 }
230 }
231 33132 return NULL;
232 }
233  
234 /* Construct a mortal expression for an array/hash child or a dereferenced
235 * slot, adjusting sigils and braces for valid Perl syntax.
236 */
237 35226 SV *dds_child_name(pTHX_ SV *parent, char kind, SV *key, IV child_index)
238 {
239 35226 SV *out = DDS_TEMP(newSVsv(parent));
240 STRLEN len;
241 35226 char *p = SvPV_force(out, len);
242  
243 68710 ▶ if (kind == '[' || kind == '{') {
Branch: 2 branches
BranchTrueFalseTotal
if (kind == '[' || kind == '{') { 21389 13837 35226
if (kind == '[' || kind == '{') { 19647 1742 21389
244 SV *underef;
245 33484 ▶ bool plain = len > 1 && strchr("@%$", p[0]);
Branch: 2 branches
BranchTrueFalseTotal
bool plain = len > 1 && strchr("@%$", p[0]); 33484 0 33484
bool plain = len > 1 && strchr("@%$", p[0]); 33122 362 33484
246  
247 33484 ▶ if (len > 1 && strchr("@%", p[0]) && p[1] == '$') {
Branch: 3 branches
BranchTrueFalseTotal
if (len > 1 && strchr("@%", p[0]) && p[1] == '$') { 33484 0 33484
if (len > 1 && strchr("@%", p[0]) && p[1] == '$') { 441 33043 33484
if (len > 1 && strchr("@%", p[0]) && p[1] == '$') { 0 441 441
248 0 sv_chop(out, p + 1);
249 0 p = SvPV_force(out, len);
250 }
251  
252 33484 underef = dds_underef(aTHX_ out);
253 33484 ▶ if (plain)
Branch: 1 branch
BranchTrueFalseTotal
if (plain) 33122 362 33484
254 33122 plain = dds_word_end(aTHX_ out, 1) == len;
255 33484 ▶ if (plain || underef || (p[0] == '*' && p[len - 1] == '}')) {
Branch: 4 branches
BranchTrueFalseTotal
if (plain || underef || (p[0] == '*' && p[len - 1] == '}')) { 15894 17590 33484
if (plain || underef || (p[0] == '*' && p[len - 1] == '}')) { 15541 353 15894
if (plain || underef || (p[0] == '*' && p[len - 1] == '}')) { 362 15179 15541
if (plain || underef || (p[0] == '*' && p[len - 1] == '}')) { 338 24 362
256 18281 ▶ bool dereferenced = p[0] == '@' || p[0] == '%';
Branch: 2 branches
BranchTrueFalseTotal
bool dereferenced = p[0] == '@' || p[0] == '%'; 17968 313 18281
bool dereferenced = p[0] == '@' || p[0] == '%'; 128 17840 17968
257  
258 18281 ▶ if (dereferenced)
Branch: 1 branch
BranchTrueFalseTotal
if (dereferenced) 441 17840 18281
259 441 p[0] = '$';
260 18281 ▶ if (!dereferenced || underef)
Branch: 2 branches
BranchTrueFalseTotal
if (!dereferenced || underef) 441 17840 18281
if (!dereferenced || underef) 0 441 441
261 17840 sv_catpvs(out, "->");
262 18281 p = SvPV_force(out, len);
263 18281 ▶ if (len > 3 && p[0] == '$' && p[1] == '$' && p[len - 1] == '>') {
Branch: 4 branches
BranchTrueFalseTotal
if (len > 3 && p[0] == '$' && p[1] == '$' && p[len - 1] == '>') { 18183 98 18281
if (len > 3 && p[0] == '$' && p[1] == '$' && p[len - 1] == '>') { 17845 338 18183
if (len > 3 && p[0] == '$' && p[1] == '$' && p[len - 1] == '>') { 148 17697 17845
if (len > 3 && p[0] == '$' && p[1] == '$' && p[len - 1] == '>') { 148 0 148
264 148 SV *tmp = DDS_TEMP(newSVpvs("${"));
265  
266 148 DDS_CAT(tmp, dds_string_slice(aTHX_ out, 1, len - 3));
267 148 sv_catpvs(tmp, "}->");
268 148 sv_setsv(out, tmp);
269 }
270 }
271  
272 33484 ▶ if (kind == '[')
Branch: 1 branch
BranchTrueFalseTotal
if (kind == '[') 13837 19647 33484
273 13837 sv_catpvf(out, "[%" IVdf "]", child_index);
274 else {
275 19647 sv_catpvs(out, "{");
276 19647 DDS_CAT(out, dds_quote(aTHX_ key, TRUE));
277 19647 sv_catpvs(out, "}");
278 }
279 } else {
280 1742 SV *tmp = DDS_TEMP(newSVpvn(&kind, 1));
281  
282 1742 ▶ if (strchr(p, '[') || strchr(p, '{') || p[0] == '*') {
Branch: 3 branches
BranchTrueFalseTotal
if (strchr(p, '[') || strchr(p, '{') || p[0] == '*') { 851 891 1742
if (strchr(p, '[') || strchr(p, '{') || p[0] == '*') { 546 305 851
if (strchr(p, '[') || strchr(p, '{') || p[0] == '*') { 0 546 546
283 1196 sv_catpvs(tmp, "{");
284 1196 DDS_CAT(tmp, out);
285 1196 sv_catpvs(tmp, "}");
286 546 ▶ } else if (p[0] == kind && kind != '$')
Branch: 2 branches
BranchTrueFalseTotal
} else if (p[0] == kind && kind != '$') 473 73 546
} else if (p[0] == kind && kind != '$') 0 473 473
287 0 return out;
288 else
289 546 DDS_CAT(tmp, out);
290 1742 sv_setsv(out, tmp);
291 }
292 35226 return out;
293 }
294  
295 /* Derive a scalar-slot name from an aggregate expression, preserving UTF-8. */
296 39649 SV *dds_clean_name(pTHX_ SV *name)
297 {
298 STRLEN len, i;
299 39649 const char *p = SvPV(name, len);
300 SV *out;
301  
302 39649 ▶ if (!len || !strchr("@%&", p[0]))
Branch: 2 branches
BranchTrueFalseTotal
if (!len || !strchr("@%&", p[0])) 39649 0 39649
if (!len || !strchr("@%&", p[0])) 39205 444 39649
303 39205 return DDS_TEMP(newSVsv(name));
304 444 i = dds_word_end(aTHX_ name, 1);
305 444 ▶ if (i == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (i == 1) 0 444 444
306 0 return DDS_TEMP(newSVsv(name));
307  
308 444 out = DDS_TEMP(newSVpvs("$"));
309 444 DDS_CAT(out, dds_string_slice(aTHX_ name, 1, i - 1));
310 444 sv_catpvs(out, "_");
311 444 DDS_CAT(out, dds_string_slice(aTHX_ name, i, len - i));
312 444 return out;
313 }
314  
315 /* Choose a root expression from an explicit name or a unique type-derived
316 * name, honoring verbose and abbreviation settings.
317 */
318 1961 SV *dds_name(pTHX_ dds_ctx *ctx, SV *sv, UV root_index)
319 {
320 1961 AV *names = dds_array(aTHX_ ctx->object, "unames", FALSE);
321 1961 ▶ SV **entry = names ? av_fetch(names, root_index, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **entry = names ? av_fetch(names, root_index, 0) : NULL; 1125 836 1961
322 SV *out;
323 const char *type;
324  
325 1961 ▶ if (entry && SvTRUE(*entry)) {
Branch: 2 branches
BranchTrueFalseTotal
if (entry && SvTRUE(*entry)) { 1089 872 1961
if (entry && SvTRUE(*entry)) { 1089 0 1089
326 STRLEN len;
327 1089 const char *p = SvPV(*entry, len);
328  
329 1089 ▶ if (p[0] == '*' || p[0] == '-') {
Branch: 2 branches
BranchTrueFalseTotal
if (p[0] == '*' || p[0] == '-') { 925 164 1089
if (p[0] == '*' || p[0] == '-') { 0 925 925
330 164 char sigil = '$';
331  
332 164 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 160 4 164
333 160 ▶ if (SvTYPE(SvRV(sv)) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(sv)) == SVt_PVAV) 85 75 160
334 85 sigil = '@';
335 75 ▶ else if (SvTYPE(SvRV(sv)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTYPE(SvRV(sv)) == SVt_PVHV) 56 19 75
336 56 sigil = '%';
337 19 ▶ else if (SvTYPE(SvRV(sv)) == SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
else if (SvTYPE(SvRV(sv)) == SVt_PVCV) 7 12 19
338 7 sigil = '&';
339 }
340 164 out = DDS_TEMP(newSVsv(*entry));
341 (void)SvPV_force(out, len);
342 164 SvPVX(out)[0] = sigil;
343 } else {
344 925 out = DDS_TEMP(newSVpvs("$"));
345 925 DDS_CAT(out, *entry);
346 }
347 1089 return out;
348 }
349  
350 872 ▶ if (SvROK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) 785 87 872
351 785 type = sv_reftype(SvRV(sv), TRUE);
352 else
353 87 ▶ type = SvREADONLY(sv) && sv != &PL_sv_undef ? "RO" : "VAR";
Branch: 2 branches
BranchTrueFalseTotal
type = SvREADONLY(sv) && sv != &PL_sv_undef ? "RO" : "VAR"; 3 84 87
type = SvREADONLY(sv) && sv != &PL_sv_undef ? "RO" : "VAR"; 2 1 3
354  
355 872 out = DDS_TEMP(newSVpvs("$"));
356 872 ▶ if (!ctx->style.verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->style.verbose) { 2 870 872
357 2 SV *abr = DDS_TEMP(newSVpvs(""));
358 2 const char *p = type, *part;
359 STRLEN n;
360  
361 2 ▶ while ((part = strstr(p, "::"))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((part = strstr(p, "::"))) { 0 2 2
362 0 ▶ if (part > p)
Branch: 1 branch
BranchTrueFalseTotal
if (part > p) 0 0 0
363 0 sv_catpvn(abr, p, 1);
364 0 p = part + 2;
365 }
366  
367 2 sv_catpv(abr, p);
368 2 hv_store(ctx->abbreviations, type, (I32)strlen(type), newSVpv(type, 0), 0);
369 2 ▶ for (n = 1; n <= SvCUR(abr); n++) {
Branch: 1 branch
BranchTrueFalseTotal
for (n = 1; n <= SvCUR(abr); n++) { 2 0 2
370 2 SV **old = hv_fetch(ctx->abbreviations, SvPVX(abr), n, 0);
371  
372 2 ▶ if (!old || strEQ(DDS_PV(*old), type))
Branch: 2 branches
BranchTrueFalseTotal
if (!old || strEQ(DDS_PV(*old), type)) 0 2 2
if (!old || strEQ(DDS_PV(*old), type)) 0 0 0
373 break;
374 }
375  
376 2 ▶ if (n <= SvCUR(abr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= SvCUR(abr)) { 2 0 2
377 2 hv_store(ctx->abbreviations, SvPVX(abr), n, newSVpv(type, 0), 0);
378 2 sv_catpvn(out, SvPVX(abr), n);
379 }
380 }
381  
382 872 ▶ if (SvCUR(out) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(out) == 1) { 870 2 872
383 const char *p;
384  
385 4671 ▶ for (p = type; *p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = type; *p; p++) { 3803 868 4671
386 3803 ▶ if (p[0] == ':' && p[1] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (p[0] == ':' && p[1] == ':') { 7 3796 3803
if (p[0] == ':' && p[1] == ':') { 7 0 7
387 7 sv_catpvs(out, "_");
388 7 p++;
389 3796 ▶ } else if (isALNUMC_A(*p) || *p == '_')
Branch: 2 branches
BranchTrueFalseTotal
} else if (isALNUMC_A(*p) || *p == '_') 9 3787 3796
} else if (isALNUMC_A(*p) || *p == '_') 7 2 9
390 3794 sv_catpvn(out, p, 1);
391 2 ▶ else if (SvCUR(out) > 1)
Branch: 1 branch
BranchTrueFalseTotal
else if (SvCUR(out) > 1) 2 0 2
392 2 break;
393 }
394 }
395  
396 {
397 872 SV **counter = hv_fetch(ctx->types, type, (I32)strlen(type), 1);
398 872 ▶ UV next = SvOK(*counter) ? SvUV(*counter) + 1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
UV next = SvOK(*counter) ? SvUV(*counter) + 1 : 1; 156 716 872
399  
400 872 sv_setuv(*counter, next);
401 872 sv_catpvf(out, "%" UVuf, next);
402 }
403  
404 872 return out;
405 }
406  
407 /* Build a mortal Perl glob expression, shortening main-package names and
408 * quoting names that require protection.
409 */
410 339 SV *dds_glob_name(pTHX_ GV *gv)
411 {
412 339 SV *name = DDS_TEMP(newSV(0)), *out;
413 STRLEN len, i;
414 const char *p;
415 339 bool quote = FALSE;
416  
417 339 gv_fullname3(name, gv, NULL);
418 339 p = SvPV(name, len);
419 339 ▶ if (len >= 6 && memEQ(p, "main::", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
if (len >= 6 && memEQ(p, "main::", 6)) { 339 0 339
if (len >= 6 && memEQ(p, "main::", 6)) { 54 285 339
420 54 p += 4;
421 54 len -= 4;
422 }
423  
424 5483 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 5144 339 5483
425 5144 ▶ if (!isALNUMC_A(p[i]) && p[i] != '_' && p[i] != ':')
Branch: 3 branches
BranchTrueFalseTotal
if (!isALNUMC_A(p[i]) && p[i] != '_' && p[i] != ':') 708 4436 5144
if (!isALNUMC_A(p[i]) && p[i] != '_' && p[i] != ':') 708 0 708
if (!isALNUMC_A(p[i]) && p[i] != '_' && p[i] != ':') 30 678 708
426 30 quote = TRUE;
427  
428 339 out = DDS_TEMP(newSVpvs("*"));
429 339 ▶ if (quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (quote) { 15 324 339
430 15 SV *value = dds_string_slice(aTHX_ name, (STRLEN)(p - SvPVX(name)), len);
431  
432 15 sv_catpvs(out, "{");
433 15 DDS_CAT(out, dds_quote(aTHX_ value, FALSE));
434 15 sv_catpvs(out, "}");
435 } else
436 324 sv_catpvn(out, p, len);
437 339 return out;
438 }
439  
440 typedef struct {
441 SV *key, *clean;
442 NV number;
443 bool numeric;
444 } dds_key;
445  
446 /*
447 * Key ordering
448 *
449 * Native comparators operate on prepared key records. Policy selection and
450 * callbacks choose the order before values are traversed.
451 */
452  
453 /* Compare smart-order keys by numeric-prefix status and value, then folded
454 * word text and the original key as a tie-breaker.
455 */
456 67792 static int dds_key_compare(const void *a, const void *b)
457 {
458 67792 const dds_key *left = (const dds_key *)a, *right = (const dds_key *)b;
459  
460 dTHX;
461 int cmp;
462  
463 67792 ▶ if (left->numeric != right->numeric)
Branch: 1 branch
BranchTrueFalseTotal
if (left->numeric != right->numeric) 258 67534 67792
464 258 ▶ return left->numeric ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
return left->numeric ? -1 : 1; 94 164 258
465 67534 ▶ if (left->numeric && left->number != right->number)
Branch: 2 branches
BranchTrueFalseTotal
if (left->numeric && left->number != right->number) 1096 66438 67534
if (left->numeric && left->number != right->number) 798 298 1096
466 798 ▶ return left->number < right->number ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
return left->number < right->number ? -1 : 1; 376 422 798
467 66736 cmp = sv_cmp(left->clean, right->clean);
468 66736 ▶ return cmp ? cmp : sv_cmp(left->key, right->key);
Branch: 1 branch
BranchTrueFalseTotal
return cmp ? cmp : sv_cmp(left->key, right->key); 86 66650 66736
469 }
470  
471 /* Compare original key strings for lexical and alphabetical ordering. */
472 1632 static int dds_key_lexical(const void *a, const void *b)
473 {
474 dTHX;
475  
476 1632 return sv_cmp(((const dds_key *)a)->key, ((const dds_key *)b)->key);
477 }
478  
479 /* Compare the numeric values prepared for numeric key ordering. */
480 40 static int dds_key_numeric(const void *a, const void *b)
481 {
482 40 const dds_key *left = (const dds_key *)a, *right = (const dds_key *)b;
483  
484 40 ▶ return left->number < right->number ? -1 : left->number > right->number ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return left->number < right->number ? -1 : left->number > right->number ? 1 : 0; 20 20 40
485 }
486  
487 /* Resolve a nonempty prefix of a supported built-in key-order mode, returning
488 * NULL for an unknown or undefined mode.
489 */
490 797 const char *dds_builtin_order(pTHX_ SV *mode)
491 {
492 static const char *names[] = {"numeric", "lexical", "alphabetical",
493 "smart", "intelligent", "each"};
494 STRLEN len;
495 const char *p;
496 UV i;
497  
498 797 ▶ if (!SvOK(mode))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(mode)) 0 797 797
499 0 return NULL;
500  
501 797 p = SvPV(mode, len);
502 3927 ▶ for (i = 0; i < sizeof(names) / sizeof(*names); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof(names) / sizeof(*names); i++) 3926 1 3927
503 3926 ▶ if (len && len <= strlen(names[i]) && memEQ(p, names[i], len))
Branch: 3 branches
BranchTrueFalseTotal
if (len && len <= strlen(names[i]) && memEQ(p, names[i], len)) 3926 0 3926
if (len && len <= strlen(names[i]) && memEQ(p, names[i], len)) 3913 13 3926
if (len && len <= strlen(names[i]) && memEQ(p, names[i], len)) 796 3117 3913
504 796 return names[i];
505  
506 1 return NULL;
507 }
508  
509 /* Return a mortal ordered key list using object/class policies or native
510 * sorting; expose a callback thaw action through the optional out parameter.
511 */
512 3395 AV *dds_keys(pTHX_ dds_ctx *ctx, HV *hv, I32 pass, SV **thaw)
513 {
514 3395 HV *style = dds_hash(aTHX_ ctx->object, "style", FALSE);
515 3395 HV *sorters = dds_hash(aTHX_ style, "sortkeys", FALSE);
516 3395 SV *address = DDS_TEMP(newSVpvf("#%" UVuf, PTR2UV(hv)));
517 3395 SV *class_name = dds_class(aTHX_(SV *) hv);
518 3395 SV *class_key = DDS_TEMP(newSVpvs("."));
519 SV *sorter;
520 AV *result;
521 HE *he;
522 3395 HE *cursor = NULL;
523 3395 UV bucket = 0;
524 dds_key *keys;
525 SV *workspace;
526 3395 UV count = 0, capacity = HvTOTALKEYS(hv) + 1, i;
527 3395 const char *order = "smart";
528 3395 ▶ bool tied = SvTIED_mg((SV *)hv, PERL_MAGIC_tied) != NULL;
Branch: 2 branches
BranchTrueFalseTotal
bool tied = SvTIED_mg((SV *)hv, PERL_MAGIC_tied) != NULL; 62 3333 3395
bool tied = SvTIED_mg((SV *)hv, PERL_MAGIC_tied) != NULL; 2 60 62
529  
530 3395 ▶ if (tied)
Branch: 1 branch
BranchTrueFalseTotal
if (tied) 2 3393 3395
531 2 dds_callback_guard(aTHX_ ctx);
532  
533 /* Resolve the object policy first, then the class and default policies. */
534 3395 ▶ if (SvOK(class_name))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(class_name)) 130 3265 3395
535 130 DDS_CAT(class_key, class_name);
536 3395 sorter = dds_hget_sv(aTHX_ sorters, address);
537 3395 ▶ if (!SvTRUE(sorter)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(sorter)) { 3389 6 3395
538 3389 GV *method = NULL;
539  
540 3389 sorter = dds_hget_sv(aTHX_ sorters, class_key);
541 3389 ▶ if (!SvTRUE(sorter) && SvOK(class_name))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(sorter) && SvOK(class_name)) 2236 1153 3389
if (!SvTRUE(sorter) && SvOK(class_name)) 118 2118 2236
542 118 method = gv_fetchmethod_autoload(SvSTASH((SV *)hv), "DDS_sortkeys", 0);
543 3389 ▶ if (method && GvCV(method))
Branch: 2 branches
BranchTrueFalseTotal
if (method && GvCV(method)) 0 3389 3389
if (method && GvCV(method)) 0 0 0
544 0 sorter = DDS_TEMP(newRV_inc((SV *)GvCV(method)));
545 3389 ▶ if (!SvTRUE(sorter))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(sorter)) 2236 1153 3389
546 2236 sorter = dds_hget(aTHX_ sorters, ".");
547 }
548  
549 3395 ▶ if (SvTRUE(sorter) && !SvROK(sorter))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(sorter) && !SvROK(sorter)) 1159 2236 3395
if (SvTRUE(sorter) && !SvROK(sorter)) 532 627 1159
550 532 order = dds_builtin_order(aTHX_ sorter);
551  
552 3395 ▶ if (SvROK(sorter)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sorter)) { 627 2768 3395
553 627 AV *source = NULL;
554  
555 627 ▶ if (SvTYPE(SvRV(sorter)) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(sorter)) == SVt_PVAV) 10 617 627
556 10 source = (AV *)SvRV(sorter);
557 else {
558 SV *args[4], *values;
559 SV **entry, **thaw_entry;
560  
561 617 args[0] = DDS_TEMP(newRV_inc((SV *)hv));
562 617 args[1] = DDS_TEMP(newSViv(pass));
563 617 args[2] = DDS_TEMP(newSVuv(PTR2UV(hv)));
564 617 args[3] = class_name;
565 617 values = dds_invoke(aTHX_ ctx, sorter, FALSE, G_ARRAY, 4, args);
566 616 entry = av_fetch((AV *)SvRV(values), 0, 0);
567 616 thaw_entry = av_fetch((AV *)SvRV(values), 1, 0);
568 616 ▶ if (thaw && thaw_entry && SvTRUE(*thaw_entry))
Branch: 3 branches
BranchTrueFalseTotal
if (thaw && thaw_entry && SvTRUE(*thaw_entry)) 308 308 616
if (thaw && thaw_entry && SvTRUE(*thaw_entry)) 1 307 308
if (thaw && thaw_entry && SvTRUE(*thaw_entry)) 0 1 1
569 0 *thaw = *thaw_entry;
570  
571 616 ▶ if (entry && SvTRUE(*entry)) {
Branch: 2 branches
BranchTrueFalseTotal
if (entry && SvTRUE(*entry)) { 616 0 616
if (entry && SvTRUE(*entry)) { 616 0 616
572 616 ▶ if (!SvROK(*entry) || SvTYPE(SvRV(*entry)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(*entry) || SvTYPE(SvRV(*entry)) != SVt_PVAV) 616 0 616
if (!SvROK(*entry) || SvTYPE(SvRV(*entry)) != SVt_PVAV) 0 616 616
573 0 croak("Sortkeys callback must return an array reference or undef");
574 616 source = (AV *)SvRV(*entry);
575 }
576 }
577  
578 626 ▶ if (source) {
Branch: 1 branch
BranchTrueFalseTotal
if (source) { 626 0 626
579 SSize_t k;
580  
581 626 result = (AV *)DDS_TEMP((SV *)newAV());
582 1884 ▶ for (k = 0; k <= av_len(source); k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k <= av_len(source); k++) { 1258 626 1884
583 1258 SV **key = av_fetch(source, k, 0);
584  
585 1258 ▶ av_push(result, key ? newSVsv(*key) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(result, key ? newSVsv(*key) : newSV(0)); 1258 0 1258
586 }
587 626 return result;
588 }
589  
590 0 order = "each";
591 }
592  
593 2768 ▶ if (!order)
Branch: 1 branch
BranchTrueFalseTotal
if (!order) 0 2768 2768
594 0 croak("Unknown native key order");
595  
596 /* Tied hashes can report no physical keys and still iterate values.
597 * An SV-owned buffer can grow and remains protected on croak. */
598 2768 workspace = DDS_TEMP(newSV(capacity * sizeof(dds_key)));
599 2768 keys = (dds_key *)SvPVX(workspace);
600 2768 ▶ if (tied)
Branch: 1 branch
BranchTrueFalseTotal
if (tied) 2 2766 2768
601 2 hv_iterinit(hv);
602  
603 32313 while (1) {
604 /* No callbacks occur during an ordinary key walk. Walking buckets
605 * avoids disturbing a caller's each(), including its lazy deletion. */
606 35081 ▶ if (tied) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied) { 6 35075 35081
607 6 ▶ if (!(he = hv_iternext(hv)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(he = hv_iternext(hv))) 2 4 6
608 2 break;
609 } else {
610 120019 ▶ while (!cursor && bucket < DDS_HASH_LIMIT(hv)) {
Branch: 2 branches
BranchTrueFalseTotal
while (!cursor && bucket < DDS_HASH_LIMIT(hv)) { 87710 32309 120019
while (!cursor && bucket < DDS_HASH_LIMIT(hv)) { 84944 2766 87710
611 84944 ▶ cursor = DDS_HASH_FIRST(hv, bucket);
Branch: 1 branch
BranchTrueFalseTotal
cursor = DDS_HASH_FIRST(hv, bucket); 84728 216 84944
612 84944 bucket++;
613 }
614 35075 ▶ if (!cursor)
Branch: 1 branch
BranchTrueFalseTotal
if (!cursor) 2766 32309 35075
615 2766 break;
616 32309 he = cursor;
617 32309 cursor = DDS_HASH_NEXT(hv, cursor);
618 32309 ▶ if (HeVAL(he) == &PL_sv_placeholder)
Branch: 1 branch
BranchTrueFalseTotal
if (HeVAL(he) == &PL_sv_placeholder) 108 32201 32309
619 108 continue;
620 }
621  
622 32205 SV *key = DDS_TEMP(newSVsv(hv_iterkeysv(he)));
623 32205 STRLEN len, j = 0, start = 0;
624 32205 const char *p = SvPV(key, len);
625 dds_key *record;
626  
627 32205 ▶ if (count == capacity) {
Branch: 1 branch
BranchTrueFalseTotal
if (count == capacity) { 2 32203 32205
628 2 capacity *= 2;
629 2 ▶ SvGROW(workspace, capacity * sizeof(dds_key));
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(workspace, capacity * sizeof(dds_key)); 2 0 2
SvGROW(workspace, capacity * sizeof(dds_key)); 2 0 2
630 2 keys = (dds_key *)SvPVX(workspace);
631 }
632  
633 32205 record = &keys[count++];
634 32205 Zero(record, 1, dds_key);
635 32205 record->key = key;
636 32205 record->clean = DDS_TEMP(newSVpvs(""));
637  
638 /* Smart order compares a numeric prefix, then a folded word key. */
639 32205 ▶ if (len && p[j] == '-')
Branch: 2 branches
BranchTrueFalseTotal
if (len && p[j] == '-') 32205 0 32205
if (len && p[j] == '-') 8 32197 32205
640 8 j++;
641 32205 ▶ if (j < len && p[j] >= '0' && p[j] <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
if (j < len && p[j] >= '0' && p[j] <= '9') { 32201 4 32205
if (j < len && p[j] >= '0' && p[j] <= '9') { 32193 8 32201
if (j < len && p[j] >= '0' && p[j] <= '9') { 616 31577 32193
642 616 STRLEN first = j;
643  
644 616 ▶ if (p[j] == '0')
Branch: 1 branch
BranchTrueFalseTotal
if (p[j] == '0') 44 572 616
645 44 j++;
646 else
647 1288 ▶ while (j < len && isDIGIT_A(p[j]) && j - first < 9)
Branch: 3 branches
BranchTrueFalseTotal
while (j < len && isDIGIT_A(p[j]) && j - first < 9) 944 344 1288
while (j < len && isDIGIT_A(p[j]) && j - first < 9) 716 228 944
while (j < len && isDIGIT_A(p[j]) && j - first < 9) 716 0 716
648 716 j++;
649 616 ▶ if (j == len || !isDIGIT_A(p[j])) {
Branch: 2 branches
BranchTrueFalseTotal
if (j == len || !isDIGIT_A(p[j])) { 236 380 616
if (j == len || !isDIGIT_A(p[j])) { 236 0 236
650 616 ▶ if (j < len && p[j] == '.' && j + 1 < len && isDIGIT_A(p[j + 1])) {
Branch: 4 branches
BranchTrueFalseTotal
if (j < len && p[j] == '.' && j + 1 < len && isDIGIT_A(p[j + 1])) { 236 380 616
if (j < len && p[j] == '.' && j + 1 < len && isDIGIT_A(p[j + 1])) { 0 236 236
if (j < len && p[j] == '.' && j + 1 < len && isDIGIT_A(p[j + 1])) { 0 0 0
if (j < len && p[j] == '.' && j + 1 < len && isDIGIT_A(p[j + 1])) { 0 0 0
651 0 STRLEN fraction = ++j;
652  
653 0 ▶ while (j < len && isDIGIT_A(p[j]) && j - fraction < 15)
Branch: 3 branches
BranchTrueFalseTotal
while (j < len && isDIGIT_A(p[j]) && j - fraction < 15) 0 0 0
while (j < len && isDIGIT_A(p[j]) && j - fraction < 15) 0 0 0
while (j < len && isDIGIT_A(p[j]) && j - fraction < 15) 0 0 0
654 0 j++;
655 }
656 616 ▶ if (j == len || !isDIGIT_A(p[j])) {
Branch: 2 branches
BranchTrueFalseTotal
if (j == len || !isDIGIT_A(p[j])) { 236 380 616
if (j == len || !isDIGIT_A(p[j])) { 236 0 236
657 616 SV *number = DDS_TEMP(newSVpvn(p, j));
658  
659 616 record->numeric = TRUE;
660 616 record->number = SvNV(number);
661 616 start = j;
662 }
663 }
664 }
665  
666 175625 ▶ for (j = start; j < len;) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = start; j < len;) { 143420 32205 175625
667 143420 STRLEN width = 1;
668 143420 UV ch = (U8)p[j];
669  
670 143420 ▶ if (SvUTF8(key))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(key)) 4 143416 143420
671 4 ch = utf8_to_uvchr_buf((U8 *)p + j, (U8 *)p + len, &width);
672 143420 j += width;
673 143420 ▶ if (isWORDCHAR_uni(ch)) {
Branch: 4 branches
BranchTrueFalseTotal
if (isWORDCHAR_uni(ch)) { 143416 4 143420
if (isWORDCHAR_uni(ch)) { 143416 0 143416
if (isWORDCHAR_uni(ch)) { 143378 38 143416
if (isWORDCHAR_uni(ch)) { 143382 38 143420
674 U8 lower[UTF8_MAXBYTES + 1];
675 STRLEN lower_len;
676  
677 143382 toLOWER_uni(ch, lower, &lower_len);
678 143382 sv_catpvn(record->clean, (char *)lower, lower_len);
679 143382 SvUTF8_on(record->clean);
680 }
681 }
682  
683 32205 ▶ if (strnEQ(order, "num", 3))
Branch: 1 branch
BranchTrueFalseTotal
if (strnEQ(order, "num", 3)) 24 32181 32205
684 24 record->number = SvNV(key);
685 }
686  
687 2768 ▶ if (strnEQ(order, "lex", 3) || strnEQ(order, "alph", 4))
Branch: 2 branches
BranchTrueFalseTotal
if (strnEQ(order, "lex", 3) || strnEQ(order, "alph", 4)) 2764 4 2768
if (strnEQ(order, "lex", 3) || strnEQ(order, "alph", 4)) 4 2760 2764
688 8 qsort(keys, count, sizeof(dds_key), dds_key_lexical);
689 2760 ▶ else if (strnEQ(order, "num", 3))
Branch: 1 branch
BranchTrueFalseTotal
else if (strnEQ(order, "num", 3)) 4 2756 2760
690 4 qsort(keys, count, sizeof(dds_key), dds_key_numeric);
691 2756 ▶ else if (!strnEQ(order, "each", 4))
Branch: 1 branch
BranchTrueFalseTotal
else if (!strnEQ(order, "each", 4)) 2500 256 2756
692 2500 qsort(keys, count, sizeof(dds_key), dds_key_compare);
693  
694 2768 result = (AV *)DDS_TEMP((SV *)newAV());
695 34973 ▶ for (i = 0; i < count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) 32205 2768 34973
696 32205 av_push(result, newSVsv(keys[i].key));
697  
698 2768 return result;
699 }
700  
701 /* Collect restricted-hash placeholder keys without disturbing the caller's
702 * iterator, returning a sorted mortal array.
703 */
704 43 AV *dds_hidden_keys(pTHX_ HV *hv)
705 {
706 43 AV *keys = (AV *)DDS_TEMP((SV *)newAV());
707 HE *he;
708 UV bucket;
709  
710 43 ▶ if (HvARRAY(hv))
Branch: 1 branch
BranchTrueFalseTotal
if (HvARRAY(hv)) 43 0 43
711 435 ▶ for (bucket = 0; bucket < DDS_HASH_LIMIT(hv); bucket++)
Branch: 1 branch
BranchTrueFalseTotal
for (bucket = 0; bucket < DDS_HASH_LIMIT(hv); bucket++) 392 43 435
712 571 ▶ for (he = DDS_HASH_FIRST(hv, bucket); he; he = DDS_HASH_NEXT(hv, he))
Branch: 2 branches
BranchTrueFalseTotal
for (he = DDS_HASH_FIRST(hv, bucket); he; he = DDS_HASH_NEXT(hv, he)) 392 0 392
for (he = DDS_HASH_FIRST(hv, bucket); he; he = DDS_HASH_NEXT(hv, he)) 179 392 571
713 179 ▶ if (HeVAL(he) == &PL_sv_placeholder)
Branch: 1 branch
BranchTrueFalseTotal
if (HeVAL(he) == &PL_sv_placeholder) 58 121 179
714 58 av_push(keys, newSVsv(hv_iterkeysv(he)));
715  
716 43 ▶ if (av_len(keys) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(keys) > 0) 12 31 43
717 12 sortsv(AvARRAY(keys), av_len(keys) + 1, Perl_sv_cmp);
718 43 return keys;
719 }
720  
721 /*
722 * Regexp literals
723 *
724 * Inspection and quoting are separate: recover the original pattern and flags,
725 * then protect them from Perl interpolation and delimiter parsing.
726 */
727  
728 /* Recognize a regexp referent using the representation supported by the
729 * current interpreter.
730 */
731 15694 bool dds_is_regexp(pTHX_ SV *target)
732 {
733 #if PERL_VERSION >= 11
734 15694 return SvTYPE(target) == SVt_REGEXP;
735 #else
736 MAGIC *magic;
737  
738 return SvTYPE(target) == SVt_PVMG && SvMAGICAL(target) &&
739 (magic = mg_find(target, PERL_MAGIC_qr)) && magic->mg_obj;
740 #endif
741 }
742  
743 /* Build a mortal qr expression that preserves pattern text and modifiers
744 * through delimiter escaping and sigil interpolation.
745 */
746 133 SV *dds_regex(pTHX_ dds_ctx *ctx, SV *rv)
747 {
748 SV *values, *pattern, *mods, *out;
749 AV *parts;
750 STRLEN len, i, width;
751 const U8 *p;
752 static const char *delimiters[] = {"/", "!", "%", "&", "<>", "{}", "\"", "#"};
753 133 UV best = UV_MAX, chosen = 0, d;
754  
755 133 values = dds_call(aTHX_ ctx, "Data::Dump::Streamer::regex", NULL, G_ARRAY, 1, &rv);
756 133 parts = (AV *)SvRV(values);
757 133 ▶ if (av_len(parts) != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(parts) != 1) 0 133 133
758 0 croak("Cannot inspect regexp");
759 133 pattern = *av_fetch(parts, 0, 0);
760 133 mods = *av_fetch(parts, 1, 0);
761 133 ▶ p = (const U8 *)SvPV(pattern, len);
Branch: 1 branch
BranchTrueFalseTotal
p = (const U8 *)SvPV(pattern, len); 133 0 133
762  
763 /* In /x patterns a literal whitespace character and an escaped one
764 * have different meanings. Keep Unicode Pattern_White_Space literal
765 * too. Its five non-ASCII members are fixed by Unicode; the parser's
766 * internal classifier is not exposed to extensions on newer Perls. */
767 133 ▶ if (strchr(DDS_PV(mods), 'x')) {
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(DDS_PV(mods), 'x')) { 15 118 133
768 15 bool preserve_layout = FALSE;
769  
770 44 ▶ for (i = 0; i < len; i += width) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i += width) { 43 1 44
771 43 UV ch = p[i];
772  
773 43 width = 1;
774 43 ▶ if (SvUTF8(pattern))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(pattern)) 23 20 43
775 23 ch = utf8_to_uvchr_buf(p + i, p + len, &width);
776  
777 43 ▶ if (ch < 32 || ch == 127 || ch == 0x85 || ch == 0x200e || ch == 0x200f ||
Branch: 6 branches
BranchTrueFalseTotal
if (ch < 32 || ch == 127 || ch == 0x85 || ch == 0x200e || ch == 0x200f || 39 4 43
if (ch < 32 || ch == 127 || ch == 0x85 || ch == 0x200e || ch == 0x200f || 39 0 39
if (ch < 32 || ch == 127 || ch == 0x85 || ch == 0x200e || ch == 0x200f || 37 2 39
if (ch < 32 || ch == 127 || ch == 0x85 || ch == 0x200e || ch == 0x200f || 35 2 37
if (ch < 32 || ch == 127 || ch == 0x85 || ch == 0x200e || ch == 0x200f || 33 2 35
if (ch < 32 || ch == 127 || ch == 0x85 || ch == 0x200e || ch == 0x200f || 31 2 33
778 31 ▶ ch == 0x2028 || ch == 0x2029) {
Branch: 1 branch
BranchTrueFalseTotal
ch == 0x2028 || ch == 0x2029) { 2 29 31
779 14 preserve_layout = TRUE;
780 14 break;
781 }
782 }
783  
784 15 ▶ if (preserve_layout) {
Branch: 1 branch
BranchTrueFalseTotal
if (preserve_layout) { 14 1 15
785 14 out = DDS_TEMP(newSVpvs("do { my $p = "));
786 14 DDS_CAT(out, dds_qquote(aTHX_ pattern));
787 14 sv_catpvs(out, "; qr/$p/");
788 14 DDS_CAT(out, mods);
789 14 sv_catpvs(out, " }");
790 14 return out;
791 }
792 }
793  
794 /* Prefer a delimiter that introduces the fewest new escapes. */
795 140 ▶ for (d = 0; d < sizeof(delimiters) / sizeof(*delimiters); d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < sizeof(delimiters) / sizeof(*delimiters); d++) { 139 1 140
796 139 UV bad = 0;
797  
798 1151 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 1012 139 1151
799 1012 ▶ if (strchr(delimiters[d], p[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(delimiters[d], p[i])) 24 988 1012
800 24 bad++;
801 139 ▶ if (bad < best) {
Branch: 1 branch
BranchTrueFalseTotal
if (bad < best) { 128 11 139
802 128 best = bad;
803 128 chosen = d;
804 }
805 139 ▶ if (!bad)
Branch: 1 branch
BranchTrueFalseTotal
if (!bad) 118 21 139
806 118 break;
807 }
808  
809 /* Sigil interpolation below uses slash delimiters so q(...) remains
810 * independent of the surrounding regexp's quoting rules. */
811 685 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 604 81 685
812 604 ▶ if (p[i] == '\\' && i + 1 < len) {
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] == '\\' && i + 1 < len) { 45 559 604
if (p[i] == '\\' && i + 1 < len) { 45 0 45
813 45 i++;
814 45 continue;
815 }
816 559 ▶ if (p[i] == '$' || p[i] == '@') {
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] == '$' || p[i] == '@') { 535 24 559
if (p[i] == '$' || p[i] == '@') { 14 521 535
817 38 chosen = 0;
818 38 break;
819 }
820 }
821  
822 119 out = DDS_TEMP(newSVpvs("qr"));
823 119 sv_catpvn(out, delimiters[chosen], 1);
824  
825 886 ▶ for (i = 0; i < len; i += width) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i += width) { 767 119 886
826 767 UV ch = p[i];
827  
828 767 width = 1;
829 767 ▶ if (SvUTF8(pattern))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(pattern)) 70 697 767
830 70 ch = utf8_to_uvchr_buf(p + i, p + len, &width);
831 767 ▶ if (ch == '\\' && i + 1 < len) {
Branch: 2 branches
BranchTrueFalseTotal
if (ch == '\\' && i + 1 < len) { 47 720 767
if (ch == '\\' && i + 1 < len) { 47 0 47
832 47 STRLEN next_width = 1;
833  
834 47 ▶ if (SvUTF8(pattern))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(pattern)) 3 44 47
835 3 (void)utf8_to_uvchr_buf(p + i + 1, p + len, &next_width);
836  
837 /* Raw UTF-8 bytes need an SV carrying their encoding so the
838 * output and its enclosing source string are upgraded too. */
839 47 ▶ if (next_width > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (next_width > 1) 2 45 47
840 2 DDS_CAT(out, dds_string_slice(aTHX_ pattern, i, 1 + next_width));
841 else
842 45 sv_catpvn(out, (const char *)p + i, 1 + next_width);
843 47 width = 1 + next_width;
844 720 ▶ } else if (ch == '@' || (ch == '$' && i + 1 < len && !strchr("()| \t\r\n", p[i + 1]))) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (ch == '@' || (ch == '$' && i + 1 < len && !strchr("()| \t\r\n", p[i + 1]))) { 706 14 720
} else if (ch == '@' || (ch == '$' && i + 1 < len && !strchr("()| \t\r\n", p[i + 1]))) { 27 679 706
} else if (ch == '@' || (ch == '$' && i + 1 < len && !strchr("()| \t\r\n", p[i + 1]))) { 26 1 27
} else if (ch == '@' || (ch == '$' && i + 1 < len && !strchr("()| \t\r\n", p[i + 1]))) { 22 4 26
845 36 STRLEN end = i + width;
846  
847 69 ▶ while (end < len && (p[end] == '{' || p[end] == '['))
Branch: 3 branches
BranchTrueFalseTotal
while (end < len && (p[end] == '{' || p[end] == '[')) 69 0 69
while (end < len && (p[end] == '{' || p[end] == '[')) 20 49 69
while (end < len && (p[end] == '{' || p[end] == '[')) 13 36 49
848 33 end++;
849  
850 /* Capture the entire opening-bracket run, as in Yves Orton's
851 * Data::Dumper sigil-interpolation patch. Interpolation then
852 * gives the regexp parser exactly the original pattern text. */
853 36 sv_catpvs(out, "${\\q(");
854 36 sv_catpvn(out, (const char *)p + i, end - i);
855 36 sv_catpvs(out, ")}");
856 36 width = end - i;
857 684 ▶ } else if (ch < 32 || ch > 126)
Branch: 2 branches
BranchTrueFalseTotal
} else if (ch < 32 || ch > 126) 680 4 684
} else if (ch < 32 || ch > 126) 5 675 680
858 9 sv_catpvf(out, "\\x{%" UVxf "}", ch);
859 else {
860 675 ▶ if (strchr(delimiters[chosen], (char)ch))
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(delimiters[chosen], (char)ch)) 0 675 675
861 0 sv_catpvs(out, "\\");
862 675 sv_catpvn(out, (const char *)p + i, width);
863 }
864 }
865  
866 119 sv_catpvn(out, delimiters[chosen] + strlen(delimiters[chosen]) - 1, 1);
867 119 DDS_CAT(out, mods);
868  
869 119 return out;
870 }
871  
872 /* Send text to the configured print callback or append it to buffered output,
873 * maintaining list-context output chunks when requested.
874 */
875 215104 void dds_write(pTHX_ dds_ctx *ctx, SV *text)
876 {
877 215104 ▶ if (ctx->printer) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->printer) { 27 215077 215104
878 SV *args[1];
879  
880 27 args[0] = text;
881 27 (void)dds_call(aTHX_ ctx, "print", ctx->printer, G_VOID | G_DISCARD, 1, args);
882 } else {
883 215077 DDS_CAT(ctx->out, text);
884 215077 ▶ if (ctx->chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->chunks) { 273 214804 215077
885 273 SV **last =
886 273 ▶ av_len(ctx->chunks) >= 0 ? av_fetch(ctx->chunks, av_len(ctx->chunks), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
av_len(ctx->chunks) >= 0 ? av_fetch(ctx->chunks, av_len(ctx->chunks), 0) : NULL; 263 10 273
887  
888 536 ▶ if (!last || strchr(DDS_PV(*last), '\n') || SvCUR(*last) > 4000)
Branch: 3 branches
BranchTrueFalseTotal
if (!last || strchr(DDS_PV(*last), '\n') || SvCUR(*last) > 4000) 263 10 273
if (!last || strchr(DDS_PV(*last), '\n') || SvCUR(*last) > 4000) 216 47 263
if (!last || strchr(DDS_PV(*last), '\n') || SvCUR(*last) > 4000) 0 216 216
889 57 av_push(ctx->chunks, newSVsv(text));
890 else
891 216 DDS_CAT(*last, text);
892 }
893 }
894 215102 }
895  
896 /* Write a NUL-terminated C string through the common output path. */
897 38567 void dds_put(pTHX_ dds_ctx *ctx, const char *text)
898 {
899 38567 dds_write(aTHX_ ctx, DDS_TEMP(newSVpv(text, 0)));
900 38567 }
901  
902 /* Write a nonnegative run of spaces for output indentation and alignment. */
903 39488 void dds_spaces(pTHX_ dds_ctx *ctx, IV count)
904 {
905 SV *space;
906  
907 39488 ▶ if (count < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (count < 0) 0 39488 39488
908 0 count = 0;
909  
910 39488 space = DDS_TEMP(newSVpvs(""));
911 39488 ▶ SvGROW(space, (STRLEN)count + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(space, (STRLEN)count + 1); 39488 0 39488
SvGROW(space, (STRLEN)count + 1); 459 39029 39488
912 39488 SvPOK_on(space);
913 39488 SvCUR_set(space, count);
914 39488 memset(SvPVX(space), ' ', count);
915 39488 SvPVX(space)[count] = 0;
916 39488 dds_write(aTHX_ ctx, space);
917 39488 }
« dds_emit.c ↑ Summary dds_engine.c »