Skip to content

Commit

Permalink
BUGFIX: CreateEscapedString better escaping of special (newline) char…
Browse files Browse the repository at this point in the history
…acters.
  • Loading branch information
Washi1337 committed Nov 19, 2023
1 parent c5f86b9 commit 6e039b9
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/AsmResolver/ISegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ bool CanUpdateOffsets

public static partial class Extensions
{
private const string ReservedStringCharacters = "\\\"\t\r\n\b";

[ThreadStatic]
private static StringBuilder? _buffer;

Expand Down Expand Up @@ -125,9 +123,22 @@ public static string CreateEscapedString(this string literal)
_buffer.Append('"');
foreach (char currentChar in literal)
{
if (ReservedStringCharacters.Contains(currentChar))
_buffer.Append('\\');
_buffer.Append(currentChar);
string? replacement = currentChar switch
{
'\0' => "\\0",
'\\' => "\\\\",
'\"' => "\\\"",
'\t' => "\\t",
'\r' => "\\r",
'\n' => "\\n",
'\b' => "\\b",
_ => null
};

if (replacement is not null)
_buffer.Append(replacement);
else
_buffer.Append(currentChar);
}
_buffer.Append('"');

Expand Down

0 comments on commit 6e039b9

Please sign in to comment.