diff: use emit_line_0 once per line

All lines that use emit_line_0 multiple times per line, are combined into a single call to emit_line_0, making use of the 'set' argument. We gain a little efficiency here, as we can omit emission of color and accompanying reset if 'len == 0'. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Aug 13, 2018 at 18:41 UTC 29ef759d7ca039590240890a604be8308b30a069
2 files changed +9 -9
diff.c
+8 -8
@@ -656,12 +656,14 @@ static void emit_line_0(struct diff_options *o,
656 fputs(set_sign, file);
657 if (first && !nofirst)
658 fputc(first, file);
659 - if (set && set != set_sign) {
660 - if (set_sign)
661 - fputs(reset, file);
662 - fputs(set, file);
659 + if (len) {
660 + if (set && set != set_sign) {
661 + if (set_sign)
662 + fputs(reset, file);
663 + fputs(set, file);
664 + }
665 + fwrite(line, len, 1, file);
666 }
664 - fwrite(line, len, 1, file);
667 fputs(reset, file);
668 }
669 if (has_trailing_carriage_return)
@@ -1207,9 +1209,7 @@ static void emit_line_ws_markup(struct diff_options *o,
1209 if (!ws && !set_sign)
1210 emit_line_0(o, set, NULL, 0, reset, sign, line, len);
1211 else if (!ws) {
1210 - /* Emit just the prefix, then the rest. */
1211 - emit_line_0(o, set_sign, NULL, !!set_sign, reset, sign, "", 0);
1212 - emit_line_0(o, set, NULL, 0, reset, 0, line, len);
1212 + emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len);
1213 } else if (blank_at_eof)
1214 /* Blank line at EOF - paint '+' as well */
1215 emit_line_0(o, ws, NULL, 0, reset, sign, line, len);
t/t3206-range-diff.sh
+1 -1
@@ -151,7 +151,7 @@ test_expect_success 'dual-coloring' '
151 : s/4/A/<RESET>
152 : <RESET>
153 : <REVERSE><GREEN>+<RESET><BOLD> Also a silly comment here!<RESET>
154 - : <REVERSE><GREEN>+<RESET><BOLD><RESET>
154 + : <REVERSE><GREEN>+<RESET>
155 : diff --git a/file b/file<RESET>
156 : <RED> --- a/file<RESET>
157 : <GREEN> +++ b/file<RESET>