diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsTextHelper.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsTextHelper.cs index 1d8f8c3825..016ae67f0f 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsTextHelper.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsTextHelper.cs @@ -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}:)?(?.+?)"" ?/>"); - private static Regex CodeTagPattern = new Regex(@"(?.+?)"); - private static Regex MultilineCodeTagPattern = new Regex(@"(?.+?)", RegexOptions.Singleline); - private static Regex ParaTagPattern = new Regex(@"(?.+?)", RegexOptions.Singleline); - public static string Humanize(string text) { if (text == null) @@ -89,22 +84,22 @@ 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) => "
" + match.Groups["display"].Value); + return ParaTag().Replace(text, (match) => "
" + match.Groups["display"].Value); } private static string DecodeXml(this string text) @@ -112,5 +107,33 @@ private static string DecodeXml(this string text) return WebUtility.HtmlDecode(text); } + private const string RefTagPattern = @"<(see|paramref) (name|cref|langword)=""([TPF]{1}:)?(?.+?)"" ?/>"; + private const string CodeTagPattern = @"(?.+?)"; + private const string MultilineCodeTagPattern = @"(?.+?)"; + private const string ParaTagPattern = @"(?.+?)"; + +#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 } -} \ No newline at end of file +}