Skip to content

Commit 29dbd29

Browse files
authored
chore: modify regex settings (#10539)
* chore: modify regex settings * chore: fix ci error of plantuml test
1 parent bb54fe6 commit 29dbd29

File tree

10 files changed

+39
-20
lines changed

10 files changed

+39
-20
lines changed

src/Docfx.Build/HtmlTemplate.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
namespace Docfx.Build;
1010

11-
struct HtmlTemplate
11+
partial struct HtmlTemplate
1212
{
13+
[GeneratedRegex("(\\s+[a-zA-Z0-9_-]+)=([\"']){(\\d)}[\"']")]
14+
private static partial Regex AttributePlaceholderRegex();
15+
1316
private string? _html;
1417

1518
public override string ToString() => _html ?? "";
@@ -18,7 +21,7 @@ struct HtmlTemplate
1821

1922
public static HtmlTemplate Html(FormattableString template)
2023
{
21-
var format = Regex.Replace(template.Format, "(\\s+[a-zA-Z0-9_-]+)=([\"']){(\\d)}[\"']", RenderAttribute);
24+
var format = AttributePlaceholderRegex().Replace(template.Format, RenderAttribute);
2225
var html = string.Format(format, Array.ConvertAll(template.GetArguments(), Render));
2326
return new() { _html = html };
2427

src/Docfx.Build/ResourceFileReaders/ResourceFileReader.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ bool filter(string s)
3636
{
3737
if (selector != null)
3838
{
39-
var regex = new Regex(selector, RegexOptions.IgnoreCase);
40-
return regex.IsMatch(s);
39+
return Regex.IsMatch(s, selector, RegexOptions.IgnoreCase);
4140
}
4241
else
4342
{

src/Docfx.Build/TemplateProcessors/ViewRenderers/MustacheTemplateRenderer.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ internal partial class MustacheTemplateRenderer : ITemplateRenderer
1111
{
1212
public const string Extension = ".tmpl";
1313

14-
[GeneratedRegex(@"{{\s*!\s*include\s*\(:?(:?['""]?)\s*(?<file>(.+?))\1\s*\)\s*}}", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en-AU")]
15-
private static partial Regex IncludeRegex();
1614

17-
[GeneratedRegex(@"{{\s*!\s*master\s*\(:?(:?['""]?)\s*(?<file>(.+?))\1\s*\)\s*}}\s*\n?", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en-AU")]
18-
private static partial Regex MasterPageRegex();
19-
20-
[GeneratedRegex(@"{{\s*!\s*body\s*}}\s*\n?", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en-AU")]
15+
// Following regex are not supported by GeneratedRegexAttribute. (Because it contains case-insensitive backreferences)
16+
// When using GeneratedRegexAttribute. following message are shown as information leve.
17+
// SYSLIB1044: The regex generator couldn't generate a complete source implementation for the specified regular expression due to an internal limitation. See the explanation in the generated source for more details.
18+
// And Regex instance is created with following remarks comments.
19+
// A custom Regex-derived type could not be generated because the expression contains case-insensitive backreferences which are not supported by the source generator.
20+
#pragma warning disable SYSLIB1045
21+
private static readonly Regex IncludeRegex = new(@"{{\s*!\s*include\s*\(:?(:?['""]?)\s*(?<file>(.+?))\1\s*\)\s*}}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
22+
private static readonly Regex MasterPageRegex = new(@"{{\s*!\s*master\s*\(:?(:?['""]?)\s*(?<file>(.+?))\1\s*\)\s*}}\s*\n?", RegexOptions.Compiled | RegexOptions.IgnoreCase);
23+
#pragma warning restore SYSLIB1045
24+
25+
[GeneratedRegex(@"{{\s*!\s*body\s*}}\s*\n?", RegexOptions.IgnoreCase)]
2126
private static partial Regex MasterPageBodyRegex();
2227

2328
private readonly ResourceFileReader _reader;
@@ -42,7 +47,7 @@ public MustacheTemplateRenderer(ResourceFileReader reader, ResourceInfo info, st
4247
})
4348
.Build();
4449

45-
var processedTemplate = ParseTemplateHelper.ExpandMasterPage(reader, info, MasterPageRegex(), MasterPageBodyRegex());
50+
var processedTemplate = ParseTemplateHelper.ExpandMasterPage(reader, info, MasterPageRegex, MasterPageBodyRegex());
4651

4752
_template = processedTemplate;
4853

@@ -68,7 +73,7 @@ public string Render(object model)
6873
/// <param name="template"></param>
6974
private IEnumerable<string> ExtractDependencyResourceNames(string template)
7075
{
71-
foreach (Match match in IncludeRegex().Matches(template))
76+
foreach (Match match in IncludeRegex.Matches(template))
7277
{
7378
var filePath = match.Groups["file"].Value;
7479
foreach (var name in ParseTemplateHelper.GetResourceName(filePath, Path, _reader))

src/Docfx.Build/TemplateProcessors/ViewRenderers/ParseTemplateHelper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ public static IEnumerable<string> GetResourceName(string file, string templateNa
7878
{
7979
file = regexPatternMatch.Groups[1].Value;
8080
var resourceKey = GetRelativeResourceKey(templateName, file);
81-
var regex = new Regex(resourceKey, RegexOptions.IgnoreCase);
8281
foreach (var name in reader.Names)
8382
{
84-
if (regex.IsMatch(name))
83+
if (Regex.IsMatch(name, resourceKey, RegexOptions.IgnoreCase))
8584
{
8685
yield return name;
8786
}

src/Docfx.Dotnet/DotnetApiCatalog.ApiPage.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ namespace Docfx.Dotnet;
1818

1919
partial class DotnetApiCatalog
2020
{
21+
// Regex to match any character other than a word character(alphabet/numeric/underscore)
22+
[GeneratedRegex(@"\W")]
23+
private static partial Regex NonWordCharRegex();
24+
2125
private static void CreatePages(Action<string, string, ApiPage> output, List<(IAssemblySymbol symbol, Compilation compilation)> assemblies, ExtractMetadataConfig config, DotnetApiOptions options)
2226
{
2327
Directory.CreateDirectory(config.OutputFolder);
@@ -122,7 +126,7 @@ void Heading(int level, string title, string? id = null)
122126
void Api(int level, string title, ISymbol symbol, Compilation compilation)
123127
{
124128
var uid = VisitorHelper.GetId(symbol);
125-
var id = Regex.Replace(uid, @"\W", "_");
129+
var id = NonWordCharRegex().Replace(uid, "_");
126130
var commentId = VisitorHelper.GetCommentId(symbol);
127131
var source = config.DisableGitFeatures ? null : VisitorHelper.GetSourceDetail(symbol, compilation);
128132
var git = source?.Remote is null ? null

src/Docfx.Dotnet/Filters/ConfigFilterRuleItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public string UidRegex
2020
}
2121
set
2222
{
23-
_uidRegex = new Regex(value);
23+
_uidRegex = new Regex(value, RegexOptions.Compiled);
2424
}
2525
}
2626

src/Docfx.Dotnet/Parsers/XmlComment.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ internal partial class XmlComment
3838
[GeneratedRegex(@"^\s*<!--\s*</(.*)>\s*-->$")]
3939
private static partial Regex XmlEndRegionRegex();
4040

41+
[GeneratedRegex(@"^(\s*)&gt;", RegexOptions.Multiline)]
42+
private static partial Regex BlockQuoteRegex();
43+
4144
private readonly XmlCommentParserContext _context;
4245

4346
public string Summary { get; private set; }
@@ -603,7 +606,7 @@ static string HandleBlockQuote(string xml)
603606
{
604607
// > is encoded to &gt; in XML. When interpreted as markdown, > is as blockquote
605608
// Decode standalone &gt; to > to enable the block quote markdown syntax
606-
return Regex.Replace(xml, @"^(\s*)&gt;", "$1>", RegexOptions.Multiline);
609+
return BlockQuoteRegex().Replace(xml, "$1>");
607610
}
608611

609612
static void MarkdownXmlDecode(MarkdownObject node)

src/Docfx.Dotnet/SymbolUrlResolver.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ enum SymbolUrlKind
1616

1717
internal static partial class SymbolUrlResolver
1818
{
19+
// Regex to match any character other than a word character(alphabet/numeric/underscore)
20+
[GeneratedRegex(@"\W")]
21+
private static partial Regex NonWordCharRegex();
22+
1923
public static string? GetSymbolUrl(ISymbol symbol, Compilation compilation, MemberLayout memberLayout, SymbolUrlKind urlKind, HashSet<IAssemblySymbol> allAssemblies, SymbolFilter filter)
2024
{
2125
// Reduce symbol into generic definitions
@@ -53,8 +57,8 @@ internal static partial class SymbolUrlResolver
5357
"!" => null,
5458
"N" or "T" => $"{VisitorHelper.PathFriendlyId(uid)}{ext}",
5559
"M" or "F" or "P" or "E" => memberLayout is MemberLayout.SeparatePages && !symbol.IsEnumMember()
56-
? $"{VisitorHelper.PathFriendlyId(VisitorHelper.GetOverloadId(symbol))}{ext}#{Regex.Replace(uid, @"\W", "_")}"
57-
: $"{VisitorHelper.PathFriendlyId(VisitorHelper.GetId(symbol.ContainingType))}{ext}#{Regex.Replace(uid, @"\W", "_")}",
60+
? $"{VisitorHelper.PathFriendlyId(VisitorHelper.GetOverloadId(symbol))}{ext}#{NonWordCharRegex().Replace(uid, "_")}"
61+
: $"{VisitorHelper.PathFriendlyId(VisitorHelper.GetId(symbol.ContainingType))}{ext}#{NonWordCharRegex().Replace(uid, "_")}",
5862
_ => throw new NotSupportedException($"Unknown comment ID format '{type}'"),
5963
};
6064
}

src/Docfx.MarkdigEngine.Extensions/CodeSnippet/CodeSnippetExtractor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Docfx.MarkdigEngine.Extensions;
99

1010
public partial class CodeSnippetExtractor
1111
{
12-
[GeneratedRegex(@"^[\w\.-]+$", RegexOptions.IgnoreCase, "en-AU")]
12+
[GeneratedRegex(@"^[\w\.-]+$", RegexOptions.IgnoreCase)]
1313
private static partial Regex TagnameFormat();
1414

1515
private readonly string StartLineTemplate;

test/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ root = false
44
[*.cs]
55
csharp_style_unused_value_expression_statement_preference = discard_variable:silent # IDE0058: Remove unnecessary expression value
66
csharp_style_unused_value_assignment_preference = discard_variable:silent # IDE0059: Remove unnecessary value assignment
7+
8+
dotnet_diagnostic.SYSLIB1045.severity = silent # SYSLIB1045: Convert to 'GeneratedRegexAttribute'.

0 commit comments

Comments
 (0)