From a6c377f7d8a311b0ce4f9c5900b1c81c27b2d60c Mon Sep 17 00:00:00 2001 From: Andreas Rottmann Date: Sun, 13 Mar 2011 22:39:14 +0100 Subject: Add `get-string-n' and `get-string-n!' for R6RS ports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * libguile/r6rs-ports.c (scm_get_string_n_x): Implement `get-string-n!' in C for efficiency. * libguile/r6rs-ports.h: Add prototype for this function. * module/ice-9/binary-ports.scm: Export `get-string-n!'. * module/rnrs/io/ports.scm (get-string-n): Implement based on `get-string-n!'. Export both `get-string-n!' and `get-string-n'. * module/rnrs.scm: Also export these. * test-suite/tests/r6rs-ports.test (8.2.9 Textual input): Add a few tests for `get-string-n' and `get-string-n!'. Signed-off-by: Ludovic Courtès --- module/ice-9/binary-ports.scm | 1 + module/rnrs.scm | 3 ++- module/rnrs/io/ports.scm | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) (limited to 'module') diff --git a/module/ice-9/binary-ports.scm b/module/ice-9/binary-ports.scm index 63d09cf21..c07900b0d 100644 --- a/module/ice-9/binary-ports.scm +++ b/module/ice-9/binary-ports.scm @@ -37,6 +37,7 @@ get-bytevector-n! get-bytevector-some get-bytevector-all + get-string-n! put-u8 put-bytevector open-bytevector-output-port diff --git a/module/rnrs.scm b/module/rnrs.scm index 476a3ab2d..77090d0e1 100644 --- a/module/rnrs.scm +++ b/module/rnrs.scm @@ -182,7 +182,8 @@ make-custom-textual-output-port call-with-string-output-port flush-output-port put-string - get-char get-datum get-line get-string-all lookahead-char + get-char get-datum get-line get-string-all get-string-n get-string-n! + lookahead-char put-char put-datum put-string standard-input-port standard-output-port standard-error-port diff --git a/module/rnrs/io/ports.scm b/module/rnrs/io/ports.scm index d3a81b7c7..d3b16ac63 100644 --- a/module/rnrs/io/ports.scm +++ b/module/rnrs/io/ports.scm @@ -68,8 +68,9 @@ put-u8 put-bytevector ;; textual input - get-char get-datum get-line get-string-all lookahead-char - + get-char get-datum get-line get-string-all get-string-n get-string-n! + lookahead-char + ;; textual output put-char put-datum put-string @@ -386,6 +387,17 @@ return the characters accumulated in that port." (define (get-string-all port) (with-i/o-decoding-error (read-delimited "" port 'concat))) +(define (get-string-n port count) + "Read up to @var{count} characters from @var{port}. +If no characters could be read before encountering the end of file, +return the end-of-file object, otherwise return a string containing +the characters read." + (let* ((s (make-string count)) + (rv (get-string-n! port s 0 count))) + (cond ((eof-object? rv) rv) + ((= rv count) s) + (else (substring/shared s 0 rv))))) + (define (lookahead-char port) (with-i/o-decoding-error (peek-char port))) -- cgit v1.2.3