From 056c69dee301f346d172293b71dfdc66ddfa0282 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Tue, 22 Jan 2013 10:12:59 +0100 Subject: [PATCH] add read-all and read-all! to (ice-9 rdelim) * module/ice-9/rdelim.scm (read-all!, read-all): New functions. * test-suite/tests/rdelim.test: Add tests. --- module/ice-9/rdelim.scm | 52 +++++++++++++++++++++++++++++++++++++- test-suite/tests/rdelim.test | 57 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 3 deletions(-) diff --git a/module/ice-9/rdelim.scm b/module/ice-9/rdelim.scm index c6ab2ba..9938942 100644 --- a/module/ice-9/rdelim.scm +++ b/module/ice-9/rdelim.scm @@ -1,6 +1,6 @@ ;;; installed-scm-file -;;;; Copyright (C) 1997, 1999, 2000, 2001, 2006, 2010 Free Software Foundation, Inc. +;;;; Copyright (C) 1997, 1999, 2000, 2001, 2006, 2010, 2013 Free Software Foundation, Inc. ;;;; ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -26,6 +26,8 @@ read-line! read-delimited read-delimited! + read-all + read-all! %read-delimited! %read-line write-line)) @@ -114,6 +116,54 @@ (else (error "unexpected handle-delim value: " handle-delim))))))))) +(define-syntax-rule (check-arg arg exp message) + (unless exp + (error message arg))) + +(define (index? n) + (and (integer? n) (exact? n) (>= n 0))) + +(define* (read-all! buf #:optional + (port (current-input-port)) + (start 0) (end (string-length buf))) + "Read all of the characters out of PORT and write them to BUF. +Returns the number of characters read. + +This function only reads out characters from PORT if it will be able to +write them to BUF. That is to say, if BUF is smaller than the number of +available characters, then BUF will be filled, and characters will be +left in the port." + (check-arg buf (string? buf) "not a string") + (check-arg start (index? start) "bad index") + (check-arg end (index? end) "bad index") + (check-arg start (<= start end) "start beyond end") + (check-arg end (<= end (string-length buf)) "end beyond string length") + (let lp ((n start)) + (if (< n end) + (let ((c (read-char port))) + (if (eof-object? c) + (- n start) + (begin + (string-set! buf n c) + (lp (1+ n))))) + (- n start)))) + +(define* (read-all #:optional (port (current-input-port))) + "Read all of the characters out of PORT and return them as a string." + (let loop ((substrings '()) + (total-chars 0) + (buf-size 100)) ; doubled each time through. + (let* ((buf (make-string buf-size)) + (nchars (read-all! buf port)) + (new-total (+ total-chars nchars))) + (cond + ((= nchars buf-size) + ;; buffer filled. + (loop (cons buf substrings) new-total (* buf-size 2))) + (else + (string-concatenate-reverse + (cons (substring buf 0 nchars) substrings))))))) + ;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string ;;; from PORT. The return value depends on the value of HANDLE-DELIM, ;;; which may be one of the symbols `trim', `concat', `peek' and diff --git a/test-suite/tests/rdelim.test b/test-suite/tests/rdelim.test index e61fc92..a102df6 100644 --- a/test-suite/tests/rdelim.test +++ b/test-suite/tests/rdelim.test @@ -1,7 +1,7 @@ ;;;; rdelim.test --- Delimited I/O. -*- mode: scheme; coding: utf-8; -*- ;;;; Ludovic Courtès ;;;; -;;;; Copyright (C) 2011 Free Software Foundation, Inc. +;;;; Copyright (C) 2011, 2013 Free Software Foundation, Inc. ;;;; ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -189,7 +189,60 @@ (pass-if "eof, split" (eof-object? (read-delimited! ":" (make-string 7) - (open-input-string "")))))) + (open-input-string ""))))) + + (with-test-prefix "read-all" + + (pass-if "short string" + (let* ((s "hello, world!") + (p (open-input-string s))) + (and (string=? (read-all p) s) + (string=? (read-all p) "")))) + + (pass-if "100 chars" + (let* ((s (make-string 100 #\space)) + (p (open-input-string s))) + (and (string=? (read-all p) s) + (string=? (read-all p) "")))) + + (pass-if "longer than 100 chars" + (let* ((s (string-concatenate (make-list 20 "hello, world!"))) + (p (open-input-string s))) + (and (string=? (read-all p) s) + (string=? (read-all p) ""))))) + + (with-test-prefix "read-all!" + + (pass-if "buf smaller" + (let* ((s "hello, world!") + (len (1- (string-length s))) + (buf (make-string len #\.)) + (p (open-input-string s))) + (and (= (read-all! buf p) len) + (string=? buf (substring s 0 len)) + (= (read-all! buf p) 1) + (string=? (substring buf 0 1) (substring s len))))) + + (pass-if "buf right size" + (let* ((s "hello, world!") + (len (string-length s)) + (buf (make-string len #\.)) + (p (open-input-string s))) + (and (= (read-all! buf p) len) + (string=? buf (substring s 0 len)) + (= (read-all! buf p) 0) + (string=? buf (substring s 0 len))))) + + (pass-if "buf bigger" + (let* ((s "hello, world!") + (len (string-length s)) + (buf (make-string (1+ len) #\.)) + (p (open-input-string s))) + (and (= (read-all! buf p) len) + (string=? (substring buf 0 len) s) + (= (read-all! buf p) 0) + (string=? (substring buf 0 len) s) + (string=? (substring buf len) ".")))))) ;;; Local Variables: ;;; eval: (put 'with-test-prefix 'scheme-indent-function 1) -- 1.7.10.4