/src/libyaml/src/emitter.c
Line | Count | Source |
1 | | |
2 | | #include "yaml_private.h" |
3 | | |
4 | | /* |
5 | | * Flush the buffer if needed. |
6 | | */ |
7 | | |
8 | | #define FLUSH(emitter) \ |
9 | 964M | ((emitter->buffer.pointer+5 < emitter->buffer.end) \ |
10 | 964M | || yaml_emitter_flush(emitter)) |
11 | | |
12 | | /* |
13 | | * Put a character to the output buffer. |
14 | | */ |
15 | | |
16 | | #define PUT(emitter,value) \ |
17 | 167M | (FLUSH(emitter) \ |
18 | 167M | && (*(emitter->buffer.pointer++) = (yaml_char_t)(value), \ |
19 | 167M | emitter->column++, \ |
20 | 167M | 1)) |
21 | | |
22 | | /* |
23 | | * Put a line break to the output buffer. |
24 | | */ |
25 | | |
26 | | #define PUT_BREAK(emitter) \ |
27 | 14.0M | (FLUSH(emitter) \ |
28 | 14.0M | && ((emitter->line_break == YAML_CR_BREAK ? \ |
29 | 14.0M | (*(emitter->buffer.pointer++) = (yaml_char_t) '\r') : \ |
30 | 14.0M | emitter->line_break == YAML_LN_BREAK ? \ |
31 | 14.0M | (*(emitter->buffer.pointer++) = (yaml_char_t) '\n') : \ |
32 | 14.0M | emitter->line_break == YAML_CRLN_BREAK ? \ |
33 | 0 | (*(emitter->buffer.pointer++) = (yaml_char_t) '\r', \ |
34 | 0 | *(emitter->buffer.pointer++) = (yaml_char_t) '\n') : 0), \ |
35 | 14.0M | emitter->column = 0, \ |
36 | 14.0M | emitter->line ++, \ |
37 | 14.0M | 1)) |
38 | | |
39 | | /* |
40 | | * Copy a character from a string into buffer. |
41 | | */ |
42 | | |
43 | | #define WRITE(emitter,string) \ |
44 | 301M | (FLUSH(emitter) \ |
45 | 301M | && (COPY(emitter->buffer,string), \ |
46 | 301M | emitter->column ++, \ |
47 | 301M | 1)) |
48 | | |
49 | | /* |
50 | | * Copy a line break character from a string into buffer. |
51 | | */ |
52 | | |
53 | | #define WRITE_BREAK(emitter,string) \ |
54 | 0 | (FLUSH(emitter) \ |
55 | 0 | && (CHECK(string,'\n') ? \ |
56 | 0 | (PUT_BREAK(emitter), \ |
57 | 0 | string.pointer ++, \ |
58 | 0 | 1) : \ |
59 | 0 | (COPY(emitter->buffer,string), \ |
60 | 0 | emitter->column = 0, \ |
61 | 0 | emitter->line ++, \ |
62 | 0 | 1))) |
63 | | |
64 | | /* |
65 | | * API functions. |
66 | | */ |
67 | | |
68 | | YAML_DECLARE(int) |
69 | | yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event); |
70 | | |
71 | | /* |
72 | | * Utility functions. |
73 | | */ |
74 | | |
75 | | static int |
76 | | yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem); |
77 | | |
78 | | static int |
79 | | yaml_emitter_need_more_events(yaml_emitter_t *emitter); |
80 | | |
81 | | static int |
82 | | yaml_emitter_append_tag_directive(yaml_emitter_t *emitter, |
83 | | yaml_tag_directive_t value, int allow_duplicates); |
84 | | |
85 | | static int |
86 | | yaml_emitter_increase_indent(yaml_emitter_t *emitter, |
87 | | int flow, int indentless); |
88 | | |
89 | | /* |
90 | | * State functions. |
91 | | */ |
92 | | |
93 | | static int |
94 | | yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event); |
95 | | |
96 | | static int |
97 | | yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, |
98 | | yaml_event_t *event); |
99 | | |
100 | | static int |
101 | | yaml_emitter_emit_document_start(yaml_emitter_t *emitter, |
102 | | yaml_event_t *event, int first); |
103 | | |
104 | | static int |
105 | | yaml_emitter_emit_document_content(yaml_emitter_t *emitter, |
106 | | yaml_event_t *event); |
107 | | |
108 | | static int |
109 | | yaml_emitter_emit_document_end(yaml_emitter_t *emitter, |
110 | | yaml_event_t *event); |
111 | | |
112 | | static int |
113 | | yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, |
114 | | yaml_event_t *event, int first); |
115 | | |
116 | | static int |
117 | | yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, |
118 | | yaml_event_t *event, int first); |
119 | | |
120 | | static int |
121 | | yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter, |
122 | | yaml_event_t *event, int simple); |
123 | | |
124 | | static int |
125 | | yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, |
126 | | yaml_event_t *event, int first); |
127 | | |
128 | | static int |
129 | | yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter, |
130 | | yaml_event_t *event, int first); |
131 | | |
132 | | static int |
133 | | yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, |
134 | | yaml_event_t *event, int simple); |
135 | | |
136 | | static int |
137 | | yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, |
138 | | int root, int sequence, int mapping, int simple_key); |
139 | | |
140 | | static int |
141 | | yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event); |
142 | | |
143 | | static int |
144 | | yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event); |
145 | | |
146 | | static int |
147 | | yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event); |
148 | | |
149 | | static int |
150 | | yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event); |
151 | | |
152 | | /* |
153 | | * Checkers. |
154 | | */ |
155 | | |
156 | | static int |
157 | | yaml_emitter_check_empty_document(yaml_emitter_t *emitter); |
158 | | |
159 | | static int |
160 | | yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter); |
161 | | |
162 | | static int |
163 | | yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter); |
164 | | |
165 | | static int |
166 | | yaml_emitter_check_simple_key(yaml_emitter_t *emitter); |
167 | | |
168 | | static int |
169 | | yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event); |
170 | | |
171 | | /* |
172 | | * Processors. |
173 | | */ |
174 | | |
175 | | static int |
176 | | yaml_emitter_process_anchor(yaml_emitter_t *emitter); |
177 | | |
178 | | static int |
179 | | yaml_emitter_process_tag(yaml_emitter_t *emitter); |
180 | | |
181 | | static int |
182 | | yaml_emitter_process_scalar(yaml_emitter_t *emitter); |
183 | | |
184 | | /* |
185 | | * Analyzers. |
186 | | */ |
187 | | |
188 | | static int |
189 | | yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter, |
190 | | yaml_version_directive_t version_directive); |
191 | | |
192 | | static int |
193 | | yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter, |
194 | | yaml_tag_directive_t tag_directive); |
195 | | |
196 | | static int |
197 | | yaml_emitter_analyze_anchor(yaml_emitter_t *emitter, |
198 | | yaml_char_t *anchor, int alias); |
199 | | |
200 | | static int |
201 | | yaml_emitter_analyze_tag(yaml_emitter_t *emitter, |
202 | | yaml_char_t *tag); |
203 | | |
204 | | static int |
205 | | yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, |
206 | | yaml_char_t *value, size_t length); |
207 | | |
208 | | static int |
209 | | yaml_emitter_analyze_event(yaml_emitter_t *emitter, |
210 | | yaml_event_t *event); |
211 | | |
212 | | /* |
213 | | * Writers. |
214 | | */ |
215 | | |
216 | | static int |
217 | | yaml_emitter_write_bom(yaml_emitter_t *emitter); |
218 | | |
219 | | static int |
220 | | yaml_emitter_write_indent(yaml_emitter_t *emitter); |
221 | | |
222 | | static int |
223 | | yaml_emitter_write_indicator(yaml_emitter_t *emitter, |
224 | | const char *indicator, int need_whitespace, |
225 | | int is_whitespace, int is_indention); |
226 | | |
227 | | static int |
228 | | yaml_emitter_write_anchor(yaml_emitter_t *emitter, |
229 | | yaml_char_t *value, size_t length); |
230 | | |
231 | | static int |
232 | | yaml_emitter_write_tag_handle(yaml_emitter_t *emitter, |
233 | | yaml_char_t *value, size_t length); |
234 | | |
235 | | static int |
236 | | yaml_emitter_write_tag_content(yaml_emitter_t *emitter, |
237 | | yaml_char_t *value, size_t length, int need_whitespace); |
238 | | |
239 | | static int |
240 | | yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, |
241 | | yaml_char_t *value, size_t length, int allow_breaks); |
242 | | |
243 | | static int |
244 | | yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, |
245 | | yaml_char_t *value, size_t length, int allow_breaks); |
246 | | |
247 | | static int |
248 | | yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, |
249 | | yaml_char_t *value, size_t length, int allow_breaks); |
250 | | |
251 | | static int |
252 | | yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, |
253 | | yaml_string_t string); |
254 | | |
255 | | static int |
256 | | yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, |
257 | | yaml_char_t *value, size_t length); |
258 | | |
259 | | static int |
260 | | yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, |
261 | | yaml_char_t *value, size_t length); |
262 | | |
263 | | /* |
264 | | * Set an emitter error and return 0. |
265 | | */ |
266 | | |
267 | | static int |
268 | | yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem) |
269 | 0 | { |
270 | 0 | emitter->error = YAML_EMITTER_ERROR; |
271 | 0 | emitter->problem = problem; |
272 | |
|
273 | 0 | return 0; |
274 | 0 | } |
275 | | |
276 | | /* |
277 | | * Emit an event. |
278 | | */ |
279 | | |
280 | | YAML_DECLARE(int) |
281 | | yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event) |
282 | 20.7M | { |
283 | 20.7M | if (!ENQUEUE(emitter, emitter->events, *event)) { |
284 | 0 | yaml_event_delete(event); |
285 | 0 | return 0; |
286 | 0 | } |
287 | | |
288 | 41.4M | while (!yaml_emitter_need_more_events(emitter)) { |
289 | 20.7M | if (!yaml_emitter_analyze_event(emitter, emitter->events.head)) |
290 | 0 | return 0; |
291 | 20.7M | if (!yaml_emitter_state_machine(emitter, emitter->events.head)) |
292 | 2.50k | return 0; |
293 | 20.7M | yaml_event_delete(&DEQUEUE(emitter, emitter->events)); |
294 | 20.7M | } |
295 | | |
296 | 20.7M | return 1; |
297 | 20.7M | } |
298 | | |
299 | | /* |
300 | | * Check if we need to accumulate more events before emitting. |
301 | | * |
302 | | * We accumulate extra |
303 | | * - 1 event for DOCUMENT-START |
304 | | * - 2 events for SEQUENCE-START |
305 | | * - 3 events for MAPPING-START |
306 | | */ |
307 | | |
308 | | static int |
309 | | yaml_emitter_need_more_events(yaml_emitter_t *emitter) |
310 | 41.4M | { |
311 | 41.4M | int level = 0; |
312 | 41.4M | int accumulate = 0; |
313 | 41.4M | yaml_event_t *event; |
314 | | |
315 | 41.4M | if (QUEUE_EMPTY(emitter, emitter->events)) |
316 | 11.7M | return 1; |
317 | | |
318 | 29.6M | switch (emitter->events.head->type) { |
319 | 17.8k | case YAML_DOCUMENT_START_EVENT: |
320 | 17.8k | accumulate = 1; |
321 | 17.8k | break; |
322 | 42.1k | case YAML_SEQUENCE_START_EVENT: |
323 | 42.1k | accumulate = 2; |
324 | 42.1k | break; |
325 | 11.8M | case YAML_MAPPING_START_EVENT: |
326 | 11.8M | accumulate = 3; |
327 | 11.8M | break; |
328 | 17.7M | default: |
329 | 17.7M | return 0; |
330 | 29.6M | } |
331 | | |
332 | 11.9M | if (emitter->events.tail - emitter->events.head > accumulate) |
333 | 2.98M | return 0; |
334 | | |
335 | 26.7M | for (event = emitter->events.head; event != emitter->events.tail; event ++) { |
336 | 17.8M | switch (event->type) { |
337 | 0 | case YAML_STREAM_START_EVENT: |
338 | 8.90k | case YAML_DOCUMENT_START_EVENT: |
339 | 37.0k | case YAML_SEQUENCE_START_EVENT: |
340 | 8.93M | case YAML_MAPPING_START_EVENT: |
341 | 8.93M | level += 1; |
342 | 8.93M | break; |
343 | 0 | case YAML_STREAM_END_EVENT: |
344 | 0 | case YAML_DOCUMENT_END_EVENT: |
345 | 0 | case YAML_SEQUENCE_END_EVENT: |
346 | 0 | case YAML_MAPPING_END_EVENT: |
347 | 0 | level -= 1; |
348 | 0 | break; |
349 | 8.89M | default: |
350 | 8.89M | break; |
351 | 17.8M | } |
352 | 17.8M | if (!level) |
353 | 0 | return 0; |
354 | 17.8M | } |
355 | | |
356 | 8.92M | return 1; |
357 | 8.92M | } |
358 | | |
359 | | /* |
360 | | * Append a directive to the directives stack. |
361 | | */ |
362 | | |
363 | | static int |
364 | | yaml_emitter_append_tag_directive(yaml_emitter_t *emitter, |
365 | | yaml_tag_directive_t value, int allow_duplicates) |
366 | 17.8k | { |
367 | 17.8k | yaml_tag_directive_t *tag_directive; |
368 | 17.8k | yaml_tag_directive_t copy = { NULL, NULL }; |
369 | | |
370 | 17.8k | for (tag_directive = emitter->tag_directives.start; |
371 | 26.7k | tag_directive != emitter->tag_directives.top; tag_directive ++) { |
372 | 8.90k | if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) { |
373 | 0 | if (allow_duplicates) |
374 | 0 | return 1; |
375 | 0 | return yaml_emitter_set_emitter_error(emitter, |
376 | 0 | "duplicate %TAG directive"); |
377 | 0 | } |
378 | 8.90k | } |
379 | | |
380 | 17.8k | copy.handle = yaml_strdup(value.handle); |
381 | 17.8k | copy.prefix = yaml_strdup(value.prefix); |
382 | 17.8k | if (!copy.handle || !copy.prefix) { |
383 | 0 | emitter->error = YAML_MEMORY_ERROR; |
384 | 0 | goto error; |
385 | 0 | } |
386 | | |
387 | 17.8k | if (!PUSH(emitter, emitter->tag_directives, copy)) |
388 | 0 | goto error; |
389 | | |
390 | 17.8k | return 1; |
391 | | |
392 | 0 | error: |
393 | 0 | yaml_free(copy.handle); |
394 | 0 | yaml_free(copy.prefix); |
395 | 0 | return 0; |
396 | 17.8k | } |
397 | | |
398 | | /* |
399 | | * Increase the indentation level. |
400 | | */ |
401 | | |
402 | | static int |
403 | | yaml_emitter_increase_indent(yaml_emitter_t *emitter, |
404 | | int flow, int indentless) |
405 | 17.7M | { |
406 | 17.7M | if (!PUSH(emitter, emitter->indents, emitter->indent)) |
407 | 0 | return 0; |
408 | | |
409 | 17.7M | if (emitter->indent < 0) { |
410 | 8.76k | emitter->indent = flow ? emitter->best_indent : 0; |
411 | 8.76k | } |
412 | 17.7M | else if (!indentless) { |
413 | 17.7M | emitter->indent += emitter->best_indent; |
414 | 17.7M | } |
415 | | |
416 | 17.7M | return 1; |
417 | 17.7M | } |
418 | | |
419 | | /* |
420 | | * State dispatcher. |
421 | | */ |
422 | | |
423 | | static int |
424 | | yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event) |
425 | 20.7M | { |
426 | 20.7M | switch (emitter->state) |
427 | 20.7M | { |
428 | 8.90k | case YAML_EMIT_STREAM_START_STATE: |
429 | 8.90k | return yaml_emitter_emit_stream_start(emitter, event); |
430 | | |
431 | 8.90k | case YAML_EMIT_FIRST_DOCUMENT_START_STATE: |
432 | 8.90k | return yaml_emitter_emit_document_start(emitter, event, 1); |
433 | | |
434 | 1.19k | case YAML_EMIT_DOCUMENT_START_STATE: |
435 | 1.19k | return yaml_emitter_emit_document_start(emitter, event, 0); |
436 | | |
437 | 8.76k | case YAML_EMIT_DOCUMENT_CONTENT_STATE: |
438 | 8.76k | return yaml_emitter_emit_document_content(emitter, event); |
439 | | |
440 | 3.69k | case YAML_EMIT_DOCUMENT_END_STATE: |
441 | 3.69k | return yaml_emitter_emit_document_end(emitter, event); |
442 | | |
443 | 6.35k | case YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE: |
444 | 6.35k | return yaml_emitter_emit_flow_sequence_item(emitter, event, 1); |
445 | | |
446 | 1.05M | case YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE: |
447 | 1.05M | return yaml_emitter_emit_flow_sequence_item(emitter, event, 0); |
448 | | |
449 | 2.05M | case YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE: |
450 | 2.05M | return yaml_emitter_emit_flow_mapping_key(emitter, event, 1); |
451 | | |
452 | 5.17M | case YAML_EMIT_FLOW_MAPPING_KEY_STATE: |
453 | 5.17M | return yaml_emitter_emit_flow_mapping_key(emitter, event, 0); |
454 | | |
455 | 901k | case YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE: |
456 | 901k | return yaml_emitter_emit_flow_mapping_value(emitter, event, 1); |
457 | | |
458 | 4.27M | case YAML_EMIT_FLOW_MAPPING_VALUE_STATE: |
459 | 4.27M | return yaml_emitter_emit_flow_mapping_value(emitter, event, 0); |
460 | | |
461 | 7.66k | case YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE: |
462 | 7.66k | return yaml_emitter_emit_block_sequence_item(emitter, event, 1); |
463 | | |
464 | 914k | case YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE: |
465 | 914k | return yaml_emitter_emit_block_sequence_item(emitter, event, 0); |
466 | | |
467 | 913k | case YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE: |
468 | 913k | return yaml_emitter_emit_block_mapping_key(emitter, event, 1); |
469 | | |
470 | 2.69M | case YAML_EMIT_BLOCK_MAPPING_KEY_STATE: |
471 | 2.69M | return yaml_emitter_emit_block_mapping_key(emitter, event, 0); |
472 | | |
473 | 2.69M | case YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE: |
474 | 2.69M | return yaml_emitter_emit_block_mapping_value(emitter, event, 1); |
475 | | |
476 | 0 | case YAML_EMIT_BLOCK_MAPPING_VALUE_STATE: |
477 | 0 | return yaml_emitter_emit_block_mapping_value(emitter, event, 0); |
478 | | |
479 | 0 | case YAML_EMIT_END_STATE: |
480 | 0 | return yaml_emitter_set_emitter_error(emitter, |
481 | 0 | "expected nothing after STREAM-END"); |
482 | | |
483 | 0 | default: |
484 | 0 | assert(1); /* Invalid state. */ |
485 | 20.7M | } |
486 | | |
487 | 0 | return 0; |
488 | 20.7M | } |
489 | | |
490 | | /* |
491 | | * Expect STREAM-START. |
492 | | */ |
493 | | |
494 | | static int |
495 | | yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, |
496 | | yaml_event_t *event) |
497 | 8.90k | { |
498 | 8.90k | emitter->open_ended = 0; |
499 | 8.90k | if (event->type == YAML_STREAM_START_EVENT) |
500 | 8.90k | { |
501 | 8.90k | if (!emitter->encoding) { |
502 | 8.90k | emitter->encoding = event->data.stream_start.encoding; |
503 | 8.90k | } |
504 | | |
505 | 8.90k | if (!emitter->encoding) { |
506 | 0 | emitter->encoding = YAML_UTF8_ENCODING; |
507 | 0 | } |
508 | | |
509 | 8.90k | if (emitter->best_indent < 2 || emitter->best_indent > 9) { |
510 | 8.90k | emitter->best_indent = 2; |
511 | 8.90k | } |
512 | | |
513 | 8.90k | if (emitter->best_width >= 0 |
514 | 8.90k | && emitter->best_width <= emitter->best_indent*2) { |
515 | 8.90k | emitter->best_width = 80; |
516 | 8.90k | } |
517 | | |
518 | 8.90k | if (emitter->best_width < 0) { |
519 | 0 | emitter->best_width = INT_MAX; |
520 | 0 | } |
521 | | |
522 | 8.90k | if (!emitter->line_break) { |
523 | 8.90k | emitter->line_break = YAML_LN_BREAK; |
524 | 8.90k | } |
525 | | |
526 | 8.90k | emitter->indent = -1; |
527 | | |
528 | 8.90k | emitter->line = 0; |
529 | 8.90k | emitter->column = 0; |
530 | 8.90k | emitter->whitespace = 1; |
531 | 8.90k | emitter->indention = 1; |
532 | | |
533 | 8.90k | if (emitter->encoding != YAML_UTF8_ENCODING) { |
534 | 0 | if (!yaml_emitter_write_bom(emitter)) |
535 | 0 | return 0; |
536 | 0 | } |
537 | | |
538 | 8.90k | emitter->state = YAML_EMIT_FIRST_DOCUMENT_START_STATE; |
539 | | |
540 | 8.90k | return 1; |
541 | 8.90k | } |
542 | | |
543 | 0 | return yaml_emitter_set_emitter_error(emitter, |
544 | 0 | "expected STREAM-START"); |
545 | 8.90k | } |
546 | | |
547 | | /* |
548 | | * Expect DOCUMENT-START or STREAM-END. |
549 | | */ |
550 | | |
551 | | static int |
552 | | yaml_emitter_emit_document_start(yaml_emitter_t *emitter, |
553 | | yaml_event_t *event, int first) |
554 | 10.1k | { |
555 | 10.1k | if (event->type == YAML_DOCUMENT_START_EVENT) |
556 | 8.90k | { |
557 | 8.90k | yaml_tag_directive_t default_tag_directives[] = { |
558 | 8.90k | {(yaml_char_t *)"!", (yaml_char_t *)"!"}, |
559 | 8.90k | {(yaml_char_t *)"!!", (yaml_char_t *)"tag:yaml.org,2002:"}, |
560 | 8.90k | {NULL, NULL} |
561 | 8.90k | }; |
562 | 8.90k | yaml_tag_directive_t *tag_directive; |
563 | 8.90k | int implicit; |
564 | | |
565 | 8.90k | if (event->data.document_start.version_directive) { |
566 | 0 | if (!yaml_emitter_analyze_version_directive(emitter, |
567 | 0 | *event->data.document_start.version_directive)) |
568 | 0 | return 0; |
569 | 0 | } |
570 | | |
571 | 8.90k | for (tag_directive = event->data.document_start.tag_directives.start; |
572 | 8.90k | tag_directive != event->data.document_start.tag_directives.end; |
573 | 8.90k | tag_directive ++) { |
574 | 0 | if (!yaml_emitter_analyze_tag_directive(emitter, *tag_directive)) |
575 | 0 | return 0; |
576 | 0 | if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0)) |
577 | 0 | return 0; |
578 | 0 | } |
579 | | |
580 | 8.90k | for (tag_directive = default_tag_directives; |
581 | 26.7k | tag_directive->handle; tag_directive ++) { |
582 | 17.8k | if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 1)) |
583 | 0 | return 0; |
584 | 17.8k | } |
585 | | |
586 | 8.90k | implicit = event->data.document_start.implicit; |
587 | 8.90k | if (!first || emitter->canonical) { |
588 | 4.19k | implicit = 0; |
589 | 4.19k | } |
590 | | |
591 | 8.90k | if ((event->data.document_start.version_directive || |
592 | 8.90k | (event->data.document_start.tag_directives.start |
593 | 8.90k | != event->data.document_start.tag_directives.end)) && |
594 | 0 | emitter->open_ended) |
595 | 0 | { |
596 | 0 | if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) |
597 | 0 | return 0; |
598 | 0 | if (!yaml_emitter_write_indent(emitter)) |
599 | 0 | return 0; |
600 | 0 | } |
601 | 8.90k | emitter->open_ended = 0; |
602 | | |
603 | 8.90k | if (event->data.document_start.version_directive) { |
604 | 0 | implicit = 0; |
605 | 0 | if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0)) |
606 | 0 | return 0; |
607 | 0 | if (event->data.document_start.version_directive->minor == 1) { |
608 | 0 | if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0)) |
609 | 0 | return 0; |
610 | 0 | } |
611 | 0 | else { |
612 | 0 | if (!yaml_emitter_write_indicator(emitter, "1.2", 1, 0, 0)) |
613 | 0 | return 0; |
614 | 0 | } |
615 | 0 | if (!yaml_emitter_write_indent(emitter)) |
616 | 0 | return 0; |
617 | 0 | } |
618 | | |
619 | 8.90k | if (event->data.document_start.tag_directives.start |
620 | 8.90k | != event->data.document_start.tag_directives.end) { |
621 | 0 | implicit = 0; |
622 | 0 | for (tag_directive = event->data.document_start.tag_directives.start; |
623 | 0 | tag_directive != event->data.document_start.tag_directives.end; |
624 | 0 | tag_directive ++) { |
625 | 0 | if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0)) |
626 | 0 | return 0; |
627 | 0 | if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle, |
628 | 0 | strlen((char *)tag_directive->handle))) |
629 | 0 | return 0; |
630 | 0 | if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix, |
631 | 0 | strlen((char *)tag_directive->prefix), 1)) |
632 | 0 | return 0; |
633 | 0 | if (!yaml_emitter_write_indent(emitter)) |
634 | 0 | return 0; |
635 | 0 | } |
636 | 0 | } |
637 | | |
638 | 8.90k | if (yaml_emitter_check_empty_document(emitter)) { |
639 | 0 | implicit = 0; |
640 | 0 | } |
641 | | |
642 | 8.90k | if (!implicit) { |
643 | 8.90k | if (!yaml_emitter_write_indent(emitter)) |
644 | 0 | return 0; |
645 | 8.90k | if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0)) |
646 | 0 | return 0; |
647 | 8.90k | if (emitter->canonical) { |
648 | 4.19k | if (!yaml_emitter_write_indent(emitter)) |
649 | 0 | return 0; |
650 | 4.19k | } |
651 | 8.90k | } |
652 | | |
653 | 8.90k | emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE; |
654 | | |
655 | 8.90k | emitter->open_ended = 0; |
656 | 8.90k | return 1; |
657 | 8.90k | } |
658 | | |
659 | 1.19k | else if (event->type == YAML_STREAM_END_EVENT) |
660 | 1.19k | { |
661 | | |
662 | | /** |
663 | | * This can happen if a block scalar with trailing empty lines |
664 | | * is at the end of the stream |
665 | | */ |
666 | 1.19k | if (emitter->open_ended == 2) |
667 | 0 | { |
668 | 0 | if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) |
669 | 0 | return 0; |
670 | 0 | emitter->open_ended = 0; |
671 | 0 | if (!yaml_emitter_write_indent(emitter)) |
672 | 0 | return 0; |
673 | 0 | } |
674 | 1.19k | if (!yaml_emitter_flush(emitter)) |
675 | 0 | return 0; |
676 | | |
677 | 1.19k | emitter->state = YAML_EMIT_END_STATE; |
678 | | |
679 | 1.19k | return 1; |
680 | 1.19k | } |
681 | | |
682 | 0 | return yaml_emitter_set_emitter_error(emitter, |
683 | 0 | "expected DOCUMENT-START or STREAM-END"); |
684 | 10.1k | } |
685 | | |
686 | | /* |
687 | | * Expect the root node. |
688 | | */ |
689 | | |
690 | | static int |
691 | | yaml_emitter_emit_document_content(yaml_emitter_t *emitter, |
692 | | yaml_event_t *event) |
693 | 8.76k | { |
694 | 8.76k | if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE)) |
695 | 0 | return 0; |
696 | | |
697 | 8.76k | return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0); |
698 | 8.76k | } |
699 | | |
700 | | /* |
701 | | * Expect DOCUMENT-END. |
702 | | */ |
703 | | |
704 | | static int |
705 | | yaml_emitter_emit_document_end(yaml_emitter_t *emitter, |
706 | | yaml_event_t *event) |
707 | 3.69k | { |
708 | 3.69k | if (event->type == YAML_DOCUMENT_END_EVENT) |
709 | 3.69k | { |
710 | 3.69k | if (!yaml_emitter_write_indent(emitter)) |
711 | 0 | return 0; |
712 | 3.69k | if (!event->data.document_end.implicit) { |
713 | 3.69k | if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) |
714 | 0 | return 0; |
715 | 3.69k | emitter->open_ended = 0; |
716 | 3.69k | if (!yaml_emitter_write_indent(emitter)) |
717 | 0 | return 0; |
718 | 3.69k | } |
719 | 0 | else if (!emitter->open_ended) |
720 | 0 | emitter->open_ended = 1; |
721 | 3.69k | if (!yaml_emitter_flush(emitter)) |
722 | 2.50k | return 0; |
723 | | |
724 | 1.19k | emitter->state = YAML_EMIT_DOCUMENT_START_STATE; |
725 | | |
726 | 3.58k | while (!STACK_EMPTY(emitter, emitter->tag_directives)) { |
727 | 2.39k | yaml_tag_directive_t tag_directive = POP(emitter, |
728 | 2.39k | emitter->tag_directives); |
729 | 2.39k | yaml_free(tag_directive.handle); |
730 | 2.39k | yaml_free(tag_directive.prefix); |
731 | 2.39k | } |
732 | | |
733 | 1.19k | return 1; |
734 | 3.69k | } |
735 | | |
736 | 0 | return yaml_emitter_set_emitter_error(emitter, |
737 | 0 | "expected DOCUMENT-END"); |
738 | 3.69k | } |
739 | | |
740 | | /* |
741 | | * |
742 | | * Expect a flow item node. |
743 | | */ |
744 | | |
745 | | static int |
746 | | yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter, |
747 | | yaml_event_t *event, int first) |
748 | 1.05M | { |
749 | 1.05M | if (first) |
750 | 6.35k | { |
751 | 6.35k | if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0)) |
752 | 0 | return 0; |
753 | 6.35k | if (!yaml_emitter_increase_indent(emitter, 1, 0)) |
754 | 0 | return 0; |
755 | 6.35k | emitter->flow_level ++; |
756 | 6.35k | } |
757 | | |
758 | 1.05M | if (event->type == YAML_SEQUENCE_END_EVENT) |
759 | 4.00k | { |
760 | 4.00k | emitter->flow_level --; |
761 | 4.00k | emitter->indent = POP(emitter, emitter->indents); |
762 | 4.00k | if (emitter->canonical && !first) { |
763 | 4.00k | if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) |
764 | 0 | return 0; |
765 | 4.00k | if (!yaml_emitter_write_indent(emitter)) |
766 | 0 | return 0; |
767 | 4.00k | } |
768 | 4.00k | if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0)) |
769 | 0 | return 0; |
770 | 4.00k | emitter->state = POP(emitter, emitter->states); |
771 | | |
772 | 4.00k | return 1; |
773 | 4.00k | } |
774 | | |
775 | 1.05M | if (!first) { |
776 | 1.04M | if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) |
777 | 0 | return 0; |
778 | 1.04M | } |
779 | | |
780 | 1.05M | if (emitter->canonical || emitter->column > emitter->best_width) { |
781 | 1.05M | if (!yaml_emitter_write_indent(emitter)) |
782 | 0 | return 0; |
783 | 1.05M | } |
784 | 1.05M | if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE)) |
785 | 0 | return 0; |
786 | | |
787 | 1.05M | return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0); |
788 | 1.05M | } |
789 | | |
790 | | /* |
791 | | * Expect a flow key node. |
792 | | */ |
793 | | |
794 | | static int |
795 | | yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter, |
796 | | yaml_event_t *event, int first) |
797 | 7.22M | { |
798 | 7.22M | if (first) |
799 | 2.05M | { |
800 | 2.05M | if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0)) |
801 | 0 | return 0; |
802 | 2.05M | if (!yaml_emitter_increase_indent(emitter, 1, 0)) |
803 | 0 | return 0; |
804 | 2.05M | emitter->flow_level ++; |
805 | 2.05M | } |
806 | | |
807 | 7.22M | if (event->type == YAML_MAPPING_END_EVENT) |
808 | 2.05M | { |
809 | 2.05M | emitter->flow_level --; |
810 | 2.05M | emitter->indent = POP(emitter, emitter->indents); |
811 | 2.05M | if (emitter->canonical && !first) { |
812 | 1.60M | if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) |
813 | 0 | return 0; |
814 | 1.60M | if (!yaml_emitter_write_indent(emitter)) |
815 | 0 | return 0; |
816 | 1.60M | } |
817 | 2.05M | if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0)) |
818 | 0 | return 0; |
819 | 2.05M | emitter->state = POP(emitter, emitter->states); |
820 | | |
821 | 2.05M | return 1; |
822 | 2.05M | } |
823 | | |
824 | 5.17M | if (!first) { |
825 | 3.12M | if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0)) |
826 | 0 | return 0; |
827 | 3.12M | } |
828 | 5.17M | if (emitter->canonical || emitter->column > emitter->best_width) { |
829 | 4.27M | if (!yaml_emitter_write_indent(emitter)) |
830 | 0 | return 0; |
831 | 4.27M | } |
832 | | |
833 | 5.17M | if (!emitter->canonical && yaml_emitter_check_simple_key(emitter)) |
834 | 901k | { |
835 | 901k | if (!PUSH(emitter, emitter->states, |
836 | 901k | YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE)) |
837 | 0 | return 0; |
838 | | |
839 | 901k | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1); |
840 | 901k | } |
841 | 4.27M | else |
842 | 4.27M | { |
843 | 4.27M | if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0)) |
844 | 0 | return 0; |
845 | 4.27M | if (!PUSH(emitter, emitter->states, |
846 | 4.27M | YAML_EMIT_FLOW_MAPPING_VALUE_STATE)) |
847 | 0 | return 0; |
848 | | |
849 | 4.27M | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); |
850 | 4.27M | } |
851 | 5.17M | } |
852 | | |
853 | | /* |
854 | | * Expect a flow value node. |
855 | | */ |
856 | | |
857 | | static int |
858 | | yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter, |
859 | | yaml_event_t *event, int simple) |
860 | 5.17M | { |
861 | 5.17M | if (simple) { |
862 | 901k | if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0)) |
863 | 0 | return 0; |
864 | 901k | } |
865 | 4.27M | else { |
866 | 4.27M | if (emitter->canonical || emitter->column > emitter->best_width) { |
867 | 4.27M | if (!yaml_emitter_write_indent(emitter)) |
868 | 0 | return 0; |
869 | 4.27M | } |
870 | 4.27M | if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0)) |
871 | 0 | return 0; |
872 | 4.27M | } |
873 | 5.17M | if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE)) |
874 | 0 | return 0; |
875 | 5.17M | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); |
876 | 5.17M | } |
877 | | |
878 | | /* |
879 | | * Expect a block item node. |
880 | | */ |
881 | | |
882 | | static int |
883 | | yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter, |
884 | | yaml_event_t *event, int first) |
885 | 922k | { |
886 | 922k | if (first) |
887 | 7.66k | { |
888 | 7.66k | if (!yaml_emitter_increase_indent(emitter, 0, |
889 | 7.66k | (emitter->mapping_context && !emitter->indention))) |
890 | 0 | return 0; |
891 | 7.66k | } |
892 | | |
893 | 922k | if (event->type == YAML_SEQUENCE_END_EVENT) |
894 | 4.94k | { |
895 | 4.94k | emitter->indent = POP(emitter, emitter->indents); |
896 | 4.94k | emitter->state = POP(emitter, emitter->states); |
897 | | |
898 | 4.94k | return 1; |
899 | 4.94k | } |
900 | | |
901 | 917k | if (!yaml_emitter_write_indent(emitter)) |
902 | 0 | return 0; |
903 | 917k | if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1)) |
904 | 0 | return 0; |
905 | 917k | if (!PUSH(emitter, emitter->states, |
906 | 917k | YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE)) |
907 | 0 | return 0; |
908 | | |
909 | 917k | return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0); |
910 | 917k | } |
911 | | |
912 | | /* |
913 | | * Expect a block key node. |
914 | | */ |
915 | | |
916 | | static int |
917 | | yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter, |
918 | | yaml_event_t *event, int first) |
919 | 3.61M | { |
920 | 3.61M | if (first) |
921 | 913k | { |
922 | 913k | if (!yaml_emitter_increase_indent(emitter, 0, 0)) |
923 | 0 | return 0; |
924 | 913k | } |
925 | | |
926 | 3.61M | if (event->type == YAML_MAPPING_END_EVENT) |
927 | 913k | { |
928 | 913k | emitter->indent = POP(emitter, emitter->indents); |
929 | 913k | emitter->state = POP(emitter, emitter->states); |
930 | | |
931 | 913k | return 1; |
932 | 913k | } |
933 | | |
934 | 2.69M | if (!yaml_emitter_write_indent(emitter)) |
935 | 0 | return 0; |
936 | | |
937 | 2.69M | if (yaml_emitter_check_simple_key(emitter)) |
938 | 2.69M | { |
939 | 2.69M | if (!PUSH(emitter, emitter->states, |
940 | 2.69M | YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE)) |
941 | 0 | return 0; |
942 | | |
943 | 2.69M | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1); |
944 | 2.69M | } |
945 | 0 | else |
946 | 0 | { |
947 | 0 | if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1)) |
948 | 0 | return 0; |
949 | 0 | if (!PUSH(emitter, emitter->states, |
950 | 0 | YAML_EMIT_BLOCK_MAPPING_VALUE_STATE)) |
951 | 0 | return 0; |
952 | | |
953 | 0 | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); |
954 | 0 | } |
955 | 2.69M | } |
956 | | |
957 | | /* |
958 | | * Expect a block value node. |
959 | | */ |
960 | | |
961 | | static int |
962 | | yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter, |
963 | | yaml_event_t *event, int simple) |
964 | 2.69M | { |
965 | 2.69M | if (simple) { |
966 | 2.69M | if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0)) |
967 | 0 | return 0; |
968 | 2.69M | } |
969 | 0 | else { |
970 | 0 | if (!yaml_emitter_write_indent(emitter)) |
971 | 0 | return 0; |
972 | 0 | if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1)) |
973 | 0 | return 0; |
974 | 0 | } |
975 | 2.69M | if (!PUSH(emitter, emitter->states, |
976 | 2.69M | YAML_EMIT_BLOCK_MAPPING_KEY_STATE)) |
977 | 0 | return 0; |
978 | | |
979 | 2.69M | return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0); |
980 | 2.69M | } |
981 | | |
982 | | /* |
983 | | * Expect a node. |
984 | | */ |
985 | | |
986 | | static int |
987 | | yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event, |
988 | | int root, int sequence, int mapping, int simple_key) |
989 | 17.7M | { |
990 | 17.7M | emitter->root_context = root; |
991 | 17.7M | emitter->sequence_context = sequence; |
992 | 17.7M | emitter->mapping_context = mapping; |
993 | 17.7M | emitter->simple_key_context = simple_key; |
994 | | |
995 | 17.7M | switch (event->type) |
996 | 17.7M | { |
997 | 0 | case YAML_ALIAS_EVENT: |
998 | 0 | return yaml_emitter_emit_alias(emitter, event); |
999 | | |
1000 | 14.7M | case YAML_SCALAR_EVENT: |
1001 | 14.7M | return yaml_emitter_emit_scalar(emitter, event); |
1002 | | |
1003 | 14.0k | case YAML_SEQUENCE_START_EVENT: |
1004 | 14.0k | return yaml_emitter_emit_sequence_start(emitter, event); |
1005 | | |
1006 | 2.96M | case YAML_MAPPING_START_EVENT: |
1007 | 2.96M | return yaml_emitter_emit_mapping_start(emitter, event); |
1008 | | |
1009 | 0 | default: |
1010 | 0 | return yaml_emitter_set_emitter_error(emitter, |
1011 | 0 | "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS"); |
1012 | 17.7M | } |
1013 | | |
1014 | 0 | return 0; |
1015 | 17.7M | } |
1016 | | |
1017 | | /* |
1018 | | * Expect ALIAS. |
1019 | | */ |
1020 | | |
1021 | | static int |
1022 | | yaml_emitter_emit_alias(yaml_emitter_t *emitter, SHIM(yaml_event_t *event)) |
1023 | 0 | { |
1024 | 0 | if (!yaml_emitter_process_anchor(emitter)) |
1025 | 0 | return 0; |
1026 | 0 | if (emitter->simple_key_context) |
1027 | 0 | if (!PUT(emitter, ' ')) return 0; |
1028 | 0 | emitter->state = POP(emitter, emitter->states); |
1029 | |
|
1030 | 0 | return 1; |
1031 | 0 | } |
1032 | | |
1033 | | /* |
1034 | | * Expect SCALAR. |
1035 | | */ |
1036 | | |
1037 | | static int |
1038 | | yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event) |
1039 | 14.7M | { |
1040 | 14.7M | if (!yaml_emitter_select_scalar_style(emitter, event)) |
1041 | 0 | return 0; |
1042 | 14.7M | if (!yaml_emitter_process_anchor(emitter)) |
1043 | 0 | return 0; |
1044 | 14.7M | if (!yaml_emitter_process_tag(emitter)) |
1045 | 0 | return 0; |
1046 | 14.7M | if (!yaml_emitter_increase_indent(emitter, 1, 0)) |
1047 | 0 | return 0; |
1048 | 14.7M | if (!yaml_emitter_process_scalar(emitter)) |
1049 | 0 | return 0; |
1050 | 14.7M | emitter->indent = POP(emitter, emitter->indents); |
1051 | 14.7M | emitter->state = POP(emitter, emitter->states); |
1052 | | |
1053 | 14.7M | return 1; |
1054 | 14.7M | } |
1055 | | |
1056 | | /* |
1057 | | * Expect SEQUENCE-START. |
1058 | | */ |
1059 | | |
1060 | | static int |
1061 | | yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event) |
1062 | 14.0k | { |
1063 | 14.0k | if (!yaml_emitter_process_anchor(emitter)) |
1064 | 0 | return 0; |
1065 | 14.0k | if (!yaml_emitter_process_tag(emitter)) |
1066 | 0 | return 0; |
1067 | | |
1068 | 14.0k | if (emitter->flow_level || emitter->canonical |
1069 | 7.66k | || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE |
1070 | 7.66k | || yaml_emitter_check_empty_sequence(emitter)) { |
1071 | 6.35k | emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE; |
1072 | 6.35k | } |
1073 | 7.66k | else { |
1074 | 7.66k | emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE; |
1075 | 7.66k | } |
1076 | | |
1077 | 14.0k | return 1; |
1078 | 14.0k | } |
1079 | | |
1080 | | /* |
1081 | | * Expect MAPPING-START. |
1082 | | */ |
1083 | | |
1084 | | static int |
1085 | | yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event) |
1086 | 2.96M | { |
1087 | 2.96M | if (!yaml_emitter_process_anchor(emitter)) |
1088 | 0 | return 0; |
1089 | 2.96M | if (!yaml_emitter_process_tag(emitter)) |
1090 | 0 | return 0; |
1091 | | |
1092 | 2.96M | if (emitter->flow_level || emitter->canonical |
1093 | 1.36M | || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE |
1094 | 2.05M | || yaml_emitter_check_empty_mapping(emitter)) { |
1095 | 2.05M | emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE; |
1096 | 2.05M | } |
1097 | 913k | else { |
1098 | 913k | emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE; |
1099 | 913k | } |
1100 | | |
1101 | 2.96M | return 1; |
1102 | 2.96M | } |
1103 | | |
1104 | | /* |
1105 | | * Check if the document content is an empty scalar. |
1106 | | */ |
1107 | | |
1108 | | static int |
1109 | | yaml_emitter_check_empty_document(SHIM(yaml_emitter_t *emitter)) |
1110 | 8.90k | { |
1111 | 8.90k | return 0; |
1112 | 8.90k | } |
1113 | | |
1114 | | /* |
1115 | | * Check if the next events represent an empty sequence. |
1116 | | */ |
1117 | | |
1118 | | static int |
1119 | | yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter) |
1120 | 7.66k | { |
1121 | 7.66k | if (emitter->events.tail - emitter->events.head < 2) |
1122 | 0 | return 0; |
1123 | | |
1124 | 7.66k | return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT |
1125 | 7.66k | && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT); |
1126 | 7.66k | } |
1127 | | |
1128 | | /* |
1129 | | * Check if the next events represent an empty mapping. |
1130 | | */ |
1131 | | |
1132 | | static int |
1133 | | yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter) |
1134 | 913k | { |
1135 | 913k | if (emitter->events.tail - emitter->events.head < 2) |
1136 | 0 | return 0; |
1137 | | |
1138 | 913k | return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT |
1139 | 913k | && emitter->events.head[1].type == YAML_MAPPING_END_EVENT); |
1140 | 913k | } |
1141 | | |
1142 | | /* |
1143 | | * Check if the next node can be expressed as a simple key. |
1144 | | */ |
1145 | | |
1146 | | static int |
1147 | | yaml_emitter_check_simple_key(yaml_emitter_t *emitter) |
1148 | 3.59M | { |
1149 | 3.59M | yaml_event_t *event = emitter->events.head; |
1150 | 3.59M | size_t length = 0; |
1151 | | |
1152 | 3.59M | switch (event->type) |
1153 | 3.59M | { |
1154 | 0 | case YAML_ALIAS_EVENT: |
1155 | 0 | length += emitter->anchor_data.anchor_length; |
1156 | 0 | break; |
1157 | | |
1158 | 3.59M | case YAML_SCALAR_EVENT: |
1159 | 3.59M | if (emitter->scalar_data.multiline) |
1160 | 0 | return 0; |
1161 | 3.59M | length += emitter->anchor_data.anchor_length |
1162 | 3.59M | + emitter->tag_data.handle_length |
1163 | 3.59M | + emitter->tag_data.suffix_length |
1164 | 3.59M | + emitter->scalar_data.length; |
1165 | 3.59M | break; |
1166 | | |
1167 | 0 | case YAML_SEQUENCE_START_EVENT: |
1168 | 0 | if (!yaml_emitter_check_empty_sequence(emitter)) |
1169 | 0 | return 0; |
1170 | 0 | length += emitter->anchor_data.anchor_length |
1171 | 0 | + emitter->tag_data.handle_length |
1172 | 0 | + emitter->tag_data.suffix_length; |
1173 | 0 | break; |
1174 | | |
1175 | 0 | case YAML_MAPPING_START_EVENT: |
1176 | 0 | if (!yaml_emitter_check_empty_mapping(emitter)) |
1177 | 0 | return 0; |
1178 | 0 | length += emitter->anchor_data.anchor_length |
1179 | 0 | + emitter->tag_data.handle_length |
1180 | 0 | + emitter->tag_data.suffix_length; |
1181 | 0 | break; |
1182 | | |
1183 | 0 | default: |
1184 | 0 | return 0; |
1185 | 3.59M | } |
1186 | | |
1187 | 3.59M | if (length > 128) |
1188 | 0 | return 0; |
1189 | | |
1190 | 3.59M | return 1; |
1191 | 3.59M | } |
1192 | | |
1193 | | /* |
1194 | | * Determine an acceptable scalar style. |
1195 | | */ |
1196 | | |
1197 | | static int |
1198 | | yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event) |
1199 | 14.7M | { |
1200 | 14.7M | yaml_scalar_style_t style = event->data.scalar.style; |
1201 | 14.7M | int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix); |
1202 | | |
1203 | 14.7M | if (no_tag && !event->data.scalar.plain_implicit |
1204 | 412k | && !event->data.scalar.quoted_implicit) { |
1205 | 0 | return yaml_emitter_set_emitter_error(emitter, |
1206 | 0 | "neither tag nor implicit flags are specified"); |
1207 | 0 | } |
1208 | | |
1209 | 14.7M | if (style == YAML_ANY_SCALAR_STYLE) |
1210 | 0 | style = YAML_PLAIN_SCALAR_STYLE; |
1211 | | |
1212 | 14.7M | if (emitter->canonical) |
1213 | 7.99M | style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; |
1214 | | |
1215 | 14.7M | if (emitter->simple_key_context && emitter->scalar_data.multiline) |
1216 | 0 | style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; |
1217 | | |
1218 | 14.7M | if (style == YAML_PLAIN_SCALAR_STYLE) |
1219 | 6.33M | { |
1220 | 6.33M | if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed) |
1221 | 6.33M | || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed)) |
1222 | 0 | style = YAML_SINGLE_QUOTED_SCALAR_STYLE; |
1223 | 6.33M | if (!emitter->scalar_data.length |
1224 | 0 | && (emitter->flow_level || emitter->simple_key_context)) |
1225 | 0 | style = YAML_SINGLE_QUOTED_SCALAR_STYLE; |
1226 | 6.33M | if (no_tag && !event->data.scalar.plain_implicit) |
1227 | 0 | style = YAML_SINGLE_QUOTED_SCALAR_STYLE; |
1228 | 6.33M | } |
1229 | | |
1230 | 14.7M | if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE) |
1231 | 0 | { |
1232 | 0 | if (!emitter->scalar_data.single_quoted_allowed) |
1233 | 0 | style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; |
1234 | 0 | } |
1235 | | |
1236 | 14.7M | if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE) |
1237 | 0 | { |
1238 | 0 | if (!emitter->scalar_data.block_allowed |
1239 | 0 | || emitter->flow_level || emitter->simple_key_context) |
1240 | 0 | style = YAML_DOUBLE_QUOTED_SCALAR_STYLE; |
1241 | 0 | } |
1242 | | |
1243 | 14.7M | if (no_tag && !event->data.scalar.quoted_implicit |
1244 | 1.09M | && style != YAML_PLAIN_SCALAR_STYLE) |
1245 | 0 | { |
1246 | 0 | emitter->tag_data.handle = (yaml_char_t *)"!"; |
1247 | 0 | emitter->tag_data.handle_length = 1; |
1248 | 0 | } |
1249 | | |
1250 | 14.7M | emitter->scalar_data.style = style; |
1251 | | |
1252 | 14.7M | return 1; |
1253 | 14.7M | } |
1254 | | |
1255 | | /* |
1256 | | * Write an anchor. |
1257 | | */ |
1258 | | |
1259 | | static int |
1260 | | yaml_emitter_process_anchor(yaml_emitter_t *emitter) |
1261 | 17.7M | { |
1262 | 17.7M | if (!emitter->anchor_data.anchor) |
1263 | 17.7M | return 1; |
1264 | | |
1265 | 0 | if (!yaml_emitter_write_indicator(emitter, |
1266 | 0 | (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0)) |
1267 | 0 | return 0; |
1268 | | |
1269 | 0 | return yaml_emitter_write_anchor(emitter, |
1270 | 0 | emitter->anchor_data.anchor, emitter->anchor_data.anchor_length); |
1271 | 0 | } |
1272 | | |
1273 | | /* |
1274 | | * Write a tag. |
1275 | | */ |
1276 | | |
1277 | | static int |
1278 | | yaml_emitter_process_tag(yaml_emitter_t *emitter) |
1279 | 17.7M | { |
1280 | 17.7M | if (!emitter->tag_data.handle && !emitter->tag_data.suffix) |
1281 | 8.11M | return 1; |
1282 | | |
1283 | 9.60M | if (emitter->tag_data.handle) |
1284 | 9.60M | { |
1285 | 9.60M | if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle, |
1286 | 9.60M | emitter->tag_data.handle_length)) |
1287 | 0 | return 0; |
1288 | 9.60M | if (emitter->tag_data.suffix) { |
1289 | 9.60M | if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix, |
1290 | 9.60M | emitter->tag_data.suffix_length, 0)) |
1291 | 0 | return 0; |
1292 | 9.60M | } |
1293 | 9.60M | } |
1294 | 0 | else |
1295 | 0 | { |
1296 | 0 | if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0)) |
1297 | 0 | return 0; |
1298 | 0 | if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix, |
1299 | 0 | emitter->tag_data.suffix_length, 0)) |
1300 | 0 | return 0; |
1301 | 0 | if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0)) |
1302 | 0 | return 0; |
1303 | 0 | } |
1304 | | |
1305 | 9.60M | return 1; |
1306 | 9.60M | } |
1307 | | |
1308 | | /* |
1309 | | * Write a scalar. |
1310 | | */ |
1311 | | |
1312 | | static int |
1313 | | yaml_emitter_process_scalar(yaml_emitter_t *emitter) |
1314 | 14.7M | { |
1315 | 14.7M | switch (emitter->scalar_data.style) |
1316 | 14.7M | { |
1317 | 6.33M | case YAML_PLAIN_SCALAR_STYLE: |
1318 | 6.33M | return yaml_emitter_write_plain_scalar(emitter, |
1319 | 6.33M | emitter->scalar_data.value, emitter->scalar_data.length, |
1320 | 6.33M | !emitter->simple_key_context); |
1321 | | |
1322 | 0 | case YAML_SINGLE_QUOTED_SCALAR_STYLE: |
1323 | 0 | return yaml_emitter_write_single_quoted_scalar(emitter, |
1324 | 0 | emitter->scalar_data.value, emitter->scalar_data.length, |
1325 | 0 | !emitter->simple_key_context); |
1326 | | |
1327 | 8.40M | case YAML_DOUBLE_QUOTED_SCALAR_STYLE: |
1328 | 8.40M | return yaml_emitter_write_double_quoted_scalar(emitter, |
1329 | 8.40M | emitter->scalar_data.value, emitter->scalar_data.length, |
1330 | 8.40M | !emitter->simple_key_context); |
1331 | | |
1332 | 0 | case YAML_LITERAL_SCALAR_STYLE: |
1333 | 0 | return yaml_emitter_write_literal_scalar(emitter, |
1334 | 0 | emitter->scalar_data.value, emitter->scalar_data.length); |
1335 | | |
1336 | 0 | case YAML_FOLDED_SCALAR_STYLE: |
1337 | 0 | return yaml_emitter_write_folded_scalar(emitter, |
1338 | 0 | emitter->scalar_data.value, emitter->scalar_data.length); |
1339 | | |
1340 | 0 | default: |
1341 | 0 | assert(1); /* Impossible. */ |
1342 | 14.7M | } |
1343 | | |
1344 | 0 | return 0; |
1345 | 14.7M | } |
1346 | | |
1347 | | /* |
1348 | | * Check if a %YAML directive is valid. |
1349 | | */ |
1350 | | |
1351 | | static int |
1352 | | yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter, |
1353 | | yaml_version_directive_t version_directive) |
1354 | 0 | { |
1355 | 0 | if (version_directive.major != 1 || ( |
1356 | 0 | version_directive.minor != 1 |
1357 | 0 | && version_directive.minor != 2 |
1358 | 0 | )) { |
1359 | 0 | return yaml_emitter_set_emitter_error(emitter, |
1360 | 0 | "incompatible %YAML directive"); |
1361 | 0 | } |
1362 | | |
1363 | 0 | return 1; |
1364 | 0 | } |
1365 | | |
1366 | | /* |
1367 | | * Check if a %TAG directive is valid. |
1368 | | */ |
1369 | | |
1370 | | static int |
1371 | | yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter, |
1372 | | yaml_tag_directive_t tag_directive) |
1373 | 0 | { |
1374 | 0 | yaml_string_t handle; |
1375 | 0 | yaml_string_t prefix; |
1376 | 0 | size_t handle_length; |
1377 | 0 | size_t prefix_length; |
1378 | |
|
1379 | 0 | handle_length = strlen((char *)tag_directive.handle); |
1380 | 0 | prefix_length = strlen((char *)tag_directive.prefix); |
1381 | 0 | STRING_ASSIGN(handle, tag_directive.handle, handle_length); |
1382 | 0 | STRING_ASSIGN(prefix, tag_directive.prefix, prefix_length); |
1383 | |
|
1384 | 0 | if (handle.start == handle.end) { |
1385 | 0 | return yaml_emitter_set_emitter_error(emitter, |
1386 | 0 | "tag handle must not be empty"); |
1387 | 0 | } |
1388 | | |
1389 | 0 | if (handle.start[0] != '!') { |
1390 | 0 | return yaml_emitter_set_emitter_error(emitter, |
1391 | 0 | "tag handle must start with '!'"); |
1392 | 0 | } |
1393 | | |
1394 | 0 | if (handle.end[-1] != '!') { |
1395 | 0 | return yaml_emitter_set_emitter_error(emitter, |
1396 | 0 | "tag handle must end with '!'"); |
1397 | 0 | } |
1398 | | |
1399 | 0 | handle.pointer ++; |
1400 | |
|
1401 | 0 | while (handle.pointer < handle.end-1) { |
1402 | 0 | if (!IS_ALPHA(handle)) { |
1403 | 0 | return yaml_emitter_set_emitter_error(emitter, |
1404 | 0 | "tag handle must contain alphanumerical characters only"); |
1405 | 0 | } |
1406 | 0 | MOVE(handle); |
1407 | 0 | } |
1408 | | |
1409 | 0 | if (prefix.start == prefix.end) { |
1410 | 0 | return yaml_emitter_set_emitter_error(emitter, |
1411 | 0 | "tag prefix must not be empty"); |
1412 | 0 | } |
1413 | | |
1414 | 0 | return 1; |
1415 | 0 | } |
1416 | | |
1417 | | /* |
1418 | | * Check if an anchor is valid. |
1419 | | */ |
1420 | | |
1421 | | static int |
1422 | | yaml_emitter_analyze_anchor(yaml_emitter_t *emitter, |
1423 | | yaml_char_t *anchor, int alias) |
1424 | 0 | { |
1425 | 0 | size_t anchor_length; |
1426 | 0 | yaml_string_t string; |
1427 | |
|
1428 | 0 | anchor_length = strlen((char *)anchor); |
1429 | 0 | STRING_ASSIGN(string, anchor, anchor_length); |
1430 | |
|
1431 | 0 | if (string.start == string.end) { |
1432 | 0 | return yaml_emitter_set_emitter_error(emitter, alias ? |
1433 | 0 | "alias value must not be empty" : |
1434 | 0 | "anchor value must not be empty"); |
1435 | 0 | } |
1436 | | |
1437 | 0 | while (string.pointer != string.end) { |
1438 | 0 | if (!IS_ALPHA(string)) { |
1439 | 0 | return yaml_emitter_set_emitter_error(emitter, alias ? |
1440 | 0 | "alias value must contain alphanumerical characters only" : |
1441 | 0 | "anchor value must contain alphanumerical characters only"); |
1442 | 0 | } |
1443 | 0 | MOVE(string); |
1444 | 0 | } |
1445 | | |
1446 | 0 | emitter->anchor_data.anchor = string.start; |
1447 | 0 | emitter->anchor_data.anchor_length = string.end - string.start; |
1448 | 0 | emitter->anchor_data.alias = alias; |
1449 | |
|
1450 | 0 | return 1; |
1451 | 0 | } |
1452 | | |
1453 | | /* |
1454 | | * Check if a tag is valid. |
1455 | | */ |
1456 | | |
1457 | | static int |
1458 | | yaml_emitter_analyze_tag(yaml_emitter_t *emitter, |
1459 | | yaml_char_t *tag) |
1460 | 9.60M | { |
1461 | 9.60M | size_t tag_length; |
1462 | 9.60M | yaml_string_t string; |
1463 | 9.60M | yaml_tag_directive_t *tag_directive; |
1464 | | |
1465 | 9.60M | tag_length = strlen((char *)tag); |
1466 | 9.60M | STRING_ASSIGN(string, tag, tag_length); |
1467 | | |
1468 | 9.60M | if (string.start == string.end) { |
1469 | 0 | return yaml_emitter_set_emitter_error(emitter, |
1470 | 0 | "tag value must not be empty"); |
1471 | 0 | } |
1472 | | |
1473 | 9.60M | for (tag_directive = emitter->tag_directives.start; |
1474 | 19.2M | tag_directive != emitter->tag_directives.top; tag_directive ++) { |
1475 | 19.2M | size_t prefix_length = strlen((char *)tag_directive->prefix); |
1476 | 19.2M | if (prefix_length < (size_t)(string.end - string.start) |
1477 | 19.2M | && strncmp((char *)tag_directive->prefix, (char *)string.start, |
1478 | 19.2M | prefix_length) == 0) |
1479 | 9.60M | { |
1480 | 9.60M | emitter->tag_data.handle = tag_directive->handle; |
1481 | 9.60M | emitter->tag_data.handle_length = |
1482 | 9.60M | strlen((char *)tag_directive->handle); |
1483 | 9.60M | emitter->tag_data.suffix = string.start + prefix_length; |
1484 | 9.60M | emitter->tag_data.suffix_length = |
1485 | 9.60M | (string.end - string.start) - prefix_length; |
1486 | 9.60M | return 1; |
1487 | 9.60M | } |
1488 | 19.2M | } |
1489 | | |
1490 | 0 | emitter->tag_data.suffix = string.start; |
1491 | 0 | emitter->tag_data.suffix_length = string.end - string.start; |
1492 | |
|
1493 | 0 | return 1; |
1494 | 9.60M | } |
1495 | | |
1496 | | /* |
1497 | | * Check if a scalar is valid. |
1498 | | */ |
1499 | | |
1500 | | static int |
1501 | | yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, |
1502 | | yaml_char_t *value, size_t length) |
1503 | 14.7M | { |
1504 | 14.7M | yaml_string_t string; |
1505 | | |
1506 | 14.7M | int block_indicators = 0; |
1507 | 14.7M | int flow_indicators = 0; |
1508 | 14.7M | int line_breaks = 0; |
1509 | 14.7M | int special_characters = 0; |
1510 | | |
1511 | 14.7M | int leading_space = 0; |
1512 | 14.7M | int leading_break = 0; |
1513 | 14.7M | int trailing_space = 0; |
1514 | 14.7M | int trailing_break = 0; |
1515 | 14.7M | int break_space = 0; |
1516 | 14.7M | int space_break = 0; |
1517 | | |
1518 | 14.7M | int preceded_by_whitespace = 0; |
1519 | 14.7M | int followed_by_whitespace = 0; |
1520 | 14.7M | int previous_space = 0; |
1521 | 14.7M | int previous_break = 0; |
1522 | | |
1523 | 14.7M | STRING_ASSIGN(string, value, length); |
1524 | | |
1525 | 14.7M | emitter->scalar_data.value = value; |
1526 | 14.7M | emitter->scalar_data.length = length; |
1527 | | |
1528 | 14.7M | if (string.start == string.end) |
1529 | 658k | { |
1530 | 658k | emitter->scalar_data.multiline = 0; |
1531 | 658k | emitter->scalar_data.flow_plain_allowed = 0; |
1532 | 658k | emitter->scalar_data.block_plain_allowed = 1; |
1533 | 658k | emitter->scalar_data.single_quoted_allowed = 1; |
1534 | 658k | emitter->scalar_data.block_allowed = 0; |
1535 | | |
1536 | 658k | return 1; |
1537 | 658k | } |
1538 | | |
1539 | 14.0M | if ((CHECK_AT(string, '-', 0) |
1540 | 29.8k | && CHECK_AT(string, '-', 1) |
1541 | 10.8k | && CHECK_AT(string, '-', 2)) |
1542 | 14.0M | || (CHECK_AT(string, '.', 0) |
1543 | 8.20k | && CHECK_AT(string, '.', 1) |
1544 | 4.33k | && CHECK_AT(string, '.', 2))) { |
1545 | 4.33k | block_indicators = 1; |
1546 | 4.33k | flow_indicators = 1; |
1547 | 4.33k | } |
1548 | | |
1549 | 14.0M | preceded_by_whitespace = 1; |
1550 | 14.0M | followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string)); |
1551 | | |
1552 | 258M | while (string.pointer != string.end) |
1553 | 244M | { |
1554 | 244M | if (string.start == string.pointer) |
1555 | 14.0M | { |
1556 | 14.0M | if (CHECK(string, '#') || CHECK(string, ',') |
1557 | 14.0M | || CHECK(string, '[') || CHECK(string, ']') |
1558 | 14.0M | || CHECK(string, '{') || CHECK(string, '}') |
1559 | 14.0M | || CHECK(string, '&') || CHECK(string, '*') |
1560 | 14.0M | || CHECK(string, '!') || CHECK(string, '|') |
1561 | 14.0M | || CHECK(string, '>') || CHECK(string, '\'') |
1562 | 14.0M | || CHECK(string, '"') || CHECK(string, '%') |
1563 | 14.0M | || CHECK(string, '@') || CHECK(string, '`')) { |
1564 | 67.6k | flow_indicators = 1; |
1565 | 67.6k | block_indicators = 1; |
1566 | 67.6k | } |
1567 | | |
1568 | 14.0M | if (CHECK(string, '?') || CHECK(string, ':')) { |
1569 | 16.0k | flow_indicators = 1; |
1570 | 16.0k | if (followed_by_whitespace) { |
1571 | 10.0k | block_indicators = 1; |
1572 | 10.0k | } |
1573 | 16.0k | } |
1574 | | |
1575 | 14.0M | if (CHECK(string, '-') && followed_by_whitespace) { |
1576 | 8.56k | flow_indicators = 1; |
1577 | 8.56k | block_indicators = 1; |
1578 | 8.56k | } |
1579 | 14.0M | } |
1580 | 230M | else |
1581 | 230M | { |
1582 | 230M | if (CHECK(string, ',') || CHECK(string, '?') |
1583 | 219M | || CHECK(string, '[') || CHECK(string, ']') |
1584 | 219M | || CHECK(string, '{') || CHECK(string, '}')) { |
1585 | 11.1M | flow_indicators = 1; |
1586 | 11.1M | } |
1587 | | |
1588 | 230M | if (CHECK(string, ':')) { |
1589 | 50.3k | flow_indicators = 1; |
1590 | 50.3k | if (followed_by_whitespace) { |
1591 | 4.19k | block_indicators = 1; |
1592 | 4.19k | } |
1593 | 50.3k | } |
1594 | | |
1595 | 230M | if (CHECK(string, '#') && preceded_by_whitespace) { |
1596 | 2.28k | flow_indicators = 1; |
1597 | 2.28k | block_indicators = 1; |
1598 | 2.28k | } |
1599 | 230M | } |
1600 | | |
1601 | 244M | if (!IS_PRINTABLE(string) |
1602 | 231M | || (!IS_ASCII(string) && !emitter->unicode)) { |
1603 | 19.5M | special_characters = 1; |
1604 | 19.5M | } |
1605 | | |
1606 | 244M | if (IS_BREAK(string)) { |
1607 | 17.0M | line_breaks = 1; |
1608 | 17.0M | } |
1609 | | |
1610 | 244M | if (IS_SPACE(string)) |
1611 | 6.97M | { |
1612 | 6.97M | if (string.start == string.pointer) { |
1613 | 1.93k | leading_space = 1; |
1614 | 1.93k | } |
1615 | 6.97M | if (string.pointer+WIDTH(string) == string.end) { |
1616 | 2.55k | trailing_space = 1; |
1617 | 2.55k | } |
1618 | 6.97M | if (previous_break) { |
1619 | 114k | break_space = 1; |
1620 | 114k | } |
1621 | 6.97M | previous_space = 1; |
1622 | 6.97M | previous_break = 0; |
1623 | 6.97M | } |
1624 | 237M | else if (IS_BREAK(string)) |
1625 | 17.0M | { |
1626 | 17.0M | if (string.start == string.pointer) { |
1627 | 2.88k | leading_break = 1; |
1628 | 2.88k | } |
1629 | 17.0M | if (string.pointer+WIDTH(string) == string.end) { |
1630 | 2.85k | trailing_break = 1; |
1631 | 2.85k | } |
1632 | 17.0M | if (previous_space) { |
1633 | 26.3k | space_break = 1; |
1634 | 26.3k | } |
1635 | 17.0M | previous_space = 0; |
1636 | 17.0M | previous_break = 1; |
1637 | 17.0M | } |
1638 | 220M | else |
1639 | 220M | { |
1640 | 220M | previous_space = 0; |
1641 | 220M | previous_break = 0; |
1642 | 220M | } |
1643 | | |
1644 | 244M | preceded_by_whitespace = IS_BLANKZ(string); |
1645 | 244M | MOVE(string); |
1646 | 244M | if (string.pointer != string.end) { |
1647 | 230M | followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string)); |
1648 | 230M | } |
1649 | 244M | } |
1650 | | |
1651 | 14.0M | emitter->scalar_data.multiline = line_breaks; |
1652 | | |
1653 | 14.0M | emitter->scalar_data.flow_plain_allowed = 1; |
1654 | 14.0M | emitter->scalar_data.block_plain_allowed = 1; |
1655 | 14.0M | emitter->scalar_data.single_quoted_allowed = 1; |
1656 | 14.0M | emitter->scalar_data.block_allowed = 1; |
1657 | | |
1658 | 14.0M | if (leading_space || leading_break || trailing_space || trailing_break) { |
1659 | 6.42k | emitter->scalar_data.flow_plain_allowed = 0; |
1660 | 6.42k | emitter->scalar_data.block_plain_allowed = 0; |
1661 | 6.42k | } |
1662 | | |
1663 | 14.0M | if (trailing_space) { |
1664 | 2.55k | emitter->scalar_data.block_allowed = 0; |
1665 | 2.55k | } |
1666 | | |
1667 | 14.0M | if (break_space) { |
1668 | 1.07k | emitter->scalar_data.flow_plain_allowed = 0; |
1669 | 1.07k | emitter->scalar_data.block_plain_allowed = 0; |
1670 | 1.07k | emitter->scalar_data.single_quoted_allowed = 0; |
1671 | 1.07k | } |
1672 | | |
1673 | 14.0M | if (space_break || special_characters) { |
1674 | 41.7k | emitter->scalar_data.flow_plain_allowed = 0; |
1675 | 41.7k | emitter->scalar_data.block_plain_allowed = 0; |
1676 | 41.7k | emitter->scalar_data.single_quoted_allowed = 0; |
1677 | 41.7k | emitter->scalar_data.block_allowed = 0; |
1678 | 41.7k | } |
1679 | | |
1680 | 14.0M | if (line_breaks) { |
1681 | 46.8k | emitter->scalar_data.flow_plain_allowed = 0; |
1682 | 46.8k | emitter->scalar_data.block_plain_allowed = 0; |
1683 | 46.8k | } |
1684 | | |
1685 | 14.0M | if (flow_indicators) { |
1686 | 114k | emitter->scalar_data.flow_plain_allowed = 0; |
1687 | 114k | } |
1688 | | |
1689 | 14.0M | if (block_indicators) { |
1690 | 93.9k | emitter->scalar_data.block_plain_allowed = 0; |
1691 | 93.9k | } |
1692 | | |
1693 | 14.0M | return 1; |
1694 | 14.7M | } |
1695 | | |
1696 | | /* |
1697 | | * Check if the event data is valid. |
1698 | | */ |
1699 | | |
1700 | | static int |
1701 | | yaml_emitter_analyze_event(yaml_emitter_t *emitter, |
1702 | | yaml_event_t *event) |
1703 | 20.7M | { |
1704 | 20.7M | emitter->anchor_data.anchor = NULL; |
1705 | 20.7M | emitter->anchor_data.anchor_length = 0; |
1706 | 20.7M | emitter->tag_data.handle = NULL; |
1707 | 20.7M | emitter->tag_data.handle_length = 0; |
1708 | 20.7M | emitter->tag_data.suffix = NULL; |
1709 | 20.7M | emitter->tag_data.suffix_length = 0; |
1710 | 20.7M | emitter->scalar_data.value = NULL; |
1711 | 20.7M | emitter->scalar_data.length = 0; |
1712 | | |
1713 | 20.7M | switch (event->type) |
1714 | 20.7M | { |
1715 | 0 | case YAML_ALIAS_EVENT: |
1716 | 0 | if (!yaml_emitter_analyze_anchor(emitter, |
1717 | 0 | event->data.alias.anchor, 1)) |
1718 | 0 | return 0; |
1719 | 0 | return 1; |
1720 | | |
1721 | 14.7M | case YAML_SCALAR_EVENT: |
1722 | 14.7M | if (event->data.scalar.anchor) { |
1723 | 0 | if (!yaml_emitter_analyze_anchor(emitter, |
1724 | 0 | event->data.scalar.anchor, 0)) |
1725 | 0 | return 0; |
1726 | 0 | } |
1727 | 14.7M | if (event->data.scalar.tag && (emitter->canonical || |
1728 | 6.74M | (!event->data.scalar.plain_implicit |
1729 | 7.99M | && !event->data.scalar.quoted_implicit))) { |
1730 | 7.99M | if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag)) |
1731 | 0 | return 0; |
1732 | 7.99M | } |
1733 | 14.7M | if (!yaml_emitter_analyze_scalar(emitter, |
1734 | 14.7M | event->data.scalar.value, event->data.scalar.length)) |
1735 | 0 | return 0; |
1736 | 14.7M | return 1; |
1737 | | |
1738 | 14.0k | case YAML_SEQUENCE_START_EVENT: |
1739 | 14.0k | if (event->data.sequence_start.anchor) { |
1740 | 0 | if (!yaml_emitter_analyze_anchor(emitter, |
1741 | 0 | event->data.sequence_start.anchor, 0)) |
1742 | 0 | return 0; |
1743 | 0 | } |
1744 | 14.0k | if (event->data.sequence_start.tag && (emitter->canonical || |
1745 | 7.66k | !event->data.sequence_start.implicit)) { |
1746 | 6.35k | if (!yaml_emitter_analyze_tag(emitter, |
1747 | 6.35k | event->data.sequence_start.tag)) |
1748 | 0 | return 0; |
1749 | 6.35k | } |
1750 | 14.0k | return 1; |
1751 | | |
1752 | 2.96M | case YAML_MAPPING_START_EVENT: |
1753 | 2.96M | if (event->data.mapping_start.anchor) { |
1754 | 0 | if (!yaml_emitter_analyze_anchor(emitter, |
1755 | 0 | event->data.mapping_start.anchor, 0)) |
1756 | 0 | return 0; |
1757 | 0 | } |
1758 | 2.96M | if (event->data.mapping_start.tag && (emitter->canonical || |
1759 | 1.60M | !event->data.mapping_start.implicit)) { |
1760 | 1.60M | if (!yaml_emitter_analyze_tag(emitter, |
1761 | 1.60M | event->data.mapping_start.tag)) |
1762 | 0 | return 0; |
1763 | 1.60M | } |
1764 | 2.96M | return 1; |
1765 | | |
1766 | 2.99M | default: |
1767 | 2.99M | return 1; |
1768 | 20.7M | } |
1769 | 20.7M | } |
1770 | | |
1771 | | /* |
1772 | | * Write the BOM character. |
1773 | | */ |
1774 | | |
1775 | | static int |
1776 | | yaml_emitter_write_bom(yaml_emitter_t *emitter) |
1777 | 0 | { |
1778 | 0 | if (!FLUSH(emitter)) return 0; |
1779 | | |
1780 | 0 | *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF'; |
1781 | 0 | *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB'; |
1782 | 0 | *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF'; |
1783 | |
|
1784 | 0 | return 1; |
1785 | 0 | } |
1786 | | |
1787 | | static int |
1788 | | yaml_emitter_write_indent(yaml_emitter_t *emitter) |
1789 | 15.0M | { |
1790 | 15.0M | int indent = (emitter->indent >= 0) ? emitter->indent : 0; |
1791 | | |
1792 | 15.0M | if (!emitter->indention || emitter->column > indent |
1793 | 14.0M | || (emitter->column == indent && !emitter->whitespace)) { |
1794 | 14.0M | if (!PUT_BREAK(emitter)) return 0; |
1795 | 14.0M | } |
1796 | | |
1797 | 65.4M | while (emitter->column < indent) { |
1798 | 50.3M | if (!PUT(emitter, ' ')) return 0; |
1799 | 50.3M | } |
1800 | | |
1801 | 15.0M | emitter->whitespace = 1; |
1802 | 15.0M | emitter->indention = 1; |
1803 | | |
1804 | 15.0M | return 1; |
1805 | 15.0M | } |
1806 | | |
1807 | | static int |
1808 | | yaml_emitter_write_indicator(yaml_emitter_t *emitter, |
1809 | | const char *indicator, int need_whitespace, |
1810 | | int is_whitespace, int is_indention) |
1811 | 39.7M | { |
1812 | 39.7M | size_t indicator_length; |
1813 | 39.7M | yaml_string_t string; |
1814 | | |
1815 | 39.7M | indicator_length = strlen(indicator); |
1816 | 39.7M | STRING_ASSIGN(string, (yaml_char_t *)indicator, indicator_length); |
1817 | | |
1818 | 39.7M | if (need_whitespace && !emitter->whitespace) { |
1819 | 10.4M | if (!PUT(emitter, ' ')) return 0; |
1820 | 10.4M | } |
1821 | | |
1822 | 79.5M | while (string.pointer != string.end) { |
1823 | 39.7M | if (!WRITE(emitter, string)) return 0; |
1824 | 39.7M | } |
1825 | | |
1826 | 39.7M | emitter->whitespace = is_whitespace; |
1827 | 39.7M | emitter->indention = (emitter->indention && is_indention); |
1828 | | |
1829 | 39.7M | return 1; |
1830 | 39.7M | } |
1831 | | |
1832 | | static int |
1833 | | yaml_emitter_write_anchor(yaml_emitter_t *emitter, |
1834 | | yaml_char_t *value, size_t length) |
1835 | 0 | { |
1836 | 0 | yaml_string_t string; |
1837 | 0 | STRING_ASSIGN(string, value, length); |
1838 | |
|
1839 | 0 | while (string.pointer != string.end) { |
1840 | 0 | if (!WRITE(emitter, string)) return 0; |
1841 | 0 | } |
1842 | | |
1843 | 0 | emitter->whitespace = 0; |
1844 | 0 | emitter->indention = 0; |
1845 | |
|
1846 | 0 | return 1; |
1847 | 0 | } |
1848 | | |
1849 | | static int |
1850 | | yaml_emitter_write_tag_handle(yaml_emitter_t *emitter, |
1851 | | yaml_char_t *value, size_t length) |
1852 | 9.60M | { |
1853 | 9.60M | yaml_string_t string; |
1854 | 9.60M | STRING_ASSIGN(string, value, length); |
1855 | | |
1856 | 9.60M | if (!emitter->whitespace) { |
1857 | 8.54M | if (!PUT(emitter, ' ')) return 0; |
1858 | 8.54M | } |
1859 | | |
1860 | 28.8M | while (string.pointer != string.end) { |
1861 | 19.2M | if (!WRITE(emitter, string)) return 0; |
1862 | 19.2M | } |
1863 | | |
1864 | 9.60M | emitter->whitespace = 0; |
1865 | 9.60M | emitter->indention = 0; |
1866 | | |
1867 | 9.60M | return 1; |
1868 | 9.60M | } |
1869 | | |
1870 | | static int |
1871 | | yaml_emitter_write_tag_content(yaml_emitter_t *emitter, |
1872 | | yaml_char_t *value, size_t length, |
1873 | | int need_whitespace) |
1874 | 9.60M | { |
1875 | 9.60M | yaml_string_t string; |
1876 | 9.60M | STRING_ASSIGN(string, value, length); |
1877 | | |
1878 | 9.60M | if (need_whitespace && !emitter->whitespace) { |
1879 | 0 | if (!PUT(emitter, ' ')) return 0; |
1880 | 0 | } |
1881 | | |
1882 | 39.7M | while (string.pointer != string.end) { |
1883 | 30.1M | if (IS_ALPHA(string) |
1884 | 0 | || CHECK(string, ';') || CHECK(string, '/') |
1885 | 0 | || CHECK(string, '?') || CHECK(string, ':') |
1886 | 0 | || CHECK(string, '@') || CHECK(string, '&') |
1887 | 0 | || CHECK(string, '=') || CHECK(string, '+') |
1888 | 0 | || CHECK(string, '$') || CHECK(string, ',') |
1889 | 0 | || CHECK(string, '_') || CHECK(string, '.') |
1890 | 0 | || CHECK(string, '~') || CHECK(string, '*') |
1891 | 0 | || CHECK(string, '\'') || CHECK(string, '(') |
1892 | 0 | || CHECK(string, ')') || CHECK(string, '[') |
1893 | 30.1M | || CHECK(string, ']')) { |
1894 | 30.1M | if (!WRITE(emitter, string)) return 0; |
1895 | 30.1M | } |
1896 | 0 | else { |
1897 | 0 | int width = WIDTH(string); |
1898 | 0 | unsigned int value; |
1899 | 0 | while (width --) { |
1900 | 0 | value = *(string.pointer++); |
1901 | 0 | if (!PUT(emitter, '%')) return 0; |
1902 | 0 | if (!PUT(emitter, (value >> 4) |
1903 | 0 | + ((value >> 4) < 10 ? '0' : 'A' - 10))) |
1904 | 0 | return 0; |
1905 | 0 | if (!PUT(emitter, (value & 0x0F) |
1906 | 0 | + ((value & 0x0F) < 10 ? '0' : 'A' - 10))) |
1907 | 0 | return 0; |
1908 | 0 | } |
1909 | 0 | } |
1910 | 30.1M | } |
1911 | | |
1912 | 9.60M | emitter->whitespace = 0; |
1913 | 9.60M | emitter->indention = 0; |
1914 | | |
1915 | 9.60M | return 1; |
1916 | 9.60M | } |
1917 | | |
1918 | | static int |
1919 | | yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, |
1920 | | yaml_char_t *value, size_t length, int allow_breaks) |
1921 | 6.33M | { |
1922 | 6.33M | yaml_string_t string; |
1923 | 6.33M | int spaces = 0; |
1924 | 6.33M | int breaks = 0; |
1925 | | |
1926 | 6.33M | STRING_ASSIGN(string, value, length); |
1927 | | |
1928 | | /** |
1929 | | * Avoid trailing spaces for empty values in block mode. |
1930 | | * In flow mode, we still want the space to prevent ambiguous things |
1931 | | * like {a:}. |
1932 | | * Currently, the emitter forbids any plain empty scalar in flow mode |
1933 | | * (e.g. it outputs {a: ''} instead), so emitter->flow_level will |
1934 | | * never be true here. |
1935 | | * But if the emitter is ever changed to allow emitting empty values, |
1936 | | * the check for flow_level is already here. |
1937 | | */ |
1938 | 6.33M | if (!emitter->whitespace && (length || emitter->flow_level)) { |
1939 | 3.18M | if (!PUT(emitter, ' ')) return 0; |
1940 | 3.18M | } |
1941 | | |
1942 | 44.2M | while (string.pointer != string.end) |
1943 | 37.9M | { |
1944 | 37.9M | if (IS_SPACE(string)) |
1945 | 0 | { |
1946 | 0 | if (allow_breaks && !spaces |
1947 | 0 | && emitter->column > emitter->best_width |
1948 | 0 | && !IS_SPACE_AT(string, 1)) { |
1949 | 0 | if (!yaml_emitter_write_indent(emitter)) return 0; |
1950 | 0 | MOVE(string); |
1951 | 0 | } |
1952 | 0 | else { |
1953 | 0 | if (!WRITE(emitter, string)) return 0; |
1954 | 0 | } |
1955 | 0 | spaces = 1; |
1956 | 0 | } |
1957 | 37.9M | else if (IS_BREAK(string)) |
1958 | 0 | { |
1959 | 0 | if (!breaks && CHECK(string, '\n')) { |
1960 | 0 | if (!PUT_BREAK(emitter)) return 0; |
1961 | 0 | } |
1962 | 0 | if (!WRITE_BREAK(emitter, string)) return 0; |
1963 | 0 | emitter->indention = 1; |
1964 | 0 | breaks = 1; |
1965 | 0 | } |
1966 | 37.9M | else |
1967 | 37.9M | { |
1968 | 37.9M | if (breaks) { |
1969 | 0 | if (!yaml_emitter_write_indent(emitter)) return 0; |
1970 | 0 | } |
1971 | 37.9M | if (!WRITE(emitter, string)) return 0; |
1972 | 37.9M | emitter->indention = 0; |
1973 | 37.9M | spaces = 0; |
1974 | 37.9M | breaks = 0; |
1975 | 37.9M | } |
1976 | 37.9M | } |
1977 | | |
1978 | 6.33M | emitter->whitespace = 0; |
1979 | 6.33M | emitter->indention = 0; |
1980 | | |
1981 | 6.33M | return 1; |
1982 | 6.33M | } |
1983 | | |
1984 | | static int |
1985 | | yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, |
1986 | | yaml_char_t *value, size_t length, int allow_breaks) |
1987 | 0 | { |
1988 | 0 | yaml_string_t string; |
1989 | 0 | int spaces = 0; |
1990 | 0 | int breaks = 0; |
1991 | |
|
1992 | 0 | STRING_ASSIGN(string, value, length); |
1993 | |
|
1994 | 0 | if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0)) |
1995 | 0 | return 0; |
1996 | | |
1997 | 0 | while (string.pointer != string.end) |
1998 | 0 | { |
1999 | 0 | if (IS_SPACE(string)) |
2000 | 0 | { |
2001 | 0 | if (allow_breaks && !spaces |
2002 | 0 | && emitter->column > emitter->best_width |
2003 | 0 | && string.pointer != string.start |
2004 | 0 | && string.pointer != string.end - 1 |
2005 | 0 | && !IS_SPACE_AT(string, 1)) { |
2006 | 0 | if (!yaml_emitter_write_indent(emitter)) return 0; |
2007 | 0 | MOVE(string); |
2008 | 0 | } |
2009 | 0 | else { |
2010 | 0 | if (!WRITE(emitter, string)) return 0; |
2011 | 0 | } |
2012 | 0 | spaces = 1; |
2013 | 0 | } |
2014 | 0 | else if (IS_BREAK(string)) |
2015 | 0 | { |
2016 | 0 | if (!breaks && CHECK(string, '\n')) { |
2017 | 0 | if (!PUT_BREAK(emitter)) return 0; |
2018 | 0 | } |
2019 | 0 | if (!WRITE_BREAK(emitter, string)) return 0; |
2020 | 0 | emitter->indention = 1; |
2021 | 0 | breaks = 1; |
2022 | 0 | } |
2023 | 0 | else |
2024 | 0 | { |
2025 | 0 | if (breaks) { |
2026 | 0 | if (!yaml_emitter_write_indent(emitter)) return 0; |
2027 | 0 | } |
2028 | 0 | if (CHECK(string, '\'')) { |
2029 | 0 | if (!PUT(emitter, '\'')) return 0; |
2030 | 0 | } |
2031 | 0 | if (!WRITE(emitter, string)) return 0; |
2032 | 0 | emitter->indention = 0; |
2033 | 0 | spaces = 0; |
2034 | 0 | breaks = 0; |
2035 | 0 | } |
2036 | 0 | } |
2037 | | |
2038 | 0 | if (breaks) |
2039 | 0 | if (!yaml_emitter_write_indent(emitter)) return 0; |
2040 | | |
2041 | 0 | if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0)) |
2042 | 0 | return 0; |
2043 | | |
2044 | 0 | emitter->whitespace = 0; |
2045 | 0 | emitter->indention = 0; |
2046 | |
|
2047 | 0 | return 1; |
2048 | 0 | } |
2049 | | |
2050 | | static int |
2051 | | yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, |
2052 | | yaml_char_t *value, size_t length, int allow_breaks) |
2053 | 8.40M | { |
2054 | 8.40M | yaml_string_t string; |
2055 | 8.40M | int spaces = 0; |
2056 | | |
2057 | 8.40M | STRING_ASSIGN(string, value, length); |
2058 | | |
2059 | 8.40M | if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0)) |
2060 | 0 | return 0; |
2061 | | |
2062 | 215M | while (string.pointer != string.end) |
2063 | 206M | { |
2064 | 206M | if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string)) |
2065 | 206M | || IS_BOM(string) || IS_BREAK(string) |
2066 | 175M | || CHECK(string, '"') || CHECK(string, '\\')) |
2067 | 32.6M | { |
2068 | 32.6M | unsigned char octet; |
2069 | 32.6M | unsigned int width; |
2070 | 32.6M | unsigned int value; |
2071 | 32.6M | int k; |
2072 | | |
2073 | 32.6M | octet = string.pointer[0]; |
2074 | 32.6M | width = (octet & 0x80) == 0x00 ? 1 : |
2075 | 32.6M | (octet & 0xE0) == 0xC0 ? 2 : |
2076 | 6.56M | (octet & 0xF0) == 0xE0 ? 3 : |
2077 | 4.61M | (octet & 0xF8) == 0xF0 ? 4 : 0; |
2078 | 32.6M | value = (octet & 0x80) == 0x00 ? octet & 0x7F : |
2079 | 32.6M | (octet & 0xE0) == 0xC0 ? octet & 0x1F : |
2080 | 6.56M | (octet & 0xF0) == 0xE0 ? octet & 0x0F : |
2081 | 4.61M | (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; |
2082 | 44.0M | for (k = 1; k < (int)width; k ++) { |
2083 | 11.4M | octet = string.pointer[k]; |
2084 | 11.4M | value = (value << 6) + (octet & 0x3F); |
2085 | 11.4M | } |
2086 | 32.6M | string.pointer += width; |
2087 | | |
2088 | 32.6M | if (!PUT(emitter, '\\')) return 0; |
2089 | | |
2090 | 32.6M | switch (value) |
2091 | 32.6M | { |
2092 | 1.50k | case 0x00: |
2093 | 1.50k | if (!PUT(emitter, '0')) return 0; |
2094 | 1.50k | break; |
2095 | | |
2096 | 4.16k | case 0x07: |
2097 | 4.16k | if (!PUT(emitter, 'a')) return 0; |
2098 | 4.16k | break; |
2099 | | |
2100 | 4.16k | case 0x08: |
2101 | 1.27k | if (!PUT(emitter, 'b')) return 0; |
2102 | 1.27k | break; |
2103 | | |
2104 | 481k | case 0x09: |
2105 | 481k | if (!PUT(emitter, 't')) return 0; |
2106 | 481k | break; |
2107 | | |
2108 | 11.9M | case 0x0A: |
2109 | 11.9M | if (!PUT(emitter, 'n')) return 0; |
2110 | 11.9M | break; |
2111 | | |
2112 | 11.9M | case 0x0B: |
2113 | 3.55M | if (!PUT(emitter, 'v')) return 0; |
2114 | 3.55M | break; |
2115 | | |
2116 | 3.55M | case 0x0C: |
2117 | 1.03M | if (!PUT(emitter, 'f')) return 0; |
2118 | 1.03M | break; |
2119 | | |
2120 | 4.03M | case 0x0D: |
2121 | 4.03M | if (!PUT(emitter, 'r')) return 0; |
2122 | 4.03M | break; |
2123 | | |
2124 | 4.03M | case 0x1B: |
2125 | 406k | if (!PUT(emitter, 'e')) return 0; |
2126 | 406k | break; |
2127 | | |
2128 | 734k | case 0x22: |
2129 | 734k | if (!PUT(emitter, '\"')) return 0; |
2130 | 734k | break; |
2131 | | |
2132 | 734k | case 0x5C: |
2133 | 390k | if (!PUT(emitter, '\\')) return 0; |
2134 | 390k | break; |
2135 | | |
2136 | 390k | case 0x85: |
2137 | 147k | if (!PUT(emitter, 'N')) return 0; |
2138 | 147k | break; |
2139 | | |
2140 | 147k | case 0xA0: |
2141 | 129k | if (!PUT(emitter, '_')) return 0; |
2142 | 129k | break; |
2143 | | |
2144 | 663k | case 0x2028: |
2145 | 663k | if (!PUT(emitter, 'L')) return 0; |
2146 | 663k | break; |
2147 | | |
2148 | 663k | case 0x2029: |
2149 | 226k | if (!PUT(emitter, 'P')) return 0; |
2150 | 226k | break; |
2151 | | |
2152 | 8.82M | default: |
2153 | 8.82M | if (value <= 0xFF) { |
2154 | 3.43M | if (!PUT(emitter, 'x')) return 0; |
2155 | 3.43M | width = 2; |
2156 | 3.43M | } |
2157 | 5.39M | else if (value <= 0xFFFF) { |
2158 | 5.13M | if (!PUT(emitter, 'u')) return 0; |
2159 | 5.13M | width = 4; |
2160 | 5.13M | } |
2161 | 262k | else { |
2162 | 262k | if (!PUT(emitter, 'U')) return 0; |
2163 | 262k | width = 8; |
2164 | 262k | } |
2165 | 38.3M | for (k = (width-1)*4; k >= 0; k -= 4) { |
2166 | 29.4M | int digit = (value >> k) & 0x0F; |
2167 | 29.4M | if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10))) |
2168 | 0 | return 0; |
2169 | 29.4M | } |
2170 | 32.6M | } |
2171 | 32.6M | spaces = 0; |
2172 | 32.6M | } |
2173 | 174M | else if (IS_SPACE(string)) |
2174 | 6.97M | { |
2175 | 6.97M | if (allow_breaks && !spaces |
2176 | 3.00M | && emitter->column > emitter->best_width |
2177 | 179k | && string.pointer != string.start |
2178 | 178k | && string.pointer != string.end - 1) { |
2179 | 178k | if (!yaml_emitter_write_indent(emitter)) return 0; |
2180 | 178k | if (IS_SPACE_AT(string, 1)) { |
2181 | 15.8k | if (!PUT(emitter, '\\')) return 0; |
2182 | 15.8k | } |
2183 | 178k | MOVE(string); |
2184 | 178k | } |
2185 | 6.79M | else { |
2186 | 6.79M | if (!WRITE(emitter, string)) return 0; |
2187 | 6.79M | } |
2188 | 6.97M | spaces = 1; |
2189 | 6.97M | } |
2190 | 167M | else |
2191 | 167M | { |
2192 | 167M | if (!WRITE(emitter, string)) return 0; |
2193 | 167M | spaces = 0; |
2194 | 167M | } |
2195 | 206M | } |
2196 | | |
2197 | 8.40M | if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0)) |
2198 | 0 | return 0; |
2199 | | |
2200 | 8.40M | emitter->whitespace = 0; |
2201 | 8.40M | emitter->indention = 0; |
2202 | | |
2203 | 8.40M | return 1; |
2204 | 8.40M | } |
2205 | | |
2206 | | static int |
2207 | | yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, |
2208 | | yaml_string_t string) |
2209 | 0 | { |
2210 | 0 | char indent_hint[2]; |
2211 | 0 | const char *chomp_hint = NULL; |
2212 | |
|
2213 | 0 | if (IS_SPACE(string) || IS_BREAK(string)) |
2214 | 0 | { |
2215 | 0 | indent_hint[0] = '0' + (char)emitter->best_indent; |
2216 | 0 | indent_hint[1] = '\0'; |
2217 | 0 | if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0)) |
2218 | 0 | return 0; |
2219 | 0 | } |
2220 | | |
2221 | 0 | emitter->open_ended = 0; |
2222 | |
|
2223 | 0 | string.pointer = string.end; |
2224 | 0 | if (string.start == string.pointer) |
2225 | 0 | { |
2226 | 0 | chomp_hint = "-"; |
2227 | 0 | } |
2228 | 0 | else |
2229 | 0 | { |
2230 | 0 | do { |
2231 | 0 | string.pointer --; |
2232 | 0 | } while ((*string.pointer & 0xC0) == 0x80); |
2233 | 0 | if (!IS_BREAK(string)) |
2234 | 0 | { |
2235 | 0 | chomp_hint = "-"; |
2236 | 0 | } |
2237 | 0 | else if (string.start == string.pointer) |
2238 | 0 | { |
2239 | 0 | chomp_hint = "+"; |
2240 | 0 | emitter->open_ended = 2; |
2241 | 0 | } |
2242 | 0 | else |
2243 | 0 | { |
2244 | 0 | do { |
2245 | 0 | string.pointer --; |
2246 | 0 | } while ((*string.pointer & 0xC0) == 0x80); |
2247 | 0 | if (IS_BREAK(string)) |
2248 | 0 | { |
2249 | 0 | chomp_hint = "+"; |
2250 | 0 | emitter->open_ended = 2; |
2251 | 0 | } |
2252 | 0 | } |
2253 | 0 | } |
2254 | |
|
2255 | 0 | if (chomp_hint) |
2256 | 0 | { |
2257 | 0 | if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0)) |
2258 | 0 | return 0; |
2259 | 0 | } |
2260 | | |
2261 | 0 | return 1; |
2262 | 0 | } |
2263 | | |
2264 | | static int |
2265 | | yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, |
2266 | | yaml_char_t *value, size_t length) |
2267 | 0 | { |
2268 | 0 | yaml_string_t string; |
2269 | 0 | int breaks = 1; |
2270 | |
|
2271 | 0 | STRING_ASSIGN(string, value, length); |
2272 | |
|
2273 | 0 | if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0)) |
2274 | 0 | return 0; |
2275 | 0 | if (!yaml_emitter_write_block_scalar_hints(emitter, string)) |
2276 | 0 | return 0; |
2277 | 0 | if (!PUT_BREAK(emitter)) return 0; |
2278 | 0 | emitter->indention = 1; |
2279 | 0 | emitter->whitespace = 1; |
2280 | |
|
2281 | 0 | while (string.pointer != string.end) |
2282 | 0 | { |
2283 | 0 | if (IS_BREAK(string)) |
2284 | 0 | { |
2285 | 0 | if (!WRITE_BREAK(emitter, string)) return 0; |
2286 | 0 | emitter->indention = 1; |
2287 | 0 | breaks = 1; |
2288 | 0 | } |
2289 | 0 | else |
2290 | 0 | { |
2291 | 0 | if (breaks) { |
2292 | 0 | if (!yaml_emitter_write_indent(emitter)) return 0; |
2293 | 0 | } |
2294 | 0 | if (!WRITE(emitter, string)) return 0; |
2295 | 0 | emitter->indention = 0; |
2296 | 0 | breaks = 0; |
2297 | 0 | } |
2298 | 0 | } |
2299 | | |
2300 | 0 | return 1; |
2301 | 0 | } |
2302 | | |
2303 | | static int |
2304 | | yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, |
2305 | | yaml_char_t *value, size_t length) |
2306 | 0 | { |
2307 | 0 | yaml_string_t string; |
2308 | 0 | int breaks = 1; |
2309 | 0 | int leading_spaces = 1; |
2310 | |
|
2311 | 0 | STRING_ASSIGN(string, value, length); |
2312 | |
|
2313 | 0 | if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0)) |
2314 | 0 | return 0; |
2315 | 0 | if (!yaml_emitter_write_block_scalar_hints(emitter, string)) |
2316 | 0 | return 0; |
2317 | 0 | if (!PUT_BREAK(emitter)) return 0; |
2318 | 0 | emitter->indention = 1; |
2319 | 0 | emitter->whitespace = 1; |
2320 | |
|
2321 | 0 | while (string.pointer != string.end) |
2322 | 0 | { |
2323 | 0 | if (IS_BREAK(string)) |
2324 | 0 | { |
2325 | 0 | if (!breaks && !leading_spaces && CHECK(string, '\n')) { |
2326 | 0 | int k = 0; |
2327 | 0 | while (IS_BREAK_AT(string, k)) { |
2328 | 0 | k += WIDTH_AT(string, k); |
2329 | 0 | } |
2330 | 0 | if (!IS_BLANKZ_AT(string, k)) { |
2331 | 0 | if (!PUT_BREAK(emitter)) return 0; |
2332 | 0 | } |
2333 | 0 | } |
2334 | 0 | if (!WRITE_BREAK(emitter, string)) return 0; |
2335 | 0 | emitter->indention = 1; |
2336 | 0 | breaks = 1; |
2337 | 0 | } |
2338 | 0 | else |
2339 | 0 | { |
2340 | 0 | if (breaks) { |
2341 | 0 | if (!yaml_emitter_write_indent(emitter)) return 0; |
2342 | 0 | leading_spaces = IS_BLANK(string); |
2343 | 0 | } |
2344 | 0 | if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1) |
2345 | 0 | && emitter->column > emitter->best_width) { |
2346 | 0 | if (!yaml_emitter_write_indent(emitter)) return 0; |
2347 | 0 | MOVE(string); |
2348 | 0 | } |
2349 | 0 | else { |
2350 | 0 | if (!WRITE(emitter, string)) return 0; |
2351 | 0 | } |
2352 | 0 | emitter->indention = 0; |
2353 | 0 | breaks = 0; |
2354 | 0 | } |
2355 | 0 | } |
2356 | | |
2357 | 0 | return 1; |
2358 | 0 | } |