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.
typesetting/xenomorph/xenomorph/bitfield.rkt

89 lines
3.1 KiB
Racket

#lang racket/base
(require "base.rkt"
"int.rkt"
racket/class
racket/dict
5 years ago
racket/list
racket/contract
sugar/unstable/dict)
6 years ago
(provide (all-defined-out))
6 years ago
#|
approximates
https://github.com/mbutterick/restructure/blob/master/src/Bitfield.coffee
|#
6 years ago
(define x:bitfield%
(class x:base%
(super-new)
(init-field [(@type type)]
[(@flags flags)])
(let ([named-flags (filter values @flags)])
5 years ago
(unless (= (length named-flags) (length (remove-duplicates named-flags)))
(raise-argument-error 'x:bitfield% "no duplicates among flag names" named-flags)))
5 years ago
(when (> (length @flags) (* 8 (send @type x:size)))
(raise-argument-error 'x:bitfield% (format "~a flags or fewer (~a-byte bitfield)" (* 8 (send @type x:size)) (send @type x:size)) (length @flags)))
5 years ago
5 years ago
(define/augment (x:decode port parent)
(define val (send @type x:decode port))
5 years ago
(define flag-hash (mhash))
6 years ago
(for ([(flag idx) (in-indexed @flags)]
#:when flag)
5 years ago
(hash-set! flag-hash flag (bitwise-bit-set? val idx)))
flag-hash)
5 years ago
(define/augment (x:encode flag-hash port [parent #f])
(define invalid-flags
(for/list ([flag (in-hash-keys flag-hash)]
#:unless (member flag @flags))
flag))
(unless (null? invalid-flags)
(raise-argument-error 'encode (format "valid flag name ~v" @flags) invalid-flags))
6 years ago
(define bit-int (for/sum ([(flag idx) (in-indexed @flags)]
6 years ago
#:when (and flag (hash-ref flag-hash flag #f)))
5 years ago
(arithmetic-shift 1 idx)))
5 years ago
(send @type x:encode bit-int port))
5 years ago
(define/augment (x:size [val #f] [parent #f])
(send @type x:size))))
(define (x:bitfield? x) (is-a? x x:bitfield%))
(define/contract (x:bitfield
[type-arg #f]
[flag-arg #f]
#:type [type-kwarg uint8]
#:flags [flag-kwarg null]
#:pre-encode [pre-proc #f]
#:post-decode [post-proc #f]
#:base-class [base-class x:bitfield%])
(()
((or/c x:int? #false)
5 years ago
(listof any/c)
#:type (or/c x:int? #false)
5 years ago
#:flags (listof any/c)
#:pre-encode (or/c (any/c . -> . any/c) #false)
#:post-decode (or/c (any/c . -> . any/c) #false)
#:base-class (λ (c) (subclass? c x:bitfield%)))
. ->* .
x:bitfield?)
6 years ago
(define type (or type-arg type-kwarg))
(define flags (or flag-arg flag-kwarg))
(new (generate-subclass base-class pre-proc post-proc)
[type type]
[flags flags]))
6 years ago
6 years ago
(module+ test
(require rackunit "number.rkt" "base.rkt")
6 years ago
(define bfer (x:bitfield uint16be '(bold italic underline #f shadow condensed extended)))
6 years ago
(define bf (decode bfer #"\0\25"))
6 years ago
(check-equal? (length (hash-keys bf)) 6) ; omits #f flag
(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 'condensed))
(check-false (hash-ref bf 'extended))
6 years ago
(check-equal? (encode bfer bf #f) #"\0\25"))