From: Andy Wingo <wingo@pobox.com>
To: ludo@gnu.org (Ludovic Courtès)
Cc: guile-devel@gnu.org
Subject: Re: read-all ?
Date: Tue, 22 Jan 2013 10:51:40 +0100 [thread overview]
Message-ID: <87k3r5y28j.fsf@pobox.com> (raw)
In-Reply-To: <877gn5zih2.fsf@pobox.com> (Andy Wingo's message of "Tue, 22 Jan 2013 10:15:37 +0100")
[-- Attachment #1: Type: text/plain, Size: 934 bytes --]
On Tue 22 Jan 2013 10:15, Andy Wingo <wingo@pobox.com> writes:
> Hi,
>
> On Sat 12 Jan 2013 22:22, ludo@gnu.org (Ludovic Courtès) writes:
>
>> Andy Wingo <wingo@pobox.com> skribis:
>>
>>> I find myself writing (read-delimited "" p) to slurp in a file as a
>>> string, but it's not a very straightforward way to say that.
>>>
>>> What about `read-all'? We could add it to `(ice-9 rdelim)', I guess.
>>> R6RS calls this `read-string-all'.
>>
>> Sounds like a good idea.
>
> Patch attached. I didn't update the docs because it wasn't clear to me
> that (ice-9 rdelim) is actually the right place to put it.
>
> What do you think? Should we perhaps put it in a new (ice-9 ports)?
> Are the names right?
>
> I started by writing it in C but I noticed the C was doing a very stupid
> get-and-set algorithm, so I figured it got no advantage and we should
> just write it in Scheme from the get-go.
>
> Andy
[-- Attachment #2: 0001-add-read-all-and-read-all-to-ice-9-rdelim.patch --]
[-- Type: text/x-diff, Size: 6340 bytes --]
From 056c69dee301f346d172293b71dfdc66ddfa0282 Mon Sep 17 00:00:00 2001
From: Andy Wingo <wingo@pobox.com>
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 <ludo@gnu.org>
;;;;
-;;;; 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
[-- Attachment #3: Type: text/plain, Size: 26 bytes --]
--
http://wingolog.org/
next prev parent reply other threads:[~2013-01-22 9:51 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-01-12 17:34 read-all ? Andy Wingo
2013-01-12 21:22 ` Ludovic Courtès
2013-01-22 9:15 ` Andy Wingo
2013-01-22 9:51 ` Andy Wingo [this message]
2013-01-22 10:01 ` Ludovic Courtès
2013-01-22 11:04 ` Andy Wingo
2013-01-22 13:32 ` Ludovic Courtès
2013-01-22 15:16 ` Andy Wingo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87k3r5y28j.fsf@pobox.com \
--to=wingo@pobox.com \
--cc=guile-devel@gnu.org \
--cc=ludo@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).