continue cleanup
parent
665ce5d36f
commit
5c879ccb92
@ -1,48 +1,47 @@
|
||||
#lang restructure/racket
|
||||
(require "number.rkt" "utils.rkt" "streamcoder.rkt")
|
||||
(provide RArray)
|
||||
(require "number.rkt" "utils.rkt" "stream.rkt")
|
||||
(provide (all-defined-out))
|
||||
|
||||
#|
|
||||
approximates
|
||||
https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
|
||||
|#
|
||||
|
||||
(define-subclass RStreamcoder (RArray type [length #f] [lengthType 'count])
|
||||
(define-subclass Streamcoder (Array type [_length #f] [lengthType 'count])
|
||||
|
||||
(define/augment (decode stream [parent #f])
|
||||
(let ([length (cond
|
||||
[length
|
||||
(resolveLength length stream parent)]
|
||||
[else
|
||||
(define num (send stream length))
|
||||
(define denom (send type size))
|
||||
(unless (andmap (λ (x) (and x (number? x))) (list num denom))
|
||||
(raise-argument-error 'RArray:decode "valid length and size" (list num denom)))
|
||||
;; implied length: length of stream divided by size of item
|
||||
(floor (/ (send stream length) (send type size)))])])
|
||||
(let ([len (cond
|
||||
;; explicit length
|
||||
[_length (resolveLength _length stream parent)]
|
||||
[else ;; implicit length: length of stream divided by size of item
|
||||
(define num (send stream length))
|
||||
(define denom (send type size))
|
||||
(unless (andmap (λ (x) (and x (number? x))) (list num denom))
|
||||
(raise-argument-error 'Array:decode "valid length and size" (list num denom)))
|
||||
(floor (/ (send stream length) (send type size)))])])
|
||||
|
||||
(caseq lengthType
|
||||
[(count) (for/list ([i (in-range length)])
|
||||
(send type decode stream this))])))
|
||||
[(count) (for/list ([i (in-range len)])
|
||||
(send type decode stream this))])))
|
||||
|
||||
(define/override (size array)
|
||||
(for/sum ([item (in-list array)])
|
||||
(send type size)))
|
||||
(unless (list? array) (raise-argument-error 'Array:size "list" array))
|
||||
(* (send type size) (length array)))
|
||||
|
||||
(define/augment (encode stream array [parent #f])
|
||||
(unless (list? array) (raise-argument-error 'Array:encode "list" array))
|
||||
(for ([item (in-list array)])
|
||||
(send type encode stream item))))
|
||||
(send type encode stream item))))
|
||||
|
||||
|
||||
(test-module
|
||||
(require "decodestream.rkt" "encodestream.rkt")
|
||||
(define stream (make-object RDecodeStream #"ABCDEFG"))
|
||||
(define stream (+DecodeStream #"ABCDEFG"))
|
||||
|
||||
(define A (make-object RArray uint16be 3))
|
||||
(define A (+Array uint16be 3))
|
||||
(check-equal? (send A decode stream) '(16706 17220 17734))
|
||||
(define os (make-object REncodeStream))
|
||||
(define os (+EncodeStream))
|
||||
(send A encode os '(16706 17220 17734))
|
||||
(check-equal? (send os dump) #"ABCDEF")
|
||||
|
||||
(check-equal? (send (make-object RArray uint16be) size '(1 2 3)) 6)
|
||||
(check-equal? (send (make-object RArray doublebe) size '(1 2 3 4 5)) 40))
|
||||
(check-equal? (send (+Array uint16be) size '(1 2 3)) 6)
|
||||
(check-equal? (send (+Array doublebe) size '(1 2 3 4 5)) 40))
|
@ -1,40 +1,46 @@
|
||||
#lang restructure/racket
|
||||
(require "number.rkt" "utils.rkt" "streamcoder.rkt")
|
||||
(provide RString)
|
||||
(require "number.rkt" "utils.rkt" "stream.rkt")
|
||||
(provide (all-defined-out))
|
||||
|
||||
#|
|
||||
approximates
|
||||
https://github.com/mbutterick/restructure/blob/master/src/String.coffee
|
||||
|#
|
||||
|
||||
(define-subclass RStreamcoder (RString [length #f] [encoding 'ascii])
|
||||
(field [_codec (caseq encoding
|
||||
[(latin-1 ascii) (cons string->bytes/latin-1 bytes->string/latin-1)]
|
||||
[(utf-8 utf8)(cons string->bytes/utf-8 bytes->string/utf-8)])])
|
||||
(struct $codec (encoder decoder) #:transparent)
|
||||
|
||||
(define-subclass Streamcoder (String [strlen #f] [encoding 'ascii])
|
||||
(field [codec (caseq encoding
|
||||
[(latin-1 ascii) ($codec string->bytes/latin-1 bytes->string/latin-1)]
|
||||
[(utf-8 utf8) ($codec string->bytes/utf-8 bytes->string/utf-8)])])
|
||||
|
||||
(define/augment (decode stream [parent #f])
|
||||
(define count (if length
|
||||
(resolveLength length stream parent)
|
||||
(define count (if strlen
|
||||
(resolveLength strlen stream parent)
|
||||
(send stream length)))
|
||||
(define bytes (send stream read count))
|
||||
((cdr _codec) bytes))
|
||||
(($codec-decoder codec) bytes))
|
||||
|
||||
(define/augment (encode stream val [parent #f])
|
||||
(define bytes ((car _codec) (format "~a" val)))
|
||||
(define bytes (($codec-encoder codec) (format "~a" val)))
|
||||
|
||||
(when (is-a? length Number) ;; length-prefixed string
|
||||
(send length encode stream (bytes-length bytes)))
|
||||
(when (Number? strlen) ;; length-prefixed string
|
||||
(send strlen encode stream (bytes-length bytes)))
|
||||
|
||||
(send stream write bytes))
|
||||
|
||||
(define/override (size) (unfinished)))
|
||||
(define/override (size str)
|
||||
(define es (+EncodeStream))
|
||||
(encode es str)
|
||||
(bytes-length (send es dump))))
|
||||
|
||||
|
||||
(test-module
|
||||
(require "decodestream.rkt" "encodestream.rkt")
|
||||
(define stream (make-object RDecodeStream #"\2BCDEF"))
|
||||
(define S (make-object RString uint8 'utf8))
|
||||
(require "stream.rkt")
|
||||
(define stream (+DecodeStream #"\2BCDEF"))
|
||||
(define S (+String uint8 'utf8))
|
||||
(check-equal? (send S decode stream) "BC")
|
||||
(define os (make-object REncodeStream))
|
||||
(define os (+EncodeStream))
|
||||
(send S encode os "Mike")
|
||||
(check-equal? (send os dump) #"\4Mike"))
|
||||
(check-equal? (send os dump) #"\4Mike")
|
||||
(check-equal? (send (+String) size "foobar") 6))
|
@ -1,27 +1,3 @@
|
||||
#lang restructure/racket
|
||||
(require racket/dict "struct.rkt")
|
||||
(provide (all-defined-out))
|
||||
|
||||
#|
|
||||
approximates
|
||||
https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
|
||||
|#
|
||||
|
||||
(define-subclass RStruct (RVersionedStruct type [versions (dictify)])
|
||||
(define/override (decode stream [parent #f] [length 0] #:version [maybe-version #f])
|
||||
(define res (send this _setup stream parent length))
|
||||
(define version (cond
|
||||
[maybe-version] ; for testing purposes: pass an explicit version
|
||||
[(procedure? type) (type parent)]
|
||||
[(is-a? type RBase) (send type decode stream)]
|
||||
[else (raise-argument-error 'decode "way of finding version" type)]))
|
||||
(hash-set! res 'version version)
|
||||
(set-field! fields this (dict-ref versions version (λ () (raise-argument-error 'RVersionedStruct:decode "valid version key" version))))
|
||||
(send this make-key-index! (· this fields))
|
||||
(cond
|
||||
[(is-a? (· this fields) RVersionedStruct) (send (· this fields) decode stream parent)]
|
||||
[else
|
||||
(send this _parseFields stream res (· this fields))
|
||||
(send this process res stream)
|
||||
res]))
|
||||
)
|
Loading…
Reference in New Issue