OSDN Git Service

* Fixed: Guess Next line broken. [Nkf-dev 44]
authorNARUSE, Yui <naruse@users.sourceforge.jp>
Tue, 2 Oct 2007 04:55:25 +0000 (04:55 +0000)
committerNARUSE, Yui <naruse@users.sourceforge.jp>
Tue, 2 Oct 2007 04:55:25 +0000 (04:55 +0000)
  reported by Motoyuki Kasahara.

* Add tests for Guess Next line.

nkf.c
nkf_test.pl

diff --git a/nkf.c b/nkf.c
index b2a9631..3415fd1 100644 (file)
--- a/nkf.c
+++ b/nkf.c
@@ -30,7 +30,7 @@
  * \e$B8=:_!"\e(Bnkf \e$B$O\e(B SorceForge \e$B$K$F%a%s%F%J%s%9$,B3$1$i$l$F$$$^$9!#\e(B
  * http://sourceforge.jp/projects/nkf/
 ***********************************************************************/
-/* $Id: nkf.c,v 1.137 2007/10/01 14:29:21 naruse Exp $ */
+/* $Id: nkf.c,v 1.138 2007/10/01 19:55:25 naruse Exp $ */
 #define NKF_VERSION "2.0.8"
 #define NKF_RELEASE_DATE "2007-10-01"
 #define COPY_RIGHT \
@@ -706,8 +706,9 @@ static char            *backup_suffix = "";
 static char *get_backup_filename(const char *suffix, const char *filename);
 #endif
 
-static int             nlmode_f = 0;   /* CR, LF, CRLF */
-static nkf_char prev_cr = 0;
+static int nlmode_f = 0;   /* CR, LF, CRLF */
+static int input_nextline = 0; /* 0: unestablished, EOF: MIXED */
+static nkf_char prev_cr = 0; /* CR or 0 */
 #ifdef EASYWIN /*Easy Win */
 static int             end_check;
 #endif /*Easy Win */
@@ -2281,7 +2282,7 @@ void module_connection(void)
        /* base64_count = 0; */
     }
 
-    if (nlmode_f) {
+    if (nlmode_f || guess_f) {
        o_nlconv = oconv; oconv = nl_conv;
     }
     if (rot_f) {
@@ -2878,10 +2879,6 @@ nkf_char kanji_convert(FILE *f)
                        SEND;
                    }
                }
-               if (!nlmode_f) {
-                   if (prev_cr && c1 == LF) nlmode_f = CRLF;
-                   else nlmode_f = c1;
-               }
            } else if (c1 == DEL && input_mode == X0208) {
                /* CP5022x */
                c2 = c1;
@@ -4298,27 +4295,22 @@ nkf_char broken_ungetc(nkf_char c, FILE *f)
 
 void nl_conv(nkf_char c2, nkf_char c1)
 {
-    if (prev_cr) {
+    if (guess_f && input_nextline != EOF) {
+       if (c2 == 0 && c1 == LF) {
+           if (!input_nextline) input_nextline = prev_cr ? CRLF : LF;
+           else if (input_nextline != (prev_cr ? CRLF : LF)) input_nextline = EOF;
+       } else if (c2 == 0 && c1 == CR && input_nextline == LF) input_nextline = EOF;
+       else if (!prev_cr);
+       else if (!input_nextline) input_nextline = CR;
+       else if (input_nextline != CR) input_nextline = EOF;
+    }
+    if (prev_cr || c2 == 0 && c1 == LF) {
        prev_cr = 0;
-       if (! (c2==0&&c1==LF)) {
-           nl_conv(0,LF);
-       }
-    }
-    if (c2) {
-        (*o_nlconv)(c2,c1);
-    } else if (c1==CR) {
-       prev_cr = c1;
-    } else if (c1==LF) {
-        if (nlmode_f==CRLF) {
-            (*o_nlconv)(0,CR);
-       } else if (nlmode_f==CR) {
-            (*o_nlconv)(0,CR);
-           return;
-       }
-       (*o_nlconv)(0,LF);
-    } else if (c1!='\032' || nlmode_f!=LF){
-        (*o_nlconv)(c2,c1);
+       if (nlmode_f != LF) (*o_nlconv)(0, CR);
+       if (nlmode_f != CR) (*o_nlconv)(0, LF);
     }
+    if (c2 == 0 && c1 == CR) prev_cr = CR;
+    else if (c2 != 0 || c1 != LF) (*o_nlconv)(c2, c1);
 }
 
 /*
@@ -4999,20 +4991,18 @@ void print_guessed_code(char *filename)
 {
     char *codename = "BINARY";
     char *str_nlmode = NULL;
-    if (!input_codename || *input_codename) {
-        if (!input_codename) {
-            codename = "ASCII";
-        } else {
-            codename = input_codename;
-        }
-        if (nlmode_f == CR) str_nlmode = "CR";
-        else if (nlmode_f == LF) str_nlmode = "LF";
-        else if (nlmode_f == CRLF) str_nlmode = "CRLF";
-        else if (nlmode_f == EOF) str_nlmode = "MIXED NL";
-    }
     if (filename != NULL) printf("%s: ", filename);
-    if (str_nlmode != NULL) printf("%s (%s)\n", codename, str_nlmode);
-    else printf("%s\n", codename);
+    if (input_codename && !*input_codename) {
+       printf("BINARY\n");
+    } else {
+       printf("%s%s\n",
+              (input_codename ? input_codename : "ASCII"),
+              input_nextline == CR   ? " (CR)" :
+              input_nextline == LF   ? " (LF)" :
+              input_nextline == CRLF ? " (CRLF)" :
+              input_nextline == EOF  ? " (MIXED NL)" :
+              "");
+    }
 }
 #endif /*WIN32DLL*/
 
index 9e60d15..c5f401c 100644 (file)
@@ -2,7 +2,7 @@
 #
 # nkf test program for nkf-2
 #
-# $Id: nkf_test.pl,v 1.19 2007/09/12 04:56:53 naruse Exp $
+# $Id: nkf_test.pl,v 1.20 2007/10/01 19:55:25 naruse Exp $
 #
 #    Shinji KONO <kono@ie.u-ryukyu.ac.jp>
 # Sun Aug 18 12:25:40 JST 1996
@@ -1049,5 +1049,26 @@ eofeof
 
 printf "%-40s", "test_data/bugs10904";
     &test("$nkf -Mj",$example{'test_data/bugs10904'},$example{'test_data/bugs10904.ans'});
-# end
 
+printf "%-40s", "Guess NL/NONE";      &test("$nkf --guess","none",      "ASCII\n");
+printf "%-40s", "Guess NL/LF";        &test("$nkf --guess","\n",        "ASCII (LF)\n");
+printf "%-40s", "Guess NL/LFLF";      &test("$nkf --guess","\n\n",      "ASCII (LF)\n");
+printf "%-40s", "Guess NL/LFCR";      &test("$nkf --guess","\n\r",      "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/LFCRLF";    &test("$nkf --guess","\n\r\n",    "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/LF.LF";     &test("$nkf --guess","\n.\n",     "ASCII (LF)\n");
+printf "%-40s", "Guess NL/LF.CR";     &test("$nkf --guess","\n.\r",     "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/LF.CRLF";   &test("$nkf --guess","\n.\r\n",   "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CR";        &test("$nkf --guess","\r",        "ASCII (CR)\n");
+printf "%-40s", "Guess NL/CRCR";      &test("$nkf --guess","\r\r",      "ASCII (CR)\n");
+printf "%-40s", "Guess NL/CRCRLF";    &test("$nkf --guess","\r\r\n",    "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CR.LF";     &test("$nkf --guess","\r.\n",     "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CR.CR";     &test("$nkf --guess","\r.\r",     "ASCII (CR)\n");
+printf "%-40s", "Guess NL/CR.CRLF";   &test("$nkf --guess","\r.\r\n",   "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CRLF";      &test("$nkf --guess","\r\n",      "ASCII (CRLF)\n");
+printf "%-40s", "Guess NL/CRLFLF";    &test("$nkf --guess","\r\n\n",    "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CRLFCR";    &test("$nkf --guess","\r\n\r",    "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CRLFCRLF";  &test("$nkf --guess","\r\n\r\n",  "ASCII (CRLF)\n");
+printf "%-40s", "Guess NL/CRLF.LF";   &test("$nkf --guess","\r\n.\n",   "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CRLF.CR";   &test("$nkf --guess","\r\n.\r",   "ASCII (MIXED NL)\n");
+printf "%-40s", "Guess NL/CRLF.CRLF"; &test("$nkf --guess","\r\n.\r\n", "ASCII (CRLF)\n");
+# end