~ chicken-core (chicken-5) d7bc26071a3ab26c58cb9474056b61f90f017fab


commit d7bc26071a3ab26c58cb9474056b61f90f017fab
Author:     Evan Hanson <evhan@foldling.org>
AuthorDate: Thu Oct 31 19:05:17 2013 +1300
Commit:     Moritz Heidkamp <moritz@twoticketsplease.de>
CommitDate: Sun Nov 3 18:18:39 2013 +0100

    Treat lone carriage returns as line endings in ##sys#scan-buffer-line
    
    This fixes #1004.
    
    Signed-off-by: Peter Bex <peter.bex@xs4all.nl>

diff --git a/library.scm b/library.scm
index 364b4778..7297a72e 100644
--- a/library.scm
+++ b/library.scm
@@ -3677,6 +3677,8 @@ EOF
 				   (##sys#string-append line "\r")))
 			 ;; Restore \r here, too (when we reached EOF)
 			 (values offset (##sys#string-append line "\r") #t)))))
+		((eq? c #\return)
+		 (values (fx+ pos 1) (copy&append buf offset pos line) #t))
 		(else (loop buf offset (fx+ pos 1) limit line)) ) ) ) ) )
 
 (define (open-input-string string)
diff --git a/tests/port-tests.scm b/tests/port-tests.scm
index ebba3503..259892cf 100644
--- a/tests/port-tests.scm
+++ b/tests/port-tests.scm
@@ -353,6 +353,27 @@ EOF
                 buf
                 "89067")))
 
+(test-group "line endings"
+  (let ((s "foo\nbar\rbaz\r\nqux")
+	(f (lambda ()
+	     (test-equal "\\n" (read-line) "foo")
+	     (test-equal "\\r" (read-line) "bar")
+	     (test-equal "\\r\\n" (read-line) "baz")
+	     (test-equal "eof" (read-line) "qux"))))
+    (test-group "string port"
+      (with-input-from-string s f))
+    (test-group "file port"
+      (let ((file "mixed-line-endings"))
+	(with-output-to-file file (lambda () (display s)))
+	(with-input-from-file file f)
+	(delete-file* file)))
+    (test-group "custom port"
+      (let* ((p (open-input-string s))
+	     (p* (make-input-port (lambda () (read-char p))
+				  (lambda () (char-ready? p))
+				  (lambda () (close-input-port p)))))
+	(with-input-from-port p* f)))))
+
 ;; Disabled because it requires `echo -n` for
 ;; the EOF test, and that is not available on all systems.
 ;; Uncomment locally to run.
Trap