1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-29 22:40:34 +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,19 +212,20 @@ interpret its input and output."
(define (clear-stream-start-for-bom-read port io-mode)
(define (maybe-consume-bom bom)
(and (eq? (peek-byte port) (bytevector-u8-ref bom 0))
(let* ((buf (fill-input port (bytevector-length bom)))
(bv (port-buffer-bytevector buf))
(cur (port-buffer-cur bv)))
(and (<= (bytevector-length bv)
(- (port-buffer-end buf) cur))
(let lp ((i 1))
(if (= i (bytevector-length bom))
(begin
(set-port-buffer-cur! buf (+ cur i))
#t)
(and (eq? (bytevector-u8-ref bv (+ cur i))
(bytevector-u8-ref bom i))
(lp (1+ i)))))))))
(call-with-values (lambda ()
(fill-input port (bytevector-length bom)))
(lambda (buf buffered)
(and (<= (bytevector-length bom) buffered)
(let ((bv (port-buffer-bytevector buf))
(cur (port-buffer-cur buf)))
(let lp ((i 1))
(if (= i (bytevector-length bom))
(begin
(set-port-buffer-cur! buf (+ cur i))
#t)
(and (eq? (bytevector-u8-ref bv (+ cur i))
(bytevector-u8-ref bom i))
(lp (1+ i)))))))))))
(when (and (port-clear-stream-start-for-bom-read port)
(eq? io-mode 'text))
(case (%port-encoding port)