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
- 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

View file

@ -735,16 +735,25 @@
(loop (+ n 1)))
(else bv))))))
(loop 0)))
;; TODO: need to finish and debug the following function:
(define (read-bytevector! vec . opts)
(letrec ((len (bytevector-length vec))
(port (if (> (length opts) 0) (car opts) (current-output-port)))
(start (if (> (length opts) 1) (cadr opts) 0))
(end (if (> (length opts) 2) (caddr opts) len))
(bv (read-bytevector (- end start) port))
)
(bytevector-copy! vec start bv)
(- end start))) ;; TODO: return number of bytes read
(define (read-bytevector! vec . o)
(let* ((in (if (pair? o) (car o) (current-input-port)))
(o (if (pair? o) (cdr o) o))
(start (if (pair? o) (car o) 0))
(end (if (and (pair? o) (pair? (cdr o)))
(cadr o)
(bytevector-length vec))))
(if (>= start end)
0
(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)
(letrec ((len (bytevector-length vec))
(port (if (> (length opts) 0) (car opts) (current-output-port)))