main
Matthew Butterick 6 years ago
parent e669cd70f0
commit 9a225905f5

@ -14,7 +14,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
|#
(define xarray-base%
(class* xenobase% ()
(class xenobase%
(super-new)
(init-field type len)
(unless (xenomorphic? type)
@ -23,7 +23,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(raise-argument-error '+xarray "length-resolvable?" len))))
(define xarray%
(class* xarray-base% ()
(class xarray-base%
(super-new)
(init-field length-type)
(unless (memq length-type '(bytes count))
@ -38,22 +38,22 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
'_currentOffset 0
'_length len)
parent))
(define decoded-len (resolve-length len #:parent parent))
(define resolved-len (resolve-length len #:parent parent))
(cond
[(or (not decoded-len) (eq? length-type 'bytes))
[(or (not resolved-len) (eq? length-type 'bytes))
(define end-pos (cond
;; decoded-len is byte length
[decoded-len (+ (pos port) decoded-len)]
;; no decoded-len, but parent has length
;; resolved-len is byte length
[resolved-len (+ (pos port) resolved-len)]
;; no resolved-len, but parent has length
[(and parent (not (zero? (dict-ref parent '_length))))
(+ (dict-ref parent '_startOffset) (dict-ref parent '_length))]
;; no decoded-len or parent, so consume whole stream
;; no resolved-len or parent, so consume whole stream
[else +inf.0]))
(for/list ([i (in-naturals)]
#:break (or (eof-object? (peek-byte)) (= (pos port) end-pos)))
(send type xxdecode port new-parent))]
;; we have decoded-len, which is treated as count of items
[else (for/list ([i (in-range decoded-len)])
;; we have resolved-len, which is treated as count of items
[else (for/list ([i (in-range resolved-len)])
(send type xxdecode port new-parent))]))
(define/augment (xxencode array port [parent #f])

@ -71,7 +71,6 @@
[(and (integer? size) (not (negative? size))) size]
[else (raise-argument-error 'size "nonnegative integer" size)]))
(define xenomorphic<%>
(interface* ()
([(generic-property gen:xenomorphic)

@ -24,7 +24,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(define system-endian (if (system-big-endian?) 'be 'le))
(define xnumber%
(class* xenobase% ()
(class xenobase%
(super-new)
(init-field size endian)
(unless (exact-positive-integer? size)
@ -37,7 +37,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(define (xint? x) (is-a? x xint%))
(define xint%
(class* xnumber% ()
(class xnumber%
(super-new)
(init-field signed)
(inherit-field endian size bits)
@ -142,7 +142,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(check-equal? (encode int8 127 #f) (bytes 127)))
(define xfloat%
(class* xnumber% ()
(class xnumber%
(super-new)
(inherit-field size endian)

Loading…
Cancel
Save