Skip to content

Commit

Permalink
chore: modify regex settings (#10539)
Browse files Browse the repository at this point in the history
* chore: modify regex settings

* chore: fix ci error of plantuml test
  • Loading branch information
filzrev authored Feb 18, 2025
1 parent bb54fe6 commit 29dbd29
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 20 deletions.
7 changes: 5 additions & 2 deletions src/Docfx.Build/HtmlTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

namespace Docfx.Build;

struct HtmlTemplate
partial struct HtmlTemplate
{
[GeneratedRegex("(\\s+[a-zA-Z0-9_-]+)=([\"']){(\\d)}[\"']")]
private static partial Regex AttributePlaceholderRegex();

private string? _html;

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

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

Expand Down
3 changes: 1 addition & 2 deletions src/Docfx.Build/ResourceFileReaders/ResourceFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ bool filter(string s)
{
if (selector != null)
{
var regex = new Regex(selector, RegexOptions.IgnoreCase);
return regex.IsMatch(s);
return Regex.IsMatch(s, selector, RegexOptions.IgnoreCase);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ internal partial class MustacheTemplateRenderer : ITemplateRenderer
{
public const string Extension = ".tmpl";

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

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

[GeneratedRegex(@"{{\s*!\s*body\s*}}\s*\n?", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en-AU")]
// Following regex are not supported by GeneratedRegexAttribute. (Because it contains case-insensitive backreferences)
// When using GeneratedRegexAttribute. following message are shown as information leve.
// 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.
// And Regex instance is created with following remarks comments.
// A custom Regex-derived type could not be generated because the expression contains case-insensitive backreferences which are not supported by the source generator.
#pragma warning disable SYSLIB1045
private static readonly Regex IncludeRegex = new(@"{{\s*!\s*include\s*\(:?(:?['""]?)\s*(?<file>(.+?))\1\s*\)\s*}}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex MasterPageRegex = new(@"{{\s*!\s*master\s*\(:?(:?['""]?)\s*(?<file>(.+?))\1\s*\)\s*}}\s*\n?", RegexOptions.Compiled | RegexOptions.IgnoreCase);
#pragma warning restore SYSLIB1045

[GeneratedRegex(@"{{\s*!\s*body\s*}}\s*\n?", RegexOptions.IgnoreCase)]
private static partial Regex MasterPageBodyRegex();

private readonly ResourceFileReader _reader;
Expand All @@ -42,7 +47,7 @@ public MustacheTemplateRenderer(ResourceFileReader reader, ResourceInfo info, st
})
.Build();

var processedTemplate = ParseTemplateHelper.ExpandMasterPage(reader, info, MasterPageRegex(), MasterPageBodyRegex());
var processedTemplate = ParseTemplateHelper.ExpandMasterPage(reader, info, MasterPageRegex, MasterPageBodyRegex());

_template = processedTemplate;

Expand All @@ -68,7 +73,7 @@ public string Render(object model)
/// <param name="template"></param>
private IEnumerable<string> ExtractDependencyResourceNames(string template)
{
foreach (Match match in IncludeRegex().Matches(template))
foreach (Match match in IncludeRegex.Matches(template))
{
var filePath = match.Groups["file"].Value;
foreach (var name in ParseTemplateHelper.GetResourceName(filePath, Path, _reader))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ public static IEnumerable<string> GetResourceName(string file, string templateNa
{
file = regexPatternMatch.Groups[1].Value;
var resourceKey = GetRelativeResourceKey(templateName, file);
var regex = new Regex(resourceKey, RegexOptions.IgnoreCase);
foreach (var name in reader.Names)
{
if (regex.IsMatch(name))
if (Regex.IsMatch(name, resourceKey, RegexOptions.IgnoreCase))
{
yield return name;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Docfx.Dotnet/DotnetApiCatalog.ApiPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ namespace Docfx.Dotnet;

partial class DotnetApiCatalog
{
// Regex to match any character other than a word character(alphabet/numeric/underscore)
[GeneratedRegex(@"\W")]
private static partial Regex NonWordCharRegex();

private static void CreatePages(Action<string, string, ApiPage> output, List<(IAssemblySymbol symbol, Compilation compilation)> assemblies, ExtractMetadataConfig config, DotnetApiOptions options)
{
Directory.CreateDirectory(config.OutputFolder);
Expand Down Expand Up @@ -122,7 +126,7 @@ void Heading(int level, string title, string? id = null)
void Api(int level, string title, ISymbol symbol, Compilation compilation)
{
var uid = VisitorHelper.GetId(symbol);
var id = Regex.Replace(uid, @"\W", "_");
var id = NonWordCharRegex().Replace(uid, "_");
var commentId = VisitorHelper.GetCommentId(symbol);
var source = config.DisableGitFeatures ? null : VisitorHelper.GetSourceDetail(symbol, compilation);
var git = source?.Remote is null ? null
Expand Down
2 changes: 1 addition & 1 deletion src/Docfx.Dotnet/Filters/ConfigFilterRuleItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public string UidRegex
}
set
{
_uidRegex = new Regex(value);
_uidRegex = new Regex(value, RegexOptions.Compiled);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/Docfx.Dotnet/Parsers/XmlComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ internal partial class XmlComment
[GeneratedRegex(@"^\s*<!--\s*</(.*)>\s*-->$")]
private static partial Regex XmlEndRegionRegex();

[GeneratedRegex(@"^(\s*)&gt;", RegexOptions.Multiline)]
private static partial Regex BlockQuoteRegex();

private readonly XmlCommentParserContext _context;

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

static void MarkdownXmlDecode(MarkdownObject node)
Expand Down
8 changes: 6 additions & 2 deletions src/Docfx.Dotnet/SymbolUrlResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ enum SymbolUrlKind

internal static partial class SymbolUrlResolver
{
// Regex to match any character other than a word character(alphabet/numeric/underscore)
[GeneratedRegex(@"\W")]
private static partial Regex NonWordCharRegex();

public static string? GetSymbolUrl(ISymbol symbol, Compilation compilation, MemberLayout memberLayout, SymbolUrlKind urlKind, HashSet<IAssemblySymbol> allAssemblies, SymbolFilter filter)
{
// Reduce symbol into generic definitions
Expand Down Expand Up @@ -53,8 +57,8 @@ internal static partial class SymbolUrlResolver
"!" => null,
"N" or "T" => $"{VisitorHelper.PathFriendlyId(uid)}{ext}",
"M" or "F" or "P" or "E" => memberLayout is MemberLayout.SeparatePages && !symbol.IsEnumMember()
? $"{VisitorHelper.PathFriendlyId(VisitorHelper.GetOverloadId(symbol))}{ext}#{Regex.Replace(uid, @"\W", "_")}"
: $"{VisitorHelper.PathFriendlyId(VisitorHelper.GetId(symbol.ContainingType))}{ext}#{Regex.Replace(uid, @"\W", "_")}",
? $"{VisitorHelper.PathFriendlyId(VisitorHelper.GetOverloadId(symbol))}{ext}#{NonWordCharRegex().Replace(uid, "_")}"
: $"{VisitorHelper.PathFriendlyId(VisitorHelper.GetId(symbol.ContainingType))}{ext}#{NonWordCharRegex().Replace(uid, "_")}",
_ => throw new NotSupportedException($"Unknown comment ID format '{type}'"),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Docfx.MarkdigEngine.Extensions;

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

private readonly string StartLineTemplate;
Expand Down
2 changes: 2 additions & 0 deletions test/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ root = false
[*.cs]
csharp_style_unused_value_expression_statement_preference = discard_variable:silent # IDE0058: Remove unnecessary expression value
csharp_style_unused_value_assignment_preference = discard_variable:silent # IDE0059: Remove unnecessary value assignment

dotnet_diagnostic.SYSLIB1045.severity = silent # SYSLIB1045: Convert to 'GeneratedRegexAttribute'.

0 comments on commit 29dbd29

Please sign in to comment.