11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license.
33
4+ using System ;
45using System . Collections . Generic ;
56using System . Linq ;
67using System . Text . RegularExpressions ;
@@ -10,10 +11,17 @@ namespace Microsoft.OpenApi
1011 /// <summary>
1112 /// String literal with embedded expressions
1213 /// </summary>
13- public class CompositeExpression : RuntimeExpression
14+ public partial class CompositeExpression : RuntimeExpression
1415 {
1516 private readonly string template ;
16- private readonly Regex expressionPattern = new ( @"{(?<exp>\$[^}]*)" ) ;
17+ private const string ExpressionPattern = @"{(?<exp>\$[^}]*)" ;
18+
19+ #if NET8_0_OR_GREATER
20+ [ GeneratedRegex ( ExpressionPattern , RegexOptions . None , matchTimeoutMilliseconds : 100 ) ]
21+ private static partial Regex ExpressionRegex ( ) ;
22+ #else
23+ private static readonly Regex ExpressionRegex = new ( ExpressionPattern , RegexOptions . None , TimeSpan . FromMilliseconds ( 100 ) ) ;
24+ #endif
1725
1826 /// <summary>
1927 /// Expressions embedded into string literal
@@ -24,12 +32,17 @@ public class CompositeExpression : RuntimeExpression
2432 /// Create a composite expression from a string literal with an embedded expression
2533 /// </summary>
2634 /// <param name="expression"></param>
35+ /// <exception cref="RegexMatchTimeoutException">Extracting embedded expressions exceeds the regex match timeout.</exception>
2736 public CompositeExpression ( string expression )
2837 {
2938 template = expression ;
3039
3140 // Extract subexpressions and convert to RuntimeExpressions
32- var matches = expressionPattern . Matches ( expression ) ;
41+ #if NET8_0_OR_GREATER
42+ var matches = ExpressionRegex ( ) . Matches ( expression ) ;
43+ #else
44+ var matches = ExpressionRegex . Matches ( expression ) ;
45+ #endif
3346
3447 foreach ( var item in matches . Cast < Match > ( ) )
3548 {
0 commit comments