Skip to content

Commit

Permalink
Use Regex.Unescape() to escape literal strings
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed Aug 1, 2024
1 parent da53476 commit 60826f7
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions LocalisationAnalyser.Tools/Php/PhpStringLiteralSyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System.Text;
using System.Text.RegularExpressions;

namespace LocalisationAnalyser.Tools.Php
{
Expand All @@ -22,33 +23,25 @@ public static PhpStringLiteralSyntaxNode Parse(PhpTokeniser tokeniser)
tokeniser.SkipWhitespace();

char trivia = tokeniser.GetTrivia();

// Skip leading trivia.
tokeniser.Advance();

var stringBuilder = new StringBuilder();
bool isEscaping = false;
char? lastToken = null;

while (isEscaping || tokeniser.GetTrivia() != trivia)
while (tokeniser.GetTrivia() != trivia || lastToken == '\\')
{
var token = tokeniser.GetTrivia();
char token = tokeniser.GetTrivia();
tokeniser.Advance();

if (token == '\\' && !isEscaping)
{
isEscaping = true;
continue;
}

stringBuilder.Append(token);
isEscaping = false;
lastToken = token;
}

// Skip trailing trivia.
tokeniser.Advance();
tokeniser.SkipWhitespace();

return new PhpStringLiteralSyntaxNode(stringBuilder.ToString());
return new PhpStringLiteralSyntaxNode(Regex.Unescape(stringBuilder.ToString()));
}
}
}

0 comments on commit 60826f7

Please sign in to comment.