From: Ralph Ronnquist Date: Sun, 16 Apr 2023 01:37:26 +0000 (+1000) Subject: add byte list functions for humans X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=1dd263248af0120fa0ab167f69f13f44adb18391;hp=ca28eb67318874b6747878c9c33b4d79a53d5779;p=rrq%2Flsp-utils.git add byte list functions for humans --- diff --git a/lsp-misc/misc.lsp b/lsp-misc/misc.lsp index f60d0d3..5b72484 100644 --- a/lsp-misc/misc.lsp +++ b/lsp-misc/misc.lsp @@ -13,4 +13,20 @@ ;; Print string as binary octals (define (octals-string S) (join (map octal-byte (unpack (dup "b" (length S)) S)))) -(global 'octals-string 'octal-byte) + +;; Return byte code as printable or as code. +(define (human-byte B) + (if (and (> B 32) (< B 127)) (char B) B)) + +;; Return a packed encoding of a list of bytes, joining string elements +(define (human-bytes BL) + (let ((OUT '()) (X nil)) + (dolist (B (map human-byte BL)) + (if (string? B) (if X (extend X B) (setf X B)) + (begin (when (string? X) (push X OUT -1)) + (push B OUT -1) + (setf X nil)))) + (when (string? X) (push X OUT -1)) + OUT)) + +(global 'octals-string 'octal-byte 'human-byte 'human-bytes)