Skip to content

Commit cc91daa

Browse files
committed
Fix: Handling of "line-ending-backslash" in multiline basic strings
1 parent fcace8b commit cc91daa

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

Tomlet/TomlParser.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ private string DecipherUnicodeEscapeSequence(string unicodeString, bool fourDigi
476476
default:
477477
if (allowNewline && escapedChar.IsNewline())
478478
return null;
479-
throw new InvalidTomlEscapeException(_lineNumber, $"\\{escapedChar}");
479+
throw new InvalidTomlEscapeException(_lineNumber, $"\\{(char) escapedChar}");
480480
}
481481

482482
return toAppend;
@@ -604,6 +604,43 @@ private TomlValue ReadMultiLineBasicString(TomletStringReader reader)
604604
if (escapeMode)
605605
{
606606
escapeMode = false;
607+
608+
//If the escaped char is whitespace, and there's no other non-whitespace on this line, we skip all whitespace until the next non-whitespace char.
609+
if (nextChar.IsWhitespace() || nextChar.IsNewline())
610+
{
611+
//Check that everything else on this line is whitespace
612+
var backtrack = 0;
613+
var skipWhitespace = false;
614+
while (reader.TryPeek(out var nextCharOnLine))
615+
{
616+
backtrack++;
617+
reader.Read();
618+
619+
if (nextCharOnLine.IsNewline())
620+
{
621+
skipWhitespace = true;
622+
break;
623+
}
624+
625+
if (!nextCharOnLine.IsWhitespace())
626+
{
627+
//Backslash-whitespace but then non-whitespace char before newline, this is invalid, we can break here and let HandleEscapedChar deal with it
628+
break;
629+
}
630+
}
631+
632+
//Return back to where we were
633+
reader.Backtrack(backtrack);
634+
635+
if (skipWhitespace)
636+
{
637+
_lineNumber += reader.SkipAnyNewlineOrWhitespace();
638+
continue; //The main while loop
639+
}
640+
641+
//Otherwise we drop down to HandleEscapedChar, which will throw due to the whitespace
642+
}
643+
607644
var toAppend = HandleEscapedChar(nextChar, out fourDigitUnicodeMode, out eightDigitUnicodeMode, true);
608645

609646
if (toAppend.HasValue)
@@ -618,7 +655,7 @@ private TomlValue ReadMultiLineBasicString(TomletStringReader reader)
618655
_lineNumber++;
619656

620657
//Escaped newline indicates we skip this newline and any whitespace at the start of the next line
621-
reader.SkipAnyNewlineOrWhitespace();
658+
_lineNumber += reader.SkipAnyNewlineOrWhitespace();
622659
}
623660

624661
continue;

0 commit comments

Comments
 (0)