You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/pitfall/restructure/decodestream.rkt

32 lines
1.4 KiB
Racket

#lang restructure/racket
(provide (all-defined-out))
;; approximates https://github.com/mbutterick/restructure/blob/master/src/DecodeStream.coffee
8 years ago
(provide (rename-out [type-sizes TYPES]))
(define type-sizes (let-values ([(intkeys intvalues)
(for*/lists (intkeys intvalues)
([signed (in-list '(U ""))]
[size (in-list '(8 16 24 32))])
(values
(format "~aInt~a" signed size)
(/ size 8)))])
(for/hash ([key (in-list (append '(Float Double) intkeys))]
[value (in-list (append '(4 8) intvalues))]
#:when key
[endian '("" BE LE)])
(values (string->symbol (format "~a~a" key endian)) value))))
(require racket/port)
8 years ago
;; basically just a wrapper for a Racket port
(define-subclass object% (RDecodeStream [buffer-in #""])
(field [buffer (if (input-port? buffer-in)
(port->bytes buffer-in)
buffer-in)]
[length (bytes-length buffer)]
8 years ago
[_port (open-input-bytes buffer)])
(getter-field [pos (port-position _port)])
8 years ago
(define/public (read count)
8 years ago
(read-bytes-exact count _port)))