From: Ralph Ronnquist Date: Mon, 24 Apr 2023 05:27:04 +0000 (+1000) Subject: Adding pre-fill. Editorial X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=130e7ff320ade199cdedb1a26b2e4a5fb087d5fa;p=rrq%2Flsp-utils.git Adding pre-fill. Editorial --- 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))