DWIW.xs

filter: Statementstmt 72.8% 488 / 670 Branchbran 64.1% 204 / 318 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.0% 692 / 988 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« old_common.c ↑ Summary evt.c »
1 /*
2 Copyright (c) 2007-2010 Don Owens <don@regexguy.com>. All rights reserved.
3  
4 This is free software; you can redistribute it and/or modify it under
5 the Perl Artistic license. You should have received a copy of the
6 Artistic license with this distribution, in the file named
7 "Artistic". You may also obtain a copy from
8 http://regexguy.com/license/Artistic
9  
10 This program is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied
12 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE.
14 */
15  
16 /* $Revision: 1675 $ */
17  
18 /* #define PERL_NO_GET_CONTEXT */
19  
20 #include "DWIW.h"
21 #include "old_common.h"
22  
23 /*
24 #include "old_parse.h"
25 */
26  
27  
28 /* get rid of "value computed is not used" warnings */
29 #define IGNORE_RV(x) (void)(x)
30  
31 static SV *
32 1 vjson_encode_error(self_context * ctx, const char * file, int line_num, const char * fmt, va_list *ap_ptr) {
33 1 SV * error = newSVpv("", 0);
34 1 bool junk = 0;
35 1 HV * error_data = Nullhv;
36  
37 1 sv_setpvf(error, "JSON::DWIW v%s - ", MOD_VERSION);
38  
39 1 sv_vcatpvfn(error, fmt, strlen(fmt), ap_ptr, (SV **)0, 0, &junk);
40  
41 1 error_data = newHV();
42 1 ctx->error_data = newRV_noinc((SV *)error_data);
43  
44 1 IGNORE_RV(hv_store(error_data, "version", 7, newSVpvf("%s", MOD_VERSION), 0));
45  
46 1 return error;
47 }
48  
49  
50 #if defined(JSONEVT_HAVE_FULL_VARIADIC_MACROS)
51  
52 static SV *
53 json_encode_error(self_context * ctx, const char * file, int line_num, const char * fmt, ...) {
54 va_list ap;
55 SV * error;
56
57 va_start(ap, fmt);
58 error = vjson_encode_error(ctx, file, line_num, fmt, &ap);
59 va_end(ap);
60  
61 return error;
62 }
63  
64  
65 #if JSON_DO_EXTENDED_ERRORS
66  
67  
68 #define JSON_ENCODE_ERROR(ctx, ...) json_encode_error(ctx, __FILE__, __LINE__, __VA_ARGS__)
69  
70 #else
71  
72 #define JSON_ENCODE_ERROR(ctx, ...) json_encode_error(ctx, NULL, 0, __VA_ARGS__)
73  
74 #endif
75  
76 #else
77  
78 static SV *
79 1 JSON_ENCODE_ERROR(self_context * ctx, const char * fmt, ...) {
80 va_list ap;
81 SV * error;
82  
83 1 va_start(ap, fmt);
84 1 error = vjson_encode_error(ctx, NULL, 0, fmt, &ap);
85 1 va_end(ap);
86  
87 1 return error;
88 }
89  
90 #endif
91  
92 #if DEBUG_UTF8
93 static STRLEN
94 print_hex(FILE * fp, const unsigned char * buf, STRLEN buf_len) {
95 STRLEN i;
96 UV c;
97  
98 for (i = 0; i < buf_len; i++) {
99 c = buf[i];
100 if (c & 0x80) {
101 fprintf(fp, "\\x{%02"UVxf"}", c);
102 }
103 else {
104 fwrite(&buf[i], 1, 1, fp);
105 }
106 }
107  
108 return i;
109 }
110  
111 static STRLEN
112 print_hex_line(FILE * fp, const unsigned char * buf, STRLEN buf_len) {
113 STRLEN i = print_hex(fp, buf, buf_len);
114
115 fwrite("\n", 1, 1, fp);
116 i++;
117  
118 return i;
119 }
120 #endif
121  
122  
123 static SV * to_json(self_context * self, SV * data_ref, int indent_level, unsigned int cur_level);
124 static SV * get_ref_addr(SV * ref);
125  
126  
127 #define JsSvLen(val) sv_len(val)
128  
129 #define JsDumpSv(sv, flags) if (flags & kDumpVars) { sv_dump(sv); }
130  
131 /*
132 static SV *
133 from_json_sv (SV * self, SV * data_sv, SV ** error_msg, int *throw_exception,
134 SV * error_data_ref, SV * stats_data_ref) {
135 STRLEN data_str_len;
136 char * data_str;
137  
138 data_str = SvPV(data_sv, data_str_len);
139  
140 return from_json(self, data_str, data_str_len, error_msg, throw_exception, error_data_ref,
141 stats_data_ref);
142 }
143 */
144  
145 static SV *
146 13 has_jsonevt() {
147 #ifdef HAVE_JSONEVT
148 13 return newSVuv(1);
149 #else
150 return newSV(0);
151 #endif
152 }
153  
154 static SV *
155 148 deserialize_json(SV * self, char * data_str, STRLEN data_str_len) {
156 SV * val;
157  
158 148 ▶ UNLESS (data_str) {
Branch: 1 branch
BranchTrueFalseTotal
UNLESS (data_str) { 0 148 148
159 /* return undef */
160 0 return (SV *)&PL_sv_undef;
161 }
162  
163 148 ▶ if (data_str_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_str_len == 0) { 0 148 148
164 /* return empty string */
165 0 val = newSVpv("", 0);
166 0 return val;
167 }
168
169 148 val = do_json_parse_buf(self, data_str, data_str_len);
170  
171 142 return (SV *)val;
172 }
173  
174 static SV *
175 148 deserialize_json_sv (SV * self, SV * data_sv) {
176 STRLEN data_str_len;
177 char * data_str;
178  
179 148 data_str = SvPV(data_sv, data_str_len);
180  
181 148 return deserialize_json(self, data_str, data_str_len);
182 }
183  
184 /*
185 static int
186 get_unicode_char_count(SV * self, U8 *c_str, STRLEN len) {
187 STRLEN i;
188 U32 count = 0;
189  
190 for (i = 0; i < len; i++) {
191 if (! UTF8_IS_INVARIANT(c_str[i])) {
192 len = UTF8SKIP(&c_str[i]);
193 i += len - 1;
194 count++;
195 }
196 }
197  
198 return count;
199 }
200 */
201  
202 #if 0
203 static SV *
204 parse_json_file(SV * self, SV * file, SV * error_msg_ref) {
205 SV * rv;
206 SV * error_msg;
207 SV * passed_error_msg_sv;
208 int throw_exception = 0;
209 char * data;
210 STRLEN data_len;
211 char * filename;
212 char * filename_len;
213 FILE * fp;
214  
215 filename = SvPV(file, filename_len);
216 if (! filename || ! (fp = fopen(filename, "r")) ) {
217 /* FIXME: put a good error msg here */
218 return &PL_sv_undef;
219 }
220  
221
222  
223 /* FIXME: read from file here */
224  
225 error_msg = (SV *)&PL_sv_undef;
226 rv = from_json(self, data, data_len, &error_msg, &throw_exception);
227 if (SvOK(error_msg) && SvROK(error_msg_ref)) {
228 passed_error_msg_sv = SvRV(error_msg_ref);
229 sv_setsv(passed_error_msg_sv, error_msg);
230 }
231  
232 return rv;
233 }
234 #endif
235  
236 static char *
237 1 _safe_dup_buf(char *buf, uint32_t buf_len) {
238 1 char *dest = (char *)malloc(buf_len + 1);
239  
240 1 memcpy(dest, buf, buf_len);
241 1 dest[buf_len] = 0;
242  
243 1 return dest;
244 }
245  
246 static SV *
247 118 escape_json_str(self_context * self, SV * sv_str) {
248 U8 * data_str;
249 STRLEN data_str_len;
250 118 STRLEN needed_len = 0;
251 118 STRLEN sv_pos = 0;
252 118 uint32_t len = 0;
253 118 U8 tmp_char = 0x00;
254 SV * rv;
255 118 UV this_uv = 0;
256 U8 unicode_bytes[5];
257 118 int escape_unicode = 0;
258 118 int pass_bad_char = 0;
259 118 uint32_t len32 = 0;
260 118 char *err_str = Nullch;
261  
262 118 memzero(unicode_bytes, 5); /* memzero macro provided by Perl */
263  
264 118 ▶ UNLESS (SvOK(sv_str)) {
Branch: 1 branch
BranchTrueFalseTotal
UNLESS (SvOK(sv_str)) { 0 118 118
265 0 return newSVpv("null", 4);
266 }
267  
268 118 data_str = (U8 *)SvPV(sv_str, data_str_len);
269 118 ▶ UNLESS (data_str) {
Branch: 1 branch
BranchTrueFalseTotal
UNLESS (data_str) { 0 118 118
270 0 return newSVpv("null", 4);
271 }
272  
273 118 self->string_count++;
274  
275 118 ▶ if (data_str_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_str_len == 0) { 3 115 118
276 /* empty string */
277 3 return newSVpv("\"\"", 2);
278 }
279  
280 115 ▶ if (self->flags & kEscapeMultiByte) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kEscapeMultiByte) { 4 111 115
281 4 escape_unicode = 1;
282 }
283  
284 /* get a better estimate of needed buffer size */
285 115 needed_len = data_str_len * 2 + 2;
286  
287 115 rv = newSV(needed_len);
288 115 SvUTF8_on(rv);
289 115 sv_setpvn(rv, "\"", 1);
290  
291 /* printf("\tencoding string %s\n", data_str); */
292
293 #if DEBUG_UTF8
294 fprintf(stderr, "\tencoding string ");
295 print_hex_line(stderr, data_str, data_str_len);
296 /* if (data_str[0] == 0xe4) { */
297 sv_dump(sv_str);
298 /* } */
299 fprintf(stderr, "==========\n");
300 #endif
301
302 838 ▶ for (sv_pos = 0; sv_pos < data_str_len; sv_pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (sv_pos = 0; sv_pos < data_str_len; sv_pos++) { 724 114 838
303 724 pass_bad_char = 0;
304  
305 /* this_uv = convert_utf8_to_uv(&data_str[sv_pos], &len); */
306 724 this_uv = (UV)utf8_bytes_to_unicode((uint8_t *)(&data_str[sv_pos]), data_str_len - sv_pos, &len);
307
308 724 ▶ if (len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { 3 721 724
309 3 len = 1;
310  
311 3 ▶ UNLESS (self->bad_char_policy) {
Branch: 1 branch
BranchTrueFalseTotal
UNLESS (self->bad_char_policy) { 1 2 3
312 /* default */
313 1 this_uv = (UV)data_str[sv_pos];
314 1 ▶ if (data_str_len < 40) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_str_len < 40) { 1 0 1
315 1 err_str = _safe_dup_buf((char *)data_str, data_str_len);
316 1 self->error = JSON_ENCODE_ERROR(self,
317 "bad utf8 sequence starting with %#02"UVxf" - %s",
318 this_uv, (char *)data_str);
319 1 free(err_str);
320 }
321 else {
322 0 self->error = JSON_ENCODE_ERROR(self,
323 "bad utf8 sequence starting with %#02"UVxf, this_uv);
324 }
325
326 1 sv_catpvn(rv, "\"", 1);
327 1 return rv;
328 }
329 2 ▶ else if (self->bad_char_policy & kBadCharConvert) {
Branch: 1 branch
BranchTrueFalseTotal
else if (self->bad_char_policy & kBadCharConvert) { 2 0 2
330 2 this_uv = (UV)data_str[sv_pos];
331 }
332 0 ▶ else if (self->bad_char_policy & kBadCharPassThrough) {
Branch: 1 branch
BranchTrueFalseTotal
else if (self->bad_char_policy & kBadCharPassThrough) { 0 0 0
333 0 this_uv = (UV)data_str[sv_pos];
334 0 pass_bad_char = 1;
335 }
336 }
337
338 723 sv_pos += len - 1;
339  
340 723 switch (this_uv) {
341 3 case '\\':
342 3 sv_catpvn(rv, "\\\\", 2);
343 3 break;
344 8 case '"':
345 8 sv_catpvn(rv, "\\\"", 2);
346 8 break;
347 /*
348 case '\'':
349 sv_catpvn(rv, "\\'", 2);
350 break;
351 */
352  
353 15 case '/':
354 15 ▶ if (self->flags & (kBareSolidus | kMinimalEscaping)) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & (kBareSolidus | kMinimalEscaping)) { 5 10 15
355 5 sv_catpvn(rv, "/", 1);
356 }
357 else {
358 10 sv_catpvn(rv, "\\/", 2);
359 }
360  
361 15 break;
362
363 1 case 0x08:
364 1 ▶ if (self->flags & kMinimalEscaping) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kMinimalEscaping) { 1 0 1
365 1 sv_catpvn(rv, "\x08", 1);
366 }
367 else {
368 0 sv_catpvn(rv, "\\b", 2);
369 }
370 1 break;
371
372 1 case 0x0c:
373 1 ▶ if (self->flags & kMinimalEscaping) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kMinimalEscaping) { 1 0 1
374 1 sv_catpvn(rv, "\x0c", 1);
375 }
376 else {
377 0 sv_catpvn(rv, "\\f", 2);
378 }
379 1 break;
380
381 2 case 0x0a:
382 2 ▶ if (self->flags & kMinimalEscaping) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kMinimalEscaping) { 1 1 2
383 1 sv_catpvn(rv, "\x0a", 1);
384 }
385 else {
386 1 sv_catpvn(rv, "\\n", 2);
387 }
388 2 break;
389
390 1 case 0x0d:
391 1 ▶ if (self->flags & kMinimalEscaping) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kMinimalEscaping) { 1 0 1
392 1 sv_catpvn(rv, "\x0d", 1);
393 }
394 else {
395 0 sv_catpvn(rv, "\\r", 2);
396 }
397 1 break;
398
399 1 case 0x09:
400 1 ▶ if (self->flags & kMinimalEscaping) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kMinimalEscaping) { 1 0 1
401 1 sv_catpvn(rv, "\x09", 1);
402 }
403 else {
404 0 sv_catpvn(rv, "\\t", 2);
405 }
406 1 break;
407
408 691 default:
409 691 ▶ if (this_uv < 0x1f) {
Branch: 1 branch
BranchTrueFalseTotal
if (this_uv < 0x1f) { 0 691 691
410 0 sv_catpvf(rv, "\\u%04"UVxf, this_uv);
411 }
412 691 ▶ else if (escape_unicode && ! UTF8_IS_INVARIANT(this_uv)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (escape_unicode && ! UTF8_IS_INVARIANT(this_uv)) { 28 663 691
else if (escape_unicode && ! UTF8_IS_INVARIANT(this_uv)) { 2 26 28
413 2 sv_catpvf(rv, "\\u%04"UVxf, this_uv);
414 }
415 689 ▶ else if (!pass_bad_char) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!pass_bad_char) { 689 0 689
416 689 len32 = common_utf8_unicode_to_bytes((uint32_t)this_uv, (uint8_t *)unicode_bytes);
417 689 ▶ if (len32 > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (len32 > 1) { 1 688 689
418 1 SvUTF8_on(rv);
419 }
420 689 sv_catpvn(rv, (char *)unicode_bytes, len32);
421 }
422 else {
423 0 tmp_char = (U8)this_uv;
424 0 sv_catpvn(rv, (char *)&tmp_char, 1);
425 }
426  
427 691 break;
428 }
429 }
430
431 114 sv_catpvn(rv, "\"", 1);
432
433 114 return rv;
434 }
435  
436 static SV *
437 9 encode_array(self_context * self, AV * array, int indent_level, unsigned int cur_level) {
438 9 SV * rsv = NULL;
439 9 SV * tmp_sv = NULL;
440 9 I32 max_i = av_len(array); /* max index, not length */
441 I32 i;
442 I32 j;
443 9 SV ** element = NULL;
444 9 I32 num_spaces = 0;
445 9 MAGIC * magic_ptr = NULL;
446  
447 9 ▶ JsDumpSv((SV *)array, self->flags);
Branch: 1 branch
BranchTrueFalseTotal
JsDumpSv((SV *)array, self->flags); 0 9 9
448  
449 9 cur_level++;
450 9 ▶ UPDATE_CUR_LEVEL(self, cur_level);
Branch: 1 branch
BranchTrueFalseTotal
UPDATE_CUR_LEVEL(self, cur_level); 7 2 9
451  
452 9 self->array_count++;
453  
454 9 ▶ if (self->flags & kPrettyPrint) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kPrettyPrint) { 2 7 9
455 2 ▶ if (indent_level == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (indent_level == 0) { 0 2 2
456 0 rsv = newSVpv("[", 1);
457 }
458 else {
459 2 num_spaces = indent_level * 4;
460 2 rsv = newSV(num_spaces + 3);
461 2 sv_setpvn(rsv, "\n", 1);
462 18 ▶ for (i = 0; i < num_spaces; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_spaces; i++) { 16 2 18
463 16 sv_catpvn(rsv, " ", 1);
464 }
465 2 sv_catpvn(rsv, "[", 1);
466 }
467 }
468 else {
469 7 rsv = newSVpv("[", 1);
470 }
471  
472 9 num_spaces = (indent_level + 1) * 4;
473  
474 9 magic_ptr = mg_find((SV *)array, PERL_MAGIC_tied);
475  
476 34 ▶ for (i = 0; i <= max_i; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= max_i; i++) { 25 9 34
477 25 element = av_fetch(array, i, 0);
478 25 ▶ if (element && *element) {
Branch: 2 branches
BranchTrueFalseTotal
if (element && *element) { 25 0 25
if (element && *element) { 25 0 25
479 25 ▶ if (self->flags & kDumpVars) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kDumpVars) { 0 25 25
480 0 fprintf(stderr, "array element:\n");
481 }
482  
483 /* need to call mg_get(val) to get the actual value if this is a tied array */
484 /* see sv_magic */
485 25 ▶ if (magic_ptr || SvTYPE(*element) == SVt_PVMG) {
Branch: 2 branches
BranchTrueFalseTotal
if (magic_ptr || SvTYPE(*element) == SVt_PVMG) { 17 8 25
if (magic_ptr || SvTYPE(*element) == SVt_PVMG) { 0 17 17
486 /* mg_get(*element); */ /* causes assertion failure in perl 5.8.5 if tied scalar */
487 8 SvGETMAGIC(*element);
488 }
489  
490 25 tmp_sv = to_json(self, *element, indent_level + 1, cur_level);
491  
492 25 ▶ if (self->flags & kPrettyPrint) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kPrettyPrint) { 6 19 25
493 6 sv_catpvn(rsv, "\n", 1);
494 78 ▶ for (j = 0; j < num_spaces; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < num_spaces; j++) { 72 6 78
495 72 sv_catpvn(rsv, " ", 1);
496 }
497 }
498  
499 25 sv_catsv(rsv, tmp_sv);
500 25 SvREFCNT_dec(tmp_sv);
501 25 ▶ if (self->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->error) { 0 25 25
502 0 SvREFCNT_dec(rsv);
503 0 return (SV *)&PL_sv_undef;
504 }
505 25 tmp_sv = NULL;
506 }
507 else {
508 /* error? */
509 0 sv_catpvn(rsv, "null", 4);
510 }
511  
512 25 ▶ if (i != max_i) {
Branch: 1 branch
BranchTrueFalseTotal
if (i != max_i) { 16 9 25
513 16 sv_catpvn(rsv, ",", 1);
514 }
515 }
516  
517 9 ▶ if (self->flags & kPrettyPrint) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kPrettyPrint) { 2 7 9
518 2 sv_catpvn(rsv, "\n", 1);
519 2 num_spaces = indent_level * 4;
520 18 ▶ for (j = 0; j < num_spaces; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < num_spaces; j++) { 16 2 18
521 16 sv_catpvn(rsv, " ", 1);
522 }
523 }
524 9 sv_catpvn(rsv, "]", 1);
525  
526 9 return rsv;
527 }
528  
529 static void
530 51 setup_self_context(SV *self_sv, self_context *self) {
531 51 SV ** ptr = NULL;
532 51 SV * self_hash = NULL;
533  
534 51 memzero((void *)self, sizeof(self_context));
535  
536 51 ▶ UNLESS (SvROK(self_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
UNLESS (SvROK(self_sv)) { 0 51 51
537 /* hmmm, this should always be a reference */
538 0 return;
539 }
540
541 51 self_hash = SvRV(self_sv);
542  
543 /* HvUSEDKEYS(hv) */
544 /* HvKEYS(hv) */
545 51 ▶ if (HvKEYS(self_hash) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (HvKEYS(self_hash) == 0) { 0 51 51
if (HvKEYS(self_hash) == 0) { 18 33 51
546 /* empty hash, so return early */
547 18 return;
548 }
549  
550 33 ptr = hv_fetch((HV *)self_hash, "bare_keys", 9, 0);
551 33 ▶ if (ptr && SvTRUE(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr && SvTRUE(*ptr)) { 3 30 33
if (ptr && SvTRUE(*ptr)) { 3 0 3
552 3 self->bare_keys = 1;
553 }
554  
555 33 ptr = hv_fetch((HV *)self_hash, "use_exceptions", 14, 0);
556 33 ▶ if (ptr && SvTRUE(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr && SvTRUE(*ptr)) { 1 32 33
if (ptr && SvTRUE(*ptr)) { 1 0 1
557 1 self->flags |= kUseExceptions;
558 }
559  
560 33 self->bad_char_policy = get_bad_char_policy((HV *)self_hash);
561  
562 33 ptr = hv_fetch((HV *)self_hash, "dump_vars", 9, 0);
563 33 ▶ if (ptr && SvTRUE(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr && SvTRUE(*ptr)) { 0 33 33
if (ptr && SvTRUE(*ptr)) { 0 0 0
564 0 self->flags |= kDumpVars;
565 }
566  
567 33 ptr = hv_fetch((HV *)self_hash, "pretty", 6, 0);
568 33 ▶ if (ptr && SvTRUE(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr && SvTRUE(*ptr)) { 2 31 33
if (ptr && SvTRUE(*ptr)) { 2 0 2
569 2 self->flags |= kPrettyPrint;
570 }
571  
572 33 ptr = hv_fetch((HV *)self_hash, "escape_multi_byte", 17, 0);
573 33 ▶ if (ptr && SvTRUE(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr && SvTRUE(*ptr)) { 1 32 33
if (ptr && SvTRUE(*ptr)) { 1 0 1
574 1 self->flags |= kEscapeMultiByte;
575 }
576  
577 33 ptr = hv_fetch((HV *)self_hash, "ascii", 5, 0);
578 33 ▶ if (ptr && SvTRUE(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr && SvTRUE(*ptr)) { 1 32 33
if (ptr && SvTRUE(*ptr)) { 1 0 1
579 1 self->flags |= kEscapeMultiByte;
580 }
581  
582 33 ptr = hv_fetch((HV *)self_hash, "detect_circular_refs", 20, 0);
583 33 ▶ if (ptr && SvTRUE(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr && SvTRUE(*ptr)) { 2 31 33
if (ptr && SvTRUE(*ptr)) { 2 0 2
584 2 self->ref_track = newHV();
585 }
586  
587 33 ptr = hv_fetch((HV *)self_hash, "bare_solidus", 12, 0);
588 33 ▶ if (ptr && SvTRUE(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr && SvTRUE(*ptr)) { 1 32 33
if (ptr && SvTRUE(*ptr)) { 1 0 1
589 1 self->flags |= kBareSolidus;
590 }
591  
592 33 ptr = hv_fetch((HV *)self_hash, "minimal_escaping", 16, 0);
593 33 ▶ if (ptr && SvTRUE(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr && SvTRUE(*ptr)) { 1 32 33
if (ptr && SvTRUE(*ptr)) { 1 0 1
594 1 self->flags |= kMinimalEscaping;
595 }
596  
597 33 ptr = hv_fetch((HV *)self_hash, "sort_keys", 9, 0);
598 33 ▶ if (ptr && SvTRUE(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr && SvTRUE(*ptr)) { 2 31 33
if (ptr && SvTRUE(*ptr)) { 2 0 2
599 2 self->flags |= kSortKeys;
600 }
601  
602  
603 #if JSON_DUMP_OPTIONS
604 {
605 char * char_policy = NULL;
606 switch (self->bad_char_policy) {
607 case kBadCharError:
608 char_policy = "error";
609 break;
610  
611 case kBadCharConvert:
612 char_policy = "convert";
613 break;
614  
615 case kBadCharPassThrough:
616 char_policy = "pass_through";
617 break;
618  
619 default:
620 char_policy = "unrecognized bad_char policy";
621 break;
622 }
623  
624 fprintf(stderr, "\nBad char policy: %s\n", char_policy);
625  
626 if (self->flags & kUseExceptions) {
627 fprintf(stderr, "Use Exceptions\n");
628 }
629
630 if (self->flags & kDumpVars) {
631 fprintf(stderr, "Dump Vars\n");
632 }
633  
634 if (self->flags & kPrettyPrint) {
635 fprintf(stderr, "Pretty Print\n");
636 }
637  
638 if (self->flags & kEscapeMultiByte) {
639 fprintf(stderr, "Escape Multi-Byte Characters\n");
640 }
641  
642 if (self-flags & kBareSolidus) {
643 fprintf(stderr, "Don't escape solidus ('/')\n");
644 }
645
646 fprintf(stderr, "\n");
647 fflush(stderr);
648 }
649 #endif
650  
651 }
652  
653 static int
654 66 hash_key_can_be_bare(self_context * self, const char *key, STRLEN key_len) {
655 U8 this_byte;
656 STRLEN i;
657  
658 66 ▶ UNLESS (self->bare_keys) {
Branch: 1 branch
BranchTrueFalseTotal
UNLESS (self->bare_keys) { 63 3 66
659 63 return 0;
660 }
661  
662 /* Only allow if 7-bit ascii, so use byte semantics, and only
663 allow if alphanumeric and '_'.
664 */
665 15 ▶ for (i = 0; i < key_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < key_len; i++) { 12 3 15
666 12 this_byte = *key;
667 12 key++;
668 12 ▶ UNLESS (this_byte == '_'
Branch: 7 branches
BranchTrueFalseTotal
UNLESS (this_byte == '_' 12 0 12
UNLESS (this_byte == '_' 9 3 12
UNLESS (this_byte == '_' 9 0 9
UNLESS (this_byte == '_' 9 3 12
UNLESS (this_byte == '_' 0 9 9
UNLESS (this_byte == '_' 3 0 3
UNLESS (this_byte == '_' 0 3 3
669 || (this_byte >= 'A' && this_byte <= 'Z')
670 || (this_byte >= 'a' && this_byte <= 'z')
671 || (this_byte >= '0' && this_byte <= '9')
672 ) {
673 0 return 0;
674 }
675 }
676  
677 3 return 1;
678 }
679  
680 static SV *
681 66 _encode_hash_entry(self_context *self, int first, HE * entry, const char *key, I32 key_len,
682 SV *key_sv, SV *val, SV *rsv,
683 int indent_level, unsigned int cur_level) {
684  
685 66 SV * tmp_sv = NULL;
686 66 SV * tmp_sv2 = NULL;
687 66 int i = 0;
688 int num_spaces;
689  
690 66 num_spaces = (indent_level + 1) * 4;
691
692 66 ▶ if (self->flags & kDumpVars) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kDumpVars) { 0 66 66
693 0 fprintf(stderr, "hash key = %s\nval:\n", key);
694 }
695
696 66 ▶ if (self->flags & kPrettyPrint) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kPrettyPrint) { 12 54 66
697 12 sv_catpvn(rsv, "\n", 1);
698 92 ▶ for (i = 0; i < num_spaces; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_spaces; i++) { 80 12 92
699 80 sv_catpvn(rsv, " ", 1);
700 }
701 }
702  
703 66 ▶ if (hash_key_can_be_bare(self, key, key_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash_key_can_be_bare(self, key, key_len)) { 3 63 66
704 /* if the key can be bare, then it cannot have any hi-bits
705 set, so no need to upgrade to utf-8
706 */
707 3 sv_catpvn(rsv, (char *)key, key_len);
708 }
709 else {
710 63 tmp_sv = newSVpv((char *)key, key_len);
711  
712 #ifdef IS_PERL_5_8
713 63 ▶ if (HeKWASUTF8(entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if (HeKWASUTF8(entry)) { 3 60 63
714 /* The hash key was utf-8 encoding, but the char * was
715  
716 given to us with as the decoded bytes (e.g., utf-8 =>
717 latin1), so convert back to utf-8
718  
719 */
720 3 sv_utf8_upgrade(tmp_sv);
721 }
722 #endif
723  
724 63 tmp_sv2 = escape_json_str(self, tmp_sv);
725 63 ▶ if (self->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->error) { 0 63 63
726 0 SvREFCNT_dec(tmp_sv);
727 0 SvREFCNT_dec(tmp_sv2);
728 0 SvREFCNT_dec(rsv);
729 0 return (SV *)&PL_sv_no;
730 }
731  
732 63 sv_catsv(rsv, tmp_sv2);
733 63 SvREFCNT_dec(tmp_sv);
734 63 SvREFCNT_dec(tmp_sv2);
735 }
736  
737 66 sv_catpvn(rsv, ":", 1);
738  
739 66 tmp_sv = to_json(self, val, indent_level + 2, cur_level);
740 66 ▶ if (self->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->error) { 1 65 66
741 1 SvREFCNT_dec(tmp_sv);
742 1 SvREFCNT_dec(rsv);
743 1 return (SV *)&PL_sv_no;
744 }
745  
746 65 sv_catsv(rsv, tmp_sv);
747 65 SvREFCNT_dec(tmp_sv);
748  
749 65 return (SV *)&PL_sv_yes;
750 }
751  
752 static SV *
753 45 encode_hash(self_context * self, HV * hash, int indent_level, unsigned int cur_level) {
754 45 SV * rsv = NULL;
755 45 SV * sv = Nullsv;
756 45 SV * key_sv = Nullsv;
757 const char * key;
758 I32 key_len;
759 SV * val;
760 45 int first = 1;
761 int i;
762 45 int num_spaces = 0;
763 45 MAGIC * magic_ptr = NULL;
764 HE * entry;
765 45 SV * success = Nullsv;
766 45 AV * keys = Nullav;
767 45 SV ** svp = (SV **)0;
768 45 STRLEN tmp_strlen = 0;
769 #if PERL_VERSION < 8
770 SV * sort_keys = Nullsv;
771 #endif
772  
773 45 cur_level++;
774 45 ▶ UPDATE_CUR_LEVEL(self, cur_level);
Branch: 1 branch
BranchTrueFalseTotal
UPDATE_CUR_LEVEL(self, cur_level); 45 0 45
775  
776 45 self->hash_count++;
777  
778 45 ▶ if (self->flags & kPrettyPrint) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kPrettyPrint) { 4 41 45
779 4 ▶ if (indent_level == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (indent_level == 0) { 2 2 4
780 2 rsv = newSVpv("{", 1);
781 }
782 else {
783 2 num_spaces = indent_level * 4;
784 2 rsv = newSV(num_spaces + 3);
785 2 sv_setpvn(rsv, "\n", 1);
786 18 ▶ for (i = 0; i < num_spaces; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_spaces; i++) { 16 2 18
787 16 sv_catpvn(rsv, " ", 1);
788 }
789 2 sv_catpvn(rsv, "{", 1);
790  
791 }
792  
793 }
794 else {
795 41 rsv = newSVpv("{", 1);
796 }
797  
798 45 ▶ JsDumpSv((SV *)hash, self->flags);
Branch: 1 branch
BranchTrueFalseTotal
JsDumpSv((SV *)hash, self->flags); 0 45 45
799  
800 45 magic_ptr = mg_find((SV *)hash, PERL_MAGIC_tied);
801
802 45 num_spaces = (indent_level + 1) * 4;
803
804 45 ▶ if (self->flags & kSortKeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kSortKeys) { 2 43 45
805 #if PERL_VERSION < 8
806 /* old-style -- work around not ahveing sortsv() */
807 sort_keys = sv_2mortal(newSVpvn("JSON::DWIW::_sort_keys", 22));
808  
809 /* FIXME: complete for Perl < 5.8 */
810 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
811 XPUSHs(sv_2mortal(newRV_inc((SV *)hash))); PUTBACK;
812 i = call_sv(sort_keys, G_SCALAR | G_EVAL);
813 SPAGAIN;
814 if (i) {
815 sv = POPs;
816 if (SvROK(sv) && (SvTYPE(SvRV(sv)) == SVt_PVAV))
817 keys = (AV*)SvREFCNT_inc(SvRV(sv));
818 }
819 UNLESS (keys) {
820 warn("Sortkeys subroutine did not return ARRAYREF\n");
821 }
822 PUTBACK; FREETMPS; LEAVE;
823  
824 #else
825 2 keys = newAV();
826 2 (void)hv_iterinit(hash);
827 10 ▶ while ((entry = hv_iternext(hash))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(hash))) { 8 2 10
828 8 sv = hv_iterkeysv(entry);
829 8 SvREFCNT_inc(sv);
830 8 av_push(keys, sv);
831 }
832  
833 #ifdef USE_LOCALE_NUMERIC
834 2 ▶ sortsv(AvARRAY(keys), av_len(keys)+1, IN_LOCALE ? Perl_sv_cmp_locale : Perl_sv_cmp);
Branch: 7 branches
BranchTrueFalseTotal
sortsv(AvARRAY(keys), av_len(keys)+1, IN_LOCALE ? Perl_sv_cmp_locale : Perl_sv_cmp); 0 2 2
sortsv(AvARRAY(keys), av_len(keys)+1, IN_LOCALE ? Perl_sv_cmp_locale : Perl_sv_cmp); 0 0 0
sortsv(AvARRAY(keys), av_len(keys)+1, IN_LOCALE ? Perl_sv_cmp_locale : Perl_sv_cmp); 0 0 0
sortsv(AvARRAY(keys), av_len(keys)+1, IN_LOCALE ? Perl_sv_cmp_locale : Perl_sv_cmp); 2 0 2
sortsv(AvARRAY(keys), av_len(keys)+1, IN_LOCALE ? Perl_sv_cmp_locale : Perl_sv_cmp); 0 2 2
sortsv(AvARRAY(keys), av_len(keys)+1, IN_LOCALE ? Perl_sv_cmp_locale : Perl_sv_cmp); 0 0 0
sortsv(AvARRAY(keys), av_len(keys)+1, IN_LOCALE ? Perl_sv_cmp_locale : Perl_sv_cmp); 0 2 2
835 #else
836 sortsv(AvARRAY(keys), av_len(keys)+1, Perl_sv_cmp);
837 #endif
838  
839 #endif
840  
841 10 ▶ for (i = 0; (I32)i <= av_len(keys); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; (I32)i <= av_len(keys); i++) { 8 2 10
842 8 svp = av_fetch(keys, i, FALSE);
843 8 ▶ key_sv = svp ? *svp : sv_mortalcopy(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
key_sv = svp ? *svp : sv_mortalcopy(&PL_sv_undef); 8 0 8
844
845 8 key = SvPV(key_sv, tmp_strlen);
846 8 key_len = tmp_strlen;
847 8 entry = hv_fetch_ent(hash, key_sv, 0, 0);
848 /* key = (unsigned char *)hv_iterkey(entry, &key_len); */
849
850 /*
851 svp = hv_fetch(hash, key, SvUTF8(keysv) ? -key_len : keylen, 0);
852 val = svp ? *svp : sv_mortalcopy(&PL_sv_undef);
853 */
854 8 val = hv_iterval(hash, entry);
855  
856 8 ▶ if (magic_ptr || SvTYPE(val) == SVt_PVMG) {
Branch: 2 branches
BranchTrueFalseTotal
if (magic_ptr || SvTYPE(val) == SVt_PVMG) { 8 0 8
if (magic_ptr || SvTYPE(val) == SVt_PVMG) { 0 8 8
857 /* mg_get(val); */ /* crashes in Perl 5.8.5 if doesn't have "get magic" */
858 0 SvGETMAGIC(val);
859 }
860  
861 8 ▶ UNLESS (first) {
Branch: 1 branch
BranchTrueFalseTotal
UNLESS (first) { 6 2 8
862 6 sv_catpvn(rsv, ",", 1);
863 }
864  
865 /* ref cnt for rsv is decremented in encode_hash_entry() if there is an error */
866 8 success = _encode_hash_entry(self, first, entry, key, key_len, key_sv, val, rsv,
867 indent_level, cur_level);
868  
869 8 ▶ if (success != &PL_sv_yes) {
Branch: 1 branch
BranchTrueFalseTotal
if (success != &PL_sv_yes) { 0 8 8
870 0 SvREFCNT_dec(keys);
871  
872 0 return &PL_sv_undef;
873 }
874  
875 8 first = 0;
876 }
877  
878 2 SvREFCNT_dec(keys); keys = Nullav;
879 }
880 else {
881  
882 /* non-sorted keys */
883 43 hv_iterinit(hash);
884 /* while ( (val = hv_iternextsv(hash, (char **)&key, &key_len)) ) { */
885 while (1) {
886 100 entry = hv_iternext(hash);
887 100 ▶ UNLESS (entry) {
Branch: 1 branch
BranchTrueFalseTotal
UNLESS (entry) { 42 58 100
888 42 break;
889 }
890  
891 /* key_sv = HeSVKEY(entry); */
892 58 key = hv_iterkey(entry, &key_len);
893 /* key = (U8 *)HePV(entry, key_len); */
894 58 val = hv_iterval(hash, entry);
895  
896 /* need to call mg_get(val) to get the actual value if this is a tied hash */
897 /* see sv_magic */
898 58 ▶ if (magic_ptr || SvTYPE(val) == SVt_PVMG) {
Branch: 2 branches
BranchTrueFalseTotal
if (magic_ptr || SvTYPE(val) == SVt_PVMG) { 56 2 58
if (magic_ptr || SvTYPE(val) == SVt_PVMG) { 1 55 56
899 /* mg_get(val); */ /* crashes in Perl 5.8.5 if doesn't have "get magic" */
900 3 SvGETMAGIC(val);
901 }
902  
903 58 ▶ UNLESS (first) {
Branch: 1 branch
BranchTrueFalseTotal
UNLESS (first) { 15 43 58
904 15 sv_catpvn(rsv, ",", 1);
905 }
906  
907 /* ref cnt for rsv is decremented in encode_hash_entry() if there is an error */
908 58 success = _encode_hash_entry(self, first, entry, key, key_len, Nullsv, val, rsv,
909 indent_level, cur_level);
910 58 ▶ if (success != &PL_sv_yes) {
Branch: 1 branch
BranchTrueFalseTotal
if (success != &PL_sv_yes) { 1 57 58
911 1 return &PL_sv_undef;
912 }
913  
914 57 first = 0;
915 }
916 }
917  
918 44 ▶ if (self->flags & kPrettyPrint) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->flags & kPrettyPrint) { 4 40 44
919 4 sv_catpvn(rsv, "\n", 1);
920 4 num_spaces = indent_level * 4;
921 20 ▶ for (i = 0; i < num_spaces; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_spaces; i++) { 16 4 20
922 16 sv_catpvn(rsv, " ", 1);
923 }
924 }
925 44 sv_catpvn(rsv, "}", 1);
926  
927 44 return rsv;
928 }
929  
930 #if 0
931 static SV *
932 old_encode_hash(self_context * self, HV * hash, int indent_level, unsigned int cur_level) {
933  
934 SV * rsv = NULL;
935 SV * tmp_sv = NULL;
936 SV * tmp_sv2 = NULL;
937 U8 * key;
938 I32 key_len;
939 SV * val;
940 int first = 1;
941 int i;
942 int num_spaces = 0;
943 MAGIC * magic_ptr = NULL;
944 HE * entry;
945 /* SV * key_sv = NULL; */
946  
947  
948 if (self->flags & kSortKeys) {
949 return encode_hash2(self, hash, indent_level, cur_level);
950 }
951  
952 cur_level++;
953 UPDATE_CUR_LEVEL(self, cur_level);
954  
955 self->hash_count++;
956  
957 if (self->flags & kPrettyPrint) {
958 if (indent_level == 0) {
959 rsv = newSVpv("{", 1);
960 }
961 else {
962 num_spaces = indent_level * 4;
963 rsv = newSV(num_spaces + 3);
964 sv_setpvn(rsv, "\n", 1);
965 for (i = 0; i < num_spaces; i++) {
966 sv_catpvn(rsv, " ", 1);
967 }
968 sv_catpvn(rsv, "{", 1);
969  
970 }
971  
972 }
973 else {
974 rsv = newSVpv("{", 1);
975 }
976  
977 JsDumpSv((SV *)hash, self->flags);
978  
979 magic_ptr = mg_find((SV *)hash, PERL_MAGIC_tied);
980
981 num_spaces = (indent_level + 1) * 4;
982  
983 /* non-sorted keys */
984 hv_iterinit(hash);
985 /* while ( (val = hv_iternextsv(hash, (char **)&key, &key_len)) ) { */
986 while (1) {
987 entry = hv_iternext(hash);
988 UNLESS (entry) {
989 break;
990 }
991  
992 /* key_sv = HeSVKEY(entry); */
993 key = (unsigned char *)hv_iterkey(entry, &key_len);
994 /* key = (U8 *)HePV(entry, key_len); */
995 val = hv_iterval(hash, entry);
996  
997 UNLESS (first) {
998 sv_catpvn(rsv, ",", 1);
999 }
1000  
1001 first = 0;
1002  
1003 /* need to call mg_get(val) to get the actual value if this is a tied hash */
1004 /* see sv_magic */
1005 if (magic_ptr || SvTYPE(val) == SVt_PVMG) {
1006 /* mg_get(val); */ /* crashes in Perl 5.8.5 if doesn't have "get magic" */
1007 SvGETMAGIC(val);
1008 }
1009  
1010 if (self->flags & kDumpVars) {
1011 fprintf(stderr, "hash key = %s\nval:\n", key);
1012 }
1013
1014 if (self->flags & kPrettyPrint) {
1015 sv_catpvn(rsv, "\n", 1);
1016 for (i = 0; i < num_spaces; i++) {
1017 sv_catpvn(rsv, " ", 1);
1018 }
1019 }
1020  
1021 if (hash_key_can_be_bare(self, key, key_len)) {
1022 /* if the key can be bare, then it cannot have any hi-bits
1023 set, so no need to upgrade to utf-8
1024 */
1025 sv_catpvn(rsv, (char *)key, key_len);
1026 }
1027 else {
1028 tmp_sv = newSVpv((char *)key, key_len);
1029  
1030 #ifdef IS_PERL_5_8
1031 if (HeKWASUTF8(entry)) {
1032 /* The hash key was utf-8 encoding, but the char * was
1033  
1034 given to us with as the decoded bytes (e.g., utf-8 =>
1035 latin1), so convert back to utf-8
1036  
1037 */
1038 sv_utf8_upgrade(tmp_sv);
1039 }
1040 #endif
1041  
1042 tmp_sv2 = escape_json_str(self, tmp_sv);
1043 if (self->error) {
1044 SvREFCNT_dec(tmp_sv);
1045 SvREFCNT_dec(tmp_sv2);
1046 SvREFCNT_dec(rsv);
1047 return (SV *)&PL_sv_undef;
1048 }
1049  
1050 sv_catsv(rsv, tmp_sv2);
1051 SvREFCNT_dec(tmp_sv);
1052 SvREFCNT_dec(tmp_sv2);
1053 }
1054  
1055 sv_catpvn(rsv, ":", 1);
1056  
1057 tmp_sv = to_json(self, val, indent_level + 2, cur_level);
1058 if (self->error) {
1059 SvREFCNT_dec(tmp_sv);
1060 SvREFCNT_dec(rsv);
1061 return (SV *)&PL_sv_undef;
1062 }
1063  
1064 sv_catsv(rsv, tmp_sv);
1065 SvREFCNT_dec(tmp_sv);
1066 }
1067  
1068 if (self->flags & kPrettyPrint) {
1069 sv_catpvn(rsv, "\n", 1);
1070 num_spaces = indent_level * 4;
1071 for (i = 0; i < num_spaces; i++) {
1072 sv_catpvn(rsv, " ", 1);
1073 }
1074 }
1075 sv_catpvn(rsv, "}", 1);
1076  
1077 return rsv;
1078 }
1079 #endif
1080  
1081 static SV *
1082 142 to_json(self_context * self, SV * data_ref, int indent_level, unsigned int cur_level) {
1083 SV * data;
1084 int type;
1085 142 SV * rsv = newSVpv("", 0);
1086 142 SV * tmp = NULL;
1087 142 STRLEN before_len = 0;
1088 142 U8 * data_str = NULL;
1089 142 STRLEN start = 0;
1090 142 STRLEN len = 0;
1091 142 SV * ref_tmp = NULL;
1092 142 IV int_val = 0;
1093 142 UV uint_val = 0;
1094 /*
1095 NV float_val = 0;
1096 STRLEN pvlen = 0;
1097 */
1098  
1099 142 ▶ JsDumpSv(data_ref, self->flags);
Branch: 1 branch
BranchTrueFalseTotal
JsDumpSv(data_ref, self->flags); 0 142 142
1100  
1101 142 ▶ UNLESS (SvROK(data_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
UNLESS (SvROK(data_ref)) { 76 66 142
1102 76 data = data_ref;
1103 76 ▶ if (SvOK(data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(data)) { 73 3 76
1104  
1105  
1106 /* scalar */
1107 73 type = SvTYPE(data);
1108 73 switch (type) {
1109 0 case SVt_NULL:
1110 /* undef? */
1111 0 sv_setpvn(rsv, "null", 4);
1112 0 return rsv;
1113 break;
1114  
1115 18 case SVt_IV:
1116 case SVt_NV:
1117 18 before_len = JsSvLen(rsv);
1118  
1119 18 ▶ if (type == SVt_IV) {
Branch: 1 branch
BranchTrueFalseTotal
if (type == SVt_IV) { 18 0 18
1120 18 ▶ if (SvIsUV(data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(data)) { 0 18 18
1121 0 uint_val = SvUVX(data);
1122 0 sv_catpvf(rsv, "%"UVuf, uint_val);
1123 }
1124 else {
1125 18 int_val = SvIVX(data);
1126 18 sv_catpvf(rsv, "%"IVdf, int_val);
1127 }
1128
1129 }
1130 else {
1131 0 tmp = newSVsv(data);
1132 0 sv_catsv(rsv, tmp);
1133 0 SvREFCNT_dec(tmp);
1134 /*
1135 float_val = SvNVX(data);
1136 sv_catpvf(rsv, "%"NVgf, float_val);
1137 */
1138 }
1139  
1140 18 self->number_count++;
1141  
1142 18 ▶ if (JsSvLen(rsv) == before_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (JsSvLen(rsv) == before_len) { 0 18 18
1143 0 sv_catpvn(rsv, "\"\"", 2);
1144 }
1145 18 return rsv;
1146 break;
1147  
1148 40 case SVt_PV:
1149 40 sv_catsv(rsv, data);
1150 40 tmp = rsv;
1151 40 rsv = escape_json_str(self, tmp);
1152 40 SvREFCNT_dec(tmp);
1153 40 return rsv; /* this works for the error case as well */
1154 break;
1155
1156 3 case SVt_PVIV:
1157 case SVt_PVNV:
1158 3 sv_catsv(rsv, data);
1159 3 tmp = rsv;
1160 3 rsv = escape_json_str(self, tmp);
1161 3 SvREFCNT_dec(tmp);
1162 3 return rsv;
1163 break;
1164  
1165 8 case SVt_PVLV:
1166 8 sv_catsv(rsv, data);
1167 8 tmp = rsv;
1168 8 rsv = escape_json_str(self, tmp);
1169 8 SvREFCNT_dec(tmp);
1170 8 return rsv;
1171 break;
1172  
1173 4 default:
1174 /* now what? */
1175 4 sv_catsv(rsv, data);
1176 4 tmp = rsv;
1177 4 rsv = escape_json_str(self, tmp);
1178 4 SvREFCNT_dec(tmp);
1179 4 return rsv;
1180 break;
1181 }
1182 }
1183 else {
1184 /* undef */
1185 3 sv_setpvn(rsv, "null", 4);
1186 3 return rsv;
1187 }
1188 }
1189  
1190 66 ▶ if (self->ref_track) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ref_track) { 6 60 66
1191 6 ref_tmp = get_ref_addr(data_ref);
1192 6 ▶ if (hv_exists_ent(self->ref_track, ref_tmp, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(self->ref_track, ref_tmp, 0)) { 2 4 6
1193 2 SvREFCNT_dec(ref_tmp);
1194 /* return a stringified version */
1195 2 sv_catpvn(rsv, "\"circular ref: ", 15);
1196 2 sv_catsv(rsv, data_ref);
1197 2 sv_catpvn(rsv, "\"", 1);
1198 2 return rsv;
1199 }
1200 else {
1201 4 IGNORE_RV(hv_store_ent(self->ref_track, ref_tmp, newSV(0), 0));
1202 4 SvREFCNT_dec(ref_tmp);
1203 }
1204 }
1205  
1206 64 ▶ if (sv_isobject(data_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(data_ref)) { 10 54 64
1207 10 ▶ if (sv_isa(data_ref, "JSON::DWIW::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isa(data_ref, "JSON::DWIW::Boolean")) { 6 4 10
1208 6 ▶ if (SvTRUE(data_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(data_ref)) { 3 3 6
1209 3 sv_setpvn(rsv, "true", 4);
1210 3 self->bool_count++;
1211 3 return rsv;
1212 }
1213 else {
1214 3 sv_setpvn(rsv, "false", 5);
1215 3 self->bool_count++;
1216 3 return rsv;
1217 }
1218 }
1219 4 ▶ else if (sv_derived_from(data_ref, "Math::BigInt")
Branch: 1 branch
BranchTrueFalseTotal
else if (sv_derived_from(data_ref, "Math::BigInt") 0 4 4
1220 0 ▶ || sv_derived_from(data_ref, "Math::BigFloat")) {
Branch: 1 branch
BranchTrueFalseTotal
|| sv_derived_from(data_ref, "Math::BigFloat")) { 0 0 0
1221 4 tmp = newSVpv("", 0);
1222 4 sv_catsv(tmp, data_ref);
1223 4 data_str = (U8 *)SvPV(tmp, before_len);
1224  
1225 4 ▶ if (before_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (before_len > 0) { 4 0 4
1226 4 start = 0;
1227 4 len = before_len;
1228 4 ▶ if (data_str[0] == '+') {
Branch: 1 branch
BranchTrueFalseTotal
if (data_str[0] == '+') { 0 4 4
1229 0 start++;
1230 0 len--;
1231 }
1232  
1233 4 ▶ if (data_str[before_len - 1] == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (data_str[before_len - 1] == '.') { 0 4 4
1234 0 len--;
1235 }
1236  
1237 4 sv_catpvn(rsv, (char *)data_str + start, len);
1238  
1239 }
1240 else {
1241 0 sv_setpvn(rsv, "\"\"", 2);
1242 }
1243  
1244 4 SvREFCNT_dec(tmp);
1245  
1246 4 return rsv;
1247 }
1248 }
1249
1250 54 data = SvRV(data_ref);
1251 54 ▶ if (SvROK(data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(data)) { 0 54 54
1252 /* reference to a referrence */
1253 0 sv_catsv(rsv, data_ref);
1254 0 tmp = rsv;
1255 0 rsv = escape_json_str(self, tmp);
1256 0 SvREFCNT_dec(tmp);
1257
1258 0 return rsv;
1259 }
1260  
1261 54 type = SvTYPE(data);
1262  
1263 54 switch (type) {
1264 0 case SVt_NULL:
1265 /* undef ? */
1266 0 sv_setpvn(rsv, "null", 4);
1267 0 return rsv;
1268 break;
1269  
1270 0 case SVt_IV:
1271 case SVt_NV:
1272 0 before_len = JsSvLen(rsv);
1273 0 sv_catsv(rsv, data);
1274 0 ▶ if (JsSvLen(rsv) == before_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (JsSvLen(rsv) == before_len) { 0 0 0
1275 0 sv_catpvn(rsv, "\"\"", 2);
1276 }
1277  
1278 0 return rsv;
1279 break;
1280  
1281 0 case SVt_PV:
1282 0 sv_catsv(rsv, data);
1283 0 tmp = rsv;
1284 0 rsv = escape_json_str(self, tmp);
1285 0 SvREFCNT_dec(tmp);
1286 0 return rsv;
1287 break;
1288  
1289 0 case SVt_PVIV:
1290 case SVt_PVNV:
1291 0 sv_catsv(rsv, data);
1292 0 tmp = rsv;
1293 0 rsv = escape_json_str(self, tmp);
1294 0 SvREFCNT_dec(tmp);
1295 0 return rsv;
1296 break;
1297  
1298 9 case SVt_PVAV: /* array */
1299 9 SvREFCNT_dec(rsv);
1300 9 return encode_array(self, (AV *)data, indent_level, cur_level);
1301 break;
1302  
1303 45 case SVt_PVHV: /* hash */
1304 45 SvREFCNT_dec(rsv);
1305 45 return encode_hash(self, (HV *)data, indent_level, cur_level);
1306 break;
1307  
1308 0 case SVt_PVCV: /* code */
1309 0 sv_catsv(rsv, data_ref);
1310 0 tmp = rsv;
1311 0 rsv = escape_json_str(self, tmp);
1312 0 SvREFCNT_dec(tmp);
1313  
1314 0 return rsv;
1315 /*
1316 sv_setpvn(rsv, "\"code\"", 6);
1317 return rsv;
1318 */
1319  
1320 break;
1321  
1322 0 case SVt_PVGV: /* glob */
1323 0 sv_catsv(rsv, data_ref);
1324 0 tmp = rsv;
1325 0 rsv = escape_json_str(self, tmp);
1326 0 SvREFCNT_dec(tmp);
1327  
1328 0 return rsv;
1329 break;
1330  
1331 0 case SVt_PVIO:
1332 0 sv_catsv(rsv, data);
1333 0 tmp = rsv;
1334 0 rsv = escape_json_str(self, tmp);
1335 0 SvREFCNT_dec(tmp);
1336 0 return rsv;
1337 break;
1338  
1339 0 case SVt_PVMG: /* blessed or magical scalar */
1340 0 ▶ if (sv_isobject(data_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(data_ref)) { 0 0 0
1341 0 sv_catsv(rsv, data);
1342 0 tmp = rsv;
1343 0 rsv = escape_json_str(self, tmp);
1344 0 SvREFCNT_dec(tmp);
1345
1346 0 return rsv;
1347 }
1348 else {
1349 0 sv_catsv(rsv, data);
1350 0 tmp = rsv;
1351 0 rsv = escape_json_str(self, tmp);
1352 0 SvREFCNT_dec(tmp);
1353
1354 0 return rsv;
1355 }
1356 break;
1357
1358 0 default:
1359 0 sv_catsv(rsv, data);
1360 0 tmp = rsv;
1361 0 rsv = escape_json_str(self, tmp);
1362 0 SvREFCNT_dec(tmp);
1363
1364 0 return rsv;
1365
1366 /* sv_setpvn(rsv, "unknown type", 12); */
1367 /* return rsv; */
1368
1369 break;
1370 }
1371  
1372 sv_setpvn(rsv, "unknown type 2", 14);
1373 return rsv;
1374  
1375 }
1376  
1377 static int
1378 51 set_encode_stats(self_context * ctx, SV * stats_data_ref) {
1379 51 SV * data = Nullsv;
1380  
1381 51 ▶ if (SvOK(stats_data_ref) && SvROK(stats_data_ref)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(stats_data_ref) && SvROK(stats_data_ref)) { 51 0 51
if (SvOK(stats_data_ref) && SvROK(stats_data_ref)) { 51 0 51
1382 51 data = SvRV(stats_data_ref);
1383
1384 /* FIXME: should destroy these if the store fails */
1385  
1386 /*
1387 hv_store((HV *)data, "max_string_bytes", 16, newSVuv(ctx->longest_string_bytes), 0);
1388 hv_store((HV *)data, "max_string_chars", 16, newSVuv(ctx->longest_string_chars), 0);
1389 hv_store((HV *)data, "nulls", 5, newSVuv(ctx->null_count), 0);
1390 */
1391  
1392 /*
1393 hv_store((HV *)data, "strings", 7, newSVuv(ctx->string_count), 0);
1394 hv_store((HV *)data, "bools", 5, newSVuv(ctx->bool_count), 0);
1395 hv_store((HV *)data, "numbers", 7, newSVuv(ctx->number_count), 0);
1396 */
1397  
1398 51 IGNORE_RV(hv_store((HV *)data, "hashes", 6, newSVuv(ctx->hash_count), 0));
1399 51 IGNORE_RV(hv_store((HV *)data, "arrays", 6, newSVuv(ctx->array_count), 0));
1400 51 IGNORE_RV(hv_store((HV *)data, "max_depth", 9, newSVuv(ctx->deepest_level), 0));
1401  
1402 }
1403  
1404 51 return 1;
1405 }
1406  
1407 static SV *
1408 0 has_mmap() {
1409 #ifdef HAS_MMAP
1410 0 return &PL_sv_yes;
1411 #else
1412 return &PL_sv_no;
1413 #endif
1414 }
1415  
1416 static SV *
1417 0 parse_mmap_file(SV * self, SV * file, SV * error_msg_ref) {
1418 #if USE_MMAP
1419 char * filename;
1420 STRLEN filename_len;
1421 void * base;
1422 int fd = -1;
1423 struct stat file_info;
1424 size_t len = 0;
1425 SV * rv;
1426 int throw_exception = 0;
1427 SV * error_msg = &PL_sv_undef;
1428 SV * passed_error_msg_sv;
1429  
1430 UNLESS (SvOK(file)) {
1431 return &PL_sv_undef;
1432 }
1433  
1434 filename = (char *)SvPV(file, filename_len);
1435 fd = open(filename, O_RDONLY, 0644);
1436 if (fd < 0) {
1437 return &PL_sv_undef;
1438 }
1439  
1440 if (fstat(fd, &file_info)) {
1441 return &PL_sv_undef;
1442 }
1443  
1444 /* FIXME: check here to see if file size too big, e.g., > 2GB */
1445  
1446 len = file_info.st_size;
1447  
1448 base = mmap(NULL, len, PROT_READ, 0, fd, 0);
1449  
1450 if (base == MAP_FAILED) {
1451 printf("mmap failed\n");
1452 return &PL_sv_undef;
1453 }
1454  
1455 fread(base, 1, len, stdout);
1456  
1457 rv = from_json(self, base, len, &error_msg, &throw_exception);
1458 if (SvOK(error_msg) && SvROK(error_msg_ref)) {
1459 passed_error_msg_sv = SvRV(error_msg_ref);
1460 sv_setsv(passed_error_msg_sv, error_msg);
1461 }
1462  
1463 munmap(base, len);
1464 #else
1465 0 return &PL_sv_undef;
1466 #endif
1467 }
1468  
1469 static SV *
1470 6 get_ref_addr(SV * ref) {
1471 6 SV * addr_str = Nullsv;
1472 6 SV * sv_addr = Nullsv;
1473 6 char * str = Nullch;
1474  
1475 6 ▶ if (SvROK(ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(ref)) { 6 0 6
1476 6 sv_addr = SvRV(ref);
1477 6 str = form("%"UVuf"", PTR2UV((void *)sv_addr));
1478 6 addr_str = newSVpvn(str, strlen(str));
1479 }
1480 else {
1481 0 return newSV(0);
1482 }
1483  
1484 6 return addr_str;
1485 }
1486  
1487 static SV *
1488 0 get_ref_type(SV * ref) {
1489 0 ▶ UNLESS (SvROK(ref)) {
Branch: 1 branch
BranchTrueFalseTotal
UNLESS (SvROK(ref)) { 0 0 0
1490 0 return newSV(0);
1491 }
1492  
1493 /* FIXME: complete the type checks here */
1494  
1495 0 return newSV(0);
1496 }
1497  
1498  
1499 MODULE = JSON::DWIW PACKAGE = JSON::DWIW
1500  
1501 PROTOTYPES: DISABLE
1502  
1503  
1504 SV *
1505 do_dummy_parse(SV *self, SV *str)
1506 CODE:
1507 0 RETVAL = do_json_dummy_parse(self, str);
1508 OUTPUT:
1509 RETVAL
1510  
1511 SV *
1512 has_deserialize(...)
1513 CODE:
1514 13 items = items;
1515 13 RETVAL = has_jsonevt();
1516 OUTPUT:
1517 RETVAL
1518  
1519 SV *
1520 deserialize(SV * data, ...)
1521 ALIAS:
1522 JSON::DWIW::load = 1
1523 JSON::DWIW::deserialize_json = 2
1524  
1525 PREINIT:
1526 148 ▶ SV * self = Nullsv;
Branch: 1 branch
BranchTrueFalseTotal
SV * self = Nullsv; 26 122 148
1527 SV * rv;
1528  
1529 CODE:
1530 148 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 26 122 148
1531 26 self = (SV *)ST(1);
1532 }
1533
1534 /* avoid compiler warnings about unused variable */
1535 148 ix = ix;
1536  
1537 148 rv = deserialize_json_sv(self, data);
1538  
1539 142 RETVAL = rv;
1540  
1541 OUTPUT:
1542 RETVAL
1543  
1544 SV *
1545 deserialize_file(SV * file, ...)
1546 ALIAS:
1547 JSON::DWIW::load_file = 1
1548  
1549 PREINIT:
1550 44 ▶ SV * self = Nullsv;
Branch: 1 branch
BranchTrueFalseTotal
SV * self = Nullsv; 0 44 44
1551 SV * rv;
1552  
1553 CODE:
1554 44 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 0 44 44
1555 0 self = (SV *)ST(1);
1556 }
1557
1558 /* avoid compiler warnings about unused variable */
1559 44 ix = ix;
1560  
1561 44 rv = do_json_parse_file(self, file);
1562  
1563 44 RETVAL = rv;
1564  
1565 OUTPUT:
1566 RETVAL
1567  
1568  
1569 SV *
1570 _xs_to_json(SV * self, SV * data, SV * error_msg_ref, SV * error_data_ref, SV * stats_ref)
1571 PREINIT:
1572 self_context self_context;
1573 SV * rv;
1574 51 int indent_level = 0;
1575 51 SV * passed_error_data_sv = Nullsv;
1576  
1577 CODE:
1578 51 setup_self_context(self, &self_context);
1579 51 rv = to_json(&self_context, data, indent_level, 0);
1580  
1581 51 ▶ if (SvOK(stats_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(stats_ref)) { 51 0 51
1582 51 set_encode_stats(&self_context, stats_ref);
1583 }
1584  
1585 51 ▶ if (self_context.error) {
Branch: 1 branch
BranchTrueFalseTotal
if (self_context.error) { 1 50 51
1586 1 sv_setsv(SvRV(error_msg_ref), self_context.error);
1587
1588 1 ▶ if (SvOK(error_data_ref) && SvROK(error_data_ref) && self_context.error_data) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvOK(error_data_ref) && SvROK(error_data_ref) && self_context.error_data) { 1 0 1
if (SvOK(error_data_ref) && SvROK(error_data_ref) && self_context.error_data) { 1 0 1
if (SvOK(error_data_ref) && SvROK(error_data_ref) && self_context.error_data) { 1 0 1
1589 1 passed_error_data_sv = SvRV(error_data_ref);
1590 1 sv_setsv(passed_error_data_sv, self_context.error_data);
1591 }
1592
1593 }
1594  
1595 51 ▶ if (self_context.ref_track) {
Branch: 1 branch
BranchTrueFalseTotal
if (self_context.ref_track) { 2 49 51
1596 2 SvREFCNT_dec(self_context.ref_track);
1597 2 self_context.ref_track = Nullhv;
1598 }
1599  
1600 51 RETVAL = rv;
1601  
1602 OUTPUT:
1603 RETVAL
1604  
1605 SV *
1606 have_big_int(SV * self)
1607 PREINIT:
1608 2 SV * rsv = newSV(0);
1609 int rv;
1610  
1611 CODE:
1612 2 self = self;
1613 2 rv = have_bigint();
1614 2 ▶ if (rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv) { 2 0 2
1615 2 sv_setsv(rsv, &PL_sv_yes);
1616 }
1617 else {
1618 0 sv_setsv(rsv, &PL_sv_no);
1619 }
1620  
1621 2 RETVAL = rsv;
1622  
1623 OUTPUT:
1624 RETVAL
1625  
1626 SV *
1627 have_big_float(SV * self)
1628 PREINIT:
1629 2 SV * rsv = newSV(0);
1630 int rv;
1631  
1632 CODE:
1633 2 self = self; /* get rid of compiler warnings */
1634 2 rv = have_bigfloat();
1635 2 ▶ if (rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv) { 2 0 2
1636 2 sv_setsv(rsv, &PL_sv_yes);
1637 }
1638 else {
1639 0 sv_setsv(rsv, &PL_sv_no);
1640 }
1641  
1642 2 RETVAL = rsv;
1643  
1644 OUTPUT:
1645 RETVAL
1646  
1647 SV *
1648 size_of_uv(SV * self)
1649 PREINIT:
1650 0 SV * rsv = newSV(0);
1651  
1652 CODE:
1653 0 self = self; /* get rid of compiler warnings */
1654 0 sv_setuv(rsv, UVSIZE);
1655  
1656 0 RETVAL = rsv;
1657  
1658 OUTPUT:
1659 RETVAL
1660  
1661 SV *
1662 peek_scalar(SV * self, SV * val)
1663 CODE:
1664 0 self = self; /* get rid of compiler warnings */
1665  
1666 0 sv_dump(val);
1667  
1668 0 RETVAL = &PL_sv_yes;
1669  
1670 OUTPUT:
1671 RETVAL
1672  
1673 SV *
1674 has_high_bit_bytes(SV *self, SV *val)
1675 PREINIT:
1676 U8 * s;
1677 STRLEN len;
1678 STRLEN i;
1679  
1680 CODE:
1681 0 self = self;
1682 0 RETVAL = &PL_sv_no;
1683 0 s = (U8 *)SvPV(val, len);
1684  
1685 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
1686 0 ▶ if (s[i] > 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (s[i] > 0x80) { 0 0 0
1687 0 RETVAL = &PL_sv_yes;
1688 }
1689 }
1690  
1691 OUTPUT:
1692 RETVAL
1693  
1694  
1695 SV *
1696 is_valid_utf8(SV * self, SV * str)
1697 PREINIT:
1698 5 SV * rv = &PL_sv_no;
1699 U8 * s;
1700 STRLEN len;
1701  
1702 CODE:
1703 5 self = self;
1704 5 s = (U8 *)SvPV(str, len);
1705 5 ▶ if (is_utf8_string(s, len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_utf8_string(s, len)) { 3 2 5
1706 3 rv = &PL_sv_yes;
1707 }
1708  
1709 5 RETVAL = rv;
1710  
1711 OUTPUT:
1712 RETVAL
1713  
1714 SV *
1715 upgrade_to_utf8(SV * self, SV * str)
1716 CODE:
1717 1 self = self;
1718 1 sv_utf8_upgrade(str);
1719  
1720 1 ▶ if (GIMME_V == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_VOID) { 1 0 1
1721 1 RETVAL = &PL_sv_yes;
1722 }
1723 else {
1724 0 RETVAL = newSVsv(str);
1725 }
1726  
1727 OUTPUT:
1728 RETVAL
1729  
1730 SV *
1731 flagged_as_utf8(SV * self, SV * str)
1732 PREINIT:
1733 5 ▶ SV * rv = &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
SV * rv = &PL_sv_no; 3 2 5
1734  
1735 CODE:
1736 5 self = self;
1737 5 ▶ if (SvUTF8(str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(str)) { 3 2 5
1738 3 rv = &PL_sv_yes;
1739 }
1740  
1741 5 RETVAL = rv;
1742  
1743 OUTPUT:
1744 RETVAL
1745  
1746 SV *
1747 flag_as_utf8(SV * self, SV * str)
1748 PREINIT:
1749 2 SV * rv = &PL_sv_yes;
1750  
1751 CODE:
1752 2 self = self;
1753 2 SvUTF8_on(str);
1754  
1755 2 RETVAL = rv;
1756  
1757 OUTPUT:
1758 RETVAL
1759  
1760 SV *
1761 unflag_as_utf8(SV * self, SV * str)
1762 PREINIT:
1763 1 SV * rv = &PL_sv_yes;
1764  
1765 CODE:
1766 1 self = self;
1767 1 SvUTF8_off(str);
1768  
1769 1 RETVAL = rv;
1770  
1771 OUTPUT:
1772 RETVAL
1773  
1774 SV *
1775 code_point_to_utf8_str(SV *, SV * code_point_sv)
1776 PREINIT:
1777 UV code_point;
1778 U8 utf8_bytes[5];
1779 4 SV * rv = Nullsv;
1780 4 uint32_t len32 = 0;
1781  
1782 CODE:
1783 4 utf8_bytes[4] = '\x00';
1784 4 code_point = SvUV(code_point_sv);
1785  
1786 4 len32 = common_utf8_unicode_to_bytes((uint32_t)code_point, (uint8_t *)utf8_bytes);
1787 4 utf8_bytes[len32] = '\x00';
1788  
1789 4 ▶ if (len32) {
Branch: 1 branch
BranchTrueFalseTotal
if (len32) { 4 0 4
1790 4 rv = newSVpv((char *)utf8_bytes, (STRLEN)len32);
1791 4 SvUTF8_on(rv);
1792 }
1793 else {
1794 0 rv = newSV(0);
1795 }
1796  
1797 4 RETVAL = rv;
1798  
1799 OUTPUT:
1800 RETVAL
1801  
1802 SV *
1803 code_point_to_hex_bytes(SV *, SV * code_point_sv)
1804 PREINIT:
1805 UV code_point;
1806 U8 utf8_bytes[5];
1807 SV * rv;
1808 0 uint32_t len32 = 0;
1809  
1810 CODE:
1811 0 utf8_bytes[4] = '\x00';
1812 0 code_point = SvUV(code_point_sv);
1813 0 rv = newSVpv("", 0);
1814  
1815 0 len32 = common_utf8_unicode_to_bytes((uint32_t)code_point, (uint8_t *) utf8_bytes);
1816 0 utf8_bytes[len32] = '\x00';
1817  
1818 0 ▶ if (len32) {
Branch: 1 branch
BranchTrueFalseTotal
if (len32) { 0 0 0
1819 uint32_t i;
1820 0 ▶ for (i = 0; i < len32; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len32; i++) { 0 0 0
1821 0 sv_catpvf(rv, "\\x%02x", (unsigned int)utf8_bytes[i]);
1822 }
1823 }
1824 else {
1825  
1826 }
1827  
1828 0 RETVAL = rv;
1829  
1830 OUTPUT:
1831 RETVAL
1832  
1833 SV *
1834 bytes_to_code_points(SV *, SV * bytes)
1835 PREINIT:
1836 U8 * data_str;
1837 STRLEN data_str_len;
1838 0 AV * array = newAV();
1839 0 STRLEN len = 0;
1840 UV this_char;
1841 0 STRLEN pos = 0;
1842 I32 max_i;
1843 0 ▶ SV * sv = NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV * sv = NULL; 0 0 0
1844 I32 i;
1845 SV ** element;
1846  
1847 CODE:
1848 0 ▶ if (SvROK(bytes) && SvTYPE(SvRV(bytes)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(bytes) && SvTYPE(SvRV(bytes)) == SVt_PVAV) { 0 0 0
if (SvROK(bytes) && SvTYPE(SvRV(bytes)) == SVt_PVAV) { 0 0 0
1849 0 AV * av = (AV *)SvRV(bytes);
1850 0 max_i = av_len(av);
1851 0 sv = newSV(max_i);
1852 0 sv_setpvn(sv, "", 0);
1853  
1854 0 ▶ for (i = 0; i <= max_i; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= max_i; i++) { 0 0 0
1855 0 element = av_fetch(av, i , 0);
1856 0 ▶ if (element && *element) {
Branch: 2 branches
BranchTrueFalseTotal
if (element && *element) { 0 0 0
if (element && *element) { 0 0 0
1857 0 this_char = SvUV(*element);
1858 0 fprintf(stderr, "%02"UVxf"\n", this_char);
1859 }
1860 else {
1861 0 this_char = 0;
1862 }
1863 0 sv_catpvf(sv, "%c", (unsigned char)this_char);
1864 }
1865 0 bytes = sv;
1866 }
1867  
1868 0 data_str = (U8 *)SvPV(bytes, data_str_len);
1869  
1870 0 ▶ while (pos < data_str_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < data_str_len) { 0 0 0
1871 0 this_char = convert_utf8_to_uv(&data_str[pos], &len);
1872 0 pos += len;
1873 0 av_push(array, newSVuv(this_char));
1874 }
1875  
1876 0 ▶ if (sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv) { 0 0 0
1877 0 SvREFCNT_dec(sv);
1878 }
1879  
1880 0 RETVAL = newRV_noinc((SV *)array);
1881  
1882 OUTPUT:
1883 RETVAL
1884  
1885 SV *
1886 _has_mmap()
1887 CODE:
1888 0 RETVAL = has_mmap();
1889  
1890 OUTPUT:
1891 RETVAL
1892  
1893 SV *
1894 _parse_mmap_file(SV * self, SV * file, SV * error_msg_ref)
1895  
1896 CODE:
1897 0 RETVAL = parse_mmap_file(self, file, error_msg_ref);
1898  
1899 OUTPUT:
1900 RETVAL
1901  
1902 SV *
1903 _check_scalar(SV *, SV * the_scalar)
1904 CODE:
1905 0 fprintf(stderr, "SV * at addr %"UVxf"\n", PTR2UV(the_scalar));
1906 0 sv_dump(the_scalar);
1907 0 ▶ if (SvROK(the_scalar)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(the_scalar)) { 0 0 0
1908 0 printf("\ndereferenced:\n");
1909 0 fprintf(stderr, "SV * at addr %"UVxf"\n", PTR2UV(SvRV(the_scalar)));
1910 0 sv_dump(SvRV(the_scalar));
1911 }
1912 0 RETVAL = &PL_sv_yes;
1913  
1914 OUTPUT:
1915 RETVAL
1916  
1917 SV *
1918 skip_deserialize_file()
1919 CODE:
1920 1 RETVAL = &PL_sv_no;
1921 OUTPUT:
1922 RETVAL
1923  
1924 SV *
1925 get_ref_addr(SV * ref)
1926 CODE:
1927 0 RETVAL = get_ref_addr(ref);
1928 OUTPUT:
1929 RETVAL
1930  
1931 SV *
1932 get_ref_type(SV * ref)
1933 CODE:
1934 0 RETVAL = get_ref_type(ref);
1935 OUTPUT:
1936 RETVAL
1937  
1938  
« old_common.c ↑ Summary evt.c »