change startOffset key name

main
Matthew Butterick 6 years ago
parent 51db49a10b
commit a0076963fd

@ -28,7 +28,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(define/augride (x:decode port parent)
(define new-parent (if (x:int? @len)
(mhasheq 'parent parent
'_startOffset (pos port)
x:start-offset-key (pos port)
'_currentOffset 0
'_length @len)
parent))
@ -40,7 +40,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
[len (+ (pos port) len)]
;; no resolved-len, but parent has length
[(and parent (not (zero? (dict-ref parent '_length))))
(+ (dict-ref parent '_startOffset) (dict-ref parent '_length))]
(+ (dict-ref parent x:start-offset-key) (dict-ref parent '_length))]
;; no resolved-len or parent, so consume whole stream
[else +inf.0]))
(for/list ([i (in-naturals)]

@ -6,7 +6,10 @@
"generic.rkt")
(provide (all-defined-out))
(define x:version-key 'x:version)
(define x:version-key 'x:version)
(define x:start-offset-key 'x:start-offset)
(define private-keys (list 'parent x:start-offset-key '_currentOffset '_length))
(define (dict-ref* d . keys)
(for/fold ([d d])

@ -18,7 +18,7 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
(define len (resolve-length @len port parent))
(define new-parent (if (x:int? @len)
(mhasheq 'parent parent
'_startOffset starting-pos
x:start-offset-key starting-pos
'_currentOffset 0
'_length @len)
parent))

@ -39,10 +39,10 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
[(and @allow-null? (= offset @null-value)) #f] ; handle null pointers
[else
(define relative (+ (case @pointer-relative-to
[(local) (dict-ref parent '_startOffset)]
[(local) (dict-ref parent x:start-offset-key)]
[(immediate) (- (pos port) (send @offset-type x:size))]
[(parent) (dict-ref (dict-ref parent 'parent) '_startOffset)]
[(global) (or (dict-ref (find-top-parent parent) '_startOffset) 0)]
[(parent) (dict-ref (dict-ref parent 'parent) x:start-offset-key)]
[(global) (or (dict-ref (find-top-parent parent) x:start-offset-key) 0)]
[else (error 'unknown-pointer-style)])))
(define ptr (+ offset relative))
(cond

@ -17,7 +17,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define mheq (make-hasheq))
(dict-set*! mheq
'parent parent
'_startOffset (pos port)
x:start-offset-key (pos port)
'_currentOffset 0
'_length len)
mheq)
@ -33,11 +33,9 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(send type x:decode port sdr)))
(unless (void? val)
(dict-set! sdr key val))
(dict-set! sdr '_currentOffset (- (pos port) (dict-ref sdr '_startOffset)))
(dict-set! sdr '_currentOffset (- (pos port) (dict-ref sdr x:start-offset-key)))
sdr))
(define private-keys '(parent _startOffset _currentOffset _length))
(define (dict->mutable-hash x)
(define h (make-hasheq))
(for ([(k v) (in-dict x)]

@ -5,6 +5,7 @@
"../number.rkt"
"../pointer.rkt"
"../generic.rkt"
"../helper.rkt"
sugar/unstable/dict)
#|
@ -61,12 +62,12 @@ https://github.com/mbutterick/restructure/blob/master/test/Array.coffee
(test-case
"array: decode to the end of parent if no length given"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (x:array #:type uint8) (current-input-port) #:parent (mhash '_length 4 '_startOffset 0)) '(1 2 3 4))))
(check-equal? (decode (x:array #:type uint8) (current-input-port) #:parent (mhash '_length 4 x:start-offset-key 0)) '(1 2 3 4))))
(test-case
"array: decode to the end of the stream if parent exists, but its length is 0"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (x:array #:type uint8) (current-input-port) #:parent (mhash '_length 0 '_startOffset 0)) '(1 2 3 4 5))))
(check-equal? (decode (x:array #:type uint8) (current-input-port) #:parent (mhash '_length 0 x:start-offset-key 0)) '(1 2 3 4 5))))
(test-case
"array: decode to the end of the stream if no parent and length is given"

@ -18,12 +18,12 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(test-case
"pointer: decode should handle null pointers"
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(check-false (decode (x:pointer) #:parent (mhash '_startOffset 50)))))
(check-false (decode (x:pointer) #:parent (mhash x:start-offset-key 50)))))
(test-case
"pointer: decode should use local offsets from start of parent by default"
(parameterize ([current-input-port (open-input-bytes (bytes 1 53))])
(check-equal? (decode (x:pointer) #:parent (mhash '_startOffset 0)) 53)))
(check-equal? (decode (x:pointer) #:parent (mhash x:start-offset-key 0)) 53)))
(test-case
"pointer: decode should support immediate offsets"
@ -34,19 +34,19 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
"pointer: decode should support offsets relative to the parent"
(parameterize ([current-input-port (open-input-bytes (bytes 0 0 1 53))])
(pos (current-input-port) 2)
(check-equal? (decode (x:pointer #:relative-to 'parent) #:parent (mhash 'parent (mhash '_startOffset 2))) 53)))
(check-equal? (decode (x:pointer #:relative-to 'parent) #:parent (mhash 'parent (mhash x:start-offset-key 2))) 53)))
(test-case
"pointer: decode should support global offsets"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 4 0 0 0 53))])
(pos (current-input-port) 2)
(check-equal? (decode (x:pointer #:relative-to 'global) #:parent (mhash 'parent (mhash 'parent (mhash '_startOffset 2))))
(check-equal? (decode (x:pointer #:relative-to 'global) #:parent (mhash 'parent (mhash 'parent (mhash x:start-offset-key 2))))
53)))
(test-case
"pointer: decode should support returning pointer if there is no decode type"
(parameterize ([current-input-port (open-input-bytes (bytes 4))])
(check-equal? (decode (x:pointer uint8 'void) #:parent (mhash '_startOffset 0)) 4)))
(check-equal? (decode (x:pointer uint8 'void) #:parent (mhash x:start-offset-key 0)) 4)))
(test-case
"pointer: decode should support decoding pointers lazily"

Loading…
Cancel
Save