Skip to content

Commit 89b8461

Browse files
authored
changing expander regex (#11210)
* changing expander regex to use code generated version instead of compiled one where applicable * review update
1 parent c03b15e commit 89b8461

1 file changed

Lines changed: 53 additions & 17 deletions

File tree

src/Build/Evaluation/Expander.cs

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ internal enum ExpanderOptions
129129
/// </remarks>
130130
/// <typeparam name="P">Type of the properties used.</typeparam>
131131
/// <typeparam name="I">Type of the items used.</typeparam>
132-
internal class Expander<P, I>
132+
internal partial class Expander<P, I>
133133
where P : class, IProperty
134134
where I : class, IItem
135135
{
@@ -952,7 +952,7 @@ internal static string ExpandMetadataLeaveEscaped(string expression, IMetadataTa
952952
// if there are no item vectors in the string
953953
// run a simpler Regex to find item metadata references
954954
MetadataMatchEvaluator matchEvaluator = new MetadataMatchEvaluator(metadata, options, elementLocation, loggingContext);
955-
result = RegularExpressions.ItemMetadataPattern.Value.Replace(expression, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));
955+
result = RegularExpressions.ItemMetadataRegex.Replace(expression, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));
956956
}
957957
else
958958
{
@@ -983,15 +983,15 @@ internal static string ExpandMetadataLeaveEscaped(string expression, IMetadataTa
983983
// Extract the part of the expression that appears before the item vector expression
984984
// e.g. the ABC in ABC@(foo->'%(FullPath)')
985985
string subExpressionToReplaceIn = expression.Substring(start, itemVectorExpressions[n].Index - start);
986-
string replacementResult = RegularExpressions.NonTransformItemMetadataPattern.Value.Replace(subExpressionToReplaceIn, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));
986+
string replacementResult = RegularExpressions.NonTransformItemMetadataRegex.Replace(subExpressionToReplaceIn, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));
987987

988988
// Append the metadata replacement
989989
finalResultBuilder.Append(replacementResult);
990990

991991
// Expand any metadata that appears in the item vector expression's separator
992992
if (itemVectorExpressions[n].Separator != null)
993993
{
994-
vectorExpression = RegularExpressions.NonTransformItemMetadataPattern.Value.Replace(itemVectorExpressions[n].Value, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata), -1, itemVectorExpressions[n].SeparatorStart);
994+
vectorExpression = RegularExpressions.NonTransformItemMetadataRegex.Replace(itemVectorExpressions[n].Value, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata), -1, itemVectorExpressions[n].SeparatorStart);
995995
}
996996

997997
// Append the item vector expression as is
@@ -1008,7 +1008,7 @@ internal static string ExpandMetadataLeaveEscaped(string expression, IMetadataTa
10081008
if (start < expression.Length)
10091009
{
10101010
string subExpressionToReplaceIn = expression.Substring(start);
1011-
string replacementResult = RegularExpressions.NonTransformItemMetadataPattern.Value.Replace(subExpressionToReplaceIn, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));
1011+
string replacementResult = RegularExpressions.NonTransformItemMetadataRegex.Replace(subExpressionToReplaceIn, new MatchEvaluator(matchEvaluator.ExpandSingleMetadata));
10121012

10131013
finalResultBuilder.Append(replacementResult);
10141014
}
@@ -2719,7 +2719,7 @@ internal static IEnumerable<KeyValuePair<string, S>> ExpandQuotedExpressionFunct
27192719
{
27202720
matchEvaluator = new MetadataMatchEvaluator(item.Key, item.Value, elementLocation);
27212721

2722-
include = RegularExpressions.ItemMetadataPattern.Value.Replace(arguments[0], matchEvaluator.GetMetadataValueFromMatch);
2722+
include = RegularExpressions.ItemMetadataRegex.Replace(arguments[0], matchEvaluator.GetMetadataValueFromMatch);
27232723
}
27242724

27252725
// Include may be empty. Historically we have created items with empty include
@@ -3086,24 +3086,43 @@ internal string GetMetadataValueFromMatch(Match match)
30863086
/// Regular expressions used by the expander.
30873087
/// The expander currently uses regular expressions rather than a parser to do its work.
30883088
/// </summary>
3089-
private static class RegularExpressions
3089+
private static partial class RegularExpressions
30903090
{
30913091
/**************************************************************************************************************************
30923092
* WARNING: The regular expressions below MUST be kept in sync with the expressions in the ProjectWriter class -- if the
30933093
* description of an item vector changes, the expressions must be updated in both places.
30943094
*************************************************************************************************************************/
30953095

3096+
3097+
3098+
#if NET7_0_OR_GREATER
3099+
[GeneratedRegex(ItemMetadataSpecification, RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture)]
3100+
internal static partial Regex ItemMetadataPattern();
3101+
#else
30963102
/// <summary>
30973103
/// Regular expression used to match item metadata references embedded in strings.
30983104
/// For example, %(Compile.DependsOn) or %(DependsOn).
30993105
/// </summary>
31003106
internal static readonly Lazy<Regex> ItemMetadataPattern = new Lazy<Regex>(
31013107
() => new Regex(ItemMetadataSpecification,
31023108
RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptions.Compiled));
3109+
#endif
31033110

3104-
/// <summary>
3105-
/// Name of the group matching the "name" of a metadatum.
3106-
/// </summary>
3111+
internal static Regex ItemMetadataRegex
3112+
{
3113+
get
3114+
{
3115+
#if NET7_0_OR_GREATER
3116+
return ItemMetadataPattern();
3117+
#else
3118+
return ItemMetadataPattern.Value;
3119+
#endif
3120+
}
3121+
}
3122+
3123+
/// <summary>
3124+
/// Name of the group matching the "name" of a metadatum.
3125+
/// </summary>
31073126
internal const string NameGroup = "NAME";
31083127

31093128
/// <summary>
@@ -3116,18 +3135,35 @@ private static class RegularExpressions
31163135
/// </summary>
31173136
internal const string ItemTypeGroup = "ITEM_TYPE";
31183137

3138+
internal const string NonTransformItemMetadataSpecification = @"((?<=" + ItemVectorWithTransformLHS + @")" + ItemMetadataSpecification + @"(?!" +
3139+
ItemVectorWithTransformRHS + @")) | ((?<!" + ItemVectorWithTransformLHS + @")" +
3140+
ItemMetadataSpecification + @"(?=" + ItemVectorWithTransformRHS + @")) | ((?<!" +
3141+
ItemVectorWithTransformLHS + @")" + ItemMetadataSpecification + @"(?!" +
3142+
ItemVectorWithTransformRHS + @"))";
3143+
3144+
#if NET7_0_OR_GREATER
3145+
[GeneratedRegex(NonTransformItemMetadataSpecification, RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture)]
3146+
internal static partial Regex NonTransformItemMetadataPattern();
3147+
#else
31193148
/// <summary>
31203149
/// regular expression used to match item metadata references outside of item vector transforms.
31213150
/// </summary>
31223151
/// <remarks>PERF WARNING: this Regex is complex and tends to run slowly.</remarks>
31233152
internal static readonly Lazy<Regex> NonTransformItemMetadataPattern = new Lazy<Regex>(
3124-
() => new Regex(
3125-
@"((?<=" + ItemVectorWithTransformLHS + @")" + ItemMetadataSpecification + @"(?!" +
3126-
ItemVectorWithTransformRHS + @")) | ((?<!" + ItemVectorWithTransformLHS + @")" +
3127-
ItemMetadataSpecification + @"(?=" + ItemVectorWithTransformRHS + @")) | ((?<!" +
3128-
ItemVectorWithTransformLHS + @")" + ItemMetadataSpecification + @"(?!" +
3129-
ItemVectorWithTransformRHS + @"))",
3130-
RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptions.Compiled));
3153+
() => new Regex(NonTransformItemMetadataSpecification,
3154+
RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptions.Compiled));
3155+
#endif
3156+
internal static Regex NonTransformItemMetadataRegex
3157+
{
3158+
get
3159+
{
3160+
#if NET7_0_OR_GREATER
3161+
return NonTransformItemMetadataPattern();
3162+
#else
3163+
return NonTransformItemMetadataPattern.Value;
3164+
#endif
3165+
}
3166+
}
31313167

31323168
/// <summary>
31333169
/// Complete description of an item metadata reference, including the optional qualifying item type.

0 commit comments

Comments
 (0)