grep: stop modifying buffer in show_line()

When showing lines via grep (or looking for funcnames), we call show_line() on a multi-line buffer. It finds the end of line and marks it with a NUL. However, we don't need to do so, as the resulting line is only used along with its "eol" marker: - we pass both to next_match(), which takes care to look at only the bytes we specified - we pass the line to output_color() without its matching eol marker. However, we do use the "match" struct we got from next_match() to tell it how many bytes to look at (which can never exceed the string we passed it). So we can stop setting and restoring this NUL marker. That makes the code simpler, and will allow us to take a const buffer in a future patch. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 20, 2021 at 23:48 UTC 995e525b1729ada354e443f16e1c0fad59df25a8
1 file changed -3
grep.c
-3
@@ -1239,7 +1239,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1239 if (opt->color || opt->only_matching) {
1240 regmatch_t match;
1241 enum grep_context ctx = GREP_CONTEXT_BODY;
1242 - int ch = *eol;
1242 int eflags = 0;
1243
1244 if (opt->color) {
@@ -1254,7 +1253,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1253 else if (sign == '=')
1254 line_color = opt->colors[GREP_COLOR_FUNCTION];
1255 }
1257 - *eol = '\0';
1256 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1257 if (match.rm_so == match.rm_eo)
1258 break;
@@ -1272,7 +1270,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
1270 rest -= match.rm_eo;
1271 eflags = REG_NOTBOL;
1272 }
1275 - *eol = ch;
1273 }
1274 if (!opt->only_matching) {
1275 output_color(opt, bol, rest, line_color);