Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: read-from-stream #130

Open
Ambrevar opened this issue Jun 14, 2022 · 1 comment
Open

Proposal: read-from-stream #130

Ambrevar opened this issue Jun 14, 2022 · 1 comment

Comments

@Ambrevar
Copy link

I found myself using this 3-liner over and over again:

(defun read-from-stream (stream)
  "Return a list of all s-expressions read in STREAM."
  (loop :for value := (read stream nil stream)
        :until (eq value stream)
        :collect value))

Considering the standard already has read-from-string, it's only logical to have the STREAM counterpart. With the small difference that it loops over all s-exps and returns the list of them.

Wanna add it to Serapeum?

@Ambrevar
Copy link
Author

Ambrevar commented Jul 6, 2022

Actually I realized that uiop has uiop:slurp-stream-forms and uiop:safe-read-from-string.

So here are the missing parts:

(defun safe-read (&optional
                    (input-stream *standard-input*)
                    (eof-error-p t)
                    (eof-value nil)
                    (recursive-p nil))
  "Like `read' with standard IO syntax but does not accept reader macros ('#.').
UIOP has `uiop:safe-read-from-string' but no `read' equivalent.
This is useful if you do not trust the input."
  (let ((package *package*))
    (uiop:with-safe-io-syntax (:package package)
      (read input-stream eof-error-p eof-value recursive-p))))

(defun safe-slurp-stream-forms (stream)
  "Like `uiop:slurp-stream-forms' but wrapped in `uiop:with-safe-io-syntax' and
package set to current package."
  (uiop:with-safe-io-syntax (:package *package*)
    (uiop:slurp-stream-forms stream)))

But maybe it'd be better to add it to UIOP directly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant