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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Nov 18, 2017 at 19:07 UTC 6653a01bf2dd1e2aabbcf1f83269bd75737acc94
1 file changed +8 -6
grep.c
+8 -6
@@ -1479,20 +1479,21 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1479 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1480 char *bol, char *end, unsigned lno)
1481 {
1482 - unsigned cur = lno, from = 1, funcname_lno = 0;
1482 + unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
1483 int funcname_needed = !!opt->funcname;
1484
1485 - if (opt->funcbody && !match_funcname(opt, gs, bol, end))
1486 - funcname_needed = 2;
1487 -
1485 if (opt->pre_context < lno)
1486 from = lno - opt->pre_context;
1487 if (from <= opt->last_shown)
1488 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 + }
1494
1495 /* Rewind. */
1494 - while (bol > gs->buf &&
1495 - cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
1496 + while (bol > gs->buf && cur > from) {
1497 char *eol = --bol;
1498
1499 while (bol > gs->buf && bol[-1] != '\n')
@@ -1501,6 +1502,7 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1502 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
1503 funcname_lno = cur;
1504 funcname_needed = 0;
1505 + from = orig_from;
1506 }
1507 }
1508