Skip to content

Commit

Permalink
fixup offset issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rkm committed Aug 8, 2023
1 parent 8ceef12 commit a91c0b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
11 changes: 9 additions & 2 deletions IsIdentifiable/Reporting/Reports/FailureStoreReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,15 @@ public static IEnumerable<Failure> Deserialize(IFileInfo oldFile, Action<int> lo
if (!string.IsNullOrWhiteSpace(partRule.IfColumn) && !string.Equals(partRule.IfColumn, problemField, StringComparison.InvariantCultureIgnoreCase))
continue;

foreach (var part in parts.Where(x => partRule.Covers(x, problemValue)))
toRemove.Add(part);
foreach (var part in parts)
{
var origOffset = part.Offset;
if (partRule.Covers(part, problemValue))
{
part.Offset = origOffset;
toRemove.Add(part);
}
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.
parts = parts.Except(toRemove);
/* TEMP */
Expand Down
33 changes: 27 additions & 6 deletions IsIdentifiable/Rules/RegexRule_PartTemp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,38 @@ private void RebuildPartRegex()
IfPartPatternRegex = new Regex(_ifPartPatternString, (CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase) | RegexOptions.Compiled);
}

private static void FixupOffset(FailurePart failurePart, string problemValue)
{
if (problemValue.Substring(failurePart.Offset, failurePart.Word.Length) == failurePart.Word)
return;

// Try looking ahead first, then back
var origOffset = failurePart.Offset;
try
{
while (problemValue.Substring(failurePart.Offset, failurePart.Word.Length) != failurePart.Word)
failurePart.Offset++;
}
catch (ArgumentOutOfRangeException)
{
failurePart.Offset = origOffset;
while (problemValue.Substring(failurePart.Offset, failurePart.Word.Length) != failurePart.Word)
failurePart.Offset--;
}
}

public bool Covers(FailurePart failurePart, string problemValue)
{
if (As != failurePart.Classification)
return false;

// Fixes any offsets that have been mangled by file endings etc.
FixupOffset(failurePart, problemValue);

bool matchesBefore = false;
if (!string.IsNullOrWhiteSpace(WordBefore))
{
var problemValueUpToOffset = problemValue[..(failurePart.Offset + failurePart.Word.Length)];
if (!problemValueUpToOffset.EndsWith(failurePart.Word))
throw new Exception("Invlaid data: actual word and word at offset did not match");

var wordBeforeRegex = new Regex($"\\b{WordBefore}\\s+{IfPartPattern.TrimStart('^')}", (CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase));
matchesBefore = wordBeforeRegex.Matches(problemValueUpToOffset).Any();
}
Expand All @@ -77,9 +97,6 @@ public bool Covers(FailurePart failurePart, string problemValue)
if (!string.IsNullOrWhiteSpace(WordAfter))
{
var problemValueFromOffset = problemValue[failurePart.Offset..];
if (!problemValueFromOffset.StartsWith(failurePart.Word))
throw new Exception("Invlaid data: actual word and word at offset did not match");

var wordAfterRegex = new Regex($"{IfPartPattern.TrimEnd('$')}\\s+{WordAfter}\\b", (CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase));
matchesAfter = wordAfterRegex.Matches(problemValueFromOffset).Any();
}
Expand All @@ -92,6 +109,10 @@ public bool Covers(FailurePart failurePart, string problemValue)
{
return true;
}
else if (!string.IsNullOrWhiteSpace(WordBefore) || !string.IsNullOrWhiteSpace(WordAfter))
{
return false;
}

return IfPartPatternRegex.Matches(failurePart.Word).Any();
}
Expand Down

0 comments on commit a91c0b9

Please sign in to comment.