first
This commit is contained in:
parent
97749f8074
commit
bff224550a
BIN
printf.fasl
Normal file
BIN
printf.fasl
Normal file
Binary file not shown.
38
printf.lisp
Normal file
38
printf.lisp
Normal 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))))
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user