Skip to content

Commit 61c6457

Browse files
committed
Merge branch 'jc/maint-1.6.0-blank-at-eof' (early part) into jc/maint-blank-at-eof
* 'jc/maint-1.6.0-blank-at-eof' (early part): diff.c: emit_add_line() takes only the rest of the line diff.c: split emit_line() from the first char and the rest of the line
2 parents bb35fef + 018cff7 commit 61c6457

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

diff.c

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,31 @@ static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
273273
ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
274274
}
275275

276-
static void emit_line(FILE *file, const char *set, const char *reset, const char *line, int len)
276+
static void emit_line_0(FILE *file, const char *set, const char *reset,
277+
int first, const char *line, int len)
277278
{
278279
int has_trailing_newline, has_trailing_carriage_return;
280+
int nofirst;
279281

280-
has_trailing_newline = (len > 0 && line[len-1] == '\n');
281-
if (has_trailing_newline)
282-
len--;
283-
has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
284-
if (has_trailing_carriage_return)
285-
len--;
282+
if (len == 0) {
283+
has_trailing_newline = (first == '\n');
284+
has_trailing_carriage_return = (!has_trailing_newline &&
285+
(first == '\r'));
286+
nofirst = has_trailing_newline || has_trailing_carriage_return;
287+
} else {
288+
has_trailing_newline = (len > 0 && line[len-1] == '\n');
289+
if (has_trailing_newline)
290+
len--;
291+
has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
292+
if (has_trailing_carriage_return)
293+
len--;
294+
nofirst = 0;
295+
}
286296

287297
fputs(set, file);
298+
299+
if (!nofirst)
300+
fputc(first, file);
288301
fwrite(line, len, 1, file);
289302
fputs(reset, file);
290303
if (has_trailing_carriage_return)
@@ -293,6 +306,12 @@ static void emit_line(FILE *file, const char *set, const char *reset, const char
293306
fputc('\n', file);
294307
}
295308

309+
static void emit_line(FILE *file, const char *set, const char *reset,
310+
const char *line, int len)
311+
{
312+
emit_line_0(file, set, reset, line[0], line+1, len-1);
313+
}
314+
296315
static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
297316
{
298317
if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
@@ -301,23 +320,25 @@ static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line
301320
ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
302321
ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
303322
return 0;
304-
return ws_blank_line(line + 1, len - 1, ecbdata->ws_rule);
323+
return ws_blank_line(line, len, ecbdata->ws_rule);
305324
}
306325

307-
static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
326+
static void emit_add_line(const char *reset,
327+
struct emit_callback *ecbdata,
328+
const char *line, int len)
308329
{
309330
const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
310331
const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
311332

312333
if (!*ws)
313-
emit_line(ecbdata->file, set, reset, line, len);
334+
emit_line_0(ecbdata->file, set, reset, '+', line, len);
314335
else if (new_blank_line_at_eof(ecbdata, line, len))
315336
/* Blank line at EOF - paint '+' as well */
316-
emit_line(ecbdata->file, ws, reset, line, len);
337+
emit_line_0(ecbdata->file, ws, reset, '+', line, len);
317338
else {
318339
/* Emit just the prefix, then the rest. */
319-
emit_line(ecbdata->file, set, reset, line, 1);
320-
ws_check_emit(line + 1, len - 1, ecbdata->ws_rule,
340+
emit_line_0(ecbdata->file, set, reset, '+', "", 0);
341+
ws_check_emit(line, len, ecbdata->ws_rule,
321342
ecbdata->file, set, reset, ws);
322343
}
323344
}
@@ -770,7 +791,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
770791
emit_line(ecbdata->file, color, reset, line, len);
771792
} else {
772793
ecbdata->lno_in_postimage++;
773-
emit_add_line(reset, ecbdata, line, len);
794+
emit_add_line(reset, ecbdata, line + 1, len - 1);
774795
}
775796
}
776797

0 commit comments

Comments
 (0)