Commit b9c0c454b663e44fa4488a5f512724181aa64c0a

Thomas de Grivel 2018-05-18T10:49:06

stream-discard-n

diff --git a/input-stream.lisp b/input-stream.lisp
index 861818c..64cbe53 100644
--- a/input-stream.lisp
+++ b/input-stream.lisp
@@ -27,6 +27,9 @@
   (:documentation "An error which is signalled when an input error
 occurs on a stream."))
 
+(defgeneric stream-discard-n (input-stream count)
+  (:documentation "Discard COUNT items from INPUT-STREAM."))
+
 (defgeneric stream-read (input-stream)
   (:documentation "Tries to read one element from STREAM.
 Returns two values : the element or NIL if read failed;
@@ -63,6 +66,19 @@ until END-ELEMENT is read. Returns two values :
   :EOF if end of file was reached
   :NON-BLOCKING if read would block."))
 
+(defmethod stream-discard-n (stream (count fixnum))
+  (check-if-open stream)
+  (loop
+     (unless (< 0 count)
+       (return (values 0 nil)))
+     (multiple-value-bind (element state) (stream-read stream)
+       (declare (ignore element))
+       (case state
+         ((nil) (decf count))
+         ((:eof) (return (values count :eof)))
+         ((:non-blocking) (return (values count :non-blocking)))
+         (otherwise (error 'stream-input-error :stream stream))))))
+
 (defmethod stream-read-sequence ((stream input-stream) seq
                                  &key (start 0) (end (length seq)))
   (check-if-open stream)
diff --git a/package.lisp b/package.lisp
index 093d266..f5c4025 100644
--- a/package.lisp
+++ b/package.lisp
@@ -77,6 +77,7 @@
    #:stream-blocking-p
    #:*stream-default-buffer-size*
    #:stream-discard-input-buffer
+   #:stream-discard-n
    #:stream-discard-output-buffer
    #:stream-element-type
    #:stream-closed-error