-
chevron_right
FILL-POINTER-OUTPUT-STRING
Michał "phoe" Herda · Monday, 3 January, 2022 - 18:55
Someone noted that they'd like a stream that can append to an existing stream with a fill pointer, like the stream that with-output-to-string
can produce, except with indefinite extent. A little bit of Lisp hackery produced something that seems to work, even if greatly untested (yet).
;;; A fill pointer output stream with indefinite extent
(defclass fill-pointer-output-stream
(trivial-gray-streams:fundamental-character-output-stream)
((string :accessor fill-pointer-output-stream-string :initarg :string))
(:default-initargs :string (a:required-argument :string)))
(defmethod trivial-gray-streams:stream-line-column
((stream fill-pointer-output-stream)))
(defmethod trivial-gray-streams:stream-start-line-p
((stream fill-pointer-output-stream)))
(defmethod trivial-gray-streams:stream-write-char
((stream fill-pointer-output-stream) char)
(vector-push-extend char (fill-pointer-output-stream-string stream)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CL-USER> (let* ((string (make-array 0 :element-type 'character :fill-pointer 0))
(stream (make-instance 'fill-pointer-output-stream :string string)))
(write-string "asdf" stream)
(close stream)
string)
"asdf"