-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitutils.rkt
43 lines (36 loc) · 1.12 KB
/
bitutils.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#lang racket/base
(require (for-syntax racket/base)
racket/string
racket/format
racket/list
racket/require)
(require (filtered-in
(λ (name) (regexp-replace #rx"unsafe-" name ""))
racket/unsafe/ops))
(provide fx+multi 8bit+ 16bit+
dumpbytes)
(define-syntax fx+multi
(syntax-rules ()
[(fx+multi x y) (fx+ x y)]
[(fx+multi x y rest ...) (fx+multi (fx+ x y) rest ...)]))
(define-syntax 8bit+
(syntax-rules ()
[(8bit+ b ...)
(fxand #xff (fx+multi b ...))]))
(define-syntax 16bit+
(syntax-rules ()
[(16bit+ b ...)
(fxand #xffff (fx+multi b ...))]))
(define (dumpbytes bs [trim-leading-zeros #t])
(define (hex bs)
(string-join (map (lambda (b)
(~r b #:base 16 #:min-width 2 #:pad-string "0"))
bs)
" "))
(if trim-leading-zeros
(let* ([l (bytes->list bs)]
[dpl (dropf l zero?)]
[zs (- (length l) (length dpl) 1)])
(string-append (if (> zs 0) (format "<0 ... ~a> " zs) "")
(hex dpl)))
(hex (bytes->list bs))))