read-line should use port's encoding, not locale's encoding
authorMichael Gran <spk121@yahoo.com>
Fri, 16 Jul 2010 13:44:59 +0000 (06:44 -0700)
committerMichael Gran <spk121@yahoo.com>
Fri, 16 Jul 2010 13:44:59 +0000 (06:44 -0700)
* libguile/rdelim.c (scm_read_line): modified, use port's encoding
* test-suite/test/ports.test: new test

libguile/rdelim.c
test-suite/tests/ports.test

index 1f46e5b..1340c62 100644 (file)
@@ -205,12 +205,16 @@ SCM_DEFINE (scm_read_line, "%read-line", 0, 1, 0,
   char *s;
   size_t slen = 0;
   SCM line, term;
+  const char *enc;
+  scm_t_string_failed_conversion_handler hndl;
 
   if (SCM_UNBNDP (port))
     port = scm_current_input_port ();
   SCM_VALIDATE_OPINPORT (1,port);
 
   pt = SCM_PTAB_ENTRY (port);
+  enc = pt->encoding;
+  hndl = pt->ilseq_handler;
   if (pt->rw_active == SCM_PORT_WRITE)
     scm_ptobs[SCM_PTOBNUM (port)].flush (port);
 
@@ -220,19 +224,22 @@ SCM_DEFINE (scm_read_line, "%read-line", 0, 1, 0,
     term = line = SCM_EOF_VAL;
   else
     {
-      if (s[slen-1] == '\n')
+      if (s[slen - 1] == '\n')
        {
          term = SCM_MAKE_CHAR ('\n');
-         s[slen-1] = '\0';
-         line = scm_take_locale_stringn (s, slen-1);
+         s[slen - 1] = '\0';
+
+         line = scm_from_stringn (s, slen - 1, enc, hndl);
+         free (s);
          SCM_INCLINE (port);
        }
       else
        {
          /* Fix: we should check for eof on the port before assuming this. */
          term = SCM_EOF_VAL;
-         line = scm_take_locale_stringn (s, slen);
-         SCM_COL (port) += slen;
+         line = scm_from_stringn (s, slen, enc, hndl);
+         free (s);
+         SCM_COL (port) += scm_i_string_length (line);
        }
     }
 
index 1d8bd50..9f9985a 100644 (file)
             (string=? line test-string)))
   (delete-file filename))
 
+;;; read-line should use the port encoding (not the locale encoding).
+(let ((str "ĉu bone?"))
+      (with-locale "C"
+        (let* ((filename (test-file))
+               (port (open-file filename "wl")))
+          (set-port-encoding! port "UTF-8")
+          (write-line str port)
+          (let ((in-port (open-input-file filename)))
+            (set-port-encoding! in-port "UTF-8")
+            (let ((line (read-line in-port)))
+              (close-port in-port)
+              (close-port port)
+              (pass-if "file: read-line honors port encoding"
+                       (string=? line str))))
+          (delete-file filename))))
+
 ;;; ungetting characters and strings.
 (with-input-from-string "walk on the moon\nmoon"
                        (lambda ()