From 7d4857f7391c96a4cd54a68d23ba7c542a975599 Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Wed, 13 Feb 2019 21:47:44 +0100 Subject: [PATCH] Fix control characters checks for multiline strings --- src/Tomlyn/Parsing/Lexer.cs | 6 ++++-- src/Tomlyn/Text/CharHelper.cs | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Tomlyn/Parsing/Lexer.cs b/src/Tomlyn/Parsing/Lexer.cs index 72c6b68..773c671 100644 --- a/src/Tomlyn/Parsing/Lexer.cs +++ b/src/Tomlyn/Parsing/Lexer.cs @@ -539,6 +539,8 @@ private void ReadNumberOrDate(char32? signPrefix = null, TextPosition? signPrefi // If we have a space, followed by a digit, try to parse the following if (CharHelper.IsWhiteSpace(_c) && CharHelper.IsDateTime(PeekChar())) { + _textBuilder.AppendUtf32(_c); // Append the space + NextChar(); // skip the space while (CharHelper.IsDateTime(_c)) { _textBuilder.AppendUtf32(_c); @@ -740,7 +742,7 @@ private void ReadString(TextPosition start, bool allowMultiline) { AddError("Invalid newline in a string", _position, _position); } - else if (_c < 32 && (!isMultiLine || !CharHelper.IsNewLine(_c))) + else if (CharHelper.IsControlCharacter(_c) && (!isMultiLine || !CharHelper.IsNewLine(_c))) { AddError($"Invalid control character found {((char)_c).ToPrintableString()}", start, start); } @@ -948,7 +950,7 @@ private void ReadStringLiteral(TextPosition start, bool allowMultiline) { AddError("Invalid newline in a string", _position, _position); } - else if (_c < 32 && (!isMultiLine || !CharHelper.IsNewLine(_c))) + else if (CharHelper.IsControlCharacter(_c) && (!isMultiLine || !CharHelper.IsNewLine(_c))) { AddError($"Invalid control character found {((char)_c).ToPrintableString()}", start, start); } diff --git a/src/Tomlyn/Text/CharHelper.cs b/src/Tomlyn/Text/CharHelper.cs index d42bb10..13bc000 100644 --- a/src/Tomlyn/Text/CharHelper.cs +++ b/src/Tomlyn/Text/CharHelper.cs @@ -18,6 +18,12 @@ internal static partial class CharHelper public static readonly Func OctalToDecFunc = OctalToDecimal; public static readonly Func BinaryToDecFunc = BinaryToDecimal; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsControlCharacter(char32 c) + { + return c <= 0x1F || c == 0x7F; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsKeyStart(char32 c) {