This commit is contained in:
Logen Kain 2020-03-26 05:29:26 -04:00
parent 97749f8074
commit bff224550a
2 changed files with 38 additions and 0 deletions

BIN
printf.fasl Normal file

Binary file not shown.

38
printf.lisp Normal file
View File

@ -0,0 +1,38 @@
;;;; Make a macro that is like C's printf statement
(defmacro printf (string &rest vars)
`(format t ,(printf-to-format string) ,@vars))
(defun replace-all (string part replacement &key (test #'char=))
"Returns a new string in which all the occurences of the part
is replaced with replacement."
(with-output-to-string (out)
(loop with part-length = (length part)
for old-pos = 0 then (+ pos part-length)
for pos = (search part string
:start2 old-pos
:test test)
do (write-string string out
:start old-pos
:end (or pos (length string)))
when pos do (write-string replacement out)
while pos)))
(defun printf-to-format (string &optional
(printf-format '("%s" "%d" "%x" "%f" "%u" "\n"))
(lisp-format '("~A" "~D" "~X" "~F" "~D" "~%")))
"Convert printf sytax to format syntax"
(cond ((NULL printf-format) (return-from printf-to-format string))
(T (printf-to-format (replace-all string
(car printf-format)
(car lisp-format))
(cdr printf-format)
(cdr lisp-format))))
)