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)
|
||||
(define (%read-line n in)
|
||||
(let ((out (open-output-string)))
|
||||
(let lp ()
|
||||
(let ((ch (read-char in)))
|
||||
(let lp ((i 0))
|
||||
(let ((ch (peek-char in)))
|
||||
(cond
|
||||
((eof-object? ch)
|
||||
(let ((res (get-output-string out)))
|
||||
(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
|
||||
(write-char ch out)
|
||||
(cond
|
||||
((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)))))))))))
|
||||
(write-char (read-char in) out)
|
||||
(lp (+ i 1))))))))))
|
||||
|
||||
(define (read-line . o)
|
||||
(let ((in (if (pair? o) (car o) (current-input-port)))
|
||||
|
|
|
@ -19,6 +19,13 @@
|
|||
(call-with-input-string "abc\ndef\n"
|
||||
(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")
|
||||
(call-with-input-string "abc\ndef"
|
||||
(lambda (in) (let ((line (read-line in))) (list line (read-line in))))))
|
||||
|
|
Loading…
Add table
Reference in a new issue