Skip to content

Commit

Permalink
Merge branch 'saudi/fix-regex-slashes-for-windows' into 'main'
Browse files Browse the repository at this point in the history
fix regex path matching for Windows when the string contains /

See merge request Sharpmake/sharpmake!542
  • Loading branch information
bchampoux committed Aug 12, 2024
2 parents 4eb8e0f + 8aa4809 commit 1dc8f0a
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Sharpmake/RegexCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,27 @@ public static CachedRegex GetRegex(string expression)

private static CachedRegex CreateCachedRegex(string expression)
{
if (Util.UsesUnixSeparator && expression.IndexOf(@"\\", System.StringComparison.Ordinal) >= 0)
expression = expression.Replace(@"\\", Regex.Escape(Util.UnixSeparator.ToString()));
if (Util.UsesUnixSeparator)
{
if (expression.Contains(@"\\", System.StringComparison.Ordinal))
{
expression = expression.Replace(@"\\", Regex.Escape(Util.UnixSeparator.ToString()));
}
}
else
{
if (expression.Contains(@"/", System.StringComparison.Ordinal))
{
string oldExpression = expression;
// Handle the case where there are character escapes:
// \/ is equivalent to /, but only if \ is not itself escaped.
expression = expression.Replace(@"\\\", @"/") // First get the double backslashes out of the way (they will be converted back). Now the only backslashes left are not escaped.
.Replace(@"\/", @"/")
.Replace(@"/", Regex.Escape(Util.WindowsSeparator.ToString()));

Util.LogWrite($"Warning: Converting regex to native separators, to avoid breaking cross-compilation on Windows: {oldExpression} changed to {expression}");
}
}

return new CachedRegex(
new Regex(
Expand Down

0 comments on commit 1dc8f0a

Please sign in to comment.