Skip to content

Commit e663313

Browse files
Use source generated regexes where appropriate (#2927)
1 parent c443a3a commit e663313

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

lib/PuppeteerSharp/Cdp/CdpHttpResponse.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
namespace PuppeteerSharp.Cdp;
99

1010
/// <inheritdoc/>
11-
public class CdpHttpResponse : Response<CdpHttpRequest>
11+
public partial class CdpHttpResponse : Response<CdpHttpRequest>
1212
{
13+
#if NETSTANDARD2_0
1314
private static readonly Regex _extraInfoLines = new(@"[^ ]* [^ ]* (?<text>.*)", RegexOptions.Multiline);
15+
#endif
1416
private readonly CDPSession _client;
1517
private readonly bool _fromDiskCache;
1618
private byte[] _buffer;
@@ -82,6 +84,13 @@ public override async ValueTask<byte[]> BufferAsync()
8284
return _buffer;
8385
}
8486

87+
#if NET8_0_OR_GREATER
88+
[GeneratedRegex(@"[^ ]* [^ ]* (?<text>.*)", RegexOptions.Multiline)]
89+
private static partial Regex GetExtraInfoLines();
90+
#else
91+
private static Regex GetExtraInfoLines() => _extraInfoLines;
92+
#endif
93+
8594
private string ParseStatusTextFromExtraInfo(ResponseReceivedExtraInfoResponse extraInfo)
8695
{
8796
if (extraInfo == null || extraInfo.HeadersText == null)
@@ -97,7 +106,7 @@ private string ParseStatusTextFromExtraInfo(ResponseReceivedExtraInfoResponse ex
97106

98107
var firstLine = lines[0];
99108

100-
var match = _extraInfoLines.Match(firstLine);
109+
var match = GetExtraInfoLines().Match(firstLine);
101110
if (!match.Success)
102111
{
103112
return null;

lib/PuppeteerSharp/ExecutionContext.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
namespace PuppeteerSharp
1313
{
1414
/// <inheritdoc cref="IExecutionContext"/>
15-
public sealed class ExecutionContext : IExecutionContext, IDisposable, IAsyncDisposable
15+
public sealed partial class ExecutionContext : IExecutionContext, IDisposable, IAsyncDisposable
1616
{
1717
internal const string EvaluationScriptUrl = "__puppeteer_evaluation_script__";
1818
private const string EvaluationScriptSuffix = $"//# sourceURL={EvaluationScriptUrl}";
1919

20-
private static readonly Regex _sourceUrlRegex = new(@"^[\040\t]*\/\/[@#] sourceURL=\s*\S*?\s*$", RegexOptions.Multiline);
20+
#if NETSTANDARD2_0
21+
private static readonly Regex _sourceUrlRegex =
22+
new(@"^[\040\t]*\/\/[@#] sourceURL=\s*\S*?\s*$", RegexOptions.Multiline);
23+
#endif
24+
2125
private readonly TaskQueue _puppeteerUtilQueue = new();
2226
private IJSHandle _puppeteerUtil;
2327

@@ -128,6 +132,13 @@ internal IJSHandle CreateJSHandle(RemoteObject remoteObject)
128132
? new CdpElementHandle(World, remoteObject)
129133
: new CdpJSHandle(World, remoteObject);
130134

135+
#if NET8_0_OR_GREATER
136+
[GeneratedRegex(@"^[\040\t]*\/\/[@#] sourceURL=\s*\S*?\s*$", RegexOptions.Multiline)]
137+
private static partial Regex GetSourceUrlRegex();
138+
#else
139+
private static Regex GetSourceUrlRegex() => _sourceUrlRegex;
140+
#endif
141+
131142
private static string GetExceptionMessage(EvaluateExceptionResponseDetails exceptionDetails)
132143
{
133144
if (exceptionDetails.Exception != null)
@@ -179,7 +190,7 @@ private async Task<T> RemoteObjectTaskToObject<T>(Task<RemoteObject> remote)
179190
private Task<RemoteObject> EvaluateExpressionInternalAsync(bool returnByValue, string script)
180191
=> ExecuteEvaluationAsync("Runtime.evaluate", new Dictionary<string, object>
181192
{
182-
["expression"] = _sourceUrlRegex.IsMatch(script) ? script : $"{script}\n{EvaluationScriptSuffix}",
193+
["expression"] = GetSourceUrlRegex().IsMatch(script) ? script : $"{script}\n{EvaluationScriptSuffix}",
183194
["contextId"] = ContextId,
184195
["returnByValue"] = returnByValue,
185196
["awaitPromise"] = true,

lib/PuppeteerSharp/QueryHandlers/AriaQueryHandler.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
namespace PuppeteerSharp.QueryHandlers
1010
{
11-
internal class AriaQueryHandler : QueryHandler
11+
internal partial class AriaQueryHandler : QueryHandler
1212
{
13-
private static readonly Regex _ariaSelectorAttributeRegEx = new(
13+
#if NETSTANDARD2_0
14+
private static readonly Regex _ariaSelectorAttributeRegex = new(
1415
"""\[\s*(?<attribute>\w+)\s*=\s*(?<quote>"|')(?<value>\\.|.*?(?=\k<quote>))\k<quote>\s*\]""",
1516
RegexOptions.Compiled);
17+
#endif
1618

1719
private static readonly string[] _nonElementNodeRoles = { "StaticText", "InlineTextBox" };
1820

@@ -77,7 +79,7 @@ private static AriaQueryOption ParseAriaSelector(string selector)
7779
{
7880
var knownAriaAttributes = new[] { "name", "role" };
7981
AriaQueryOption queryOptions = new();
80-
var defaultName = _ariaSelectorAttributeRegEx.Replace(selector, match =>
82+
var defaultName = GetAriaSelectorAttributeRegex().Replace(selector, match =>
8183
{
8284
var attribute = match.Groups["attribute"].Value;
8385
if (!knownAriaAttributes.Contains(attribute))
@@ -104,5 +106,12 @@ private static AriaQueryOption ParseAriaSelector(string selector)
104106

105107
return queryOptions;
106108
}
109+
110+
#if NET8_0_OR_GREATER
111+
[GeneratedRegex("""\[\s*(?<attribute>\w+)\s*=\s*(?<quote>"|')(?<value>\\.|.*?(?=\k<quote>))\k<quote>\s*\]""")]
112+
private static partial Regex GetAriaSelectorAttributeRegex();
113+
#else
114+
private static Regex GetAriaSelectorAttributeRegex() => _ariaSelectorAttributeRegex;
115+
#endif
107116
}
108117
}

0 commit comments

Comments
 (0)