edicl / flexi-streams

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

add peek-byte for in-memory streams. #20

Closed alt-vena closed 5 years ago

alt-vena commented 6 years ago

peek-byte is not implemented for in-memory streams. I have added the methods.

stassats commented 6 years ago

Is it a good idea having virtually the same doc-string for every method? Maybe move it to a defgeneric.

stassats commented 6 years ago

And is it a good idea calling read-byte? Since these are lists and vectors, maybe you can just use the as such.

alt-vena commented 6 years ago

@stassats I will have a look at the doc-strings again. I see your point.

The reason I am calling read-byte and not aref etc is that there might be a transformer function or some other functionality when reading from the stream. This is also how it is implemented for normal flexi-streams.

stassats commented 6 years ago

read-sequence does not call read-byte repeatedly.

alt-vena commented 6 years ago

hm, you are correct. Have a look at the following though. read-byte calls the transformer on the read byte but read-sequence does not. Is this consistent at all?

(defmethod stream-read-byte ((stream list-input-stream))
  "Reads one byte by simply popping it off of the top of the list."
  (declare #.*standard-optimize-settings*)
  (check-if-open stream)
  (with-accessors ((list list-stream-list))
      stream
    (transform-octet stream (or (pop list) (return-from stream-read-byte :eof)))))

(defmethod stream-read-sequence ((stream list-input-stream) sequence start end &key)
  "Repeatedly pops elements from the list until it's empty."
  (declare #.*standard-optimize-settings*)
  (declare (fixnum start end))
  (with-accessors ((list list-stream-list))
      stream
    (loop for index of-type fixnum from start below end
          while list
          do (setf (elt sequence index) (pop list))
          finally (return index))))
alt-vena commented 6 years ago

Also, this is how peek-byte is implemented for normal flexi-streams:

https://github.com/edicl/flexi-streams/blob/d7745e3eb184754f25ec57f6b802b0d0925f7362/input.lisp#L277-L294

alt-vena commented 6 years ago

I made an attempt to fix up the doc-string. I defined a defgeneric in in-memory.lisp. Although this might not be the most natural place as in-memory streams are kind of ad-hoc to this library? At least I am not getting redefinition warnings like this.

edit: wrote in wrong PR..

mohe2015 commented 5 years ago

I would love to see this merged!