Always close stream in with-input-from-string & Cosmetic changes

The Common Lisp Standard defines that with-input-from-string always closes the
input stream that it creates. But with the current definition it would only be
closed in the case of an index variable being provided.

This change wraps the code for both cases (index given or not) in a unified
unwind-protect form, updates the index if present, and always closes the
stream. It also unifies the processing of declarations.

Downcase code & fix indentation:

Set the uppercased code in lower case to harmonise appearance and increase
readability. Differentiated indentation for the first forms in unwind-protect
and multiple-value-prog1 helps to understand their special meaning.
This commit is contained in:
Moritz Petersen 2020-04-14 13:21:20 +02:00
parent 1d7551c773
commit 72560efa5a

View file

@ -34,18 +34,17 @@ automatically closed on exit."
Evaluates FORMs with VAR bound to a string input stream from the string that
is the value of STRING-FORM. The stream is automatically closed on exit.
Possible keywords are :INDEX, :START, and :END."
(if index
(multiple-value-bind (ds b)
(find-declarations body)
`(LET ((,var (MAKE-STRING-INPUT-STREAM ,string ,start ,end)))
`(let ((,var (make-string-input-stream ,string ,start ,end)))
,@ds
(UNWIND-PROTECT
(MULTIPLE-VALUE-PROG1
(PROGN ,@b)
(SETF ,index (FILE-POSITION ,var)))
(CLOSE ,var))))
`(LET ((,var (MAKE-STRING-INPUT-STREAM ,string ,start ,end)))
,@body)))
(unwind-protect
,(if index
`(multiple-value-prog1
(progn ,@b)
(setf ,index (file-position ,var)))
`(progn ,@b))
(close ,var)))))
(defmacro with-output-to-string ((var &optional string &rest r &key element-type) &rest body)
"Syntax: (with-output-to-string (var [string-form]) {decl}* {form}*)