add byte list functions for humans
[rrq/lsp-utils.git] / lsp-misc / misc.lsp
1 (define (prog1 X) X)
2 (global 'prog1)
3
4 (define (die N)
5   (when (args) (write-line 2 (join (map string (args)) " ")))
6   (and N (exit N)))
7 (global 'die)
8
9 ;; Print binary byte as octal or as ASCII character [32-126]
10 (define (octal-byte x)
11   (if (and (> x 31) (< x 127)) (char x) (format "\\%o" x)))
12
13 ;; Print string as binary octals
14 (define (octals-string S)
15   (join (map octal-byte (unpack (dup "b" (length S)) S))))
16
17 ;; Return byte code as printable or as code.
18 (define (human-byte B)
19   (if (and (> B 32) (< B 127)) (char B) B))
20
21 ;; Return a packed encoding of a list of bytes, joining string elements
22 (define (human-bytes BL)
23   (let ((OUT '()) (X nil))
24     (dolist (B (map human-byte BL))
25       (if (string? B) (if X (extend X B) (setf X B))
26         (begin (when (string? X) (push X OUT -1))
27              (push B OUT -1)
28              (setf X nil))))
29     (when (string? X) (push X OUT -1))
30     OUT))
31       
32 (global 'octals-string 'octal-byte 'human-byte 'human-bytes)