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:
parent
7fb1d585b2
commit
edddbd2925
52
scsh/rw.scm
52
scsh/rw.scm
|
@ -21,28 +21,50 @@
|
||||||
|
|
||||||
(cond ((integer? fd/port)
|
(cond ((integer? fd/port)
|
||||||
(let ((port (fdes->inport fd/port)))
|
(let ((port (fdes->inport fd/port)))
|
||||||
(set-port-buffering port bufpol/block (- end start))
|
(set-port-buffering port bufpol/none)
|
||||||
(read-string!/partial port start end)))
|
(read-string!/partial s port start end)))
|
||||||
|
|
||||||
(else ; no differnce between fd/ports and s48 ports
|
((open-input-port? fd/port)
|
||||||
(let* ((buffer (make-string (- end start)))
|
(if (= start end)
|
||||||
|
0
|
||||||
|
(let* ((buffer (if (= start 0)
|
||||||
|
s
|
||||||
|
(make-string (- end start))))
|
||||||
(needed (if (> (byte-vector-length (port-buffer fd/port)) 1)
|
(needed (if (> (byte-vector-length (port-buffer fd/port)) 1)
|
||||||
'any 'immediate)) ;bufpol/none may return with 0
|
'any
|
||||||
(len (read-block buffer 0 needed fd/port)))
|
'immediate)) ;bufpol/none may return with 0
|
||||||
(if (eof-object? len)
|
(nread (read-block buffer 0 needed fd/port)))
|
||||||
|
(if (eof-object? nread)
|
||||||
#f
|
#f
|
||||||
(begin
|
(begin
|
||||||
(copy-bytes! buffer 0 s start len)
|
(if (not (eq? s buffer))
|
||||||
len)))))))
|
(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)
|
(define (read-string/partial len . maybe-fd/port)
|
||||||
(let* ((s (make-string len))
|
(let* ((fd/port (:optional maybe-fd/port (current-input-port))))
|
||||||
(fd/port (:optional maybe-fd/port (current-input-port)))
|
(cond ((integer? fd/port)
|
||||||
(nread (read-string!/partial s fd/port 0 len)))
|
(let ((port (fdes->inport fd/port)))
|
||||||
(cond ((not nread) #f) ; EOF
|
(set-port-buffering port bufpol/none)
|
||||||
((= nread len) s)
|
(read-string/partial len port)))
|
||||||
(else (substring s 0 nread)))))
|
|
||||||
|
((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
|
;;; Persistent reading
|
||||||
|
|
Loading…
Reference in New Issue