main
Matthew Butterick 6 years ago
parent fa97961eb1
commit fc78111cd1

@ -26,7 +26,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(raise-argument-error '+xarray "'bytes or 'count" @length-type))
(define/augride (x:decode port parent)
(define new-parent (if (xint? @len)
(define new-parent (if (x:int? @len)
(mhasheq 'parent parent
'_startOffset (pos port)
'_currentOffset 0
@ -62,7 +62,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(for ([item array])
(send @type x:encode item port parent))))
(cond
[(xint? @len)
[(x:int? @len)
(define new-parent (mhash 'pointers null
'startOffset (pos port)
'parent parent))
@ -78,7 +78,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(raise-argument-error 'xarray-size "sequence" val)))
(cond
[val (define-values (new-parent len-size)
(if (xint? @len)
(if (x:int? @len)
(values (mhasheq 'parent parent) (send @len x:size))
(values parent 0)))
(define items-size (for/sum ([item val])

@ -21,7 +21,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Buffer.coffee
(define/augment (x:encode buf port [parent #f])
(unless (bytes? buf)
(raise-argument-error 'xbuffer-encode "bytes" buf))
(when (xint? @len)
(when (x:int? @len)
(send @len x:encode (bytes-length buf) port))
(write-bytes buf port))

@ -16,7 +16,7 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
(define/override (x:decode port parent)
(define starting-pos (pos port)) ; ! placement matters. `resolve-length` will change `pos`
(define len (resolve-length @len port #:parent parent))
(define new-parent (if (xint? @len)
(define new-parent (if (x:int? @len)
(mhasheq 'parent parent
'_startOffset starting-pos
'_currentOffset 0

@ -37,7 +37,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(define/augment (x:size . _) @size)))
(define (xint? x) (is-a? x x:int%))
(define (x:int? x) (is-a? x x:int%))
(define x:int%
(class x:number%

@ -57,7 +57,7 @@ https://github.com/mbutterick/restructure/blob/master/src/String.coffee
(define encoded-length (bytes-length encoded-str))
(when (and (exact-nonnegative-integer? @len) (> encoded-length @len))
(raise-argument-error 'xstring-encode (format "string no longer than ~a" @len) val))
(when (xint? @len)
(when (x:int? @len)
(send @len x:encode encoded-length port parent))
(define string-terminator (if (not @len) (bytes 0) (bytes))) ; null terminated when no len
(bytes-append encoded-str string-terminator))
@ -74,7 +74,7 @@ https://github.com/mbutterick/restructure/blob/master/src/String.coffee
(define string-size (bytes-length (encode-string val encoding)))
(define strlen-size (cond
[(not @len) 1]
[(xint? @len) (send @len x:size)]
[(x:int? @len) (send @len x:size)]
[else 0]))
(+ string-size strlen-size)]
[else (resolve-length @len #f #:parent parent)]))))

@ -11,5 +11,5 @@
[(exact-nonnegative-integer? x) x]
[(procedure? x) (x parent)]
[(and parent (symbol? x)) (dict-ref parent x)]
[(and port (xint? x)) (decode x port)]
[(and port (x:int? x)) (decode x port)]
[else (raise-argument-error 'resolve-length "fixed-size argument" x)]))
Loading…
Cancel
Save