Skip to content

Commit

Permalink
Fix control character bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ndtoan96 committed Dec 21, 2022
1 parent d64a5d2 commit 1691ab6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Tomlyn.Tests/ValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,20 @@ public void TestTableArrayAndInvalidTable()
Assert.True(doc.HasErrors, "The document should have errors");
}

[Test]
public void TestValidControlCharacters()
{
var input = "";
input += "backspace='\b'\n";
input += "tab='\t'\n";
input += "form_feed='\f'\n";
input += "quote='\"'\n";
input += "backslash='\\'\n";
input += "heart='\u2665'\n";
input += "some_unicode='\U0002B695'\n";
var doc = Toml.Parse(input);
Assert.False(doc.HasErrors, "The document should not have errors on valid control characters");
}

}
}
2 changes: 1 addition & 1 deletion src/Tomlyn/Parsing/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ private void ReadStringLiteral(TextPosition start, bool allowMultiline)
{
AddError("Invalid newline in a string", _position, _position);
}
else if (CharHelper.IsControlCharacter(_c) && (!isMultiLine || !CharHelper.IsNewLine(_c)))
else if (CharHelper.IsControlCharacter(_c) && !CharHelper.IsNonNewlineValidControlCharacter(_c) && (!isMultiLine || !CharHelper.IsNewLine(_c)))
{
AddError($"Invalid control character found {((char)_c).ToPrintableString()}", start, start);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Tomlyn/Text/CharHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static bool IsControlCharacter(char32 c)
return c <= 0x1F || c == 0x7F;
}

public static bool IsNonNewlineValidControlCharacter(char32 c)
{
return c == '\b' || c == '\t' || c == '\f';
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsKeyStart(char32 c)
{
Expand Down

0 comments on commit 1691ab6

Please sign in to comment.