Skip to content

Commit 43e46ea

Browse files
Copilotkzu
andcommitted
Migrate TagExpressions.cs to source-generated regexes
Agent-Logs-Url: https://github.com/devlooped/Schematron/sessions/f32a5ddd-99bd-47c6-b680-474fbf82aa8e Co-authored-by: kzu <169707+kzu@users.noreply.github.com>
1 parent c0de3a0 commit 43e46ea

6 files changed

Lines changed: 30 additions & 41 deletions

File tree

src/Schematron/Config.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static void Setup()
143143
{
144144
System.Diagnostics.Trace.Write("Loading schematron statics...");
145145
System.Diagnostics.Trace.Write(CompiledExpressions.Schema.ReturnType);
146-
System.Diagnostics.Trace.Write(TagExpressions.Dir.RightToLeft);
146+
System.Diagnostics.Trace.Write(TagExpressions.Dir().RightToLeft);
147147
System.Diagnostics.Trace.WriteLine(FormattingUtils.XmlErrorPosition.RightToLeft);
148148
}
149149
}

src/Schematron/Formatters/LogFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public override void Format(Test source, XPathNavigator context, StringBuilder o
1717
{
1818
var sb = FormatMessage(source, context, source.Message);
1919
// Finally remove any non-name schematron tag in the message.
20-
var res = TagExpressions.AllSchematron.Replace(sb.ToString(), string.Empty);
20+
var res = TagExpressions.AllSchematron().Replace(sb.ToString(), string.Empty);
2121
sb = new StringBuilder();
2222
if (source is Assert)
2323
{

src/Schematron/Formatters/XmlFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override void Format(Test source, XPathNavigator context, StringBuilder o
4141
msg = FormatMessage(source, context, msg).ToString();
4242

4343
// Finally remove any non-name schematron tag in the message.
44-
var res = TagExpressions.AllSchematron.Replace(msg, string.Empty);
44+
var res = TagExpressions.AllSchematron().Replace(msg, string.Empty);
4545

4646
//Accumulate namespaces found during traversal of node for its position.
4747
var ns = new Hashtable();

src/Schematron/Schematron.csproj

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<AssemblyName>Schematron</AssemblyName>
55
<RootNamespace>Schematron</RootNamespace>
6-
<TargetFramework>netstandard2.0</TargetFramework>
6+
<TargetFramework>net8.0</TargetFramework>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageId>Schematron.NET</PackageId>
99
<LangVersion>Latest</LangVersion>
@@ -13,11 +13,4 @@
1313
<InternalsVisibleTo Include="Schematron.Tests" />
1414
</ItemGroup>
1515

16-
<ItemGroup>
17-
<PackageReference Include="PolySharp" Version="1.15.0">
18-
<PrivateAssets>all</PrivateAssets>
19-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20-
</PackageReference>
21-
</ItemGroup>
22-
2316
</Project>

src/Schematron/TagExpressions.cs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,37 @@
33
namespace Schematron;
44

55
/// <summary />
6-
class TagExpressions
6+
partial class TagExpressions
77
{
88
/// <summary>
99
/// The compiled regular expression to replace the special <c>name</c> and <c>value</c> tags inside a message.
1010
/// </summary>
1111
/// <remarks>
1212
/// Replaces each instance of <c>name</c> and <c>value</c>tags with the value in the current context element.
1313
/// </remarks>
14-
public static Regex NameValueOf;
15-
16-
public static Regex Emph;
17-
public static Regex Dir;
18-
public static Regex Span;
19-
public static Regex Para;
20-
public static Regex Any;
21-
public static Regex AllSchematron;
22-
23-
/// <summary />
24-
static TagExpressions()
25-
{
26-
// The element declarations can contain the namespace if expanded in a loaded document.
27-
NameValueOf = new Regex(@"<[^\s>]*\b(name|value-of)\b[^>]*/>", RegexOptions.Compiled);
28-
Emph = new Regex(@"<[^\s>]*\bemph\b[^>]*>", RegexOptions.Compiled);
29-
Dir = new Regex(@"<[^\s]*\bdir\b[^>]*>", RegexOptions.Compiled);
30-
Span = new Regex(@"<[^\s]*\bspan\b[^>]*>", RegexOptions.Compiled);
31-
Para = new Regex(@"<[^\s]*\bp\b[^>]*>", RegexOptions.Compiled);
32-
Any = new Regex(@"<[^\s]*[^>]*>", RegexOptions.Compiled);
33-
// Closing elements don't have an expanded xmlns so they will be matched too.
34-
// TODO: improve this to avoid removing non-schematron closing elements.
35-
var nsPattern = "(?:" + Regex.Escape(Schema.LegacyNamespace) + "|" + Regex.Escape(Schema.IsoNamespace) + ")";
36-
AllSchematron = new Regex(@"<.*\bxmlns\b[^\s]*" + nsPattern + "[^>]*>|</[^>]*>", RegexOptions.Compiled);
37-
}
38-
39-
TagExpressions()
40-
{
41-
}
14+
// The element declarations can contain the namespace if expanded in a loaded document.
15+
[GeneratedRegex(@"<[^\s>]*\b(name|value-of)\b[^>]*/>")]
16+
public static partial Regex NameValueOf();
17+
18+
[GeneratedRegex(@"<[^\s>]*\bemph\b[^>]*>")]
19+
public static partial Regex Emph();
20+
21+
[GeneratedRegex(@"<[^\s]*\bdir\b[^>]*>")]
22+
public static partial Regex Dir();
23+
24+
[GeneratedRegex(@"<[^\s]*\bspan\b[^>]*>")]
25+
public static partial Regex Span();
26+
27+
[GeneratedRegex(@"<[^\s]*\bp\b[^>]*>")]
28+
public static partial Regex Para();
29+
30+
[GeneratedRegex(@"<[^\s]*[^>]*>")]
31+
public static partial Regex Any();
32+
33+
// Closing elements don't have an expanded xmlns so they will be matched too.
34+
// TODO: improve this to avoid removing non-schematron closing elements.
35+
// Pattern derived from Schema.LegacyNamespace and Schema.IsoNamespace (with Regex.Escape applied).
36+
[GeneratedRegex(@"<.*\bxmlns\b[^\s]*(?:http://www\.ascc\.net/xml/schematron|http://purl\.oclc\.org/dsdl/schematron)[^>]*>|</[^>]*>")]
37+
public static partial Regex AllSchematron();
4238
}
4339

src/Schematron/Test.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public Test(string test, string message) : base(test)
2222
// Save <name> and <value-of> tags in the message and their paths / selects in their compiled form.
2323
// TODO: see if we can work with the XML in the message, instead of using RE.
2424
// TODO: Check the correct usage of path and select attributes.
25-
NameValueOfExpressions = TagExpressions.NameValueOf.Matches(Message);
25+
NameValueOfExpressions = TagExpressions.NameValueOf().Matches(Message);
2626
var nc = NameValueOfExpressions.Count;
2727
NamePaths = new XPathExpression[nc];
2828
ValueOfSelects = new XPathExpression[nc];

0 commit comments

Comments
 (0)