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.
42 lines
1.3 KiB
Racket
42 lines
1.3 KiB
Racket
#lang restructure/racket
|
|
(require "stream.rkt")
|
|
(provide (all-defined-out))
|
|
|
|
#|
|
|
approximates
|
|
https://github.com/mbutterick/restructure/blob/master/src/Bitfield.coffee
|
|
|#
|
|
|
|
(define-subclass Streamcoder (Bitfield type [flags empty])
|
|
|
|
(define/augment (decode stream . args)
|
|
(for*/fold ([flag-hash (mhash)])
|
|
([val (in-value (send type decode stream))]
|
|
[(flag i) (in-indexed flags)])
|
|
(hash-set! flag-hash flag (bitwise-bit-set? val i))
|
|
flag-hash))
|
|
|
|
(define/override (size . args) (send type size))
|
|
|
|
(define/augment (encode stream flag-hash)
|
|
(define bitfield-int (for/sum ([(flag i) (in-indexed flags)]
|
|
#:when (hash-ref flag-hash flag))
|
|
(expt 2 i)))
|
|
(send type encode stream bitfield-int)))
|
|
|
|
|
|
(test-module
|
|
(require "number.rkt" "stream.rkt")
|
|
(define bfer (+Bitfield uint16be '(bold italic underline outline shadow condensed extended)))
|
|
(define bf (send bfer decode (+DecodeStream #"\0\25")))
|
|
(check-true (hash-ref bf 'bold))
|
|
(check-true (hash-ref bf 'underline))
|
|
(check-true (hash-ref bf 'shadow))
|
|
(check-false (hash-ref bf 'italic))
|
|
(check-false (hash-ref bf 'outline))
|
|
(check-false (hash-ref bf 'condensed))
|
|
(check-false (hash-ref bf 'extended))
|
|
|
|
(define os (+EncodeStream))
|
|
(send bfer encode os bf)
|
|
(check-equal? (send os dump) #"\0\25")) |