unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: ludo@gnu.org (Ludovic Courtès)
To: Andy Wingo <wingo@pobox.com>
Cc: guile-devel@gnu.org
Subject: Re: read-all ?
Date: Tue, 22 Jan 2013 11:01:35 +0100	[thread overview]
Message-ID: <8738xtk03k.fsf@gnu.org> (raw)
In-Reply-To: <87k3r5y28j.fsf@pobox.com> (Andy Wingo's message of "Tue, 22 Jan 2013 10:51:40 +0100")

Hi!

Andy Wingo <wingo@pobox.com> skribis:

> On Tue 22 Jan 2013 10:15, Andy Wingo <wingo@pobox.com> writes:

[...]

>> 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)?

I would avoid adding a new module, unless there are other things to put
in there.

>> Are the names right?

‘read-all’ doesn’t convey the idea that it’s textual (unlike the R6RS
names).

Perhaps ‘port-contents-as-string’, or ‘read-all-string’, or...?

> +(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))))

As you note, this is fairly inefficient, like ‘get-string-n!’.

Given that ‘string-set!’ is (unduly) costly, I wonder if consing all the
chars and then calling ‘list->string’ wouldn’t be more efficient in time
(it’d be less efficient in space.)

Thanks!

Ludo’.



  reply	other threads:[~2013-01-22 10:01 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
2013-01-22 10:01       ` Ludovic Courtès [this message]
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=8738xtk03k.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=guile-devel@gnu.org \
    --cc=wingo@pobox.com \
    /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).