diff --git a/printf.fasl b/printf.fasl new file mode 100644 index 0000000..169f68c Binary files /dev/null and b/printf.fasl differ diff --git a/printf.lisp b/printf.lisp new file mode 100644 index 0000000..14914b9 --- /dev/null +++ b/printf.lisp @@ -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)))) + ) + + + + + + +