X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=lsp-misc%2Fmisc.lsp;h=ff76d5a294b8dfd6b3e1da5e3e3669d4ae8d1429;hb=fdf17284e8a259e26d70461d9c11a153f9dcca93;hp=9e77db93efe06db5c81e050265abcc7f3dd561b7;hpb=2439953c058c792c3ce7a5ca419f2502c8f776d8;p=rrq%2Flsp-utils.git diff --git a/lsp-misc/misc.lsp b/lsp-misc/misc.lsp index 9e77db9..ff76d5a 100644 --- a/lsp-misc/misc.lsp +++ b/lsp-misc/misc.lsp @@ -1,3 +1,5 @@ +;; This module provides some global utility functions. + (define (prog1 X) X) (global 'prog1) @@ -6,8 +8,14 @@ (and N (exit N))) (global 'die) -(define (char2hex STR) - (join (map (curry format "%2x") (map char (explode STR))))) +;; Prepend with C onto S so as to fill width W, if it's a number. +(define (pre-fill C S W) + (if (and (number? W) (> (setf W (- W (length S))))) (string (dup C W) S) S)) +(global 'pre-fill) + +;; Make a hex string from a data block pad with "0" to W if non-nil +(define (char2hex STR W) + (pre-fill "0" (join (map (curry format "%2x") (map char (explode STR)))) W)) (global 'char2hex) ;; Print binary byte as octal or as ASCII character [32-126] @@ -20,12 +28,12 @@ (join (map octal-byte (unpack (dup "b" (length S)) S)))) (global 'octals-string) -;; Return byte code as printable or as code. +;; Return byte code as printable or as decimal number. (define (human-byte B) (if (and (> B 32) (< B 127)) (char B) B)) (global 'human-byte) -;; Return a packed encoding of a list of bytes, joining string elements +;; Return a packed encoding of a list of bytes, joining its string elements (define (human-bytes BL) (let ((OUT '()) (X nil)) (dolist (B (map human-byte BL))