1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 03:40:34 +02:00

Merge from stable-2.2

This commit is contained in:
Andy Wingo 2019-08-02 15:04:48 +02:00
commit ce10a30e23

View file

@ -246,51 +246,73 @@
(fast-path buf bv cur buffered))) (fast-path buf bv cur buffered)))
(peek-bytes port 1 fast-path slow-path)) (peek-bytes port 1 fast-path slow-path))
(define* (get-bytevector-n port count) (define (get-bytevector-n! port bv start count)
(let ((ret (make-bytevector count))) (define (port-buffer-take! pos buf cur to-copy)
(define (port-buffer-take! pos buf cur to-copy) (bytevector-copy! (port-buffer-bytevector buf) cur
(bytevector-copy! (port-buffer-bytevector buf) cur bv pos to-copy)
ret pos to-copy) (set-port-buffer-cur! buf (+ cur to-copy))
(set-port-buffer-cur! buf (+ cur to-copy)) (+ pos to-copy))
(+ pos to-copy)) (define (take-already-buffered)
(define (take-already-buffered) (let* ((buf (port-read-buffer port))
(let* ((buf (port-read-buffer port)) (cur (port-buffer-cur buf))
(cur (port-buffer-cur buf)) (buffered (max (- (port-buffer-end buf) cur) 0)))
(buffered (max (- (port-buffer-end buf) cur) 0))) (port-buffer-take! start buf cur (min count buffered))))
(port-buffer-take! 0 buf cur (min count buffered)))) (define (buffer-and-fill pos)
(define (trim-and-return len) (call-with-values (lambda () (fill-input port 1 'binary))
(if (zero? len) (lambda (buf cur buffered)
the-eof-object (if (zero? buffered)
(let ((partial (make-bytevector len))) ;; We found EOF, which is marked in the port read buffer.
(bytevector-copy! ret 0 partial 0 len) ;; If we haven't read any bytes yet, clear the EOF from the
partial))) ;; buffer and return it. Otherwise return the number of
(define (buffer-and-fill pos) ;; bytes that we have read.
(call-with-values (lambda () (fill-input port 1 'binary)) (if (= pos start)
(lambda (buf cur buffered) (begin
(if (zero? buffered) (set-port-buffer-has-eof?! buf #f)
the-eof-object)
(- pos start))
(let ((pos (port-buffer-take! pos buf cur
(min (- (+ start count) pos)
buffered))))
(if (= pos (+ start count))
count
(buffer-and-fill pos)))))))
(define (fill-directly pos)
(when (port-random-access? port)
(flush-output port))
(port-clear-stream-start-for-bom-read port)
(let lp ((pos pos))
(let ((read (read-bytes port bv pos (- (+ start count) pos))))
(cond
((= (+ pos read) (+ start count))
count)
((zero? read)
;; We found EOF. If we haven't read any bytes yet, return
;; EOF. Otherwise save the EOF in the port read buffer.
(if (= pos start)
the-eof-object
(begin (begin
(set-port-buffer-has-eof?! buf #f) (set-port-buffer-has-eof?! (port-read-buffer port) #t)
(trim-and-return pos)) (- pos start))))
(let ((pos (port-buffer-take! pos buf cur (else (lp (+ pos read)))))))
(min (- count pos) buffered)))) (let ((pos (take-already-buffered)))
(if (= pos count) (cond
ret ((= pos (+ start count))
(buffer-and-fill pos))))))) count)
(define (fill-directly pos) ((< (- (+ start count) pos) (port-read-buffering port))
(when (port-random-access? port) (buffer-and-fill pos))
(flush-output port)) (else (fill-directly pos)))))
(port-clear-stream-start-for-bom-read port)
(let lp ((pos pos)) (define (get-bytevector-n port count)
(let ((read (read-bytes port ret pos (- count pos)))) (let* ((bv (make-bytevector count))
(cond (result (get-bytevector-n! port bv 0 count)))
((= read (- count pos)) ret) (cond ((eof-object? result)
((zero? read) (trim-and-return pos)) result)
(else (lp (+ pos read))))))) ((= result count)
(let ((pos (take-already-buffered))) bv)
(cond (else
((= pos count) (if (zero? pos) the-eof-object ret)) (let ((bv* (make-bytevector result)))
((< (- count pos) (port-read-buffering port)) (buffer-and-fill pos)) (bytevector-copy! bv 0 bv* 0 result)
(else (fill-directly pos)))))) bv*)))))
(define (get-bytevector-some port) (define (get-bytevector-some port)
(call-with-values (lambda () (fill-input port 1 'binary)) (call-with-values (lambda () (fill-input port 1 'binary))
@ -730,7 +752,7 @@
read-char peek-char force-output close-port read-char peek-char force-output close-port
accept connect) accept connect)
((ice-9 binary-ports) ((ice-9 binary-ports)
get-u8 lookahead-u8 get-bytevector-n get-u8 lookahead-u8 get-bytevector-n get-bytevector-n!
get-bytevector-some get-bytevector-some! get-bytevector-some get-bytevector-some!
put-u8 put-bytevector) put-u8 put-bytevector)
((ice-9 textual-ports) ((ice-9 textual-ports)