mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-17 17:20:29 +02:00
Custom binary input ports support 'setvbuf'.
* libguile/r6rs-ports.c (CBIP_BUFFER_SIZE): Adjust comment. Set to 8KiB. (SCM_SET_CBIP_BYTEVECTOR): New macro. (cbip_setvbuf): New function. (make_cbip): Set PORT's 'setvbuf' internal field. (cbip_fill_input): Check whether PORT is buffered. When unbuffered, check whether BV can hold C_REQUESTED bytes, and allocate a new bytevector if not; copy the data back from BV to c_port->read_pos. Remove 'again' label, and don't loop there. * test-suite/tests/r6rs-ports.test ("7.2.7 Input Ports")["custom binary input port unbuffered & 'port-position'", "custom binary input port unbuffered & 'read!' calls", "custom binary input port, unbuffered then buffered", "custom binary input port, buffered then unbuffered"]: New tests. * doc/ref/api-io.texi (R6RS Binary Input): Document the buffering of custom binary input ports, and link to 'setvbuf'.
This commit is contained in:
parent
122f24cc8a
commit
8ca97482b0
3 changed files with 206 additions and 17 deletions
|
@ -456,6 +456,129 @@ not `set-port-position!'"
|
|||
(u8-list->bytevector
|
||||
(map char->integer (string->list "Port!")))))))
|
||||
|
||||
(pass-if-equal "custom binary input port unbuffered & 'port-position'"
|
||||
'(0 2 5 11)
|
||||
;; Check that the value returned by 'port-position' is correct, and
|
||||
;; that each 'port-position' call leads one call to the
|
||||
;; 'get-position' method.
|
||||
(let* ((str "Hello Port!")
|
||||
(output (make-bytevector (string-length str)))
|
||||
(source (with-fluids ((%default-port-encoding "UTF-8"))
|
||||
(open-string-input-port str)))
|
||||
(read! (lambda (bv start count)
|
||||
(let ((r (get-bytevector-n! source bv start count)))
|
||||
(if (eof-object? r)
|
||||
0
|
||||
r))))
|
||||
(pos '())
|
||||
(get-pos (lambda ()
|
||||
(let ((p (port-position source)))
|
||||
(set! pos (cons p pos))
|
||||
p)))
|
||||
(port (make-custom-binary-input-port "the port" read!
|
||||
get-pos #f #f)))
|
||||
(setvbuf port _IONBF)
|
||||
(and (= 0 (port-position port))
|
||||
(begin
|
||||
(get-bytevector-n! port output 0 2)
|
||||
(= 2 (port-position port)))
|
||||
(begin
|
||||
(get-bytevector-n! port output 2 3)
|
||||
(= 5 (port-position port)))
|
||||
(let ((bv (string->utf8 (get-string-all port))))
|
||||
(bytevector-copy! bv 0 output 5 (bytevector-length bv))
|
||||
(= (string-length str) (port-position port)))
|
||||
(bytevector=? output (string->utf8 str))
|
||||
(reverse pos))))
|
||||
|
||||
(pass-if-equal "custom binary input port unbuffered & 'read!' calls"
|
||||
`((2 "He") (3 "llo") (42 " Port!"))
|
||||
(let* ((str "Hello Port!")
|
||||
(source (with-fluids ((%default-port-encoding "UTF-8"))
|
||||
(open-string-input-port str)))
|
||||
(reads '())
|
||||
(read! (lambda (bv start count)
|
||||
(set! reads (cons count reads))
|
||||
(let ((r (get-bytevector-n! source bv start count)))
|
||||
(if (eof-object? r)
|
||||
0
|
||||
r))))
|
||||
(port (make-custom-binary-input-port "the port" read!
|
||||
#f #f #f)))
|
||||
|
||||
(setvbuf port _IONBF)
|
||||
(let ((ret (list (get-bytevector-n port 2)
|
||||
(get-bytevector-n port 3)
|
||||
(get-bytevector-n port 42))))
|
||||
(zip (reverse reads)
|
||||
(map (lambda (obj)
|
||||
(if (bytevector? obj)
|
||||
(utf8->string obj)
|
||||
obj))
|
||||
ret)))))
|
||||
|
||||
(pass-if-equal "custom binary input port, unbuffered then buffered"
|
||||
`((6 "Lorem ") (12 "ipsum dolor ") (777 "sit amet, consectetur…")
|
||||
(777 ,(eof-object)))
|
||||
(let* ((str "Lorem ipsum dolor sit amet, consectetur…")
|
||||
(source (with-fluids ((%default-port-encoding "UTF-8"))
|
||||
(open-string-input-port str)))
|
||||
(reads '())
|
||||
(read! (lambda (bv start count)
|
||||
(set! reads (cons count reads))
|
||||
(let ((r (get-bytevector-n! source bv start count)))
|
||||
(if (eof-object? r)
|
||||
0
|
||||
r))))
|
||||
(port (make-custom-binary-input-port "the port" read!
|
||||
#f #f #f)))
|
||||
|
||||
(setvbuf port _IONBF)
|
||||
(let ((ret (list (get-bytevector-n port 6)
|
||||
(get-bytevector-n port 12)
|
||||
(begin
|
||||
(setvbuf port _IOFBF 777)
|
||||
(get-bytevector-n port 42))
|
||||
(get-bytevector-n port 42))))
|
||||
(zip (reverse reads)
|
||||
(map (lambda (obj)
|
||||
(if (bytevector? obj)
|
||||
(utf8->string obj)
|
||||
obj))
|
||||
ret)))))
|
||||
|
||||
(pass-if-equal "custom binary input port, buffered then unbuffered"
|
||||
`((18
|
||||
42 14 ; scm_c_read tries to fill the 42-byte buffer
|
||||
42)
|
||||
("Lorem " "ipsum dolor " "sit amet, consectetur bla…" ,(eof-object)))
|
||||
(let* ((str "Lorem ipsum dolor sit amet, consectetur bla…")
|
||||
(source (with-fluids ((%default-port-encoding "UTF-8"))
|
||||
(open-string-input-port str)))
|
||||
(reads '())
|
||||
(read! (lambda (bv start count)
|
||||
(set! reads (cons count reads))
|
||||
(let ((r (get-bytevector-n! source bv start count)))
|
||||
(if (eof-object? r)
|
||||
0
|
||||
r))))
|
||||
(port (make-custom-binary-input-port "the port" read!
|
||||
#f #f #f)))
|
||||
|
||||
(setvbuf port _IOFBF 18)
|
||||
(let ((ret (list (get-bytevector-n port 6)
|
||||
(get-bytevector-n port 12)
|
||||
(begin
|
||||
(setvbuf port _IONBF)
|
||||
(get-bytevector-n port 42))
|
||||
(get-bytevector-n port 42))))
|
||||
(list (reverse reads)
|
||||
(map (lambda (obj)
|
||||
(if (bytevector? obj)
|
||||
(utf8->string obj)
|
||||
obj))
|
||||
ret)))))
|
||||
|
||||
(pass-if "custom binary input port `close-proc' is called"
|
||||
(let* ((closed? #f)
|
||||
(read! (lambda (bv start count) 0))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue