mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-23 20:15:05 +02:00
Use read-bytevector!
from Chibi
This commit is contained in:
parent
48ce2709b9
commit
837954b310
2 changed files with 20 additions and 11 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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)))
|
||||
|
|
Loading…
Add table
Reference in a new issue