Skip to content

Commit

Permalink
Use Regex source generator
Browse files Browse the repository at this point in the history
Use the Regex source generator for .NET 7+.
Resolves domaindrivendev#2794.
  • Loading branch information
martincostello committed Apr 14, 2024
1 parent 58b873f commit 75512e3
Showing 1 changed file with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@

namespace Swashbuckle.AspNetCore.SwaggerGen
{
public static class XmlCommentsTextHelper
public static partial class XmlCommentsTextHelper
{
private static Regex RefTagPattern = new Regex(@"<(see|paramref) (name|cref|langword)=""([TPF]{1}:)?(?<display>.+?)"" ?/>");
private static Regex CodeTagPattern = new Regex(@"<c>(?<display>.+?)</c>");
private static Regex MultilineCodeTagPattern = new Regex(@"<code>(?<display>.+?)</code>", RegexOptions.Singleline);
private static Regex ParaTagPattern = new Regex(@"<para>(?<display>.+?)</para>", RegexOptions.Singleline);

public static string Humanize(string text)
{
if (text == null)
Expand Down Expand Up @@ -89,28 +84,56 @@ private static string GetCommonLeadingWhitespace(string[] lines)

private static string HumanizeRefTags(this string text)
{
return RefTagPattern.Replace(text, (match) => match.Groups["display"].Value);
return RefTag().Replace(text, (match) => match.Groups["display"].Value);
}

private static string HumanizeCodeTags(this string text)
{
return CodeTagPattern.Replace(text, (match) => "`" + match.Groups["display"].Value + "`");
return CodeTag().Replace(text, (match) => "`" + match.Groups["display"].Value + "`");
}

private static string HumanizeMultilineCodeTags(this string text)
{
return MultilineCodeTagPattern.Replace(text, (match) => "```" + match.Groups["display"].Value + "```");
return MultilineCodeTag().Replace(text, (match) => "```" + match.Groups["display"].Value + "```");
}

private static string HumanizeParaTags(this string text)
{
return ParaTagPattern.Replace(text, (match) => "<br>" + match.Groups["display"].Value);
return ParaTag().Replace(text, (match) => "<br>" + match.Groups["display"].Value);
}

private static string DecodeXml(this string text)
{
return WebUtility.HtmlDecode(text);
}

private const string RefTagPattern = @"<(see|paramref) (name|cref|langword)=""([TPF]{1}:)?(?<display>.+?)"" ?/>";
private const string CodeTagPattern = @"<c>(?<display>.+?)</c>";
private const string MultilineCodeTagPattern = @"<code>(?<display>.+?)</code>";
private const string ParaTagPattern = @"<para>(?<display>.+?)</para>";

#if NET7_0_OR_GREATER
[GeneratedRegex(RefTagPattern)]
private static partial Regex RefTag();

[GeneratedRegex(CodeTagPattern)]
private static partial Regex CodeTag();

[GeneratedRegex(MultilineCodeTagPattern, RegexOptions.Singleline)]
private static partial Regex MultilineCodeTag();

[GeneratedRegex(ParaTagPattern, RegexOptions.Singleline)]
private static partial Regex ParaTag();
#else
private static readonly Regex _refTag = new(RefTagPattern);
private static readonly Regex _codeTag = new(CodeTagPattern);
private static readonly Regex _multilineCodeTag = new(MultilineCodeTagPattern, RegexOptions.Singleline);
private static readonly Regex _paraTag = new(ParaTagPattern, RegexOptions.Singleline);

private static Regex RefTag() => _refTag;
private static Regex CodeTag() => _codeTag;
private static Regex MultilineCodeTag() => _multilineCodeTag;
private static Regex ParaTag() => _paraTag;
#endif
}
}
}

0 comments on commit 75512e3

Please sign in to comment.