Skip to content

Commit

Permalink
add peek-byte for in-memory streams.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chream authored and stassats committed Dec 12, 2018
1 parent c905079 commit d2111fd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
38 changes: 38 additions & 0 deletions in-memory.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,44 @@ associated vector."))
(declare #.*standard-optimize-settings*)
'octet)

(defgeneric peek-byte (stream &optional peek-type eof-err-p eof-value)
(:documentation
"PEEK-BYTE is like PEEK-CHAR, i.e. it returns a byte from the stream without
actually removing it. If PEEK-TYPE is NIL the next byte is returned, if
PEEK-TYPE is T, the next byte which is not 0 is returned, if PEEK-TYPE is an
byte, the next byte which equals PEEK-TYPE is returned. EOF-ERROR-P and
EOF-VALUE are interpreted as usual."))

(defmethod peek-byte ((stream vector-input-stream) &optional peek-type (eof-error-p t) eof-value)
"Returns a byte from VECTOR-INPUT-STREAM without actually removing it."
(declare #.*standard-optimize-settings*)
(let ((index (vector-stream-index stream)))
(loop :for byte = (read-byte stream eof-error-p :eof)
:for new-index :from index
:until (cond ((eq byte :eof)
(return eof-value))
((null peek-type))
((eq peek-type 't)
(plusp byte))
((= byte peek-type)))
:finally (setf (slot-value stream 'index) new-index)
(return byte))))

(defmethod peek-byte ((stream list-input-stream) &optional peek-type (eof-error-p t) eof-value)
"Returns a byte from VECTOR-INPUT-STREAM without actually removing it."
(declare #.*standard-optimize-settings*)
(loop
:for list-elem = (car (list-stream-list stream))
:for byte = (read-byte stream eof-error-p :eof)
:until (cond ((eq byte :eof)
(return eof-value))
((null peek-type))
((eq peek-type 't)
(plusp byte))
((= byte peek-type)))
:finally (push list-elem (list-stream-list stream))
(return byte)))

(defmethod transform-octet ((stream in-memory-stream) octet)
"Applies the transformer of STREAM to octet and returns the result."
(declare #.*standard-optimize-settings*)
Expand Down
9 changes: 2 additions & 7 deletions input.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,10 @@ READ-BYTE."
(decf (the integer position))
(push byte octet-stack)
nil))

(defmethod peek-byte ((flexi-input-stream flexi-input-stream)
&optional peek-type (eof-error-p t) eof-value)
"PEEK-BYTE is like PEEK-CHAR, i.e. it returns an octet from
FLEXI-INPUT-STREAM without actually removing it. If PEEK-TYPE is NIL
the next octet is returned, if PEEK-TYPE is T, the next octet which is
not 0 is returned, if PEEK-TYPE is an octet, the next octet which
equals PEEK-TYPE is returned. EOF-ERROR-P and EOF-VALUE are
interpreted as usual."
"Returns an octet from FLEXI-INPUT-STREAM without actually removing it."
(declare #.*standard-optimize-settings*)
(loop for octet = (read-byte flexi-input-stream eof-error-p :eof)
until (cond ((eq octet :eof)
Expand Down

0 comments on commit d2111fd

Please sign in to comment.