mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-07-11 23:17:34 +02:00
Respecting line length limit in read-line.
This commit is contained in:
parent
a0854df2ca
commit
c1a5bc9d43
2 changed files with 21 additions and 12 deletions
|
@ -71,23 +71,25 @@
|
||||||
((not string-streams)
|
((not string-streams)
|
||||||
(define (%read-line n in)
|
(define (%read-line n in)
|
||||||
(let ((out (open-output-string)))
|
(let ((out (open-output-string)))
|
||||||
(let lp ()
|
(let lp ((i 0))
|
||||||
(let ((ch (read-char in)))
|
(let ((ch (peek-char in)))
|
||||||
(cond
|
(cond
|
||||||
((eof-object? ch)
|
((eof-object? ch)
|
||||||
(let ((res (get-output-string out)))
|
(let ((res (get-output-string out)))
|
||||||
(and (not (equal? res "")) res)))
|
(and (not (equal? res "")) res)))
|
||||||
|
((eqv? ch #\newline)
|
||||||
|
(read-char in)
|
||||||
|
(get-output-string out))
|
||||||
|
((eqv? ch #\return)
|
||||||
|
(read-char in)
|
||||||
|
(if (eqv? #\newline (peek-char in))
|
||||||
|
(read-char in))
|
||||||
|
(get-output-string out))
|
||||||
|
((>= i n)
|
||||||
|
(get-output-string out))
|
||||||
(else
|
(else
|
||||||
(write-char ch out)
|
(write-char (read-char in) out)
|
||||||
(cond
|
(lp (+ i 1))))))))))
|
||||||
((eqv? ch #\newline)
|
|
||||||
(get-output-string out))
|
|
||||||
((eqv? ch #\return)
|
|
||||||
(if (eqv? #\newline (peek-char in))
|
|
||||||
(read-char in))
|
|
||||||
(get-output-string out))
|
|
||||||
(else
|
|
||||||
(lp)))))))))))
|
|
||||||
|
|
||||||
(define (read-line . o)
|
(define (read-line . o)
|
||||||
(let ((in (if (pair? o) (car o) (current-input-port)))
|
(let ((in (if (pair? o) (car o) (current-input-port)))
|
||||||
|
|
|
@ -19,6 +19,13 @@
|
||||||
(call-with-input-string "abc\ndef\n"
|
(call-with-input-string "abc\ndef\n"
|
||||||
(lambda (in) (let ((line (read-line in))) (list line (read-line in))))))
|
(lambda (in) (let ((line (read-line in))) (list line (read-line in))))))
|
||||||
|
|
||||||
|
(test "read-line" '("abc" "def" "ghi")
|
||||||
|
(call-with-input-string "abcdef\nghi\n"
|
||||||
|
(lambda (in)
|
||||||
|
(let* ((line1 (read-line in 3))
|
||||||
|
(line2 (read-line in 3)))
|
||||||
|
(list line1 line2 (read-line in 3))))))
|
||||||
|
|
||||||
(test "read-line-to-eof" '("abc" "def")
|
(test "read-line-to-eof" '("abc" "def")
|
||||||
(call-with-input-string "abc\ndef"
|
(call-with-input-string "abc\ndef"
|
||||||
(lambda (in) (let ((line (read-line in))) (list line (read-line in))))))
|
(lambda (in) (let ((line (read-line in))) (list line (read-line in))))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue