From edddbd29250af4f8fb7173ff2d4bf73004057774 Mon Sep 17 00:00:00 2001 From: mainzelm Date: Tue, 7 Jan 2003 17:32:57 +0000 Subject: [PATCH] Partially rewrite of read-string(!)/partial: - immediately return on vacuous request - try to avoid copying in read-string!/partial - don't use read-string!/partial in read-string/partial to avoid duplicate copying - if argument is fd, set port-buffer to bufpol/none as usual --- scsh/rw.scm | 58 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/scsh/rw.scm b/scsh/rw.scm index c0ff5b3..28b4ade 100644 --- a/scsh/rw.scm +++ b/scsh/rw.scm @@ -21,28 +21,50 @@ (cond ((integer? fd/port) (let ((port (fdes->inport fd/port))) - (set-port-buffering port bufpol/block (- end start)) - (read-string!/partial port start end))) + (set-port-buffering port bufpol/none) + (read-string!/partial s port start end))) - (else ; no differnce between fd/ports and s48 ports - (let* ((buffer (make-string (- end start))) - (needed (if (> (byte-vector-length (port-buffer fd/port)) 1) - 'any 'immediate)) ;bufpol/none may return with 0 - (len (read-block buffer 0 needed fd/port))) - (if (eof-object? len) - #f - (begin - (copy-bytes! buffer 0 s start len) - len))))))) + ((open-input-port? fd/port) + (if (= start end) + 0 + (let* ((buffer (if (= start 0) + s + (make-string (- end start)))) + (needed (if (> (byte-vector-length (port-buffer fd/port)) 1) + 'any + 'immediate)) ;bufpol/none may return with 0 + (nread (read-block buffer 0 needed fd/port))) + (if (eof-object? nread) + #f + (begin + (if (not (eq? s buffer)) + (copy-bytes! buffer 0 s start nread)) + nread))))) + + (else + (apply error "Not a fd/port in read-string!/partial" s args))))) (define (read-string/partial len . maybe-fd/port) - (let* ((s (make-string len)) - (fd/port (:optional maybe-fd/port (current-input-port))) - (nread (read-string!/partial s fd/port 0 len))) - (cond ((not nread) #f) ; EOF - ((= nread len) s) - (else (substring s 0 nread))))) + (let* ((fd/port (:optional maybe-fd/port (current-input-port)))) + (cond ((integer? fd/port) + (let ((port (fdes->inport fd/port))) + (set-port-buffering port bufpol/none) + (read-string/partial len port))) + + ((open-input-port? fd/port) + (if (= len 0) + 0 + (let* ((buffer (make-string len)) + (needed (if (> (byte-vector-length (port-buffer fd/port)) 1) + 'any + 'immediate));; bufpol/none may return with 0 + (nread (read-block buffer 0 needed fd/port))) + (cond ((eof-object? nread) #f) + ((= nread len) buffer) + (else (substring buffer 0 nread)))))) + (else + (error "Not a fd/port in read-string/partial" len fd/port))))) ;;; Persistent reading