emitter.c

filter: Statementstmt 79.6% 602 / 756 Branchbran 65.7% 388 / 590 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.5% 990 / 1346 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« node.c ↑ Summary syck_.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 201 syck_base64dec( char *s, long len, long *out_len )
63 {
64 201 int a = -1,b = -1,c = 0,d;
65 static int first = 1;
66 static int b64_xtable[256];
67 201 char *ptr = syck_strndup( s, len );
68 201 char *end = ptr;
69 201 char *send = s + len;
70  
71 201 ▶ if (first) {
Branch: 1 branch
BranchTrueFalseTotal
if (first) { 2 199 201
72 int i;
73 2 first = 0;
74  
75 514 ▶ for (i = 0; i < 256; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i++) { 512 2 514
76 512 b64_xtable[i] = -1;
77 }
78 130 ▶ for (i = 0; i < 64; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 64; i++) { 128 2 130
79 128 b64_xtable[(int)b64_table[i]] = i;
80 }
81 }
82 201 ▶ while (s < send) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < send) { 201 0 201
83 201 ▶ while (s < send && (s[0] == '\r' || s[0] == '\n')) { s++; }
Branch: 3 branches
BranchTrueFalseTotal
while (s < send && (s[0] == '\r' || s[0] == '\n')) { s++; } 201 0 201
while (s < send && (s[0] == '\r' || s[0] == '\n')) { s++; } 0 201 201
while (s < send && (s[0] == '\r' || s[0] == '\n')) { s++; } 0 201 201
84 201 ▶ if (s >= send) break;
Branch: 1 branch
BranchTrueFalseTotal
if (s >= send) break; 0 201 201
85 201 ▶ if ((a = b64_xtable[(int)s[0]]) == -1) break;
Branch: 1 branch
BranchTrueFalseTotal
if ((a = b64_xtable[(int)s[0]]) == -1) break; 0 201 201
86 201 ▶ if ((b = b64_xtable[(int)s[1]]) == -1) break;
Branch: 1 branch
BranchTrueFalseTotal
if ((b = b64_xtable[(int)s[1]]) == -1) break; 0 201 201
87 201 ▶ if ((c = b64_xtable[(int)s[2]]) == -1) break;
Branch: 1 branch
BranchTrueFalseTotal
if ((c = b64_xtable[(int)s[2]]) == -1) break; 0 201 201
88 201 ▶ if ((d = b64_xtable[(int)s[3]]) == -1) break;
Branch: 1 branch
BranchTrueFalseTotal
if ((d = b64_xtable[(int)s[3]]) == -1) break; 201 0 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 201 ▶ if (a != -1 && b != -1) {
Branch: 2 branches
BranchTrueFalseTotal
if (a != -1 && b != -1) { 201 0 201
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 201 *end = '\0';
103 /*RSTRING(buf)->len = ptr - RSTRING(buf)->ptr;*/
104  
105 201 *out_len = end - ptr;
106 201 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 202 syck_base64_free( char *ptr )
118 {
119 202 ▶ if ( ptr != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( ptr != NULL ) 202 0 202
120 202 free( ptr );
121 202 }
122  
123 /*
124 * Allocate an emitter
125 */
126 SyckEmitter *
127 1514 syck_new_emitter(void)
128 {
129 SyckEmitter *e;
130 1514 e = S_ALLOC( SyckEmitter );
131 1514 e->headless = 0;
132 1514 e->use_header = 0;
133 1514 e->use_version = 0;
134 1514 e->sort_keys = 0;
135 1514 e->anchor_format = NULL;
136 1514 e->explicit_typing = 0;
137 1514 e->best_width = 80;
138 1514 e->style = scalar_none;
139 1514 e->stage = doc_open;
140 1514 e->depth = 0;
141 1514 e->max_depth = 512;
142 1514 e->indent = 2;
143 1514 e->level = -1;
144 1514 e->anchors = NULL;
145 1514 e->markers = NULL;
146 1514 e->anchored = NULL;
147 1514 e->bufsize = SYCK_BUFFERSIZE;
148 1514 e->buffer = NULL;
149 1514 e->marker = NULL;
150 1514 e->bufpos = 0;
151 1514 e->emitter_handler = NULL;
152 1514 e->output_handler = NULL;
153 1514 e->lvl_idx = 0;
154 1514 e->lvl_capa = ALLOC_CT;
155 1514 e->levels = S_ALLOC_N( SyckLevel, e->lvl_capa );
156 1514 syck_emitter_reset_levels( e );
157 1514 e->json_mode = 0;
158 1514 e->bonus = NULL;
159 1514 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 1514 syck_emitter_st_free( SyckEmitter *e )
171 {
172 /*
173 * Free the anchor tables
174 */
175 1514 ▶ if ( e->anchors != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->anchors != NULL ) 86 1428 1514
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 1514 ▶ if ( e->anchored != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->anchored != NULL ) 40 1474 1514
183 {
184 40 st_free_table( e->anchored );
185 40 e->anchored = NULL;
186 }
187  
188 /*
189 * Free the markers tables
190 */
191 1514 ▶ if ( e->markers != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->markers != NULL ) 1514 0 1514
192 {
193 1514 st_free_table( e->markers );
194 1514 e->markers = NULL;
195 }
196 1514 }
197  
198 SyckLevel *
199 40579 syck_emitter_current_level( SyckEmitter *e )
200 {
201 40579 return &e->levels[e->lvl_idx-1];
202 }
203  
204 SyckLevel *
205 12833 syck_emitter_parent_level( SyckEmitter *e )
206 {
207 12833 return &e->levels[e->lvl_idx-2];
208 }
209  
210 void
211 8433 syck_emitter_pop_level( SyckEmitter *e )
212 {
213 ASSERT( e != NULL );
214  
215 /* The root level should never be popped */
216 8433 ▶ if ( e->lvl_idx <= 1 ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( e->lvl_idx <= 1 ) return; 0 8433 8433
217  
218 8433 e->lvl_idx -= 1;
219 8433 free( e->levels[e->lvl_idx].domain );
220 }
221  
222 void
223 8433 syck_emitter_add_level( SyckEmitter *e, int len, enum syck_level_status status )
224 {
225 ASSERT( e != NULL );
226 8433 ▶ if ( e->lvl_idx + 1 > e->lvl_capa )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->lvl_idx + 1 > e->lvl_capa ) 408 8025 8433
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 8433 e->levels[e->lvl_idx].spaces = len;
234 8433 e->levels[e->lvl_idx].ncount = 0;
235 8433 e->levels[e->lvl_idx].domain = syck_strndup( e->levels[e->lvl_idx-1].domain, strlen( e->levels[e->lvl_idx-1].domain ) );
236 8433 e->levels[e->lvl_idx].status = status;
237 8433 e->levels[e->lvl_idx].anctag = 0;
238 8433 e->lvl_idx += 1;
239 8433 }
240  
241 void
242 3028 syck_emitter_reset_levels( SyckEmitter *e )
243 {
244 3070 ▶ while ( e->lvl_idx > 1 )
Branch: 1 branch
BranchTrueFalseTotal
while ( e->lvl_idx > 1 ) 42 3028 3070
245 {
246 42 syck_emitter_pop_level( e );
247 }
248  
249 3028 ▶ if ( e->lvl_idx < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->lvl_idx < 1 ) 1514 1514 3028
250 {
251 1514 e->lvl_idx = 1;
252 1514 e->levels[0].spaces = -1;
253 1514 e->levels[0].ncount = 0;
254 1514 e->levels[0].domain = syck_strndup( "", 0 );
255 1514 e->levels[0].anctag = 0;
256 }
257 3028 e->levels[0].status = syck_lvl_header;
258 3028 }
259  
260 void
261 1514 syck_emitter_handler( SyckEmitter *e, SyckEmitterHandler hdlr )
262 {
263 1514 e->emitter_handler = hdlr;
264 1514 }
265  
266 void
267 1514 syck_output_handler( SyckEmitter *e, SyckOutputHandler hdlr )
268 {
269 1514 e->output_handler = hdlr;
270 1514 }
271  
272 void
273 1514 syck_free_emitter( SyckEmitter *e )
274 {
275 /*
276 * Free tables
277 */
278 1514 syck_emitter_st_free( e );
279 1514 syck_emitter_reset_levels( e );
280 1514 S_FREE( e->levels[0].domain );
281 1514 S_FREE( e->levels );
282 1514 ▶ if ( e->buffer != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->buffer != NULL ) 1465 49 1514
283 {
284 1465 S_FREE( e->buffer );
285 }
286 1514 S_FREE( e );
287 1514 }
288  
289 void
290 1465 syck_emitter_clear( SyckEmitter *e )
291 {
292 1465 ▶ if ( e->buffer == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->buffer == NULL ) 1465 0 1465
293 {
294 1465 e->buffer = S_ALLOC_N( char, e->bufsize );
295 1465 S_MEMZERO( e->buffer, char, e->bufsize );
296 }
297 1465 e->buffer[0] = '\0';
298 1465 e->marker = e->buffer;
299 1465 e->bufpos = 0;
300 1465 }
301  
302 /*
303 * Raw write to the emitter buffer.
304 */
305 void
306 25925 syck_emitter_write( SyckEmitter *e, const char *str, long len )
307 {
308 long at;
309 ASSERT( str != NULL )
310 25925 ▶ if ( e->buffer == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->buffer == NULL ) 1465 24460 25925
311 {
312 1465 syck_emitter_clear( e );
313 }
314
315 /*
316 * Flush if at end of buffer
317 */
318 25925 at = e->marker - e->buffer;
319 25925 ▶ if ( len + at >= e->bufsize )
Branch: 1 branch
BranchTrueFalseTotal
if ( len + at >= e->bufsize ) 108 25817 25925
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 25925 S_MEMCPY( e->marker, str, char, len );
337 25925 e->marker += len;
338 25925 }
339  
340 /*
341 * Write a chunk of data out.
342 */
343 void
344 1552 syck_emitter_flush( SyckEmitter *e, long check_room )
345 {
346 /*
347 * Check for enough space in the buffer for check_room length.
348 */
349 1552 ▶ if ( check_room > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( check_room > 0 ) 0 1552 1552
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 1552 check_room = e->bufsize;
359 }
360  
361 /*
362 * Commit buffer.
363 */
364 1552 ▶ if ( check_room > e->marker - e->buffer )
Branch: 1 branch
BranchTrueFalseTotal
if ( check_room > e->marker - e->buffer ) 1552 0 1552
365 {
366 1552 check_room = e->marker - e->buffer;
367 }
368 1552 (e->output_handler)( e, e->buffer, check_room );
369 1552 e->bufpos += check_room;
370 1552 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 8433 syck_emit( SyckEmitter *e, st_data_t n )
379 {
380 SYMID oid;
381 8433 char *anchor_name = NULL;
382 8433 int indent = 0;
383 SyckLevel *parent;
384 8433 SyckLevel *lvl = syck_emitter_current_level( e );
385
386 /*
387 * Determine headers.
388 */
389 8433 ▶ 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 ) ) 2495 5938 8433
if ( e->stage == doc_open && ( e->headless == 0 || e->use_header == 1 ) ) 1267 1228 2495
if ( e->stage == doc_open && ( e->headless == 0 || e->use_header == 1 ) ) 0 1267 1267
390 {
391 1228 ▶ if ( e->use_version == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->use_version == 1 ) 0 1228 1228
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 1228 syck_emitter_write( e, "--- ", 4 );
402 }
403 1228 e->stage = doc_processing;
404 }
405  
406 /* Add new level */
407 8433 ▶ if ( lvl->spaces >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->spaces >= 0 ) { 6968 1465 8433
408 6968 indent = lvl->spaces + e->indent;
409 }
410 8433 syck_emitter_add_level( e, indent, syck_lvl_open );
411 8433 parent = lvl;
412 8433 lvl = syck_emitter_current_level( e );
413  
414 /* Look for anchor */
415 9097 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 8247 (e->emitter_handler)( e, n );
453  
454 /* Pop the level */
455 8391 end_emit:
456 8391 syck_emitter_pop_level( e );
457 8391 ▶ if ( e->lvl_idx == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( e->lvl_idx == 1 ) { 1444 6947 8391
458 1444 syck_emitter_write( e, "\n", 1 );
459 1444 e->headless = 0;
460 1444 e->stage = doc_open;
461 }
462 8391 }
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 6075 void syck_emit_tag( SyckEmitter *e, const char *tag, const char *ignore )
470 {
471 SyckLevel *lvl;
472 6075 ▶ if ( tag == NULL ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( tag == NULL ) return; 0 6075 6075
473 6075 ▶ 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; 6075 0 6075
if ( ignore != NULL && syck_tagcmp( tag, ignore ) == 0 && e->explicit_typing == 0 ) return; 1913 4162 6075
if ( ignore != NULL && syck_tagcmp( tag, ignore ) == 0 && e->explicit_typing == 0 ) return; 1913 0 1913
474 4162 lvl = syck_emitter_current_level( e );
475  
476 /* implicit */
477 4162 ▶ if ( strlen( tag ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strlen( tag ) == 0 ) { 0 4162 4162
478 0 syck_emitter_write( e, "! ", 2 );
479  
480 /* global types */
481 4162 ▶ } else if ( strncmp( tag, "tag:", 4 ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( strncmp( tag, "tag:", 4 ) == 0 ) { 494 3668 4162
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 3668 ▶ } else if ( strncmp( tag, "x-private:", 10 ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( strncmp( tag, "x-private:", 10 ) == 0 ) { 0 3668 3668
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 4162 lvl->anctag = 1;
515 }
516  
517 /*
518 * Emit a newline and an appropriately spaced indent.
519 */
520 3626 void syck_emit_indent( SyckEmitter *e )
521 {
522 int i;
523 3626 SyckLevel *lvl = syck_emitter_current_level( e );
524 3626 ▶ if ( e->bufpos == 0 && ( e->marker - e->buffer ) == 0 ) return;
Branch: 2 branches
BranchTrueFalseTotal
if ( e->bufpos == 0 && ( e->marker - e->buffer ) == 0 ) return; 2954 672 3626
if ( e->bufpos == 0 && ( e->marker - e->buffer ) == 0 ) return; 2 2952 2954
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 5454 ▶ while ( e->marker > e->buffer && *(e->marker - 1) == ' ' ) {
Branch: 2 branches
BranchTrueFalseTotal
while ( e->marker > e->buffer && *(e->marker - 1) == ' ' ) { 5454 0 5454
while ( e->marker > e->buffer && *(e->marker - 1) == ' ' ) { 1830 3624 5454
530 1830 e->marker--;
531 }
532  
533 3624 ▶ if ( lvl->spaces >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->spaces >= 0 ) { 3624 0 3624
534 3624 char *spcs = S_ALLOC_N( char, lvl->spaces + 2 );
535  
536 3624 spcs[0] = '\n'; spcs[lvl->spaces + 1] = '\0';
537 408752 ▶ for ( i = 0; i < lvl->spaces; i++ ) spcs[i+1] = ' ';
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < lvl->spaces; i++ ) spcs[i+1] = ' '; 405128 3624 408752
538 3624 syck_emitter_write( e, spcs, lvl->spaces + 1 );
539 3624 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 3362 syck_scan_scalar( int req_width, char *cursor, long len )
579 {
580 3362 long i = 0, start = 0;
581 3362 int flags = SCAN_NONE;
582  
583 3362 ▶ if ( len < 1 ) return flags;
Branch: 1 branch
BranchTrueFalseTotal
if ( len < 1 ) return flags; 15 3347 3362
584  
585 /* c-indicators from the spec */
586 3347 ▶ if ( cursor[0] == '[' || cursor[0] == ']' ||
Branch: 2 branches
BranchTrueFalseTotal
if ( cursor[0] == '[' || cursor[0] == ']' || 3347 0 3347
if ( cursor[0] == '[' || cursor[0] == ']' || 3347 0 3347
587 3347 ▶ cursor[0] == '{' || cursor[0] == '}' ||
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '{' || cursor[0] == '}' || 3137 210 3347
cursor[0] == '{' || cursor[0] == '}' || 3137 0 3137
588 3137 ▶ cursor[0] == '!' || cursor[0] == '*' ||
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '!' || cursor[0] == '*' || 3137 0 3137
cursor[0] == '!' || cursor[0] == '*' || 2937 200 3137
589 2937 ▶ cursor[0] == '&' || cursor[0] == '|' ||
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '&' || cursor[0] == '|' || 2937 0 2937
cursor[0] == '&' || cursor[0] == '|' || 2937 0 2937
590 2937 ▶ cursor[0] == '>' || cursor[0] == '\'' ||
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '>' || cursor[0] == '\'' || 2937 0 2937
cursor[0] == '>' || cursor[0] == '\'' || 2933 4 2937
591 2933 ▶ cursor[0] == '"' || cursor[0] == '#' ||
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '"' || cursor[0] == '#' || 2929 4 2933
cursor[0] == '"' || cursor[0] == '#' || 2929 0 2929
592 2929 ▶ cursor[0] == '%' || cursor[0] == '@' || cursor[0] == '`' ||
Branch: 3 branches
BranchTrueFalseTotal
cursor[0] == '%' || cursor[0] == '@' || cursor[0] == '`' || 2929 0 2929
cursor[0] == '%' || cursor[0] == '@' || cursor[0] == '`' || 2929 0 2929
cursor[0] == '%' || cursor[0] == '@' || cursor[0] == '`' || 2928 1 2929
593 2928 ▶ cursor[0] == '&' || cursor[0] == '^' ) {
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '&' || cursor[0] == '^' ) { 2928 0 2928
cursor[0] == '&' || cursor[0] == '^' ) { 0 2928 2928
594 419 flags |= SCAN_INDIC_S;
595 }
596 3347 ▶ if ( ( cursor[0] == '-' || cursor[0] == ':' ||
Branch: 2 branches
BranchTrueFalseTotal
if ( ( cursor[0] == '-' || cursor[0] == ':' || 3318 29 3347
if ( ( cursor[0] == '-' || cursor[0] == ':' || 3188 130 3318
597 3188 ▶ cursor[0] == '?' || cursor[0] == ',' ) &&
Branch: 2 branches
BranchTrueFalseTotal
cursor[0] == '?' || cursor[0] == ',' ) && 3188 0 3188
cursor[0] == '?' || cursor[0] == ',' ) && 0 3188 3188
598 159 ▶ ( cursor[1] == ' ' || cursor[1] == '\n' || cursor[1] == '\r' || len == 1 ) )
Branch: 4 branches
BranchTrueFalseTotal
( cursor[1] == ' ' || cursor[1] == '\n' || cursor[1] == '\r' || len == 1 ) ) 158 1 159
( cursor[1] == ' ' || cursor[1] == '\n' || cursor[1] == '\r' || len == 1 ) ) 158 0 158
( cursor[1] == ' ' || cursor[1] == '\n' || cursor[1] == '\r' || len == 1 ) ) 158 0 158
( cursor[1] == ' ' || cursor[1] == '\n' || cursor[1] == '\r' || len == 1 ) ) 0 158 158
599 {
600 1 flags |= SCAN_INDIC_S;
601 }
602  
603 /* whitespace edges */
604 3347 ▶ if ( cursor[len-1] != '\n' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( cursor[len-1] != '\n' ) { 3326 21 3347
605 3326 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 3347 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 3347 0 3347
610 3347 ▶ ( len > 0 && ( cursor[0] == ' ' || cursor[0] == '\t' ) ) ||
Branch: 3 branches
BranchTrueFalseTotal
( len > 0 && ( cursor[0] == ' ' || cursor[0] == '\t' ) ) || 3345 2 3347
( len > 0 && ( cursor[0] == ' ' || cursor[0] == '\t' ) ) || 3341 4 3345
( len > 0 && ( cursor[0] == ' ' || cursor[0] == '\t' ) ) || 1675 1666 3341
611 1675 ▶ ( len > 1 && ( cursor[len-1] == ' ' || cursor[len-1] == '\t' ) )
Branch: 2 branches
BranchTrueFalseTotal
( len > 1 && ( cursor[len-1] == ' ' || cursor[len-1] == '\t' ) ) 1672 3 1675
( len > 1 && ( cursor[len-1] == ' ' || cursor[len-1] == '\t' ) ) 0 1672 1672
612 ) {
613 9 flags |= SCAN_WHITEEDGE;
614 }
615  
616 /* opening doc sep or doc end */
617 3347 ▶ 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 ) ) 1593 1754 3347
if ( len >= 3 && ( strncmp( cursor, "---", 3 ) == 0 || strncmp( cursor, "...", 3 ) == 0 ) ) 1592 1 1593
if ( len >= 3 && ( strncmp( cursor, "---", 3 ) == 0 || strncmp( cursor, "...", 3 ) == 0 ) ) 2 1590 1592
618 3 flags |= SCAN_DOCSEP;
619  
620 /* scan string */
621 18301 ▶ for ( i = 0; i < len; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < len; i++ ) { 14954 3347 18301
622  
623 14954 ▶ if ( ! ( (unsigned char)cursor[i] == 0xA ||
Branch: 1 branch
BranchTrueFalseTotal
if ( ! ( (unsigned char)cursor[i] == 0xA || 14284 670 14954
624 14284 ▶ ( (unsigned char)cursor[i] >= 0x20 &&
Branch: 1 branch
BranchTrueFalseTotal
( (unsigned char)cursor[i] >= 0x20 && 14025 259 14284
625 14025 ▶ (unsigned char)cursor[i] <= 0x7E ) ||
Branch: 1 branch
BranchTrueFalseTotal
(unsigned char)cursor[i] <= 0x7E ) || 987 13038 14025
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 14695 ▶ else if ( cursor[i] == '\n' ) {
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == '\n' ) { 670 14025 14695
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 14025 ▶ else if ( cursor[i] == '\'' )
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == '\'' ) 16 14009 14025
641 {
642 16 flags |= SCAN_SINGLEQ;
643 }
644 14009 ▶ else if ( cursor[i] == '"' )
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == '"' ) 14 13995 14009
645 {
646 14 flags |= SCAN_DOUBLEQ;
647 }
648 13995 ▶ else if ( cursor[i] == ']' )
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == ']' ) 4 13991 13995
649 {
650 4 flags |= SCAN_FLOWSEQ;
651 }
652 13991 ▶ else if ( cursor[i] == '}' )
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == '}' ) 215 13776 13991
653 {
654 215 flags |= SCAN_FLOWMAP;
655 }
656 /* remember, if plain collections get implemented, to add nb-plain-flow-char */
657 13776 ▶ else if ( ( cursor[i] == ' ' && cursor[i+1] == '#' ) ||
Branch: 2 branches
BranchTrueFalseTotal
else if ( ( cursor[i] == ' ' && cursor[i+1] == '#' ) || 1986 11790 13776
else if ( ( cursor[i] == ' ' && cursor[i+1] == '#' ) || 1985 1 1986
658 13775 ▶ ( cursor[i] == ':' &&
Branch: 1 branch
BranchTrueFalseTotal
( cursor[i] == ':' && 569 13206 13775
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 13768 ▶ else if ( cursor[i] == ',' &&
Branch: 1 branch
BranchTrueFalseTotal
else if ( cursor[i] == ',' && 10 13758 13768
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 3347 return flags;
673 }
674 /*
675 * All scalars should be emitted through this function, which determines an appropriate style,
676 * tag and indent.
677 */
678 3362 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 3362 enum scalar_style favor_style = scalar_literal;
682 3362 SyckLevel *parent = syck_emitter_parent_level( e );
683 3362 SyckLevel *lvl = syck_emitter_current_level( e );
684 3362 int scan = 0;
685 char *implicit;
686
687 3362 ▶ if ( str == NULL ) str = "";
Branch: 1 branch
BranchTrueFalseTotal
if ( str == NULL ) str = ""; 0 3362 3362
688  
689 /* No empty nulls as map keys */
690 3362 ▶ 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 3347 3362
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 3362 scan = syck_scan_scalar( force_width, str, len );
698 3362 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 3362 if (
705 (
706 3362 ▶ (strncmp( implicit, "bool", 4 ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strncmp( implicit, "bool", 4 ) == 0) || 3324 38 3362
707 3324 ▶ (strncmp( implicit, "null", 4 ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strncmp( implicit, "null", 4 ) == 0) || 3267 57 3324
708 3267 ▶ (strncmp( implicit, "int#", 4 ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strncmp( implicit, "int#", 4 ) == 0) || 3246 21 3267
709 3246 ▶ (strcmp( implicit, "float#inf" ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strcmp( implicit, "float#inf" ) == 0) || 3239 7 3246
710 3239 ▶ (strcmp( implicit, "float#neginf" ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strcmp( implicit, "float#neginf" ) == 0) || 3237 2 3239
711 3237 ▶ (strcmp( implicit, "float#nan" ) == 0) ||
Branch: 1 branch
BranchTrueFalseTotal
(strcmp( implicit, "float#nan" ) == 0) || 3234 3 3237
712 3234 ▶ (strcmp( implicit, "float#base60" ) == 0)
Branch: 1 branch
BranchTrueFalseTotal
(strcmp( implicit, "float#base60" ) == 0) 0 3234 3234
713 )
714 128 ▶ &&
Branch: 1 branch
BranchTrueFalseTotal
&& 94 34 128
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 3283 syck_emit_tag( e, tag, implicit );
731 }
732  
733 /* if still arbitrary, sniff a good block style. */
734 3362 ▶ if ( force_style == scalar_none ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( force_style == scalar_none ) { 2762 600 3362
735 2762 ▶ if ( scan & SCAN_NEWLINE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( scan & SCAN_NEWLINE ) { 8 2754 2762
736 8 force_style = scalar_literal;
737 } else {
738 2754 force_style = scalar_plain;
739 }
740 }
741  
742 3362 ▶ if ( e->style == scalar_fold ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( e->style == scalar_fold ) { 351 3011 3362
743 351 favor_style = scalar_fold;
744 }
745  
746 /* Determine block style */
747 3362 ▶ if ( scan & SCAN_NONPRINT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( scan & SCAN_NONPRINT ) { 254 3108 3362
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 3108 ▶ } 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 ) ) { 2982 126 3108
} else if ( force_style != scalar_1quote && force_style != scalar_2quote_1 && ( scan & SCAN_WHITEEDGE ) ) { 2936 46 2982
} else if ( force_style != scalar_1quote && force_style != scalar_2quote_1 && ( scan & SCAN_WHITEEDGE ) ) { 4 2932 2936
751 4 force_style = scalar_2quote;
752 3104 ▶ } else if ( force_style != scalar_fold && ( scan & SCAN_INDENTED ) ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( force_style != scalar_fold && ( scan & SCAN_INDENTED ) ) { 2893 211 3104
} else if ( force_style != scalar_fold && ( scan & SCAN_INDENTED ) ) { 1 2892 2893
753 1 force_style = scalar_literal;
754 3103 ▶ } else if ( force_style == scalar_plain && ( scan & SCAN_NEWLINE ) ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( force_style == scalar_plain && ( scan & SCAN_NEWLINE ) ) { 2573 530 3103
} else if ( force_style == scalar_plain && ( scan & SCAN_NEWLINE ) ) { 0 2573 2573
755 0 force_style = favor_style;
756 3103 ▶ } 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 ) ) { 2573 530 3103
} else if ( force_style == scalar_plain && parent->status == syck_lvl_iseq && ( scan & SCAN_FLOWSEQ ) ) { 63 2510 2573
} else if ( force_style == scalar_plain && parent->status == syck_lvl_iseq && ( scan & SCAN_FLOWSEQ ) ) { 0 63 63
757 0 force_style = scalar_2quote;
758 3103 ▶ } 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 ) ) { 2573 530 3103
} else if ( force_style == scalar_plain && parent->status == syck_lvl_imap && ( scan & SCAN_FLOWMAP ) ) { 22 2551 2573
} 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 3103 ▶ } 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 ) ) { 2573 530 3103
} else if ( force_style == scalar_plain && ( scan & SCAN_INDIC_S || scan & SCAN_INDIC_C || scan & SCAN_DOCSEP ) ) { 2372 201 2573
} else if ( force_style == scalar_plain && ( scan & SCAN_INDIC_S || scan & SCAN_INDIC_C || scan & SCAN_DOCSEP ) ) { 2368 4 2372
} else if ( force_style == scalar_plain && ( scan & SCAN_INDIC_S || scan & SCAN_INDIC_C || scan & SCAN_DOCSEP ) ) { 3 2365 2368
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 3362 ▶ if ( force_indent > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( force_indent > 0 ) { 0 3362 3362
771 0 lvl->spaces = parent->spaces + force_indent;
772 3362 ▶ } else if ( scan & SCAN_DOCSEP ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( scan & SCAN_DOCSEP ) { 3 3359 3362
773 3 lvl->spaces = parent->spaces + e->indent;
774 }
775  
776 /* For now, all ambiguous keys are going to be double-quoted */
777 3362 ▶ 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 ) { 1582 1780 3362
if ( ( parent->status == syck_lvl_map || parent->status == syck_lvl_mapx ) && parent->ncount % 2 == 1 ) { 0 1582 1582
if ( ( parent->status == syck_lvl_map || parent->status == syck_lvl_mapx ) && parent->ncount % 2 == 1 ) { 1086 694 1780
778 1086 ▶ if ( force_style != scalar_plain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( force_style != scalar_plain ) { 9 1077 1086
779 9 force_style = scalar_2quote;
780 }
781 }
782  
783 /* If the parent is an inline, double quote anything complex */
784 3362 ▶ 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 ) { 3245 117 3362
if ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) { 87 3158 3245
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 3362 ▶ if ( scan & SCAN_NONL_E ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( scan & SCAN_NONL_E ) { 3326 36 3362
792 3326 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 3362 ▶ if ( force_style == scalar_plain &&
Branch: 1 branch
BranchTrueFalseTotal
if ( force_style == scalar_plain && 2365 997 3362
802 2365 ▶ strncmp( implicit, "str", 4 ) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
strncmp( implicit, "str", 4 ) == 0 && 1310 1055 2365
803 1310 ▶ str[0] == ':' ) {
Branch: 1 branch
BranchTrueFalseTotal
str[0] == ':' ) { 127 1183 1310
804 127 force_style = scalar_literal;
805 }
806  
807 /* Write the text node */
808 3362 switch ( force_style )
809 {
810 120 case scalar_1quote:
811 120 syck_emit_1quoted( e, force_width, str, len );
812 120 break;
813  
814 601 case scalar_none:
815 case scalar_2quote:
816 601 syck_emit_2quoted( e, force_width, str, len );
817 601 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 2238 case scalar_plain:
833 2238 syck_emitter_write( e, str, len );
834 2238 break;
835 }
836  
837 3362 ▶ if ( parent->status == syck_lvl_mapx )
Branch: 1 branch
BranchTrueFalseTotal
if ( parent->status == syck_lvl_mapx ) 0 3362 3362
838 {
839 0 syck_emitter_write( e, "\n", 1 );
840 }
841 3362 }
842  
843 void
844 4071 syck_emitter_escape( SyckEmitter *e, unsigned char *src, long len )
845 {
846 int i;
847 8142 ▶ for( i = 0; i < len; i++ )
Branch: 1 branch
BranchTrueFalseTotal
for( i = 0; i < len; i++ ) 4071 4071 8142
848 {
849 /* XXX - scalar_fold overloaded to mean utf8 from Audrey Tang */
850 8142 ▶ if( e->json_mode
Branch: 2 branches
BranchTrueFalseTotal
if( e->json_mode 760 3311 4071
if( e->json_mode 618 3453 4071
851 760 ? (src[i] < 0x20)
852 3311 ▶ : (e->style == scalar_fold)
Branch: 1 branch
BranchTrueFalseTotal
: (e->style == scalar_fold) 431 2880 3311
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 2880 ▶ : ((src[i] < 0x20) || (0x7E < src[i])) )
Branch: 2 branches
BranchTrueFalseTotal
: ((src[i] < 0x20) || (0x7E < src[i])) ) 2880 0 2880
: ((src[i] < 0x20) || (0x7E < src[i])) ) 604 2276 2880
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 3453 syck_emitter_write( e, (const char *)src + i, 1 );
879 3453 ▶ if( '\\' == src[i] )
Branch: 1 branch
BranchTrueFalseTotal
if( '\\' == src[i] ) 0 3453 3453
880 0 syck_emitter_write( e, "\\", 1 );
881 }
882 }
883 4071 }
884  
885 /*
886 * Outputs a single-quoted block.
887 */
888 120 void syck_emit_1quoted( SyckEmitter *e, int width, char *str, long len )
889 {
890 120 char do_indent = 0;
891 120 char *mark = str;
892 120 char *start = str;
893 120 syck_emitter_write( e, "'", 1 );
894 693 ▶ while ( mark < str + len ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( mark < str + len ) { 573 120 693
895 573 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 566 default:
908 566 syck_emitter_write( e, mark, 1 );
909 566 break;
910 }
911 573 mark++;
912 }
913 120 syck_emitter_write( e, "'", 1 );
914 120 }
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 735 void syck_emit_2quoted( SyckEmitter *e, int width, char *str, long len )
1003 {
1004 735 char do_indent = 0;
1005 735 char *mark = str;
1006 735 char *start = str;
1007 735 char *end = str;
1008 735 syck_emitter_write( e, "\"", 1 );
1009 5014 ▶ while ( mark < str + len ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( mark < str + len ) { 4279 735 5014
1010 4279 ▶ if ( do_indent > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( do_indent > 0 ) { 0 4279 4279
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 4279 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 3944 default:
1074 3944 syck_emitter_escape( e, (unsigned char*)mark, 1 );
1075 3944 break;
1076 }
1077 4279 mark++;
1078 }
1079 735 syck_emitter_write( e, "\"", 1 );
1080 735 }
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 1919 void syck_emit_seq( SyckEmitter *e, char *tag, enum seq_style style )
1170 {
1171 1919 SyckLevel *parent = syck_emitter_parent_level( e );
1172 1919 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 1919 ▶ 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 1919 1919
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 1919 syck_emit_tag( e, tag, "tag:yaml.org,2002:seq" );
1185 1919 ▶ 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 ) ) { 1077 842 1919
if ( style == seq_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) { 1077 0 1077
if ( style == seq_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) { 0 1077 1077
1186 842 syck_emitter_write( e, "[", 1 );
1187 842 lvl->status = syck_lvl_iseq;
1188 } else {
1189 1077 lvl->status = syck_lvl_seq;
1190 }
1191 1919 }
1192  
1193 /*
1194 * Begins emission of a mapping.
1195 */
1196 873 void syck_emit_map( SyckEmitter *e, char *tag, enum map_style style )
1197 {
1198 873 SyckLevel *parent = syck_emitter_parent_level( e );
1199 873 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 873 ▶ 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 873 873
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 873 syck_emit_tag( e, tag, "tag:yaml.org,2002:map" );
1212 873 ▶ 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 ) ) { 811 62 873
if ( style == map_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) { 811 0 811
if ( style == map_inline || ( parent->status == syck_lvl_imap || parent->status == syck_lvl_iseq ) ) { 0 811 811
1213 62 syck_emitter_write( e, "{", 1 );
1214 62 lvl->status = syck_lvl_imap;
1215 } else {
1216 811 lvl->status = syck_lvl_map;
1217 }
1218 873 }
1219  
1220 /*
1221 * Handles emitting of a collection item (for both
1222 * sequences and maps)
1223 */
1224 6968 void syck_emit_item( SyckEmitter *e, st_data_t n )
1225 {
1226 6968 SyckLevel *lvl = syck_emitter_current_level( e );
1227 6968 switch ( lvl->status )
1228 {
1229 1704 case syck_lvl_seq:
1230 {
1231 1704 SyckLevel *parent = syck_emitter_parent_level( e );
1232  
1233 /* seq-in-map shortcut */
1234 1704 ▶ if ( parent->status == syck_lvl_mapx && lvl->ncount == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( parent->status == syck_lvl_mapx && lvl->ncount == 0 ) { 0 1704 1704
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 1704 ▶ 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 1704 1704
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 1704 syck_emit_indent( e );
1257 1704 syck_emitter_write( e, "- ", 2 );
1258 }
1259 1704 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 2172 case syck_lvl_map:
1270 {
1271 2172 SyckLevel *parent = syck_emitter_parent_level( e );
1272  
1273 /* map-in-seq shortcut */
1274 2172 ▶ 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 2172 2172
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 2172 ▶ if ( lvl->ncount % 2 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->ncount % 2 == 0 ) { 1086 1086 2172
1286 1086 syck_emit_indent( e );
1287 } else {
1288 1086 syck_emitter_write( e, ": ", 2 );
1289 }
1290 }
1291 2172 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 2068 default: break;
1326 }
1327 6968 lvl->ncount++;
1328  
1329 6968 syck_emit( e, n );
1330 6947 }
1331  
1332 /*
1333 * Closes emission of a collection.
1334 */
1335 2803 void syck_emit_end( SyckEmitter *e )
1336 {
1337 2803 SyckLevel *lvl = syck_emitter_current_level( e );
1338 2803 SyckLevel *parent = syck_emitter_parent_level( e );
1339 2803 switch ( lvl->status )
1340 {
1341 1077 case syck_lvl_seq:
1342 1077 ▶ if ( lvl->ncount == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->ncount == 0 ) { 11 1066 1077
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 1066 ▶ } else if ( parent->status == syck_lvl_mapx ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( parent->status == syck_lvl_mapx ) { 0 1066 1066
1348 0 syck_emitter_write( e, "\n", 1 );
1349 }
1350 1077 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 811 case syck_lvl_map:
1360 811 ▶ if ( lvl->ncount == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( lvl->ncount == 0 ) { 45 766 811
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 766 ▶ } else if ( lvl->ncount % 2 == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( lvl->ncount % 2 == 1 ) { 0 766 766
1366 0 syck_emitter_write( e, ":", 1 );
1367 766 ▶ } else if ( parent->status == syck_lvl_mapx ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( parent->status == syck_lvl_mapx ) { 0 766 766
1368 0 syck_emitter_write( e, "\n", 1 );
1369 }
1370 811 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 2803 }
1382  
1383 /*
1384 * Fill markers table with emitter nodes in the
1385 * soon-to-be-emitted tree.
1386 */
1387 SYMID
1388 33298 syck_emitter_mark_node( SyckEmitter *e, st_data_t n, int flags )
1389 {
1390 33298 SYMID oid = 0;
1391 33298 char *anchor_name = NULL;
1392  
1393 /*
1394 * Ensure markers table is initialized.
1395 */
1396 33298 ▶ if ( e->markers == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( e->markers == NULL ) 1514 31784 33298
1397 {
1398 1514 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 33298 ▶ if ( ! st_lookup( e->markers, n, (st_data_t *)&oid ) )
Branch: 1 branch
BranchTrueFalseTotal
if ( ! st_lookup( e->markers, n, (st_data_t *)&oid ) ) 10756 22542 33298
1407 {
1408 /*
1409 * Store all markers
1410 */
1411 10756 oid = e->markers->num_entries + 1;
1412 10756 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 33108 return oid;
1447 }
1448  
« node.c ↑ Summary syck_.c »