edicl / flexi-streams

Flexible bivalent streams for Common Lisp
http://edicl.github.io/flexi-streams/
55 stars 26 forks source link

peek-char method of flexi-streams:in-memory-stream #43

Open ccqpein opened 3 years ago

ccqpein commented 3 years ago

Hi guys, I am trying to run JSON parser (yason:parse) with flexi-streams:in-memory-stream directly, because (yason:parse) can accept string steam input directly.

Then SBCL gives me the error when I did it. After checking the code of yason::parse and the error message, I found there are two generic functions of SBCL that needed to be implemented: SB-GRAY:STREAM-READ-CHAR and SB-GRAY:STREAM-PEEK-CHAR

So I tried to implement by code:

(defmethod SB-GRAY:STREAM-READ-CHAR ((s flexi-streams:in-memory-stream))
  (code-char (read-byte s)))

(defmethod SB-GRAY:STREAM-PEEK-CHAR ((s flexi-streams:in-memory-stream))
  (let ((char (code-char (flexi-streams:peek-byte s))))
    char))

Then yason:parse works fine with flexi-streams:in-memory-stream (at least now).

Do you guys trying to implement peek-char for flexi-streams:in-memory-stream? My code just work, I am not sure I met all hidden details of flexi-streams:in-memory-stream. It would be nice if flexi-stream has peek-char natively.

Thanks

stassats commented 3 years ago

code-char doesn't seem right, though.

fjl commented 3 years ago

I think this issue should be closed. in-memory-stream is explicitly a binary stream with no support for characters. To get characters, a flexi-stream needs to be used on top of the in-memory-stream:

(flexi-streams:with-input-from-sequence (mem input-sequence)
  (let ((text (flexi-streams:make-flexi-stream mem :external-format :utf-8)))
    (yason:parse text)))