Changeset 30653 in project for release/4/r7rs/trunk/scheme.base.scm


Ignore:
Timestamp:
04/05/14 23:50:10 (7 years ago)
Author:
evhan
Message:

r7rs/base: Bytevector ports (credit to Seth Alves)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • release/4/r7rs/trunk/scheme.base.scm

    r30579 r30653  
    2323                (make-u8vector make-bytevector)
    2424                (write-u8vector write-bytevector)))
     25
     26(import (only ports make-input-port make-output-port))
    2527
    2628(%include "scheme.base-interface.scm")
     
    785787       (read-u8vector!/eof (fx- end start) bv port start)))))
    786788
     789(define (open-input-bytevector bv)
     790  (let ((index 0)
     791        (bv-len (bytevector-length bv)))
     792    (make-input-port
     793     (lambda () ; read-char
     794       (if (= index bv-len)
     795           (eof-object)
     796           (let ((c (bytevector-u8-ref bv index)))
     797             (set! index (+ index 1))
     798             (integer->char c))))
     799     (lambda () ; char-ready?
     800       (not (= index bv-len)))
     801     (lambda () #t) ; close
     802     (lambda () ; peek-char
     803       (if (= index bv-len)
     804           (eof-object)
     805           (bytevector-u8-ref bv index))))))
     806
     807(define (open-output-bytevector) (open-output-string))
     808
     809(define (get-output-bytevector p)
     810  (string->utf8 (get-output-string p)))
     811
    787812)
Note: See TracChangeset for help on using the changeset viewer.