]> andersk Git - libyaml.git/blobdiff - src/emitter.c
Fixed emitting folded scalars with trailing breaks; Forced emitting of a document...
[libyaml.git] / src / emitter.c
index ad3fc39a24d5c944d629ca7bf494de43c810e9b8..e4e3d2612b5e055965fc75eb95c953154ba7b290 100644 (file)
@@ -249,7 +249,7 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
         yaml_char_t *value, size_t length, int allow_breaks);
 
 static int
-yaml_emitter_determine_chomping(yaml_emitter_t *emitter,
+yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
         yaml_string_t string);
 
 static int
@@ -290,7 +290,7 @@ yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event)
             return 0;
         if (!yaml_emitter_state_machine(emitter, emitter->events.head))
             return 0;
-        DEQUEUE(emitter, emitter->events);
+        yaml_event_delete(&DEQUEUE(emitter, emitter->events));
     }
 
     return 1;
@@ -587,6 +587,17 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
             implicit = 0;
         }
 
+        if ((event->data.document_start.version_directive ||
+                    (event->data.document_start.tag_directives.start
+                     != event->data.document_start.tag_directives.end)) &&
+                emitter->open_ended)
+        {
+            if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
+                return 0;
+            if (!yaml_emitter_write_indent(emitter))
+                return 0;
+        }
+
         if (event->data.document_start.version_directive) {
             implicit = 0;
             if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0))
@@ -638,6 +649,14 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
 
     else if (event->type == YAML_STREAM_END_EVENT)
     {
+        if (emitter->open_ended)
+        {
+            if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
+                return 0;
+            if (!yaml_emitter_write_indent(emitter))
+                return 0;
+        }
+
         if (!yaml_emitter_flush(emitter))
             return 0;
 
@@ -687,6 +706,13 @@ yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
 
         emitter->state = YAML_EMIT_DOCUMENT_START_STATE;
 
+        while (!STACK_EMPTY(emitter, emitter->tag_directives)) {
+            yaml_tag_directive_t tag_directive = POP(emitter,
+                    emitter->tag_directives);
+            yaml_free(tag_directive.handle);
+            yaml_free(tag_directive.prefix);
+        }
+
         return 1;
     }
 
@@ -695,6 +721,7 @@ yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
 }
 
 /*
+ * 
  * Expect a flow item node.
  */
 
@@ -728,11 +755,16 @@ yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
         return 1;
     }
 
+    if (!first) {
+        if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
+            return 0;
+    }
+
     if (emitter->canonical || emitter->column > emitter->best_width) {
         if (!yaml_emitter_write_indent(emitter))
             return 0;
     }
-    if (PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
+    if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
         return 0;
 
     return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
@@ -1147,6 +1179,13 @@ static int
 yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
 {
     yaml_scalar_style_t style = event->data.scalar.style;
+    int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix);
+
+    if (no_tag && !event->data.scalar.plain_implicit
+            && !event->data.scalar.quoted_implicit) {
+        return yaml_emitter_set_emitter_error(emitter,
+                "neither tag nor implicit flags are specified");
+    }
 
     if (style == YAML_ANY_SCALAR_STYLE)
         style = YAML_PLAIN_SCALAR_STYLE;
@@ -1165,8 +1204,7 @@ yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
         if (!emitter->scalar_data.length
                 && (emitter->flow_level || emitter->simple_key_context))
             style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
-        if (!event->data.scalar.plain_implicit
-                && !emitter->tag_data.handle && !emitter->tag_data.suffix)
+        if (no_tag && !event->data.scalar.plain_implicit)
             style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
     }
 
@@ -1178,23 +1216,16 @@ yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
 
     if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
     {
-        if (!emitter->scalar_data.block_allowed)
+        if (!emitter->scalar_data.block_allowed
+                || emitter->flow_level || emitter->simple_key_context)
             style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
     }
 
-    if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
+    if (no_tag && !event->data.scalar.quoted_implicit
+            && style != YAML_PLAIN_SCALAR_STYLE)
     {
-        if (!event->data.scalar.plain_implicit
-                && !event->data.scalar.quoted_implicit) {
-            return yaml_emitter_set_emitter_error(emitter,
-                    "neither tag nor implicit flags are specified");
-        }
-
-        if (event->data.scalar.plain_implicit
-                && style != YAML_PLAIN_SCALAR_STYLE) {
-            emitter->tag_data.handle = (yaml_char_t *)"!";
-            emitter->tag_data.handle_length = 1;
-        }
+        emitter->tag_data.handle = (yaml_char_t *)"!";
+        emitter->tag_data.handle_length = 1;
     }
 
     emitter->scalar_data.style = style;
@@ -1340,7 +1371,7 @@ yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
 
     handle.pointer ++;
 
-    while (handle.pointer != handle.end-1) {
+    while (handle.pointer < handle.end-1) {
         if (!IS_ALPHA(handle)) {
             return yaml_emitter_set_emitter_error(emitter,
                     "tag handle must contain alphanumerical characters only");
@@ -1380,6 +1411,12 @@ yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
         }
         MOVE(string);
     }
+
+    emitter->anchor_data.anchor = string.start;
+    emitter->anchor_data.anchor_length = string.end - string.start;
+    emitter->anchor_data.alias = alias;
+
+    return 1;
 }
 
 /*
@@ -1399,9 +1436,9 @@ yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
     }
 
     for (tag_directive = emitter->tag_directives.start;
-            tag_directive != emitter->tag_directives.end; tag_directive ++) {
+            tag_directive != emitter->tag_directives.top; tag_directive ++) {
         size_t prefix_length = strlen((char *)tag_directive->prefix);
-        if (prefix_length < (string.end - string.start)
+        if (prefix_length < (size_t)(string.end - string.start)
                 && strncmp((char *)tag_directive->prefix, (char *)string.start,
                     prefix_length) == 0)
         {
@@ -1436,21 +1473,17 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
     int line_breaks = 0;
     int special_characters = 0;
 
-    int inline_spaces = 0;
-    int inline_breaks = 0;
-    int leading_spaces = 0;
-    int leading_breaks = 0;
-    int trailing_spaces = 0;
-    int trailing_breaks = 0;
-    int inline_breaks_spaces = 0;
-    int mixed_breaks_spaces = 0;
-
-    int preceeded_by_space = 0;
-    int followed_by_space = 0;
-    int spaces = 0;
-    int breaks = 0;
-    int mixed = 0;
-    int leading = 0;
+    int leading_space = 0;
+    int leading_break = 0;
+    int trailing_space = 0;
+    int trailing_break = 0;
+    int break_space = 0;
+    int space_break = 0;
+
+    int preceeded_by_whitespace = 0;
+    int followed_by_whitespace = 0;
+    int previous_space = 0;
+    int previous_break = 0;
 
     emitter->scalar_data.value = value;
     emitter->scalar_data.length = length;
@@ -1476,8 +1509,8 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
         flow_indicators = 1;
     }
 
-    preceeded_by_space = 1;
-    followed_by_space = IS_BLANKZ_AT(string, WIDTH(string));
+    preceeded_by_whitespace = 1;
+    followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
 
     while (string.pointer != string.end)
     {
@@ -1497,12 +1530,12 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
 
             if (CHECK(string, '?') || CHECK(string, ':')) {
                 flow_indicators = 1;
-                if (followed_by_space) {
+                if (followed_by_whitespace) {
                     block_indicators = 1;
                 }
             }
 
-            if (CHECK(string, '-') && followed_by_space) {
+            if (CHECK(string, '-') && followed_by_whitespace) {
                 flow_indicators = 1;
                 block_indicators = 1;
             }
@@ -1517,12 +1550,12 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
 
             if (CHECK(string, ':')) {
                 flow_indicators = 1;
-                if (followed_by_space) {
+                if (followed_by_whitespace) {
                     block_indicators = 1;
                 }
             }
 
-            if (CHECK(string, '#') && preceeded_by_space) {
+            if (CHECK(string, '#') && preceeded_by_whitespace) {
                 flow_indicators = 1;
                 block_indicators = 1;
             }
@@ -1539,57 +1572,42 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
 
         if (IS_SPACE(string))
         {
-            spaces = 1;
             if (string.start == string.pointer) {
-                leading = 1;
+                leading_space = 1;
+            }
+            if (string.pointer+WIDTH(string) == string.end) {
+                trailing_space = 1;
+            }
+            if (previous_break) {
+                break_space = 1;
             }
+            previous_space = 1;
+            previous_break = 0;
         }
-
         else if (IS_BREAK(string))
         {
-            if (spaces) {
-                mixed = 1;
-            }
-            breaks = 1;
             if (string.start == string.pointer) {
-                leading = 1;
+                leading_break = 1;
             }
-        }
-
-        else if (spaces || breaks)
-        {
-            if (leading) {
-                if (spaces && breaks) {
-                    mixed_breaks_spaces = 1;
-                }
-                else if (spaces) {
-                    leading_spaces = 1;
-                }
-                else if (breaks) {
-                    leading_breaks = 1;
-                }
+            if (string.pointer+WIDTH(string) == string.end) {
+                trailing_break = 1;
             }
-            else {
-                if (mixed) {
-                    mixed_breaks_spaces = 1;
-                }
-                else if (spaces && breaks) {
-                    inline_breaks_spaces = 1;
-                }
-                else if (spaces) {
-                    inline_spaces = 1;
-                }
-                else if (breaks) {
-                    inline_breaks = 1;
-                }
+            if (previous_space) {
+                space_break = 1;
             }
-            spaces = breaks = mixed = leading = 0;
+            previous_space = 0;
+            previous_break = 1;
+        }
+        else
+        {
+            previous_space = 0;
+            previous_break = 0;
         }
 
-        preceeded_by_space = IS_BLANKZ(string);
+        preceeded_by_whitespace = IS_BLANKZ(string);
         MOVE(string);
         if (string.pointer != string.end) {
-            followed_by_space = IS_BLANKZ_AT(string, WIDTH(string));
+            followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
         }
     }
 
@@ -1600,24 +1618,22 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
     emitter->scalar_data.single_quoted_allowed = 1;
     emitter->scalar_data.block_allowed = 1;
 
-    if (leading_spaces || leading_breaks || trailing_spaces) {
+    if (leading_space || leading_break || trailing_space || trailing_break) {
         emitter->scalar_data.flow_plain_allowed = 0;
         emitter->scalar_data.block_plain_allowed = 0;
-        emitter->scalar_data.block_allowed = 0;
     }
 
-    if (trailing_breaks) {
-        emitter->scalar_data.flow_plain_allowed = 0;
-        emitter->scalar_data.block_plain_allowed = 0;
+    if (trailing_space) {
+        emitter->scalar_data.block_allowed = 0;
     }
 
-    if (inline_breaks_spaces) {
+    if (break_space) {
         emitter->scalar_data.flow_plain_allowed = 0;
         emitter->scalar_data.block_plain_allowed = 0;
         emitter->scalar_data.single_quoted_allowed = 0;
     }
 
-    if (mixed_breaks_spaces || special_characters) {
+    if (space_break || special_characters) {
         emitter->scalar_data.flow_plain_allowed = 0;
         emitter->scalar_data.block_plain_allowed = 0;
         emitter->scalar_data.single_quoted_allowed = 0;
@@ -1768,6 +1784,7 @@ yaml_emitter_write_indicator(yaml_emitter_t *emitter,
 
     emitter->whitespace = is_whitespace;
     emitter->indention = (emitter->indention && is_indention);
+    emitter->open_ended = 0;
 
     return 1;
 }
@@ -1839,8 +1856,8 @@ yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
             while (width --) {
                 value = *(string.pointer++);
                 if (!PUT(emitter, '%')) return 0;
-                if (!PUT(emitter, (value >> 8)
-                            + ((value >> 8) < 10 ? '0' : 'A' - 10)))
+                if (!PUT(emitter, (value >> 4)
+                            + ((value >> 4) < 10 ? '0' : 'A' - 10)))
                     return 0;
                 if (!PUT(emitter, (value & 0x0F)
                             + ((value & 0x0F) < 10 ? '0' : 'A' - 10)))
@@ -1905,6 +1922,10 @@ yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
 
     emitter->whitespace = 0;
     emitter->indention = 0;
+    if (emitter->root_context)
+    {
+        emitter->open_ended = 1;
+    }
 
     return 1;
 }
@@ -2000,7 +2021,7 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
                     (octet & 0xE0) == 0xC0 ? octet & 0x1F :
                     (octet & 0xF0) == 0xE0 ? octet & 0x0F :
                     (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
-            for (k = 1; k < width; k ++) {
+            for (k = 1; k < (int)width; k ++) {
                 octet = string.pointer[k];
                 value = (value << 6) + (octet & 0x3F);
             }
@@ -2083,8 +2104,10 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
                         if (!PUT(emitter, 'U')) return 0;
                         width = 8;
                     }
-                    for (k = width*4; k >= 0; k -= 4) {
-                        if (!PUT(emitter, (value >> k) & 0x0F)) return 0;
+                    for (k = (width-1)*4; k >= 0; k -= 4) {
+                        int digit = (value >> k) & 0x0F;
+                        if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10)))
+                            return 0;
                     }
             }
             spaces = 0;
@@ -2123,26 +2146,61 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
 }
 
 static int
-yaml_emitter_determine_chomping(yaml_emitter_t *emitter,
+yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
         yaml_string_t string)
 {
+    char indent_hint[2];
+    char *chomp_hint = NULL;
+
+    if (IS_SPACE(string) || IS_BREAK(string))
+    {
+        indent_hint[0] = '0' + (char)emitter->best_indent;
+        indent_hint[1] = '\0';
+        if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0))
+            return 0;
+    }
+
+    emitter->open_ended = 0;
+
     string.pointer = string.end;
     if (string.start == string.pointer)
-        return -1;
-    while ((string.pointer[-1] & 0xC0) == 0x80) {
-        string.pointer --;
+    {
+        chomp_hint = "-";
     }
-    if (!IS_BREAK(string))
-        return -1;
-    if (string.start == string.pointer)
-        return 0;
-    while ((string.pointer[-1] & 0xC0) == 0x80) {
-        string.pointer --;
+    else
+    {
+        do {
+            string.pointer --;
+        } while ((*string.pointer & 0xC0) == 0x80);
+        if (!IS_BREAK(string))
+        {
+            chomp_hint = "-";
+        }
+        else if (string.start == string.pointer)
+        {
+            chomp_hint = "+";
+            emitter->open_ended = 1;
+        }
+        else
+        {
+            do {
+                string.pointer --;
+            } while ((*string.pointer & 0xC0) == 0x80);
+            if (IS_BREAK(string))
+            {
+                chomp_hint = "+";
+                emitter->open_ended = 1;
+            }
+        }
     }
-    if (!IS_BREAK(string))
-        return 0;
-    return +1;
-    
+
+    if (chomp_hint)
+    {
+        if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0))
+            return 0;
+    }
+
+    return 1;
 }
 
 static int
@@ -2150,14 +2208,15 @@ yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
         yaml_char_t *value, size_t length)
 {
     yaml_string_t string = STRING(value, length);
-    int chomp = yaml_emitter_determine_chomping(emitter, string);
-    int breaks = 0;
+    int breaks = 1;
 
-    if (!yaml_emitter_write_indicator(emitter,
-                chomp == -1 ? "|-" : chomp == +1 ? "|+" : "|", 1, 0, 0))
+    if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0))
         return 0;
-    if (!yaml_emitter_write_indent(emitter))
+    if (!yaml_emitter_write_block_scalar_hints(emitter, string))
         return 0;
+    if (!PUT_BREAK(emitter)) return 0;
+    emitter->indention = 1;
+    emitter->whitespace = 1;
 
     while (string.pointer != string.end)
     {
@@ -2178,8 +2237,6 @@ yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
         }
     }
 
-    if (!yaml_emitter_write_indent(emitter)) return 0;
-
     return 1;
 }
 
@@ -2188,15 +2245,16 @@ yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
         yaml_char_t *value, size_t length)
 {
     yaml_string_t string = STRING(value, length);
-    int chomp = yaml_emitter_determine_chomping(emitter, string);
-    int breaks = 0;
+    int breaks = 1;
     int leading_spaces = 1;
 
-    if (!yaml_emitter_write_indicator(emitter,
-                chomp == -1 ? ">-" : chomp == +1 ? ">+" : ">", 1, 0, 0))
+    if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0))
         return 0;
-    if (!yaml_emitter_write_indent(emitter))
+    if (!yaml_emitter_write_block_scalar_hints(emitter, string))
         return 0;
+    if (!PUT_BREAK(emitter)) return 0;
+    emitter->indention = 1;
+    emitter->whitespace = 1;
 
     while (string.pointer != string.end)
     {
@@ -2207,7 +2265,7 @@ yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
                 while (IS_BREAK_AT(string, k)) {
                     k += WIDTH_AT(string, k);
                 }
-                if (!IS_BLANK_AT(string, k)) {
+                if (!IS_BLANKZ_AT(string, k)) {
                     if (!PUT_BREAK(emitter)) return 0;
                 }
             }
@@ -2234,8 +2292,6 @@ yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
         }
     }
 
-    if (!yaml_emitter_write_indent(emitter)) return 0;
-
     return 1;
 }
 
This page took 0.081413 seconds and 4 git commands to generate.