Skip to content

Commit 6653a01

Browse files
rscharfegitster
authored andcommitted
grep: update boundary variable for pre-context
Function context can be bigger than -A/-B/-C context. To find the beginning of the combined context we search backwards. Currently we check at each loop iteration what we're looking for and determine the effective upper boundary based on that. Simplify this a bit by setting the variable "from" to the lowest unshown line number up front if we're looking for a function line and set it back to the required -B/-C context line number when we find one. This prepares the ground for the next patch; no functional change intended. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 76e650d commit 6653a01

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

grep.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,20 +1479,21 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
14791479
static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
14801480
char *bol, char *end, unsigned lno)
14811481
{
1482-
unsigned cur = lno, from = 1, funcname_lno = 0;
1482+
unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
14831483
int funcname_needed = !!opt->funcname;
14841484

1485-
if (opt->funcbody && !match_funcname(opt, gs, bol, end))
1486-
funcname_needed = 2;
1487-
14881485
if (opt->pre_context < lno)
14891486
from = lno - opt->pre_context;
14901487
if (from <= opt->last_shown)
14911488
from = opt->last_shown + 1;
1489+
orig_from = from;
1490+
if (opt->funcbody && !match_funcname(opt, gs, bol, end)) {
1491+
funcname_needed = 1;
1492+
from = opt->last_shown + 1;
1493+
}
14921494

14931495
/* Rewind. */
1494-
while (bol > gs->buf &&
1495-
cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
1496+
while (bol > gs->buf && cur > from) {
14961497
char *eol = --bol;
14971498

14981499
while (bol > gs->buf && bol[-1] != '\n')
@@ -1501,6 +1502,7 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
15011502
if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
15021503
funcname_lno = cur;
15031504
funcname_needed = 0;
1505+
from = orig_from;
15041506
}
15051507
}
15061508

0 commit comments

Comments
 (0)