Use read-bytevector! from Chibi

This commit is contained in:
Justin Ethier 2019-12-29 18:10:05 -05:00
parent 48ce2709b9
commit 837954b310
2 changed files with 20 additions and 11 deletions

View file

@ -4,7 +4,7 @@
Features Features
- Added `read-bytevector` and `write-bytevector` IO functions from R7RS. - Added `read-bytevector`, `read-bytevector!`, and `write-bytevector` I/O functions from R7RS.
Bug Fixes Bug Fixes

View file

@ -735,16 +735,25 @@
(loop (+ n 1))) (loop (+ n 1)))
(else bv)))))) (else bv))))))
(loop 0))) (loop 0)))
;; TODO: need to finish and debug the following function: (define (read-bytevector! vec . o)
(define (read-bytevector! vec . opts) (let* ((in (if (pair? o) (car o) (current-input-port)))
(letrec ((len (bytevector-length vec)) (o (if (pair? o) (cdr o) o))
(port (if (> (length opts) 0) (car opts) (current-output-port))) (start (if (pair? o) (car o) 0))
(start (if (> (length opts) 1) (cadr opts) 0)) (end (if (and (pair? o) (pair? (cdr o)))
(end (if (> (length opts) 2) (caddr opts) len)) (cadr o)
(bv (read-bytevector (- end start) port)) (bytevector-length vec))))
) (if (>= start end)
(bytevector-copy! vec start bv) 0
(- end start))) ;; TODO: return number of bytes read (let ((res (read-bytevector (- end start) in)))
(cond
((eof-object? res)
res)
(else
(let ((len (bytevector-length res)))
(do ((i 0 (+ i 1)))
((>= i len) len)
(bytevector-u8-set! vec (+ i start) (bytevector-u8-ref res i))
))))))))
(define (write-bytevector vec . opts) (define (write-bytevector vec . opts)
(letrec ((len (bytevector-length vec)) (letrec ((len (bytevector-length vec))
(port (if (> (length opts) 0) (car opts) (current-output-port))) (port (if (> (length opts) 0) (car opts) (current-output-port)))