1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-30 15:00:21 +02:00

clear-stream-start-for-bom-read refactor

* module/ice-9/ports.scm (clear-stream-start-for-bom-read): Use the
  "buffered" value that fill-input returns.
This commit is contained in:
Andy Wingo 2016-05-10 15:36:31 +02:00
parent 502e3a2213
commit 0dd18191bc

View file

@ -212,11 +212,12 @@ interpret its input and output."
(define (clear-stream-start-for-bom-read port io-mode) (define (clear-stream-start-for-bom-read port io-mode)
(define (maybe-consume-bom bom) (define (maybe-consume-bom bom)
(and (eq? (peek-byte port) (bytevector-u8-ref bom 0)) (and (eq? (peek-byte port) (bytevector-u8-ref bom 0))
(let* ((buf (fill-input port (bytevector-length bom))) (call-with-values (lambda ()
(bv (port-buffer-bytevector buf)) (fill-input port (bytevector-length bom)))
(cur (port-buffer-cur bv))) (lambda (buf buffered)
(and (<= (bytevector-length bv) (and (<= (bytevector-length bom) buffered)
(- (port-buffer-end buf) cur)) (let ((bv (port-buffer-bytevector buf))
(cur (port-buffer-cur buf)))
(let lp ((i 1)) (let lp ((i 1))
(if (= i (bytevector-length bom)) (if (= i (bytevector-length bom))
(begin (begin
@ -224,7 +225,7 @@ interpret its input and output."
#t) #t)
(and (eq? (bytevector-u8-ref bv (+ cur i)) (and (eq? (bytevector-u8-ref bv (+ cur i))
(bytevector-u8-ref bom i)) (bytevector-u8-ref bom i))
(lp (1+ i))))))))) (lp (1+ i)))))))))))
(when (and (port-clear-stream-start-for-bom-read port) (when (and (port-clear-stream-start-for-bom-read port)
(eq? io-mode 'text)) (eq? io-mode 'text))
(case (%port-encoding port) (case (%port-encoding port)