(external-format :default)
;; fixed width, or function to call with a character
(char-size 1 :type (or fixnum function))
- (output-bytes #'ill-out :type function))
+ (output-bytes #'ill-out :type function)
+ ;; a boolean indicating whether the stream is bivalent. For
+ ;; internal use only.
+ (bivalent-p nil :type boolean))
(def!method print-object ((fd-stream fd-stream) stream)
(declare (type stream stream))
(print-unreadable-object (fd-stream stream :type t :identity t)
(do-listen)))))))
(do-listen)))
(:unread
- (setf (fd-stream-unread fd-stream) arg1)
+ ;; If the stream is bivalent, the user might follow an
+ ;; unread-char with a read-byte. In this case, the bookkeeping
+ ;; is simpler if we adjust the buffer head by the number of code
+ ;; units in the character.
+ ;; FIXME: there has to be a proper way to check for bivalence,
+ ;; right?
+ (if (fd-stream-bivalent-p fd-stream)
+ (decf (buffer-head (fd-stream-ibuf fd-stream))
+ (fd-stream-character-size fd-stream arg1))
+ (setf (fd-stream-unread fd-stream) arg1))
(setf (fd-stream-listen fd-stream) t))
(:close
;; Drop input buffers
:buffering buffering
:dual-channel-p dual-channel-p
:external-format external-format
+ :bivalent-p (eq element-type :default)
:char-size (external-format-char-size external-format)
:timeout
(if timeout
(assert (= 3 (read-sequence buffer s :start 2 :end 3)))
(file-position s :end)
(assert (= 3 (read-sequence buffer s :start 3))))))
+
+;;; In 1.0.27 (and also 0.9.16; presumably in between, too), binary
+;;; input operations on a bivalent stream did something bad after
+;;; unread-char: READ-BYTE would return the character, and
+;;; READ-SEQUENCE into a byte buffer would lose when attempting to
+;;; store the character in the vector.
+(let ((pathname "bivalent-stream-unread-char-test.tmp"))
+ (with-open-file (s pathname
+ :element-type :default
+ :direction :io :if-exists :rename)
+ (write-char #\a s)
+ (file-position s :start)
+ (unread-char (read-char s) s)
+ (assert (integerp (read-byte s))))
+ (delete-file pathname))
+
+(let ((pathname "bivalent-stream-unread-char-test.tmp"))
+ (with-open-file (s pathname
+ :element-type :default
+ :direction :io :if-exists :rename)
+ (write-char #\a s)
+ (file-position s :start)
+ (unread-char (read-char s) s)
+ (assert (let ((buffer (make-array 10 :element-type '(unsigned-byte 8))))
+ (read-sequence buffer s))))
+ (delete-file pathname))
+
+#+sb-unicode
+(let ((pathname "bivalent-stream-unread-char-test.tmp"))
+ (with-open-file (s pathname
+ :element-type :default
+ :direction :io :if-exists :rename
+ :external-format :utf8)
+ (write-char (code-char 192) s)
+ (file-position s :start)
+ (unread-char (read-char s) s)
+ (assert (integerp (read-byte s))))
+ (delete-file pathname))
+
+#+sb-unicode
+(let ((pathname "bivalent-stream-unread-char-test.tmp"))
+ (with-open-file (s pathname
+ :element-type :default
+ :direction :io :if-exists :rename
+ :external-format :utf8)
+ (write-char (code-char 192) s)
+ (file-position s :start)
+ (unread-char (read-char s) s)
+ (assert (let ((buffer (make-array 10 :element-type '(unsigned-byte 8))))
+ (read-sequence buffer s))))
+ (delete-file pathname))
\f
;;; success