diff --git a/Src/ILGPU/Frontend/Intrinsic/LanguageIntrinsics.cs b/Src/ILGPU/Frontend/Intrinsic/LanguageIntrinsics.cs
index 645acc20f..90637ce49 100644
--- a/Src/ILGPU/Frontend/Intrinsic/LanguageIntrinsics.cs
+++ b/Src/ILGPU/Frontend/Intrinsic/LanguageIntrinsics.cs
@@ -15,6 +15,7 @@
using ILGPU.Util;
using System;
using System.Collections.Immutable;
+using System.Text.RegularExpressions;
using static ILGPU.Util.FormatString;
using FormatArray = System.Collections.Immutable.ImmutableArray<
ILGPU.Util.FormatString.FormatExpression>;
@@ -47,6 +48,13 @@ public LanguageIntrinsicAttribute(LanguageIntrinsicKind intrinsicKind)
partial class Intrinsics
{
+ ///
+ /// Regex for parsing PTX assembly instructions.
+ ///
+ private static readonly Regex PTXExpressionRegex =
+ // Escape sequence, %n arguments, singular % detection.
+ new Regex("(%%|%\\d+|%)");
+
///
/// Handles language operations.
///
@@ -165,59 +173,33 @@ public static bool TryParse(
string ptxExpression,
out FormatArray expressions)
{
- expressions = FormatArray.Empty;
-
// Search for '%n' format arguments
- var result = ImmutableArray.CreateBuilder(10);
- var expression = ptxExpression.AsSpan();
+ var parts = PTXExpressionRegex.Split(ptxExpression);
+ var result = ImmutableArray.CreateBuilder(parts.Length);
- while (expression.Length > 0)
+ foreach (var part in parts)
{
- // Search for next %
- int foundIndex = expression.IndexOf('%');
- if (foundIndex < 0)
+ if (part == "%%")
{
- result.Add(new FormatExpression(expression));
- break;
+ result.Add(new FormatExpression("%"));
}
-
- var escaped =
- foundIndex + 1 < expression.Length &&
- expression[foundIndex] == expression[foundIndex + 1];
- if (escaped)
- {
- result.Add(new FormatExpression(expression.Slice(0, foundIndex + 1)));
- expression = expression.Slice(foundIndex + 2);
- }
- else
+ else if (part.StartsWith("%"))
{
- // Append text before new argument
- if (foundIndex > 0)
+ // Check whether the argument can be resolved to an integer.
+ if (int.TryParse(part.Substring(1), out int argument))
{
- result.Add(new FormatExpression(
- expression.Slice(0, foundIndex)));
+ result.Add(new FormatExpression(argument));
}
- expression = expression.Slice(foundIndex + 1);
-
- // Retrieve all the numeric text
- if (expression.Length == 0)
- return false;
-
- int endIndex = 0;
- while (endIndex < expression.Length &&
- char.IsDigit(expression[endIndex]))
+ else
{
- endIndex++;
- }
-
- // Check whether the argument can be resolved to an integer
- var numericExpr = expression.Slice(0, endIndex);
- if (!int.TryParse(numericExpr.ToString(), out int argument))
+ // Singular % or remaining text was not a number.
+ expressions = FormatArray.Empty;
return false;
-
- // Append current argument
- result.Add(new FormatExpression(argument));
- expression = expression.Slice(endIndex);
+ }
+ }
+ else if (part.Length > 0)
+ {
+ result.Add(new FormatExpression(part));
}
}