diff --git a/emitterc.go b/emitterc.go index 0f47c9ca..0abf4418 100644 --- a/emitterc.go +++ b/emitterc.go @@ -1943,11 +1943,11 @@ func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) boo for i := 0; i < len(value); { if is_break(value, i) { if !breaks && !leading_spaces && value[i] == '\n' { - k := 0 - for is_break(value, k) { + k := i + for k < len(value) && is_break(value, k) { k += width(value[k]) } - if !is_blankz(value, k) { + if k < len(value) && !is_blankz(value, k) { if !put_break(emitter) { return false } diff --git a/encode_test.go b/encode_test.go index 4a8bf2e2..fba70d35 100644 --- a/encode_test.go +++ b/encode_test.go @@ -456,6 +456,24 @@ var marshalTests = []struct { Style: yaml.SingleQuotedStyle, }, "'foo'\n", + }, { + yaml.Node{ + Kind: yaml.ScalarNode, + Tag: "!!str", + Style: yaml.FoldedStyle, + Value: `first line + more`, + }, + ">-\n first line\n more\n", + }, { + yaml.Node{ + Kind: yaml.ScalarNode, + Tag: "!!str", + Style: yaml.FoldedStyle, + Value: `first line +more`, + }, + ">-\n first line\n\n more\n", }, // Enforced tagging with shorthand notation (issue #616).