Skip to content

Commit

Permalink
cache regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
rkm committed Aug 24, 2023
1 parent 03b435c commit 7dbe860
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions IsIdentifiable/Rules/PartPatternFilterRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public override bool CaseSensitive

public string WordAfter { get; set; }

private Regex? _wordBeforeRegex;
private Regex? _wordAfterRegex;

// TODO(rkm 2023-07-25) Shouldn't be needed when IfPattern is readonly
private void RebuildPartRegex()
{
Expand All @@ -65,16 +68,16 @@ public bool Covers(FailurePart failurePart, string problemValue)
if (!string.IsNullOrWhiteSpace(WordBefore))
{
var problemValueUpToOffset = problemValue[..(failurePart.Offset + failurePart.Word.Length)];
var wordBeforeRegex = new Regex(@$"\b{WordBefore}(\s|-)+{IfPartPattern.TrimStart('^')}", (CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase));
matchesBefore = wordBeforeRegex.Matches(problemValueUpToOffset).Any();
_wordBeforeRegex ??= new Regex(@$"\b{WordBefore}(\s|-)+{IfPartPattern.TrimStart('^')}", (CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase) | RegexOptions.Compiled);
matchesBefore = _wordBeforeRegex.Matches(problemValueUpToOffset).Any();
}

bool matchesAfter = false;
if (!string.IsNullOrWhiteSpace(WordAfter))
{
var problemValueFromOffset = problemValue[failurePart.Offset..];
var wordAfterRegex = new Regex(@$"{IfPartPattern.TrimEnd('$')}(\s|-)+{WordAfter}\b", (CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase));
matchesAfter = wordAfterRegex.Matches(problemValueFromOffset).Any();
_wordAfterRegex ??= new Regex(@$"{IfPartPattern.TrimEnd('$')}(\s|-)+{WordAfter}\b", (CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase) | RegexOptions.Compiled);
matchesAfter = _wordAfterRegex.Matches(problemValueFromOffset).Any();
}

if (
Expand Down

0 comments on commit 7dbe860

Please sign in to comment.