emitter.c

filter: Statementstmt 79.6% 602 / 756 Branchbran 65.9% 393 / 596 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.5% 995 / 1352 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« node.c ↑ Summary handler.c »
1 /*
2 * emitter.c
3 *
4 * $Author: why $
5 * $Date: 2006-05-09 12:03:50 +0800 (二, 09 5 2006) $
6 *
7 * Copyright (C) 2003 why the lucky stiff
8 *
9 * All Base64 code from Ruby's pack.c.
10 * Ruby is Copyright (C) 1993-2003 Yukihiro Matsumoto
11 */
12 #include <stdio.h>
13 #include <string.h>
14  
15 #include "syck.h"
16  
17 #define DEFAULT_ANCHOR_FORMAT "id%03d"
18  
19 const unsigned char hex_table[] =
20 "0123456789ABCDEF";
21 static unsigned char b64_table[] =
22 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23  
24 /*
25 * Built-in base64 (from Ruby's pack.c)
26 */
27 char *
28 1 syck_base64enc( char *s, long len )
29 {
30 1 long i = 0;
31 1 int padding = '=';
32 1 char *buff = S_ALLOC_N(char, len * 4 / 3 + 6);
33  
34 1 ▶ while (len >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
while (len >= 3) { 0 1 1
35 0 buff[i++] = b64_table[077 & (*s >> 2)];
36 0 buff[i++] = b64_table[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
37 0 buff[i++] = b64_table[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
38 0 buff[i++] = b64_table[077 & s[2]];
39 0 s += 3;
40 0 len -= 3;
41 }
42 1 ▶ if (len == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 2) { 1 0 1
43 1 buff[i++] = b64_table[077 & (*s >> 2)];
44 1 buff[i++] = b64_table[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
45 1 buff[i++] = b64_table[077 & (((s[1] << 2) & 074) | (('\0' >> 6) & 03))];
46 1 buff[i++] = padding;
47 }
48 0 ▶ else if (len == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (len == 1) { 0 0 0
49 0 buff[i++] = b64_table[077 & (*s >> 2)];
50 0 buff[i++] = b64_table[077 & (((*s << 4) & 060) | (('\0' >> 4) & 017))];
51 0 buff[i++] = padding;
52 0 buff[i++] = padding;
53 }
54  
55 /* XXX - Changed by Audrey Tang to terminate the string instead of adding an useless \n */
56 1 buff[i++] = '\0';
57 1 return buff;
58 }
59  
60 /* XXX - Changed by Audrey Tang to deal with \0 in decoded base64 buffers */
61 char *
62 202 syck_base64dec( char *s, long len, long *out_len )
63 {
64 202 int a = -1,b = -1,c = 0,d;
65 static int first = 1;
66 static int b64_xtable[256];
67 202 char *ptr = syck_strndup( s, len );
68 202 char *end = ptr;
69 202 char *send = s + len;
70  
71 202 ▶ if (first) {
Branch: 1 branch
BranchTrueFalseTotal
if (first) { 3 199 202
72 int i;
73 3 first = 0;
74  
75 771 ▶ for (i = 0; i < 256; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i++) { 768 3 771
76 768 b64_xtable[i] = -1;
77 }
78 195 ▶ for (i = 0; i < 64; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 64; i++) { 192 3 195
79 192 b64_xtable[(int)b64_table[i]] = i;
80 }
81 }
82 202 ▶ while (s < send) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < send) { 202 0 202
83 202 ▶ while (s < send && (s[0] == '\r' || s[0] == '\n')) { s++; }
Branch: 3 branches
BranchTrueFalseTotal
while (s < send && (s[0] == '\r' || s[0] == '\n')) { s++; } 202 0 202
while (s < send && (s[0] == '\r' || s[0] == '\n')) { s++; } 0 202 202
while (s < send && (s[0] == '\r' || s[0] == '\n')) { s++; } 0 202 202
84 202 ▶ if (s >= send) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s >= send) break; 0 202 202
85 202 ▶ if ((a = b64_xtable[(unsigned char)s[0]]) == -1) break;
Branch: 1 branch
BranchTrueFalseTotal
if ((a = b64_xtable[(unsigned char)s[0]]) == -1) break; 1 201 202
86 201 ▶ if (s + 1 >= send || (b = b64_xtable[(unsigned char)s[1]]) == -1) break;
Branch: 2 branches
BranchTrueFalseTotal
if (s + 1 >= send || (b = b64_xtable[(unsigned char)s[1]]) == -1) break; 201 0 201
if (s + 1 >= send || (b = b64_xtable[(unsigned char)s[1]]) == -1) break; 201 0 201
87 201 ▶ if (s + 2 >= send || (c = b64_xtable[(unsigned char)s[2]]) == -1) break;
Branch: 2 branches
BranchTrueFalseTotal
if (s + 2 >= send || (c = b64_xtable[(unsigned char)s[2]]) == -1) break; 201 0 201
if (s + 2 >= send || (c = b64_xtable[(unsigned char)s[2]]) == -1) break; 201 0 201
88 201 ▶ if (s + 3 >= send || (d = b64_xtable[(unsigned char)s[3]]) == -1) break;
Branch: 2 branches
BranchTrueFalseTotal
if (s + 3 >= send || (d = b64_xtable[(unsigned char)s[3]]) == -1) break; 201 0 201
if (s + 3 >= send || (d = b64_xtable[(unsigned char)s[3]]) == -1) break; 0 201 201
89 0 *end++ = a << 2 | b >> 4;
90 0 *end++ = b << 4 | c >> 2;
91 0 *end++ = c << 6 | d;
92 0 s += 4;
93 }
94 202 ▶ if (a != -1 && b != -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (a != -1 && b != -1) { 201 1 202
if (a != -1 && b != -1) { 201 0 201
95 201 ▶ if (s + 2 < send && s[2] == '=')
Branch: 2 branches
BranchTrueFalseTotal
if (s + 2 < send && s[2] == '=') 201 0 201
if (s + 2 < send && s[2] == '=') 0 201 201
96 0 *end++ = a << 2 | b >> 4;
97 201 ▶ if (c != -1 && s + 3 < send && s[3] == '=') {
Branch: 3 branches
BranchTrueFalseTotal
if (c != -1 && s + 3 < send && s[3] == '=') { 201 0 201
if (c != -1 && s + 3 < send && s[3] == '=') { 201 0 201
if (c != -1 && s + 3 < send && s[3] == '=') { 201 0 201
98 201 *end++ = a << 2 | b >> 4;
99 201 *end++ = b << 4 | c >> 2;
100 }
101 }
102 202 *end = '\0';
103 /*RSTRING(buf)->len = ptr - RSTRING(buf)->ptr;*/
104  
105 202 *out_len = end - ptr;
106 202 return ptr;
107 }
108  
109 /*
110 * Free a buffer returned by syck_base64enc() or syck_base64dec().
111 * These buffers are allocated with C's malloc() in this translation unit.
112 * Callers in XS code must NOT use free()/Safefree() directly because
113 * Perl may redefine those to use a different allocator (causes
114 * "Free to wrong pool" on Windows).
115 */
116 void
117 203 syck_base64_free( char *ptr )
118 {
119 203 ▶ if ( ptr != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( ptr != NULL ) 203 0 203
120 203 free( ptr );
121 203 }
122  
123 /*
124 * Allocate an emitter
125 */
126 SyckEmitter *
127 1528 syck_new_emitter(void)
128 {
129 SyckEmitter *e;
130 1528 e = S_ALLOC( SyckEmitter );
131 1528 e->headless = 0;
132 1528 e->use_header = 0;
133 1528 e->use_version = 0;
134 1528 e->sort_keys = 0;
135 1528 e->anchor_format = NULL;
136 1528 e->explicit_typing = 0;
137 1528 e->best_width = 80;
138 1528 e->style = scalar_none;
139 1528 e->stage = doc_open;
140 1528 e->depth = 0;
141 1528 e->max_depth = 512;
142 1528 e->indent = 2;
143 1528 e->level = -1;
144 1528 e->anchors = NULL;
145 1528 e->markers = NULL;
146 1528 e->anchored = NULL;
147 1528 e->bufsize = SYCK_BUFFERSIZE;
148 1528 e->buffer = NULL;
149 1528 e->marker = NULL;
150 1528 e->bufpos = 0;
151 1528 e->emitter_handler = NULL;
152 1528 e->output_handler = NULL;
153 1528 e->lvl_idx = 0;
154 1528 e->lvl_capa = ALLOC_CT;
155 1528 e->levels = S_ALLOC_N( SyckLevel, e->lvl_capa );
156 1528 syck_emitter_reset_levels( e );
157 1528 e->json_mode = 0;
158 1528 e->bonus = NULL;
159 1528 return e;
160 }
161  
162 enum st_retval
163 189 syck_st_free_anchors( st_data_t key, st_data_t name, st_data_t arg )
164 {
165 189 free( (void *)name );
166 189 return ST_CONTINUE;
167 }
168  
169 void
170 1528 syck_emitter_st_free( SyckEmitter *e )
171 {
172 /*
173 * Free the anchor tables
174 */
175 1528 ▶ if ( e->anchors != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->anchors != NULL ) 86 1442 1528
176 {
177 86 st_foreach( e->anchors, syck_st_free_anchors, 0 );
178 86 st_free_table( e->anchors );
179 86 e->anchors = NULL;
180 }
181  
182 1528 ▶ if ( e->anchored != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->anchored != NULL ) 40 1488 1528
183 {
184 40 st_free_table( e->anchored );
185 40 e->anchored = NULL;
186 }
187  
188 /*
189 * Free the markers tables
190 */
191 1528 ▶ if ( e->markers != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->markers != NULL ) 1528 0 1528
192 {
193 1528 st_free_table( e->markers );
194 1528 e->markers = NULL;
195 }
196 1528 }
197  
198 SyckLevel *
199 40817 syck_emitter_current_level( SyckEmitter *e )
200 {
201 40817 return &e->levels[e->lvl_idx-1];
202 }
203  
204 SyckLevel *
205 12911 syck_emitter_parent_level( SyckEmitter *e )
206 {
207 12911 return &e->levels[e->lvl_idx-2];
208 }
209  
210 void
211 8485 syck_emitter_pop_level( SyckEmitter *e )
212 {
213 ASSERT( e != NULL );
214  
215 /* The root level should never be popped */
216 8485 ▶ if ( e->lvl_idx <= 1 ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( e->lvl_idx <= 1 ) return; 0 8485 8485
217  
218 8485 e->lvl_idx -= 1;
219 8485 free( e->levels[e->lvl_idx].domain );
220 }
221  
222 void
223 8485 syck_emitter_add_level( SyckEmitter *e, int len, enum syck_level_status status )
224 {
225 ASSERT( e != NULL );
226 8485 ▶ if ( e->lvl_idx + 1 > e->lvl_capa )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->lvl_idx + 1 > e->lvl_capa ) 408 8077 8485
227 {
228 408 e->lvl_capa += ALLOC_CT;
229 408 S_REALLOC_N( e->levels, SyckLevel, e->lvl_capa );
230 }
231  
232 ASSERT( len > e->levels[e->lvl_idx-1].spaces );
233 8485 e->levels[e->lvl_idx].spaces = len;
234 8485 e->levels[e->lvl_idx].ncount = 0;
235 8485 e->levels[e->lvl_idx].domain = syck_strndup( e->levels[e->lvl_idx-1].domain, strlen( e->levels[e->lvl_idx-1].domain ) );
236 8485 e->levels[e->lvl_idx].status = status;
237 8485 e->levels[e->lvl_idx].anctag = 0;
238 8485 e->lvl_idx += 1;
239 8485 }
240  
241 void
242 3056 syck_emitter_reset_levels( SyckEmitter *e )
243 {
244 3098 ▶ while ( e->lvl_idx > 1 )
Branch: 1 branch
BranchTrueFalseTotal
while ( e->lvl_idx > 1 ) 42 3056 3098
245 {
246 42 syck_emitter_pop_level( e );
247 }
248  
249 3056 ▶ if ( e->lvl_idx < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->lvl_idx < 1 ) 1528 1528 3056
250 {
251 1528 e->lvl_idx = 1;
252 1528 e->levels[0].spaces = -1;
253 1528 e->levels[0].ncount = 0;
254 1528 e->levels[0].domain = syck_strndup( "", 0 );
255 1528 e->levels[0].anctag = 0;
256 }
257 3056 e->levels[0].status = syck_lvl_header;
258 3056 }
259  
260 void
261 1528 syck_emitter_handler( SyckEmitter *e, SyckEmitterHandler hdlr )
262 {
263 1528 e->emitter_handler = hdlr;
264 1528 }
265  
266 void
267 1528 syck_output_handler( SyckEmitter *e, SyckOutputHandler hdlr )
268 {
269 1528 e->output_handler = hdlr;
270 1528 }
271  
272 void
273 1528 syck_free_emitter( SyckEmitter *e )
274 {
275 /*
276 * Free tables
277 */
278 1528 syck_emitter_st_free( e );
279 1528 syck_emitter_reset_levels( e );
280 1528 S_FREE( e->levels[0].domain );
281 1528 S_FREE( e->levels );
282 1528 ▶ if ( e->buffer != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->buffer != NULL ) 1479 49 1528
283 {
284 1479 S_FREE( e->buffer );
285 }
286 1528 S_FREE( e );
287 1528 }
288  
289 void
290 1479 syck_emitter_clear( SyckEmitter *e )
291 {
292 1479 ▶ if ( e->buffer == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->buffer == NULL ) 1479 0 1479
293 {
294 1479 e->buffer = S_ALLOC_N( char, e->bufsize );
295 1479 S_MEMZERO( e->buffer, char, e->bufsize );
296 }
297 1479 e->buffer[0] = '\0';
298 1479 e->marker = e->buffer;
299 1479 e->bufpos = 0;
300 1479 }
301  
302 /*
303 * Raw write to the emitter buffer.
304 */
305 void
306 27096 syck_emitter_write( SyckEmitter *e, const char *str, long len )
307 {
308 long at;
309 ASSERT( str != NULL )
310 27096 ▶ if ( e->buffer == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->buffer == NULL ) 1479 25617 27096
311 {
312 1479 syck_emitter_clear( e );
313 }
314
315 /*
316 * Flush if at end of buffer
317 */
318 27096 at = e->marker - e->buffer;
319 27096 ▶ if ( len + at >= e->bufsize )
Branch: 1 branch
BranchTrueFalseTotal
if ( len + at >= e->bufsize ) 108 26988 27096
320 {
321 108 syck_emitter_flush( e, 0 );
322 0 for (;;) {
323 108 long rest = e->bufsize - (e->marker - e->buffer);
324 108 ▶ if (len <= rest) break;
Branch: 1 branch
BranchTrueFalseTotal
if (len <= rest) break; 108 0 108
325 0 S_MEMCPY( e->marker, str, char, rest );
326 0 e->marker += rest;
327 0 str += rest;
328 0 len -= rest;
329 0 syck_emitter_flush( e, 0 );
330 }
331 }
332  
333 /*
334 * Write to buffer
335 */
336 27096 S_MEMCPY( e->marker, str, char, len );
337 27096 e->marker += len;
338 27096 }
339  
340 /*
341 * Write a chunk of data out.
342 */
343 void
344 1566 syck_emitter_flush( SyckEmitter *e, long check_room )
345 {
346 /*
347 * Check for enough space in the buffer for check_room length.
348 */
349 1566 ▶ if ( check_room > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( check_room > 0 ) 0 1566 1566
350 {
351 0 ▶ if ( e->bufsize > ( e->marker - e->buffer ) + check_room )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->bufsize > ( e->marker - e->buffer ) + check_room ) 0 0 0
352 {
353 0 return;
354 }
355 }
356 else
357 {
358 1566 check_room = e->bufsize;
359 }
360  
361 /*
362 * Commit buffer.
363 */
364 1566 ▶ if ( check_room > e->marker - e->buffer )
Branch: 1 branch
BranchTrueFalseTotal
if ( check_room > e->marker - e->buffer ) 1566 0 1566
365 {
366 1566 check_room = e->marker - e->buffer;
367 }
368 1566 (e->output_handler)( e, e->buffer, check_room );
369 1566 e->bufpos += check_room;
370 1566 e->marker -= check_room;
371 }
372  
373 /*
374 * Start emitting from the given node, check for anchoring and then
375 * issue the callback to the emitter handler.
376 */
377 void
378 8485 syck_emit( SyckEmitter *e, st_data_t n )
379 {
380 SYMID oid;
381 8485 char *anchor_name = NULL;
382 8485 int indent = 0;
383 SyckLevel *parent;
384 8485 SyckLevel *lvl = syck_emitter_current_level( e );
385
386 /*
387 * Determine headers.
388 */
389 8485 ▶ if ( e->stage == doc_open && ( e->headless == 0 || e->use_header == 1 ) )
Branch: 3 branches
BranchTrueFalseTotal
if ( e->stage == doc_open && ( e->headless == 0 || e->use_header == 1 ) ) 2509 5976 8485
if ( e->stage == doc_open && ( e->headless == 0 || e->use_header == 1 ) ) 1267 1242 2509
if ( e->stage == doc_open && ( e->headless == 0 || e->use_header == 1 ) ) 0 1267 1267
390 {
391 1242 ▶ if ( e->use_version == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->use_version == 1 ) 0 1242 1242
392 {
393 0 char *header = S_ALLOC_N( char, 64 );
394 0 S_MEMZERO( header, char, 64 );
395 0 sprintf( header, "--- %%YAML:%d.%d ", SYCK_YAML_MAJOR, SYCK_YAML_MINOR );
396 0 syck_emitter_write( e, header, strlen( header ) );
397 0 S_FREE( header );
398 }
399 else
400 {
401 1242 syck_emitter_write( e, "--- ", 4 );
402 }
403 1242 e->stage = doc_processing;
404 }
405  
406 /* Add new level */
407 8485 ▶ if ( lvl->spaces >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->spaces >= 0 ) { 7006 1479 8485
408 7006 indent = lvl->spaces + e->indent;
409 }
410 8485 syck_emitter_add_level( e, indent, syck_lvl_open );
411 8485 parent = lvl;
412 8485 lvl = syck_emitter_current_level( e );
413  
414 /* Look for anchor */
415 9149 if ( e->anchors != NULL &&
416 1250 ▶ st_lookup( e->markers, n, (st_data_t *)&oid ) &&
Branch: 1 branch
BranchTrueFalseTotal
st_lookup( e->markers, n, (st_data_t *)&oid ) && 235 351 586
417 586 st_lookup( e->anchors, (st_data_t)oid, (st_data_t *)&anchor_name ) )
418 {
419 235 ▶ if ( e->anchored == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->anchored == NULL ) 36 199 235
420 {
421 36 e->anchored = st_init_numtable();
422 }
423  
424 235 ▶ if ( ! st_lookup( e->anchored, (st_data_t)anchor_name, 0 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ( ! st_lookup( e->anchored, (st_data_t)anchor_name, 0 ) ) 49 186 235
425 {
426 49 char *an = S_ALLOC_N( char, strlen( anchor_name ) + 3 );
427 49 sprintf( an, "&%s ", anchor_name );
428  
429 /* Complex key */
430 49 ▶ if ( parent->status == syck_lvl_map && parent->ncount % 2 == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ( parent->status == syck_lvl_map && parent->ncount % 2 == 1) { 5 44 49
if ( parent->status == syck_lvl_map && parent->ncount % 2 == 1) { 0 5 5
431 0 syck_emitter_write( e, "? ", 2 );
432 0 parent->status = syck_lvl_mapx;
433 }
434  
435 49 syck_emitter_write( e, an, strlen( anchor_name ) + 2 );
436 49 free( an );
437  
438 49 st_insert( e->anchored, (st_data_t)anchor_name, 0 );
439 49 lvl->anctag = 1;
440 }
441 else
442 {
443 186 char *an = S_ALLOC_N( char, strlen( anchor_name ) + 2 );
444 186 sprintf( an, "*%s", anchor_name );
445 186 syck_emitter_write( e, an, strlen( anchor_name ) + 1 );
446 186 free( an );
447  
448 186 goto end_emit;
449 }
450 }
451  
452 8299 (e->emitter_handler)( e, n );
453  
454 /* Pop the level */
455 8443 end_emit:
456 8443 syck_emitter_pop_level( e );
457 8443 ▶ if ( e->lvl_idx == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( e->lvl_idx == 1 ) { 1458 6985 8443
458 1458 syck_emitter_write( e, "\n", 1 );
459 1458 e->headless = 0;
460 1458 e->stage = doc_open;
461 }
462 8443 }
463  
464 /*
465 * Determine what tag needs to be written, based on the taguri of the node
466 * and the implicit tag which would be assigned to this node. If a tag is
467 * required, write the tag.
468 */
469 6115 void syck_emit_tag( SyckEmitter *e, const char *tag, const char *ignore )
470 {
471 SyckLevel *lvl;
472 6115 ▶ if ( tag == NULL ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( tag == NULL ) return; 0 6115 6115
473 6115 ▶ if ( ignore != NULL && syck_tagcmp( tag, ignore ) == 0 && e->explicit_typing == 0 ) return;
Branch: 3 branches
BranchTrueFalseTotal
if ( ignore != NULL && syck_tagcmp( tag, ignore ) == 0 && e->explicit_typing == 0 ) return; 6115 0 6115
if ( ignore != NULL && syck_tagcmp( tag, ignore ) == 0 && e->explicit_typing == 0 ) return; 1925 4190 6115
if ( ignore != NULL && syck_tagcmp( tag, ignore ) == 0 && e->explicit_typing == 0 ) return; 1925 0 1925
474 4190 lvl = syck_emitter_current_level( e );
475  
476 /* implicit */
477 4190 ▶ if ( strlen( tag ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strlen( tag ) == 0 ) { 0 4190 4190
478 0 syck_emitter_write( e, "! ", 2 );
479  
480 /* global types */
481 4190 ▶ } else if ( strncmp( tag, "tag:", 4 ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( strncmp( tag, "tag:", 4 ) == 0 ) { 494 3696 4190
482 494 int taglen = strlen( tag );
483 494 syck_emitter_write( e, "!", 1 );
484 494 ▶ if ( strncmp( tag + 4, YAML_DOMAIN, strlen( YAML_DOMAIN ) ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strncmp( tag + 4, YAML_DOMAIN, strlen( YAML_DOMAIN ) ) == 0 ) { 1 493 494
485 1 int skip = 4 + strlen( YAML_DOMAIN ) + 1;
486 1 syck_emitter_write( e, tag + skip, taglen - skip );
487 } else {
488 493 const char *subd = tag + 4;
489 2958 ▶ while ( *subd != ':' && *subd != '\0' ) subd++;
Branch: 2 branches
BranchTrueFalseTotal
while ( *subd != ':' && *subd != '\0' ) subd++; 2465 493 2958
while ( *subd != ':' && *subd != '\0' ) subd++; 2465 0 2465
490 493 ▶ if ( *subd == ':' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( *subd == ':' ) { 493 0 493
491 493 ▶ if ( subd - tag > ( strlen( YAML_DOMAIN ) + 5 ) &&
Branch: 1 branch
BranchTrueFalseTotal
if ( subd - tag > ( strlen( YAML_DOMAIN ) + 5 ) && 0 493 493
492 0 ▶ strncmp( subd - strlen( YAML_DOMAIN ), YAML_DOMAIN, strlen( YAML_DOMAIN ) ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
strncmp( subd - strlen( YAML_DOMAIN ), YAML_DOMAIN, strlen( YAML_DOMAIN ) ) == 0 ) { 0 0 0
493 0 syck_emitter_write( e, tag + 4, subd - strlen( YAML_DOMAIN ) - ( tag + 4 ) - 1 );
494 0 syck_emitter_write( e, "/", 1 );
495 0 syck_emitter_write( e, subd + 1, ( tag + taglen ) - ( subd + 1 ) );
496 } else {
497 493 syck_emitter_write( e, tag + 4, subd - ( tag + 4 ) );
498 493 syck_emitter_write( e, "/", 1 );
499 493 syck_emitter_write( e, subd + 1, ( tag + taglen ) - ( subd + 1 ) );
500 }
501 } else {
502 /* TODO: Invalid tag (no colon after domain) */
503 0 return;
504 }
505 }
506 494 syck_emitter_write( e, " ", 1 );
507  
508 /* private types */
509 3696 ▶ } else if ( strncmp( tag, "x-private:", 10 ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( strncmp( tag, "x-private:", 10 ) == 0 ) { 0 3696 3696
510 0 syck_emitter_write( e, "!!", 2 );
511 0 syck_emitter_write( e, tag + 10, strlen( tag ) - 10 );
512 0 syck_emitter_write( e, " ", 1 );
513 }
514 4190 lvl->anctag = 1;
515 }
516  
517 /*
518 * Emit a newline and an appropriately spaced indent.
519 */
520 3642 void syck_emit_indent( SyckEmitter *e )
521 {
522 int i;
523 3642 SyckLevel *lvl = syck_emitter_current_level( e );
524 3642 ▶ if ( e->bufpos == 0 && ( e->marker - e->buffer ) == 0 ) return;
Branch: 2 branches
BranchTrueFalseTotal
if ( e->bufpos == 0 && ( e->marker - e->buffer ) == 0 ) return; 2970 672 3642
if ( e->bufpos == 0 && ( e->marker - e->buffer ) == 0 ) return; 2 2968 2970
525  
526 /* Trim trailing spaces before emitting the newline to avoid
527 * trailing whitespace on lines like "--- " or "&anchor " when
528 * the content continues on the next line (GitHub #38). */
529 5482 ▶ while ( e->marker > e->buffer && *(e->marker - 1) == ' ' ) {
Branch: 2 branches
BranchTrueFalseTotal
while ( e->marker > e->buffer && *(e->marker - 1) == ' ' ) { 5482 0 5482
while ( e->marker > e->buffer && *(e->marker - 1) == ' ' ) { 1842 3640 5482
530 1842 e->marker--;
531 }
532  
533 3640 ▶ if ( lvl->spaces >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->spaces >= 0 ) { 3640 0 3640
534 3640 char *spcs = S_ALLOC_N( char, lvl->spaces + 2 );
535  
536 3640 spcs[0] = '\n'; spcs[lvl->spaces + 1] = '\0';
537 408774 ▶ for ( i = 0; i < lvl->spaces; i++ ) spcs[i+1] = ' ';
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < lvl->spaces; i++ ) spcs[i+1] = ' '; 405134 3640 408774
538 3640 syck_emitter_write( e, spcs, lvl->spaces + 1 );
539 3640 free( spcs );
540 }
541 }
542  
543 /* Clear the scan */
544 #define SCAN_NONE 0
545 /* All printable characters? */
546 #define SCAN_NONPRINT 1
547 /* Any indented lines? */
548 #define SCAN_INDENTED 2
549 /* Larger than the requested width? */
550 #define SCAN_WIDE 4
551 /* Opens or closes with whitespace? */
552 #define SCAN_WHITEEDGE 8
553 /* Contains a newline */
554 #define SCAN_NEWLINE 16
555 /* Contains a single quote */
556 #define SCAN_SINGLEQ 32
557 /* Contains a double quote */
558 #define SCAN_DOUBLEQ 64
559 /* Starts with a token */
560 #define SCAN_INDIC_S 128
561 /* Contains a flow indicator */
562 #define SCAN_INDIC_C 256
563 /* Ends without newlines */
564 #define SCAN_NONL_E 512
565 /* Ends with many newlines */
566 #define SCAN_MANYNL_E 1024
567 /* Contains flow map indicators */
568 #define SCAN_FLOWMAP 2048
569 /* Contains flow seq indicators */
570 #define SCAN_FLOWSEQ 4096
571 /* Contains a valid doc separator */
572 #define SCAN_DOCSEP 8192
573  
574 /*
575 * Basic printable test for LATIN-1 characters.
576 */
577 int
578 3390 syck_scan_scalar( int req_width, char *cursor, long len )
579 {
580 3390 long i = 0, start = 0;
581 3390 int flags = SCAN_NONE;
582  
583 3390 ▶ if ( len < 1 ) return flags;
Branch: 1 branch
BranchTrueFalseTotal
if ( len < 1 ) return flags; 15 3375 3390
584  
585 /* c-indicators from the spec */
586 3375 ▶ if ( cursor[0] == '[' || cursor[0] == ']' ||
Branch: 2 branches
BranchTrueFalseTotal
if ( cursor[0] == '[' || cursor[0] == ']' || 3375 0 3375
if ( cursor[0] == '[' || cursor[0] == ']' || 3375 0 3375
587 3375 ▶ cursor[0] == '{' || cursor[0] == '}' ||
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '{' || cursor[0] == '}' || 3165 210 3375
cursor[0] == '{' || cursor[0] == '}' || 3165 0 3165
588 3165 ▶ cursor[0] == '!' || cursor[0] == '*' ||
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '!' || cursor[0] == '*' || 3165 0 3165
cursor[0] == '!' || cursor[0] == '*' || 2965 200 3165
589 2965 ▶ cursor[0] == '&' || cursor[0] == '|' ||
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '&' || cursor[0] == '|' || 2965 0 2965
cursor[0] == '&' || cursor[0] == '|' || 2965 0 2965
590 2965 ▶ cursor[0] == '>' || cursor[0] == '\'' ||
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '>' || cursor[0] == '\'' || 2965 0 2965
cursor[0] == '>' || cursor[0] == '\'' || 2961 4 2965
591 2961 ▶ cursor[0] == '"' || cursor[0] == '#' ||
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '"' || cursor[0] == '#' || 2957 4 2961
cursor[0] == '"' || cursor[0] == '#' || 2957 0 2957
592 2957 ▶ cursor[0] == '%' || cursor[0] == '@' || cursor[0] == '`' ||
Branch: 3 branches
BranchTrueFalseTotal
cursor[0] == '%' || cursor[0] == '@' || cursor[0] == '`' || 2957 0 2957
cursor[0] == '%' || cursor[0] == '@' || cursor[0] == '`' || 2957 0 2957
cursor[0] == '%' || cursor[0] == '@' || cursor[0] == '`' || 2956 1 2957
593 2956 ▶ cursor[0] == '&' || cursor[0] == '^' ) {
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '&' || cursor[0] == '^' ) { 2956 0 2956
cursor[0] == '&' || cursor[0] == '^' ) { 0 2956 2956
594 419 flags |= SCAN_INDIC_S;
595 }
596 3375 ▶ if ( ( cursor[0] == '-' || cursor[0] == ':' ||
Branch: 2 branches
BranchTrueFalseTotal
if ( ( cursor[0] == '-' || cursor[0] == ':' || 3342 33 3375
if ( ( cursor[0] == '-' || cursor[0] == ':' || 3212 130 3342
597 3212 ▶ cursor[0] == '?' || cursor[0] == ',' ) &&
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '?' || cursor[0] == ',' ) && 3212 0 3212
cursor[0] == '?' || cursor[0] == ',' ) && 0 3212 3212
598 163 ▶ ( cursor[1] == ' ' || cursor[1] == '\n' || cursor[1] == '\r' || len == 1 ) )
Branch: 4 branches
BranchTrueFalseTotal
( cursor[1] == ' ' || cursor[1] == '\n' || cursor[1] == '\r' || len == 1 ) ) 162 1 163
( cursor[1] == ' ' || cursor[1] == '\n' || cursor[1] == '\r' || len == 1 ) ) 162 0 162
( cursor[1] == ' ' || cursor[1] == '\n' || cursor[1] == '\r' || len == 1 ) ) 162 0 162
( cursor[1] == ' ' || cursor[1] == '\n' || cursor[1] == '\r' || len == 1 ) ) 0 162 162
599 {
600 1 flags |= SCAN_INDIC_S;
601 }
602  
603 /* whitespace edges */
604 3375 ▶ if ( cursor[len-1] != '\n' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( cursor[len-1] != '\n' ) { 3354 21 3375
605 3354 flags |= SCAN_NONL_E;
606 21 ▶ } else if ( len > 1 && cursor[len-2] == '\n' ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( len > 1 && cursor[len-2] == '\n' ) { 14 7 21
} else if ( len > 1 && cursor[len-2] == '\n' ) { 2 12 14
607 2 flags |= SCAN_MANYNL_E;
608 }
609 3375 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 3375 0 3375
610 3375 ▶ ( len > 0 && ( cursor[0] == ' ' || cursor[0] == '\t' ) ) ||
Branch: 3 branches
BranchTrueFalseTotal
( len > 0 && ( cursor[0] == ' ' || cursor[0] == '\t' ) ) || 3373 2 3375
( len > 0 && ( cursor[0] == ' ' || cursor[0] == '\t' ) ) || 3369 4 3373
( len > 0 && ( cursor[0] == ' ' || cursor[0] == '\t' ) ) || 1701 1668 3369
611 1701 ▶ ( len > 1 && ( cursor[len-1] == ' ' || cursor[len-1] == '\t' ) )
Branch: 2 branches
BranchTrueFalseTotal
( len > 1 && ( cursor[len-1] == ' ' || cursor[len-1] == '\t' ) ) 1698 3 1701
( len > 1 && ( cursor[len-1] == ' ' || cursor[len-1] == '\t' ) ) 0 1698 1698
612 ) {
613 9 flags |= SCAN_WHITEEDGE;
614 }
615  
616 /* opening doc sep or doc end */
617 3375 ▶ if ( len >= 3 && ( strncmp( cursor, "---", 3 ) == 0 || strncmp( cursor, "...", 3 ) == 0 ) )
Branch: 3 branches
BranchTrueFalseTotal
if ( len >= 3 && ( strncmp( cursor, "---", 3 ) == 0 || strncmp( cursor, "...", 3 ) == 0 ) ) 1618 1757 3375
if ( len >= 3 && ( strncmp( cursor, "---", 3 ) == 0 || strncmp( cursor, "...", 3 ) == 0 ) ) 1617 1 1618
if ( len >= 3 && ( strncmp( cursor, "---", 3 ) == 0 || strncmp( cursor, "...", 3 ) == 0 ) ) 2 1615 1617
618 3 flags |= SCAN_DOCSEP;
619  
620 /* scan string */
621 18430 ▶ for ( i = 0; i < len; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < len; i++ ) { 15055 3375 18430
622  
623 15055 ▶ if ( ! ( (unsigned char)cursor[i] == 0xA ||
Branch: 1 branch
BranchTrueFalseTotal
if ( ! ( (unsigned char)cursor[i] == 0xA || 14385 670 15055
624 14385 ▶ ( (unsigned char)cursor[i] >= 0x20 &&
Branch: 1 branch
BranchTrueFalseTotal
( (unsigned char)cursor[i] >= 0x20 && 14126 259 14385
625 14126 ▶ (unsigned char)cursor[i] <= 0x7E ) ||
Branch: 1 branch
BranchTrueFalseTotal
(unsigned char)cursor[i] <= 0x7E ) || 987 13139 14126
626 1246 ▶ (unsigned char)cursor[i] >= 0x80 )
Branch: 1 branch
BranchTrueFalseTotal
(unsigned char)cursor[i] >= 0x80 ) 259 987 1246
627 ) {
628 259 flags |= SCAN_NONPRINT;
629 }
630 14796 ▶ else if ( cursor[i] == '\n' ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == '\n' ) { 670 14126 14796
631 670 flags |= SCAN_NEWLINE;
632 670 ▶ if ( len - i >= 3 && ( strncmp( &cursor[i+1], "---", 3 ) == 0 || strncmp( &cursor[i+1], "...", 3 ) == 0 ) )
Branch: 3 branches
BranchTrueFalseTotal
if ( len - i >= 3 && ( strncmp( &cursor[i+1], "---", 3 ) == 0 || strncmp( &cursor[i+1], "...", 3 ) == 0 ) ) 437 233 670
if ( len - i >= 3 && ( strncmp( &cursor[i+1], "---", 3 ) == 0 || strncmp( &cursor[i+1], "...", 3 ) == 0 ) ) 437 0 437
if ( len - i >= 3 && ( strncmp( &cursor[i+1], "---", 3 ) == 0 || strncmp( &cursor[i+1], "...", 3 ) == 0 ) ) 0 437 437
633 0 flags |= SCAN_DOCSEP;
634 670 ▶ if ( cursor[i+1] == ' ' || cursor[i+1] == '\t' )
Branch: 2 branches
BranchTrueFalseTotal
if ( cursor[i+1] == ' ' || cursor[i+1] == '\t' ) 253 417 670
if ( cursor[i+1] == ' ' || cursor[i+1] == '\t' ) 0 253 253
635 417 flags |= SCAN_INDENTED;
636 670 ▶ if ( req_width > 0 && i - start > req_width )
Branch: 2 branches
BranchTrueFalseTotal
if ( req_width > 0 && i - start > req_width ) 0 670 670
if ( req_width > 0 && i - start > req_width ) 0 0 0
637 0 flags |= SCAN_WIDE;
638 670 start = i;
639 }
640 14126 ▶ else if ( cursor[i] == '\'' )
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == '\'' ) 16 14110 14126
641 {
642 16 flags |= SCAN_SINGLEQ;
643 }
644 14110 ▶ else if ( cursor[i] == '"' )
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == '"' ) 14 14096 14110
645 {
646 14 flags |= SCAN_DOUBLEQ;
647 }
648 14096 ▶ else if ( cursor[i] == ']' )
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == ']' ) 4 14092 14096
649 {
650 4 flags |= SCAN_FLOWSEQ;
651 }
652 14092 ▶ else if ( cursor[i] == '}' )
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == '}' ) 215 13877 14092
653 {
654 215 flags |= SCAN_FLOWMAP;
655 }
656 /* remember, if plain collections get implemented, to add nb-plain-flow-char */
657 13877 ▶ else if ( ( cursor[i] == ' ' && cursor[i+1] == '#' ) ||
Branch: 2 branches
BranchTrueFalseTotal
else if ( ( cursor[i] == ' ' && cursor[i+1] == '#' ) || 1986 11891 13877
else if ( ( cursor[i] == ' ' && cursor[i+1] == '#' ) || 1985 1 1986
658 13876 ▶ ( cursor[i] == ':' &&
Branch: 1 branch
BranchTrueFalseTotal
( cursor[i] == ':' && 569 13307 13876
659 569 ▶ ( cursor[i+1] == ' ' || cursor[i+1] == '\n' || i == len - 1 ) ) )
Branch: 3 branches
BranchTrueFalseTotal
( cursor[i+1] == ' ' || cursor[i+1] == '\n' || i == len - 1 ) ) ) 564 5 569
( cursor[i+1] == ' ' || cursor[i+1] == '\n' || i == len - 1 ) ) ) 564 0 564
( cursor[i+1] == ' ' || cursor[i+1] == '\n' || i == len - 1 ) ) ) 2 562 564
660 {
661 8 flags |= SCAN_INDIC_C;
662 }
663 13869 ▶ else if ( cursor[i] == ',' &&
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == ',' && 10 13859 13869
664 10 ▶ ( cursor[i+1] == ' ' || cursor[i+1] == '\n' || i == len - 1 ) )
Branch: 3 branches
BranchTrueFalseTotal
( cursor[i+1] == ' ' || cursor[i+1] == '\n' || i == len - 1 ) ) 5 5 10
( cursor[i+1] == ' ' || cursor[i+1] == '\n' || i == len - 1 ) ) 5 0 5
( cursor[i+1] == ' ' || cursor[i+1] == '\n' || i == len - 1 ) ) 0 5 5
665 {
666 5 flags |= SCAN_FLOWMAP;
667 5 flags |= SCAN_FLOWSEQ;
668 }
669 }
670  
671 /* printf( "---STR---\n%s\nFLAGS: %d\n", cursor, flags ); */
672 3375 return flags;
673 }
674 /*
675 * All scalars should be emitted through this function, which determines an appropriate style,
676 * tag and indent.
677 */
678 3390 void syck_emit_scalar( SyckEmitter *e, char *tag, enum scalar_style force_style, int force_indent, int force_width,
679 char keep_nl, char *str, long len )
680 {
681 3390 enum scalar_style favor_style = scalar_literal;
682 3390 SyckLevel *parent = syck_emitter_parent_level( e );
683 3390 SyckLevel *lvl = syck_emitter_current_level( e );
684 3390 int scan = 0;
685 char *implicit;
686
687 3390 ▶ if ( str == NULL ) str = "";
Branch: 1 branch
BranchTrueFalseTotal
if ( str == NULL ) str = ""; 0 3390 3390
688  
689 /* No empty nulls as map keys */
690 3390 ▶ if ( len == 0 && ( parent->status == syck_lvl_map || parent->status == syck_lvl_imap ) &&
Branch: 3 branches
BranchTrueFalseTotal
if ( len == 0 && ( parent->status == syck_lvl_map || parent->status == syck_lvl_imap ) && 15 3375 3390
if ( len == 0 && ( parent->status == syck_lvl_map || parent->status == syck_lvl_imap ) && 13 2 15
if ( len == 0 && ( parent->status == syck_lvl_map || parent->status == syck_lvl_imap ) && 5 8 13
691 7 ▶ parent->ncount % 2 == 1 && syck_tagcmp( tag, "tag:yaml.org,2002:null" ) == 0 )
Branch: 2 branches
BranchTrueFalseTotal
parent->ncount % 2 == 1 && syck_tagcmp( tag, "tag:yaml.org,2002:null" ) == 0 ) 0 7 7
parent->ncount % 2 == 1 && syck_tagcmp( tag, "tag:yaml.org,2002:null" ) == 0 ) 0 0 0
692 {
693 0 str = "~";
694 0 len = 1;
695 }
696  
697 3390 scan = syck_scan_scalar( force_width, str, len );
698 3390 implicit = syck_match_implicit( str, len );
699  
700 /* quote strings which default to implicits so they roundtrip correctly
701 * when ImplicitTyping is enabled. Besides bool and null, this covers
702 * hex (0x1A), octal (010), base-60 (1:30), and special float values
703 * (.inf, .nan) which all transform on load. */
704 3390 if (
705 (
706 3390 ▶ (strncmp( implicit, "bool", 4 ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strncmp( implicit, "bool", 4 ) == 0) || 3352 38 3390
707 3352 ▶ (strncmp( implicit, "null", 4 ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strncmp( implicit, "null", 4 ) == 0) || 3295 57 3352
708 3295 ▶ (strncmp( implicit, "int#", 4 ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strncmp( implicit, "int#", 4 ) == 0) || 3274 21 3295
709 3274 ▶ (strcmp( implicit, "float#inf" ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strcmp( implicit, "float#inf" ) == 0) || 3265 9 3274
710 3265 ▶ (strcmp( implicit, "float#neginf" ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strcmp( implicit, "float#neginf" ) == 0) || 3261 4 3265
711 3261 ▶ (strcmp( implicit, "float#nan" ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strcmp( implicit, "float#nan" ) == 0) || 3256 5 3261
712 3256 ▶ (strcmp( implicit, "float#base60" ) == 0)
Branch: 1 branch
BranchTrueFalseTotal
(strcmp( implicit, "float#base60" ) == 0) 0 3256 3256
713 )
714 134 ▶ &&
Branch: 1 branch
BranchTrueFalseTotal
&& 94 40 134
715 (force_style != scalar_plain)
716 94 ▶ &&
Branch: 1 branch
BranchTrueFalseTotal
&& 79 15 94
717 (len > 0)
718 ) {
719 79 ▶ force_style = (force_style == scalar_2quote) ? scalar_2quote : scalar_1quote;
Branch: 1 branch
BranchTrueFalseTotal
force_style = (force_style == scalar_2quote) ? scalar_2quote : scalar_1quote; 3 76 79
720 } else {
721 /* complex key -- disabled by Audrey Tang -/
722 if ( parent->status == syck_lvl_map && parent->ncount % 2 == 1 &&
723 ( !( tag == NULL ||
724 ( implicit != NULL && syck_tagcmp( tag, implicit ) == 0 && e->explicit_typing == 0 ) ) ) )
725 {
726 syck_emitter_write( e, "? ", 2 );
727 parent->status = syck_lvl_mapx;
728 }
729 */
730 3311 syck_emit_tag( e, tag, implicit );
731 }
732  
733 /* if still arbitrary, sniff a good block style. */
734 3390 ▶ if ( force_style == scalar_none ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( force_style == scalar_none ) { 2252 1138 3390
735 2252 ▶ if ( scan & SCAN_NEWLINE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( scan & SCAN_NEWLINE ) { 8 2244 2252
736 8 force_style = scalar_literal;
737 } else {
738 2244 force_style = scalar_plain;
739 }
740 }
741  
742 3390 ▶ if ( e->style == scalar_fold ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( e->style == scalar_fold ) { 351 3039 3390
743 351 favor_style = scalar_fold;
744 }
745  
746 /* Determine block style */
747 3390 ▶ if ( scan & SCAN_NONPRINT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( scan & SCAN_NONPRINT ) { 254 3136 3390
748 254 ▶ if ( force_style != scalar_2quote_1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( force_style != scalar_2quote_1 ) 242 12 254
749 242 force_style = scalar_2quote;
750 3136 ▶ } else if ( force_style != scalar_1quote && force_style != scalar_2quote_1 && ( scan & SCAN_WHITEEDGE ) ) {
Branch: 3 branches
BranchTrueFalseTotal
} else if ( force_style != scalar_1quote && force_style != scalar_2quote_1 && ( scan & SCAN_WHITEEDGE ) ) { 2478 658 3136
} else if ( force_style != scalar_1quote && force_style != scalar_2quote_1 && ( scan & SCAN_WHITEEDGE ) ) { 2432 46 2478
} else if ( force_style != scalar_1quote && force_style != scalar_2quote_1 && ( scan & SCAN_WHITEEDGE ) ) { 4 2428 2432
751 4 force_style = scalar_2quote;
752 3132 ▶ } else if ( force_style != scalar_fold && ( scan & SCAN_INDENTED ) ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( force_style != scalar_fold && ( scan & SCAN_INDENTED ) ) { 2921 211 3132
} else if ( force_style != scalar_fold && ( scan & SCAN_INDENTED ) ) { 1 2920 2921
753 1 force_style = scalar_literal;
754 3131 ▶ } else if ( force_style == scalar_plain && ( scan & SCAN_NEWLINE ) ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( force_style == scalar_plain && ( scan & SCAN_NEWLINE ) ) { 2069 1062 3131
} else if ( force_style == scalar_plain && ( scan & SCAN_NEWLINE ) ) { 0 2069 2069
755 0 force_style = favor_style;
756 3131 ▶ } else if ( force_style == scalar_plain && parent->status == syck_lvl_iseq && ( scan & SCAN_FLOWSEQ ) ) {
Branch: 3 branches
BranchTrueFalseTotal
} else if ( force_style == scalar_plain && parent->status == syck_lvl_iseq && ( scan & SCAN_FLOWSEQ ) ) { 2069 1062 3131
} else if ( force_style == scalar_plain && parent->status == syck_lvl_iseq && ( scan & SCAN_FLOWSEQ ) ) { 63 2006 2069
} else if ( force_style == scalar_plain && parent->status == syck_lvl_iseq && ( scan & SCAN_FLOWSEQ ) ) { 0 63 63
757 0 force_style = scalar_2quote;
758 3131 ▶ } else if ( force_style == scalar_plain && parent->status == syck_lvl_imap && ( scan & SCAN_FLOWMAP ) ) {
Branch: 3 branches
BranchTrueFalseTotal
} else if ( force_style == scalar_plain && parent->status == syck_lvl_imap && ( scan & SCAN_FLOWMAP ) ) { 2069 1062 3131
} else if ( force_style == scalar_plain && parent->status == syck_lvl_imap && ( scan & SCAN_FLOWMAP ) ) { 22 2047 2069
} else if ( force_style == scalar_plain && parent->status == syck_lvl_imap && ( scan & SCAN_FLOWMAP ) ) { 0 22 22
759 0 force_style = scalar_2quote;
760 /* } else if ( force_style == scalar_fold && ( ! ( scan & SCAN_WIDE ) ) ) {
761 force_style = scalar_literal; */
762 3131 ▶ } else if ( force_style == scalar_plain && ( scan & SCAN_INDIC_S || scan & SCAN_INDIC_C || scan & SCAN_DOCSEP ) ) {
Branch: 4 branches
BranchTrueFalseTotal
} else if ( force_style == scalar_plain && ( scan & SCAN_INDIC_S || scan & SCAN_INDIC_C || scan & SCAN_DOCSEP ) ) { 2069 1062 3131
} else if ( force_style == scalar_plain && ( scan & SCAN_INDIC_S || scan & SCAN_INDIC_C || scan & SCAN_DOCSEP ) ) { 1868 201 2069
} else if ( force_style == scalar_plain && ( scan & SCAN_INDIC_S || scan & SCAN_INDIC_C || scan & SCAN_DOCSEP ) ) { 1864 4 1868
} else if ( force_style == scalar_plain && ( scan & SCAN_INDIC_S || scan & SCAN_INDIC_C || scan & SCAN_DOCSEP ) ) { 3 1861 1864
763 208 ▶ if ( scan & SCAN_NEWLINE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( scan & SCAN_NEWLINE ) { 0 208 208
764 0 force_style = favor_style;
765 } else {
766 208 force_style = scalar_2quote;
767 }
768 }
769  
770 3390 ▶ if ( force_indent > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( force_indent > 0 ) { 0 3390 3390
771 0 lvl->spaces = parent->spaces + force_indent;
772 3390 ▶ } else if ( scan & SCAN_DOCSEP ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( scan & SCAN_DOCSEP ) { 3 3387 3390
773 3 lvl->spaces = parent->spaces + e->indent;
774 }
775  
776 /* For now, all ambiguous keys are going to be double-quoted */
777 3390 ▶ if ( ( parent->status == syck_lvl_map || parent->status == syck_lvl_mapx ) && parent->ncount % 2 == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( ( parent->status == syck_lvl_map || parent->status == syck_lvl_mapx ) && parent->ncount % 2 == 1 ) { 1591 1799 3390
if ( ( parent->status == syck_lvl_map || parent->status == syck_lvl_mapx ) && parent->ncount % 2 == 1 ) { 0 1591 1591
if ( ( parent->status == syck_lvl_map || parent->status == syck_lvl_mapx ) && parent->ncount % 2 == 1 ) { 1096 703 1799
778 1096 ▶ if ( force_style != scalar_plain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( force_style != scalar_plain ) { 20 1076 1096
779 20 force_style = scalar_2quote;
780 }
781 }
782  
783 /* If the parent is an inline, double quote anything complex */
784 3390 ▶ if ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) { 3273 117 3390
if ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) { 87 3186 3273
785 204 ▶ if ( force_style != scalar_plain && force_style != scalar_1quote && force_style != scalar_2quote_1) {
Branch: 3 branches
BranchTrueFalseTotal
if ( force_style != scalar_plain && force_style != scalar_1quote && force_style != scalar_2quote_1) { 119 85 204
if ( force_style != scalar_plain && force_style != scalar_1quote && force_style != scalar_2quote_1) { 119 0 119
if ( force_style != scalar_plain && force_style != scalar_1quote && force_style != scalar_2quote_1) { 89 30 119
786 89 force_style = scalar_2quote;
787 }
788 }
789  
790 /* Fix the ending newlines */
791 3390 ▶ if ( scan & SCAN_NONL_E ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( scan & SCAN_NONL_E ) { 3354 36 3390
792 3354 keep_nl = NL_CHOMP;
793 36 ▶ } else if ( scan & SCAN_MANYNL_E ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( scan & SCAN_MANYNL_E ) { 2 34 36
794 2 keep_nl = NL_KEEP;
795 }
796  
797 /* A key/value like C<foo: :bar> will be interpreted as ":bar" by Perl, but
798 * Ruby will treat it as the symbol :bar. For compatability with Ruby emit
799 * these with quotes.
800 */
801 3390 ▶ if ( force_style == scalar_plain &&
Branch: 1 branch
BranchTrueFalseTotal
if ( force_style == scalar_plain && 1861 1529 3390
802 1861 ▶ strncmp( implicit, "str", 4 ) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
strncmp( implicit, "str", 4 ) == 0 && 1321 540 1861
803 1321 ▶ str[0] == ':' ) {
Branch: 1 branch
BranchTrueFalseTotal
str[0] == ':' ) { 127 1194 1321
804 127 force_style = scalar_literal;
805 }
806  
807 /* Write the text node */
808 3390 switch ( force_style )
809 {
810 641 case scalar_1quote:
811 641 syck_emit_1quoted( e, force_width, str, len );
812 641 break;
813  
814 612 case scalar_none:
815 case scalar_2quote:
816 612 syck_emit_2quoted( e, force_width, str, len );
817 612 break;
818  
819 211 case scalar_fold:
820 211 syck_emit_folded( e, force_width, keep_nl, str, len );
821 211 break;
822  
823 58 case scalar_2quote_1:
824 58 syck_emit_2quoted_1( e, force_width, str, len );
825 58 break;
826  
827 134 case scalar_literal:
828 /* syck_emit_literal( e, keep_nl, str, len ); */
829 134 syck_emit_2quoted( e, force_width, str, len );
830 134 break;
831  
832 1734 case scalar_plain:
833 1734 syck_emitter_write( e, str, len );
834 1734 break;
835 }
836  
837 3390 ▶ if ( parent->status == syck_lvl_mapx )
Branch: 1 branch
BranchTrueFalseTotal
if ( parent->status == syck_lvl_mapx ) 0 3390 3390
838 {
839 0 syck_emitter_write( e, "\n", 1 );
840 }
841 3390 }
842  
843 void
844 4085 syck_emitter_escape( SyckEmitter *e, unsigned char *src, long len )
845 {
846 int i;
847 8170 ▶ for( i = 0; i < len; i++ )
Branch: 1 branch
BranchTrueFalseTotal
for( i = 0; i < len; i++ ) 4085 4085 8170
848 {
849 /* XXX - scalar_fold overloaded to mean utf8 from Audrey Tang */
850 8170 ▶ if( e->json_mode
Branch: 2 branches
BranchTrueFalseTotal
if( e->json_mode 760 3325 4085
if( e->json_mode 618 3467 4085
851 760 ? (src[i] < 0x20)
852 3325 ▶ : (e->style == scalar_fold)
Branch: 1 branch
BranchTrueFalseTotal
: (e->style == scalar_fold) 431 2894 3325
853 431 ▶ ? ((src[i] < 0x20) && (0 < src[i]))
Branch: 2 branches
BranchTrueFalseTotal
? ((src[i] < 0x20) && (0 < src[i])) 0 431 431
? ((src[i] < 0x20) && (0 < src[i])) 0 0 0
854 2894 ▶ : ((src[i] < 0x20) || (0x7E < src[i])) )
Branch: 2 branches
BranchTrueFalseTotal
: ((src[i] < 0x20) || (0x7E < src[i])) ) 2894 0 2894
: ((src[i] < 0x20) || (0x7E < src[i])) ) 604 2290 2894
855 {
856 618 ▶ if( e->json_mode )
Branch: 1 branch
BranchTrueFalseTotal
if( e->json_mode ) 14 604 618
857 {
858 /* JSON only allows \uXXXX for arbitrary characters */
859 char ubuf[7];
860 14 sprintf( ubuf, "\\u%04x", src[i] );
861 14 syck_emitter_write( e, ubuf, 6 );
862 }
863 else
864 {
865 604 syck_emitter_write( e, "\\", 1 );
866 604 ▶ if( '\0' == src[i] )
Branch: 1 branch
BranchTrueFalseTotal
if( '\0' == src[i] ) 0 604 604
867 0 syck_emitter_write( e, "0", 1 );
868 else
869 {
870 604 syck_emitter_write( e, "x", 1 );
871 604 syck_emitter_write( e, (char *)hex_table + ((src[i] & 0xF0) >> 4), 1 );
872 604 syck_emitter_write( e, (char *)hex_table + (src[i] & 0x0F), 1 );
873 }
874 }
875 }
876 else
877 {
878 3467 syck_emitter_write( e, (const char *)src + i, 1 );
879 3467 ▶ if( '\\' == src[i] )
Branch: 1 branch
BranchTrueFalseTotal
if( '\\' == src[i] ) 0 3467 3467
880 0 syck_emitter_write( e, "\\", 1 );
881 }
882 }
883 4085 }
884  
885 /*
886 * Outputs a single-quoted block.
887 */
888 641 void syck_emit_1quoted( SyckEmitter *e, int width, char *str, long len )
889 {
890 641 char do_indent = 0;
891 641 char *mark = str;
892 641 char *start = str;
893 641 syck_emitter_write( e, "'", 1 );
894 1751 ▶ while ( mark < str + len ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( mark < str + len ) { 1110 641 1751
895 1110 switch ( *mark ) {
896 1 case '\'': syck_emitter_write( e, "''", 2 ); break;
897  
898 6 case '\n':
899 6 ▶ if ( *start != '\n' || start == str ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( *start != '\n' || start == str ) { 1 5 6
if ( *start != '\n' || start == str ) { 1 0 1
900 6 syck_emitter_write( e, "\n\n", 2 );
901 } else {
902 0 syck_emitter_write( e, "\n", 1 );
903 }
904 6 start = mark + 1;
905 6 break;
906  
907 1103 default:
908 1103 syck_emitter_write( e, mark, 1 );
909 1103 break;
910 }
911 1110 mark++;
912 }
913 641 syck_emitter_write( e, "'", 1 );
914 641 }
915  
916 /*
917 * Outputs a double-quoted block.
918 */
919 58 void syck_emit_2quoted_1( SyckEmitter *e, int width, char *str, long len )
920 {
921 58 char do_indent = 0;
922 58 char *mark = str;
923 58 char *start = str;
924 58 char *end = str;
925 58 syck_emitter_write( e, "\'", 1 );
926 212 ▶ while ( mark < str + len ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( mark < str + len ) { 154 58 212
927 154 ▶ if ( do_indent > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( do_indent > 0 ) { 0 154 154
928 0 ▶ if ( do_indent == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( do_indent == 2 ) { 0 0 0
929 0 syck_emitter_write( e, "\\", 1 );
930 }
931 0 syck_emit_indent( e );
932 0 do_indent = 0;
933 }
934 154 switch ( *mark ) {
935  
936 /* Escape sequences allowed within double quotes. */
937 9 case '\'': syck_emitter_write( e, "\\\'", 2 ); break;
938 0 case '\\': syck_emitter_write( e, "\\\\", 2 ); break;
939 2 case '\b': syck_emitter_write( e, "\\b", 2 ); break;
940 2 case '\f': syck_emitter_write( e, "\\f", 2 ); break;
941 4 case '\r': syck_emitter_write( e, "\\r", 2 ); break;
942 2 case '\t': syck_emitter_write( e, "\\t", 2 ); break;
943 4 case '\n': syck_emitter_write( e, "\\n", 2 ); break;
944  
945 /* Solidus: escape in JSON mode for XSS safety (RFC 8259 §7) */
946 6 case '/':
947 6 ▶ if ( e->json_mode ) { syck_emitter_write( e, "\\/", 2 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ( e->json_mode ) { syck_emitter_write( e, "\\/", 2 ); } 6 0 6
948 0 else { syck_emitter_write( e, "/", 1 ); }
949 6 break;
950  
951 /* YAML-only escapes: use \uXXXX in JSON mode */
952 0 case '\0':
953 0 ▶ if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); } 0 0 0
954 0 else { syck_emitter_write( e, "\\0", 2 ); }
955 0 break;
956 0 case '\a':
957 0 ▶ if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); } 0 0 0
958 0 else { syck_emitter_write( e, "\\a", 2 ); }
959 0 break;
960 0 case '\v':
961 0 ▶ if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); } 0 0 0
962 0 else { syck_emitter_write( e, "\\v", 2 ); }
963 0 break;
964 0 case 0x1b:
965 0 ▶ if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); } 0 0 0
966 0 else { syck_emitter_write( e, "\\e", 2 ); }
967 0 break;
968  
969 /* XXX - Disabled by Audrey Tang for YAML.pm compat
970 case '\n':
971 end = mark + 1;
972 syck_emitter_write( e, "\\n", 2 );
973 do_indent = e->indent;
974 start = mark + 1;
975 if ( start < str + len && ( *start == ' ' || *start == '\n' ) ) {
976 do_indent = 0;
977 }
978 break;
979 */
980  
981 3 case ' ':
982 3 ▶ if ( width > 0 && *start != ' ' && mark - end > width ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( width > 0 && *start != ' ' && mark - end > width ) { 0 3 3
if ( width > 0 && *start != ' ' && mark - end > width ) { 0 0 0
if ( width > 0 && *start != ' ' && mark - end > width ) { 0 0 0
983 0 do_indent = 1;
984 0 end = mark + 1;
985 } else {
986 3 syck_emitter_write( e, " ", 1 );
987 }
988 3 break;
989  
990 122 default:
991 122 syck_emitter_escape( e, (unsigned char *)mark, 1 );
992 122 break;
993 }
994 154 mark++;
995 }
996 58 syck_emitter_write( e, "\'", 1 );
997 58 }
998  
999 /*
1000 * Outputs a double-quoted block.
1001 */
1002 746 void syck_emit_2quoted( SyckEmitter *e, int width, char *str, long len )
1003 {
1004 746 char do_indent = 0;
1005 746 char *mark = str;
1006 746 char *start = str;
1007 746 char *end = str;
1008 746 syck_emitter_write( e, "\"", 1 );
1009 5039 ▶ while ( mark < str + len ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( mark < str + len ) { 4293 746 5039
1010 4293 ▶ if ( do_indent > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( do_indent > 0 ) { 0 4293 4293
1011 0 ▶ if ( do_indent == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( do_indent == 2 ) { 0 0 0
1012 0 syck_emitter_write( e, "\\", 1 );
1013 }
1014 0 syck_emit_indent( e );
1015 0 do_indent = 0;
1016 }
1017 4293 switch ( *mark ) {
1018  
1019 /* Escape sequences allowed within double quotes. */
1020 8 case '"': syck_emitter_write( e, "\\\"", 2 ); break;
1021 2 case '\\': syck_emitter_write( e, "\\\\", 2 ); break;
1022 4 case '\b': syck_emitter_write( e, "\\b", 2 ); break;
1023 4 case '\f': syck_emitter_write( e, "\\f", 2 ); break;
1024 11 case '\r': syck_emitter_write( e, "\\r", 2 ); break;
1025 9 case '\t': syck_emitter_write( e, "\\t", 2 ); break;
1026 35 case '\n': syck_emitter_write( e, "\\n", 2 ); break;
1027  
1028 /* Solidus: escape in JSON mode for XSS safety (RFC 8259 §7) */
1029 10 case '/':
1030 10 ▶ if ( e->json_mode ) { syck_emitter_write( e, "\\/", 2 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ( e->json_mode ) { syck_emitter_write( e, "\\/", 2 ); } 10 0 10
1031 0 else { syck_emitter_write( e, "/", 1 ); }
1032 10 break;
1033  
1034 /* YAML-only escapes: use \uXXXX in JSON mode */
1035 205 case '\0':
1036 205 ▶ if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); } 2 203 205
1037 203 else { syck_emitter_write( e, "\\0", 2 ); }
1038 205 break;
1039 3 case '\a':
1040 3 ▶ if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); } 1 2 3
1041 2 else { syck_emitter_write( e, "\\a", 2 ); }
1042 3 break;
1043 1 case '\v':
1044 1 ▶ if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); } 1 0 1
1045 0 else { syck_emitter_write( e, "\\v", 2 ); }
1046 1 break;
1047 3 case 0x1b:
1048 3 ▶ if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); }
Branch: 1 branch
BranchTrueFalseTotal
if ( e->json_mode ) { syck_emitter_escape( e, (unsigned char *)mark, 1 ); } 1 2 3
1049 2 else { syck_emitter_write( e, "\\e", 2 ); }
1050 3 break;
1051  
1052 /* XXX - Disabled by Audrey Tang for YAML.pm compat
1053 case '\n':
1054 end = mark + 1;
1055 syck_emitter_write( e, "\\n", 2 );
1056 do_indent = e->indent;
1057 start = mark + 1;
1058 if ( start < str + len && ( *start == ' ' || *start == '\n' ) ) {
1059 do_indent = 0;
1060 }
1061 break;
1062 */
1063  
1064 40 case ' ':
1065 40 ▶ if ( width > 0 && *start != ' ' && mark - end > width ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( width > 0 && *start != ' ' && mark - end > width ) { 0 40 40
if ( width > 0 && *start != ' ' && mark - end > width ) { 0 0 0
if ( width > 0 && *start != ' ' && mark - end > width ) { 0 0 0
1066 0 do_indent = 1;
1067 0 end = mark + 1;
1068 } else {
1069 40 syck_emitter_write( e, " ", 1 );
1070 }
1071 40 break;
1072  
1073 3958 default:
1074 3958 syck_emitter_escape( e, (unsigned char*)mark, 1 );
1075 3958 break;
1076 }
1077 4293 mark++;
1078 }
1079 746 syck_emitter_write( e, "\"", 1 );
1080 746 }
1081  
1082 /*
1083 * Outputs a literal block.
1084 */
1085 0 void syck_emit_literal( SyckEmitter *e, char keep_nl, char *str, long len )
1086 {
1087 0 char *mark = str;
1088 0 char *start = str;
1089 0 char *end = str;
1090 0 syck_emitter_write( e, "|", 1 );
1091 0 ▶ if ( keep_nl == NL_CHOMP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( keep_nl == NL_CHOMP ) { 0 0 0
1092 0 syck_emitter_write( e, "-", 1 );
1093 0 ▶ } else if ( keep_nl == NL_KEEP ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( keep_nl == NL_KEEP ) { 0 0 0
1094 0 syck_emitter_write( e, "+", 1 );
1095 }
1096 0 syck_emit_indent( e );
1097 0 ▶ while ( mark < str + len ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( mark < str + len ) { 0 0 0
1098 0 ▶ if ( *mark == '\n' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( *mark == '\n' ) { 0 0 0
1099 0 end = mark;
1100 0 ▶ if ( *start != ' ' && *start != '\n' && *end != '\n' && *end != ' ' ) end += 1;
Branch: 4 branches
BranchTrueFalseTotal
if ( *start != ' ' && *start != '\n' && *end != '\n' && *end != ' ' ) end += 1; 0 0 0
if ( *start != ' ' && *start != '\n' && *end != '\n' && *end != ' ' ) end += 1; 0 0 0
if ( *start != ' ' && *start != '\n' && *end != '\n' && *end != ' ' ) end += 1; 0 0 0
if ( *start != ' ' && *start != '\n' && *end != '\n' && *end != ' ' ) end += 1; 0 0 0
1101 0 syck_emitter_write( e, start, end - start );
1102 0 ▶ if ( mark + 1 == str + len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( mark + 1 == str + len ) { 0 0 0
1103 0 ▶ if ( keep_nl != NL_KEEP ) syck_emitter_write( e, "\n", 1 );
Branch: 1 branch
BranchTrueFalseTotal
if ( keep_nl != NL_KEEP ) syck_emitter_write( e, "\n", 1 ); 0 0 0
1104 } else {
1105 0 syck_emit_indent( e );
1106 }
1107 0 start = mark + 1;
1108 }
1109 0 mark++;
1110 }
1111 0 end = str + len;
1112 0 ▶ if ( start < end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( start < end ) { 0 0 0
1113 0 syck_emitter_write( e, start, end - start );
1114 }
1115 0 }
1116  
1117 /*
1118 * Outputs a folded block.
1119 */
1120 211 void syck_emit_folded( SyckEmitter *e, int width, char keep_nl, char *str, long len )
1121 {
1122 211 char *mark = str;
1123 211 char *start = str;
1124 211 char *end = str;
1125 211 syck_emitter_write( e, ">", 1 );
1126 211 ▶ if ( keep_nl == NL_CHOMP ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( keep_nl == NL_CHOMP ) { 211 0 211
1127 211 syck_emitter_write( e, "-", 1 );
1128 0 ▶ } else if ( keep_nl == NL_KEEP ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( keep_nl == NL_KEEP ) { 0 0 0
1129 0 syck_emitter_write( e, "+", 1 );
1130 }
1131 211 syck_emit_indent( e );
1132 211 ▶ if ( width <= 0 ) width = e->best_width;
Branch: 1 branch
BranchTrueFalseTotal
if ( width <= 0 ) width = e->best_width; 211 0 211
1133 5883 ▶ while ( mark < str + len ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( mark < str + len ) { 5672 211 5883
1134 5672 switch ( *mark ) {
1135 625 case '\n':
1136 625 syck_emitter_write( e, end, mark - end );
1137 625 end = mark + 1;
1138 625 ▶ if ( *start != ' ' && *start != '\n' && *end != '\n' && *end != ' ' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ( *start != ' ' && *start != '\n' && *end != '\n' && *end != ' ' ) { 210 415 625
if ( *start != ' ' && *start != '\n' && *end != '\n' && *end != ' ' ) { 210 0 210
if ( *start != ' ' && *start != '\n' && *end != '\n' && *end != ' ' ) { 210 0 210
if ( *start != ' ' && *start != '\n' && *end != '\n' && *end != ' ' ) { 0 210 210
1139 0 syck_emitter_write( e, "\n", 1 );
1140 }
1141 625 ▶ if ( mark + 1 == str + len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( mark + 1 == str + len ) { 0 625 625
1142 0 ▶ if ( keep_nl != NL_KEEP ) syck_emitter_write( e, "\n", 1 );
Branch: 1 branch
BranchTrueFalseTotal
if ( keep_nl != NL_KEEP ) syck_emitter_write( e, "\n", 1 ); 0 0 0
1143 } else {
1144 625 syck_emit_indent( e );
1145 }
1146 625 start = mark + 1;
1147 625 break;
1148  
1149 1880 case ' ':
1150 1880 ▶ if ( *start != ' ' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( *start != ' ' ) { 6 1874 1880
1151 6 ▶ if ( mark - end > width ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( mark - end > width ) { 0 6 6
1152 0 syck_emitter_write( e, end, mark - end );
1153 0 syck_emit_indent( e );
1154 0 end = mark + 1;
1155 }
1156 }
1157 1880 break;
1158 }
1159 5672 mark++;
1160 }
1161 211 ▶ if ( end < mark ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( end < mark ) { 211 0 211
1162 211 syck_emitter_write( e, end, mark - end );
1163 }
1164 211 }
1165  
1166 /*
1167 * Begins emission of a sequence.
1168 */
1169 1921 void syck_emit_seq( SyckEmitter *e, char *tag, enum seq_style style )
1170 {
1171 1921 SyckLevel *parent = syck_emitter_parent_level( e );
1172 1921 SyckLevel *lvl = syck_emitter_current_level( e );
1173  
1174 /* complex key
1175 * There should also be the check "&& style == seq_none",
1176 * but unfortunately syck cannot parse flow collections as simple keys
1177 * now, so we will make a complex key.
1178 * Add the check when syck is able to parse "[]: foo" */
1179 1921 ▶ if ( parent->status == syck_lvl_map && parent->ncount % 2 == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( parent->status == syck_lvl_map && parent->ncount % 2 == 1 ) { 0 1921 1921
if ( parent->status == syck_lvl_map && parent->ncount % 2 == 1 ) { 0 0 0
1180 0 syck_emitter_write( e, "? ", 2 );
1181 0 parent->status = syck_lvl_mapx;
1182 }
1183  
1184 1921 syck_emit_tag( e, tag, "tag:yaml.org,2002:seq" );
1185 1921 ▶ if ( style == seq_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( style == seq_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) { 1079 842 1921
if ( style == seq_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) { 1079 0 1079
if ( style == seq_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) { 0 1079 1079
1186 842 syck_emitter_write( e, "[", 1 );
1187 842 lvl->status = syck_lvl_iseq;
1188 } else {
1189 1079 lvl->status = syck_lvl_seq;
1190 }
1191 1921 }
1192  
1193 /*
1194 * Begins emission of a mapping.
1195 */
1196 883 void syck_emit_map( SyckEmitter *e, char *tag, enum map_style style )
1197 {
1198 883 SyckLevel *parent = syck_emitter_parent_level( e );
1199 883 SyckLevel *lvl = syck_emitter_current_level( e );
1200  
1201 /* complex key
1202 * There should also be the check "&& style == map_none",
1203 * but unfortunately syck cannot parse flow collections as simple keys
1204 * now, so we will make a complex key.
1205 * Add the check when syck is able to parse "{}: foo". */
1206 883 ▶ if ( parent->status == syck_lvl_map && parent->ncount % 2 == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( parent->status == syck_lvl_map && parent->ncount % 2 == 1 ) { 0 883 883
if ( parent->status == syck_lvl_map && parent->ncount % 2 == 1 ) { 0 0 0
1207 0 syck_emitter_write( e, "? ", 2 );
1208 0 parent->status = syck_lvl_mapx;
1209 }
1210  
1211 883 syck_emit_tag( e, tag, "tag:yaml.org,2002:map" );
1212 883 ▶ if ( style == map_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( style == map_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) { 821 62 883
if ( style == map_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) { 821 0 821
if ( style == map_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) { 0 821 821
1213 62 syck_emitter_write( e, "{", 1 );
1214 62 lvl->status = syck_lvl_imap;
1215 } else {
1216 821 lvl->status = syck_lvl_map;
1217 }
1218 883 }
1219  
1220 /*
1221 * Handles emitting of a collection item (for both
1222 * sequences and maps)
1223 */
1224 7006 void syck_emit_item( SyckEmitter *e, st_data_t n )
1225 {
1226 7006 SyckLevel *lvl = syck_emitter_current_level( e );
1227 7006 switch ( lvl->status )
1228 {
1229 1710 case syck_lvl_seq:
1230 {
1231 1710 SyckLevel *parent = syck_emitter_parent_level( e );
1232  
1233 /* seq-in-map shortcut */
1234 1710 ▶ if ( parent->status == syck_lvl_mapx && lvl->ncount == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( parent->status == syck_lvl_mapx && lvl->ncount == 0 ) { 0 1710 1710
if ( parent->status == syck_lvl_mapx && lvl->ncount == 0 ) { 0 0 0
1235 /* shortcut -- the lvl->anctag check should be unneccesary but
1236 * there is a nasty shift/reduce in the parser on this point and
1237 * i'm not ready to tickle it. */
1238 0 ▶ if ( parent->ncount % 2 == 0 && lvl->anctag == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( parent->ncount % 2 == 0 && lvl->anctag == 0 ) { 0 0 0
if ( parent->ncount % 2 == 0 && lvl->anctag == 0 ) { 0 0 0
1239 0 lvl->spaces = parent->spaces;
1240 }
1241 }
1242  
1243 /* seq-in-seq shortcut */
1244 1710 ▶ else if ( lvl->anctag == 0 && parent->status == syck_lvl_seq && lvl->ncount == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
else if ( lvl->anctag == 0 && parent->status == syck_lvl_seq && lvl->ncount == 0 ) { 0 1710 1710
else if ( lvl->anctag == 0 && parent->status == syck_lvl_seq && lvl->ncount == 0 ) { 0 0 0
else if ( lvl->anctag == 0 && parent->status == syck_lvl_seq && lvl->ncount == 0 ) { 0 0 0
1245 0 int spcs = ( lvl->spaces - parent->spaces ) - 2;
1246 0 ▶ if ( spcs >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( spcs >= 0 ) { 0 0 0
1247 0 int i = 0;
1248 0 ▶ for ( i = 0; i < spcs; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < spcs; i++ ) { 0 0 0
1249 0 syck_emitter_write( e, " ", 1 );
1250 }
1251 0 syck_emitter_write( e, "- ", 2 );
1252 0 break;
1253 }
1254 }
1255  
1256 1710 syck_emit_indent( e );
1257 1710 syck_emitter_write( e, "- ", 2 );
1258 }
1259 1710 break;
1260  
1261 898 case syck_lvl_iseq:
1262 {
1263 898 ▶ if ( lvl->ncount > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->ncount > 0 ) { 57 841 898
1264 57 syck_emitter_write( e, ", ", 2 );
1265 }
1266 }
1267 898 break;
1268  
1269 2192 case syck_lvl_map:
1270 {
1271 2192 SyckLevel *parent = syck_emitter_parent_level( e );
1272  
1273 /* map-in-seq shortcut */
1274 2192 ▶ if ( lvl->anctag == 0 && parent->status == syck_lvl_seq && lvl->ncount == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( lvl->anctag == 0 && parent->status == syck_lvl_seq && lvl->ncount == 0 ) { 0 2192 2192
if ( lvl->anctag == 0 && parent->status == syck_lvl_seq && lvl->ncount == 0 ) { 0 0 0
if ( lvl->anctag == 0 && parent->status == syck_lvl_seq && lvl->ncount == 0 ) { 0 0 0
1275 0 int spcs = ( lvl->spaces - parent->spaces ) - 2;
1276 0 ▶ if ( spcs >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( spcs >= 0 ) { 0 0 0
1277 0 int i = 0;
1278 0 ▶ for ( i = 0; i < spcs; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < spcs; i++ ) { 0 0 0
1279 0 syck_emitter_write( e, " ", 1 );
1280 }
1281 0 break;
1282 }
1283 }
1284  
1285 2192 ▶ if ( lvl->ncount % 2 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->ncount % 2 == 0 ) { 1096 1096 2192
1286 1096 syck_emit_indent( e );
1287 } else {
1288 1096 syck_emitter_write( e, ": ", 2 );
1289 }
1290 }
1291 2192 break;
1292  
1293 0 case syck_lvl_mapx:
1294 {
1295 0 ▶ if ( lvl->ncount % 2 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->ncount % 2 == 0 ) { 0 0 0
1296 0 syck_emit_indent( e );
1297 0 lvl->status = syck_lvl_map;
1298 } else {
1299 int i;
1300 0 ▶ if ( lvl->spaces > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->spaces > 0 ) { 0 0 0
1301 0 char *spcs = S_ALLOC_N( char, lvl->spaces + 1 );
1302  
1303 0 spcs[lvl->spaces] = '\0';
1304 0 ▶ for ( i = 0; i < lvl->spaces; i++ ) spcs[i] = ' ';
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < lvl->spaces; i++ ) spcs[i] = ' '; 0 0 0
1305 0 syck_emitter_write( e, spcs, lvl->spaces );
1306 0 S_FREE( spcs );
1307 }
1308 0 syck_emitter_write( e, ": ", 2 );
1309 }
1310 }
1311 0 break;
1312  
1313 126 case syck_lvl_imap:
1314 {
1315 126 ▶ if ( lvl->ncount > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->ncount > 0 ) { 67 59 126
1316 67 ▶ if ( lvl->ncount % 2 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->ncount % 2 == 0 ) { 4 63 67
1317 4 syck_emitter_write( e, ", ", 2 );
1318 } else {
1319 63 syck_emitter_write( e, ": ", 2 );
1320 }
1321 }
1322 }
1323 126 break;
1324  
1325 2080 default: break;
1326 }
1327 7006 lvl->ncount++;
1328  
1329 7006 syck_emit( e, n );
1330 6985 }
1331  
1332 /*
1333 * Closes emission of a collection.
1334 */
1335 2815 void syck_emit_end( SyckEmitter *e )
1336 {
1337 2815 SyckLevel *lvl = syck_emitter_current_level( e );
1338 2815 SyckLevel *parent = syck_emitter_parent_level( e );
1339 2815 switch ( lvl->status )
1340 {
1341 1079 case syck_lvl_seq:
1342 1079 ▶ if ( lvl->ncount == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->ncount == 0 ) { 11 1068 1079
1343 11 syck_emitter_write( e, "[]", 2 );
1344 11 ▶ if ( parent->status == syck_lvl_mapx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( parent->status == syck_lvl_mapx ) { 0 11 11
1345 0 syck_emitter_write( e, "\n", 1 );
1346 }
1347 1068 ▶ } else if ( parent->status == syck_lvl_mapx ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( parent->status == syck_lvl_mapx ) { 0 1068 1068
1348 0 syck_emitter_write( e, "\n", 1 );
1349 }
1350 1079 break;
1351  
1352 842 case syck_lvl_iseq:
1353 842 syck_emitter_write( e, "]", 1 );
1354 842 ▶ if ( parent->status == syck_lvl_mapx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( parent->status == syck_lvl_mapx ) { 0 842 842
1355 0 syck_emitter_write( e, "\n", 1 );
1356 }
1357 842 break;
1358  
1359 821 case syck_lvl_map:
1360 821 ▶ if ( lvl->ncount == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->ncount == 0 ) { 45 776 821
1361 45 syck_emitter_write( e, "{}", 2 );
1362 45 ▶ if ( parent->status == syck_lvl_mapx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( parent->status == syck_lvl_mapx ) { 0 45 45
1363 0 syck_emitter_write( e, "\n", 1 );
1364 }
1365 776 ▶ } else if ( lvl->ncount % 2 == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( lvl->ncount % 2 == 1 ) { 0 776 776
1366 0 syck_emitter_write( e, ":", 1 );
1367 776 ▶ } else if ( parent->status == syck_lvl_mapx ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( parent->status == syck_lvl_mapx ) { 0 776 776
1368 0 syck_emitter_write( e, "\n", 1 );
1369 }
1370 821 break;
1371  
1372 62 case syck_lvl_imap:
1373 62 syck_emitter_write( e, "}", 1 );
1374 62 ▶ if ( parent->status == syck_lvl_mapx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( parent->status == syck_lvl_mapx ) { 0 62 62
1375 0 syck_emitter_write( e, "\n", 1 );
1376 }
1377 62 break;
1378  
1379 11 default: break;
1380 }
1381 2815 }
1382  
1383 /*
1384 * Fill markers table with emitter nodes in the
1385 * soon-to-be-emitted tree.
1386 */
1387 SYMID
1388 33340 syck_emitter_mark_node( SyckEmitter *e, st_data_t n, int flags )
1389 {
1390 33340 SYMID oid = 0;
1391 33340 char *anchor_name = NULL;
1392  
1393 /*
1394 * Ensure markers table is initialized.
1395 */
1396 33340 ▶ if ( e->markers == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->markers == NULL ) 1528 31812 33340
1397 {
1398 1528 e->markers = st_init_numtable();
1399 }
1400  
1401 /*
1402 * Markers table initially marks the string position of the
1403 * object. Doesn't yet create an anchor, simply notes the
1404 * position.
1405 */
1406 33340 ▶ if ( ! st_lookup( e->markers, n, (st_data_t *)&oid ) )
Branch: 1 branch
BranchTrueFalseTotal
if ( ! st_lookup( e->markers, n, (st_data_t *)&oid ) ) 10798 22542 33340
1407 {
1408 /*
1409 * Store all markers
1410 */
1411 10798 oid = e->markers->num_entries + 1;
1412 10798 st_insert( e->markers, n, (st_data_t)oid );
1413 }
1414 else
1415 {
1416 22542 ▶ if ( e->anchors == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->anchors == NULL ) 86 22456 22542
1417 {
1418 86 e->anchors = st_init_numtable();
1419 }
1420  
1421 22542 ▶ if ( ! st_lookup( e->anchors, (st_data_t)oid, (st_data_t *)&anchor_name ) )
Branch: 1 branch
BranchTrueFalseTotal
if ( ! st_lookup( e->anchors, (st_data_t)oid, (st_data_t *)&anchor_name ) ) 189 22353 22542
1422 {
1423 189 int idx = 0;
1424 189 ▶ char *anc = ( e->anchor_format == NULL ? DEFAULT_ANCHOR_FORMAT : e->anchor_format );
Branch: 1 branch
BranchTrueFalseTotal
char *anc = ( e->anchor_format == NULL ? DEFAULT_ANCHOR_FORMAT : e->anchor_format ); 189 0 189
1425  
1426 /*
1427 * Second time hitting this object, let's give it an anchor
1428 */
1429 189 idx = e->anchors->num_entries + 1;
1430 189 anchor_name = S_ALLOC_N( char, strlen( anc ) + 10 );
1431 189 S_MEMZERO( anchor_name, char, strlen( anc ) + 10 );
1432 189 sprintf( anchor_name, anc, idx );
1433  
1434 /*
1435 * Insert into anchors table
1436 */
1437 189 st_insert( e->anchors, (st_data_t)oid, (st_data_t)anchor_name );
1438 }
1439  
1440 /* XXX - Flag added by BDRACO as the perl_syck.h now has a max_depth - XXX */
1441 22542 ▶ if (! (flags & EMITTER_MARK_NODE_FLAG_PERMIT_DUPLICATE_NODES) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (! (flags & EMITTER_MARK_NODE_FLAG_PERMIT_DUPLICATE_NODES) ) { 190 22352 22542
1442 190 return 0;
1443 }
1444 /* XXX - Added by Audrey Tang to handle self-recursive structures - XXX */
1445 }
1446 33150 return oid;
1447 }
1448  
« node.c ↑ Summary handler.c »