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
This commit is contained in:
mainzelm 2003-01-07 17:32:57 +00:00
parent 7fb1d585b2
commit edddbd2925
1 changed files with 40 additions and 18 deletions

View File

@ -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