diff.c: rewrite emit_line_0 more understandably
Rewrite emit_line_0 to have fewer (nested) conditions. The change in 'emit_line' makes sure that 'first' is never user data, but always under our control, a sign or special character in the beginning of the line (or 0, in which case we ignore it). So from now on, let's pass only a diff marker or 0 as the 'first' character of the line. 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
444106791ee2c4c4575db439ef74d5db5ede6a27
1 file changed
+40
-33
diff.c
+40
-33
@@ -626,43 +626,50 @@ static void emit_line_0(struct diff_options *o,
626
int first, const char *line, int len)
627
{
628
int has_trailing_newline, has_trailing_carriage_return;
629
- int nofirst;
629
+ int needs_reset = 0; /* at the end of the line */
630
FILE *file = o->file;
631
632
fputs(diff_line_prefix(o), file);
633
634
- if (len == 0) {
635
- has_trailing_newline = (first == '\n');
636
- has_trailing_carriage_return = (!has_trailing_newline &&
637
- (first == '\r'));
638
- nofirst = has_trailing_newline || has_trailing_carriage_return;
639
- } else {
640
- has_trailing_newline = (len > 0 && line[len-1] == '\n');
641
- if (has_trailing_newline)
642
- len--;
643
- has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
644
- if (has_trailing_carriage_return)
645
- len--;
646
- nofirst = 0;
647
- }
648
-
649
- if (len || !nofirst) {
650
- if (reverse && want_color(o->use_color))
651
- fputs(GIT_COLOR_REVERSE, file);
652
- if (set_sign)
653
- fputs(set_sign, file);
654
- if (first && !nofirst)
655
- fputc(first, file);
656
- if (len) {
657
- if (set && set != set_sign) {
658
- if (set_sign)
659
- fputs(reset, file);
660
- fputs(set, file);
661
- }
662
- fwrite(line, len, 1, file);
663
- }
664
- fputs(reset, file);
634
+ has_trailing_newline = (len > 0 && line[len-1] == '\n');
635
+ if (has_trailing_newline)
636
+ len--;
637
+
638
+ has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
639
+ if (has_trailing_carriage_return)
640
+ len--;
641
+
642
+ if (!len && !first)
643
+ goto end_of_line;
644
+
645
+ if (reverse && want_color(o->use_color)) {
646
+ fputs(GIT_COLOR_REVERSE, file);
647
+ needs_reset = 1;
648
+ }
649
+
650
+ if (set_sign) {
651
+ fputs(set_sign, file);
652
+ needs_reset = 1;
653
}
654
+
655
+ if (first)
656
+ fputc(first, file);
657
+
658
+ if (!len)
659
+ goto end_of_line;
660
+
661
+ if (set) {
662
+ if (set_sign && set != set_sign)
663
+ fputs(reset, file);
664
+ fputs(set, file);
665
+ needs_reset = 1;
666
+ }
667
+ fwrite(line, len, 1, file);
668
+ needs_reset = 1; /* 'line' may contain color codes. */
669
+
670
+end_of_line:
671
+ if (needs_reset)
672
+ fputs(reset, file);
673
if (has_trailing_carriage_return)
674
fputc('\r', file);
675
if (has_trailing_newline)
@@ -672,7 +679,7 @@ static void emit_line_0(struct diff_options *o,
679
static void emit_line(struct diff_options *o, const char *set, const char *reset,
680
const char *line, int len)
681
{
675
- emit_line_0(o, set, NULL, 0, reset, line[0], line+1, len-1);
682
+ emit_line_0(o, set, NULL, 0, reset, 0, line, len);
683
}
684
685
enum diff_symbol {