Skip to content

Commit

Permalink
Fix zero length matches
Browse files Browse the repository at this point in the history
  • Loading branch information
gusarov committed Oct 3, 2020
1 parent 7476942 commit 1d0fc8a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Grepl.Tests/Commands/BasicCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -627,5 +627,24 @@ public void Should_30_show_only_file_name_replace_save()

}

[TestMethod]
public void Should_10_exclude_zero_length_match()
{
File.WriteAllText("test.txt", "abc\n");

var r = GreplEntry(".*", "-r");
Assert.AreEqual(0, r.Code);

var raw = r.Output;
var exp = @"
test.txt
abc
";

CompareDetails(exp, raw);

Assert.AreEqual(exp, raw);

}
}
}
4 changes: 4 additions & 0 deletions Grepl/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ void Process(string file, bool printFileName, Dictionary<ColoredMessage, Capture
// var currentlnStart = -2; // initially unknown
foreach (Match match in matches)
{
if (match.Length == 0)
{
continue; // ignore zero-length matches and prevent index out of range when index >= length (zero length match at the end of a string)
}
// find the start of the line
var lineStart = body.LastIndexOf('\n', match.Index);
lineStart++;
Expand Down

0 comments on commit 1d0fc8a

Please sign in to comment.