lib/Data/Pond.xs

filter: Statementstmt 87.7% 374 / 426 Branchbran 75.1% 263 / 350 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.0% 637 / 776 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Data/Pond.pm ↑ Summary
1 #define PERL_NO_GET_CONTEXT 1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
7 #define PERL_DECIMAL_VERSION \
8 PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
9 #define PERL_VERSION_GE(r,v,s) \
10 (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
11  
12 #ifndef cBOOL
13 # define cBOOL(x) ((bool)!!(x))
14 #endif /* !cBOOL */
15  
16 #ifndef hv_fetchs
17 # define hv_fetchs(hv, keystr, lval) \
18 hv_fetch(hv, ""keystr"", sizeof(keystr)-1, lval)
19 #endif /* !hv_fetchs */
20  
21 #ifndef newSVpvs
22 # define newSVpvs(string) newSVpvn(""string"", sizeof(string)-1)
23 #endif /* !newSVpvs */
24  
25 #ifndef sv_catpvs_nomg
26 # define sv_catpvs_nomg(sv, string) \
27 sv_catpvn_nomg(sv, ""string"", sizeof(string)-1)
28 #endif /* !sv_catpvs_nomg */
29  
30 #if PERL_VERSION_GE(5,19,4)
31 typedef SSize_t array_ix_t;
32 #else /* <5.19.4 */
33 typedef I32 array_ix_t;
34 #endif /* <5.19.4 */
35  
36 #ifndef uvchr_to_utf8_flags
37 #define uvchr_to_utf8_flags(d, uv, flags) uvuni_to_utf8_flags(d, uv, flags);
38 #endif
39  
40 /* parameter classification */
41  
42 #define sv_is_glob(sv) (SvTYPE(sv) == SVt_PVGV)
43  
44 #if PERL_VERSION_GE(5,11,0)
45 # define sv_is_regexp(sv) (SvTYPE(sv) == SVt_REGEXP)
46 #else /* <5.11.0 */
47 # define sv_is_regexp(sv) 0
48 #endif /* <5.11.0 */
49  
50 #define sv_is_undef(sv) (!sv_is_glob(sv) && !sv_is_regexp(sv) && !SvOK(sv))
51  
52 #define sv_is_string(sv) \
53 (!sv_is_glob(sv) && !sv_is_regexp(sv) && \
54 (SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK|SVp_IOK|SVp_NOK|SVp_POK)))
55  
56 /* exceptions */
57  
58 #define throw_utf8_error() croak("broken internal UTF-8 encoding\n")
59 #define throw_syntax_error(p) croak("Pond syntax error\n")
60 #define throw_constraint_error(MSG) croak("Pond constraint error: "MSG"\n")
61 #define throw_data_error(MSG) croak("Pond data error: "MSG"\n")
62  
63 /*
64 * string walking
65 *
66 * The parser deals with strings that are internally encoded using Perl's
67 * extended form of UTF-8. It is not assumed that the encoding is
68 * well-formed; encoding errors will result in an exception. The encoding
69 * octets are treated as U8 type.
70 *
71 * Characters that are known to be in the ASCII range are in some places
72 * processed as U8. General Unicode characters are processed as U32, with
73 * the intent that the entire ISO-10646 31-bit range be handleable. Any
74 * codepoint is accepted for processing, even the surrogates (which are
75 * not legal in true UTF-8 encoding). Perl's extended UTF-8 extends to
76 * 72-bit codepoints; encodings beyond the 31-bit range are translated to
77 * codepoint U+80000000, whereby they are all treated as invalid.
78 *
79 * char_unicode() returns the codepoint represented by the character being
80 * pointed at, or throws an exception if the encoding is malformed.
81 *
82 * To move on to the character following the one pointed at, use the core
83 * macro UTF8SKIP(), as in (p + UTF8SKIP(p)). It assumes that the character
84 * is properly encoded, so it is essential that char_unicode() has been
85 * called on it first.
86 *
87 * Given an input SV (that is meant to be a string), pass it through
88 * upgrade_sv() to return an SV that contains the string in UTF-8. This
89 * could be either the same SV (if it is already UTF-8-encoded or contains
90 * no non-ASCII characters) or a mortal upgraded copy.
91 */
92  
93 #define char_unicode(p) THX_char_unicode(aTHX_ p)
94 439 static U32 THX_char_unicode(pTHX_ U8 *p)
95 {
96 439 U32 val = *p;
97 U8 req_c1;
98 int ncont;
99 int i;
100 439 ▶ if(!(val & 0x80)) return val;
Branch: 1 branch
BranchTrueFalseTotal
if(!(val & 0x80)) return val; 401 38 439
101 38 ▶ if(!(val & 0x40)) throw_utf8_error();
Branch: 1 branch
BranchTrueFalseTotal
if(!(val & 0x40)) throw_utf8_error(); 0 38 38
102 38 ▶ if(!(val & 0x20)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!(val & 0x20)) { 38 0 38
103 38 ▶ if(!(val & 0x1e)) throw_utf8_error();
Branch: 1 branch
BranchTrueFalseTotal
if(!(val & 0x1e)) throw_utf8_error(); 0 38 38
104 38 val &= 0x1f;
105 38 ncont = 1;
106 38 req_c1 = 0x00;
107 0 ▶ } else if(!(val & 0x10)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(!(val & 0x10)) { 0 0 0
108 0 val &= 0x0f;
109 0 ncont = 2;
110 0 req_c1 = 0x20;
111 0 ▶ } else if(!(val & 0x08)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(!(val & 0x08)) { 0 0 0
112 0 val &= 0x07;
113 0 ncont = 3;
114 0 req_c1 = 0x30;
115 0 ▶ } else if(!(val & 0x04)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(!(val & 0x04)) { 0 0 0
116 0 val &= 0x03;
117 0 ncont = 4;
118 0 req_c1 = 0x38;
119 0 ▶ } else if(!(val & 0x02)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(!(val & 0x02)) { 0 0 0
120 0 val &= 0x01;
121 0 ncont = 5;
122 0 req_c1 = 0x3c;
123 0 ▶ } else if(!(val & 0x01)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(!(val & 0x01)) { 0 0 0
124 0 ▶ if(!(p[1] & 0x3e)) throw_utf8_error();
Branch: 1 branch
BranchTrueFalseTotal
if(!(p[1] & 0x3e)) throw_utf8_error(); 0 0 0
125 0 ▶ for(i = 6; i--; )
Branch: 1 branch
BranchTrueFalseTotal
for(i = 6; i--; ) 0 0 0
126 0 ▶ if((*++p & 0xc0) != 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if((*++p & 0xc0) != 0x80) 0 0 0
127 0 throw_utf8_error();
128 0 return 0x80000000;
129 } else {
130 0 U8 first_six = 0;
131 0 ▶ for(i = 6; i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 6; i--; ) { 0 0 0
132 0 U8 ext = *++p;
133 0 ▶ if((ext & 0xc0) != 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if((ext & 0xc0) != 0x80) 0 0 0
134 0 throw_utf8_error();
135 0 first_six |= ext;
136 }
137 0 ▶ if(!(first_six & 0x3f))
Branch: 1 branch
BranchTrueFalseTotal
if(!(first_six & 0x3f)) 0 0 0
138 0 throw_utf8_error();
139 0 ▶ for(i = 6; i--; )
Branch: 1 branch
BranchTrueFalseTotal
for(i = 6; i--; ) 0 0 0
140 0 ▶ if((*++p & 0xc0) != 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if((*++p & 0xc0) != 0x80) 0 0 0
141 0 throw_utf8_error();
142 0 return 0x80000000;
143 }
144 38 ▶ if(val == 0 && !(p[1] & req_c1))
Branch: 2 branches
BranchTrueFalseTotal
if(val == 0 && !(p[1] & req_c1)) 0 38 38
if(val == 0 && !(p[1] & req_c1)) 0 0 0
145 0 throw_utf8_error();
146 76 ▶ for(i = ncont; i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = ncont; i--; ) { 38 38 76
147 38 U8 ext = *++p;
148 38 ▶ if((ext & 0xc0) != 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if((ext & 0xc0) != 0x80) 0 38 38
149 0 throw_utf8_error();
150 38 val = UTF8_ACCUMULATE(val, ext);
151 }
152 38 return val;
153 }
154  
155 #define sv_cat_unichar(str, val) THX_sv_cat_unichar(aTHX_ str, val)
156 33 static void THX_sv_cat_unichar(pTHX_ SV *str, U32 val)
157 {
158 STRLEN vlen;
159 U8 *vstart, *voldend, *vnewend;
160 33 vlen = SvCUR(str);
161 33 ▶ vstart = (U8*)SvGROW(str, vlen+6+1);
Branch: 2 branches
BranchTrueFalseTotal
vstart = (U8*)SvGROW(str, vlen+6+1); 33 0 33
vstart = (U8*)SvGROW(str, vlen+6+1); 0 33 33
162 33 voldend = vstart + vlen;
163 33 vnewend = uvchr_to_utf8_flags(voldend, val, UNICODE_ALLOW_ANY);
164 33 *vnewend = 0;
165 33 SvCUR_set(str, vnewend - vstart);
166 33 }
167  
168 #define upgrade_sv(input) THX_upgrade_sv(aTHX_ input)
169 408 static SV *THX_upgrade_sv(pTHX_ SV *input)
170 {
171 U8 *p, *end;
172 STRLEN len;
173 408 ▶ if(SvUTF8(input)) return input;
Branch: 1 branch
BranchTrueFalseTotal
if(SvUTF8(input)) return input; 158 250 408
174 250 p = (U8*)SvPV(input, len);
175 2064 ▶ for(end = p + len; p != end; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for(end = p + len; p != end; p++) { 1820 244 2064
176 1820 ▶ if(*p & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if(*p & 0x80) { 6 1814 1820
177 6 SV *output = sv_mortalcopy(input);
178 6 sv_utf8_upgrade(output);
179 6 return output;
180 }
181 }
182 244 return input;
183 }
184  
185 /*
186 * Pond reading
187 */
188  
189 #define CHARATTR_WSP 0x01
190 #define CHARATTR_DQSPECIAL 0x02
191 #define CHARATTR_CONTROL 0x04
192 #define CHARATTR_HEXDIGIT 0x08
193 #define CHARATTR_WORDSTART 0x10
194 #define CHARATTR_WORDCONT 0x20
195 #define CHARATTR_DECDIGIT 0x40
196 #define CHARATTR_OCTDIGIT 0x80
197  
198 static U8 const asciichar_attr[128] = {
199 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* NUL to BEL */
200 0x04, 0x05, 0x05, 0x04, 0x05, 0x05, 0x04, 0x04, /* BS to SI */
201 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* DLE to ETB */
202 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* CAN to US */
203 0x01, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, /* SP to ' */
204 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ( to / */
205 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, /* 0 to 7 */
206 0x68, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8 to ? */
207 0x02, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x30, /* @ to G */
208 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, /* H to O */
209 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, /* P to W */
210 0x30, 0x30, 0x30, 0x00, 0x02, 0x00, 0x00, 0x30, /* X to _ */
211 0x00, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x30, /* ` to g */
212 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, /* h to o */
213 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, /* p to w */
214 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x04, /* x to DEL */
215 };
216  
217 1201 static int char_is_wsp(U8 c)
218 {
219 1201 ▶ return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_WSP);
Branch: 2 branches
BranchTrueFalseTotal
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_WSP); 1201 0 1201
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_WSP); 351 850 1201
220 }
221  
222 783 static int char_is_dqspecial(U8 c)
223 {
224 783 ▶ return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_DQSPECIAL);
Branch: 2 branches
BranchTrueFalseTotal
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_DQSPECIAL); 772 11 783
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_DQSPECIAL); 396 376 772
225 }
226  
227 433 static int char_is_control(U8 c)
228 {
229 433 ▶ return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_CONTROL);
Branch: 2 branches
BranchTrueFalseTotal
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_CONTROL); 429 4 433
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_CONTROL); 8 421 429
230 }
231  
232 415 static int unichar_is_control(U32 c)
233 {
234 415 ▶ return (c >= 0x80) ? c <= 0xa0 : (asciichar_attr[c] & CHARATTR_CONTROL);
Branch: 1 branch
BranchTrueFalseTotal
return (c >= 0x80) ? c <= 0xa0 : (asciichar_attr[c] & CHARATTR_CONTROL); 14 401 415
235 }
236  
237 50 static int char_is_wordstart(U8 c)
238 {
239 50 ▶ return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_WORDSTART);
Branch: 2 branches
BranchTrueFalseTotal
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_WORDSTART); 50 0 50
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_WORDSTART); 42 8 50
240 }
241  
242 59 static int char_is_wordcont(U8 c)
243 {
244 59 ▶ return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_WORDCONT);
Branch: 2 branches
BranchTrueFalseTotal
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_WORDCONT); 55 4 59
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_WORDCONT); 8 47 55
245 }
246  
247 242 static int char_is_decdigit(U8 c)
248 {
249 242 ▶ return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_DECDIGIT);
Branch: 2 branches
BranchTrueFalseTotal
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_DECDIGIT); 242 0 242
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_DECDIGIT); 94 148 242
250 }
251  
252 5 static int char_is_octdigit(U8 c)
253 {
254 5 ▶ return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_OCTDIGIT);
Branch: 2 branches
BranchTrueFalseTotal
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_OCTDIGIT); 5 0 5
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_OCTDIGIT); 3 2 5
255 }
256  
257 85 static int char_is_hexdigit(U8 c)
258 {
259 85 ▶ return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_HEXDIGIT);
Branch: 2 branches
BranchTrueFalseTotal
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_HEXDIGIT); 85 0 85
return !(c & 0x80) && (asciichar_attr[c] & CHARATTR_HEXDIGIT); 67 18 85
260 }
261  
262 67 static int hexdigit_value(U8 c)
263 {
264 67 ▶ return c <= '9' ? c - '0' : c <= 'F' ? c - 'A' + 10 : c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
return c <= '9' ? c - '0' : c <= 'F' ? c - 'A' + 10 : c - 'a' + 10; 54 13 67
return c <= '9' ? c - '0' : c <= 'F' ? c - 'A' + 10 : c - 'a' + 10; 0 13 13
265 }
266  
267 850 static U8 *parse_opt_wsp(U8 *p)
268 {
269 1201 ▶ while(char_is_wsp(*p))
Branch: 1 branch
BranchTrueFalseTotal
while(char_is_wsp(*p)) 351 850 1201
270 351 p++;
271 850 return p;
272 }
273  
274 static U8 const asciichar_backslash[128] = {
275 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, /* NUL to BEL */
276 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, /* BS to SI */
277 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, /* DLE to ETB */
278 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, /* CAN to US */
279 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, /* SP to ' */
280 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, /* ( to / */
281 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0 to 7 */
282 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* 8 to ? */
283 0x40, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, /* @ to G */
284 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, /* H to O */
285 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, /* P to W */
286 0xfd, 0xfd, 0xfd, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, /* X to _ */
287 0x60, 0x07, 0x08, 0xfd, 0xfd, 0x1b, 0x0c, 0xfd, /* ` to g */
288 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0x0a, 0xfd, /* h to o */
289 0xfd, 0xfd, 0x0d, 0xfd, 0x09, 0xfd, 0xfd, 0xfd, /* p to w */
290 0xfe, 0xfd, 0xfd, 0x7b, 0x7c, 0x7d, 0x7e, 0xfd, /* x to DEL */
291 };
292  
293 #define parse_dqstring(end, pp) THX_parse_dqstring(aTHX_ end, pp)
294 148 static SV *THX_parse_dqstring(pTHX_ U8 *end, U8 **pp)
295 {
296 148 U8 *p = *pp;
297 148 SV *datum = sv_2mortal(newSVpvs(""));
298 148 SvUTF8_on(datum);
299 254 while(1) {
300 402 U8 c = *p, e;
301 402 ▶ if(p == end || char_is_control(c)) throw_syntax_error(p);
Branch: 2 branches
BranchTrueFalseTotal
if(p == end || char_is_control(c)) throw_syntax_error(p); 402 0 402
if(p == end || char_is_control(c)) throw_syntax_error(p); 4 398 402
302 398 ▶ if(!char_is_dqspecial(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!char_is_dqspecial(c)) { 197 201 398
303 197 U8 *q = p;
304 do {
305 387 U32 val = char_unicode(q);
306 387 ▶ if(unichar_is_control(val))
Branch: 1 branch
BranchTrueFalseTotal
if(unichar_is_control(val)) 2 385 387
307 2 throw_syntax_error(q);
308 385 q += UTF8SKIP(q);
309 385 c = *q;
310 385 ▶ } while(q != end && !char_is_dqspecial(c));
Branch: 2 branches
BranchTrueFalseTotal
} while(q != end && !char_is_dqspecial(c)); 385 0 385
} while(q != end && !char_is_dqspecial(c)); 190 195 385
311 195 sv_catpvn_nomg(datum, (char*)p, q-p);
312 195 p = q;
313 195 continue;
314 }
315 201 ▶ if(c == '"') break;
Branch: 1 branch
BranchTrueFalseTotal
if(c == '"') break; 141 60 201
316 60 ▶ if(c != '\\') throw_syntax_error(p);
Branch: 1 branch
BranchTrueFalseTotal
if(c != '\\') throw_syntax_error(p); 0 60 60
317 60 c = *++p;
318 60 ▶ if(p == end) throw_syntax_error(p);
Branch: 1 branch
BranchTrueFalseTotal
if(p == end) throw_syntax_error(p); 0 60 60
319 60 ▶ if(c & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if(c & 0x80) { 0 60 60
320 0 U32 val = char_unicode(p);
321 0 ▶ if(unichar_is_control(val)) throw_syntax_error(q);
Branch: 1 branch
BranchTrueFalseTotal
if(unichar_is_control(val)) throw_syntax_error(q); 0 0 0
322 /* character will be treated as literal anyway */
323 0 continue;
324 }
325 60 e = asciichar_backslash[c];
326 60 ▶ if(e == 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
if(e == 0xff) { 3 57 60
327 3 U32 val = c & 7;
328 3 c = *++p;
329 3 ▶ if(char_is_octdigit(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if(char_is_octdigit(c)) { 2 1 3
330 2 p++;
331 2 val = (val << 3) | (c & 7);
332 2 c = *p;
333 2 ▶ if(char_is_octdigit(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if(char_is_octdigit(c)) { 1 1 2
334 1 p++;
335 1 val = (val << 3) | (c & 7);
336 }
337 }
338 3 sv_cat_unichar(datum, val);
339 57 ▶ } else if(e == 0xfe) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(e == 0xfe) { 30 27 57
340 U32 val;
341 30 c = *++p;
342 30 ▶ if(char_is_hexdigit(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if(char_is_hexdigit(c)) { 21 9 30
343 21 p++;
344 21 val = hexdigit_value(c);
345 21 c = *p;
346 21 ▶ if(char_is_hexdigit(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if(char_is_hexdigit(c)) { 21 0 21
347 21 p++;
348 21 val = (val << 4) | hexdigit_value(c);
349 }
350 9 ▶ } else if(c == '{') {
Branch: 1 branch
BranchTrueFalseTotal
} else if(c == '{') { 9 0 9
351 9 p++;
352 9 c = *p;
353 9 ▶ if(!char_is_hexdigit(c))
Branch: 1 branch
BranchTrueFalseTotal
if(!char_is_hexdigit(c)) 0 9 9
354 0 throw_syntax_error(p);
355 9 val = 0;
356 do {
357 25 ▶ if(val & 0x78000000)
Branch: 1 branch
BranchTrueFalseTotal
if(val & 0x78000000) 0 25 25
358 0 throw_constraint_error(
359 "invalid character");
360 25 val = (val << 4) | hexdigit_value(c);
361 25 c = *++p;
362 25 ▶ } while(char_is_hexdigit(c));
Branch: 1 branch
BranchTrueFalseTotal
} while(char_is_hexdigit(c)); 16 9 25
363 9 ▶ if(c != '}') throw_syntax_error(p);
Branch: 1 branch
BranchTrueFalseTotal
if(c != '}') throw_syntax_error(p); 0 9 9
364 9 p++;
365 } else {
366 0 throw_syntax_error(p);
367 }
368 30 sv_cat_unichar(datum, val);
369 27 ▶ } else if(e == 0xfd) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(e == 0xfd) { 1 26 27
370 1 throw_syntax_error(p);
371 } else {
372 26 p++;
373 26 sv_catpvn_nomg(datum, (char*)&e, 1);
374 }
375 }
376 141 *pp = p+1;
377 141 return datum;
378 }
379  
380 #define parse_sqstring(end, pp) THX_parse_sqstring(aTHX_ end, pp)
381 15 static SV *THX_parse_sqstring(pTHX_ U8 *end, U8 **pp)
382 {
383 15 U8 *p = *pp;
384 15 SV *datum = sv_2mortal(newSVpvs(""));
385 15 SvUTF8_on(datum);
386 16 while(1) {
387 31 U8 c = *p;
388 31 ▶ if(p == end || char_is_control(c)) throw_syntax_error(p);
Branch: 2 branches
BranchTrueFalseTotal
if(p == end || char_is_control(c)) throw_syntax_error(p); 31 0 31
if(p == end || char_is_control(c)) throw_syntax_error(p); 4 27 31
389 27 ▶ if(c == '\'') break;
Branch: 1 branch
BranchTrueFalseTotal
if(c == '\'') break; 9 18 27
390 18 ▶ if(c != '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if(c != '\\') { 14 4 18
391 14 U8 *q = p;
392 do {
393 28 U32 val = char_unicode(q);
394 28 ▶ if(unichar_is_control(val))
Branch: 1 branch
BranchTrueFalseTotal
if(unichar_is_control(val)) 2 26 28
395 2 throw_syntax_error(q);
396 26 q += UTF8SKIP(q);
397 26 c = *q;
398 26 ▶ } while(q != end && c != '\'' && c != '\\');
Branch: 3 branches
BranchTrueFalseTotal
} while(q != end && c != '\'' && c != '\\'); 26 0 26
} while(q != end && c != '\'' && c != '\\'); 18 8 26
} while(q != end && c != '\'' && c != '\\'); 14 4 18
399 12 sv_catpvn_nomg(datum, (char*)p, q-p);
400 12 p = q;
401 } else {
402 4 c = p[1];
403 4 ▶ if(c == '\\' || c == '\'')
Branch: 2 branches
BranchTrueFalseTotal
if(c == '\\' || c == '\'') 3 1 4
if(c == '\\' || c == '\'') 1 2 3
404 2 p++;
405 4 sv_catpvn_nomg(datum, (char*)p, 1);
406 4 p++;
407 }
408 }
409 9 *pp = p+1;
410 9 return datum;
411 }
412  
413 #define array_to_hash(array) THX_array_to_hash(aTHX_ array)
414 45 static SV *THX_array_to_hash(pTHX_ AV *array)
415 {
416 HV *hash;
417 SV *href;
418 array_ix_t alen, i;
419 45 alen = av_len(array);
420 45 ▶ if(!(alen & 1))
Branch: 1 branch
BranchTrueFalseTotal
if(!(alen & 1)) 0 45 45
421 0 throw_constraint_error(
422 "odd number of elements in hash constructor");
423 45 hash = newHV();
424 45 href = sv_2mortal(newRV_noinc((SV*)hash));
425 103 ▶ for(i = 0; i <= alen; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i <= alen; i += 2) { 58 45 103
426 58 SV **key_ptr = av_fetch(array, i, 0);
427 STRLEN key_len;
428 char *key_str;
429 SV *value;
430 58 ▶ if(!key_ptr || !sv_is_string(*key_ptr))
Branch: 4 branches
BranchTrueFalseTotal
if(!key_ptr || !sv_is_string(*key_ptr)) 58 0 58
if(!key_ptr || !sv_is_string(*key_ptr)) 58 0 58
if(!key_ptr || !sv_is_string(*key_ptr)) 58 0 58
if(!key_ptr || !sv_is_string(*key_ptr)) 0 58 58
431 0 throw_constraint_error("non-string hash key");
432 58 key_str = SvPV(*key_ptr, key_len);
433 58 value = *av_fetch(array, i+1, 0);
434 58 ▶ if(!hv_store(hash, key_str, -key_len, SvREFCNT_inc(value), 0))
Branch: 1 branch
BranchTrueFalseTotal
if(!hv_store(hash, key_str, -key_len, SvREFCNT_inc(value), 0)) 0 58 58
435 0 SvREFCNT_dec(value);
436 }
437 45 return href;
438 }
439  
440 #define parse_datum(end, pp) THX_parse_datum(aTHX_ end, pp)
441 static SV *THX_parse_datum(pTHX_ U8 *end, U8 **pp);
442 382 static SV *THX_parse_datum(pTHX_ U8 *end, U8 **pp)
443 {
444 382 U8 *p = *pp;
445 382 U8 c = *p;
446 SV *datum;
447 382 ▶ if(c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if(c == '"') { 148 234 382
448 148 p++;
449 148 datum = parse_dqstring(end, &p);
450 234 ▶ } else if(c == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
} else if(c == '\'') { 15 219 234
451 15 p++;
452 15 datum = parse_sqstring(end, &p);
453 219 ▶ } else if(c == '[' || c == '{') {
Branch: 2 branches
BranchTrueFalseTotal
} else if(c == '[' || c == '{') { 162 57 219
} else if(c == '[' || c == '{') { 45 117 162
454 102 int is_hash = c == '{';
455 102 ▶ U8 close = is_hash ? '}' : ']';
Branch: 1 branch
BranchTrueFalseTotal
U8 close = is_hash ? '}' : ']'; 45 57 102
456 102 AV *array = newAV();
457 102 sv_2mortal((SV*)array);
458 102 p++;
459 while(1) {
460 228 p = parse_opt_wsp(p);
461 228 ▶ if(*p == close) break;
Branch: 1 branch
BranchTrueFalseTotal
if(*p == close) break; 64 164 228
462 164 av_push(array, SvREFCNT_inc(parse_datum(end, &p)));
463 160 p = parse_opt_wsp(p);
464 160 ▶ if(*p == close) break;
Branch: 1 branch
BranchTrueFalseTotal
if(*p == close) break; 34 126 160
465 126 ▶ if(*p == ',') {
Branch: 1 branch
BranchTrueFalseTotal
if(*p == ',') { 70 56 126
466 70 p++;
467 56 ▶ } else if(p[0] == '=' && p[1] == '>') {
Branch: 2 branches
BranchTrueFalseTotal
} else if(p[0] == '=' && p[1] == '>') { 56 0 56
} else if(p[0] == '=' && p[1] == '>') { 56 0 56
468 56 p += 2;
469 } else {
470 0 throw_syntax_error(p);
471 }
472 }
473 98 p++;
474 151 ▶ datum = is_hash ? array_to_hash(array) :
Branch: 1 branch
BranchTrueFalseTotal
datum = is_hash ? array_to_hash(array) : 45 53 98
475 53 sv_2mortal(newRV_inc((SV*)array));
476 117 ▶ } else if(c & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(c & 0x80) { 0 117 117
477 0 throw_syntax_error(p);
478 } else {
479 117 U8 attr = asciichar_attr[c];
480 117 ▶ if(attr & CHARATTR_WORDSTART) {
Branch: 1 branch
BranchTrueFalseTotal
if(attr & CHARATTR_WORDSTART) { 47 70 117
481 47 U8 *start = p++;
482 U8 *q;
483 55 ▶ while(char_is_wordcont(*p))
Branch: 1 branch
BranchTrueFalseTotal
while(char_is_wordcont(*p)) 8 47 55
484 8 p++;
485 47 q = parse_opt_wsp(p);
486 47 ▶ if(!(q[0] == '=' && q[1] == '>'))
Branch: 2 branches
BranchTrueFalseTotal
if(!(q[0] == '=' && q[1] == '>')) 45 2 47
if(!(q[0] == '=' && q[1] == '>')) 0 45 45
487 2 throw_syntax_error(q);
488 45 datum = sv_2mortal(newSVpvn((char*)start, p-start));
489 70 ▶ } else if(attr & CHARATTR_DECDIGIT) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(attr & CHARATTR_DECDIGIT) { 64 6 70
490 64 U8 *start = p++;
491 64 ▶ if(c == '0') {
Branch: 1 branch
BranchTrueFalseTotal
if(c == '0') { 5 59 64
492 5 ▶ if(char_is_decdigit(*p)) throw_syntax_error(p);
Branch: 1 branch
BranchTrueFalseTotal
if(char_is_decdigit(*p)) throw_syntax_error(p); 0 5 5
493 } else {
494 93 ▶ while(char_is_decdigit(*p))
Branch: 1 branch
BranchTrueFalseTotal
while(char_is_decdigit(*p)) 34 59 93
495 34 p++;
496 }
497 64 datum = sv_2mortal(newSVpvn((char*)start, p-start));
498 } else {
499 6 throw_syntax_error(p);
500 }
501 }
502 357 *pp = p;
503 357 return datum;
504 }
505  
506 /*
507 * Pond writing
508 */
509  
510 struct writer_options {
511 int indent;
512 int undef_is_empty, unicode;
513 };
514  
515 152 static int pvn_is_integer(U8 *p, STRLEN len)
516 {
517 152 U8 *e = p + len;
518 152 ▶ if(len == 0 || len > 9) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if(len == 0 || len > 9) return 0; 148 4 152
if(len == 0 || len > 9) return 0; 8 140 148
519 140 ▶ if(*p == '0') return len == 1;
Branch: 1 branch
BranchTrueFalseTotal
if(*p == '0') return len == 1; 20 120 140
520 180 ▶ for(; p != e; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for(; p != e; p++) { 144 36 180
521 144 ▶ if(!char_is_decdigit(*p)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(!char_is_decdigit(*p)) return 0; 84 60 144
522 }
523 36 return 1;
524 }
525  
526 #define ASCIICHAR_QUOTE_LITERAL 0x00
527 #define ASCIICHAR_QUOTE_HEXPAIR 0x01
528  
529 static U8 const asciichar_quote[128] = {
530 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* NUL to BEL */
531 0x01, 0x74, 0x6e, 0x01, 0x01, 0x01, 0x01, 0x01, /* BS to SI */
532 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* DLE to ETB */
533 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* CAN to US */
534 0x00, 0x00, 0x22, 0x00, 0x24, 0x00, 0x00, 0x00, /* SP to ' */
535 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ( to / */
536 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0 to 7 */
537 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8 to ? */
538 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* @ to G */
539 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* H to O */
540 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* P to W */
541 0x00, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, /* X to _ */
542 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ` to g */
543 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* h to o */
544 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* p to w */
545 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* x to DEL */
546 };
547  
548 static char const hexdig[16] = "0123456789abcdef";
549  
550 #define serialise_as_string(wo, out, datum) \
551 THX_serialise_as_string(aTHX_ wo, out, datum)
552 152 static void THX_serialise_as_string(pTHX_ struct writer_options *wo,
553 SV *out, SV *datum)
554 {
555 U8 *p;
556 STRLEN len;
557 152 p = (U8*)SvPV(datum, len);
558 152 ▶ if(pvn_is_integer(p, len)) {
Branch: 1 branch
BranchTrueFalseTotal
if(pvn_is_integer(p, len)) { 40 112 152
559 40 sv_catpvn_nomg(out, (char *)p, len);
560 } else {
561 112 U8 *e = p + len;
562 112 U8 *lstart = p;
563 112 sv_catpvs_nomg(out, "\"");
564 472 ▶ while(p != e) {
Branch: 1 branch
BranchTrueFalseTotal
while(p != e) { 360 112 472
565 360 U8 c = *p;
566 360 ▶ if(c & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if(c & 0x80) { 24 336 360
567 24 U32 val = char_unicode(p);
568 24 ▶ if(val == 0x80000000)
Branch: 1 branch
BranchTrueFalseTotal
if(val == 0x80000000) 0 24 24
569 0 throw_data_error("invalid character");
570 24 ▶ if(val <= 0xa0 || !wo->unicode) {
Branch: 2 branches
BranchTrueFalseTotal
if(val <= 0xa0 || !wo->unicode) { 16 8 24
if(val <= 0xa0 || !wo->unicode) { 8 8 16
571 16 ▶ if(lstart != p)
Branch: 1 branch
BranchTrueFalseTotal
if(lstart != p) 16 0 16
572 16 sv_catpvn_nomg(out,
573 (char*)lstart,
574 p-lstart);
575 }
576 24 p += UTF8SKIP(p);
577 24 ▶ if(val <= 0xa0) {
Branch: 1 branch
BranchTrueFalseTotal
if(val <= 0xa0) { 8 16 24
578 8 c = val;
579 8 p--;
580 8 goto hexpair;
581 }
582 16 ▶ if(!wo->unicode) {
Branch: 1 branch
BranchTrueFalseTotal
if(!wo->unicode) { 8 8 16
583 char hexbuf[12];
584 8 sprintf(hexbuf, "\\x{%02x}",
585 (unsigned)val);
586 8 sv_catpvn_nomg(out, hexbuf,
587 strlen(hexbuf));
588 8 lstart = p;
589 }
590 } else {
591 336 U8 quote = asciichar_quote[c];
592 336 ▶ if(quote == ASCIICHAR_QUOTE_LITERAL) {
Branch: 1 branch
BranchTrueFalseTotal
if(quote == ASCIICHAR_QUOTE_LITERAL) { 308 28 336
593 308 p++;
594 308 continue;
595 }
596 28 ▶ if(lstart != p)
Branch: 1 branch
BranchTrueFalseTotal
if(lstart != p) 28 0 28
597 28 sv_catpvn_nomg(out, (char*)lstart,
598 p-lstart);
599 28 ▶ if(quote == ASCIICHAR_QUOTE_HEXPAIR) {
Branch: 1 branch
BranchTrueFalseTotal
if(quote == ASCIICHAR_QUOTE_HEXPAIR) { 8 20 28
600 char hexbuf[4];
601 8 hexpair:
602 16 hexbuf[0] = '\\';
603 16 hexbuf[1] = 'x';
604 16 hexbuf[2] = hexdig[c >> 4];
605 16 hexbuf[3] = hexdig[c & 0xf];
606 16 sv_catpvn_nomg(out, hexbuf, 4);
607 } else {
608 char bsbuf[2];
609 20 bsbuf[0] = '\\';
610 20 bsbuf[1] = (char)quote;
611 20 sv_catpvn_nomg(out, bsbuf, 2);
612 }
613 36 lstart = ++p;
614 }
615 }
616 112 ▶ if(lstart != p) sv_catpvn_nomg(out, (char*)lstart, p-lstart);
Branch: 1 branch
BranchTrueFalseTotal
if(lstart != p) sv_catpvn_nomg(out, (char*)lstart, p-lstart); 108 4 112
617 112 sv_catpvs_nomg(out, "\"");
618 }
619 152 }
620  
621 50 static int pvn_is_bareword(U8 *p, STRLEN len)
622 {
623 50 U8 *e = p + len;
624 50 ▶ if(!char_is_wordstart(*p)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(!char_is_wordstart(*p)) return 0; 8 42 50
625 42 ▶ while(++p != e) {
Branch: 1 branch
BranchTrueFalseTotal
while(++p != e) { 4 38 42
626 4 ▶ if(!char_is_wordcont(*p)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(!char_is_wordcont(*p)) return 0; 4 0 4
627 }
628 38 return 1;
629 }
630  
631 #define serialise_as_bareword(wo, out, datum) \
632 THX_serialise_as_bareword(aTHX_ wo, out, datum)
633 50 static void THX_serialise_as_bareword(pTHX_ struct writer_options *wo,
634 SV *out, SV *datum)
635 {
636 U8 *p;
637 STRLEN len;
638 50 p = (U8*)SvPV(datum, len);
639 50 ▶ if(pvn_is_bareword(p, len)) {
Branch: 1 branch
BranchTrueFalseTotal
if(pvn_is_bareword(p, len)) { 38 12 50
640 38 sv_catpvn_nomg(out, (char *)p, len);
641 } else {
642 12 serialise_as_string(wo, out, datum);
643 }
644 50 }
645  
646 #define serialise_newline(wo, out) THX_serialise_newline(aTHX_ wo, out)
647 135 static void THX_serialise_newline(pTHX_ struct writer_options *wo, SV *out)
648 {
649 135 int indent = wo->indent;
650 135 ▶ if(indent != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if(indent != -1) { 64 71 135
651 64 STRLEN cur = SvCUR(out);
652 64 ▶ char *p = SvGROW(out, cur+indent+2) + cur;
Branch: 2 branches
BranchTrueFalseTotal
char *p = SvGROW(out, cur+indent+2) + cur; 64 0 64
char *p = SvGROW(out, cur+indent+2) + cur; 0 64 64
653 64 *p++ = '\n';
654 64 memset(p, ' ', indent);
655 64 p[indent] = 0;
656 64 SvCUR_set(out, cur+1+indent);
657 }
658 135 }
659  
660 #define serialise_datum(wo, out, datum) \
661 THX_serialise_datum(aTHX_ wo, out, datum)
662 static void THX_serialise_datum(pTHX_ struct writer_options *wo,
663 SV *out, SV *datum);
664  
665 #define serialise_array(wo, out, adatum) \
666 THX_serialise_array(aTHX_ wo, out, adatum)
667 43 static void THX_serialise_array(pTHX_ struct writer_options *wo,
668 SV *out, AV *adatum)
669 {
670 43 array_ix_t alen = av_len(adatum), pos;
671 43 ▶ if(alen == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if(alen == -1) { 20 23 43
672 20 sv_catpvs_nomg(out, "[]");
673 20 return;
674 }
675 23 sv_catpvs_nomg(out, "[");
676 23 ▶ if(wo->indent != -1) wo->indent += 4;
Branch: 1 branch
BranchTrueFalseTotal
if(wo->indent != -1) wo->indent += 4; 10 13 23
677 23 serialise_newline(wo, out);
678 23 for(pos = 0; ; pos++) {
679 31 serialise_datum(wo, out,
680 *av_fetch(adatum, pos, 0));
681 29 ▶ if(pos == alen && wo->indent == -1)
Branch: 2 branches
BranchTrueFalseTotal
if(pos == alen && wo->indent == -1) 21 8 29
if(pos == alen && wo->indent == -1) 11 10 21
682 11 break;
683 18 sv_catpvs_nomg(out, ",");
684 18 ▶ if(pos == alen)
Branch: 1 branch
BranchTrueFalseTotal
if(pos == alen) 10 8 18
685 10 break;
686 8 serialise_newline(wo, out);
687 }
688 21 ▶ if(wo->indent != -1) wo->indent -= 4;
Branch: 1 branch
BranchTrueFalseTotal
if(wo->indent != -1) wo->indent -= 4; 10 11 21
689 21 serialise_newline(wo, out);
690 21 sv_catpvs_nomg(out, "]");
691 }
692  
693 #define serialise_hash(wo, out, hdatum) \
694 THX_serialise_hash(aTHX_ wo, out, hdatum)
695 38 static void THX_serialise_hash(pTHX_ struct writer_options *wo,
696 SV *out, HV *hdatum)
697 {
698 AV *keys;
699 38 U32 nelem = hv_iterinit(hdatum), pos;
700 38 ▶ if(nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(nelem == 0) { 4 34 38
701 4 sv_catpvs_nomg(out, "{}");
702 4 return;
703 }
704 34 keys = newAV();
705 34 sv_2mortal((SV*)keys);
706 34 av_extend(keys, nelem-1);
707 84 ▶ for(pos = nelem; pos--; ) {
Branch: 1 branch
BranchTrueFalseTotal
for(pos = nelem; pos--; ) { 50 34 84
708 50 SV *keysv = upgrade_sv(
709 hv_iterkeysv(hv_iternext(hdatum)));
710 50 av_push(keys, SvREFCNT_inc(keysv));
711 }
712 34 sortsv(AvARRAY(keys), nelem, Perl_sv_cmp);
713 34 sv_catpvs_nomg(out, "{");
714 34 ▶ if(wo->indent != -1) wo->indent += 4;
Branch: 1 branch
BranchTrueFalseTotal
if(wo->indent != -1) wo->indent += 4; 16 18 34
715 34 serialise_newline(wo, out);
716 50 for(pos = 0; ; pos++) {
717 50 SV *keysv = *av_fetch(keys, pos, 0);
718 STRLEN klen;
719 char *key;
720 50 serialise_as_bareword(wo, out, keysv);
721 50 ▶ if(wo->indent == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if(wo->indent == -1) { 26 24 50
722 26 sv_catpvs_nomg(out, "=>");
723 } else {
724 24 sv_catpvs_nomg(out, " => ");
725 }
726 50 key = SvPV(keysv, klen);
727 50 serialise_datum(wo, out, *hv_fetch(hdatum, key, -klen, 0));
728 49 ▶ if(pos == nelem-1 && wo->indent == -1)
Branch: 2 branches
BranchTrueFalseTotal
if(pos == nelem-1 && wo->indent == -1) 33 16 49
if(pos == nelem-1 && wo->indent == -1) 17 16 33
729 17 break;
730 32 sv_catpvs_nomg(out, ",");
731 32 ▶ if(pos == nelem-1)
Branch: 1 branch
BranchTrueFalseTotal
if(pos == nelem-1) 16 16 32
732 16 break;
733 16 serialise_newline(wo, out);
734 }
735 33 ▶ if(wo->indent != -1) wo->indent -= 4;
Branch: 1 branch
BranchTrueFalseTotal
if(wo->indent != -1) wo->indent -= 4; 16 17 33
736 33 serialise_newline(wo, out);
737 33 sv_catpvs_nomg(out, "}");
738 }
739  
740 235 static void THX_serialise_datum(pTHX_ struct writer_options *wo,
741 SV *out, SV *datum)
742 {
743 235 ▶ if(sv_is_undef(datum) && wo->undef_is_empty) {
Branch: 4 branches
BranchTrueFalseTotal
if(sv_is_undef(datum) && wo->undef_is_empty) { 234 1 235
if(sv_is_undef(datum) && wo->undef_is_empty) { 234 0 234
if(sv_is_undef(datum) && wo->undef_is_empty) { 6 228 234
if(sv_is_undef(datum) && wo->undef_is_empty) { 3 3 6
744 3 sv_catpvs_nomg(out, "\"\"");
745 232 ▶ } else if(sv_is_string(datum)) {
Branch: 3 branches
BranchTrueFalseTotal
} else if(sv_is_string(datum)) { 231 1 232
} else if(sv_is_string(datum)) { 231 0 231
} else if(sv_is_string(datum)) { 140 91 231
746 140 datum = upgrade_sv(datum);
747 140 serialise_as_string(wo, out, datum);
748 } else {
749 92 ▶ if(!SvROK(datum))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(datum)) 4 88 92
750 4 throw_data_error("unsupported data type");
751 88 datum = SvRV(datum);
752 88 ▶ if(SvOBJECT(datum))
Branch: 1 branch
BranchTrueFalseTotal
if(SvOBJECT(datum)) 4 84 88
753 4 throw_data_error("unsupported data type");
754 84 ▶ if(SvTYPE(datum) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(datum) == SVt_PVAV) { 43 41 84
755 43 serialise_array(wo, out, (AV*)datum);
756 41 ▶ } else if(SvTYPE(datum) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(SvTYPE(datum) == SVt_PVHV) { 38 3 41
757 38 serialise_hash(wo, out, (HV*)datum);
758 } else {
759 3 throw_data_error("unsupported data type");
760 }
761 }
762 221 }
763  
764 MODULE = Data::Pond PACKAGE = Data::Pond
765  
766 PROTOTYPES: DISABLE
767  
768 SV *
769 pond_read_datum(SV *text_sv)
770 PROTOTYPE: $
771 PREINIT:
772 STRLEN text_len;
773 U8 *p, *end;
774 CODE:
775 229 ▶ if(!sv_is_string(text_sv)) throw_data_error("text isn't a string");
Branch: 3 branches
BranchTrueFalseTotal
if(!sv_is_string(text_sv)) throw_data_error("text isn't a string"); 228 1 229
if(!sv_is_string(text_sv)) throw_data_error("text isn't a string"); 228 0 228
if(!sv_is_string(text_sv)) throw_data_error("text isn't a string"); 10 218 228
776 218 text_sv = upgrade_sv(text_sv);
777 218 p = (U8*)SvPV(text_sv, text_len);
778 218 end = p + text_len;
779 218 p = parse_opt_wsp(p);
780 218 RETVAL = parse_datum(end, &p);
781 197 p = parse_opt_wsp(p);
782 197 ▶ if(p != end) throw_syntax_error(p);
Branch: 1 branch
BranchTrueFalseTotal
if(p != end) throw_syntax_error(p); 2 195 197
783 195 SvREFCNT_inc(RETVAL);
784 OUTPUT:
785 RETVAL
786  
787 SV *
788 pond_write_datum(SV *datum, SV *options = 0)
789 PROTOTYPE: $;$
790 PREINIT:
791 154 ▶ struct writer_options wo = { -1, 0, 0 };
Branch: 1 branch
BranchTrueFalseTotal
struct writer_options wo = { -1, 0, 0 }; 0 154 154
792 CODE:
793 154 ▶ if(options) {
Branch: 1 branch
BranchTrueFalseTotal
if(options) { 154 0 154
794 HV *opthash;
795 SV **item_ptr;
796 154 ▶ if(!SvROK(options))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(options)) 0 154 154
797 0 throw_data_error("option hash isn't a hash");
798 154 options = SvRV(options);
799 154 ▶ if(SvOBJECT(options) || SvTYPE(options) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if(SvOBJECT(options) || SvTYPE(options) != SVt_PVHV) 154 0 154
if(SvOBJECT(options) || SvTYPE(options) != SVt_PVHV) 0 154 154
800 0 throw_data_error("option hash isn't a hash");
801 154 opthash = (HV*)options;
802 154 ▶ if((item_ptr = hv_fetchs(opthash, "indent", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if((item_ptr = hv_fetchs(opthash, "indent", 0))) { 70 84 154
803 70 SV *item = *item_ptr;
804 70 ▶ if(!sv_is_undef(item)) {
Branch: 3 branches
BranchTrueFalseTotal
if(!sv_is_undef(item)) { 70 0 70
if(!sv_is_undef(item)) { 70 0 70
if(!sv_is_undef(item)) { 70 0 70
805 70 ▶ if(!sv_is_string(item))
Branch: 3 branches
BranchTrueFalseTotal
if(!sv_is_string(item)) 70 0 70
if(!sv_is_string(item)) 70 0 70
if(!sv_is_string(item)) 0 70 70
806 0 throw_data_error(
807 "indent option isn't a number");
808 70 wo.indent = SvIV(item);
809 70 ▶ if(wo.indent < 0)
Branch: 1 branch
BranchTrueFalseTotal
if(wo.indent < 0) 0 70 70
810 0 throw_data_error(
811 "indent option is negative");
812 }
813 }
814 154 ▶ if((item_ptr = hv_fetchs(opthash, "undef_is_empty", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if((item_ptr = hv_fetchs(opthash, "undef_is_empty", 0))) { 3 151 154
815 3 SV *item = *item_ptr;
816 3 wo.undef_is_empty = cBOOL(SvTRUE(item));
817 }
818 154 ▶ if((item_ptr = hv_fetchs(opthash, "unicode", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if((item_ptr = hv_fetchs(opthash, "unicode", 0))) { 70 84 154
819 70 SV *item = *item_ptr;
820 70 wo.unicode = cBOOL(SvTRUE(item));
821 }
822 }
823 154 RETVAL = sv_2mortal(newSVpvs(""));
824 154 SvUTF8_on(RETVAL);
825 154 serialise_datum(&wo, RETVAL, datum);
826 143 SvREFCNT_inc(RETVAL);
827 OUTPUT:
828 RETVAL
« blib/lib/Data/Pond.pm ↑ Summary