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