aboutsummaryrefslogtreecommitdiffstats
path: root/src/coding.c
diff options
context:
space:
mode:
authorRichard M. Stallman1997-08-05 18:19:33 +0000
committerRichard M. Stallman1997-08-05 18:19:33 +0000
commitbc4bc72a9a8c1c3ba9c0e1c79086b67c451b6fff (patch)
tree64a92f12f2a305af563a8b15d0febd46897d2057 /src/coding.c
parentfc8915913f77829e27e9acbd9ae9e962bcfeb556 (diff)
downloademacs-bc4bc72a9a8c1c3ba9c0e1c79086b67c451b6fff.tar.gz
emacs-bc4bc72a9a8c1c3ba9c0e1c79086b67c451b6fff.zip
(detect_eol_type): If EOL representation does not
seem consistent, use no conversion.
Diffstat (limited to 'src/coding.c')
-rw-r--r--src/coding.c31
1 files changed, 23 insertions, 8 deletions
diff --git a/src/coding.c b/src/coding.c
index ee5fdf25fc2..c3f4e19d615 100644
--- a/src/coding.c
+++ b/src/coding.c
@@ -2703,6 +2703,8 @@ detect_coding (coding, src, src_bytes)
2703 is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF, 2703 is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
2704 CODING_EOL_CR, and CODING_EOL_UNDECIDED. */ 2704 CODING_EOL_CR, and CODING_EOL_UNDECIDED. */
2705 2705
2706#define MAX_EOL_CHECK_COUNT 3
2707
2706int 2708int
2707detect_eol_type (src, src_bytes) 2709detect_eol_type (src, src_bytes)
2708 unsigned char *src; 2710 unsigned char *src;
@@ -2710,21 +2712,34 @@ detect_eol_type (src, src_bytes)
2710{ 2712{
2711 unsigned char *src_end = src + src_bytes; 2713 unsigned char *src_end = src + src_bytes;
2712 unsigned char c; 2714 unsigned char c;
2715 int total = 0; /* How many end-of-lines are found so far. */
2716 int eol_type = CODING_EOL_UNDECIDED;
2717 int this_eol_type;
2713 2718
2714 while (src < src_end) 2719 while (src < src_end && total < MAX_EOL_CHECK_COUNT)
2715 { 2720 {
2716 c = *src++; 2721 c = *src++;
2717 if (c == '\n') 2722 if (c == '\n' || c == '\r')
2718 return CODING_EOL_LF;
2719 else if (c == '\r')
2720 { 2723 {
2721 if (src < src_end && *src == '\n') 2724 total++;
2722 return CODING_EOL_CRLF; 2725 if (c == '\n')
2726 this_eol_type = CODING_EOL_LF;
2727 else if (src >= src_end || *src != '\n')
2728 this_eol_type = CODING_EOL_CR;
2723 else 2729 else
2724 return CODING_EOL_CR; 2730 this_eol_type = CODING_EOL_CRLF, src++;
2731
2732 if (eol_type == CODING_EOL_UNDECIDED)
2733 /* This is the first end-of-line. */
2734 eol_type = this_eol_type;
2735 else if (eol_type != this_eol_type)
2736 /* The found type is different from what found before.
2737 We had better not decode end-of-line. */
2738 return CODING_EOL_LF;
2725 } 2739 }
2726 } 2740 }
2727 return CODING_EOL_UNDECIDED; 2741
2742 return (total ? eol_type : CODING_EOL_UNDECIDED);
2728} 2743}
2729 2744
2730/* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC 2745/* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC