From d731f92e7da45f66a2c79950cfe6c22159eb7e35 Mon Sep 17 00:00:00 2001 From: Vasilij Schneidermann Date: Thu, 7 Sep 2017 17:07:05 +0200 Subject: [PATCH] read-string: return EOF if nothing can be read R7RS states that there's three possible scenarios for read-string: - More characters can be read than asked for (return string) - Less characters can be read than asked for (return string) - No characters can be read (return EOF) This commit ensures the last scenario works as intended. --- scheme/base.sld | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/scheme/base.sld b/scheme/base.sld index 47af273b..8dcbbfb9 100644 --- a/scheme/base.sld +++ b/scheme/base.sld @@ -645,21 +645,23 @@ (let ((port (if (null? opts) (current-input-port) (car opts)))) - (let loop ((acc '()) - (i k) - (chr #f)) - (cond - ((eof-object? chr) - (list->string - (reverse acc))) - ((zero? i) - (list->string - (reverse - (if chr (cons chr acc) acc)))) - (else - (loop (if chr (cons chr acc) acc) - (- i 1) - (read-char port))))))) + (if (eof-object? (peek-char port)) + (eof-object) + (let loop ((acc '()) + (i k) + (chr #f)) + (cond + ((eof-object? chr) + (list->string + (reverse acc))) + ((zero? i) + (list->string + (reverse + (if chr (cons chr acc) acc)))) + (else + (loop (if chr (cons chr acc) acc) + (- i 1) + (read-char port)))))))) (define (flush-output-port . port) (if (null? port) (Cyc-flush-output-port (current-output-port))