Context Lines Display Discrepancy
A Windows File Searching Utility (grep)
Brought to you by:
jackslade,
theodoreward
There is a slight error with the number of preceding context lines shown and with the inclusion of the blank line between matches:
Grep.cs
SearchFileContents(FileInfo file)
// this line
if (match.Matches.Count > 0 && _lastHit < -_maxContextLines)
// should be
if (match.Matches.Count > 0 && _lastHit <= -_maxContextLines)
// and this line
if(_lastHit >= -_maxContextLines)
// should be
if (_lastHit > -_maxContextLines)
This gives the following:
// Display context lines if applicable.
if (SearchSpec.ContextLines > 0 && _lastHit <= 0)
{
if (match.Matches.Count > 0 && _lastHit <= -_maxContextLines)
{
// Insert a blank space before the context lines.
var matchLine = new MatchResultLine() { Line = string.Empty, LineNumber = -1 };
match.Matches.Add(matchLine);
int _pos = match.Matches.Count - 1;
if (DoesPassHitCountCheck(match))
{
OnLineHit(match, _pos);
}
}
// Display preceding n context lines before the hit.
int tempContextLines = SearchSpec.ContextLines;
// But only output the context lines which are not part of the previous context
if (_lastHit > -_maxContextLines)
{
tempContextLines = -_lastHit;
}
This text file allows this to be tested by searching for "aa" and checking the context lines and spaces displayed for various values of "Context Lines".
Anonymous
Thanks, will take a look at verifying and fixing this with your changes.