Skip to content

Commit

Permalink
Fix newline handling when emitting folded scalars.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaubry committed Aug 24, 2013
1 parent 60002d0 commit a05e021
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
22 changes: 15 additions & 7 deletions YamlDotNet.Core/Emitter.cs
Expand Up @@ -1091,6 +1091,11 @@ private static bool IsBreak(char character)
return character == '\r' || character == '\n' || character == '\x85' || character == '\x2028' || character == '\x2029';
}

private static bool IsBlank(char character)
{
return character == ' ' || character == '\t';
}

/// <summary>
/// Check if the specified character is a space.
/// </summary>
Expand All @@ -1099,6 +1104,8 @@ private static bool IsSpace(char character)
return character == ' ';
}



private static bool IsPrintable(char character)
{
return
Expand All @@ -1114,6 +1121,7 @@ private static bool IsPrintable(char character)
private void WriteFoldedScalar(string value)
{
bool previous_break = true;
bool leading_spaces = true;

WriteIndicator(">", true, false, false);
WriteBlockScalarHints(value);
Expand All @@ -1125,21 +1133,20 @@ private void WriteFoldedScalar(string value)
for (int i = 0; i < value.Length; ++i)
{
char character = value[i];

if (IsBreak(character))
{
if ((i + 1) < value.Length)
if (!previous_break && !leading_spaces && character == '\n')
{
if (character == '\r' && value[i + 1] == '\n')
int k = 0;
while (i + k < value.Length && IsBreak(value[i + k]))
{
++i;
++k;
}
else if (character == '\n' && value[i + 1] == '\r')
if (i + k < value.Length && !(IsBlank(value[i + k]) || IsBreak(value[i + k])))
{
++i;
WriteBreak();
}
}

WriteBreak();
isIndentation = true;
previous_break = true;
Expand All @@ -1149,6 +1156,7 @@ private void WriteFoldedScalar(string value)
if (previous_break)
{
WriteIndent();
leading_spaces = IsBlank(character);
}
if (!previous_break && character == ' ' && i + 1 < value.Length && value[i + 1] != ' ' && column > bestWidth)
{
Expand Down
26 changes: 19 additions & 7 deletions YamlDotNet.UnitTests/Core/EmitterTests.cs
Expand Up @@ -206,33 +206,45 @@ private string Emit(params ParsingEvent[] events)
[InlineData("CRLF hello\r\nworld")]
public void FoldedStyleDoesNotLooseCharacters(string text)
{
var yaml = Emit(new Scalar(null, null, text, ScalarStyle.Folded, true, false));
var yaml = EmitScalar(new Scalar(null, null, text, ScalarStyle.Folded, true, false));
Console.WriteLine(yaml);
Assert.True(yaml.Contains("world"));
}

[Fact]
public void FoldedStyleIsSelectedWhenNewLinesAreFoundInLiteral()
{
var yaml = Emit(new Scalar(null, null, "hello\nworld", ScalarStyle.Any, true, false));
var yaml = EmitScalar(new Scalar(null, null, "hello\nworld", ScalarStyle.Any, true, false));
Console.WriteLine(yaml);
Assert.True(yaml.Contains(">"));
}

[Fact]
public void FoldedStyleDoesNotGenerateExtraLineBreaks()
{
var yaml = Emit(new Scalar(null, null, "hello\nworld", ScalarStyle.Folded, true, false));
var yaml = EmitScalar(new Scalar(null, null, "hello\nworld", ScalarStyle.Folded, true, false));
Console.WriteLine(yaml);
Assert.False(yaml.Contains("\r\n\r\n"));

var stream = new YamlStream();
stream.Load(new StringReader(yaml));
var sequence = (YamlSequenceNode)stream.Documents[0].RootNode;
var scalar = (YamlScalarNode)sequence.Children[0];

Assert.Equal("hello\nworld", scalar.Value);
}

[Fact]
public void FoldedStyleDoesNotCollapseLineBreaks()
{
var yaml = Emit(new Scalar(null, null, ">+\n", ScalarStyle.Folded, true, false));
Console.WriteLine(yaml);
Assert.True(yaml.Contains("\r\n\r\n"));
var yaml = EmitScalar(new Scalar(null, null, ">+\n", ScalarStyle.Folded, true, false));
Console.WriteLine("${0}$", yaml);

var stream = new YamlStream();
stream.Load(new StringReader(yaml));
var sequence = (YamlSequenceNode)stream.Documents[0].RootNode;
var scalar = (YamlScalarNode)sequence.Children[0];

Assert.Equal(">+\n", scalar.Value);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet.build
Expand Up @@ -4,7 +4,7 @@
<!-- This build file requires .NET 4.0 and nant 0.91 alpha 2 or higher -->
<property name="nant.settings.currentframework" value="net-4.0"/>

<property name="version" value="2.1.0" />
<property name="version" value="2.2.0" />

<fileset id="binaries">
<include name="YamlDotNet.Core/bin/Release/YamlDotNet.Core.dll" />
Expand Down

0 comments on commit a05e021

Please sign in to comment.