Skip to content

Commit 6e336a5

Browse files
tboegigitster
authored andcommitted
convert.c: simplify text_stat
Simplify the statistics: lonecr counts the CR which is not followed by a LF, lonelf counts the LF which is not preceded by a CR, crlf counts CRLF combinations. This simplifies the evaluation of the statistics. Signed-off-by: Torsten Bögershausen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent df747b8 commit 6e336a5

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

convert.c

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum crlf_action {
3131

3232
struct text_stat {
3333
/* NUL, CR, LF and CRLF counts */
34-
unsigned nul, cr, lf, crlf;
34+
unsigned nul, lonecr, lonelf, crlf;
3535

3636
/* These are just approximations! */
3737
unsigned printable, nonprintable;
@@ -46,13 +46,15 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat *
4646
for (i = 0; i < size; i++) {
4747
unsigned char c = buf[i];
4848
if (c == '\r') {
49-
stats->cr++;
50-
if (i+1 < size && buf[i+1] == '\n')
49+
if (i+1 < size && buf[i+1] == '\n') {
5150
stats->crlf++;
51+
i++;
52+
} else
53+
stats->lonecr++;
5254
continue;
5355
}
5456
if (c == '\n') {
55-
stats->lf++;
57+
stats->lonelf++;
5658
continue;
5759
}
5860
if (c == 127)
@@ -86,7 +88,7 @@ static void gather_stats(const char *buf, unsigned long size, struct text_stat *
8688
*/
8789
static int convert_is_binary(unsigned long size, const struct text_stat *stats)
8890
{
89-
if (stats->cr != stats->crlf)
91+
if (stats->lonecr)
9092
return 1;
9193
if (stats->nul)
9294
return 1;
@@ -98,19 +100,18 @@ static int convert_is_binary(unsigned long size, const struct text_stat *stats)
98100
static unsigned int gather_convert_stats(const char *data, unsigned long size)
99101
{
100102
struct text_stat stats;
103+
int ret = 0;
101104
if (!data || !size)
102105
return 0;
103106
gather_stats(data, size, &stats);
104107
if (convert_is_binary(size, &stats))
105-
return CONVERT_STAT_BITS_BIN;
106-
else if (stats.crlf && stats.crlf == stats.lf)
107-
return CONVERT_STAT_BITS_TXT_CRLF;
108-
else if (stats.crlf && stats.lf)
109-
return CONVERT_STAT_BITS_TXT_CRLF | CONVERT_STAT_BITS_TXT_LF;
110-
else if (stats.lf)
111-
return CONVERT_STAT_BITS_TXT_LF;
112-
else
113-
return 0;
108+
ret |= CONVERT_STAT_BITS_BIN;
109+
if (stats.crlf)
110+
ret |= CONVERT_STAT_BITS_TXT_CRLF;
111+
if (stats.lonelf)
112+
ret |= CONVERT_STAT_BITS_TXT_LF;
113+
114+
return ret;
114115
}
115116

116117
static const char *gather_convert_stats_ascii(const char *data, unsigned long size)
@@ -207,7 +208,7 @@ static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
207208
* CRLFs would be added by checkout:
208209
* check if we have "naked" LFs
209210
*/
210-
if (stats->lf != stats->crlf) {
211+
if (stats->lonelf) {
211212
if (checksafe == SAFE_CRLF_WARN)
212213
warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
213214
else /* i.e. SAFE_CRLF_FAIL */
@@ -266,8 +267,8 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
266267

267268
check_safe_crlf(path, crlf_action, &stats, checksafe);
268269

269-
/* Optimization: No CR? Nothing to convert, regardless. */
270-
if (!stats.cr)
270+
/* Optimization: No CRLF? Nothing to convert, regardless. */
271+
if (!stats.crlf)
271272
return 0;
272273

273274
/*
@@ -314,19 +315,15 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len,
314315

315316
gather_stats(src, len, &stats);
316317

317-
/* No LF? Nothing to convert, regardless. */
318-
if (!stats.lf)
319-
return 0;
320-
321-
/* Was it already in CRLF format? */
322-
if (stats.lf == stats.crlf)
318+
/* No "naked" LF? Nothing to convert, regardless. */
319+
if (!stats.lonelf)
323320
return 0;
324321

325322
if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
326323
if (crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
327324
/* If we have any CR or CRLF line endings, we do not touch it */
328325
/* This is the new safer autocrlf-handling */
329-
if (stats.cr > 0 || stats.crlf > 0)
326+
if (stats.lonecr || stats.crlf )
330327
return 0;
331328
}
332329

@@ -338,7 +335,7 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len,
338335
if (src == buf->buf)
339336
to_free = strbuf_detach(buf, NULL);
340337

341-
strbuf_grow(buf, len + stats.lf - stats.crlf);
338+
strbuf_grow(buf, len + stats.lonelf);
342339
for (;;) {
343340
const char *nl = memchr(src, '\n', len);
344341
if (!nl)

0 commit comments

Comments
 (0)