XS.xs

filter: Statementstmt 94.8% 555 / 585 Branchbran 81.6% 436 / 534 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.5% 991 / 1119 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ckdl/src/utf8.c ↑ Summary ckdl/src/bigint.c »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "ppport.h"
6  
7 #include <kdl/kdl.h>
8  
9 #include <stdlib.h>
10 #include <string.h>
11  
12 #if IVSIZE < 8
13 # error "Text::KDL::XS needs a perl with 64-bit integers (ivsize >= 8)"
14 #endif
15  
16 #define PTKX_PARSER_CLASS "Text::KDL::XS::Parser"
17 #define PTKX_EMITTER_CLASS "Text::KDL::XS::Emitter"
18 #define PTKX_VALUE_CLASS "Text::KDL::XS::Value"
19  
20 /* Room for any number this module formats: 36 significant digits of a quad
21 * with sign, point and a four-digit exponent, plus ".0". */
22 #define PTKX_NUMBER_BUFSIZE 64
23  
24 #define PTKX_MAX_INDENT 64
25  
26 #define PTKX_V1_NON_FINITE "emit_kdl: KDL v1 has no representation for inf/nan"
27  
28 /* Keeps the object behind the reference self alive until the caller's next
29 * FREETMPS, for XSUBs that run Perl code (source callbacks, overloaded
30 * operators) while they hold the object's C handle. */
31 #define PTKX_KEEP_ALIVE(self) sv_2mortal(SvREFCNT_inc_simple_NN(SvRV(self)))
32  
33 /* KDL numbers always use '.' as the radix, so they are formatted and read
34 * back with LC_NUMERIC in the C locale whatever the program's locale is. */
35 #if defined(STORE_LC_NUMERIC_SET_STANDARD)
36 # define PTKX_DECLARE_NUMERIC_STATE DECLARATION_FOR_LC_NUMERIC_MANIPULATION
37 # define PTKX_NUMERIC_STANDARD() STORE_LC_NUMERIC_SET_STANDARD()
38 # define PTKX_NUMERIC_RESTORE() RESTORE_LC_NUMERIC()
39 #elif defined(STORE_LC_NUMERIC_SET_TO_NEEDED)
40 # define PTKX_DECLARE_NUMERIC_STATE DECLARATION_FOR_LC_NUMERIC_MANIPULATION
41 # define PTKX_NUMERIC_STANDARD() STORE_LC_NUMERIC_SET_TO_NEEDED()
42 # define PTKX_NUMERIC_RESTORE() RESTORE_LC_NUMERIC()
43 #else
44 # define PTKX_DECLARE_NUMERIC_STATE dNOOP
45 # define PTKX_NUMERIC_STANDARD() NOOP
46 # define PTKX_NUMERIC_RESTORE() NOOP
47 #endif
48  
49 /* ------------------------------------------------------------------------- */
50 /* Types */
51 /* ------------------------------------------------------------------------- */
52  
53 typedef enum {
54 PTKX_ACTIVE, /* events can be read */
55 PTKX_FINISHED, /* end of input reached: every further call returns undef */
56 PTKX_FAILED /* an error occurred: every further call raises it again */
57 } ptkx_parser_state;
58  
59 /* Progress of the UTF-8 validator through a multi-byte sequence. */
60 typedef struct {
61 U8 continuation_bytes; /* continuation bytes still expected */
62 UV code_point; /* bits collected so far */
63 UV minimum; /* smallest code point this sequence length may encode */
64 } ptkx_utf8_state;
65  
66 typedef struct {
67 kdl_parser* parser; /* NULL once finished or failed */
68 HV* value_stash; /* Text::KDL::XS::Value, for parsed values */
69 SV* read_cb; /* source callback, or NULL */
70 SV* source_copy; /* private copy of a string source; ckdl reads its buffer */
71 SV* pending_chunk; /* callback bytes not yet handed to ckdl */
72 STRLEN pending_offset; /* bytes of pending_chunk already handed over */
73 SV* error; /* exception to raise, or NULL */
74 ptkx_parser_state state;
75 bool in_callback; /* the source callback is running */
76 bool source_exhausted; /* the source callback signalled end of input */
77 ptkx_utf8_state utf8; /* validation of callback bytes across chunks */
78 IV depth; /* nodes currently open */
79 IV max_depth; /* deepest nesting allowed, 0 for unlimited */
80 } ptkx_parser;
81  
82 typedef struct {
83 kdl_emitter* emitter;
84 kdl_version version; /* syntax of the output */
85 bool writes_bare; /* identifiers may be written without quotes */
86 bool needs_quoting; /* some bare text would read back as a keyword or number */
87 } ptkx_emitter;
88  
89 typedef enum {
90 PTKX_NOT_A_NUMBER,
91 PTKX_INTEGER,
92 PTKX_FLOAT
93 } ptkx_number_kind;
94  
95 /* ------------------------------------------------------------------------- */
96 /* Scalars */
97 /* ------------------------------------------------------------------------- */
98  
99 /* sv itself, or a mortal copy when sv has get-magic, so that a tied or
100 * otherwise magical value is fetched exactly once. */
101 static SV*
102 62054 ptkx_plain(pTHX_ SV* sv)
103 {
104 62054 ▶ return SvGMAGICAL(sv) ? sv_mortalcopy(sv) : sv;
Branch: 1 branch
BranchTrueFalseTotal
return SvGMAGICAL(sv) ? sv_mortalcopy(sv) : sv; 0 62054 62054
105 }
106  
107 /* Drops the reference held in *slot and clears the slot first, so that code
108 * run by a destructor never sees a dangling pointer. */
109 static void
110 1135 ptkx_clear_sv(pTHX_ SV** slot)
111 {
112 1135 SV* sv = *slot;
113 1135 *slot = NULL;
114 1135 SvREFCNT_dec(sv);
115 1135 }
116  
117 /* ------------------------------------------------------------------------- */
118 /* UTF-8 */
119 /* ------------------------------------------------------------------------- */
120  
121 static bool
122 94 ptkx_is_unicode_scalar(UV code_point, UV minimum)
123 {
124 return code_point >= minimum
125 88 ▶ && code_point <= 0x10FFFF
Branch: 1 branch
BranchTrueFalseTotal
&& code_point <= 0x10FFFF 81 7 88
126 182 ▶ && (code_point < 0xD800 || code_point > 0xDFFF);
Branch: 3 branches
BranchTrueFalseTotal
&& (code_point < 0xD800 || code_point > 0xDFFF); 88 6 94
&& (code_point < 0xD800 || code_point > 0xDFFF); 17 64 81
&& (code_point < 0xD800 || code_point > 0xDFFF); 6 11 17
127 }
128  
129 static void
130 98 ptkx_utf8_begin(ptkx_utf8_state* state, U8 continuation_bytes, UV lead_bits, UV minimum)
131 {
132 98 state->continuation_bytes = continuation_bytes;
133 98 state->code_point = lead_bits;
134 98 state->minimum = minimum;
135 98 }
136  
137 /* Feeds bytes to an incremental validator. Returns FALSE at the first byte
138 * that makes the input anything but RFC 3629 UTF-8: a malformed or overlong
139 * sequence, a surrogate or a code point above U+10FFFF. A sequence may be
140 * split across calls; state->continuation_bytes is non-zero while one is
141 * incomplete. */
142 static bool
143 6904 ptkx_utf8_feed(ptkx_utf8_state* state, const U8* bytes, STRLEN len)
144 {
145 6904 const U8* end = bytes + len;
146  
147 297533 ▶ for (; bytes < end; ++bytes) {
Branch: 1 branch
BranchTrueFalseTotal
for (; bytes < end; ++bytes) { 290656 6877 297533
148 290656 U8 byte = *bytes;
149  
150 290656 ▶ if (state->continuation_bytes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (state->continuation_bytes > 0) { 166 290490 290656
151 166 ▶ if ((byte & 0xC0) != 0x80) return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if ((byte & 0xC0) != 0x80) return FALSE; 3 163 166
152 163 state->code_point = (state->code_point << 6) | (byte & 0x3F);
153 163 ▶ if (--state->continuation_bytes == 0
Branch: 1 branch
BranchTrueFalseTotal
if (--state->continuation_bytes == 0 94 69 163
154 94 ▶ && !ptkx_is_unicode_scalar(state->code_point, state->minimum))
Branch: 1 branch
BranchTrueFalseTotal
&& !ptkx_is_unicode_scalar(state->code_point, state->minimum)) 24 70 94
155 24 return FALSE;
156 139 continue;
157 }
158 290490 ▶ if (byte < 0x80) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (byte < 0x80) continue; 290392 98 290490
159  
160 98 ▶ if ((byte & 0xE0) == 0xC0) ptkx_utf8_begin(state, 1, byte & 0x1F, 0x80);
Branch: 1 branch
BranchTrueFalseTotal
if ((byte & 0xE0) == 0xC0) ptkx_utf8_begin(state, 1, byte & 0x1F, 0x80); 42 56 98
161 56 ▶ else if ((byte & 0xF0) == 0xE0) ptkx_utf8_begin(state, 2, byte & 0x0F, 0x800);
Branch: 1 branch
BranchTrueFalseTotal
else if ((byte & 0xF0) == 0xE0) ptkx_utf8_begin(state, 2, byte & 0x0F, 0x800); 43 13 56
162 13 ▶ else if ((byte & 0xF8) == 0xF0) ptkx_utf8_begin(state, 3, byte & 0x07, 0x10000);
Branch: 1 branch
BranchTrueFalseTotal
else if ((byte & 0xF8) == 0xF0) ptkx_utf8_begin(state, 3, byte & 0x07, 0x10000); 13 0 13
163 0 else return FALSE;
164 }
165 6877 return TRUE;
166 }
167  
168 /* True if text[0..len) is complete RFC 3629 UTF-8 (see ptkx_utf8_feed). */
169 static bool
170 6745 ptkx_is_unicode_utf8(const char* text, STRLEN len)
171 {
172 6745 ptkx_utf8_state state = { 0, 0, 0 };
173 6745 return ptkx_utf8_feed(&state, (const U8*) text, len)
174 6745 ▶ && state.continuation_bytes == 0;
Branch: 2 branches
BranchTrueFalseTotal
&& state.continuation_bytes == 0; 6733 12 6745
&& state.continuation_bytes == 0; 6733 0 6733
175 }
176  
177 /* True for a missing string (NULL data) and for valid Unicode text. */
178 static bool
179 47537 ptkx_is_text(kdl_str text)
180 {
181 47537 ▶ return text.data == NULL || ptkx_is_unicode_utf8(text.data, text.len);
Branch: 2 branches
BranchTrueFalseTotal
return text.data == NULL || ptkx_is_unicode_utf8(text.data, text.len); 5998 41539 47537
return text.data == NULL || ptkx_is_unicode_utf8(text.data, text.len); 5996 2 5998
182 }
183  
184 static bool
185 592 ptkx_is_ascii(const char* text, STRLEN len)
186 {
187 592 const char* end = text + len;
188 2172 ▶ for (; text < end; ++text)
Branch: 1 branch
BranchTrueFalseTotal
for (; text < end; ++text) 1586 586 2172
189 1586 ▶ if ((U8) *text >= 0x80) return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if ((U8) *text >= 0x80) return FALSE; 6 1580 1586
190 586 return TRUE;
191 }
192  
193 /* ------------------------------------------------------------------------- */
194 /* Numbers */
195 /* ------------------------------------------------------------------------- */
196  
197 static STRLEN
198 29 ptkx_copy_text(char* buf, const char* text)
199 {
200 29 STRLEN len = strlen(text);
201 29 Copy(text, buf, len + 1, char);
202 29 return len;
203 }
204  
205 /* Text to NV at the NV's own width, inside PTKX_NUMERIC_STANDARD(). Perl's
206 * Strtod() makes the same choice but switches LC_NUMERIC on its own. */
207 #if defined(USE_QUADMATH)
208 # define PTKX_STRTONV(text) strtoflt128(text, NULL)
209 #elif defined(USE_LONG_DOUBLE) && defined(__MINGW64_VERSION_MAJOR)
210 # define PTKX_STRTONV(text) __mingw_strtold(text, NULL)
211 #elif defined(USE_LONG_DOUBLE)
212 # define PTKX_STRTONV(text) strtold(text, NULL)
213 #else
214 # define PTKX_STRTONV(text) strtod(text, NULL)
215 #endif
216  
217 /* Significant digits that always read back as the same NV: 17 for a
218 * double, 21 for an x87 long double, 36 for a quad. */
219 #define PTKX_NV_ROUND_TRIP_DIGITS (NV_DIG + 3)
220  
221 /* Writes nv into buf with precision significant digits, "%.<precision>g"
222 * at the NV's width. my_snprintf() knows %Lg and, on a quadmath perl, %Qg,
223 * but only with the precision spelled out: it reads one NV argument. */
224 static STRLEN
225 23947 ptkx_format_nv_digits(NV nv, int precision, char* buf)
226 {
227 char format[16];
228 23947 snprintf(format, sizeof format, "%%.%d" NVgf, precision);
229 23947 return (STRLEN) my_snprintf(buf, PTKX_NUMBER_BUFSIZE, format, nv);
230 }
231  
232 /* Writes nv as a KDL number into buf (PTKX_NUMBER_BUFSIZE bytes): the
233 * shortest rendering of 15 to PTKX_NV_ROUND_TRIP_DIGITS significant digits
234 * that reads back as nv, with ".0" appended when the text would otherwise
235 * read back as an integer; #inf, #-inf or #nan when nv is not finite.
236 * Returns the length. */
237 static STRLEN
238 10033 ptkx_format_nv(pTHX_ NV nv, char* buf)
239 {
240 10033 STRLEN len = 0;
241 int precision;
242 10033 PTKX_DECLARE_NUMERIC_STATE;
243 PERL_UNUSED_CONTEXT;
244  
245 10033 ▶ if (Perl_isnan(nv)) return ptkx_copy_text(buf, "#nan");
Branch: 1 branch
BranchTrueFalseTotal
if (Perl_isnan(nv)) return ptkx_copy_text(buf, "#nan"); 2 10031 10033
246 10031 ▶ if (Perl_isinf(nv)) return ptkx_copy_text(buf, nv < 0 ? "#-inf" : "#inf");
Branch: 2 branches
BranchTrueFalseTotal
if (Perl_isinf(nv)) return ptkx_copy_text(buf, nv < 0 ? "#-inf" : "#inf"); 4 10027 10031
if (Perl_isinf(nv)) return ptkx_copy_text(buf, nv < 0 ? "#-inf" : "#inf"); 2 2 4
247  
248 10027 ▶ PTKX_NUMERIC_STANDARD();
Branch: 1 branch
BranchTrueFalseTotal
PTKX_NUMERIC_STANDARD(); 0 10027 10027
249 23947 ▶ for (precision = 15; precision <= PTKX_NV_ROUND_TRIP_DIGITS; ++precision) {
Branch: 1 branch
BranchTrueFalseTotal
for (precision = 15; precision <= PTKX_NV_ROUND_TRIP_DIGITS; ++precision) { 23947 0 23947
250 23947 len = ptkx_format_nv_digits(nv, precision, buf);
251 23947 ▶ if (PTKX_STRTONV(buf) == nv) break;
Branch: 1 branch
BranchTrueFalseTotal
if (PTKX_STRTONV(buf) == nv) break; 10027 13920 23947
252 }
253 10027 ▶ PTKX_NUMERIC_RESTORE();
Branch: 1 branch
BranchTrueFalseTotal
PTKX_NUMERIC_RESTORE(); 0 10027 10027
254  
255 10027 ▶ if (!strpbrk(buf, ".e")) len += ptkx_copy_text(buf + len, ".0");
Branch: 1 branch
BranchTrueFalseTotal
if (!strpbrk(buf, ".e")) len += ptkx_copy_text(buf + len, ".0"); 23 10004 10027
256 10027 return len;
257 }
258  
259 /* ckdl reports a floating point number only for literals with at most 15
260 * significant digits, but computes it as mantissa / pow(10, k), which can be
261 * an ulp off. The 15-digit rendering of that double is the literal again;
262 * reading it at the NV's width gives the NV nearest the literal. */
263 static NV
264 568 ptkx_nv_of_literal(pTHX_ double d)
265 {
266 char buf[PTKX_NUMBER_BUFSIZE];
267 NV nearest;
268 568 PTKX_DECLARE_NUMERIC_STATE;
269 PERL_UNUSED_CONTEXT;
270  
271 568 ▶ if (Perl_isnan(d) || Perl_isinf(d)) return (NV) d;
Branch: 2 branches
BranchTrueFalseTotal
if (Perl_isnan(d) || Perl_isinf(d)) return (NV) d; 567 1 568
if (Perl_isnan(d) || Perl_isinf(d)) return (NV) d; 2 565 567
272  
273 565 ▶ PTKX_NUMERIC_STANDARD();
Branch: 1 branch
BranchTrueFalseTotal
PTKX_NUMERIC_STANDARD(); 0 565 565
274 565 snprintf(buf, sizeof buf, "%.15g", d);
275 565 nearest = PTKX_STRTONV(buf);
276 565 ▶ PTKX_NUMERIC_RESTORE();
Branch: 1 branch
BranchTrueFalseTotal
PTKX_NUMERIC_RESTORE(); 0 565 565
277 565 return nearest;
278 }
279  
280 static bool
281 126 ptkx_is_digit(char c, int radix)
282 {
283 126 switch (radix) {
284 3 ▶ case 2: return c == '0' || c == '1';
Branch: 2 branches
BranchTrueFalseTotal
case 2: return c == '0' || c == '1'; 2 1 3
case 2: return c == '0' || c == '1'; 2 0 2
285 7 ▶ case 8: return c >= '0' && c <= '7';
Branch: 2 branches
BranchTrueFalseTotal
case 8: return c >= '0' && c <= '7'; 7 0 7
case 8: return c >= '0' && c <= '7'; 7 0 7
286 104 case 10: return isDIGIT(c);
287 12 default: return isXDIGIT(c);
288 }
289 }
290  
291 /* Advances *p over a digit of the given radix followed by digits and '_'
292 * separators. Returns FALSE when *p does not start with a digit. */
293 static bool
294 44 ptkx_skip_digits(const char** p, const char* end, int radix)
295 {
296 44 ▶ if (*p == end || !ptkx_is_digit(**p, radix)) return FALSE;
Branch: 2 branches
BranchTrueFalseTotal
if (*p == end || !ptkx_is_digit(**p, radix)) return FALSE; 41 3 44
if (*p == end || !ptkx_is_digit(**p, radix)) return FALSE; 6 35 41
297 108 ▶ for (++*p; *p < end && (**p == '_' || ptkx_is_digit(**p, radix)); ++*p)
Branch: 3 branches
BranchTrueFalseTotal
for (++*p; *p < end && (**p == '_' || ptkx_is_digit(**p, radix)); ++*p) 88 20 108
for (++*p; *p < end && (**p == '_' || ptkx_is_digit(**p, radix)); ++*p) 3 85 88
for (++*p; *p < end && (**p == '_' || ptkx_is_digit(**p, radix)); ++*p) 70 15 85
298 ;
299 35 return TRUE;
300 }
301  
302 static bool
303 43 ptkx_is_keyword_number(const char* text, STRLEN len)
304 {
305 13 ▶ return memEQs(text, len, "#inf")
Branch: 1 branch
BranchTrueFalseTotal
return memEQs(text, len, "#inf") 11 2 13
306 41 ▶ || memEQs(text, len, "#-inf")
Branch: 2 branches
BranchTrueFalseTotal
|| memEQs(text, len, "#-inf") 11 30 41
|| memEQs(text, len, "#-inf") 9 2 11
307 86 ▶ || memEQs(text, len, "#nan");
Branch: 3 branches
BranchTrueFalseTotal
|| memEQs(text, len, "#nan"); 13 30 43
|| memEQs(text, len, "#nan"); 11 28 39
|| memEQs(text, len, "#nan"); 4 7 11
308 }
309  
310 /* True if text is a KDL number literal: an optional sign followed by 0x, 0o
311 * or 0b digits or by a decimal with optional fraction and exponent, digits
312 * separated by '_' after the first; or one of #inf, #-inf and #nan. */
313 static bool
314 42 ptkx_is_number_literal(const char* text, STRLEN len)
315 {
316 42 const char* p = text;
317 42 const char* end = text + len;
318  
319 42 ▶ if (ptkx_is_keyword_number(text, len)) return TRUE;
Branch: 1 branch
BranchTrueFalseTotal
if (ptkx_is_keyword_number(text, len)) return TRUE; 7 35 42
320  
321 35 ▶ if (p < end && (*p == '+' || *p == '-')) ++p;
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == '+' || *p == '-')) ++p; 33 2 35
if (p < end && (*p == '+' || *p == '-')) ++p; 31 2 33
if (p < end && (*p == '+' || *p == '-')) ++p; 5 26 31
322 35 ▶ if (end - p > 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'o' || p[1] == 'b')) {
Branch: 5 branches
BranchTrueFalseTotal
if (end - p > 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'o' || p[1] == 'b')) { 27 8 35
if (end - p > 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'o' || p[1] == 'b')) { 10 17 27
if (end - p > 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'o' || p[1] == 'b')) { 4 6 10
if (end - p > 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'o' || p[1] == 'b')) { 1 3 4
if (end - p > 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'o' || p[1] == 'b')) { 1 0 1
323 10 ▶ int radix = p[1] == 'x' ? 16 : p[1] == 'o' ? 8 : 2;
Branch: 2 branches
BranchTrueFalseTotal
int radix = p[1] == 'x' ? 16 : p[1] == 'o' ? 8 : 2; 4 6 10
int radix = p[1] == 'x' ? 16 : p[1] == 'o' ? 8 : 2; 3 1 4
324 10 p += 2;
325 10 ▶ return ptkx_skip_digits(&p, end, radix) && p == end;
Branch: 2 branches
BranchTrueFalseTotal
return ptkx_skip_digits(&p, end, radix) && p == end; 10 0 10
return ptkx_skip_digits(&p, end, radix) && p == end; 10 0 10
326 }
327  
328 25 ▶ if (!ptkx_skip_digits(&p, end, 10)) return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_skip_digits(&p, end, 10)) return FALSE; 7 18 25
329 18 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 13 5 18
if (p < end && *p == '.') { 4 9 13
330 4 ++p;
331 4 ▶ if (!ptkx_skip_digits(&p, end, 10)) return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_skip_digits(&p, end, 10)) return FALSE; 1 3 4
332 }
333 17 ▶ if (p < end && (*p == 'e' || *p == 'E')) {
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == 'e' || *p == 'E')) { 11 6 17
if (p < end && (*p == 'e' || *p == 'E')) { 8 3 11
if (p < end && (*p == 'e' || *p == 'E')) { 2 6 8
334 5 ++p;
335 5 ▶ if (p < end && (*p == '+' || *p == '-')) ++p;
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == '+' || *p == '-')) ++p; 4 1 5
if (p < end && (*p == '+' || *p == '-')) ++p; 4 0 4
if (p < end && (*p == '+' || *p == '-')) ++p; 2 2 4
336 5 ▶ if (!ptkx_skip_digits(&p, end, 10)) return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_skip_digits(&p, end, 10)) return FALSE; 1 4 5
337 }
338 16 return p == end;
339 }
340  
341 /* True if text is an optional sign followed by decimal digits only. */
342 static bool
343 8 ptkx_is_decimal_integer(const char* text, STRLEN len)
344 {
345 8 const char* end = text + len;
346 8 ▶ if (text < end && (*text == '+' || *text == '-')) ++text;
Branch: 3 branches
BranchTrueFalseTotal
if (text < end && (*text == '+' || *text == '-')) ++text; 8 0 8
if (text < end && (*text == '+' || *text == '-')) ++text; 8 0 8
if (text < end && (*text == '+' || *text == '-')) ++text; 1 7 8
347 8 ▶ if (text == end) return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if (text == end) return FALSE; 0 8 8
348 52 ▶ for (; text < end; ++text)
Branch: 1 branch
BranchTrueFalseTotal
for (; text < end; ++text) 50 2 52
349 50 ▶ if (!isDIGIT(*text)) return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if (!isDIGIT(*text)) return FALSE; 6 44 50
350 2 return TRUE;
351 }
352  
353 /* Writes the integer held by sv as decimal digits into buf
354 * (PTKX_NUMBER_BUFSIZE bytes): the IV or UV of an integer scalar, or the
355 * value of a string of decimal digits with an optional sign that lies in
356 * the native range IV_MIN .. UV_MAX. Returns FALSE for anything else.
357 * sv must not have get-magic. */
358 static bool
359 34 ptkx_format_integer(pTHX_ SV* sv, char* buf, STRLEN* len)
360 {
361 const char* text;
362 STRLEN text_len;
363 UV magnitude;
364 int flags;
365 bool negative;
366  
367 34 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 26 8 34
368 52 *len = SvIsUV(sv)
369 2 ? (STRLEN) my_snprintf(buf, PTKX_NUMBER_BUFSIZE, "%" UVuf, SvUVX(sv))
370 28 ▶ : (STRLEN) my_snprintf(buf, PTKX_NUMBER_BUFSIZE, "%" IVdf, SvIVX(sv));
Branch: 1 branch
BranchTrueFalseTotal
: (STRLEN) my_snprintf(buf, PTKX_NUMBER_BUFSIZE, "%" IVdf, SvIVX(sv)); 2 24 26
371 26 return TRUE;
372 }
373  
374 8 text = SvPV_nomg(sv, text_len);
375 8 ▶ if (!ptkx_is_decimal_integer(text, text_len)) return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_is_decimal_integer(text, text_len)) return FALSE; 6 2 8
376  
377 2 flags = grok_number(text, text_len, &magnitude);
378 2 ▶ if ((flags & ~IS_NUMBER_NEG) != IS_NUMBER_IN_UV) return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & ~IS_NUMBER_NEG) != IS_NUMBER_IN_UV) return FALSE; 1 1 2
379 1 ▶ negative = (flags & IS_NUMBER_NEG) && magnitude > 0;
Branch: 2 branches
BranchTrueFalseTotal
negative = (flags & IS_NUMBER_NEG) && magnitude > 0; 1 0 1
negative = (flags & IS_NUMBER_NEG) && magnitude > 0; 1 0 1
380 1 ▶ if (negative && magnitude > (UV) IV_MAX + 1) return FALSE;
Branch: 2 branches
BranchTrueFalseTotal
if (negative && magnitude > (UV) IV_MAX + 1) return FALSE; 1 0 1
if (negative && magnitude > (UV) IV_MAX + 1) return FALSE; 1 0 1
381  
382 0 ▶ *len = (STRLEN) my_snprintf(buf, PTKX_NUMBER_BUFSIZE,
Branch: 1 branch
BranchTrueFalseTotal
*len = (STRLEN) my_snprintf(buf, PTKX_NUMBER_BUFSIZE, 0 0 0
383 negative ? "-%" UVuf : "%" UVuf, magnitude);
384 0 return TRUE;
385 }
386  
387 /* True if the string value of sv (POK) is exactly Perl's own rendering of
388 * its numeric value (IOK or NOK). */
389 static bool
390 7 ptkx_has_canonical_number_text(pTHX_ SV* sv)
391 {
392 char buf[PTKX_NUMBER_BUFSIZE];
393 7 const char* canonical = buf;
394 STRLEN canonical_len;
395  
396 7 ▶ if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(sv)) { 6 1 7
397 6 canonical_len = SvIsUV(sv)
398 0 ? (STRLEN) my_snprintf(buf, sizeof buf, "%" UVuf, SvUVX(sv))
399 6 ▶ : (STRLEN) my_snprintf(buf, sizeof buf, "%" IVdf, SvIVX(sv));
Branch: 1 branch
BranchTrueFalseTotal
: (STRLEN) my_snprintf(buf, sizeof buf, "%" IVdf, SvIVX(sv)); 0 6 6
400 }
401 else {
402 1 SV* rendering = sv_2mortal(newSVnv(SvNVX(sv)));
403 1 canonical = SvPV(rendering, canonical_len);
404 }
405 7 ▶ return canonical_len == SvCUR(sv) && memEQ(canonical, SvPVX_const(sv), canonical_len);
Branch: 2 branches
BranchTrueFalseTotal
return canonical_len == SvCUR(sv) && memEQ(canonical, SvPVX_const(sv), canonical_len); 1 6 7
return canonical_len == SvCUR(sv) && memEQ(canonical, SvPVX_const(sv), canonical_len); 1 0 1
406 }
407  
408 /* How a plain scalar is written by data mode: a number when it has a
409 * numeric value and its string value, if any, is exactly Perl's rendering
410 * of that number; otherwise a string. So "42" used in arithmetic is the
411 * integer 42, while "007", "1.50" and dualvars stay strings. sv must not
412 * have get-magic. */
413 static ptkx_number_kind
414 20127 ptkx_scalar_number_kind(pTHX_ SV* sv)
415 {
416 20127 ▶ if (!SvIOK(sv) && !SvNOK(sv)) return PTKX_NOT_A_NUMBER;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvIOK(sv) && !SvNOK(sv)) return PTKX_NOT_A_NUMBER; 20094 33 20127
if (!SvIOK(sv) && !SvNOK(sv)) return PTKX_NOT_A_NUMBER; 9506 10588 20094
417 10621 ▶ if (SvPOK(sv) && !ptkx_has_canonical_number_text(aTHX_ sv)) return PTKX_NOT_A_NUMBER;
Branch: 2 branches
BranchTrueFalseTotal
if (SvPOK(sv) && !ptkx_has_canonical_number_text(aTHX_ sv)) return PTKX_NOT_A_NUMBER; 7 10614 10621
if (SvPOK(sv) && !ptkx_has_canonical_number_text(aTHX_ sv)) return PTKX_NOT_A_NUMBER; 6 1 7
418 10615 ▶ return SvIOK(sv) ? PTKX_INTEGER : PTKX_FLOAT;
Branch: 1 branch
BranchTrueFalseTotal
return SvIOK(sv) ? PTKX_INTEGER : PTKX_FLOAT; 28 10587 10615
419 }
420  
421 /* ------------------------------------------------------------------------- */
422 /* Object handles: the C struct hangs off the referent as ext magic and is */
423 /* freed with it. Forged or reblessed scalars have no such magic. */
424 /* ------------------------------------------------------------------------- */
425  
426 static void ptkx_parser_free(pTHX_ ptkx_parser* p);
427  
428 static int
429 163 ptkx_parser_mg_free(pTHX_ SV* sv, MAGIC* mg)
430 {
431 PERL_UNUSED_ARG(sv);
432 163 ▶ if (mg->mg_ptr) ptkx_parser_free(aTHX_ (ptkx_parser*) mg->mg_ptr);
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_ptr) ptkx_parser_free(aTHX_ (ptkx_parser*) mg->mg_ptr); 163 0 163
433 163 mg->mg_ptr = NULL;
434 163 return 0;
435 }
436  
437 static int
438 184 ptkx_emitter_mg_free(pTHX_ SV* sv, MAGIC* mg)
439 {
440 184 ptkx_emitter* e = (ptkx_emitter*) mg->mg_ptr;
441 PERL_UNUSED_CONTEXT;
442 PERL_UNUSED_ARG(sv);
443 184 ▶ if (e) {
Branch: 1 branch
BranchTrueFalseTotal
if (e) { 184 0 184
444 184 kdl_destroy_emitter(e->emitter);
445 184 Safefree(e);
446 }
447 184 mg->mg_ptr = NULL;
448 184 return 0;
449 }
450  
451 static MGVTBL ptkx_parser_vtbl = { NULL, NULL, NULL, NULL, ptkx_parser_mg_free, NULL, NULL, NULL };
452 static MGVTBL ptkx_emitter_vtbl = { NULL, NULL, NULL, NULL, ptkx_emitter_mg_free, NULL, NULL, NULL };
453  
454 /* The stash of klass, a class name or object that must be class_name or a
455 * subclass of it. */
456 static HV*
457 352 ptkx_class_stash(pTHX_ SV* klass, const char* class_name)
458 {
459 352 ▶ if (!SvOK(klass) || !sv_derived_from(klass, class_name))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(klass) || !sv_derived_from(klass, class_name)) 352 0 352
if (!SvOK(klass) || !sv_derived_from(klass, class_name)) 1 351 352
460 1 croak("%s: '%" SVf "' is not %s or a subclass of it",
461 class_name, SVfARG(klass), class_name);
462 351 ▶ return SvROK(klass) ? SvSTASH(SvRV(klass)) : gv_stashsv(klass, GV_ADD);
Branch: 1 branch
BranchTrueFalseTotal
return SvROK(klass) ? SvSTASH(SvRV(klass)) : gv_stashsv(klass, GV_ADD); 0 351 351
463 }
464  
465 /* A new object blessed into stash whose referent carries handle as ext
466 * magic with the given vtable. */
467 static SV*
468 347 ptkx_wrap(pTHX_ HV* stash, void* handle, MGVTBL* vtbl)
469 {
470 347 SV* body = newSV(0);
471 347 SV* object = newRV_noinc(body);
472 347 sv_magicext(body, NULL, PERL_MAGIC_ext, vtbl, (const char*) handle, 0);
473 347 return sv_bless(object, stash);
474 }
475  
476 /* The C handle of an object made by ptkx_wrap with vtbl. */
477 static void*
478 34859 ptkx_unwrap(pTHX_ SV* object, MGVTBL* vtbl, const char* class_name)
479 {
480 MAGIC* mg;
481 34859 ▶ SvGETMAGIC(object);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(object); 0 34859 34859
SvGETMAGIC(object); 0 0 0
482 34859 ▶ mg = SvROK(object) ? mg_findext(SvRV(object), PERL_MAGIC_ext, vtbl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
mg = SvROK(object) ? mg_findext(SvRV(object), PERL_MAGIC_ext, vtbl) : NULL; 34859 0 34859
483 34859 ▶ if (!mg || !mg->mg_ptr) croak("not a valid %s object", class_name);
Branch: 2 branches
BranchTrueFalseTotal
if (!mg || !mg->mg_ptr) croak("not a valid %s object", class_name); 34856 3 34859
if (!mg || !mg->mg_ptr) croak("not a valid %s object", class_name); 0 34856 34856
484 34856 return mg->mg_ptr;
485 }
486  
487 /* ------------------------------------------------------------------------- */
488 /* Parser lifecycle */
489 /* ------------------------------------------------------------------------- */
490  
491 static void
492 167 ptkx_check_parse_options(pTHX_ IV options, IV max_depth)
493 {
494 167 IV version = options & (IV) KDL_DETECT_VERSION;
495  
496 167 ▶ if ((options & ~(IV) (KDL_DETECT_VERSION | KDL_EMIT_COMMENTS)) != 0
Branch: 1 branch
BranchTrueFalseTotal
if ((options & ~(IV) (KDL_DETECT_VERSION | KDL_EMIT_COMMENTS)) != 0 167 0 167
497 167 ▶ || (version != (IV) KDL_READ_VERSION_1
Branch: 1 branch
BranchTrueFalseTotal
|| (version != (IV) KDL_READ_VERSION_1 149 18 167
498 149 ▶ && version != (IV) KDL_READ_VERSION_2
Branch: 1 branch
BranchTrueFalseTotal
&& version != (IV) KDL_READ_VERSION_2 115 34 149
499 115 ▶ && version != (IV) KDL_DETECT_VERSION))
Branch: 1 branch
BranchTrueFalseTotal
&& version != (IV) KDL_DETECT_VERSION)) 0 115 115
500 0 croak(PTKX_PARSER_CLASS ": invalid parse options 0x%" UVxf, (UV) options);
501 167 ▶ if (max_depth < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (max_depth < 0) 0 167 167
502 0 croak(PTKX_PARSER_CLASS ": max_depth must be a non-negative integer");
503 167 }
504  
505 static ptkx_parser*
506 166 ptkx_parser_new(pTHX_ IV max_depth)
507 {
508 ptkx_parser* p;
509 166 Newxz(p, 1, ptkx_parser);
510 166 p->value_stash = (HV*) SvREFCNT_inc_simple_NN((SV*) gv_stashpvs(PTKX_VALUE_CLASS, GV_ADD));
511 166 p->max_depth = max_depth;
512 166 p->state = PTKX_ACTIVE;
513 166 return p;
514 }
515  
516 /* Frees what a finished or failed parser no longer needs: the ckdl parser,
517 * the source and any pending input. The recorded error stays. Never called
518 * while ckdl is running. */
519 static void
520 323 ptkx_parser_release(pTHX_ ptkx_parser* p)
521 {
522 323 ▶ if (p->parser) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->parser) { 166 157 323
523 166 kdl_parser* parser = p->parser;
524 166 p->parser = NULL;
525 166 kdl_destroy_parser(parser);
526 }
527 323 ptkx_clear_sv(aTHX_ &p->read_cb);
528 323 ptkx_clear_sv(aTHX_ &p->source_copy);
529 323 ptkx_clear_sv(aTHX_ &p->pending_chunk);
530 323 }
531  
532 static void
533 166 ptkx_parser_free(pTHX_ ptkx_parser* p)
534 {
535 166 ptkx_parser_release(aTHX_ p);
536 166 ptkx_clear_sv(aTHX_ &p->error);
537 166 SvREFCNT_dec((SV*) p->value_stash);
538 166 Safefree(p);
539 166 }
540  
541 static void
542 126 ptkx_parser_finish(pTHX_ ptkx_parser* p)
543 {
544 126 p->state = PTKX_FINISHED;
545 126 ptkx_parser_release(aTHX_ p);
546 126 }
547  
548 /* Puts the parser into the FAILED state and raises its error; every later
549 * call raises the same error again. A non-NULL error (ownership passes to
550 * the parser) replaces the recorded one. */
551 static void ptkx_parser_fail(pTHX_ ptkx_parser* p, SV* error) __attribute__noreturn__;
552  
553 static void
554 31 ptkx_parser_fail(pTHX_ ptkx_parser* p, SV* error)
555 {
556 31 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 13 18 31
557 13 SV* previous = p->error;
558 13 p->error = error;
559 13 SvREFCNT_dec(previous);
560 }
561 31 p->state = PTKX_FAILED;
562 31 ptkx_parser_release(aTHX_ p);
563 31 croak_sv(p->error);
564 }
565  
566 /* Records an error from code that runs inside ckdl and must not croak. The
567 * first error wins; ownership of error passes to the parser. */
568 static void
569 21 ptkx_parser_record_error(pTHX_ ptkx_parser* p, SV* error)
570 {
571 21 ▶ if (p->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->error) { 0 21 21
572 0 SvREFCNT_dec(error);
573 0 return;
574 }
575 21 p->error = error;
576 }
577  
578 /* ------------------------------------------------------------------------- */
579 /* Callback sources */
580 /* ------------------------------------------------------------------------- */
581  
582 static void
583 17 ptkx_source_ended(pTHX_ ptkx_parser* p)
584 {
585 17 p->source_exhausted = TRUE;
586 17 ▶ if (p->utf8.continuation_bytes > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p->utf8.continuation_bytes > 0) 1 16 17
587 1 ptkx_parser_record_error(aTHX_ p, newSVpvs("KDL parse error: input is not valid UTF-8"));
588 17 }
589  
590 /* Takes a chunk returned by the source callback into p->pending_chunk.
591 * Chunks are UTF-8 bytes: the buffer of a scalar is used as it is, which for
592 * a character string (UTF-8 flag on) is its UTF-8 encoding. Returns TRUE if
593 * a non-empty chunk is pending. */
594 static bool
595 177 ptkx_accept_chunk(pTHX_ ptkx_parser* p, SV* chunk)
596 {
597 const char* bytes;
598 STRLEN len;
599  
600 177 ▶ if (SvROK(chunk) || SvGMAGICAL(chunk)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(chunk) || SvGMAGICAL(chunk)) { 176 1 177
if (SvROK(chunk) || SvGMAGICAL(chunk)) { 0 176 176
601 1 ptkx_parser_record_error(aTHX_ p, newSVpvs(PTKX_PARSER_CLASS
602 ": the source callback must return a string or undef, not a reference"));
603 1 return FALSE;
604 }
605 176 ▶ if (!SvOK(chunk)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(chunk)) { 3 173 176
606 3 ptkx_source_ended(aTHX_ p);
607 3 return FALSE;
608 }
609  
610 173 bytes = SvPV_nomg(chunk, len);
611 173 ▶ if (len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { 14 159 173
612 14 ptkx_source_ended(aTHX_ p);
613 14 return FALSE;
614 }
615 159 ▶ if (!ptkx_utf8_feed(&p->utf8, (const U8*) bytes, len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_utf8_feed(&p->utf8, (const U8*) bytes, len)) { 15 144 159
616 15 ptkx_parser_record_error(aTHX_ p, newSVpvs("KDL parse error: input is not valid UTF-8"));
617 15 return FALSE;
618 }
619  
620 144 ▶ if (!p->pending_chunk) p->pending_chunk = newSV(len);
Branch: 1 branch
BranchTrueFalseTotal
if (!p->pending_chunk) p->pending_chunk = newSV(len); 34 110 144
621 144 sv_setpvn(p->pending_chunk, bytes, len);
622 144 p->pending_offset = 0;
623 144 return TRUE;
624 }
625  
626 /* Calls the source callback once. Runs inside ckdl, so it never croaks: an
627 * exception from the callback is recorded in p->error and raised by the
628 * XSUB after ckdl has returned. Returns TRUE if a non-empty chunk is
629 * pending. */
630 static bool
631 181 ptkx_fetch_chunk(pTHX_ ptkx_parser* p, size_t wanted)
632 {
633 181 dSP;
634 SV* chunk;
635 SV* error;
636 int count;
637 181 bool fetched = FALSE;
638  
639 181 ENTER;
640 181 SAVETMPS;
641 181 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 181 181
642 181 ▶ mXPUSHu((UV) wanted);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHu((UV) wanted); 0 181 181
643 181 PUTBACK;
644  
645 181 p->in_callback = TRUE;
646 181 count = call_sv(p->read_cb, G_SCALAR | G_EVAL);
647 181 p->in_callback = FALSE;
648  
649 181 SPAGAIN;
650 181 ▶ chunk = count > 0 ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
chunk = count > 0 ? POPs : &PL_sv_undef; 181 0 181
651 181 PUTBACK;
652  
653 181 ▶ error = ERRSV;
Branch: 1 branch
BranchTrueFalseTotal
error = ERRSV; 181 0 181
654 181 ▶ if (SvROK(error) || SvTRUE_nomg(error))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(error) || SvTRUE_nomg(error)) 180 1 181
if (SvROK(error) || SvTRUE_nomg(error)) 3 177 180
655 4 ptkx_parser_record_error(aTHX_ p, newSVsv(error));
656 else
657 177 fetched = ptkx_accept_chunk(aTHX_ p, chunk);
658  
659 181 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 181 0 181
660 181 LEAVE;
661 181 return fetched;
662 }
663  
664 /* kdl_read_func for callback sources: hands ckdl up to bufsize bytes of the
665 * pending chunk and fetches the next chunk once it is used up. Returns 0 at
666 * end of input and after an error. */
667 static size_t
668 185 ptkx_read_thunk(void* user_data, char* buf, size_t bufsize)
669 {
670 dTHX;
671 185 ptkx_parser* p = (ptkx_parser*) user_data;
672 STRLEN available;
673 size_t delivered;
674  
675 185 ▶ if (bufsize == 0 || !p->read_cb || p->error || p->source_exhausted) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (bufsize == 0 || !p->read_cb || p->error || p->source_exhausted) return 0; 185 0 185
if (bufsize == 0 || !p->read_cb || p->error || p->source_exhausted) return 0; 185 0 185
if (bufsize == 0 || !p->read_cb || p->error || p->source_exhausted) return 0; 185 0 185
if (bufsize == 0 || !p->read_cb || p->error || p->source_exhausted) return 0; 1 184 185
676  
677 184 ▶ if (!p->pending_chunk || p->pending_offset >= SvCUR(p->pending_chunk)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!p->pending_chunk || p->pending_offset >= SvCUR(p->pending_chunk)) { 146 38 184
if (!p->pending_chunk || p->pending_offset >= SvCUR(p->pending_chunk)) { 143 3 146
678 181 ▶ if (!ptkx_fetch_chunk(aTHX_ p, bufsize)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_fetch_chunk(aTHX_ p, bufsize)) return 0; 37 144 181
679 }
680  
681 147 available = SvCUR(p->pending_chunk) - p->pending_offset;
682 147 delivered = available < bufsize ? available : bufsize;
683 147 Copy(SvPVX_const(p->pending_chunk) + p->pending_offset, buf, delivered, char);
684 147 p->pending_offset += delivered;
685 147 return delivered;
686 }
687  
688 /* ------------------------------------------------------------------------- */
689 /* Parser events */
690 /* ------------------------------------------------------------------------- */
691  
692 static SV*
693 8 ptkx_parse_error(pTHX_ const kdl_event_data* ev)
694 {
695 8 const kdl_str* message = &ev->value.string;
696 8 ▶ if (ev->value.type != KDL_TYPE_STRING || message->data == NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (ev->value.type != KDL_TYPE_STRING || message->data == NULL) 8 0 8
if (ev->value.type != KDL_TYPE_STRING || message->data == NULL) 0 8 8
697 0 return newSVpvs("KDL parse error");
698 8 return newSVpvf("KDL parse error: %.*s", (int) message->len, message->data);
699 }
700  
701 /* Counts open nodes and fails the parser when they exceed max_depth. */
702 static void
703 23709 ptkx_track_depth(pTHX_ ptkx_parser* p, kdl_event base)
704 {
705 23709 ▶ if (base == KDL_EVENT_END_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if (base == KDL_EVENT_END_NODE) { 5208 18501 23709
706 5208 --p->depth;
707 5208 return;
708 }
709 18501 ▶ if (base != KDL_EVENT_START_NODE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (base != KDL_EVENT_START_NODE) return; 12742 5759 18501
710 5759 ▶ if (++p->depth > p->max_depth && p->max_depth > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (++p->depth > p->max_depth && p->max_depth > 0) 2003 3756 5759
if (++p->depth > p->max_depth && p->max_depth > 0) 3 2000 2003
711 3 ptkx_parser_fail(aTHX_ p, newSVpvf(
712 "KDL parse error: nesting depth exceeds max_depth (%" IVdf ")", p->max_depth));
713 }
714  
715 /* The first text field of ev that is not valid Unicode, or NULL. ckdl can
716 * produce surrogates and code points above U+10FFFF from \u{...} escapes. */
717 static const char*
718 23706 ptkx_invalid_text_field(const kdl_event_data* ev, kdl_event base)
719 {
720 23706 const kdl_value* value = &ev->value;
721  
722 23706 ▶ if (!ptkx_is_text(ev->name))
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_is_text(ev->name)) 0 23706 23706
723 0 ▶ return base == KDL_EVENT_PROPERTY ? "property key" : "node name";
Branch: 1 branch
BranchTrueFalseTotal
return base == KDL_EVENT_PROPERTY ? "property key" : "node name"; 0 0 0
724 23706 ▶ if (!ptkx_is_text(value->type_annotation)) return "type annotation";
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_is_text(value->type_annotation)) return "type annotation"; 0 23706 23706
725 23706 ▶ if (value->type == KDL_TYPE_STRING && !ptkx_is_text(value->string))
Branch: 2 branches
BranchTrueFalseTotal
if (value->type == KDL_TYPE_STRING && !ptkx_is_text(value->string)) 125 23581 23706
if (value->type == KDL_TYPE_STRING && !ptkx_is_text(value->string)) 2 123 125
726 2 ▶ return ev->event == KDL_EVENT_COMMENT ? "comment" : "string";
Branch: 1 branch
BranchTrueFalseTotal
return ev->event == KDL_EVENT_COMMENT ? "comment" : "string"; 0 2 2
727 23704 return NULL;
728 }
729  
730 static const char*
731 23709 ptkx_event_name(const kdl_event_data* ev, kdl_event base)
732 {
733 23709 ▶ if (ev->event == KDL_EVENT_COMMENT) return "comment";
Branch: 1 branch
BranchTrueFalseTotal
if (ev->event == KDL_EVENT_COMMENT) return "comment"; 4 23705 23709
734 23705 switch (base) {
735 5759 case KDL_EVENT_START_NODE: return "start_node";
736 5208 case KDL_EVENT_END_NODE: return "end_node";
737 12690 case KDL_EVENT_ARGUMENT: return "argument";
738 48 case KDL_EVENT_PROPERTY: return "property";
739 0 default: return NULL;
740 }
741 }
742  
743 /* A character string SV holding validated UTF-8 text. */
744 static SV*
745 5996 ptkx_text_sv(pTHX_ kdl_str text)
746 {
747 5996 return newSVpvn_flags(text.data, text.len, SVf_UTF8);
748 }
749  
750 /* The Perl value of a parsed number and its kind. Integer text that ckdl
751 * leaves string-encoded but that fits IV or UV becomes an integer. */
752 static SV*
753 12610 ptkx_number_sv(pTHX_ const kdl_number* number, const char** kind)
754 {
755 UV magnitude;
756 int flags;
757  
758 12610 switch (number->type) {
759 2575 case KDL_NUMBER_TYPE_INTEGER:
760 2575 *kind = "integer";
761 2575 return newSViv((IV) number->integer);
762 568 case KDL_NUMBER_TYPE_FLOATING_POINT:
763 568 *kind = "float";
764 568 return newSVnv(ptkx_nv_of_literal(aTHX_ number->floating_point));
765 9467 case KDL_NUMBER_TYPE_STRING_ENCODED:
766 9467 break;
767 }
768  
769 9467 flags = grok_number(number->string.data, number->string.len, &magnitude);
770 9467 ▶ if (flags == IS_NUMBER_IN_UV) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags == IS_NUMBER_IN_UV) { 6 9461 9467
771 6 *kind = "integer";
772 6 ▶ return magnitude <= (UV) IV_MAX ? newSViv((IV) magnitude) : newSVuv(magnitude);
Branch: 1 branch
BranchTrueFalseTotal
return magnitude <= (UV) IV_MAX ? newSViv((IV) magnitude) : newSVuv(magnitude); 3 3 6
773 }
774 9461 ▶ if (flags == (IS_NUMBER_IN_UV | IS_NUMBER_NEG) && magnitude <= (UV) IV_MAX + 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (flags == (IS_NUMBER_IN_UV | IS_NUMBER_NEG) && magnitude <= (UV) IV_MAX + 1) { 3 9458 9461
if (flags == (IS_NUMBER_IN_UV | IS_NUMBER_NEG) && magnitude <= (UV) IV_MAX + 1) { 2 1 3
775 2 *kind = "integer";
776 2 ▶ return newSViv(magnitude == (UV) IV_MAX + 1 ? IV_MIN : -(IV) magnitude);
Branch: 1 branch
BranchTrueFalseTotal
return newSViv(magnitude == (UV) IV_MAX + 1 ? IV_MIN : -(IV) magnitude); 1 1 2
777 }
778 9459 *kind = "string";
779 9459 return newSVpvn(number->string.data, number->string.len);
780 }
781  
782 /* A Text::KDL::XS::Value object. Every slot holds a fresh SV, so callers may
783 * modify the hash. */
784 static SV*
785 12736 ptkx_value_sv(pTHX_ HV* stash, const kdl_value* value)
786 {
787 12736 HV* fields = newHV();
788 12736 SV* object = newRV_noinc((SV*) fields);
789 12736 SV* kind = NULL;
790 SV* content;
791 const char* type;
792  
793 12736 switch (value->type) {
794 3 case KDL_TYPE_NULL:
795 3 type = "null";
796 3 content = newSV(0);
797 3 break;
798 4 case KDL_TYPE_BOOLEAN:
799 4 type = "bool";
800 4 content = newSViv(value->boolean ? 1 : 0);
801 4 break;
802 119 case KDL_TYPE_STRING:
803 119 type = "string";
804 119 content = ptkx_text_sv(aTHX_ value->string);
805 119 break;
806 12610 case KDL_TYPE_NUMBER:
807 default: {
808 12610 const char* kind_name = NULL;
809 12610 type = "number";
810 12610 content = ptkx_number_sv(aTHX_ &value->number, &kind_name);
811 12610 kind = newSVpv(kind_name, 0);
812 12610 break;
813 }
814 }
815  
816 12736 (void) hv_stores(fields, "type", newSVpv(type, 0));
817 12736 ▶ (void) hv_stores(fields, "kind", kind ? kind : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void) hv_stores(fields, "kind", kind ? kind : newSV(0)); 126 12610 12736
818 12736 (void) hv_stores(fields, "value", content);
819 12736 ▶ (void) hv_stores(fields, "type_annotation", value->type_annotation.data
Branch: 1 branch
BranchTrueFalseTotal
(void) hv_stores(fields, "type_annotation", value->type_annotation.data 35 12701 12736
820 ? ptkx_text_sv(aTHX_ value->type_annotation)
821 : newSV(0));
822 12736 return sv_bless(object, stash);
823 }
824  
825 /* The next event of an active parser as a hash reference, or NULL at the
826 * end of input. Failures put the parser into the FAILED state and croak. */
827 static SV*
828 23861 ptkx_next_event(pTHX_ ptkx_parser* p)
829 {
830 23861 const kdl_event_data* ev = kdl_parser_next_event(p->parser);
831 kdl_event base;
832 const char* name;
833 const char* invalid_field;
834 HV* fields;
835  
836 23861 ▶ if (p->error) ptkx_parser_fail(aTHX_ p, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (p->error) ptkx_parser_fail(aTHX_ p, NULL); 18 23843 23861
837 23843 ▶ if (ev == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (ev == NULL) 0 23843 23843
838 0 ptkx_parser_fail(aTHX_ p, newSVpvs(PTKX_PARSER_CLASS ": ckdl returned no event"));
839  
840 23843 ▶ if (ev->event == KDL_EVENT_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (ev->event == KDL_EVENT_EOF) { 126 23717 23843
841 126 ptkx_parser_finish(aTHX_ p);
842 126 return NULL;
843 }
844 23717 ▶ if (ev->event == KDL_EVENT_PARSE_ERROR) ptkx_parser_fail(aTHX_ p, ptkx_parse_error(aTHX_ ev));
Branch: 1 branch
BranchTrueFalseTotal
if (ev->event == KDL_EVENT_PARSE_ERROR) ptkx_parser_fail(aTHX_ p, ptkx_parse_error(aTHX_ ev)); 8 23709 23717
845  
846 23709 base = (kdl_event) (ev->event & ~KDL_EVENT_COMMENT);
847 23709 name = ptkx_event_name(ev, base);
848 23709 ▶ if (name == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (name == NULL) 0 23709 23709
849 0 ptkx_parser_fail(aTHX_ p, newSVpvf(PTKX_PARSER_CLASS ": unexpected ckdl event 0x%x",
850 (unsigned) ev->event));
851 23709 ptkx_track_depth(aTHX_ p, base);
852 23706 invalid_field = ptkx_invalid_text_field(ev, base);
853 23706 ▶ if (invalid_field)
Branch: 1 branch
BranchTrueFalseTotal
if (invalid_field) 2 23704 23706
854 2 ptkx_parser_fail(aTHX_ p, newSVpvf(
855 "KDL parse error: %s contains a surrogate or a code point above U+10FFFF", invalid_field));
856  
857 23704 fields = newHV();
858 23704 (void) hv_stores(fields, "event", newSVpv(name, 0));
859 23704 (void) hv_stores(fields, "commented", newSViv((ev->event & KDL_EVENT_COMMENT) ? 1 : 0));
860  
861 23704 ▶ if (ev->event == KDL_EVENT_COMMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (ev->event == KDL_EVENT_COMMENT) { 4 23700 23704
862 4 (void) hv_stores(fields, "text", ptkx_text_sv(aTHX_ ev->value.string));
863 }
864 23700 ▶ else if (base == KDL_EVENT_START_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (base == KDL_EVENT_START_NODE) { 5756 17944 23700
865 5756 (void) hv_stores(fields, "name", ptkx_text_sv(aTHX_ ev->name));
866 5756 ▶ if (ev->value.type_annotation.data)
Branch: 1 branch
BranchTrueFalseTotal
if (ev->value.type_annotation.data) 34 5722 5756
867 34 (void) hv_stores(fields, "type", ptkx_text_sv(aTHX_ ev->value.type_annotation));
868 }
869 17944 ▶ else if (base == KDL_EVENT_ARGUMENT) {
Branch: 1 branch
BranchTrueFalseTotal
else if (base == KDL_EVENT_ARGUMENT) { 12688 5256 17944
870 12688 (void) hv_stores(fields, "value", ptkx_value_sv(aTHX_ p->value_stash, &ev->value));
871 }
872 5256 ▶ else if (base == KDL_EVENT_PROPERTY) {
Branch: 1 branch
BranchTrueFalseTotal
else if (base == KDL_EVENT_PROPERTY) { 48 5208 5256
873 48 (void) hv_stores(fields, "name", ptkx_text_sv(aTHX_ ev->name));
874 48 (void) hv_stores(fields, "value", ptkx_value_sv(aTHX_ p->value_stash, &ev->value));
875 }
876 23704 return newRV_noinc((SV*) fields);
877 }
878  
879 /* ------------------------------------------------------------------------- */
880 /* Emitter input */
881 /* ------------------------------------------------------------------------- */
882  
883 /* True if a KDL parser reads text, written without quotes, as a keyword or a
884 * number rather than as a string: true, false and null (and in v2 inf, -inf
885 * and nan), or text starting like a number (+1, -1, .5, -.5). ckdl checks
886 * only the characters and would write such text bare. */
887 static bool
888 248 ptkx_is_ambiguous_bare(kdl_version version, const char* text, STRLEN len)
889 {
890 248 const char* p = text;
891 248 const char* end = text + len;
892  
893 248 ▶ if (memEQs(text, len, "true") || memEQs(text, len, "false") || memEQs(text, len, "null"))
Branch: 6 branches
BranchTrueFalseTotal
if (memEQs(text, len, "true") || memEQs(text, len, "false") || memEQs(text, len, "null")) 32 216 248
if (memEQs(text, len, "true") || memEQs(text, len, "false") || memEQs(text, len, "null")) 25 7 32
if (memEQs(text, len, "true") || memEQs(text, len, "false") || memEQs(text, len, "null")) 23 218 241
if (memEQs(text, len, "true") || memEQs(text, len, "false") || memEQs(text, len, "null")) 20 3 23
if (memEQs(text, len, "true") || memEQs(text, len, "false") || memEQs(text, len, "null")) 25 213 238
if (memEQs(text, len, "true") || memEQs(text, len, "false") || memEQs(text, len, "null")) 3 22 25
894 13 return TRUE;
895 235 ▶ if (version == KDL_VERSION_2
Branch: 1 branch
BranchTrueFalseTotal
if (version == KDL_VERSION_2 212 23 235
896 212 ▶ && (memEQs(text, len, "inf") || memEQs(text, len, "-inf") || memEQs(text, len, "nan")))
Branch: 6 branches
BranchTrueFalseTotal
&& (memEQs(text, len, "inf") || memEQs(text, len, "-inf") || memEQs(text, len, "nan"))) 35 177 212
&& (memEQs(text, len, "inf") || memEQs(text, len, "-inf") || memEQs(text, len, "nan"))) 33 2 35
&& (memEQs(text, len, "inf") || memEQs(text, len, "-inf") || memEQs(text, len, "nan"))) 18 192 210
&& (memEQs(text, len, "inf") || memEQs(text, len, "-inf") || memEQs(text, len, "nan"))) 16 2 18
&& (memEQs(text, len, "inf") || memEQs(text, len, "-inf") || memEQs(text, len, "nan"))) 33 175 208
&& (memEQs(text, len, "inf") || memEQs(text, len, "-inf") || memEQs(text, len, "nan"))) 4 29 33
897 8 return TRUE;
898  
899 227 ▶ if (p < end && (*p == '+' || *p == '-')) {
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == '+' || *p == '-')) { 226 1 227
if (p < end && (*p == '+' || *p == '-')) { 220 6 226
if (p < end && (*p == '+' || *p == '-')) { 15 205 220
900 21 ++p;
901 21 ▶ if (p < end && *p == '.') ++p;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') ++p; 16 5 21
if (p < end && *p == '.') ++p; 6 10 16
902 }
903 206 ▶ else if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
else if (p < end && *p == '.') { 205 1 206
else if (p < end && *p == '.') { 3 202 205
904 3 ++p;
905 }
906 else {
907 203 return FALSE;
908 }
909 24 ▶ return p < end && isDIGIT(*p);
Branch: 2 branches
BranchTrueFalseTotal
return p < end && isDIGIT(*p); 19 5 24
return p < end && isDIGIT(*p); 15 4 19
910 }
911  
912 /* Notes when text that the emitter may write bare (a node name, property
913 * key, type annotation, or a v2 string value) would not read back as a
914 * string, so that the caller can emit the document again in quote-all mode. */
915 static void
916 565 ptkx_note_bare_text(ptkx_emitter* e, kdl_str text)
917 {
918 565 ▶ if (e->writes_bare && !e->needs_quoting
Branch: 2 branches
BranchTrueFalseTotal
if (e->writes_bare && !e->needs_quoting 386 179 565
if (e->writes_bare && !e->needs_quoting 248 138 386
919 248 ▶ && ptkx_is_ambiguous_bare(e->version, text.data, text.len))
Branch: 1 branch
BranchTrueFalseTotal
&& ptkx_is_ambiguous_bare(e->version, text.data, text.len)) 36 212 248
920 36 e->needs_quoting = TRUE;
921 565 }
922  
923 /* The UTF-8 text of a string argument for ckdl. Croaks when sv is undef or
924 * holds a surrogate or a code point above U+10FFFF. A byte string is taken
925 * as Latin-1 characters and encoded in a temporary copy. */
926 static kdl_str
927 618 ptkx_text_arg(pTHX_ SV* sv, const char* what)
928 {
929 kdl_str text;
930 STRLEN len;
931  
932 618 sv = ptkx_plain(aTHX_ sv);
933 618 ▶ if (!SvOK(sv)) croak("emit_kdl: %s must be defined", what);
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) croak("emit_kdl: %s must be defined", what); 0 618 618
934  
935 618 text.data = SvPV_nomg(sv, len);
936 618 ▶ if (!SvUTF8(sv) && !ptkx_is_ascii(text.data, len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvUTF8(sv) && !ptkx_is_ascii(text.data, len)) { 592 26 618
if (!SvUTF8(sv) && !ptkx_is_ascii(text.data, len)) { 6 586 592
937 6 SV* encoded = sv_2mortal(newSVpvn(text.data, len));
938 6 sv_utf8_upgrade(encoded);
939 6 text.data = SvPV_nomg(encoded, len);
940 }
941 618 text.len = len;
942  
943 618 ▶ if (!ptkx_is_unicode_utf8(text.data, text.len))
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_is_unicode_utf8(text.data, text.len)) 9 609 618
944 9 croak("emit_kdl: %s contains a surrogate or a code point above U+10FFFF", what);
945 609 return text;
946 }
947  
948 #define PTKX_FIELD(fields, key) ptkx_field(aTHX_ (fields), STR_WITH_LEN(key))
949  
950 /* A field of a value payload hash; undef when missing. */
951 static SV*
952 40877 ptkx_field(pTHX_ HV* fields, const char* key, I32 key_len)
953 {
954 40877 SV** slot = hv_fetch(fields, key, key_len, 0);
955 40877 ▶ return slot ? ptkx_plain(aTHX_ *slot) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return slot ? ptkx_plain(aTHX_ *slot) : &PL_sv_undef; 30744 10133 40877
956 }
957  
958 /* Fills number with the text of a payload number: integers and floats are
959 * formatted into text_buf (PTKX_NUMBER_BUFSIZE bytes), string-encoded
960 * numbers are validated and used verbatim. ckdl always receives
961 * KDL_NUMBER_TYPE_STRING_ENCODED and writes the text as it is. */
962 static void
963 10070 ptkx_number_from_payload(pTHX_ const ptkx_emitter* e, SV* kind, SV* content,
964 char* text_buf, kdl_number* number)
965 {
966 const char* kind_name;
967 STRLEN kind_len;
968 STRLEN len;
969  
970 10070 ▶ if (!SvOK(kind))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(kind)) 1 10069 10070
971 1 croak("emit_kdl: number has no kind (expected integer, float or string)");
972 10069 ▶ if (!SvOK(content))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(content)) 0 10069 10069
973 0 croak("emit_kdl: number value must be defined");
974  
975 10069 kind_name = SvPV_nomg(kind, kind_len);
976 10069 number->type = KDL_NUMBER_TYPE_STRING_ENCODED;
977  
978 10069 ▶ if (memEQs(kind_name, kind_len, "integer")) {
Branch: 2 branches
BranchTrueFalseTotal
if (memEQs(kind_name, kind_len, "integer")) { 27 10042 10069
if (memEQs(kind_name, kind_len, "integer")) { 26 1 27
979 26 ▶ if (!ptkx_format_integer(aTHX_ content, text_buf, &len))
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_format_integer(aTHX_ content, text_buf, &len)) 2 24 26
980 2 croak("emit_kdl: integer value '%" SVf "' is not an integer from %" IVdf " to %" UVuf,
981 SVfARG(content), IV_MIN, UV_MAX);
982 24 number->string.data = text_buf;
983 24 number->string.len = len;
984 24 return;
985 }
986  
987 10043 ▶ if (memEQs(kind_name, kind_len, "float")) {
Branch: 2 branches
BranchTrueFalseTotal
if (memEQs(kind_name, kind_len, "float")) { 10027 16 10043
if (memEQs(kind_name, kind_len, "float")) { 10027 0 10027
988 NV nv;
989 10027 ▶ if (!SvNIOK(content) && !looks_like_number(content))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvNIOK(content) && !looks_like_number(content)) 1 10026 10027
if (!SvNIOK(content) && !looks_like_number(content)) 1 0 1
990 1 croak("emit_kdl: float value '%" SVf "' is not a number", SVfARG(content));
991 10026 nv = SvNV_nomg(content);
992 10026 ▶ if (e->version == KDL_VERSION_1 && (Perl_isnan(nv) || Perl_isinf(nv)))
Branch: 3 branches
BranchTrueFalseTotal
if (e->version == KDL_VERSION_1 && (Perl_isnan(nv) || Perl_isinf(nv))) 1 10025 10026
if (e->version == KDL_VERSION_1 && (Perl_isnan(nv) || Perl_isinf(nv))) 1 0 1
if (e->version == KDL_VERSION_1 && (Perl_isnan(nv) || Perl_isinf(nv))) 1 0 1
993 1 croak(PTKX_V1_NON_FINITE);
994 10025 number->string.data = text_buf;
995 10025 number->string.len = ptkx_format_nv(aTHX_ nv, text_buf);
996 10025 return;
997 }
998  
999 16 ▶ if (memEQs(kind_name, kind_len, "string")) {
Branch: 2 branches
BranchTrueFalseTotal
if (memEQs(kind_name, kind_len, "string")) { 15 1 16
if (memEQs(kind_name, kind_len, "string")) { 15 0 15
1000 15 const char* literal = SvPV_nomg(content, len);
1001 15 ▶ if (!ptkx_is_number_literal(literal, len))
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_is_number_literal(literal, len)) 6 9 15
1002 6 croak("emit_kdl: '%" SVf "' is not a KDL number", SVfARG(content));
1003 9 ▶ if (e->version == KDL_VERSION_1 && ptkx_is_keyword_number(literal, len))
Branch: 2 branches
BranchTrueFalseTotal
if (e->version == KDL_VERSION_1 && ptkx_is_keyword_number(literal, len)) 1 8 9
if (e->version == KDL_VERSION_1 && ptkx_is_keyword_number(literal, len)) 1 0 1
1004 1 croak(PTKX_V1_NON_FINITE);
1005 8 number->string.data = literal;
1006 8 number->string.len = len;
1007 8 return;
1008 }
1009  
1010 1 croak("emit_kdl: unknown number kind '%" SVf "' (expected integer, float or string)",
1011 SVfARG(kind));
1012 }
1013  
1014 /* Fills value from a payload: a hash reference with the keys type, kind,
1015 * value and type_annotation (a Text::KDL::XS::Value is one). Text pointers
1016 * refer to SV buffers and to text_buf, valid until the calling XSUB
1017 * returns. */
1018 static void
1019 10269 ptkx_value_from_payload(pTHX_ ptkx_emitter* e, SV* payload, char* text_buf, kdl_value* value)
1020 {
1021 HV* fields;
1022 SV* type;
1023 SV* annotation;
1024 SV* content;
1025 const char* type_name;
1026 STRLEN type_len;
1027  
1028 10269 payload = ptkx_plain(aTHX_ payload);
1029 10269 ▶ if (!SvROK(payload) || SvTYPE(SvRV(payload)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(payload) || SvTYPE(SvRV(payload)) != SVt_PVHV) 10269 0 10269
if (!SvROK(payload) || SvTYPE(SvRV(payload)) != SVt_PVHV) 0 10269 10269
1030 0 croak("emit_kdl: a value payload must be a HASH reference");
1031  
1032 10269 fields = (HV*) SvRV(payload);
1033 10269 type = PTKX_FIELD(fields, "type");
1034 10269 annotation = PTKX_FIELD(fields, "type_annotation");
1035 10269 content = PTKX_FIELD(fields, "value");
1036  
1037 10269 Zero(value, 1, kdl_value);
1038 10269 ▶ if (SvOK(annotation)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(annotation)) { 64 10205 10269
1039 64 value->type_annotation = ptkx_text_arg(aTHX_ annotation, "type annotation");
1040 64 ptkx_note_bare_text(e, value->type_annotation);
1041 }
1042  
1043 10269 ▶ if (!SvOK(type))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(type)) 0 10269 10269
1044 0 croak("emit_kdl: value has no type (expected null, bool, number or string)");
1045 10269 type_name = SvPV_nomg(type, type_len);
1046  
1047 10269 ▶ if (memEQs(type_name, type_len, "null")) {
Branch: 2 branches
BranchTrueFalseTotal
if (memEQs(type_name, type_len, "null")) { 3 10266 10269
if (memEQs(type_name, type_len, "null")) { 1 2 3
1048 1 value->type = KDL_TYPE_NULL;
1049 }
1050 10268 ▶ else if (memEQs(type_name, type_len, "bool")) {
Branch: 2 branches
BranchTrueFalseTotal
else if (memEQs(type_name, type_len, "bool")) { 2 10266 10268
else if (memEQs(type_name, type_len, "bool")) { 2 0 2
1051 2 value->type = KDL_TYPE_BOOLEAN;
1052 2 value->boolean = SvTRUE_nomg(content) ? true : false;
1053 }
1054 10266 ▶ else if (memEQs(type_name, type_len, "string")) {
Branch: 2 branches
BranchTrueFalseTotal
else if (memEQs(type_name, type_len, "string")) { 10266 0 10266
else if (memEQs(type_name, type_len, "string")) { 196 10070 10266
1055 196 value->type = KDL_TYPE_STRING;
1056 196 value->string = ptkx_text_arg(aTHX_ content, "string value");
1057 193 ▶ if (e->version == KDL_VERSION_2) ptkx_note_bare_text(e, value->string);
Branch: 1 branch
BranchTrueFalseTotal
if (e->version == KDL_VERSION_2) ptkx_note_bare_text(e, value->string); 149 44 193
1058 }
1059 10070 ▶ else if (memEQs(type_name, type_len, "number")) {
Branch: 2 branches
BranchTrueFalseTotal
else if (memEQs(type_name, type_len, "number")) { 10070 0 10070
else if (memEQs(type_name, type_len, "number")) { 10070 0 10070
1060 10070 value->type = KDL_TYPE_NUMBER;
1061 10070 ptkx_number_from_payload(aTHX_ e, PTKX_FIELD(fields, "kind"), content, text_buf, &value->number);
1062 }
1063 else {
1064 0 croak("emit_kdl: unknown value type '%" SVf "' (expected null, bool, number or string)",
1065 SVfARG(type));
1066 }
1067 10253 }
1068  
1069 /* ckdl emitter options from the private constructor's arguments. -1 keeps
1070 * ckdl's default for indent, escape_mode and identifier_mode; version 0
1071 * (detect) writes KDL v2. v2 output always escapes newlines, which KDL v2
1072 * does not allow literally inside a quoted string. */
1073 static kdl_emitter_options
1074 184 ptkx_emitter_options(pTHX_ IV version, IV indent, IV escape_mode, IV identifier_mode)
1075 {
1076 184 kdl_emitter_options options = KDL_DEFAULT_EMITTER_OPTIONS;
1077  
1078 184 switch (version) {
1079 156 case 0:
1080 156 case 2: options.version = KDL_VERSION_2; break;
1081 28 case 1: options.version = KDL_VERSION_1; break;
1082 0 default: croak("emit_kdl: version must be 0 (detect), 1 or 2");
1083 }
1084  
1085 184 ▶ if (indent != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (indent != -1) { 2 182 184
1086 2 ▶ if (indent < 0 || indent > PTKX_MAX_INDENT)
Branch: 2 branches
BranchTrueFalseTotal
if (indent < 0 || indent > PTKX_MAX_INDENT) 2 0 2
if (indent < 0 || indent > PTKX_MAX_INDENT) 0 2 2
1087 0 croak("emit_kdl: indent must be an integer from 0 to %d", PTKX_MAX_INDENT);
1088 2 options.indent = (int) indent;
1089 }
1090 184 ▶ if (escape_mode != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (escape_mode != -1) { 3 181 184
1091 3 ▶ if (escape_mode < 0 || (escape_mode & ~(IV) KDL_ESCAPE_ASCII_MODE) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (escape_mode < 0 || (escape_mode & ~(IV) KDL_ESCAPE_ASCII_MODE) != 0) 3 0 3
if (escape_mode < 0 || (escape_mode & ~(IV) KDL_ESCAPE_ASCII_MODE) != 0) 0 3 3
1092 0 croak("emit_kdl: escape_mode must be a combination of 0x10, 0x20, 0x40 and 0x170");
1093 3 options.escape_mode = (kdl_escape_mode) escape_mode;
1094 }
1095 184 ▶ if (identifier_mode != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (identifier_mode != -1) { 38 146 184
1096 38 ▶ if (identifier_mode < 0 || identifier_mode > 2)
Branch: 2 branches
BranchTrueFalseTotal
if (identifier_mode < 0 || identifier_mode > 2) 38 0 38
if (identifier_mode < 0 || identifier_mode > 2) 0 38 38
1097 0 croak("emit_kdl: identifier_mode must be 0, 1 or 2");
1098 38 options.identifier_mode = (kdl_identifier_emission_mode) identifier_mode;
1099 }
1100  
1101 184 ▶ if (options.version == KDL_VERSION_2)
Branch: 1 branch
BranchTrueFalseTotal
if (options.version == KDL_VERSION_2) 156 28 184
1102 156 options.escape_mode = (kdl_escape_mode) (options.escape_mode | KDL_ESCAPE_NEWLINE);
1103 184 return options;
1104 }
1105  
1106 static void
1107 10659 ptkx_check_emit(pTHX_ bool succeeded, const char* operation)
1108 {
1109 10659 ▶ if (!succeeded) croak("emit_kdl: %s failed", operation);
Branch: 1 branch
BranchTrueFalseTotal
if (!succeeded) croak("emit_kdl: %s failed", operation); 0 10659 10659
1110 10659 }
1111  
1112 /* ------------------------------------------------------------------------- */
1113 /* XS bindings */
1114 /* ------------------------------------------------------------------------- */
1115  
1116 MODULE = Text::KDL::XS PACKAGE = Text::KDL::XS
1117  
1118 PROTOTYPES: DISABLE
1119  
1120 int
1121 _OPT_DETECT()
1122 CODE:
1123 121 ▶ RETVAL = (int) KDL_DETECT_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int) KDL_DETECT_VERSION; 105 16 121
1124 OUTPUT:
1125 RETVAL
1126  
1127 int
1128 _OPT_V1()
1129 CODE:
1130 18 ▶ RETVAL = (int) KDL_READ_VERSION_1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int) KDL_READ_VERSION_1; 16 2 18
1131 OUTPUT:
1132 RETVAL
1133  
1134 int
1135 _OPT_V2()
1136 CODE:
1137 34 ▶ RETVAL = (int) KDL_READ_VERSION_2;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int) KDL_READ_VERSION_2; 29 5 34
1138 OUTPUT:
1139 RETVAL
1140  
1141 int
1142 _OPT_EMIT_COMMENTS()
1143 CODE:
1144 9 ▶ RETVAL = (int) KDL_EMIT_COMMENTS;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int) KDL_EMIT_COMMENTS; 7 2 9
1145 OUTPUT:
1146 RETVAL
1147  
1148 SV*
1149 _float_text(number)
1150 NV number
1151 PREINIT:
1152 char text[PTKX_NUMBER_BUFSIZE];
1153 STRLEN len;
1154 CODE:
1155 8 len = ptkx_format_nv(aTHX_ number, text);
1156 8 RETVAL = newSVpvn(text, len);
1157 OUTPUT:
1158 RETVAL
1159  
1160 bool
1161 _is_number_literal(text)
1162 SV* text
1163 PREINIT:
1164 const char* literal;
1165 STRLEN len;
1166 CODE:
1167 27 text = ptkx_plain(aTHX_ text);
1168 27 RETVAL = FALSE;
1169 27 ▶ if (SvOK(text) && !SvROK(text)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(text) && !SvROK(text)) { 27 0 27
if (SvOK(text) && !SvROK(text)) { 27 0 27
1170 27 literal = SvPV_nomg(text, len);
1171 27 RETVAL = ptkx_is_number_literal(literal, len);
1172 }
1173 OUTPUT:
1174 RETVAL
1175  
1176 bool
1177 _is_native_integer(value)
1178 SV* value
1179 PREINIT:
1180 char digits[PTKX_NUMBER_BUFSIZE];
1181 STRLEN len;
1182 CODE:
1183 8 value = ptkx_plain(aTHX_ value);
1184 8 ▶ RETVAL = SvOK(value) && !SvROK(value) && ptkx_format_integer(aTHX_ value, digits, &len);
Branch: 4 branches
BranchTrueFalseTotal
RETVAL = SvOK(value) && !SvROK(value) && ptkx_format_integer(aTHX_ value, digits, &len); 8 0 8
RETVAL = SvOK(value) && !SvROK(value) && ptkx_format_integer(aTHX_ value, digits, &len); 8 0 8
RETVAL = SvOK(value) && !SvROK(value) && ptkx_format_integer(aTHX_ value, digits, &len); 2 6 8
RETVAL = SvOK(value) && !SvROK(value) && ptkx_format_integer(aTHX_ value, digits, &len); 2 6 8
1185 OUTPUT:
1186 RETVAL
1187  
1188 SV*
1189 _number_kind_of(value)
1190 SV* value
1191 CODE:
1192 20127 value = ptkx_plain(aTHX_ value);
1193 20127 ▶ if (SvROK(value)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(value)) XSRETURN_UNDEF; 0 20127 20127
1194 20127 switch (ptkx_scalar_number_kind(aTHX_ value)) {
1195 28 case PTKX_INTEGER: RETVAL = newSVpvs("integer"); break;
1196 10587 case PTKX_FLOAT: RETVAL = newSVpvs("float"); break;
1197 9512 case PTKX_NOT_A_NUMBER:
1198 9512 default: XSRETURN_UNDEF;
1199 }
1200 OUTPUT:
1201 RETVAL
1202  
1203  
1204 MODULE = Text::KDL::XS PACKAGE = Text::KDL::XS::Parser
1205  
1206 int
1207 CLONE_SKIP(...)
1208 CODE:
1209 PERL_UNUSED_VAR(items);
1210 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
1211 OUTPUT:
1212 RETVAL
1213  
1214 SV*
1215 _new_string_parser(klass, document, options, max_depth)
1216 SV* klass
1217 SV* document
1218 IV options
1219 IV max_depth
1220 PREINIT:
1221 HV* stash;
1222 SV* source_copy;
1223 kdl_str text;
1224 STRLEN len;
1225 ptkx_parser* parser;
1226 CODE:
1227 130 stash = ptkx_class_stash(aTHX_ klass, PTKX_PARSER_CLASS);
1228 129 ptkx_check_parse_options(aTHX_ options, max_depth);
1229  
1230 /* ckdl keeps pointers into the source: read it once into a private
1231 * copy and encode only the copy */
1232 129 source_copy = sv_2mortal(newSVsv(document));
1233 129 ▶ if (!SvOK(source_copy) || SvROK(source_copy))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(source_copy) || SvROK(source_copy)) 129 0 129
if (!SvOK(source_copy) || SvROK(source_copy)) 0 129 129
1234 0 croak(PTKX_PARSER_CLASS ": a string source must be a defined string");
1235 129 text.data = SvPVutf8(source_copy, len);
1236 129 text.len = len;
1237 129 ▶ if (!ptkx_is_unicode_utf8(text.data, text.len))
Branch: 1 branch
BranchTrueFalseTotal
if (!ptkx_is_unicode_utf8(text.data, text.len)) 1 128 129
1238 1 croak("KDL parse error: input is not valid UTF-8");
1239  
1240 128 parser = ptkx_parser_new(aTHX_ max_depth);
1241 128 parser->source_copy = SvREFCNT_inc_simple_NN(source_copy);
1242 128 parser->parser = kdl_create_string_parser(text, (kdl_parse_option) options);
1243 128 ▶ if (!parser->parser) {
Branch: 1 branch
BranchTrueFalseTotal
if (!parser->parser) { 0 128 128
1244 0 ptkx_parser_free(aTHX_ parser);
1245 0 croak(PTKX_PARSER_CLASS ": kdl_create_string_parser failed");
1246 }
1247 128 RETVAL = ptkx_wrap(aTHX_ stash, parser, &ptkx_parser_vtbl);
1248 OUTPUT:
1249 RETVAL
1250  
1251 SV*
1252 _new_stream_parser(klass, callback, options, max_depth)
1253 SV* klass
1254 SV* callback
1255 IV options
1256 IV max_depth
1257 PREINIT:
1258 HV* stash;
1259 ptkx_parser* parser;
1260 CODE:
1261 38 stash = ptkx_class_stash(aTHX_ klass, PTKX_PARSER_CLASS);
1262 38 ptkx_check_parse_options(aTHX_ options, max_depth);
1263 38 callback = ptkx_plain(aTHX_ callback);
1264 38 ▶ if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 38 0 38
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 38 38
1265 0 croak(PTKX_PARSER_CLASS ": a stream source must be a CODE reference");
1266  
1267 38 parser = ptkx_parser_new(aTHX_ max_depth);
1268 38 parser->read_cb = newSVsv(callback);
1269 /* ckdl calls the source once here, looking for a byte order mark */
1270 38 parser->parser = kdl_create_stream_parser(ptkx_read_thunk, (void*) parser,
1271 (kdl_parse_option) options);
1272 38 ▶ if (!parser->parser || parser->error) {
Branch: 2 branches
BranchTrueFalseTotal
if (!parser->parser || parser->error) { 38 0 38
if (!parser->parser || parser->error) { 3 35 38
1273 3 ▶ SV* error = parser->error ? sv_2mortal(SvREFCNT_inc_simple_NN(parser->error)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV* error = parser->error ? sv_2mortal(SvREFCNT_inc_simple_NN(parser->error)) : NULL; 3 0 3
1274 3 ptkx_parser_free(aTHX_ parser);
1275 3 ▶ if (error) croak_sv(error);
Branch: 1 branch
BranchTrueFalseTotal
if (error) croak_sv(error); 3 0 3
1276 0 croak(PTKX_PARSER_CLASS ": kdl_create_stream_parser failed");
1277 }
1278 35 RETVAL = ptkx_wrap(aTHX_ stash, parser, &ptkx_parser_vtbl);
1279 OUTPUT:
1280 RETVAL
1281  
1282 SV*
1283 _next_event(parser)
1284 ptkx_parser* parser
1285 CODE:
1286 23867 ▶ if (parser->in_callback)
Branch: 1 branch
BranchTrueFalseTotal
if (parser->in_callback) 1 23866 23867
1287 1 croak(PTKX_PARSER_CLASS ": next_event called from inside the parser's own source callback");
1288 23866 ▶ if (parser->state == PTKX_FAILED) croak_sv(parser->error);
Branch: 1 branch
BranchTrueFalseTotal
if (parser->state == PTKX_FAILED) croak_sv(parser->error); 2 23864 23866
1289 23864 ▶ if (parser->state == PTKX_FINISHED) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (parser->state == PTKX_FINISHED) XSRETURN_UNDEF; 3 23861 23864
1290  
1291 23861 PTKX_KEEP_ALIVE(ST(0));
1292 23861 RETVAL = ptkx_next_event(aTHX_ parser);
1293 23830 ▶ if (!RETVAL) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) XSRETURN_UNDEF; 126 23704 23830
1294 OUTPUT:
1295 RETVAL
1296  
1297  
1298 MODULE = Text::KDL::XS PACKAGE = Text::KDL::XS::Emitter
1299  
1300 int
1301 CLONE_SKIP(...)
1302 CODE:
1303 PERL_UNUSED_VAR(items);
1304 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
1305 OUTPUT:
1306 RETVAL
1307  
1308 SV*
1309 _new(klass, version, indent, escape_mode, identifier_mode)
1310 SV* klass
1311 IV version
1312 IV indent
1313 IV escape_mode
1314 IV identifier_mode
1315 PREINIT:
1316 HV* stash;
1317 kdl_emitter_options options;
1318 ptkx_emitter* emitter;
1319 CODE:
1320 184 stash = ptkx_class_stash(aTHX_ klass, PTKX_EMITTER_CLASS);
1321 184 options = ptkx_emitter_options(aTHX_ version, indent, escape_mode, identifier_mode);
1322  
1323 184 Newxz(emitter, 1, ptkx_emitter);
1324 184 emitter->version = options.version;
1325 184 emitter->writes_bare = options.identifier_mode != KDL_QUOTE_ALL_IDENTIFIERS;
1326 184 emitter->emitter = kdl_create_buffering_emitter(&options);
1327 184 ▶ if (!emitter->emitter) {
Branch: 1 branch
BranchTrueFalseTotal
if (!emitter->emitter) { 0 184 184
1328 0 Safefree(emitter);
1329 0 croak("emit_kdl: kdl_create_buffering_emitter failed");
1330 }
1331 184 RETVAL = ptkx_wrap(aTHX_ stash, emitter, &ptkx_emitter_vtbl);
1332 OUTPUT:
1333 RETVAL
1334  
1335 void
1336 _emit_node(emitter, name, type_annotation)
1337 ptkx_emitter* emitter
1338 SV* name
1339 SV* type_annotation
1340 PREINIT:
1341 kdl_str name_text;
1342 CODE:
1343 225 PTKX_KEEP_ALIVE(ST(0));
1344 225 name_text = ptkx_text_arg(aTHX_ name, "node name");
1345 223 type_annotation = ptkx_plain(aTHX_ type_annotation);
1346 223 ptkx_note_bare_text(emitter, name_text);
1347 223 ▶ if (SvOK(type_annotation)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(type_annotation)) { 66 157 223
1348 66 kdl_str type_text = ptkx_text_arg(aTHX_ type_annotation, "type annotation");
1349 64 ptkx_note_bare_text(emitter, type_text);
1350 64 ptkx_check_emit(aTHX_ kdl_emit_node_with_type(emitter->emitter, type_text, name_text),
1351 "kdl_emit_node_with_type");
1352 }
1353 else {
1354 157 ptkx_check_emit(aTHX_ kdl_emit_node(emitter->emitter, name_text), "kdl_emit_node");
1355 }
1356  
1357 void
1358 _emit_arg(emitter, payload)
1359 ptkx_emitter* emitter
1360 SV* payload
1361 PREINIT:
1362 kdl_value value;
1363 char number_text[PTKX_NUMBER_BUFSIZE];
1364 CODE:
1365 10204 PTKX_KEEP_ALIVE(ST(0));
1366 10204 ptkx_value_from_payload(aTHX_ emitter, payload, number_text, &value);
1367 10188 ptkx_check_emit(aTHX_ kdl_emit_arg(emitter->emitter, &value), "kdl_emit_arg");
1368  
1369 void
1370 _emit_property(emitter, key, payload)
1371 ptkx_emitter* emitter
1372 SV* key
1373 SV* payload
1374 PREINIT:
1375 kdl_str key_text;
1376 kdl_value value;
1377 char number_text[PTKX_NUMBER_BUFSIZE];
1378 CODE:
1379 67 PTKX_KEEP_ALIVE(ST(0));
1380 67 key_text = ptkx_text_arg(aTHX_ key, "property key");
1381 65 ptkx_note_bare_text(emitter, key_text);
1382 65 ptkx_value_from_payload(aTHX_ emitter, payload, number_text, &value);
1383 65 ptkx_check_emit(aTHX_ kdl_emit_property(emitter->emitter, key_text, &value),
1384 "kdl_emit_property");
1385  
1386 void
1387 _start_children(emitter)
1388 ptkx_emitter* emitter
1389 CODE:
1390 16 ptkx_check_emit(aTHX_ kdl_start_emitting_children(emitter->emitter),
1391 "kdl_start_emitting_children");
1392  
1393 void
1394 _finish_children(emitter)
1395 ptkx_emitter* emitter
1396 CODE:
1397 15 ptkx_check_emit(aTHX_ kdl_finish_emitting_children(emitter->emitter),
1398 "kdl_finish_emitting_children");
1399  
1400 void
1401 _emit_end(emitter)
1402 ptkx_emitter* emitter
1403 CODE:
1404 154 ptkx_check_emit(aTHX_ kdl_emit_end(emitter->emitter), "kdl_emit_end");
1405  
1406 bool
1407 _needs_quoting(emitter)
1408 ptkx_emitter* emitter
1409 CODE:
1410 154 ▶ RETVAL = emitter->needs_quoting;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = emitter->needs_quoting; 36 118 154
1411 OUTPUT:
1412 RETVAL
1413  
1414 SV*
1415 _get_buffer(emitter)
1416 ptkx_emitter* emitter
1417 PREINIT:
1418 kdl_str buffer;
1419 CODE:
1420 154 buffer = kdl_get_emitter_buffer(emitter->emitter);
1421 154 ▶ RETVAL = newSVpvn_flags(buffer.data ? buffer.data : "", buffer.len, SVf_UTF8);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSVpvn_flags(buffer.data ? buffer.data : "", buffer.len, SVf_UTF8); 154 0 154
1422 OUTPUT:
1423 RETVAL
« ckdl/src/utf8.c ↑ Summary ckdl/src/bigint.c »